blob: ad459580bce17efce7dd946179351d6772f033b4 [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 Reck5d43ce82011-06-21 17:16:51 -070019import com.android.browser.BrowserWebView.ScrollListener;
John Reck30c714c2010-12-16 17:30:34 -080020import com.android.browser.Tab.LockIcon;
Michael Kolbfedb4922011-04-20 16:45:33 -070021import com.android.internal.view.menu.MenuBuilder;
John Reck30c714c2010-12-16 17:30:34 -080022
John Reck74830e12011-03-14 11:43:05 -070023import android.animation.ObjectAnimator;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.app.Activity;
John Reckb9a051b2011-03-18 11:55:48 -070025import android.content.pm.PackageManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.res.Configuration;
27import android.content.res.Resources;
28import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
Michael Kolb5a4372f2011-04-29 13:53:10 -070030import android.graphics.Color;
31import android.graphics.drawable.BitmapDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.graphics.drawable.Drawable;
Michael Kolb5a4372f2011-04-29 13:53:10 -070033import android.graphics.drawable.LayerDrawable;
34import android.graphics.drawable.PaintDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070035import android.os.Bundle;
John Reck5d43ce82011-06-21 17:16:51 -070036import android.os.Handler;
37import android.os.Message;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.text.TextUtils;
39import android.util.Log;
Michael Kolb8233fac2010-10-26 16:08:53 -070040import android.view.Gravity;
41import android.view.LayoutInflater;
42import android.view.Menu;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.view.View;
John Reck5d43ce82011-06-21 17:16:51 -070044import android.view.ViewConfiguration;
Michael Kolb1514bb72010-11-22 09:11:48 -080045import android.view.View.OnClickListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080047import android.view.ViewGroup.LayoutParams;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.view.WindowManager;
Michael Kolb3a696282010-12-05 13:23:24 -080049import android.view.inputmethod.InputMethodManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.webkit.WebChromeClient;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.webkit.WebView;
52import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080053import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.widget.LinearLayout;
55import android.widget.Toast;
56
Michael Kolb1bf23132010-11-19 12:55:12 -080057import java.util.List;
58
Michael Kolb8233fac2010-10-26 16:08:53 -070059/**
60 * UI interface definitions
61 */
John Reck5d43ce82011-06-21 17:16:51 -070062public abstract class BaseUi implements UI, WebViewFactory, ScrollListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070063
64 private static final String LOGTAG = "BaseUi";
65
Michael Kolb66706532010-12-12 19:50:22 -080066 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070067 new FrameLayout.LayoutParams(
68 ViewGroup.LayoutParams.MATCH_PARENT,
69 ViewGroup.LayoutParams.MATCH_PARENT);
70
Michael Kolb66706532010-12-12 19:50:22 -080071 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070072 new FrameLayout.LayoutParams(
73 ViewGroup.LayoutParams.MATCH_PARENT,
74 ViewGroup.LayoutParams.MATCH_PARENT,
75 Gravity.CENTER);
76
John Reck5d43ce82011-06-21 17:16:51 -070077 private static final int MSG_HIDE_TITLEBAR = 1;
78 private static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
79
Michael Kolb8233fac2010-10-26 16:08:53 -070080 Activity mActivity;
81 UiController mUiController;
82 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080083 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080084 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070085
86 private Drawable mSecLockIcon;
87 private Drawable mMixLockIcon;
Michael Kolb5a4372f2011-04-29 13:53:10 -070088 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070089
Michael Kolb8233fac2010-10-26 16:08:53 -070090 private FrameLayout mBrowserFrameLayout;
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 Reck5d43ce82011-06-21 17:16:51 -0700104 private int mLastScrollY;
105 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;
113
114 public BaseUi(Activity browser, UiController controller) {
115 mActivity = browser;
116 mUiController = controller;
117 mTabControl = controller.getTabControl();
118 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800119 mInputManager = (InputMethodManager)
120 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Michael Kolb5a72f182011-01-13 20:35:06 -0800121 mSecLockIcon = res.getDrawable(R.drawable.ic_secure_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure);
123
Michael Kolb8233fac2010-10-26 16:08:53 -0700124 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
125 .getDecorView().findViewById(android.R.id.content);
126 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(mActivity)
127 .inflate(R.layout.custom_screen, null);
128 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
129 R.id.main_content);
130 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
131 .findViewById(R.id.error_console);
132 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
133 .findViewById(R.id.fullscreen_custom_content);
134 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
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);
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 }
Michael Kolb77df4562010-11-19 14:49:34 -0800273 mActiveTab = tab;
John Reck5d43ce82011-06-21 17:16:51 -0700274 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800276 onTabDataChanged(tab);
277 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800278 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800279 getTitleBar().setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500280 updateAutoLogin(tab, false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 }
282
Michael Kolbcfa3af52010-12-14 10:36:11 -0800283 Tab getActiveTab() {
284 return mActiveTab;
285 }
286
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800288 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800289 }
290
291 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700292 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800293 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700294 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800295 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700297 }
298
299 @Override
300 public void detachTab(Tab tab) {
301 removeTabFromContentView(tab);
302 }
303
304 @Override
305 public void attachTab(Tab tab) {
306 attachTabToContentView(tab);
307 }
308
Michael Kolb377ea312011-02-17 14:36:56 -0800309 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800310 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700311 return;
312 }
313 View container = tab.getViewContainer();
314 WebView mainView = tab.getWebView();
315 // Attach the WebView to the container and then attach the
316 // container to the content view.
317 FrameLayout wrapper =
318 (FrameLayout) container.findViewById(R.id.webview_wrapper);
319 ViewGroup parent = (ViewGroup) mainView.getParent();
320 if (parent != wrapper) {
321 if (parent != null) {
322 Log.w(LOGTAG, "mMainView already has a parent in"
323 + " attachTabToContentView!");
324 parent.removeView(mainView);
325 }
326 wrapper.addView(mainView);
327 } else {
328 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
329 + " attachTabToContentView!");
330 }
331 parent = (ViewGroup) container.getParent();
332 if (parent != mContentView) {
333 if (parent != null) {
334 Log.w(LOGTAG, "mContainer already has a parent in"
335 + " attachTabToContentView!");
336 parent.removeView(container);
337 }
338 mContentView.addView(container, COVER_SCREEN_PARAMS);
339 } else {
340 Log.w(LOGTAG, "mContainer is already attached to content in"
341 + " attachTabToContentView!");
342 }
343 mUiController.attachSubWindow(tab);
344 }
345
346 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800347 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700348 // Remove the container that contains the main WebView.
349 WebView mainView = tab.getWebView();
350 View container = tab.getViewContainer();
351 if (mainView == null) {
352 return;
353 }
354 // Remove the container from the content and then remove the
355 // WebView from the container. This will trigger a focus change
356 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800357 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 FrameLayout wrapper =
359 (FrameLayout) container.findViewById(R.id.webview_wrapper);
360 wrapper.removeView(mainView);
361 mContentView.removeView(container);
362 mUiController.endActionMode();
363 mUiController.removeSubWindow(tab);
364 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
365 if (errorConsole != null) {
366 mErrorConsoleContainer.removeView(errorConsole);
367 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700368 }
369
Michael Kolba713ec82010-11-29 17:27:06 -0800370 @Override
371 public void onSetWebView(Tab tab, WebView webView) {
372 View container = tab.getViewContainer();
373 if (container == null) {
374 // The tab consists of a container view, which contains the main
375 // WebView, as well as any other UI elements associated with the tab.
376 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
377 null);
378 tab.setViewContainer(container);
379 }
380 if (tab.getWebView() != webView) {
381 // Just remove the old one.
382 FrameLayout wrapper =
383 (FrameLayout) container.findViewById(R.id.webview_wrapper);
384 wrapper.removeView(tab.getWebView());
385 }
386 }
387
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800389 * create a sub window container and webview for the tab
390 * Note: this methods operates through side-effects for now
391 * it sets both the subView and subViewContainer for the given tab
392 * @param tab tab to create the sub window for
393 * @param subView webview to be set as a subwindow for the tab
394 */
395 @Override
396 public void createSubWindow(Tab tab, WebView subView) {
397 View subViewContainer = mActivity.getLayoutInflater().inflate(
398 R.layout.browser_subwindow, null);
399 ViewGroup inner = (ViewGroup) subViewContainer
400 .findViewById(R.id.inner_container);
401 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
402 LayoutParams.MATCH_PARENT));
403 final ImageButton cancel = (ImageButton) subViewContainer
404 .findViewById(R.id.subwindow_close);
405 final WebView cancelSubView = subView;
406 cancel.setOnClickListener(new OnClickListener() {
407 public void onClick(View v) {
408 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
409 }
410 });
411 tab.setSubWebView(subView);
412 tab.setSubViewContainer(subViewContainer);
413 }
414
415 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700416 * Remove the sub window from the content view.
417 */
418 @Override
419 public void removeSubWindow(View subviewContainer) {
420 mContentView.removeView(subviewContainer);
421 mUiController.endActionMode();
422 }
423
424 /**
425 * Attach the sub window to the content view.
426 */
427 @Override
428 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800429 if (container.getParent() != null) {
430 // already attached, remove first
431 ((ViewGroup) container.getParent()).removeView(container);
432 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700433 mContentView.addView(container, COVER_SCREEN_PARAMS);
434 }
435
Michael Kolb11d19782011-03-20 10:17:40 -0700436 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700437 WebView web = getWebView();
438 if (web != null) {
439 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700440 }
441 }
442
Michael Kolb46f987e2011-04-05 10:39:10 -0700443 public void editUrl(boolean clearInput) {
444 if (mUiController.isInCustomActionMode()) {
445 mUiController.endActionMode();
446 }
447 showTitleBar();
448 getTitleBar().startEditingUrl(clearInput);
449 }
450
Michael Kolb7cdc4902011-02-03 17:54:40 -0800451 boolean canShowTitleBar() {
452 return !isTitleBarShowing()
453 && !isActivityPaused()
454 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700455 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800456 && !mUiController.isInCustomActionMode();
457 }
458
459 void showTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700460 if (canShowTitleBar()) {
461 getTitleBar().show();
462 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800463 }
464
465 protected void hideTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700466 if (getTitleBar().isShowing()) {
467 getTitleBar().hide();
468 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800469 }
470
471 protected boolean isTitleBarShowing() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700472 return getTitleBar().isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800473 }
474
John Reck5d43ce82011-06-21 17:16:51 -0700475 public boolean isEditingUrl() {
476 return getTitleBar().isEditingUrl();
477 }
478
Michael Kolb7cdc4902011-02-03 17:54:40 -0800479 protected abstract TitleBarBase getTitleBar();
480
481 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700482 WebView web = getWebView();
483 if (web != null) {
484 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700485 }
486 }
487
Michael Kolb66706532010-12-12 19:50:22 -0800488 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700489 public void showVoiceTitleBar(String title, List<String> results) {
490 getTitleBar().setInVoiceMode(true, results);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800491 getTitleBar().setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700492 }
493
Michael Kolb66706532010-12-12 19:50:22 -0800494 @Override
495 public void revertVoiceTitleBar(Tab tab) {
Michael Kolb11d19782011-03-20 10:17:40 -0700496 getTitleBar().setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800497 String url = tab.getUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800498 getTitleBar().setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700499 }
500
501 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700502 public void registerDropdownChangeListener(DropdownChangeListener d) {
503 getTitleBar().registerDropdownChangeListener(d);
504 }
505
506 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700507 public void showComboView(boolean startWithHistory, Bundle extras) {
John Reck439c9a52010-12-14 10:04:39 -0800508 if (mComboView != null) {
509 return;
510 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 mComboView = new CombinedBookmarkHistoryView(mActivity,
512 mUiController,
513 startWithHistory ?
514 CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY
515 : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS,
516 extras);
Michael Kolb47d63f82011-01-18 10:48:40 -0800517 FrameLayout wrapper =
518 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
519 wrapper.setVisibility(View.GONE);
Michael Kolbc16c5952011-03-29 15:37:03 -0700520 getTitleBar().stopEditingUrl();
Michael Kolb3a696282010-12-05 13:23:24 -0800521 dismissIME();
Michael Kolbc16c5952011-03-29 15:37:03 -0700522 hideTitleBar();
Michael Kolb3a696282010-12-05 13:23:24 -0800523 if (mActiveTab != null) {
Michael Kolb3a696282010-12-05 13:23:24 -0800524 mActiveTab.putInBackground();
525 }
John Reck74830e12011-03-14 11:43:05 -0700526 mComboView.setAlpha(0f);
527 ObjectAnimator anim = ObjectAnimator.ofFloat(mComboView, "alpha", 0f, 1f);
528 Resources res = mActivity.getResources();
529 anim.setDuration(res.getInteger(R.integer.comboViewFadeInDuration));
530 anim.start();
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
532 }
533
Michael Kolbba238702011-03-08 10:40:50 -0800534 public boolean isComboViewShowing() {
535 return (mComboView != null);
536 }
537
Michael Kolb8233fac2010-10-26 16:08:53 -0700538 /**
539 * dismiss the ComboPage
540 */
541 @Override
542 public void hideComboView() {
543 if (mComboView != null) {
544 mContentView.removeView(mComboView);
Michael Kolb47d63f82011-01-18 10:48:40 -0800545 FrameLayout wrapper =
546 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
547 wrapper.setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700548 mComboView = null;
549 }
Michael Kolb3a696282010-12-05 13:23:24 -0800550 if (mActiveTab != null) {
551 mActiveTab.putInForeground();
552 }
John Reckb3417f02011-01-14 11:01:05 -0800553 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 }
555
556 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400557 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 WebChromeClient.CustomViewCallback callback) {
559 // if a view already exists then immediately terminate the new one
560 if (mCustomView != null) {
561 callback.onCustomViewHidden();
562 return;
563 }
564
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400565 mOriginalOrientation = mActivity.getRequestedOrientation();
566
Michael Kolb8233fac2010-10-26 16:08:53 -0700567 // Add the custom view to its container.
568 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
569 mCustomView = view;
570 mCustomViewCallback = callback;
571 // Hide the content view.
572 mContentView.setVisibility(View.GONE);
573 // Finally show the custom view container.
574 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400575 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700576 mCustomViewContainer.setVisibility(View.VISIBLE);
577 mCustomViewContainer.bringToFront();
578 }
579
580 @Override
581 public void onHideCustomView() {
582 if (mCustomView == null)
583 return;
584
585 // Hide the custom view.
586 mCustomView.setVisibility(View.GONE);
587 // Remove the custom view from its container.
588 mCustomViewContainer.removeView(mCustomView);
589 mCustomView = null;
590 mCustomViewContainer.setVisibility(View.GONE);
591 mCustomViewCallback.onCustomViewHidden();
592 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400593 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 setStatusBarVisibility(true);
595 mContentView.setVisibility(View.VISIBLE);
596 }
597
598 @Override
599 public boolean isCustomViewShowing() {
600 return mCustomView != null;
601 }
602
Michael Kolb66706532010-12-12 19:50:22 -0800603 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800604 if (mInputManager.isActive()) {
605 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
606 0);
607 }
608 }
609
Michael Kolb66706532010-12-12 19:50:22 -0800610 @Override
611 public boolean showsWeb() {
612 return mCustomView == null
613 && mComboView == null;
614 }
615
Patrick Scott92066772011-03-10 08:46:27 -0500616 @Override
617 public void showAutoLogin(Tab tab) {
618 updateAutoLogin(tab, true);
619 }
620
621 @Override
622 public void hideAutoLogin(Tab tab) {
623 updateAutoLogin(tab, true);
624 }
625
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 // -------------------------------------------------------------------------
627
Michael Kolb5a72f182011-01-13 20:35:06 -0800628 protected void updateNavigationState(Tab tab) {
629 }
630
Michael Kolb11d19782011-03-20 10:17:40 -0700631 protected void updateAutoLogin(Tab tab, boolean animate) {
632 getTitleBar().updateAutoLogin(tab, animate);
633 }
Patrick Scott92066772011-03-10 08:46:27 -0500634
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 /**
636 * Update the lock icon to correspond to our latest state.
637 */
Michael Kolb66706532010-12-12 19:50:22 -0800638 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800639 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 updateLockIconImage(t.getLockIconType());
641 }
642 }
643
644 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700645 * Updates the lock-icon image in the title-bar.
646 */
John Reck30c714c2010-12-16 17:30:34 -0800647 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800649 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700650 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800651 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 d = mMixLockIcon;
653 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800654 getTitleBar().setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 }
656
John Reck30c714c2010-12-16 17:30:34 -0800657 protected void setUrlTitle(Tab tab) {
658 String url = tab.getUrl();
659 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800660 if (TextUtils.isEmpty(title)) {
661 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800662 }
Michael Kolb66706532010-12-12 19:50:22 -0800663 if (tab.isInVoiceSearchMode()) return;
664 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800665 getTitleBar().setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800666 }
667 }
668
669 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800670 protected void setFavicon(Tab tab) {
671 if (tab.inForeground()) {
672 Bitmap icon = tab.getFavicon();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800673 getTitleBar().setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800674 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 }
676
677 @Override
678 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 }
680
Michael Kolb66706532010-12-12 19:50:22 -0800681 // active tabs page
682
683 public void showActiveTabsPage() {
684 }
685
686 /**
687 * Remove the active tabs page.
688 */
689 public void removeActiveTabsPage() {
690 }
691
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 // menu handling callbacks
693
694 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800695 public boolean onPrepareOptionsMenu(Menu menu) {
696 return true;
697 }
698
699 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700700 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 }
702
703 @Override
704 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 }
706
707 @Override
708 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 }
710
711 @Override
712 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 }
714
715 @Override
716 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 }
718
719 @Override
720 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 }
722
723 // error console
724
725 @Override
726 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800727 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
729 if (flag) {
730 // Setting the show state of the console will cause it's the layout
731 // to be inflated.
732 if (errorConsole.numberOfErrors() > 0) {
733 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
734 } else {
735 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
736 }
737 if (errorConsole.getParent() != null) {
738 mErrorConsoleContainer.removeView(errorConsole);
739 }
740 // Now we can add it to the main view.
741 mErrorConsoleContainer.addView(errorConsole,
742 new LinearLayout.LayoutParams(
743 ViewGroup.LayoutParams.MATCH_PARENT,
744 ViewGroup.LayoutParams.WRAP_CONTENT));
745 } else {
746 mErrorConsoleContainer.removeView(errorConsole);
747 }
748 }
749
750 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800751 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
752 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
753 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 }
755
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 // -------------------------------------------------------------------------
757 // Helper function for WebChromeClient
758 // -------------------------------------------------------------------------
759
760 @Override
761 public Bitmap getDefaultVideoPoster() {
762 if (mDefaultVideoPoster == null) {
763 mDefaultVideoPoster = BitmapFactory.decodeResource(
764 mActivity.getResources(), R.drawable.default_video_poster);
765 }
766 return mDefaultVideoPoster;
767 }
768
769 @Override
770 public View getVideoLoadingProgressView() {
771 if (mVideoProgressView == null) {
772 LayoutInflater inflater = LayoutInflater.from(mActivity);
773 mVideoProgressView = inflater.inflate(
774 R.layout.video_loading_progress, null);
775 }
776 return mVideoProgressView;
777 }
778
Michael Kolb843510f2010-12-09 10:51:49 -0800779 @Override
780 public void showMaxTabsWarning() {
781 Toast warning = Toast.makeText(mActivity,
782 mActivity.getString(R.string.max_tabs_warning),
783 Toast.LENGTH_SHORT);
784 warning.show();
785 }
786
Michael Kolbfdb70242011-03-24 09:41:11 -0700787 protected void captureTab(final Tab tab) {
788 captureTab(tab,
789 (int) mActivity.getResources()
790 .getDimension(R.dimen.qc_thumb_width),
791 (int) mActivity.getResources()
792 .getDimension(R.dimen.qc_thumb_height));
793 }
794
795 protected void captureTab(final Tab tab, int width, int height) {
796 if ((tab == null) || (tab.getWebView() == null)) return;
797 Bitmap sshot = Controller.createScreenshot(tab, width, height);
798 tab.setScreenshot(sshot);
799 }
800
Michael Kolb46f987e2011-04-05 10:39:10 -0700801 protected WebView getWebView() {
802 if (mActiveTab != null) {
803 return mActiveTab.getWebView();
804 } else {
805 return null;
806 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700807 }
808
Michael Kolbfedb4922011-04-20 16:45:33 -0700809 protected Menu getMenu() {
810 MenuBuilder menu = new MenuBuilder(mActivity);
811 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
812 return menu;
813 }
814
Michael Kolbc38c6042011-04-27 10:46:06 -0700815 public void setFullscreen(boolean enabled) {
816 if (enabled) {
817 mActivity.getWindow().setFlags(
818 WindowManager.LayoutParams.FLAG_FULLSCREEN,
819 WindowManager.LayoutParams.FLAG_FULLSCREEN);
820 } else {
821 mActivity.getWindow().clearFlags(
822 WindowManager.LayoutParams.FLAG_FULLSCREEN);
823 }
824 }
825
Michael Kolb5a4372f2011-04-29 13:53:10 -0700826 protected Drawable getFaviconDrawable(Bitmap icon) {
827 Drawable[] array = new Drawable[3];
828 array[0] = new PaintDrawable(Color.BLACK);
829 PaintDrawable p = new PaintDrawable(Color.WHITE);
830 array[1] = p;
831 if (icon == null) {
832 array[2] = mGenericFavicon;
833 } else {
834 array[2] = new BitmapDrawable(icon);
835 }
836 LayerDrawable d = new LayerDrawable(array);
837 d.setLayerInset(1, 1, 1, 1, 1);
838 d.setLayerInset(2, 2, 2, 2, 2);
839 return d;
840 }
841
John Reck5d43ce82011-06-21 17:16:51 -0700842 public boolean isLoading() {
843 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
844 }
845
846 /**
847 * Suggest to the UI that the title bar can be hidden. The UI will then
848 * decide whether or not to hide based off a number of factors, such
849 * as if the user is editing the URL bar or if the page is loading
850 */
851 public void suggestHideTitleBar() {
852 if (!isLoading() && !isEditingUrl()) {
853 hideTitleBar();
854 }
855 }
856
857 @Override
858 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
859 WebView view = mActiveTab != null ? mActiveTab.getWebView() : null;
John Reck7e5b8b52011-06-22 13:46:25 -0700860 if (view == null) {
John Reck5d43ce82011-06-21 17:16:51 -0700861 return;
862 }
863 int scrollY = view.getScrollY();
John Reck7e5b8b52011-06-22 13:46:25 -0700864 if (isTitleBarShowing()
865 || scrollY < (mLastScrollY - mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700866 mLastScrollY = scrollY;
John Reck7e5b8b52011-06-22 13:46:25 -0700867 if (visibleTitleHeight == 0 && userInitiated) {
John Reck5d43ce82011-06-21 17:16:51 -0700868 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
869 showTitleBar();
870 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
871 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
872 } else if (visibleTitleHeight == getTitleBar().getEmbeddedHeight()
873 && mHandler.hasMessages(MSG_HIDE_TITLEBAR)) {
874 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
875 hideTitleBar();
876 }
877 } else if (scrollY > mLastScrollY) {
878 mLastScrollY = scrollY;
879 }
880 }
881
882 private Handler mHandler = new Handler() {
883
884 @Override
885 public void handleMessage(Message msg) {
886 if (msg.what == MSG_HIDE_TITLEBAR) {
887 suggestHideTitleBar();
888 }
889 }
890 };
Michael Kolb8233fac2010-10-26 16:08:53 -0700891}