blob: fd19a414f5b122ce468aaa657500387f7babf882 [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
John Reck0f602f32011-07-07 15:38:43 -070019import android.content.Context;
Michael Kolb57059a82012-04-30 14:32:03 -070020import android.content.res.Configuration;
John Reck0f602f32011-07-07 15:38:43 -070021import android.content.res.Resources;
Panos Thomas752ce592014-10-16 13:00:35 -070022import android.graphics.Rect;
John Reck0f602f32011-07-07 15:38:43 -070023import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
Ben Murdochd51bb572011-08-17 20:42:02 +010026import android.view.ViewStub;
Kulanthaivel Palanichamy846bb0f2014-12-02 13:50:34 -080027import android.view.ViewTreeObserver;
John Reck1cc1d1d2012-09-04 18:13:51 -070028import android.view.accessibility.AccessibilityManager;
Vivek Sekhard4de6162015-07-21 15:01:45 -070029
30import org.codeaurora.swe.BrowserCommandLine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080031import org.codeaurora.swe.WebView;
32
John Reck0f602f32011-07-07 15:38:43 -070033import android.widget.FrameLayout;
John Reck0f602f32011-07-07 15:38:43 -070034
35/**
36 * Base class for a title bar used by the browser.
37 */
Kulanthaivel Palanichamy846bb0f2014-12-02 13:50:34 -080038public class TitleBar extends FrameLayout implements ViewTreeObserver.OnPreDrawListener {
John Reck0f602f32011-07-07 15:38:43 -070039
40 private static final int PROGRESS_MAX = 100;
41 private static final float ANIM_TITLEBAR_DECELERATE = 2.5f;
42
43 private UiController mUiController;
44 private BaseUi mBaseUi;
John Reck2711fab2012-05-18 21:38:59 -070045 private FrameLayout mContentView;
John Reck0f602f32011-07-07 15:38:43 -070046 private PageProgressView mProgress;
John Reck1cc1d1d2012-09-04 18:13:51 -070047 private AccessibilityManager mAccessibilityManager;
John Reck0f602f32011-07-07 15:38:43 -070048
John Reck0f602f32011-07-07 15:38:43 -070049 private NavigationBarBase mNavBar;
John Reckef654f12011-07-12 16:42:08 -070050 private SnapshotBar mSnapshotBar;
John Reck0f602f32011-07-07 15:38:43 -070051
52 //state
53 private boolean mShowing;
54 private boolean mInLoad;
John Reck2711fab2012-05-18 21:38:59 -070055 private boolean mIsFixedTitleBar;
Kulanthaivel Palanichamy846bb0f2014-12-02 13:50:34 -080056 private float mCurrentTranslationY;
57 private boolean mUpdateTranslationY = false;
John Reck0f602f32011-07-07 15:38:43 -070058
59 public TitleBar(Context context, UiController controller, BaseUi ui,
John Reck2711fab2012-05-18 21:38:59 -070060 FrameLayout contentView) {
John Reck0f602f32011-07-07 15:38:43 -070061 super(context, null);
62 mUiController = controller;
63 mBaseUi = ui;
John Reck2711fab2012-05-18 21:38:59 -070064 mContentView = contentView;
John Reck1cc1d1d2012-09-04 18:13:51 -070065 mAccessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
John Reck0f602f32011-07-07 15:38:43 -070066 initLayout(context);
John Reck0f9aaeb2012-05-23 14:40:19 -070067 setFixedTitleBar();
John Reck0f602f32011-07-07 15:38:43 -070068 }
69
70 private void initLayout(Context context) {
71 LayoutInflater factory = LayoutInflater.from(context);
72 factory.inflate(R.layout.title_bar, this);
73 mProgress = (PageProgressView) findViewById(R.id.progress);
John Reck0f602f32011-07-07 15:38:43 -070074 mNavBar = (NavigationBarBase) findViewById(R.id.taburlbar);
75 mNavBar.setTitleBar(this);
Ben Murdochd51bb572011-08-17 20:42:02 +010076 }
77
Ben Murdochd51bb572011-08-17 20:42:02 +010078 private void inflateSnapshotBar() {
79 if (mSnapshotBar != null) {
80 return;
81 }
82
83 ViewStub stub = (ViewStub) findViewById(R.id.snapshotbar_stub);
84 mSnapshotBar = (SnapshotBar) stub.inflate();
John Reckef654f12011-07-12 16:42:08 -070085 mSnapshotBar.setTitleBar(this);
John Reck0f602f32011-07-07 15:38:43 -070086 }
87
Michael Kolb57059a82012-04-30 14:32:03 -070088 @Override
89 protected void onConfigurationChanged(Configuration config) {
90 super.onConfigurationChanged(config);
John Reck0f9aaeb2012-05-23 14:40:19 -070091 setFixedTitleBar();
John Reck2711fab2012-05-18 21:38:59 -070092 }
93
94 @Override
95 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
96 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Vivek Sekhar8a660782014-10-27 17:00:53 -070097 mBaseUi.setContentViewMarginTop(0);
John Reck2711fab2012-05-18 21:38:59 -070098 }
99
Kulanthaivel Palanichamy846bb0f2014-12-02 13:50:34 -0800100 @Override
101 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
102 super.onLayout(changed, left, top, right, bottom);
103
104 mCurrentTranslationY = this.getTranslationY();
105 if (mCurrentTranslationY < 0) {
106 mUpdateTranslationY = true;
107 this.setTranslationY(0);
108
109 final ViewTreeObserver observer = this.getViewTreeObserver();
110 observer.addOnPreDrawListener(this);
111 }
112 }
113
114 @Override
115 public boolean onPreDraw() {
116 if (mUpdateTranslationY) {
117 this.setTranslationY(mCurrentTranslationY);
118 mUpdateTranslationY = false;
119 }
120 final ViewTreeObserver observer = this.getViewTreeObserver();
121 observer.removeOnPreDrawListener(this);
122 return true;
123 }
124
John Reck0f9aaeb2012-05-23 14:40:19 -0700125 private void setFixedTitleBar() {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700126 boolean isFixed = !getContext().getResources().getBoolean(R.bool.hide_title) ||
127 BrowserCommandLine.hasSwitch(BrowserSwitches.DISABLE_TOP_CONTROLS);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700128
Vivek Sekhar80572142014-12-01 10:54:22 -0800129 isFixed |= mAccessibilityManager.isEnabled() &&
130 mAccessibilityManager.isTouchExplorationEnabled();
John Reck2711fab2012-05-18 21:38:59 -0700131 // If getParent() returns null, we are initializing
132 ViewGroup parent = (ViewGroup)getParent();
133 if (mIsFixedTitleBar == isFixed && parent != null) return;
134 mIsFixedTitleBar = isFixed;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700135 showTopControls(false);
John Reck2711fab2012-05-18 21:38:59 -0700136 if (parent != null) {
137 parent.removeView(this);
138 }
Vivek Sekhar8a660782014-10-27 17:00:53 -0700139 mContentView.addView(this, makeLayoutParams());
140 mBaseUi.setContentViewMarginTop(0);
Michael Kolb57059a82012-04-30 14:32:03 -0700141 }
142
John Reck0f602f32011-07-07 15:38:43 -0700143 public BaseUi getUi() {
144 return mBaseUi;
145 }
146
147 public UiController getUiController() {
148 return mUiController;
149 }
150
John Reck0f602f32011-07-07 15:38:43 -0700151 void setShowProgressOnly(boolean progress) {
John Reckef654f12011-07-12 16:42:08 -0700152 if (progress && !wantsToBeVisible()) {
John Reck0f602f32011-07-07 15:38:43 -0700153 mNavBar.setVisibility(View.GONE);
154 } else {
155 mNavBar.setVisibility(View.VISIBLE);
156 }
157 }
158
John Reck0f602f32011-07-07 15:38:43 -0700159 boolean isShowing() {
160 return mShowing;
161 }
162
John Reck0f602f32011-07-07 15:38:43 -0700163 private int getVisibleTitleHeight() {
164 Tab tab = mBaseUi.getActiveTab();
165 WebView webview = tab != null ? tab.getWebView() : null;
166 return webview != null ? webview.getVisibleTitleHeight() : 0;
167 }
168
Vivek Sekhare337acf2015-04-02 21:00:48 -0700169 protected void hideTopControls(boolean animate) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700170 if (mIsFixedTitleBar)
171 return;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700172 Tab tab = mBaseUi.getActiveTab();
173 WebView view = tab != null ? tab.getWebView() : null;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700174 if (view != null) {
Vivek Sekhare337acf2015-04-02 21:00:48 -0700175 view.updateTopControls(true, false, animate);
Vivek Sekhard4de6162015-07-21 15:01:45 -0700176 }
177 mShowing = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700178 }
179
Vivek Sekhare337acf2015-04-02 21:00:48 -0700180 protected void showTopControls(boolean animate) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700181 Tab tab = mBaseUi.getActiveTab();
182 WebView view = tab != null ? tab.getWebView() : null;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700183 if (view != null) {
Vivek Sekhare337acf2015-04-02 21:00:48 -0700184 view.updateTopControls(false, true, animate);
Vivek Sekhard4de6162015-07-21 15:01:45 -0700185 }
186 mShowing = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700187 }
188
Vivek Sekhare337acf2015-04-02 21:00:48 -0700189 protected void enableTopControls(boolean animate) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700190 if (mIsFixedTitleBar || mNavBar.getTrustLevel() == SiteTileView.TRUST_AVOID
191 || mNavBar.getTrustLevel() == SiteTileView.TRUST_UNTRUSTED)
Vivek Sekhard4de6162015-07-21 15:01:45 -0700192 return;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700193 Tab tab = mBaseUi.getActiveTab();
194 WebView view = tab != null ? tab.getWebView() : null;
195 if (view != null)
Vivek Sekhare337acf2015-04-02 21:00:48 -0700196 view.updateTopControls(true, true, animate);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700197 }
198
199
John Reck0f602f32011-07-07 15:38:43 -0700200 /**
201 * Update the progress, from 0 to 100.
202 */
203 public void setProgress(int newProgress) {
204 if (newProgress >= PROGRESS_MAX) {
205 mProgress.setProgress(PageProgressView.MAX_PROGRESS);
206 mProgress.setVisibility(View.GONE);
207 mInLoad = false;
208 mNavBar.onProgressStopped();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700209 //onPageFinished
Vivek Sekhard4de6162015-07-21 15:01:45 -0700210 enableTopControls(true);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700211
John Reck0f602f32011-07-07 15:38:43 -0700212 } else {
213 if (!mInLoad) {
214 mProgress.setVisibility(View.VISIBLE);
215 mInLoad = true;
216 mNavBar.onProgressStarted();
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700217 mProgress.onProgressStarted();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700218 //onPageStarted
John Reck0f602f32011-07-07 15:38:43 -0700219 }
220 mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
221 / PROGRESS_MAX);
Vivek Sekhare337acf2015-04-02 21:00:48 -0700222 showTopControls(false);
John Reck0f602f32011-07-07 15:38:43 -0700223 }
224 }
225
226 public int getEmbeddedHeight() {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700227 if (mIsFixedTitleBar) return 0;
John Reck2711fab2012-05-18 21:38:59 -0700228 return calculateEmbeddedHeight();
229 }
230
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800231 public boolean isFixed() {
232 return mIsFixedTitleBar;
233 }
234
235 int calculateEmbeddedHeight() {
John Reck0f602f32011-07-07 15:38:43 -0700236 int height = mNavBar.getHeight();
John Reck0f602f32011-07-07 15:38:43 -0700237 return height;
238 }
239
John Reckef654f12011-07-12 16:42:08 -0700240 public boolean wantsToBeVisible() {
Bijan Amirzada289e1e62014-04-15 11:53:48 -0700241 return (mSnapshotBar != null && mSnapshotBar.getVisibility() == View.VISIBLE
John Reckef654f12011-07-12 16:42:08 -0700242 && mSnapshotBar.isAnimating());
243 }
244
John Reck0f602f32011-07-07 15:38:43 -0700245 public boolean isEditingUrl() {
246 return mNavBar.isEditingUrl();
247 }
248
249 public WebView getCurrentWebView() {
250 Tab t = mBaseUi.getActiveTab();
251 if (t != null) {
252 return t.getWebView();
253 } else {
254 return null;
255 }
256 }
257
258 public PageProgressView getProgressView() {
259 return mProgress;
260 }
261
262 public NavigationBarBase getNavigationBar() {
263 return mNavBar;
264 }
265
John Reck0f602f32011-07-07 15:38:43 -0700266 public boolean isInLoad() {
267 return mInLoad;
268 }
269
270 private ViewGroup.LayoutParams makeLayoutParams() {
Michael Kolb4923c222012-04-02 16:18:36 -0700271 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
272 LayoutParams.WRAP_CONTENT);
John Reck0f602f32011-07-07 15:38:43 -0700273 }
274
275 @Override
Panos Thomas752ce592014-10-16 13:00:35 -0700276 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
277 return mBaseUi.isCustomViewShowing() ? false :
278 super.requestFocus(direction, previouslyFocusedRect);
279 }
280
281 @Override
John Reck0f602f32011-07-07 15:38:43 -0700282 public View focusSearch(View focused, int dir) {
John Reckfcb60952012-05-30 09:45:28 -0700283 WebView web = getCurrentWebView();
John Reckd70419a2012-05-30 16:09:17 -0700284 if (FOCUS_DOWN == dir && hasFocus() && web != null
John Reckfcb60952012-05-30 09:45:28 -0700285 && web.hasFocusable() && web.getParent() != null) {
286 return web;
John Reck0f602f32011-07-07 15:38:43 -0700287 }
288 return super.focusSearch(focused, dir);
289 }
290
John Reckef654f12011-07-12 16:42:08 -0700291 public void onTabDataChanged(Tab tab) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100292 if (mSnapshotBar != null) {
293 mSnapshotBar.onTabDataChanged(tab);
294 }
295
Ze G Riande2a675c22015-06-03 11:15:24 -0700296 if (tab.isSnapshot() || tab.isDistilled()) {
Ben Murdochd51bb572011-08-17 20:42:02 +0100297 inflateSnapshotBar();
John Reckef654f12011-07-12 16:42:08 -0700298 mSnapshotBar.setVisibility(VISIBLE);
299 mNavBar.setVisibility(GONE);
Ze G Riande2a675c22015-06-03 11:15:24 -0700300 if (tab.isDistilled()) {
301 mSnapshotBar.setTitle(tab.getWebView().getTitle());
302 mSnapshotBar.setDate(tab.getNonDistilledUrl());
303 mSnapshotBar.setSnapshoticonVisibility(View.GONE);
304 mSnapshotBar.setFaviconVisibility(View.GONE);
305 mSnapshotBar.setReadericonVisibility(View.VISIBLE);
306 } else {
307 mSnapshotBar.setSnapshoticonVisibility(View.VISIBLE);
Sagar Dhawan90236142015-07-24 14:16:38 -0700308 mSnapshotBar.setFaviconVisibility(View.GONE); // Snapshot Tabs don't have a Favicon
Ze G Riande2a675c22015-06-03 11:15:24 -0700309 mSnapshotBar.setReadericonVisibility(View.GONE);
310 }
John Reckef654f12011-07-12 16:42:08 -0700311 } else {
Ben Murdochd51bb572011-08-17 20:42:02 +0100312 if (mSnapshotBar != null) {
313 mSnapshotBar.setVisibility(GONE);
314 }
John Reckef654f12011-07-12 16:42:08 -0700315 mNavBar.setVisibility(VISIBLE);
316 }
317 }
318
Michael Kolb4923c222012-04-02 16:18:36 -0700319 public void onScrollChanged() {
John Reck2711fab2012-05-18 21:38:59 -0700320 if (!mShowing && !mIsFixedTitleBar) {
Michael Kolb4923c222012-04-02 16:18:36 -0700321 setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight());
322 }
323 }
324
John Reck1cc1d1d2012-09-04 18:13:51 -0700325 public void onResume() {
326 setFixedTitleBar();
327 }
328
John Reck0f602f32011-07-07 15:38:43 -0700329}