blob: 1ea4dad5e82b858bbf9ae6eb3624e0fcefc778dc [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
John Reck0f602f32011-07-07 15:38:43 -070018
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;
Panos Thomas752ce592014-10-16 13:00:35 -070025import android.graphics.Rect;
John Reck0f602f32011-07-07 15:38:43 -070026import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
Ben Murdochd51bb572011-08-17 20:42:02 +010029import android.view.ViewStub;
John Reck1cc1d1d2012-09-04 18:13:51 -070030import android.view.accessibility.AccessibilityManager;
John Reck0f602f32011-07-07 15:38:43 -070031import android.view.animation.Animation;
32import android.view.animation.Animation.AnimationListener;
33import android.view.animation.AnimationUtils;
34import android.view.animation.DecelerateInterpolator;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080035import org.codeaurora.swe.WebView;
36
Bijan Amirzada41242f22014-03-21 12:12:18 -070037import com.android.browser.R;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080038
John Reck0f602f32011-07-07 15:38:43 -070039import android.widget.FrameLayout;
40import android.widget.RelativeLayout;
41
42
43/**
44 * Base class for a title bar used by the browser.
45 */
46public class TitleBar extends RelativeLayout {
47
48 private static final int PROGRESS_MAX = 100;
49 private static final float ANIM_TITLEBAR_DECELERATE = 2.5f;
50
51 private UiController mUiController;
52 private BaseUi mBaseUi;
John Reck2711fab2012-05-18 21:38:59 -070053 private FrameLayout mContentView;
John Reck0f602f32011-07-07 15:38:43 -070054 private PageProgressView mProgress;
John Reck1cc1d1d2012-09-04 18:13:51 -070055 private AccessibilityManager mAccessibilityManager;
John Reck0f602f32011-07-07 15:38:43 -070056
John Reck0f602f32011-07-07 15:38:43 -070057 private NavigationBarBase mNavBar;
John Reckef654f12011-07-12 16:42:08 -070058 private SnapshotBar mSnapshotBar;
John Reck0f602f32011-07-07 15:38:43 -070059
60 //state
61 private boolean mShowing;
62 private boolean mInLoad;
63 private boolean mSkipTitleBarAnimations;
64 private Animator mTitleBarAnimator;
John Reck2711fab2012-05-18 21:38:59 -070065 private boolean mIsFixedTitleBar;
John Reck0f602f32011-07-07 15:38:43 -070066
67 public TitleBar(Context context, UiController controller, BaseUi ui,
John Reck2711fab2012-05-18 21:38:59 -070068 FrameLayout contentView) {
John Reck0f602f32011-07-07 15:38:43 -070069 super(context, null);
70 mUiController = controller;
71 mBaseUi = ui;
John Reck2711fab2012-05-18 21:38:59 -070072 mContentView = contentView;
John Reck1cc1d1d2012-09-04 18:13:51 -070073 mAccessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
John Reck0f602f32011-07-07 15:38:43 -070074 initLayout(context);
John Reck0f9aaeb2012-05-23 14:40:19 -070075 setFixedTitleBar();
John Reck0f602f32011-07-07 15:38:43 -070076 }
77
78 private void initLayout(Context context) {
79 LayoutInflater factory = LayoutInflater.from(context);
80 factory.inflate(R.layout.title_bar, this);
81 mProgress = (PageProgressView) findViewById(R.id.progress);
John Reck0f602f32011-07-07 15:38:43 -070082 mNavBar = (NavigationBarBase) findViewById(R.id.taburlbar);
83 mNavBar.setTitleBar(this);
Ben Murdochd51bb572011-08-17 20:42:02 +010084 }
85
Ben Murdochd51bb572011-08-17 20:42:02 +010086 private void inflateSnapshotBar() {
87 if (mSnapshotBar != null) {
88 return;
89 }
90
91 ViewStub stub = (ViewStub) findViewById(R.id.snapshotbar_stub);
92 mSnapshotBar = (SnapshotBar) stub.inflate();
John Reckef654f12011-07-12 16:42:08 -070093 mSnapshotBar.setTitleBar(this);
John Reck0f602f32011-07-07 15:38:43 -070094 }
95
Michael Kolb57059a82012-04-30 14:32:03 -070096 @Override
97 protected void onConfigurationChanged(Configuration config) {
98 super.onConfigurationChanged(config);
John Reck0f9aaeb2012-05-23 14:40:19 -070099 setFixedTitleBar();
John Reck2711fab2012-05-18 21:38:59 -0700100 }
101
102 @Override
103 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
104 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Vivek Sekhar60eb9802014-07-21 19:13:33 -0700105 if (mIsFixedTitleBar) {
John Reck2711fab2012-05-18 21:38:59 -0700106 int margin = getMeasuredHeight() - calculateEmbeddedHeight();
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700107 if (!isEditingUrl())
108 mBaseUi.setContentViewMarginTop(-margin);
John Reck0f9aaeb2012-05-23 14:40:19 -0700109 } else {
110 mBaseUi.setContentViewMarginTop(0);
John Reck2711fab2012-05-18 21:38:59 -0700111 }
112 }
113
John Reck0f9aaeb2012-05-23 14:40:19 -0700114 private void setFixedTitleBar() {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700115 boolean isFixed = !getContext().getResources().getBoolean(R.bool.hide_title);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700116
John Reck1cc1d1d2012-09-04 18:13:51 -0700117 isFixed |= mAccessibilityManager.isEnabled();
John Reck2711fab2012-05-18 21:38:59 -0700118 // If getParent() returns null, we are initializing
119 ViewGroup parent = (ViewGroup)getParent();
120 if (mIsFixedTitleBar == isFixed && parent != null) return;
121 mIsFixedTitleBar = isFixed;
122 setSkipTitleBarAnimations(true);
123 show();
124 setSkipTitleBarAnimations(false);
125 if (parent != null) {
126 parent.removeView(this);
127 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700128 if (mIsFixedTitleBar) {
John Reck2711fab2012-05-18 21:38:59 -0700129 mBaseUi.addFixedTitleBar(this);
130 } else {
131 mContentView.addView(this, makeLayoutParams());
132 mBaseUi.setContentViewMarginTop(0);
Michael Kolb57059a82012-04-30 14:32:03 -0700133 }
134 }
135
John Reck0f602f32011-07-07 15:38:43 -0700136 public BaseUi getUi() {
137 return mBaseUi;
138 }
139
140 public UiController getUiController() {
141 return mUiController;
142 }
143
John Reck0f602f32011-07-07 15:38:43 -0700144 void setShowProgressOnly(boolean progress) {
John Reckef654f12011-07-12 16:42:08 -0700145 if (progress && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700146 mNavBar.setVisibility(View.GONE);
147 } else {
148 mNavBar.setVisibility(View.VISIBLE);
149 }
150 }
151
152 void setSkipTitleBarAnimations(boolean skip) {
153 mSkipTitleBarAnimations = skip;
154 }
155
156 void setupTitleBarAnimator(Animator animator) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800157 Resources res = getContext().getResources();
John Reck0f602f32011-07-07 15:38:43 -0700158 int duration = res.getInteger(R.integer.titlebar_animation_duration);
159 animator.setInterpolator(new DecelerateInterpolator(
160 ANIM_TITLEBAR_DECELERATE));
161 animator.setDuration(duration);
162 }
163
Tarun Nainani8eb00912014-07-17 12:28:32 -0700164 //Disable stock autohide behavior in favor of top controls
165 private static final boolean bOldStyleAutoHideDisabled = true;
John Reck0f602f32011-07-07 15:38:43 -0700166 void show() {
John Reck2711fab2012-05-18 21:38:59 -0700167 cancelTitleBarAnimation(false);
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700168 if (mSkipTitleBarAnimations) {
Michael Kolb4923c222012-04-02 16:18:36 -0700169 this.setVisibility(View.VISIBLE);
170 this.setTranslationY(0);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700171 hideTopControls();
172 } else if (!bOldStyleAutoHideDisabled) {
John Reck2711fab2012-05-18 21:38:59 -0700173 int visibleHeight = getVisibleTitleHeight();
174 float startPos = (-getEmbeddedHeight() + visibleHeight);
175 if (getTranslationY() != 0) {
176 startPos = Math.max(startPos, getTranslationY());
John Reck0f602f32011-07-07 15:38:43 -0700177 }
John Reck2711fab2012-05-18 21:38:59 -0700178 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
179 "translationY",
180 startPos, 0);
181 setupTitleBarAnimator(mTitleBarAnimator);
182 mTitleBarAnimator.start();
John Reck0f602f32011-07-07 15:38:43 -0700183 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700184
John Reck0f602f32011-07-07 15:38:43 -0700185 mShowing = true;
186 }
187
188 void hide() {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700189 if (mIsFixedTitleBar || bOldStyleAutoHideDisabled) return;
190 if (!mSkipTitleBarAnimations) {
191 cancelTitleBarAnimation(false);
192 int visibleHeight = getVisibleTitleHeight();
193 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
194 "translationY", getTranslationY(),
195 (-getEmbeddedHeight() + visibleHeight));
196 mTitleBarAnimator.addListener(mHideTileBarAnimatorListener);
197 setupTitleBarAnimator(mTitleBarAnimator);
198 mTitleBarAnimator.start();
John Reck0f602f32011-07-07 15:38:43 -0700199 } else {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700200 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700201 }
202 mShowing = false;
203 }
204
205 boolean isShowing() {
206 return mShowing;
207 }
208
209 void cancelTitleBarAnimation(boolean reset) {
210 if (mTitleBarAnimator != null) {
211 mTitleBarAnimator.cancel();
212 mTitleBarAnimator = null;
213 }
214 if (reset) {
215 setTranslationY(0);
216 }
217 }
218
219 private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() {
220
John Reck0f602f32011-07-07 15:38:43 -0700221 @Override
222 public void onAnimationStart(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700223 }
224
225 @Override
226 public void onAnimationRepeat(Animator animation) {
227 }
228
229 @Override
230 public void onAnimationEnd(Animator animation) {
Michael Kolb4923c222012-04-02 16:18:36 -0700231 // update position
232 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700233 }
234
235 @Override
236 public void onAnimationCancel(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700237 }
238 };
239
240 private int getVisibleTitleHeight() {
241 Tab tab = mBaseUi.getActiveTab();
242 WebView webview = tab != null ? tab.getWebView() : null;
243 return webview != null ? webview.getVisibleTitleHeight() : 0;
244 }
245
Tarun Nainani8eb00912014-07-17 12:28:32 -0700246 private void hideTopControls() {
247 Tab tab = mBaseUi.getActiveTab();
248 WebView view = tab != null ? tab.getWebView() : null;
249 if (view != null)
250 view.updateTopControls(true, false, true);
251 }
252
253 private void showTopControls() {
254 Tab tab = mBaseUi.getActiveTab();
255 WebView view = tab != null ? tab.getWebView() : null;
256 if (view != null)
257 view.updateTopControls(false, true, true);
258 }
259
260 private void enableTopControls() {
261 Tab tab = mBaseUi.getActiveTab();
262 WebView view = tab != null ? tab.getWebView() : null;
263 if (view != null)
264 view.updateTopControls(true, true, true);
265 }
266
267
John Reck0f602f32011-07-07 15:38:43 -0700268 /**
269 * Update the progress, from 0 to 100.
270 */
271 public void setProgress(int newProgress) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700272 Tab tab = mBaseUi.getActiveTab();
273 WebView view = tab != null ? tab.getWebView() : null;
274
John Reck0f602f32011-07-07 15:38:43 -0700275 if (newProgress >= PROGRESS_MAX) {
276 mProgress.setProgress(PageProgressView.MAX_PROGRESS);
277 mProgress.setVisibility(View.GONE);
278 mInLoad = false;
279 mNavBar.onProgressStopped();
280 // check if needs to be hidden
John Reckef654f12011-07-12 16:42:08 -0700281 if (!isEditingUrl() && !wantsToBeVisible()) {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700282 mBaseUi.showTitleBarForDuration();
John Reck0f602f32011-07-07 15:38:43 -0700283 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700284
285 //onPageFinished
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700286 enableTopControls();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700287
John Reck0f602f32011-07-07 15:38:43 -0700288 } else {
289 if (!mInLoad) {
290 mProgress.setVisibility(View.VISIBLE);
291 mInLoad = true;
292 mNavBar.onProgressStarted();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700293
294 //onPageStarted
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700295 showTopControls();
John Reck0f602f32011-07-07 15:38:43 -0700296 }
297 mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
298 / PROGRESS_MAX);
299 if (!mShowing) {
John Reck0f602f32011-07-07 15:38:43 -0700300 show();
301 }
302 }
303 }
304
305 public int getEmbeddedHeight() {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700306 if (mIsFixedTitleBar) return 0;
John Reck2711fab2012-05-18 21:38:59 -0700307 return calculateEmbeddedHeight();
308 }
309
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800310 public boolean isFixed() {
311 return mIsFixedTitleBar;
312 }
313
314 int calculateEmbeddedHeight() {
John Reck0f602f32011-07-07 15:38:43 -0700315 int height = mNavBar.getHeight();
John Reck0f602f32011-07-07 15:38:43 -0700316 return height;
317 }
318
John Reckef654f12011-07-12 16:42:08 -0700319 public boolean wantsToBeVisible() {
Bijan Amirzada289e1e62014-04-15 11:53:48 -0700320 return (mSnapshotBar != null && mSnapshotBar.getVisibility() == View.VISIBLE
John Reckef654f12011-07-12 16:42:08 -0700321 && mSnapshotBar.isAnimating());
322 }
323
John Reck0f602f32011-07-07 15:38:43 -0700324 public boolean isEditingUrl() {
325 return mNavBar.isEditingUrl();
326 }
327
328 public WebView getCurrentWebView() {
329 Tab t = mBaseUi.getActiveTab();
330 if (t != null) {
331 return t.getWebView();
332 } else {
333 return null;
334 }
335 }
336
337 public PageProgressView getProgressView() {
338 return mProgress;
339 }
340
341 public NavigationBarBase getNavigationBar() {
342 return mNavBar;
343 }
344
John Reck0f602f32011-07-07 15:38:43 -0700345 public boolean isInLoad() {
346 return mInLoad;
347 }
348
349 private ViewGroup.LayoutParams makeLayoutParams() {
Michael Kolb4923c222012-04-02 16:18:36 -0700350 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
351 LayoutParams.WRAP_CONTENT);
John Reck0f602f32011-07-07 15:38:43 -0700352 }
353
354 @Override
Panos Thomas752ce592014-10-16 13:00:35 -0700355 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
356 return mBaseUi.isCustomViewShowing() ? false :
357 super.requestFocus(direction, previouslyFocusedRect);
358 }
359
360 @Override
John Reck0f602f32011-07-07 15:38:43 -0700361 public View focusSearch(View focused, int dir) {
John Reckfcb60952012-05-30 09:45:28 -0700362 WebView web = getCurrentWebView();
John Reckd70419a2012-05-30 16:09:17 -0700363 if (FOCUS_DOWN == dir && hasFocus() && web != null
John Reckfcb60952012-05-30 09:45:28 -0700364 && web.hasFocusable() && web.getParent() != null) {
365 return web;
John Reck0f602f32011-07-07 15:38:43 -0700366 }
367 return super.focusSearch(focused, dir);
368 }
369
John Reckef654f12011-07-12 16:42:08 -0700370 public void onTabDataChanged(Tab tab) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100371 if (mSnapshotBar != null) {
372 mSnapshotBar.onTabDataChanged(tab);
373 }
374
John Reckef654f12011-07-12 16:42:08 -0700375 if (tab.isSnapshot()) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100376 inflateSnapshotBar();
John Reckef654f12011-07-12 16:42:08 -0700377 mSnapshotBar.setVisibility(VISIBLE);
378 mNavBar.setVisibility(GONE);
379 } else {
Ben Murdochd51bb572011-08-17 20:42:02 +0100380 if (mSnapshotBar != null) {
381 mSnapshotBar.setVisibility(GONE);
382 }
John Reckef654f12011-07-12 16:42:08 -0700383 mNavBar.setVisibility(VISIBLE);
384 }
385 }
386
Michael Kolb4923c222012-04-02 16:18:36 -0700387 public void onScrollChanged() {
John Reck2711fab2012-05-18 21:38:59 -0700388 if (!mShowing && !mIsFixedTitleBar) {
Michael Kolb4923c222012-04-02 16:18:36 -0700389 setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight());
390 }
391 }
392
John Reck1cc1d1d2012-09-04 18:13:51 -0700393 public void onResume() {
394 setFixedTitleBar();
395 }
396
John Reck0f602f32011-07-07 15:38:43 -0700397}