blob: 78f2d0effe2a98978c0fa26f35a41e4c22d23d33 [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 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() {
115 boolean isFixed = !mUseQuickControls
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800116 && !getContext().getResources().getBoolean(R.bool.hide_title);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700117 boolean hide_title_on_scroll =
118 getContext().getResources().getBoolean(R.bool.hide_title_on_scroll);
John Reck1cc1d1d2012-09-04 18:13:51 -0700119 isFixed |= mAccessibilityManager.isEnabled();
John Reck2711fab2012-05-18 21:38:59 -0700120 // If getParent() returns null, we are initializing
121 ViewGroup parent = (ViewGroup)getParent();
122 if (mIsFixedTitleBar == isFixed && parent != null) return;
123 mIsFixedTitleBar = isFixed;
124 setSkipTitleBarAnimations(true);
125 show();
126 setSkipTitleBarAnimations(false);
127 if (parent != null) {
128 parent.removeView(this);
129 }
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700130 if (mIsFixedTitleBar && !hide_title_on_scroll) {
John Reck2711fab2012-05-18 21:38:59 -0700131 mBaseUi.addFixedTitleBar(this);
132 } else {
133 mContentView.addView(this, makeLayoutParams());
134 mBaseUi.setContentViewMarginTop(0);
Michael Kolb57059a82012-04-30 14:32:03 -0700135 }
136 }
137
John Reck0f602f32011-07-07 15:38:43 -0700138 public BaseUi getUi() {
139 return mBaseUi;
140 }
141
142 public UiController getUiController() {
143 return mUiController;
144 }
145
146 public void setUseQuickControls(boolean use) {
147 mUseQuickControls = use;
John Reck0f9aaeb2012-05-23 14:40:19 -0700148 setFixedTitleBar();
Michael Kolb4923c222012-04-02 16:18:36 -0700149 if (use) {
150 this.setVisibility(View.GONE);
151 } else {
152 this.setVisibility(View.VISIBLE);
153 }
John Reck0f602f32011-07-07 15:38:43 -0700154 }
155
156 void setShowProgressOnly(boolean progress) {
John Reckef654f12011-07-12 16:42:08 -0700157 if (progress && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700158 mNavBar.setVisibility(View.GONE);
159 } else {
160 mNavBar.setVisibility(View.VISIBLE);
161 }
162 }
163
164 void setSkipTitleBarAnimations(boolean skip) {
165 mSkipTitleBarAnimations = skip;
166 }
167
168 void setupTitleBarAnimator(Animator animator) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800169 Resources res = getContext().getResources();
John Reck0f602f32011-07-07 15:38:43 -0700170 int duration = res.getInteger(R.integer.titlebar_animation_duration);
171 animator.setInterpolator(new DecelerateInterpolator(
172 ANIM_TITLEBAR_DECELERATE));
173 animator.setDuration(duration);
174 }
175
176 void show() {
John Reck2711fab2012-05-18 21:38:59 -0700177 cancelTitleBarAnimation(false);
178 if (mUseQuickControls || mSkipTitleBarAnimations) {
Michael Kolb4923c222012-04-02 16:18:36 -0700179 this.setVisibility(View.VISIBLE);
180 this.setTranslationY(0);
John Reck0f602f32011-07-07 15:38:43 -0700181 } else {
John Reck2711fab2012-05-18 21:38:59 -0700182 int visibleHeight = getVisibleTitleHeight();
183 float startPos = (-getEmbeddedHeight() + visibleHeight);
184 if (getTranslationY() != 0) {
185 startPos = Math.max(startPos, getTranslationY());
John Reck0f602f32011-07-07 15:38:43 -0700186 }
John Reck2711fab2012-05-18 21:38:59 -0700187 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
188 "translationY",
189 startPos, 0);
190 setupTitleBarAnimator(mTitleBarAnimator);
191 mTitleBarAnimator.start();
John Reck0f602f32011-07-07 15:38:43 -0700192 }
193 mShowing = true;
194 }
195
196 void hide() {
197 if (mUseQuickControls) {
Michael Kolb4923c222012-04-02 16:18:36 -0700198 this.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700199 } else {
John Reck2711fab2012-05-18 21:38:59 -0700200 if (mIsFixedTitleBar) return;
John Reck0f602f32011-07-07 15:38:43 -0700201 if (!mSkipTitleBarAnimations) {
202 cancelTitleBarAnimation(false);
203 int visibleHeight = getVisibleTitleHeight();
204 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
205 "translationY", getTranslationY(),
206 (-getEmbeddedHeight() + visibleHeight));
207 mTitleBarAnimator.addListener(mHideTileBarAnimatorListener);
208 setupTitleBarAnimator(mTitleBarAnimator);
209 mTitleBarAnimator.start();
210 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700211 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700212 }
213 }
214 mShowing = false;
215 }
216
217 boolean isShowing() {
218 return mShowing;
219 }
220
221 void cancelTitleBarAnimation(boolean reset) {
222 if (mTitleBarAnimator != null) {
223 mTitleBarAnimator.cancel();
224 mTitleBarAnimator = null;
225 }
226 if (reset) {
227 setTranslationY(0);
228 }
229 }
230
231 private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() {
232
John Reck0f602f32011-07-07 15:38:43 -0700233 @Override
234 public void onAnimationStart(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700235 }
236
237 @Override
238 public void onAnimationRepeat(Animator animation) {
239 }
240
241 @Override
242 public void onAnimationEnd(Animator animation) {
Michael Kolb4923c222012-04-02 16:18:36 -0700243 // update position
244 onScrollChanged();
John Reck0f602f32011-07-07 15:38:43 -0700245 }
246
247 @Override
248 public void onAnimationCancel(Animator animation) {
John Reck0f602f32011-07-07 15:38:43 -0700249 }
250 };
251
252 private int getVisibleTitleHeight() {
253 Tab tab = mBaseUi.getActiveTab();
254 WebView webview = tab != null ? tab.getWebView() : null;
255 return webview != null ? webview.getVisibleTitleHeight() : 0;
256 }
257
258 /**
259 * Update the progress, from 0 to 100.
260 */
261 public void setProgress(int newProgress) {
262 if (newProgress >= PROGRESS_MAX) {
263 mProgress.setProgress(PageProgressView.MAX_PROGRESS);
264 mProgress.setVisibility(View.GONE);
265 mInLoad = false;
266 mNavBar.onProgressStopped();
267 // check if needs to be hidden
John Reckef654f12011-07-12 16:42:08 -0700268 if (!isEditingUrl() && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700269 if (mUseQuickControls) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700270 hide();
John Reckbc82ec92012-04-19 13:12:35 -0700271 } else {
272 mBaseUi.showTitleBarForDuration();
John Reck0f602f32011-07-07 15:38:43 -0700273 }
274 }
275 } else {
276 if (!mInLoad) {
277 mProgress.setVisibility(View.VISIBLE);
278 mInLoad = true;
279 mNavBar.onProgressStarted();
280 }
281 mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
282 / PROGRESS_MAX);
Michael Kolbe8a82332012-04-25 14:14:26 -0700283 if (mUseQuickControls && !isEditingUrl()) {
284 setShowProgressOnly(true);
285 }
John Reck0f602f32011-07-07 15:38:43 -0700286 if (!mShowing) {
John Reck0f602f32011-07-07 15:38:43 -0700287 show();
288 }
289 }
290 }
291
292 public int getEmbeddedHeight() {
John Reck2711fab2012-05-18 21:38:59 -0700293 if (mUseQuickControls || mIsFixedTitleBar) return 0;
294 return calculateEmbeddedHeight();
295 }
296
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800297 public boolean isFixed() {
298 return mIsFixedTitleBar;
299 }
300
301 int calculateEmbeddedHeight() {
John Reck0f602f32011-07-07 15:38:43 -0700302 int height = mNavBar.getHeight();
John Reck0f602f32011-07-07 15:38:43 -0700303 return height;
304 }
305
John Reckef654f12011-07-12 16:42:08 -0700306 public boolean wantsToBeVisible() {
Bijan Amirzada289e1e62014-04-15 11:53:48 -0700307 return (mSnapshotBar != null && mSnapshotBar.getVisibility() == View.VISIBLE
John Reckef654f12011-07-12 16:42:08 -0700308 && mSnapshotBar.isAnimating());
309 }
310
John Reck0f602f32011-07-07 15:38:43 -0700311 public boolean isEditingUrl() {
312 return mNavBar.isEditingUrl();
313 }
314
315 public WebView getCurrentWebView() {
316 Tab t = mBaseUi.getActiveTab();
317 if (t != null) {
318 return t.getWebView();
319 } else {
320 return null;
321 }
322 }
323
324 public PageProgressView getProgressView() {
325 return mProgress;
326 }
327
328 public NavigationBarBase getNavigationBar() {
329 return mNavBar;
330 }
331
332 public boolean useQuickControls() {
333 return mUseQuickControls;
334 }
335
336 public boolean isInLoad() {
337 return mInLoad;
338 }
339
340 private ViewGroup.LayoutParams makeLayoutParams() {
Michael Kolb4923c222012-04-02 16:18:36 -0700341 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
342 LayoutParams.WRAP_CONTENT);
John Reck0f602f32011-07-07 15:38:43 -0700343 }
344
345 @Override
346 public View focusSearch(View focused, int dir) {
John Reckfcb60952012-05-30 09:45:28 -0700347 WebView web = getCurrentWebView();
John Reckd70419a2012-05-30 16:09:17 -0700348 if (FOCUS_DOWN == dir && hasFocus() && web != null
John Reckfcb60952012-05-30 09:45:28 -0700349 && web.hasFocusable() && web.getParent() != null) {
350 return web;
John Reck0f602f32011-07-07 15:38:43 -0700351 }
352 return super.focusSearch(focused, dir);
353 }
354
John Reckef654f12011-07-12 16:42:08 -0700355 public void onTabDataChanged(Tab tab) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100356 if (mSnapshotBar != null) {
357 mSnapshotBar.onTabDataChanged(tab);
358 }
359
John Reckef654f12011-07-12 16:42:08 -0700360 if (tab.isSnapshot()) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100361 inflateSnapshotBar();
John Reckef654f12011-07-12 16:42:08 -0700362 mSnapshotBar.setVisibility(VISIBLE);
363 mNavBar.setVisibility(GONE);
364 } else {
Ben Murdochd51bb572011-08-17 20:42:02 +0100365 if (mSnapshotBar != null) {
366 mSnapshotBar.setVisibility(GONE);
367 }
John Reckef654f12011-07-12 16:42:08 -0700368 mNavBar.setVisibility(VISIBLE);
369 }
370 }
371
Michael Kolb4923c222012-04-02 16:18:36 -0700372 public void onScrollChanged() {
John Reck2711fab2012-05-18 21:38:59 -0700373 if (!mShowing && !mIsFixedTitleBar) {
Michael Kolb4923c222012-04-02 16:18:36 -0700374 setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight());
375 }
376 }
377
John Reck1cc1d1d2012-09-04 18:13:51 -0700378 public void onResume() {
379 setFixedTitleBar();
380 }
381
John Reck0f602f32011-07-07 15:38:43 -0700382}