blob: 7438d26ba5b73cb2ad97a1a01da8ed07757b84ea [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);
105 if (mIsFixedTitleBar) {
106 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() {
115 boolean isFixed = !mUseQuickControls
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800116 && !getContext().getResources().getBoolean(R.bool.hide_title);
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 }
128 if (mIsFixedTitleBar) {
129 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
144 public void setUseQuickControls(boolean use) {
145 mUseQuickControls = use;
John Reck0f9aaeb2012-05-23 14:40:19 -0700146 setFixedTitleBar();
Michael Kolb4923c222012-04-02 16:18:36 -0700147 if (use) {
148 this.setVisibility(View.GONE);
149 } else {
150 this.setVisibility(View.VISIBLE);
151 }
John Reck0f602f32011-07-07 15:38:43 -0700152 }
153
154 void setShowProgressOnly(boolean progress) {
John Reckef654f12011-07-12 16:42:08 -0700155 if (progress && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700156 mNavBar.setVisibility(View.GONE);
157 } else {
158 mNavBar.setVisibility(View.VISIBLE);
159 }
160 }
161
162 void setSkipTitleBarAnimations(boolean skip) {
163 mSkipTitleBarAnimations = skip;
164 }
165
166 void setupTitleBarAnimator(Animator animator) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800167 Resources res = getContext().getResources();
John Reck0f602f32011-07-07 15:38:43 -0700168 int duration = res.getInteger(R.integer.titlebar_animation_duration);
169 animator.setInterpolator(new DecelerateInterpolator(
170 ANIM_TITLEBAR_DECELERATE));
171 animator.setDuration(duration);
172 }
173
174 void show() {
John Reck2711fab2012-05-18 21:38:59 -0700175 cancelTitleBarAnimation(false);
176 if (mUseQuickControls || mSkipTitleBarAnimations) {
Michael Kolb4923c222012-04-02 16:18:36 -0700177 this.setVisibility(View.VISIBLE);
178 this.setTranslationY(0);
John Reck0f602f32011-07-07 15:38:43 -0700179 } else {
John Reck2711fab2012-05-18 21:38:59 -0700180 int visibleHeight = getVisibleTitleHeight();
181 float startPos = (-getEmbeddedHeight() + visibleHeight);
182 if (getTranslationY() != 0) {
183 startPos = Math.max(startPos, getTranslationY());
John Reck0f602f32011-07-07 15:38:43 -0700184 }
John Reck2711fab2012-05-18 21:38:59 -0700185 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
186 "translationY",
187 startPos, 0);
188 setupTitleBarAnimator(mTitleBarAnimator);
189 mTitleBarAnimator.start();
John Reck0f602f32011-07-07 15:38:43 -0700190 }
191 mShowing = true;
192 }
193
194 void hide() {
195 if (mUseQuickControls) {
Michael Kolb4923c222012-04-02 16:18:36 -0700196 this.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700197 } else {
John Reck2711fab2012-05-18 21:38:59 -0700198 if (mIsFixedTitleBar) return;
John Reck0f602f32011-07-07 15:38:43 -0700199 if (!mSkipTitleBarAnimations) {
200 cancelTitleBarAnimation(false);
201 int visibleHeight = getVisibleTitleHeight();
202 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
203 "translationY", getTranslationY(),
204 (-getEmbeddedHeight() + visibleHeight));
205 mTitleBarAnimator.addListener(mHideTileBarAnimatorListener);
206 setupTitleBarAnimator(mTitleBarAnimator);
207 mTitleBarAnimator.start();
208 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700209 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700210 }
211 }
212 mShowing = false;
213 }
214
215 boolean isShowing() {
216 return mShowing;
217 }
218
219 void cancelTitleBarAnimation(boolean reset) {
220 if (mTitleBarAnimator != null) {
221 mTitleBarAnimator.cancel();
222 mTitleBarAnimator = null;
223 }
224 if (reset) {
225 setTranslationY(0);
226 }
227 }
228
229 private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() {
230
John Reck0f602f32011-07-07 15:38:43 -0700231 @Override
232 public void onAnimationStart(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700233 }
234
235 @Override
236 public void onAnimationRepeat(Animator animation) {
237 }
238
239 @Override
240 public void onAnimationEnd(Animator animation) {
Michael Kolb4923c222012-04-02 16:18:36 -0700241 // update position
242 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700243 }
244
245 @Override
246 public void onAnimationCancel(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700247 }
248 };
249
250 private int getVisibleTitleHeight() {
251 Tab tab = mBaseUi.getActiveTab();
252 WebView webview = tab != null ? tab.getWebView() : null;
253 return webview != null ? webview.getVisibleTitleHeight() : 0;
254 }
255
256 /**
257 * Update the progress, from 0 to 100.
258 */
259 public void setProgress(int newProgress) {
260 if (newProgress >= PROGRESS_MAX) {
261 mProgress.setProgress(PageProgressView.MAX_PROGRESS);
262 mProgress.setVisibility(View.GONE);
263 mInLoad = false;
264 mNavBar.onProgressStopped();
265 // check if needs to be hidden
John Reckef654f12011-07-12 16:42:08 -0700266 if (!isEditingUrl() && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700267 if (mUseQuickControls) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700268 hide();
John Reckbc82ec92012-04-19 13:12:35 -0700269 } else {
270 mBaseUi.showTitleBarForDuration();
John Reck0f602f32011-07-07 15:38:43 -0700271 }
272 }
273 } else {
274 if (!mInLoad) {
275 mProgress.setVisibility(View.VISIBLE);
276 mInLoad = true;
277 mNavBar.onProgressStarted();
278 }
279 mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
280 / PROGRESS_MAX);
Michael Kolbe8a82332012-04-25 14:14:26 -0700281 if (mUseQuickControls && !isEditingUrl()) {
282 setShowProgressOnly(true);
283 }
John Reck0f602f32011-07-07 15:38:43 -0700284 if (!mShowing) {
John Reck0f602f32011-07-07 15:38:43 -0700285 show();
286 }
287 }
288 }
289
290 public int getEmbeddedHeight() {
John Reck2711fab2012-05-18 21:38:59 -0700291 if (mUseQuickControls || mIsFixedTitleBar) return 0;
292 return calculateEmbeddedHeight();
293 }
294
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800295 public boolean isFixed() {
296 return mIsFixedTitleBar;
297 }
298
299 int calculateEmbeddedHeight() {
John Reck0f602f32011-07-07 15:38:43 -0700300 int height = mNavBar.getHeight();
John Reck0f602f32011-07-07 15:38:43 -0700301 return height;
302 }
303
John Reckef654f12011-07-12 16:42:08 -0700304 public boolean wantsToBeVisible() {
Bijan Amirzada289e1e62014-04-15 11:53:48 -0700305 return (mSnapshotBar != null && mSnapshotBar.getVisibility() == View.VISIBLE
John Reckef654f12011-07-12 16:42:08 -0700306 && mSnapshotBar.isAnimating());
307 }
308
John Reck0f602f32011-07-07 15:38:43 -0700309 public boolean isEditingUrl() {
310 return mNavBar.isEditingUrl();
311 }
312
313 public WebView getCurrentWebView() {
314 Tab t = mBaseUi.getActiveTab();
315 if (t != null) {
316 return t.getWebView();
317 } else {
318 return null;
319 }
320 }
321
322 public PageProgressView getProgressView() {
323 return mProgress;
324 }
325
326 public NavigationBarBase getNavigationBar() {
327 return mNavBar;
328 }
329
330 public boolean useQuickControls() {
331 return mUseQuickControls;
332 }
333
334 public boolean isInLoad() {
335 return mInLoad;
336 }
337
338 private ViewGroup.LayoutParams makeLayoutParams() {
Michael Kolb4923c222012-04-02 16:18:36 -0700339 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
340 LayoutParams.WRAP_CONTENT);
John Reck0f602f32011-07-07 15:38:43 -0700341 }
342
343 @Override
344 public View focusSearch(View focused, int dir) {
John Reckfcb60952012-05-30 09:45:28 -0700345 WebView web = getCurrentWebView();
John Reckd70419a2012-05-30 16:09:17 -0700346 if (FOCUS_DOWN == dir && hasFocus() && web != null
John Reckfcb60952012-05-30 09:45:28 -0700347 && web.hasFocusable() && web.getParent() != null) {
348 return web;
John Reck0f602f32011-07-07 15:38:43 -0700349 }
350 return super.focusSearch(focused, dir);
351 }
352
John Reckef654f12011-07-12 16:42:08 -0700353 public void onTabDataChanged(Tab tab) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100354 if (mSnapshotBar != null) {
355 mSnapshotBar.onTabDataChanged(tab);
356 }
357
John Reckef654f12011-07-12 16:42:08 -0700358 if (tab.isSnapshot()) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100359 inflateSnapshotBar();
John Reckef654f12011-07-12 16:42:08 -0700360 mSnapshotBar.setVisibility(VISIBLE);
361 mNavBar.setVisibility(GONE);
362 } else {
Ben Murdochd51bb572011-08-17 20:42:02 +0100363 if (mSnapshotBar != null) {
364 mSnapshotBar.setVisibility(GONE);
365 }
John Reckef654f12011-07-12 16:42:08 -0700366 mNavBar.setVisibility(VISIBLE);
367 }
368 }
369
Michael Kolb4923c222012-04-02 16:18:36 -0700370 public void onScrollChanged() {
John Reck2711fab2012-05-18 21:38:59 -0700371 if (!mShowing && !mIsFixedTitleBar) {
Michael Kolb4923c222012-04-02 16:18:36 -0700372 setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight());
373 }
374 }
375
John Reck1cc1d1d2012-09-04 18:13:51 -0700376 public void onResume() {
377 setFixedTitleBar();
378 }
379
John Reck0f602f32011-07-07 15:38:43 -0700380}