blob: 741dc41f338f8abd423b8fa72137195576f3e353 [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
John Reck0f602f32011-07-07 15:38:43 -070037import android.widget.FrameLayout;
38import android.widget.RelativeLayout;
39
40
41/**
42 * Base class for a title bar used by the browser.
43 */
44public class TitleBar extends RelativeLayout {
45
46 private static final int PROGRESS_MAX = 100;
47 private static final float ANIM_TITLEBAR_DECELERATE = 2.5f;
48
49 private UiController mUiController;
50 private BaseUi mBaseUi;
John Reck2711fab2012-05-18 21:38:59 -070051 private FrameLayout mContentView;
John Reck0f602f32011-07-07 15:38:43 -070052 private PageProgressView mProgress;
John Reck1cc1d1d2012-09-04 18:13:51 -070053 private AccessibilityManager mAccessibilityManager;
John Reck0f602f32011-07-07 15:38:43 -070054
John Reck0f602f32011-07-07 15:38:43 -070055 private NavigationBarBase mNavBar;
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
Ben Murdochd51bb572011-08-17 20:42:02 +010084 private void inflateSnapshotBar() {
85 if (mSnapshotBar != null) {
86 return;
87 }
88
89 ViewStub stub = (ViewStub) findViewById(R.id.snapshotbar_stub);
90 mSnapshotBar = (SnapshotBar) stub.inflate();
John Reckef654f12011-07-12 16:42:08 -070091 mSnapshotBar.setTitleBar(this);
John Reck0f602f32011-07-07 15:38:43 -070092 }
93
Michael Kolb57059a82012-04-30 14:32:03 -070094 @Override
95 protected void onConfigurationChanged(Configuration config) {
96 super.onConfigurationChanged(config);
John Reck0f9aaeb2012-05-23 14:40:19 -070097 setFixedTitleBar();
John Reck2711fab2012-05-18 21:38:59 -070098 }
99
100 @Override
101 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
102 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Vivek Sekhar8a660782014-10-27 17:00:53 -0700103 mBaseUi.setContentViewMarginTop(0);
John Reck2711fab2012-05-18 21:38:59 -0700104 }
105
John Reck0f9aaeb2012-05-23 14:40:19 -0700106 private void setFixedTitleBar() {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700107 boolean isFixed = !getContext().getResources().getBoolean(R.bool.hide_title);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700108
John Reck1cc1d1d2012-09-04 18:13:51 -0700109 isFixed |= mAccessibilityManager.isEnabled();
John Reck2711fab2012-05-18 21:38:59 -0700110 // If getParent() returns null, we are initializing
111 ViewGroup parent = (ViewGroup)getParent();
112 if (mIsFixedTitleBar == isFixed && parent != null) return;
113 mIsFixedTitleBar = isFixed;
114 setSkipTitleBarAnimations(true);
115 show();
116 setSkipTitleBarAnimations(false);
117 if (parent != null) {
118 parent.removeView(this);
119 }
Vivek Sekhar8a660782014-10-27 17:00:53 -0700120 mContentView.addView(this, makeLayoutParams());
121 mBaseUi.setContentViewMarginTop(0);
Michael Kolb57059a82012-04-30 14:32:03 -0700122 }
123
John Reck0f602f32011-07-07 15:38:43 -0700124 public BaseUi getUi() {
125 return mBaseUi;
126 }
127
128 public UiController getUiController() {
129 return mUiController;
130 }
131
John Reck0f602f32011-07-07 15:38:43 -0700132 void setShowProgressOnly(boolean progress) {
John Reckef654f12011-07-12 16:42:08 -0700133 if (progress && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700134 mNavBar.setVisibility(View.GONE);
135 } else {
136 mNavBar.setVisibility(View.VISIBLE);
137 }
138 }
139
140 void setSkipTitleBarAnimations(boolean skip) {
141 mSkipTitleBarAnimations = skip;
142 }
143
144 void setupTitleBarAnimator(Animator animator) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800145 Resources res = getContext().getResources();
John Reck0f602f32011-07-07 15:38:43 -0700146 int duration = res.getInteger(R.integer.titlebar_animation_duration);
147 animator.setInterpolator(new DecelerateInterpolator(
148 ANIM_TITLEBAR_DECELERATE));
149 animator.setDuration(duration);
150 }
151
Tarun Nainani8eb00912014-07-17 12:28:32 -0700152 //Disable stock autohide behavior in favor of top controls
153 private static final boolean bOldStyleAutoHideDisabled = true;
John Reck0f602f32011-07-07 15:38:43 -0700154 void show() {
John Reck2711fab2012-05-18 21:38:59 -0700155 cancelTitleBarAnimation(false);
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700156 if (mSkipTitleBarAnimations) {
Michael Kolb4923c222012-04-02 16:18:36 -0700157 this.setVisibility(View.VISIBLE);
158 this.setTranslationY(0);
Vivek Sekhar8a660782014-10-27 17:00:53 -0700159 // reaffirm top-controls
160 if (isFixed() || isInLoad())
161 showTopControls();
162 else
163 enableTopControls();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700164 } else if (!bOldStyleAutoHideDisabled) {
John Reck2711fab2012-05-18 21:38:59 -0700165 int visibleHeight = getVisibleTitleHeight();
166 float startPos = (-getEmbeddedHeight() + visibleHeight);
167 if (getTranslationY() != 0) {
168 startPos = Math.max(startPos, getTranslationY());
John Reck0f602f32011-07-07 15:38:43 -0700169 }
John Reck2711fab2012-05-18 21:38:59 -0700170 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
171 "translationY",
172 startPos, 0);
173 setupTitleBarAnimator(mTitleBarAnimator);
174 mTitleBarAnimator.start();
John Reck0f602f32011-07-07 15:38:43 -0700175 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700176
John Reck0f602f32011-07-07 15:38:43 -0700177 mShowing = true;
178 }
179
180 void hide() {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700181 if (mIsFixedTitleBar || bOldStyleAutoHideDisabled) return;
182 if (!mSkipTitleBarAnimations) {
183 cancelTitleBarAnimation(false);
184 int visibleHeight = getVisibleTitleHeight();
185 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
186 "translationY", getTranslationY(),
187 (-getEmbeddedHeight() + visibleHeight));
188 mTitleBarAnimator.addListener(mHideTileBarAnimatorListener);
189 setupTitleBarAnimator(mTitleBarAnimator);
190 mTitleBarAnimator.start();
John Reck0f602f32011-07-07 15:38:43 -0700191 } else {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700192 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700193 }
194 mShowing = false;
195 }
196
197 boolean isShowing() {
198 return mShowing;
199 }
200
201 void cancelTitleBarAnimation(boolean reset) {
202 if (mTitleBarAnimator != null) {
203 mTitleBarAnimator.cancel();
204 mTitleBarAnimator = null;
205 }
206 if (reset) {
207 setTranslationY(0);
208 }
209 }
210
211 private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() {
212
John Reck0f602f32011-07-07 15:38:43 -0700213 @Override
214 public void onAnimationStart(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700215 }
216
217 @Override
218 public void onAnimationRepeat(Animator animation) {
219 }
220
221 @Override
222 public void onAnimationEnd(Animator animation) {
Michael Kolb4923c222012-04-02 16:18:36 -0700223 // update position
224 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700225 }
226
227 @Override
228 public void onAnimationCancel(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700229 }
230 };
231
232 private int getVisibleTitleHeight() {
233 Tab tab = mBaseUi.getActiveTab();
234 WebView webview = tab != null ? tab.getWebView() : null;
235 return webview != null ? webview.getVisibleTitleHeight() : 0;
236 }
237
Tarun Nainani8eb00912014-07-17 12:28:32 -0700238 private void hideTopControls() {
239 Tab tab = mBaseUi.getActiveTab();
240 WebView view = tab != null ? tab.getWebView() : null;
241 if (view != null)
242 view.updateTopControls(true, false, true);
243 }
244
245 private void showTopControls() {
246 Tab tab = mBaseUi.getActiveTab();
247 WebView view = tab != null ? tab.getWebView() : null;
248 if (view != null)
249 view.updateTopControls(false, true, true);
250 }
251
252 private void enableTopControls() {
253 Tab tab = mBaseUi.getActiveTab();
254 WebView view = tab != null ? tab.getWebView() : null;
255 if (view != null)
256 view.updateTopControls(true, true, true);
257 }
258
259
John Reck0f602f32011-07-07 15:38:43 -0700260 /**
261 * Update the progress, from 0 to 100.
262 */
263 public void setProgress(int newProgress) {
264 if (newProgress >= PROGRESS_MAX) {
265 mProgress.setProgress(PageProgressView.MAX_PROGRESS);
266 mProgress.setVisibility(View.GONE);
267 mInLoad = false;
268 mNavBar.onProgressStopped();
269 // check if needs to be hidden
John Reckef654f12011-07-12 16:42:08 -0700270 if (!isEditingUrl() && !wantsToBeVisible()) {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700271 mBaseUi.showTitleBarForDuration();
John Reck0f602f32011-07-07 15:38:43 -0700272 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700273
274 //onPageFinished
Vivek Sekhar8a660782014-10-27 17:00:53 -0700275 if (isFixed())
276 showTopControls();
277 else
278 enableTopControls();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700279
John Reck0f602f32011-07-07 15:38:43 -0700280 } else {
281 if (!mInLoad) {
282 mProgress.setVisibility(View.VISIBLE);
283 mInLoad = true;
284 mNavBar.onProgressStarted();
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700285 mProgress.onProgressStarted();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700286 //onPageStarted
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700287 showTopControls();
John Reck0f602f32011-07-07 15:38:43 -0700288 }
289 mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
290 / PROGRESS_MAX);
291 if (!mShowing) {
John Reck0f602f32011-07-07 15:38:43 -0700292 show();
293 }
294 }
295 }
296
297 public int getEmbeddedHeight() {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700298 if (mIsFixedTitleBar) return 0;
John Reck2711fab2012-05-18 21:38:59 -0700299 return calculateEmbeddedHeight();
300 }
301
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800302 public boolean isFixed() {
303 return mIsFixedTitleBar;
304 }
305
306 int calculateEmbeddedHeight() {
John Reck0f602f32011-07-07 15:38:43 -0700307 int height = mNavBar.getHeight();
John Reck0f602f32011-07-07 15:38:43 -0700308 return height;
309 }
310
John Reckef654f12011-07-12 16:42:08 -0700311 public boolean wantsToBeVisible() {
Bijan Amirzada289e1e62014-04-15 11:53:48 -0700312 return (mSnapshotBar != null && mSnapshotBar.getVisibility() == View.VISIBLE
John Reckef654f12011-07-12 16:42:08 -0700313 && mSnapshotBar.isAnimating());
314 }
315
John Reck0f602f32011-07-07 15:38:43 -0700316 public boolean isEditingUrl() {
317 return mNavBar.isEditingUrl();
318 }
319
320 public WebView getCurrentWebView() {
321 Tab t = mBaseUi.getActiveTab();
322 if (t != null) {
323 return t.getWebView();
324 } else {
325 return null;
326 }
327 }
328
329 public PageProgressView getProgressView() {
330 return mProgress;
331 }
332
333 public NavigationBarBase getNavigationBar() {
334 return mNavBar;
335 }
336
John Reck0f602f32011-07-07 15:38:43 -0700337 public boolean isInLoad() {
338 return mInLoad;
339 }
340
341 private ViewGroup.LayoutParams makeLayoutParams() {
Michael Kolb4923c222012-04-02 16:18:36 -0700342 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
343 LayoutParams.WRAP_CONTENT);
John Reck0f602f32011-07-07 15:38:43 -0700344 }
345
346 @Override
Panos Thomas752ce592014-10-16 13:00:35 -0700347 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
348 return mBaseUi.isCustomViewShowing() ? false :
349 super.requestFocus(direction, previouslyFocusedRect);
350 }
351
352 @Override
John Reck0f602f32011-07-07 15:38:43 -0700353 public View focusSearch(View focused, int dir) {
John Reckfcb60952012-05-30 09:45:28 -0700354 WebView web = getCurrentWebView();
John Reckd70419a2012-05-30 16:09:17 -0700355 if (FOCUS_DOWN == dir && hasFocus() && web != null
John Reckfcb60952012-05-30 09:45:28 -0700356 && web.hasFocusable() && web.getParent() != null) {
357 return web;
John Reck0f602f32011-07-07 15:38:43 -0700358 }
359 return super.focusSearch(focused, dir);
360 }
361
John Reckef654f12011-07-12 16:42:08 -0700362 public void onTabDataChanged(Tab tab) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100363 if (mSnapshotBar != null) {
364 mSnapshotBar.onTabDataChanged(tab);
365 }
366
John Reckef654f12011-07-12 16:42:08 -0700367 if (tab.isSnapshot()) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100368 inflateSnapshotBar();
John Reckef654f12011-07-12 16:42:08 -0700369 mSnapshotBar.setVisibility(VISIBLE);
370 mNavBar.setVisibility(GONE);
371 } else {
Ben Murdochd51bb572011-08-17 20:42:02 +0100372 if (mSnapshotBar != null) {
373 mSnapshotBar.setVisibility(GONE);
374 }
John Reckef654f12011-07-12 16:42:08 -0700375 mNavBar.setVisibility(VISIBLE);
376 }
377 }
378
Michael Kolb4923c222012-04-02 16:18:36 -0700379 public void onScrollChanged() {
John Reck2711fab2012-05-18 21:38:59 -0700380 if (!mShowing && !mIsFixedTitleBar) {
Michael Kolb4923c222012-04-02 16:18:36 -0700381 setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight());
382 }
383 }
384
John Reck1cc1d1d2012-09-04 18:13:51 -0700385 public void onResume() {
386 setFixedTitleBar();
387 }
388
John Reck0f602f32011-07-07 15:38:43 -0700389}