John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
| 21 | import android.animation.ObjectAnimator; |
| 22 | import android.content.Context; |
| 23 | import android.content.res.Resources; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 27 | import android.view.ViewStub; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 28 | import android.view.animation.Animation; |
| 29 | import android.view.animation.Animation.AnimationListener; |
| 30 | import android.view.animation.AnimationUtils; |
| 31 | import android.view.animation.DecelerateInterpolator; |
| 32 | import android.webkit.WebView; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 33 | import android.widget.FrameLayout; |
| 34 | import android.widget.RelativeLayout; |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Base class for a title bar used by the browser. |
| 39 | */ |
| 40 | public class TitleBar extends RelativeLayout { |
| 41 | |
| 42 | private static final int PROGRESS_MAX = 100; |
| 43 | private static final float ANIM_TITLEBAR_DECELERATE = 2.5f; |
| 44 | |
| 45 | private UiController mUiController; |
| 46 | private BaseUi mBaseUi; |
| 47 | private FrameLayout mParent; |
| 48 | private PageProgressView mProgress; |
| 49 | |
| 50 | private AutologinBar mAutoLogin; |
| 51 | private NavigationBarBase mNavBar; |
| 52 | private boolean mUseQuickControls; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 53 | private SnapshotBar mSnapshotBar; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 54 | |
| 55 | //state |
| 56 | private boolean mShowing; |
| 57 | private boolean mInLoad; |
| 58 | private boolean mSkipTitleBarAnimations; |
| 59 | private Animator mTitleBarAnimator; |
| 60 | |
| 61 | public TitleBar(Context context, UiController controller, BaseUi ui, |
| 62 | FrameLayout parent) { |
| 63 | super(context, null); |
| 64 | mUiController = controller; |
| 65 | mBaseUi = ui; |
| 66 | mParent = parent; |
| 67 | initLayout(context); |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 68 | mParent.addView(this, makeLayoutParams()); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private void initLayout(Context context) { |
| 72 | LayoutInflater factory = LayoutInflater.from(context); |
| 73 | factory.inflate(R.layout.title_bar, this); |
| 74 | mProgress = (PageProgressView) findViewById(R.id.progress); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 75 | mNavBar = (NavigationBarBase) findViewById(R.id.taburlbar); |
| 76 | mNavBar.setTitleBar(this); |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | private void inflateAutoLoginBar() { |
| 80 | if (mAutoLogin != null) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | ViewStub stub = (ViewStub) findViewById(R.id.autologin_stub); |
| 85 | mAutoLogin = (AutologinBar) stub.inflate(); |
| 86 | mAutoLogin.setTitleBar(this); |
| 87 | } |
| 88 | |
| 89 | private void inflateSnapshotBar() { |
| 90 | if (mSnapshotBar != null) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | ViewStub stub = (ViewStub) findViewById(R.id.snapshotbar_stub); |
| 95 | mSnapshotBar = (SnapshotBar) stub.inflate(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 96 | mSnapshotBar.setTitleBar(this); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | public BaseUi getUi() { |
| 100 | return mBaseUi; |
| 101 | } |
| 102 | |
| 103 | public UiController getUiController() { |
| 104 | return mUiController; |
| 105 | } |
| 106 | |
| 107 | public void setUseQuickControls(boolean use) { |
| 108 | mUseQuickControls = use; |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 109 | if (use) { |
| 110 | this.setVisibility(View.GONE); |
| 111 | } else { |
| 112 | this.setVisibility(View.VISIBLE); |
| 113 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void setShowProgressOnly(boolean progress) { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 117 | if (progress && !wantsToBeVisible()) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 118 | mNavBar.setVisibility(View.GONE); |
| 119 | } else { |
| 120 | mNavBar.setVisibility(View.VISIBLE); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void setSkipTitleBarAnimations(boolean skip) { |
| 125 | mSkipTitleBarAnimations = skip; |
| 126 | } |
| 127 | |
| 128 | void setupTitleBarAnimator(Animator animator) { |
| 129 | Resources res = mContext.getResources(); |
| 130 | int duration = res.getInteger(R.integer.titlebar_animation_duration); |
| 131 | animator.setInterpolator(new DecelerateInterpolator( |
| 132 | ANIM_TITLEBAR_DECELERATE)); |
| 133 | animator.setDuration(duration); |
| 134 | } |
| 135 | |
| 136 | void show() { |
| 137 | if (mUseQuickControls) { |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 138 | this.setVisibility(View.VISIBLE); |
| 139 | this.setTranslationY(0); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 140 | } else { |
| 141 | if (!mSkipTitleBarAnimations) { |
| 142 | cancelTitleBarAnimation(false); |
| 143 | int visibleHeight = getVisibleTitleHeight(); |
| 144 | float startPos = (-getEmbeddedHeight() + visibleHeight); |
| 145 | if (getTranslationY() != 0) { |
| 146 | startPos = Math.max(startPos, getTranslationY()); |
| 147 | } |
| 148 | mTitleBarAnimator = ObjectAnimator.ofFloat(this, |
| 149 | "translationY", |
| 150 | startPos, 0); |
| 151 | setupTitleBarAnimator(mTitleBarAnimator); |
| 152 | mTitleBarAnimator.start(); |
| 153 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 154 | } |
| 155 | mShowing = true; |
| 156 | } |
| 157 | |
| 158 | void hide() { |
| 159 | if (mUseQuickControls) { |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 160 | this.setVisibility(View.GONE); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 161 | } else { |
| 162 | if (!mSkipTitleBarAnimations) { |
| 163 | cancelTitleBarAnimation(false); |
| 164 | int visibleHeight = getVisibleTitleHeight(); |
| 165 | mTitleBarAnimator = ObjectAnimator.ofFloat(this, |
| 166 | "translationY", getTranslationY(), |
| 167 | (-getEmbeddedHeight() + visibleHeight)); |
| 168 | mTitleBarAnimator.addListener(mHideTileBarAnimatorListener); |
| 169 | setupTitleBarAnimator(mTitleBarAnimator); |
| 170 | mTitleBarAnimator.start(); |
| 171 | } else { |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 172 | onScrollChanged(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | mShowing = false; |
| 176 | } |
| 177 | |
| 178 | boolean isShowing() { |
| 179 | return mShowing; |
| 180 | } |
| 181 | |
| 182 | void cancelTitleBarAnimation(boolean reset) { |
| 183 | if (mTitleBarAnimator != null) { |
| 184 | mTitleBarAnimator.cancel(); |
| 185 | mTitleBarAnimator = null; |
| 186 | } |
| 187 | if (reset) { |
| 188 | setTranslationY(0); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() { |
| 193 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 194 | @Override |
| 195 | public void onAnimationStart(Animator animation) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void onAnimationRepeat(Animator animation) { |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void onAnimationEnd(Animator animation) { |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 204 | // update position |
| 205 | onScrollChanged(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void onAnimationCancel(Animator animation) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 210 | } |
| 211 | }; |
| 212 | |
| 213 | private int getVisibleTitleHeight() { |
| 214 | Tab tab = mBaseUi.getActiveTab(); |
| 215 | WebView webview = tab != null ? tab.getWebView() : null; |
| 216 | return webview != null ? webview.getVisibleTitleHeight() : 0; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Update the progress, from 0 to 100. |
| 221 | */ |
| 222 | public void setProgress(int newProgress) { |
| 223 | if (newProgress >= PROGRESS_MAX) { |
| 224 | mProgress.setProgress(PageProgressView.MAX_PROGRESS); |
| 225 | mProgress.setVisibility(View.GONE); |
| 226 | mInLoad = false; |
| 227 | mNavBar.onProgressStopped(); |
| 228 | // check if needs to be hidden |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 229 | if (!isEditingUrl() && !wantsToBeVisible()) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 230 | if (mUseQuickControls) { |
Michael Kolb | e8a8233 | 2012-04-25 14:14:26 -0700 | [diff] [blame] | 231 | hide(); |
John Reck | bc82ec9 | 2012-04-19 13:12:35 -0700 | [diff] [blame] | 232 | } else { |
| 233 | mBaseUi.showTitleBarForDuration(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } else { |
| 237 | if (!mInLoad) { |
| 238 | mProgress.setVisibility(View.VISIBLE); |
| 239 | mInLoad = true; |
| 240 | mNavBar.onProgressStarted(); |
| 241 | } |
| 242 | mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS |
| 243 | / PROGRESS_MAX); |
Michael Kolb | e8a8233 | 2012-04-25 14:14:26 -0700 | [diff] [blame] | 244 | if (mUseQuickControls && !isEditingUrl()) { |
| 245 | setShowProgressOnly(true); |
| 246 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 247 | if (!mShowing) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 248 | show(); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | public int getEmbeddedHeight() { |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 254 | if (mUseQuickControls) return 0; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 255 | int height = mNavBar.getHeight(); |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 256 | if (mAutoLogin != null && mAutoLogin.getVisibility() == View.VISIBLE) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 257 | height += mAutoLogin.getHeight(); |
| 258 | } |
| 259 | return height; |
| 260 | } |
| 261 | |
| 262 | public void updateAutoLogin(Tab tab, boolean animate) { |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 263 | if (mAutoLogin == null) { |
| 264 | if (tab.getDeviceAccountLogin() == null) { |
| 265 | return; |
| 266 | } |
| 267 | inflateAutoLoginBar(); |
| 268 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 269 | mAutoLogin.updateAutoLogin(tab, animate); |
| 270 | } |
| 271 | |
| 272 | public void showAutoLogin(boolean animate) { |
| 273 | if (mUseQuickControls) { |
| 274 | mBaseUi.showTitleBar(); |
| 275 | } |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 276 | if (mAutoLogin == null) { |
| 277 | inflateAutoLoginBar(); |
| 278 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 279 | mAutoLogin.setVisibility(View.VISIBLE); |
| 280 | if (animate) { |
| 281 | mAutoLogin.startAnimation(AnimationUtils.loadAnimation( |
| 282 | getContext(), R.anim.autologin_enter)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | public void hideAutoLogin(boolean animate) { |
| 287 | if (mUseQuickControls) { |
| 288 | mBaseUi.hideTitleBar(); |
| 289 | mAutoLogin.setVisibility(View.GONE); |
| 290 | mBaseUi.refreshWebView(); |
| 291 | } else { |
| 292 | if (animate) { |
| 293 | Animation anim = AnimationUtils.loadAnimation(getContext(), |
| 294 | R.anim.autologin_exit); |
| 295 | anim.setAnimationListener(new AnimationListener() { |
| 296 | @Override |
| 297 | public void onAnimationEnd(Animation a) { |
| 298 | mAutoLogin.setVisibility(View.GONE); |
| 299 | mBaseUi.refreshWebView(); |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void onAnimationStart(Animation a) { |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public void onAnimationRepeat(Animation a) { |
| 308 | } |
| 309 | }); |
| 310 | mAutoLogin.startAnimation(anim); |
| 311 | } else if (mAutoLogin.getAnimation() == null) { |
| 312 | mAutoLogin.setVisibility(View.GONE); |
| 313 | mBaseUi.refreshWebView(); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 318 | public boolean wantsToBeVisible() { |
| 319 | return inAutoLogin() |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 320 | || (mSnapshotBar != null && mSnapshotBar.getVisibility() == View.VISIBLE |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 321 | && mSnapshotBar.isAnimating()); |
| 322 | } |
| 323 | |
| 324 | private boolean inAutoLogin() { |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 325 | return mAutoLogin != null && mAutoLogin.getVisibility() == View.VISIBLE; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | public boolean isEditingUrl() { |
| 329 | return mNavBar.isEditingUrl(); |
| 330 | } |
| 331 | |
| 332 | public WebView getCurrentWebView() { |
| 333 | Tab t = mBaseUi.getActiveTab(); |
| 334 | if (t != null) { |
| 335 | return t.getWebView(); |
| 336 | } else { |
| 337 | return null; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | public PageProgressView getProgressView() { |
| 342 | return mProgress; |
| 343 | } |
| 344 | |
| 345 | public NavigationBarBase getNavigationBar() { |
| 346 | return mNavBar; |
| 347 | } |
| 348 | |
| 349 | public boolean useQuickControls() { |
| 350 | return mUseQuickControls; |
| 351 | } |
| 352 | |
| 353 | public boolean isInLoad() { |
| 354 | return mInLoad; |
| 355 | } |
| 356 | |
| 357 | private ViewGroup.LayoutParams makeLayoutParams() { |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 358 | return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, |
| 359 | LayoutParams.WRAP_CONTENT); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public View focusSearch(View focused, int dir) { |
| 364 | if (FOCUS_DOWN == dir && hasFocus()) { |
| 365 | return getCurrentWebView(); |
| 366 | } |
| 367 | return super.focusSearch(focused, dir); |
| 368 | } |
| 369 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 370 | public void onTabDataChanged(Tab tab) { |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 371 | if (mSnapshotBar != null) { |
| 372 | mSnapshotBar.onTabDataChanged(tab); |
| 373 | } |
| 374 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 375 | if (tab.isSnapshot()) { |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 376 | inflateSnapshotBar(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 377 | mSnapshotBar.setVisibility(VISIBLE); |
| 378 | mNavBar.setVisibility(GONE); |
| 379 | } else { |
Ben Murdoch | d51bb57 | 2011-08-17 20:42:02 +0100 | [diff] [blame] | 380 | if (mSnapshotBar != null) { |
| 381 | mSnapshotBar.setVisibility(GONE); |
| 382 | } |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 383 | mNavBar.setVisibility(VISIBLE); |
| 384 | } |
| 385 | } |
| 386 | |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 387 | public void onScrollChanged() { |
| 388 | if (!mShowing) { |
| 389 | setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight()); |
| 390 | } |
| 391 | } |
| 392 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 393 | } |