blob: 9963bc4dd30238ace5eb636a04c6e3858eeaf433 [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;
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080034import org.codeaurora.swe.WebView;
35
Bijan Amirzada41242f22014-03-21 12:12:18 -070036import com.android.browser.R;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080037
John Reck0f602f32011-07-07 15:38:43 -070038import android.widget.FrameLayout;
39import android.widget.RelativeLayout;
40
41
42/**
43 * Base class for a title bar used by the browser.
44 */
45public class TitleBar extends RelativeLayout {
46
47 private static final int PROGRESS_MAX = 100;
48 private static final float ANIM_TITLEBAR_DECELERATE = 2.5f;
49
50 private UiController mUiController;
51 private BaseUi mBaseUi;
John Reck2711fab2012-05-18 21:38:59 -070052 private FrameLayout mContentView;
John Reck0f602f32011-07-07 15:38:43 -070053 private PageProgressView mProgress;
John Reck1cc1d1d2012-09-04 18:13:51 -070054 private AccessibilityManager mAccessibilityManager;
John Reck0f602f32011-07-07 15:38:43 -070055
John Reck0f602f32011-07-07 15:38:43 -070056 private NavigationBarBase mNavBar;
57 private boolean mUseQuickControls;
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 Sekhar8ee3abb2014-07-14 12:32:05 -0700105 boolean hide_title_on_scroll =
106 getContext().getResources().getBoolean(R.bool.hide_title_on_scroll);
107 if (mIsFixedTitleBar && !hide_title_on_scroll) {
John Reck2711fab2012-05-18 21:38:59 -0700108 int margin = getMeasuredHeight() - calculateEmbeddedHeight();
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700109 if (!isEditingUrl())
110 mBaseUi.setContentViewMarginTop(-margin);
John Reck0f9aaeb2012-05-23 14:40:19 -0700111 } else {
112 mBaseUi.setContentViewMarginTop(0);
John Reck2711fab2012-05-18 21:38:59 -0700113 }
114 }
115
John Reck0f9aaeb2012-05-23 14:40:19 -0700116 private void setFixedTitleBar() {
117 boolean isFixed = !mUseQuickControls
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800118 && !getContext().getResources().getBoolean(R.bool.hide_title);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700119 boolean hide_title_on_scroll =
120 getContext().getResources().getBoolean(R.bool.hide_title_on_scroll);
John Reck1cc1d1d2012-09-04 18:13:51 -0700121 isFixed |= mAccessibilityManager.isEnabled();
John Reck2711fab2012-05-18 21:38:59 -0700122 // If getParent() returns null, we are initializing
123 ViewGroup parent = (ViewGroup)getParent();
124 if (mIsFixedTitleBar == isFixed && parent != null) return;
125 mIsFixedTitleBar = isFixed;
126 setSkipTitleBarAnimations(true);
127 show();
128 setSkipTitleBarAnimations(false);
129 if (parent != null) {
130 parent.removeView(this);
131 }
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700132 if (mIsFixedTitleBar && !hide_title_on_scroll) {
John Reck2711fab2012-05-18 21:38:59 -0700133 mBaseUi.addFixedTitleBar(this);
134 } else {
135 mContentView.addView(this, makeLayoutParams());
136 mBaseUi.setContentViewMarginTop(0);
Michael Kolb57059a82012-04-30 14:32:03 -0700137 }
138 }
139
John Reck0f602f32011-07-07 15:38:43 -0700140 public BaseUi getUi() {
141 return mBaseUi;
142 }
143
144 public UiController getUiController() {
145 return mUiController;
146 }
147
148 public void setUseQuickControls(boolean use) {
149 mUseQuickControls = use;
John Reck0f9aaeb2012-05-23 14:40:19 -0700150 setFixedTitleBar();
Michael Kolb4923c222012-04-02 16:18:36 -0700151 if (use) {
152 this.setVisibility(View.GONE);
153 } else {
154 this.setVisibility(View.VISIBLE);
155 }
John Reck0f602f32011-07-07 15:38:43 -0700156 }
157
158 void setShowProgressOnly(boolean progress) {
John Reckef654f12011-07-12 16:42:08 -0700159 if (progress && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700160 mNavBar.setVisibility(View.GONE);
161 } else {
162 mNavBar.setVisibility(View.VISIBLE);
163 }
164 }
165
166 void setSkipTitleBarAnimations(boolean skip) {
167 mSkipTitleBarAnimations = skip;
168 }
169
170 void setupTitleBarAnimator(Animator animator) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800171 Resources res = getContext().getResources();
John Reck0f602f32011-07-07 15:38:43 -0700172 int duration = res.getInteger(R.integer.titlebar_animation_duration);
173 animator.setInterpolator(new DecelerateInterpolator(
174 ANIM_TITLEBAR_DECELERATE));
175 animator.setDuration(duration);
176 }
177
178 void show() {
John Reck2711fab2012-05-18 21:38:59 -0700179 cancelTitleBarAnimation(false);
180 if (mUseQuickControls || mSkipTitleBarAnimations) {
Michael Kolb4923c222012-04-02 16:18:36 -0700181 this.setVisibility(View.VISIBLE);
182 this.setTranslationY(0);
John Reck0f602f32011-07-07 15:38:43 -0700183 } else {
John Reck2711fab2012-05-18 21:38:59 -0700184 int visibleHeight = getVisibleTitleHeight();
185 float startPos = (-getEmbeddedHeight() + visibleHeight);
186 if (getTranslationY() != 0) {
187 startPos = Math.max(startPos, getTranslationY());
John Reck0f602f32011-07-07 15:38:43 -0700188 }
John Reck2711fab2012-05-18 21:38:59 -0700189 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
190 "translationY",
191 startPos, 0);
192 setupTitleBarAnimator(mTitleBarAnimator);
193 mTitleBarAnimator.start();
John Reck0f602f32011-07-07 15:38:43 -0700194 }
195 mShowing = true;
196 }
197
198 void hide() {
199 if (mUseQuickControls) {
Michael Kolb4923c222012-04-02 16:18:36 -0700200 this.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700201 } else {
John Reck2711fab2012-05-18 21:38:59 -0700202 if (mIsFixedTitleBar) return;
John Reck0f602f32011-07-07 15:38:43 -0700203 if (!mSkipTitleBarAnimations) {
204 cancelTitleBarAnimation(false);
205 int visibleHeight = getVisibleTitleHeight();
206 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
207 "translationY", getTranslationY(),
208 (-getEmbeddedHeight() + visibleHeight));
209 mTitleBarAnimator.addListener(mHideTileBarAnimatorListener);
210 setupTitleBarAnimator(mTitleBarAnimator);
211 mTitleBarAnimator.start();
212 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700213 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700214 }
215 }
216 mShowing = false;
217 }
218
219 boolean isShowing() {
220 return mShowing;
221 }
222
223 void cancelTitleBarAnimation(boolean reset) {
224 if (mTitleBarAnimator != null) {
225 mTitleBarAnimator.cancel();
226 mTitleBarAnimator = null;
227 }
228 if (reset) {
229 setTranslationY(0);
230 }
231 }
232
233 private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() {
234
John Reck0f602f32011-07-07 15:38:43 -0700235 @Override
236 public void onAnimationStart(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700237 }
238
239 @Override
240 public void onAnimationRepeat(Animator animation) {
241 }
242
243 @Override
244 public void onAnimationEnd(Animator animation) {
Michael Kolb4923c222012-04-02 16:18:36 -0700245 // update position
246 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700247 }
248
249 @Override
250 public void onAnimationCancel(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700251 }
252 };
253
254 private int getVisibleTitleHeight() {
255 Tab tab = mBaseUi.getActiveTab();
256 WebView webview = tab != null ? tab.getWebView() : null;
257 return webview != null ? webview.getVisibleTitleHeight() : 0;
258 }
259
260 /**
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()) {
John Reck0f602f32011-07-07 15:38:43 -0700271 if (mUseQuickControls) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700272 hide();
John Reckbc82ec92012-04-19 13:12:35 -0700273 } else {
274 mBaseUi.showTitleBarForDuration();
John Reck0f602f32011-07-07 15:38:43 -0700275 }
276 }
277 } else {
278 if (!mInLoad) {
279 mProgress.setVisibility(View.VISIBLE);
280 mInLoad = true;
281 mNavBar.onProgressStarted();
282 }
283 mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
284 / PROGRESS_MAX);
Michael Kolbe8a82332012-04-25 14:14:26 -0700285 if (mUseQuickControls && !isEditingUrl()) {
286 setShowProgressOnly(true);
287 }
John Reck0f602f32011-07-07 15:38:43 -0700288 if (!mShowing) {
John Reck0f602f32011-07-07 15:38:43 -0700289 show();
290 }
291 }
292 }
293
294 public int getEmbeddedHeight() {
John Reck2711fab2012-05-18 21:38:59 -0700295 if (mUseQuickControls || mIsFixedTitleBar) return 0;
296 return calculateEmbeddedHeight();
297 }
298
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800299 public boolean isFixed() {
300 return mIsFixedTitleBar;
301 }
302
303 int calculateEmbeddedHeight() {
John Reck0f602f32011-07-07 15:38:43 -0700304 int height = mNavBar.getHeight();
John Reck0f602f32011-07-07 15:38:43 -0700305 return height;
306 }
307
John Reckef654f12011-07-12 16:42:08 -0700308 public boolean wantsToBeVisible() {
Bijan Amirzada289e1e62014-04-15 11:53:48 -0700309 return (mSnapshotBar != null && mSnapshotBar.getVisibility() == View.VISIBLE
John Reckef654f12011-07-12 16:42:08 -0700310 && mSnapshotBar.isAnimating());
311 }
312
John Reck0f602f32011-07-07 15:38:43 -0700313 public boolean isEditingUrl() {
314 return mNavBar.isEditingUrl();
315 }
316
317 public WebView getCurrentWebView() {
318 Tab t = mBaseUi.getActiveTab();
319 if (t != null) {
320 return t.getWebView();
321 } else {
322 return null;
323 }
324 }
325
326 public PageProgressView getProgressView() {
327 return mProgress;
328 }
329
330 public NavigationBarBase getNavigationBar() {
331 return mNavBar;
332 }
333
334 public boolean useQuickControls() {
335 return mUseQuickControls;
336 }
337
338 public boolean isInLoad() {
339 return mInLoad;
340 }
341
342 private ViewGroup.LayoutParams makeLayoutParams() {
Michael Kolb4923c222012-04-02 16:18:36 -0700343 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
344 LayoutParams.WRAP_CONTENT);
John Reck0f602f32011-07-07 15:38:43 -0700345 }
346
347 @Override
348 public View focusSearch(View focused, int dir) {
John Reckfcb60952012-05-30 09:45:28 -0700349 WebView web = getCurrentWebView();
John Reckd70419a2012-05-30 16:09:17 -0700350 if (FOCUS_DOWN == dir && hasFocus() && web != null
John Reckfcb60952012-05-30 09:45:28 -0700351 && web.hasFocusable() && web.getParent() != null) {
352 return web;
John Reck0f602f32011-07-07 15:38:43 -0700353 }
354 return super.focusSearch(focused, dir);
355 }
356
John Reckef654f12011-07-12 16:42:08 -0700357 public void onTabDataChanged(Tab tab) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100358 if (mSnapshotBar != null) {
359 mSnapshotBar.onTabDataChanged(tab);
360 }
361
John Reckef654f12011-07-12 16:42:08 -0700362 if (tab.isSnapshot()) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100363 inflateSnapshotBar();
John Reckef654f12011-07-12 16:42:08 -0700364 mSnapshotBar.setVisibility(VISIBLE);
365 mNavBar.setVisibility(GONE);
366 } else {
Ben Murdochd51bb572011-08-17 20:42:02 +0100367 if (mSnapshotBar != null) {
368 mSnapshotBar.setVisibility(GONE);
369 }
John Reckef654f12011-07-12 16:42:08 -0700370 mNavBar.setVisibility(VISIBLE);
371 }
372 }
373
Michael Kolb4923c222012-04-02 16:18:36 -0700374 public void onScrollChanged() {
John Reck2711fab2012-05-18 21:38:59 -0700375 if (!mShowing && !mIsFixedTitleBar) {
Michael Kolb4923c222012-04-02 16:18:36 -0700376 setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight());
377 }
378 }
379
John Reck1cc1d1d2012-09-04 18:13:51 -0700380 public void onResume() {
381 setFixedTitleBar();
382 }
383
John Reck0f602f32011-07-07 15:38:43 -0700384}