blob: 874296a41d100d5ac378828e4337481c1c02eb72 [file] [log] [blame]
John Reck0f602f32011-07-07 15:38:43 -07001/*
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
17package com.android.browser;
18
19import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.ObjectAnimator;
22import android.content.Context;
Michael Kolb57059a82012-04-30 14:32:03 -070023import android.content.res.Configuration;
John Reck0f602f32011-07-07 15:38:43 -070024import android.content.res.Resources;
John Reck0f602f32011-07-07 15:38:43 -070025import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
Ben Murdochd51bb572011-08-17 20:42:02 +010028import android.view.ViewStub;
John Reck1cc1d1d2012-09-04 18:13:51 -070029import android.view.accessibility.AccessibilityManager;
John Reck0f602f32011-07-07 15:38:43 -070030import android.view.animation.Animation;
31import android.view.animation.Animation.AnimationListener;
32import android.view.animation.AnimationUtils;
33import android.view.animation.DecelerateInterpolator;
34import android.webkit.WebView;
John Reck0f602f32011-07-07 15:38:43 -070035import android.widget.FrameLayout;
36import android.widget.RelativeLayout;
37
38
39/**
40 * Base class for a title bar used by the browser.
41 */
42public class TitleBar extends RelativeLayout {
43
44 private static final int PROGRESS_MAX = 100;
45 private static final float ANIM_TITLEBAR_DECELERATE = 2.5f;
46
47 private UiController mUiController;
48 private BaseUi mBaseUi;
John Reck2711fab2012-05-18 21:38:59 -070049 private FrameLayout mContentView;
John Reck0f602f32011-07-07 15:38:43 -070050 private PageProgressView mProgress;
John Reck1cc1d1d2012-09-04 18:13:51 -070051 private AccessibilityManager mAccessibilityManager;
John Reck0f602f32011-07-07 15:38:43 -070052
53 private AutologinBar mAutoLogin;
54 private NavigationBarBase mNavBar;
55 private boolean mUseQuickControls;
John Reckef654f12011-07-12 16:42:08 -070056 private SnapshotBar mSnapshotBar;
John Reck0f602f32011-07-07 15:38:43 -070057
58 //state
59 private boolean mShowing;
60 private boolean mInLoad;
61 private boolean mSkipTitleBarAnimations;
62 private Animator mTitleBarAnimator;
John Reck2711fab2012-05-18 21:38:59 -070063 private boolean mIsFixedTitleBar;
John Reck0f602f32011-07-07 15:38:43 -070064
65 public TitleBar(Context context, UiController controller, BaseUi ui,
John Reck2711fab2012-05-18 21:38:59 -070066 FrameLayout contentView) {
John Reck0f602f32011-07-07 15:38:43 -070067 super(context, null);
68 mUiController = controller;
69 mBaseUi = ui;
John Reck2711fab2012-05-18 21:38:59 -070070 mContentView = contentView;
John Reck1cc1d1d2012-09-04 18:13:51 -070071 mAccessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
John Reck0f602f32011-07-07 15:38:43 -070072 initLayout(context);
John Reck0f9aaeb2012-05-23 14:40:19 -070073 setFixedTitleBar();
John Reck0f602f32011-07-07 15:38:43 -070074 }
75
76 private void initLayout(Context context) {
77 LayoutInflater factory = LayoutInflater.from(context);
78 factory.inflate(R.layout.title_bar, this);
79 mProgress = (PageProgressView) findViewById(R.id.progress);
John Reck0f602f32011-07-07 15:38:43 -070080 mNavBar = (NavigationBarBase) findViewById(R.id.taburlbar);
81 mNavBar.setTitleBar(this);
Ben Murdochd51bb572011-08-17 20:42:02 +010082 }
83
84 private void inflateAutoLoginBar() {
85 if (mAutoLogin != null) {
86 return;
87 }
88
89 ViewStub stub = (ViewStub) findViewById(R.id.autologin_stub);
90 mAutoLogin = (AutologinBar) stub.inflate();
91 mAutoLogin.setTitleBar(this);
92 }
93
94 private void inflateSnapshotBar() {
95 if (mSnapshotBar != null) {
96 return;
97 }
98
99 ViewStub stub = (ViewStub) findViewById(R.id.snapshotbar_stub);
100 mSnapshotBar = (SnapshotBar) stub.inflate();
John Reckef654f12011-07-12 16:42:08 -0700101 mSnapshotBar.setTitleBar(this);
John Reck0f602f32011-07-07 15:38:43 -0700102 }
103
Michael Kolb57059a82012-04-30 14:32:03 -0700104 @Override
105 protected void onConfigurationChanged(Configuration config) {
106 super.onConfigurationChanged(config);
John Reck0f9aaeb2012-05-23 14:40:19 -0700107 setFixedTitleBar();
John Reck2711fab2012-05-18 21:38:59 -0700108 }
109
110 @Override
111 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
112 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
113 if (mIsFixedTitleBar) {
114 int margin = getMeasuredHeight() - calculateEmbeddedHeight();
115 mBaseUi.setContentViewMarginTop(-margin);
John Reck0f9aaeb2012-05-23 14:40:19 -0700116 } else {
117 mBaseUi.setContentViewMarginTop(0);
John Reck2711fab2012-05-18 21:38:59 -0700118 }
119 }
120
John Reck0f9aaeb2012-05-23 14:40:19 -0700121 private void setFixedTitleBar() {
122 boolean isFixed = !mUseQuickControls
123 && !mContext.getResources().getBoolean(R.bool.hide_title);
John Reck1cc1d1d2012-09-04 18:13:51 -0700124 isFixed |= mAccessibilityManager.isEnabled();
John Reck2711fab2012-05-18 21:38:59 -0700125 // If getParent() returns null, we are initializing
126 ViewGroup parent = (ViewGroup)getParent();
127 if (mIsFixedTitleBar == isFixed && parent != null) return;
128 mIsFixedTitleBar = isFixed;
129 setSkipTitleBarAnimations(true);
130 show();
131 setSkipTitleBarAnimations(false);
132 if (parent != null) {
133 parent.removeView(this);
134 }
135 if (mIsFixedTitleBar) {
136 mBaseUi.addFixedTitleBar(this);
137 } else {
138 mContentView.addView(this, makeLayoutParams());
139 mBaseUi.setContentViewMarginTop(0);
Michael Kolb57059a82012-04-30 14:32:03 -0700140 }
141 }
142
John Reck0f602f32011-07-07 15:38:43 -0700143 public BaseUi getUi() {
144 return mBaseUi;
145 }
146
147 public UiController getUiController() {
148 return mUiController;
149 }
150
151 public void setUseQuickControls(boolean use) {
152 mUseQuickControls = use;
John Reck0f9aaeb2012-05-23 14:40:19 -0700153 setFixedTitleBar();
Michael Kolb4923c222012-04-02 16:18:36 -0700154 if (use) {
155 this.setVisibility(View.GONE);
156 } else {
157 this.setVisibility(View.VISIBLE);
158 }
John Reck0f602f32011-07-07 15:38:43 -0700159 }
160
161 void setShowProgressOnly(boolean progress) {
John Reckef654f12011-07-12 16:42:08 -0700162 if (progress && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700163 mNavBar.setVisibility(View.GONE);
164 } else {
165 mNavBar.setVisibility(View.VISIBLE);
166 }
167 }
168
169 void setSkipTitleBarAnimations(boolean skip) {
170 mSkipTitleBarAnimations = skip;
171 }
172
173 void setupTitleBarAnimator(Animator animator) {
174 Resources res = mContext.getResources();
175 int duration = res.getInteger(R.integer.titlebar_animation_duration);
176 animator.setInterpolator(new DecelerateInterpolator(
177 ANIM_TITLEBAR_DECELERATE));
178 animator.setDuration(duration);
179 }
180
181 void show() {
John Reck2711fab2012-05-18 21:38:59 -0700182 cancelTitleBarAnimation(false);
183 if (mUseQuickControls || mSkipTitleBarAnimations) {
Michael Kolb4923c222012-04-02 16:18:36 -0700184 this.setVisibility(View.VISIBLE);
185 this.setTranslationY(0);
John Reck0f602f32011-07-07 15:38:43 -0700186 } else {
John Reck2711fab2012-05-18 21:38:59 -0700187 int visibleHeight = getVisibleTitleHeight();
188 float startPos = (-getEmbeddedHeight() + visibleHeight);
189 if (getTranslationY() != 0) {
190 startPos = Math.max(startPos, getTranslationY());
John Reck0f602f32011-07-07 15:38:43 -0700191 }
John Reck2711fab2012-05-18 21:38:59 -0700192 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
193 "translationY",
194 startPos, 0);
195 setupTitleBarAnimator(mTitleBarAnimator);
196 mTitleBarAnimator.start();
John Reck0f602f32011-07-07 15:38:43 -0700197 }
198 mShowing = true;
199 }
200
201 void hide() {
202 if (mUseQuickControls) {
Michael Kolb4923c222012-04-02 16:18:36 -0700203 this.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700204 } else {
John Reck2711fab2012-05-18 21:38:59 -0700205 if (mIsFixedTitleBar) return;
John Reck0f602f32011-07-07 15:38:43 -0700206 if (!mSkipTitleBarAnimations) {
207 cancelTitleBarAnimation(false);
208 int visibleHeight = getVisibleTitleHeight();
209 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
210 "translationY", getTranslationY(),
211 (-getEmbeddedHeight() + visibleHeight));
212 mTitleBarAnimator.addListener(mHideTileBarAnimatorListener);
213 setupTitleBarAnimator(mTitleBarAnimator);
214 mTitleBarAnimator.start();
215 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700216 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700217 }
218 }
219 mShowing = false;
220 }
221
222 boolean isShowing() {
223 return mShowing;
224 }
225
226 void cancelTitleBarAnimation(boolean reset) {
227 if (mTitleBarAnimator != null) {
228 mTitleBarAnimator.cancel();
229 mTitleBarAnimator = null;
230 }
231 if (reset) {
232 setTranslationY(0);
233 }
234 }
235
236 private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() {
237
John Reck0f602f32011-07-07 15:38:43 -0700238 @Override
239 public void onAnimationStart(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700240 }
241
242 @Override
243 public void onAnimationRepeat(Animator animation) {
244 }
245
246 @Override
247 public void onAnimationEnd(Animator animation) {
Michael Kolb4923c222012-04-02 16:18:36 -0700248 // update position
249 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700250 }
251
252 @Override
253 public void onAnimationCancel(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700254 }
255 };
256
257 private int getVisibleTitleHeight() {
258 Tab tab = mBaseUi.getActiveTab();
259 WebView webview = tab != null ? tab.getWebView() : null;
260 return webview != null ? webview.getVisibleTitleHeight() : 0;
261 }
262
263 /**
264 * Update the progress, from 0 to 100.
265 */
266 public void setProgress(int newProgress) {
267 if (newProgress >= PROGRESS_MAX) {
268 mProgress.setProgress(PageProgressView.MAX_PROGRESS);
269 mProgress.setVisibility(View.GONE);
270 mInLoad = false;
271 mNavBar.onProgressStopped();
272 // check if needs to be hidden
John Reckef654f12011-07-12 16:42:08 -0700273 if (!isEditingUrl() && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700274 if (mUseQuickControls) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700275 hide();
John Reckbc82ec92012-04-19 13:12:35 -0700276 } else {
277 mBaseUi.showTitleBarForDuration();
John Reck0f602f32011-07-07 15:38:43 -0700278 }
279 }
280 } else {
281 if (!mInLoad) {
282 mProgress.setVisibility(View.VISIBLE);
283 mInLoad = true;
284 mNavBar.onProgressStarted();
285 }
286 mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
287 / PROGRESS_MAX);
Michael Kolbe8a82332012-04-25 14:14:26 -0700288 if (mUseQuickControls && !isEditingUrl()) {
289 setShowProgressOnly(true);
290 }
John Reck0f602f32011-07-07 15:38:43 -0700291 if (!mShowing) {
John Reck0f602f32011-07-07 15:38:43 -0700292 show();
293 }
294 }
295 }
296
297 public int getEmbeddedHeight() {
John Reck2711fab2012-05-18 21:38:59 -0700298 if (mUseQuickControls || mIsFixedTitleBar) return 0;
299 return calculateEmbeddedHeight();
300 }
301
302 private int calculateEmbeddedHeight() {
John Reck0f602f32011-07-07 15:38:43 -0700303 int height = mNavBar.getHeight();
Ben Murdochd51bb572011-08-17 20:42:02 +0100304 if (mAutoLogin != null && mAutoLogin.getVisibility() == View.VISIBLE) {
John Reck0f602f32011-07-07 15:38:43 -0700305 height += mAutoLogin.getHeight();
306 }
307 return height;
308 }
309
310 public void updateAutoLogin(Tab tab, boolean animate) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100311 if (mAutoLogin == null) {
312 if (tab.getDeviceAccountLogin() == null) {
313 return;
314 }
315 inflateAutoLoginBar();
316 }
John Reck0f602f32011-07-07 15:38:43 -0700317 mAutoLogin.updateAutoLogin(tab, animate);
318 }
319
320 public void showAutoLogin(boolean animate) {
321 if (mUseQuickControls) {
322 mBaseUi.showTitleBar();
323 }
Ben Murdochd51bb572011-08-17 20:42:02 +0100324 if (mAutoLogin == null) {
325 inflateAutoLoginBar();
326 }
John Reck0f602f32011-07-07 15:38:43 -0700327 mAutoLogin.setVisibility(View.VISIBLE);
328 if (animate) {
329 mAutoLogin.startAnimation(AnimationUtils.loadAnimation(
330 getContext(), R.anim.autologin_enter));
331 }
332 }
333
334 public void hideAutoLogin(boolean animate) {
335 if (mUseQuickControls) {
336 mBaseUi.hideTitleBar();
337 mAutoLogin.setVisibility(View.GONE);
338 mBaseUi.refreshWebView();
339 } else {
340 if (animate) {
341 Animation anim = AnimationUtils.loadAnimation(getContext(),
342 R.anim.autologin_exit);
343 anim.setAnimationListener(new AnimationListener() {
344 @Override
345 public void onAnimationEnd(Animation a) {
346 mAutoLogin.setVisibility(View.GONE);
347 mBaseUi.refreshWebView();
348 }
349
350 @Override
351 public void onAnimationStart(Animation a) {
352 }
353
354 @Override
355 public void onAnimationRepeat(Animation a) {
356 }
357 });
358 mAutoLogin.startAnimation(anim);
359 } else if (mAutoLogin.getAnimation() == null) {
360 mAutoLogin.setVisibility(View.GONE);
361 mBaseUi.refreshWebView();
362 }
363 }
364 }
365
John Reckef654f12011-07-12 16:42:08 -0700366 public boolean wantsToBeVisible() {
367 return inAutoLogin()
Ben Murdochd51bb572011-08-17 20:42:02 +0100368 || (mSnapshotBar != null && mSnapshotBar.getVisibility() == View.VISIBLE
John Reckef654f12011-07-12 16:42:08 -0700369 && mSnapshotBar.isAnimating());
370 }
371
372 private boolean inAutoLogin() {
Ben Murdochd51bb572011-08-17 20:42:02 +0100373 return mAutoLogin != null && mAutoLogin.getVisibility() == View.VISIBLE;
John Reck0f602f32011-07-07 15:38:43 -0700374 }
375
376 public boolean isEditingUrl() {
377 return mNavBar.isEditingUrl();
378 }
379
380 public WebView getCurrentWebView() {
381 Tab t = mBaseUi.getActiveTab();
382 if (t != null) {
383 return t.getWebView();
384 } else {
385 return null;
386 }
387 }
388
389 public PageProgressView getProgressView() {
390 return mProgress;
391 }
392
393 public NavigationBarBase getNavigationBar() {
394 return mNavBar;
395 }
396
397 public boolean useQuickControls() {
398 return mUseQuickControls;
399 }
400
401 public boolean isInLoad() {
402 return mInLoad;
403 }
404
405 private ViewGroup.LayoutParams makeLayoutParams() {
Michael Kolb4923c222012-04-02 16:18:36 -0700406 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
407 LayoutParams.WRAP_CONTENT);
John Reck0f602f32011-07-07 15:38:43 -0700408 }
409
410 @Override
411 public View focusSearch(View focused, int dir) {
John Reckfcb60952012-05-30 09:45:28 -0700412 WebView web = getCurrentWebView();
John Reckd70419a2012-05-30 16:09:17 -0700413 if (FOCUS_DOWN == dir && hasFocus() && web != null
John Reckfcb60952012-05-30 09:45:28 -0700414 && web.hasFocusable() && web.getParent() != null) {
415 return web;
John Reck0f602f32011-07-07 15:38:43 -0700416 }
417 return super.focusSearch(focused, dir);
418 }
419
John Reckef654f12011-07-12 16:42:08 -0700420 public void onTabDataChanged(Tab tab) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100421 if (mSnapshotBar != null) {
422 mSnapshotBar.onTabDataChanged(tab);
423 }
424
John Reckef654f12011-07-12 16:42:08 -0700425 if (tab.isSnapshot()) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100426 inflateSnapshotBar();
John Reckef654f12011-07-12 16:42:08 -0700427 mSnapshotBar.setVisibility(VISIBLE);
428 mNavBar.setVisibility(GONE);
429 } else {
Ben Murdochd51bb572011-08-17 20:42:02 +0100430 if (mSnapshotBar != null) {
431 mSnapshotBar.setVisibility(GONE);
432 }
John Reckef654f12011-07-12 16:42:08 -0700433 mNavBar.setVisibility(VISIBLE);
434 }
435 }
436
Michael Kolb4923c222012-04-02 16:18:36 -0700437 public void onScrollChanged() {
John Reck2711fab2012-05-18 21:38:59 -0700438 if (!mShowing && !mIsFixedTitleBar) {
Michael Kolb4923c222012-04-02 16:18:36 -0700439 setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight());
440 }
441 }
442
John Reck1cc1d1d2012-09-04 18:13:51 -0700443 public void onResume() {
444 setFixedTitleBar();
445 }
446
John Reck0f602f32011-07-07 15:38:43 -0700447}