blob: 3ab009817c9f0a04d8d9b8efbdc4bc935d7f5f5a [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -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
17package com.android.browser;
18
John Reck74830e12011-03-14 11:43:05 -070019import android.animation.ObjectAnimator;
Michael Kolb8233fac2010-10-26 16:08:53 -070020import android.app.Activity;
John Reckb9a051b2011-03-18 11:55:48 -070021import android.content.pm.PackageManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070022import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Michael Kolb5a4372f2011-04-29 13:53:10 -070026import android.graphics.Color;
27import android.graphics.drawable.BitmapDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.graphics.drawable.Drawable;
Michael Kolb5a4372f2011-04-29 13:53:10 -070029import android.graphics.drawable.LayerDrawable;
30import android.graphics.drawable.PaintDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.os.Bundle;
John Reck5d43ce82011-06-21 17:16:51 -070032import android.os.Handler;
33import android.os.Message;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.text.TextUtils;
35import android.util.Log;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.view.Gravity;
37import android.view.LayoutInflater;
38import android.view.Menu;
John Reckbf2ec202011-06-29 17:47:38 -070039import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070040import android.view.View;
Michael Kolb1514bb72010-11-22 09:11:48 -080041import android.view.View.OnClickListener;
John Reckbf2ec202011-06-29 17:47:38 -070042import android.view.View.OnTouchListener;
43import android.view.ViewConfiguration;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080045import android.view.ViewGroup.LayoutParams;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.view.WindowManager;
Michael Kolb3a696282010-12-05 13:23:24 -080047import android.view.inputmethod.InputMethodManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.webkit.WebChromeClient;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.webkit.WebView;
50import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080051import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.widget.LinearLayout;
53import android.widget.Toast;
54
John Reckbf2ec202011-06-29 17:47:38 -070055import com.android.browser.Tab.LockIcon;
56import com.android.internal.view.menu.MenuBuilder;
57
Michael Kolb1bf23132010-11-19 12:55:12 -080058import java.util.List;
59
Michael Kolb8233fac2010-10-26 16:08:53 -070060/**
61 * UI interface definitions
62 */
John Reckbf2ec202011-06-29 17:47:38 -070063public abstract class BaseUi implements UI, WebViewFactory, OnTouchListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070064
65 private static final String LOGTAG = "BaseUi";
66
Michael Kolb66706532010-12-12 19:50:22 -080067 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070068 new FrameLayout.LayoutParams(
69 ViewGroup.LayoutParams.MATCH_PARENT,
70 ViewGroup.LayoutParams.MATCH_PARENT);
71
Michael Kolb66706532010-12-12 19:50:22 -080072 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070073 new FrameLayout.LayoutParams(
74 ViewGroup.LayoutParams.MATCH_PARENT,
75 ViewGroup.LayoutParams.MATCH_PARENT,
76 Gravity.CENTER);
77
John Reck5d43ce82011-06-21 17:16:51 -070078 private static final int MSG_HIDE_TITLEBAR = 1;
79 private static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
80
Michael Kolb8233fac2010-10-26 16:08:53 -070081 Activity mActivity;
82 UiController mUiController;
83 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080084 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080085 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070086
87 private Drawable mSecLockIcon;
88 private Drawable mMixLockIcon;
Michael Kolb5a4372f2011-04-29 13:53:10 -070089 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070090
Michael Kolb66706532010-12-12 19:50:22 -080091 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070092 protected FrameLayout mCustomViewContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070093
94 private View mCustomView;
95 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040096 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070097
98 private CombinedBookmarkHistoryView mComboView;
99
100 private LinearLayout mErrorConsoleContainer = null;
101
102 private Toast mStopToast;
Michael Kolb8233fac2010-10-26 16:08:53 -0700103
John Reckbf2ec202011-06-29 17:47:38 -0700104 private float mInitialY;
John Reck5d43ce82011-06-21 17:16:51 -0700105 private int mTitlebarScrollTriggerSlop;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700106
Michael Kolb8233fac2010-10-26 16:08:53 -0700107 // the default <video> poster
108 private Bitmap mDefaultVideoPoster;
109 // the video progress view
110 private View mVideoProgressView;
111
Michael Kolb8233fac2010-10-26 16:08:53 -0700112 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700113 protected boolean mUseQuickControls;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114
115 public BaseUi(Activity browser, UiController controller) {
116 mActivity = browser;
117 mUiController = controller;
118 mTabControl = controller.getTabControl();
119 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800120 mInputManager = (InputMethodManager)
121 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Michael Kolb5a72f182011-01-13 20:35:06 -0800122 mSecLockIcon = res.getDrawable(R.drawable.ic_secure_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure);
124
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
126 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700127 LayoutInflater.from(mActivity)
128 .inflate(R.layout.custom_screen, frameLayout);
129 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 R.id.main_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700131 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 .findViewById(R.id.error_console);
John Reck7c6e1c92011-06-30 11:55:55 -0700133 mCustomViewContainer = (FrameLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 .findViewById(R.id.fullscreen_custom_content);
Michael Kolbc38c6042011-04-27 10:46:06 -0700135 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700136 mGenericFavicon = res.getDrawable(
137 R.drawable.app_web_browser_sm);
John Reck5d43ce82011-06-21 17:16:51 -0700138 ViewConfiguration config = ViewConfiguration.get(browser);
139 mTitlebarScrollTriggerSlop = Math.max(
140 config.getScaledOverflingDistance(),
141 config.getScaledOverscrollDistance());
142 mTitlebarScrollTriggerSlop = Math.max(mTitlebarScrollTriggerSlop,
143 config.getScaledTouchSlop());
Michael Kolb8233fac2010-10-26 16:08:53 -0700144 }
145
John Reckb9a051b2011-03-18 11:55:48 -0700146 @Override
147 public WebView createWebView(boolean privateBrowsing) {
148 // Create a new WebView
149 BrowserWebView w = new BrowserWebView(mActivity, null,
150 android.R.attr.webViewStyle, privateBrowsing);
151 initWebViewSettings(w);
152 return w;
153 }
154
155 @Override
156 public WebView createSubWebView(boolean privateBrowsing) {
157 return createWebView(privateBrowsing);
158 }
159
Michael Kolb66706532010-12-12 19:50:22 -0800160 /**
161 * common webview initialization
162 * @param w the webview to initialize
163 */
164 protected void initWebViewSettings(WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700165 w.setScrollbarFadingEnabled(true);
166 w.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
167 w.setMapTrackballToArrowKeys(false); // use trackball directly
168 // Enable the built-in zoom
169 w.getSettings().setBuiltInZoomControls(true);
John Reckb9a051b2011-03-18 11:55:48 -0700170 boolean supportsMultiTouch = mActivity.getPackageManager()
171 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
172 w.getSettings().setDisplayZoomControls(!supportsMultiTouch);
173 w.setExpandedTileBounds(true); // smoother scrolling
Michael Kolb8233fac2010-10-26 16:08:53 -0700174
175 // Add this WebView to the settings observer list and update the
176 // settings
177 final BrowserSettings s = BrowserSettings.getInstance();
John Reck35e9dd62011-04-25 09:01:54 -0700178 s.startManagingSettings(w.getSettings());
Michael Kolb8233fac2010-10-26 16:08:53 -0700179 }
180
181 private void cancelStopToast() {
182 if (mStopToast != null) {
183 mStopToast.cancel();
184 mStopToast = null;
185 }
186 }
187
188 // lifecycle
189
190 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800191 if (isCustomViewShowing()) {
192 onHideCustomView();
193 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 cancelStopToast();
195 mActivityPaused = true;
196 }
197
198 public void onResume() {
199 mActivityPaused = false;
200 }
201
Michael Kolb66706532010-12-12 19:50:22 -0800202 protected boolean isActivityPaused() {
203 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 }
205
206 public void onConfigurationChanged(Configuration config) {
207 }
208
209 // key handling
210
211 @Override
212 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 if (mComboView != null) {
214 if (!mComboView.onBackPressed()) {
215 mUiController.removeComboView();
216 }
217 return true;
218 }
219 if (mCustomView != null) {
220 mUiController.hideCustomView();
221 return true;
222 }
223 return false;
224 }
225
Michael Kolb2814a362011-05-19 15:49:41 -0700226 @Override
227 public boolean onMenuKey() {
228 return false;
229 }
230
John Reck30c714c2010-12-16 17:30:34 -0800231 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 @Override
John Reck30c714c2010-12-16 17:30:34 -0800233 public void onTabDataChanged(Tab tab) {
234 setUrlTitle(tab);
235 setFavicon(tab);
236 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800237 updateNavigationState(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 }
239
240 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500241 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800242 if (tab.inForeground()) {
243 boolean isBookmark = tab.isBookmarkedSite();
244 getTitleBar().setCurrentUrlIsBookmark(isBookmark);
245 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500246 }
247
248 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 public void onPageStopped(Tab tab) {
250 cancelStopToast();
251 if (tab.inForeground()) {
252 mStopToast = Toast
253 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
254 mStopToast.show();
255 }
256 }
257
258 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800259 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700260 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800261 }
262
263 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 }
266
267 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800268 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700269 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800270 if ((tab != mActiveTab) && (mActiveTab != null)) {
271 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700272 WebView web = mActiveTab.getWebView();
273 if (web != null) {
274 web.setOnTouchListener(null);
275 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 }
Michael Kolb77df4562010-11-19 14:49:34 -0800277 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700278 WebView web = mActiveTab.getWebView();
279 if (web != null && !mUseQuickControls) {
280 web.setOnTouchListener(this);
281 }
John Reck5d43ce82011-06-21 17:16:51 -0700282 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800284 onTabDataChanged(tab);
285 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800286 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800287 getTitleBar().setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500288 updateAutoLogin(tab, false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700289 }
290
Michael Kolbcfa3af52010-12-14 10:36:11 -0800291 Tab getActiveTab() {
292 return mActiveTab;
293 }
294
Michael Kolb8233fac2010-10-26 16:08:53 -0700295 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800296 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800297 }
298
299 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700300 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800301 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700302 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800303 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700304 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700305 }
306
307 @Override
308 public void detachTab(Tab tab) {
309 removeTabFromContentView(tab);
310 }
311
312 @Override
313 public void attachTab(Tab tab) {
314 attachTabToContentView(tab);
315 }
316
Michael Kolb377ea312011-02-17 14:36:56 -0800317 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800318 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700319 return;
320 }
321 View container = tab.getViewContainer();
322 WebView mainView = tab.getWebView();
323 // Attach the WebView to the container and then attach the
324 // container to the content view.
325 FrameLayout wrapper =
326 (FrameLayout) container.findViewById(R.id.webview_wrapper);
327 ViewGroup parent = (ViewGroup) mainView.getParent();
328 if (parent != wrapper) {
329 if (parent != null) {
330 Log.w(LOGTAG, "mMainView already has a parent in"
331 + " attachTabToContentView!");
332 parent.removeView(mainView);
333 }
334 wrapper.addView(mainView);
335 } else {
336 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
337 + " attachTabToContentView!");
338 }
339 parent = (ViewGroup) container.getParent();
340 if (parent != mContentView) {
341 if (parent != null) {
342 Log.w(LOGTAG, "mContainer already has a parent in"
343 + " attachTabToContentView!");
344 parent.removeView(container);
345 }
346 mContentView.addView(container, COVER_SCREEN_PARAMS);
347 } else {
348 Log.w(LOGTAG, "mContainer is already attached to content in"
349 + " attachTabToContentView!");
350 }
351 mUiController.attachSubWindow(tab);
352 }
353
354 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800355 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 // Remove the container that contains the main WebView.
357 WebView mainView = tab.getWebView();
358 View container = tab.getViewContainer();
359 if (mainView == null) {
360 return;
361 }
362 // Remove the container from the content and then remove the
363 // WebView from the container. This will trigger a focus change
364 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800365 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 FrameLayout wrapper =
367 (FrameLayout) container.findViewById(R.id.webview_wrapper);
368 wrapper.removeView(mainView);
369 mContentView.removeView(container);
370 mUiController.endActionMode();
371 mUiController.removeSubWindow(tab);
372 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
373 if (errorConsole != null) {
374 mErrorConsoleContainer.removeView(errorConsole);
375 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700376 }
377
Michael Kolba713ec82010-11-29 17:27:06 -0800378 @Override
379 public void onSetWebView(Tab tab, WebView webView) {
380 View container = tab.getViewContainer();
381 if (container == null) {
382 // The tab consists of a container view, which contains the main
383 // WebView, as well as any other UI elements associated with the tab.
384 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700385 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800386 tab.setViewContainer(container);
387 }
388 if (tab.getWebView() != webView) {
389 // Just remove the old one.
390 FrameLayout wrapper =
391 (FrameLayout) container.findViewById(R.id.webview_wrapper);
392 wrapper.removeView(tab.getWebView());
393 }
394 }
395
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800397 * create a sub window container and webview for the tab
398 * Note: this methods operates through side-effects for now
399 * it sets both the subView and subViewContainer for the given tab
400 * @param tab tab to create the sub window for
401 * @param subView webview to be set as a subwindow for the tab
402 */
403 @Override
404 public void createSubWindow(Tab tab, WebView subView) {
405 View subViewContainer = mActivity.getLayoutInflater().inflate(
406 R.layout.browser_subwindow, null);
407 ViewGroup inner = (ViewGroup) subViewContainer
408 .findViewById(R.id.inner_container);
409 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
410 LayoutParams.MATCH_PARENT));
411 final ImageButton cancel = (ImageButton) subViewContainer
412 .findViewById(R.id.subwindow_close);
413 final WebView cancelSubView = subView;
414 cancel.setOnClickListener(new OnClickListener() {
415 public void onClick(View v) {
416 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
417 }
418 });
419 tab.setSubWebView(subView);
420 tab.setSubViewContainer(subViewContainer);
421 }
422
423 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700424 * Remove the sub window from the content view.
425 */
426 @Override
427 public void removeSubWindow(View subviewContainer) {
428 mContentView.removeView(subviewContainer);
429 mUiController.endActionMode();
430 }
431
432 /**
433 * Attach the sub window to the content view.
434 */
435 @Override
436 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800437 if (container.getParent() != null) {
438 // already attached, remove first
439 ((ViewGroup) container.getParent()).removeView(container);
440 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700441 mContentView.addView(container, COVER_SCREEN_PARAMS);
442 }
443
Michael Kolb11d19782011-03-20 10:17:40 -0700444 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700445 WebView web = getWebView();
446 if (web != null) {
447 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700448 }
449 }
450
Michael Kolb46f987e2011-04-05 10:39:10 -0700451 public void editUrl(boolean clearInput) {
452 if (mUiController.isInCustomActionMode()) {
453 mUiController.endActionMode();
454 }
455 showTitleBar();
456 getTitleBar().startEditingUrl(clearInput);
457 }
458
Michael Kolb7cdc4902011-02-03 17:54:40 -0800459 boolean canShowTitleBar() {
460 return !isTitleBarShowing()
461 && !isActivityPaused()
462 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700463 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800464 && !mUiController.isInCustomActionMode();
465 }
466
467 void showTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700468 if (canShowTitleBar()) {
469 getTitleBar().show();
470 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800471 }
472
473 protected void hideTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700474 if (getTitleBar().isShowing()) {
475 getTitleBar().hide();
476 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800477 }
478
479 protected boolean isTitleBarShowing() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700480 return getTitleBar().isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800481 }
482
John Reck5d43ce82011-06-21 17:16:51 -0700483 public boolean isEditingUrl() {
484 return getTitleBar().isEditingUrl();
485 }
486
Michael Kolb7cdc4902011-02-03 17:54:40 -0800487 protected abstract TitleBarBase getTitleBar();
488
489 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700490 WebView web = getWebView();
491 if (web != null) {
492 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 }
494 }
495
Michael Kolb66706532010-12-12 19:50:22 -0800496 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700497 public void showVoiceTitleBar(String title, List<String> results) {
498 getTitleBar().setInVoiceMode(true, results);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800499 getTitleBar().setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700500 }
501
Michael Kolb66706532010-12-12 19:50:22 -0800502 @Override
503 public void revertVoiceTitleBar(Tab tab) {
Michael Kolb11d19782011-03-20 10:17:40 -0700504 getTitleBar().setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800505 String url = tab.getUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800506 getTitleBar().setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700507 }
508
509 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700510 public void registerDropdownChangeListener(DropdownChangeListener d) {
511 getTitleBar().registerDropdownChangeListener(d);
512 }
513
514 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700515 public void showComboView(boolean startWithHistory, Bundle extras) {
John Reck439c9a52010-12-14 10:04:39 -0800516 if (mComboView != null) {
517 return;
518 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 mComboView = new CombinedBookmarkHistoryView(mActivity,
520 mUiController,
521 startWithHistory ?
522 CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY
523 : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS,
524 extras);
Michael Kolb47d63f82011-01-18 10:48:40 -0800525 FrameLayout wrapper =
526 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
527 wrapper.setVisibility(View.GONE);
Michael Kolbc16c5952011-03-29 15:37:03 -0700528 getTitleBar().stopEditingUrl();
Michael Kolb3a696282010-12-05 13:23:24 -0800529 dismissIME();
Michael Kolbc16c5952011-03-29 15:37:03 -0700530 hideTitleBar();
Michael Kolb3a696282010-12-05 13:23:24 -0800531 if (mActiveTab != null) {
Michael Kolb3a696282010-12-05 13:23:24 -0800532 mActiveTab.putInBackground();
533 }
John Reck74830e12011-03-14 11:43:05 -0700534 mComboView.setAlpha(0f);
535 ObjectAnimator anim = ObjectAnimator.ofFloat(mComboView, "alpha", 0f, 1f);
536 Resources res = mActivity.getResources();
537 anim.setDuration(res.getInteger(R.integer.comboViewFadeInDuration));
538 anim.start();
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
540 }
541
Michael Kolbba238702011-03-08 10:40:50 -0800542 public boolean isComboViewShowing() {
543 return (mComboView != null);
544 }
545
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 /**
547 * dismiss the ComboPage
548 */
549 @Override
550 public void hideComboView() {
551 if (mComboView != null) {
552 mContentView.removeView(mComboView);
Michael Kolb47d63f82011-01-18 10:48:40 -0800553 FrameLayout wrapper =
554 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
555 wrapper.setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 mComboView = null;
557 }
Michael Kolb3a696282010-12-05 13:23:24 -0800558 if (mActiveTab != null) {
559 mActiveTab.putInForeground();
560 }
John Reckb3417f02011-01-14 11:01:05 -0800561 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700562 }
563
564 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400565 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700566 WebChromeClient.CustomViewCallback callback) {
567 // if a view already exists then immediately terminate the new one
568 if (mCustomView != null) {
569 callback.onCustomViewHidden();
570 return;
571 }
572
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400573 mOriginalOrientation = mActivity.getRequestedOrientation();
574
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 // Add the custom view to its container.
576 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
577 mCustomView = view;
578 mCustomViewCallback = callback;
579 // Hide the content view.
580 mContentView.setVisibility(View.GONE);
581 // Finally show the custom view container.
582 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400583 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700584 mCustomViewContainer.setVisibility(View.VISIBLE);
585 mCustomViewContainer.bringToFront();
586 }
587
588 @Override
589 public void onHideCustomView() {
590 if (mCustomView == null)
591 return;
592
593 // Hide the custom view.
594 mCustomView.setVisibility(View.GONE);
595 // Remove the custom view from its container.
596 mCustomViewContainer.removeView(mCustomView);
597 mCustomView = null;
598 mCustomViewContainer.setVisibility(View.GONE);
599 mCustomViewCallback.onCustomViewHidden();
600 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400601 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700602 setStatusBarVisibility(true);
603 mContentView.setVisibility(View.VISIBLE);
604 }
605
606 @Override
607 public boolean isCustomViewShowing() {
608 return mCustomView != null;
609 }
610
Michael Kolb66706532010-12-12 19:50:22 -0800611 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800612 if (mInputManager.isActive()) {
613 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
614 0);
615 }
616 }
617
Michael Kolb66706532010-12-12 19:50:22 -0800618 @Override
619 public boolean showsWeb() {
620 return mCustomView == null
621 && mComboView == null;
622 }
623
Patrick Scott92066772011-03-10 08:46:27 -0500624 @Override
625 public void showAutoLogin(Tab tab) {
626 updateAutoLogin(tab, true);
627 }
628
629 @Override
630 public void hideAutoLogin(Tab tab) {
631 updateAutoLogin(tab, true);
632 }
633
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 // -------------------------------------------------------------------------
635
Michael Kolb5a72f182011-01-13 20:35:06 -0800636 protected void updateNavigationState(Tab tab) {
637 }
638
Michael Kolb11d19782011-03-20 10:17:40 -0700639 protected void updateAutoLogin(Tab tab, boolean animate) {
640 getTitleBar().updateAutoLogin(tab, animate);
641 }
Patrick Scott92066772011-03-10 08:46:27 -0500642
Michael Kolb8233fac2010-10-26 16:08:53 -0700643 /**
644 * Update the lock icon to correspond to our latest state.
645 */
Michael Kolb66706532010-12-12 19:50:22 -0800646 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800647 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 updateLockIconImage(t.getLockIconType());
649 }
650 }
651
652 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 * Updates the lock-icon image in the title-bar.
654 */
John Reck30c714c2010-12-16 17:30:34 -0800655 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800657 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800659 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 d = mMixLockIcon;
661 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800662 getTitleBar().setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 }
664
John Reck30c714c2010-12-16 17:30:34 -0800665 protected void setUrlTitle(Tab tab) {
666 String url = tab.getUrl();
667 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800668 if (TextUtils.isEmpty(title)) {
669 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800670 }
Michael Kolb66706532010-12-12 19:50:22 -0800671 if (tab.isInVoiceSearchMode()) return;
672 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800673 getTitleBar().setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800674 }
675 }
676
677 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800678 protected void setFavicon(Tab tab) {
679 if (tab.inForeground()) {
680 Bitmap icon = tab.getFavicon();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800681 getTitleBar().setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800682 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 }
684
685 @Override
686 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 }
688
Michael Kolb66706532010-12-12 19:50:22 -0800689 // active tabs page
690
691 public void showActiveTabsPage() {
692 }
693
694 /**
695 * Remove the active tabs page.
696 */
697 public void removeActiveTabsPage() {
698 }
699
Michael Kolb8233fac2010-10-26 16:08:53 -0700700 // menu handling callbacks
701
702 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800703 public boolean onPrepareOptionsMenu(Menu menu) {
704 return true;
705 }
706
707 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 }
710
711 @Override
712 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 }
714
715 @Override
716 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 }
718
719 @Override
720 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 }
722
723 @Override
724 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 }
726
727 @Override
728 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 }
730
731 // error console
732
733 @Override
734 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800735 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700736 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
737 if (flag) {
738 // Setting the show state of the console will cause it's the layout
739 // to be inflated.
740 if (errorConsole.numberOfErrors() > 0) {
741 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
742 } else {
743 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
744 }
745 if (errorConsole.getParent() != null) {
746 mErrorConsoleContainer.removeView(errorConsole);
747 }
748 // Now we can add it to the main view.
749 mErrorConsoleContainer.addView(errorConsole,
750 new LinearLayout.LayoutParams(
751 ViewGroup.LayoutParams.MATCH_PARENT,
752 ViewGroup.LayoutParams.WRAP_CONTENT));
753 } else {
754 mErrorConsoleContainer.removeView(errorConsole);
755 }
756 }
757
758 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800759 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
760 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
761 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 }
763
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 // -------------------------------------------------------------------------
765 // Helper function for WebChromeClient
766 // -------------------------------------------------------------------------
767
768 @Override
769 public Bitmap getDefaultVideoPoster() {
770 if (mDefaultVideoPoster == null) {
771 mDefaultVideoPoster = BitmapFactory.decodeResource(
772 mActivity.getResources(), R.drawable.default_video_poster);
773 }
774 return mDefaultVideoPoster;
775 }
776
777 @Override
778 public View getVideoLoadingProgressView() {
779 if (mVideoProgressView == null) {
780 LayoutInflater inflater = LayoutInflater.from(mActivity);
781 mVideoProgressView = inflater.inflate(
782 R.layout.video_loading_progress, null);
783 }
784 return mVideoProgressView;
785 }
786
Michael Kolb843510f2010-12-09 10:51:49 -0800787 @Override
788 public void showMaxTabsWarning() {
789 Toast warning = Toast.makeText(mActivity,
790 mActivity.getString(R.string.max_tabs_warning),
791 Toast.LENGTH_SHORT);
792 warning.show();
793 }
794
Michael Kolbfdb70242011-03-24 09:41:11 -0700795 protected void captureTab(final Tab tab) {
796 captureTab(tab,
797 (int) mActivity.getResources()
798 .getDimension(R.dimen.qc_thumb_width),
799 (int) mActivity.getResources()
800 .getDimension(R.dimen.qc_thumb_height));
801 }
802
803 protected void captureTab(final Tab tab, int width, int height) {
804 if ((tab == null) || (tab.getWebView() == null)) return;
805 Bitmap sshot = Controller.createScreenshot(tab, width, height);
806 tab.setScreenshot(sshot);
807 }
808
Michael Kolb46f987e2011-04-05 10:39:10 -0700809 protected WebView getWebView() {
810 if (mActiveTab != null) {
811 return mActiveTab.getWebView();
812 } else {
813 return null;
814 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700815 }
816
Michael Kolbfedb4922011-04-20 16:45:33 -0700817 protected Menu getMenu() {
818 MenuBuilder menu = new MenuBuilder(mActivity);
819 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
820 return menu;
821 }
822
Michael Kolbc38c6042011-04-27 10:46:06 -0700823 public void setFullscreen(boolean enabled) {
824 if (enabled) {
825 mActivity.getWindow().setFlags(
826 WindowManager.LayoutParams.FLAG_FULLSCREEN,
827 WindowManager.LayoutParams.FLAG_FULLSCREEN);
828 } else {
829 mActivity.getWindow().clearFlags(
830 WindowManager.LayoutParams.FLAG_FULLSCREEN);
831 }
832 }
833
Michael Kolb5a4372f2011-04-29 13:53:10 -0700834 protected Drawable getFaviconDrawable(Bitmap icon) {
835 Drawable[] array = new Drawable[3];
836 array[0] = new PaintDrawable(Color.BLACK);
837 PaintDrawable p = new PaintDrawable(Color.WHITE);
838 array[1] = p;
839 if (icon == null) {
840 array[2] = mGenericFavicon;
841 } else {
842 array[2] = new BitmapDrawable(icon);
843 }
844 LayerDrawable d = new LayerDrawable(array);
845 d.setLayerInset(1, 1, 1, 1, 1);
846 d.setLayerInset(2, 2, 2, 2, 2);
847 return d;
848 }
849
John Reck5d43ce82011-06-21 17:16:51 -0700850 public boolean isLoading() {
851 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
852 }
853
854 /**
855 * Suggest to the UI that the title bar can be hidden. The UI will then
856 * decide whether or not to hide based off a number of factors, such
857 * as if the user is editing the URL bar or if the page is loading
858 */
859 public void suggestHideTitleBar() {
860 if (!isLoading() && !isEditingUrl()) {
861 hideTitleBar();
862 }
863 }
864
865 @Override
John Reckbf2ec202011-06-29 17:47:38 -0700866 public boolean onTouch(View v, MotionEvent event) {
867 switch (event.getAction()) {
868 case MotionEvent.ACTION_DOWN:
869 mInitialY = event.getY();
870 break;
871 case MotionEvent.ACTION_MOVE:
872 WebView web = (WebView) v;
873 if (!isTitleBarShowing()
874 && web.getVisibleTitleHeight() == 0
875 && event.getY() > (mInitialY + mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700876 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
877 showTitleBar();
John Reckbf2ec202011-06-29 17:47:38 -0700878 } else if (event.getY() < mInitialY) {
879 mInitialY = event.getY();
880 }
881 break;
882 case MotionEvent.ACTION_CANCEL:
883 case MotionEvent.ACTION_UP:
884 if (isTitleBarShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700885 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
886 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
John Reck5d43ce82011-06-21 17:16:51 -0700887 }
John Reckbf2ec202011-06-29 17:47:38 -0700888 break;
John Reck5d43ce82011-06-21 17:16:51 -0700889 }
John Reckbf2ec202011-06-29 17:47:38 -0700890 return false;
John Reck5d43ce82011-06-21 17:16:51 -0700891 }
892
893 private Handler mHandler = new Handler() {
894
895 @Override
896 public void handleMessage(Message msg) {
897 if (msg.what == MSG_HIDE_TITLEBAR) {
898 suggestHideTitleBar();
899 }
900 }
901 };
Michael Kolb8233fac2010-10-26 16:08:53 -0700902}