blob: c7494658f152b458560db4b783289059446e8ea8 [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
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Michael Kolb53ed62c2011-10-04 16:18:44 -070020import android.content.Context;
John Reckd3e4d5b2011-07-13 15:48:43 -070021import android.content.Intent;
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;
Michael Kolb8233fac2010-10-26 16:08:53 -070035import android.view.Gravity;
36import android.view.LayoutInflater;
37import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070038import android.view.MenuItem;
Michael Kolb31065b12011-10-06 13:51:32 -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;
Michael Kolb8233fac2010-10-26 16:08:53 -070042import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080043import android.view.ViewGroup.LayoutParams;
Michael Kolbc5675ad2011-10-21 13:34:28 -070044import android.view.Window;
Michael Kolb8233fac2010-10-26 16:08:53 -070045import android.view.WindowManager;
Michael Kolb3a696282010-12-05 13:23:24 -080046import android.view.inputmethod.InputMethodManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070047import android.webkit.WebChromeClient;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000049import android.webkit.WebViewClassic;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import 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
Steve Block2466eff2011-10-03 15:33:09 +010055import com.android.browser.Tab.SecurityState;
John Reckbf2ec202011-06-29 17:47:38 -070056import 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 Reck718a24d2011-08-12 11:08:30 -070063public abstract class BaseUi implements UI {
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;
John Reckef654f12011-07-12 16:42:08 -070079 public static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
John Reck5d43ce82011-06-21 17:16:51 -070080
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
Steve Block2466eff2011-10-03 15:33:09 +010087 private Drawable mLockIconSecure;
88 private Drawable mLockIconMixed;
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 Kolb31065b12011-10-06 13:51:32 -070093 protected FrameLayout mFullscreenContainer;
John Reck2711fab2012-05-18 21:38:59 -070094 private FrameLayout mFixedTitlebarContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070095
Michael Kolb31065b12011-10-06 13:51:32 -070096 private View mCustomView;
Michael Kolb8233fac2010-10-26 16:08:53 -070097 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040098 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070099
Michael Kolb8233fac2010-10-26 16:08:53 -0700100 private LinearLayout mErrorConsoleContainer = null;
101
John Reck718a24d2011-08-12 11:08:30 -0700102 private UrlBarAutoShowManager mUrlBarAutoShowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -0700103
John Reck718a24d2011-08-12 11:08:30 -0700104 private Toast mStopToast;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106 // the default <video> poster
107 private Bitmap mDefaultVideoPoster;
108 // the video progress view
109 private View mVideoProgressView;
110
Michael Kolb8233fac2010-10-26 16:08:53 -0700111 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700112 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700113 protected TitleBar mTitleBar;
114 private NavigationBarBase mNavigationBar;
Michael Kolbda580632012-04-16 13:30:28 -0700115 protected PieControl mPieControl;
Michael Kolbbae0cb22012-05-29 11:15:27 -0700116 private boolean mBlockFocusAnimations;
Michael Kolb8233fac2010-10-26 16:08:53 -0700117
118 public BaseUi(Activity browser, UiController controller) {
119 mActivity = browser;
120 mUiController = controller;
121 mTabControl = controller.getTabControl();
122 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800123 mInputManager = (InputMethodManager)
124 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Steve Block2466eff2011-10-03 15:33:09 +0100125 mLockIconSecure = res.getDrawable(R.drawable.ic_secure_holo_dark);
126 mLockIconMixed = res.getDrawable(R.drawable.ic_secure_partial_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
128 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700129 LayoutInflater.from(mActivity)
130 .inflate(R.layout.custom_screen, frameLayout);
John Reck2711fab2012-05-18 21:38:59 -0700131 mFixedTitlebarContainer = (FrameLayout) frameLayout.findViewById(
132 R.id.fixed_titlebar_container);
John Reck7c6e1c92011-06-30 11:55:55 -0700133 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 R.id.main_content);
Michael Kolb53ed62c2011-10-04 16:18:44 -0700135 mCustomViewContainer = (FrameLayout) frameLayout.findViewById(
136 R.id.fullscreen_custom_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700137 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700138 .findViewById(R.id.error_console);
Michael Kolbc38c6042011-04-27 10:46:06 -0700139 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700140 mGenericFavicon = res.getDrawable(
141 R.drawable.app_web_browser_sm);
John Reck0f602f32011-07-07 15:38:43 -0700142 mTitleBar = new TitleBar(mActivity, mUiController, this,
143 mContentView);
144 mTitleBar.setProgress(100);
145 mNavigationBar = mTitleBar.getNavigationBar();
John Reck718a24d2011-08-12 11:08:30 -0700146 mUrlBarAutoShowManager = new UrlBarAutoShowManager(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700147 }
148
Michael Kolb8233fac2010-10-26 16:08:53 -0700149 private void cancelStopToast() {
150 if (mStopToast != null) {
151 mStopToast.cancel();
152 mStopToast = null;
153 }
154 }
155
156 // lifecycle
157
158 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800159 if (isCustomViewShowing()) {
160 onHideCustomView();
161 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 cancelStopToast();
163 mActivityPaused = true;
164 }
165
166 public void onResume() {
167 mActivityPaused = false;
Michael Kolb2ae6ef72011-08-22 14:49:34 -0700168 // check if we exited without setting active tab
169 // b: 5188145
Michael Kolb1a4625a2011-08-24 13:40:44 -0700170 final Tab ct = mTabControl.getCurrentTab();
171 if (ct != null) {
172 setActiveTab(ct);
173 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 }
175
Michael Kolb66706532010-12-12 19:50:22 -0800176 protected boolean isActivityPaused() {
177 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700178 }
179
180 public void onConfigurationChanged(Configuration config) {
181 }
182
John Reck0f602f32011-07-07 15:38:43 -0700183 public Activity getActivity() {
184 return mActivity;
185 }
186
Michael Kolb8233fac2010-10-26 16:08:53 -0700187 // key handling
188
189 @Override
190 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700191 if (mCustomView != null) {
192 mUiController.hideCustomView();
193 return true;
194 }
195 return false;
196 }
197
Michael Kolb2814a362011-05-19 15:49:41 -0700198 @Override
199 public boolean onMenuKey() {
200 return false;
201 }
202
Michael Kolbda580632012-04-16 13:30:28 -0700203 @Override
204 public void setUseQuickControls(boolean useQuickControls) {
205 mUseQuickControls = useQuickControls;
206 mTitleBar.setUseQuickControls(mUseQuickControls);
207 if (useQuickControls) {
208 mPieControl = new PieControl(mActivity, mUiController, this);
209 mPieControl.attachToContainer(mContentView);
210 } else {
211 if (mPieControl != null) {
212 mPieControl.removeFromContainer(mContentView);
213 }
214 }
215 updateUrlBarAutoShowManagerTarget();
216 }
217
John Reck30c714c2010-12-16 17:30:34 -0800218 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 @Override
John Reck30c714c2010-12-16 17:30:34 -0800220 public void onTabDataChanged(Tab tab) {
221 setUrlTitle(tab);
222 setFavicon(tab);
223 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800224 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700225 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700226 mNavigationBar.onTabDataChanged(tab);
Michael Kolba53c9892011-10-05 13:31:40 -0700227 onProgressChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700228 }
229
230 @Override
Michael Kolbe8a82332012-04-25 14:14:26 -0700231 public void onProgressChanged(Tab tab) {
232 int progress = tab.getLoadProgress();
233 if (tab.inForeground()) {
234 mTitleBar.setProgress(progress);
235 }
236 }
237
238 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500239 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800240 if (tab.inForeground()) {
241 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700242 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800243 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500244 }
245
246 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700247 public void onPageStopped(Tab tab) {
248 cancelStopToast();
249 if (tab.inForeground()) {
250 mStopToast = Toast
251 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
252 mStopToast.show();
253 }
254 }
255
256 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800257 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700258 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800259 }
260
261 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 }
264
265 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800266 public void setActiveTab(final Tab tab) {
Michael Kolb7ac63b62011-12-16 09:52:29 -0800267 if (tab == null) return;
Michael Kolbbae0cb22012-05-29 11:15:27 -0700268 // block unnecessary focus change animations during tab switch
269 mBlockFocusAnimations = true;
John Reck5d43ce82011-06-21 17:16:51 -0700270 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800271 if ((tab != mActiveTab) && (mActiveTab != null)) {
272 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700273 WebView web = mActiveTab.getWebView();
274 if (web != null) {
275 web.setOnTouchListener(null);
276 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 }
Michael Kolb77df4562010-11-19 14:49:34 -0800278 mActiveTab = tab;
Michael Kolbda580632012-04-16 13:30:28 -0700279 BrowserWebView web = (BrowserWebView) mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700280 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700281 attachTabToContentView(tab);
Michael Kolbda580632012-04-16 13:30:28 -0700282 if (web != null) {
283 // Request focus on the top window.
284 if (mUseQuickControls) {
285 mPieControl.forceToTop(mContentView);
286 web.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700287 mTitleBar.hide();
Michael Kolbda580632012-04-16 13:30:28 -0700288 } else {
289 web.setTitleBar(mTitleBar);
290 mTitleBar.onScrollChanged();
291 }
292 }
Michael Kolb4923c222012-04-02 16:18:36 -0700293 mTitleBar.bringToFront();
Michael Kolbf8963522012-04-10 10:52:34 -0700294 tab.getTopWindow().requestFocus();
Michael Kolb8233fac2010-10-26 16:08:53 -0700295 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800296 onTabDataChanged(tab);
297 onProgressChanged(tab);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700298 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
Patrick Scott92066772011-03-10 08:46:27 -0500299 updateAutoLogin(tab, false);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700300 mBlockFocusAnimations = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700301 }
302
John Reck718a24d2011-08-12 11:08:30 -0700303 protected void updateUrlBarAutoShowManagerTarget() {
304 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
305 if (!mUseQuickControls && web instanceof BrowserWebView) {
306 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
307 } else {
308 mUrlBarAutoShowManager.setTarget(null);
309 }
310 }
311
Michael Kolbcfa3af52010-12-14 10:36:11 -0800312 Tab getActiveTab() {
313 return mActiveTab;
314 }
315
Michael Kolb8233fac2010-10-26 16:08:53 -0700316 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800317 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800318 }
319
320 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700321 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800322 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700323 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800324 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700325 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700326 }
327
328 @Override
329 public void detachTab(Tab tab) {
330 removeTabFromContentView(tab);
331 }
332
333 @Override
334 public void attachTab(Tab tab) {
335 attachTabToContentView(tab);
336 }
337
Michael Kolb377ea312011-02-17 14:36:56 -0800338 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800339 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700340 return;
341 }
342 View container = tab.getViewContainer();
343 WebView mainView = tab.getWebView();
344 // Attach the WebView to the container and then attach the
345 // container to the content view.
346 FrameLayout wrapper =
347 (FrameLayout) container.findViewById(R.id.webview_wrapper);
348 ViewGroup parent = (ViewGroup) mainView.getParent();
349 if (parent != wrapper) {
350 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 parent.removeView(mainView);
352 }
353 wrapper.addView(mainView);
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 }
355 parent = (ViewGroup) container.getParent();
356 if (parent != mContentView) {
357 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 parent.removeView(container);
359 }
360 mContentView.addView(container, COVER_SCREEN_PARAMS);
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 }
362 mUiController.attachSubWindow(tab);
363 }
364
365 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800366 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700367 // Remove the container that contains the main WebView.
368 WebView mainView = tab.getWebView();
369 View container = tab.getViewContainer();
370 if (mainView == null) {
371 return;
372 }
373 // Remove the container from the content and then remove the
374 // WebView from the container. This will trigger a focus change
375 // needed by WebView.
376 FrameLayout wrapper =
377 (FrameLayout) container.findViewById(R.id.webview_wrapper);
378 wrapper.removeView(mainView);
379 mContentView.removeView(container);
380 mUiController.endActionMode();
381 mUiController.removeSubWindow(tab);
382 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
383 if (errorConsole != null) {
384 mErrorConsoleContainer.removeView(errorConsole);
385 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700386 }
387
Michael Kolba713ec82010-11-29 17:27:06 -0800388 @Override
389 public void onSetWebView(Tab tab, WebView webView) {
390 View container = tab.getViewContainer();
391 if (container == null) {
392 // The tab consists of a container view, which contains the main
393 // WebView, as well as any other UI elements associated with the tab.
394 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700395 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800396 tab.setViewContainer(container);
397 }
398 if (tab.getWebView() != webView) {
399 // Just remove the old one.
400 FrameLayout wrapper =
401 (FrameLayout) container.findViewById(R.id.webview_wrapper);
402 wrapper.removeView(tab.getWebView());
403 }
404 }
405
Michael Kolb8233fac2010-10-26 16:08:53 -0700406 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800407 * create a sub window container and webview for the tab
408 * Note: this methods operates through side-effects for now
409 * it sets both the subView and subViewContainer for the given tab
410 * @param tab tab to create the sub window for
411 * @param subView webview to be set as a subwindow for the tab
412 */
413 @Override
414 public void createSubWindow(Tab tab, WebView subView) {
415 View subViewContainer = mActivity.getLayoutInflater().inflate(
416 R.layout.browser_subwindow, null);
417 ViewGroup inner = (ViewGroup) subViewContainer
418 .findViewById(R.id.inner_container);
419 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
420 LayoutParams.MATCH_PARENT));
421 final ImageButton cancel = (ImageButton) subViewContainer
422 .findViewById(R.id.subwindow_close);
423 final WebView cancelSubView = subView;
424 cancel.setOnClickListener(new OnClickListener() {
425 public void onClick(View v) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000426 WebViewClassic.fromWebView(cancelSubView).getWebChromeClient().onCloseWindow(
427 cancelSubView);
Michael Kolb1514bb72010-11-22 09:11:48 -0800428 }
429 });
430 tab.setSubWebView(subView);
431 tab.setSubViewContainer(subViewContainer);
432 }
433
434 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700435 * Remove the sub window from the content view.
436 */
437 @Override
438 public void removeSubWindow(View subviewContainer) {
439 mContentView.removeView(subviewContainer);
440 mUiController.endActionMode();
441 }
442
443 /**
444 * Attach the sub window to the content view.
445 */
446 @Override
447 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800448 if (container.getParent() != null) {
449 // already attached, remove first
450 ((ViewGroup) container.getParent()).removeView(container);
451 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700452 mContentView.addView(container, COVER_SCREEN_PARAMS);
453 }
454
Michael Kolb11d19782011-03-20 10:17:40 -0700455 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700456 WebView web = getWebView();
457 if (web != null) {
458 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700459 }
460 }
461
Michael Kolb1f9b3562012-04-24 14:38:34 -0700462 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700463 if (mUiController.isInCustomActionMode()) {
464 mUiController.endActionMode();
465 }
466 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700467 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700468 mNavigationBar.startEditingUrl(clearInput, forceIME);
John Reckef654f12011-07-12 16:42:08 -0700469 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700470 }
471
Michael Kolb7cdc4902011-02-03 17:54:40 -0800472 boolean canShowTitleBar() {
473 return !isTitleBarShowing()
474 && !isActivityPaused()
475 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700476 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800477 && !mUiController.isInCustomActionMode();
478 }
479
John Reck0f602f32011-07-07 15:38:43 -0700480 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700481 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700482 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700483 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700484 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800485 }
486
487 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700488 if (mTitleBar.isShowing()) {
489 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700490 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800491 }
492
493 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700494 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800495 }
496
John Reck5d43ce82011-06-21 17:16:51 -0700497 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700498 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700499 }
500
Michael Kolb80f75082012-04-10 10:50:06 -0700501 public void stopEditingUrl() {
502 mTitleBar.getNavigationBar().stopEditingUrl();
503 }
504
John Reck0f602f32011-07-07 15:38:43 -0700505 public TitleBar getTitleBar() {
506 return mTitleBar;
507 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800508
Michael Kolb66706532010-12-12 19:50:22 -0800509 @Override
John Reck2bc80422011-06-30 15:11:49 -0700510 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700511 Intent intent = new Intent(mActivity, ComboViewActivity.class);
512 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
513 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
514 Tab t = getActiveTab();
515 if (t != null) {
516 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800517 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700518 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 }
520
521 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400522 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 WebChromeClient.CustomViewCallback callback) {
524 // if a view already exists then immediately terminate the new one
525 if (mCustomView != null) {
526 callback.onCustomViewHidden();
527 return;
528 }
529
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400530 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb31065b12011-10-06 13:51:32 -0700531 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
532 mFullscreenContainer = new FullscreenHolder(mActivity);
533 mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
534 decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
535 mCustomView = view;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700536 setFullscreen(true);
Michael Kolb54217b32012-05-15 13:24:24 -0700537 ((BrowserWebView) getWebView()).setVisibility(View.INVISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700538 mCustomViewCallback = callback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400539 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 }
541
542 @Override
543 public void onHideCustomView() {
Michael Kolb54217b32012-05-15 13:24:24 -0700544 ((BrowserWebView) getWebView()).setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 if (mCustomView == null)
546 return;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700547 setFullscreen(false);
Michael Kolb31065b12011-10-06 13:51:32 -0700548 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
549 decor.removeView(mFullscreenContainer);
550 mFullscreenContainer = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700551 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700552 mCustomViewCallback.onCustomViewHidden();
553 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400554 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700555 }
556
557 @Override
558 public boolean isCustomViewShowing() {
559 return mCustomView != null;
560 }
561
Michael Kolb66706532010-12-12 19:50:22 -0800562 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800563 if (mInputManager.isActive()) {
564 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
565 0);
566 }
567 }
568
Michael Kolb66706532010-12-12 19:50:22 -0800569 @Override
John Reck3ba45532011-08-11 16:26:53 -0700570 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700571 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800572 }
573
Patrick Scott92066772011-03-10 08:46:27 -0500574 @Override
575 public void showAutoLogin(Tab tab) {
576 updateAutoLogin(tab, true);
577 }
578
579 @Override
580 public void hideAutoLogin(Tab tab) {
581 updateAutoLogin(tab, true);
582 }
583
Michael Kolb8233fac2010-10-26 16:08:53 -0700584 // -------------------------------------------------------------------------
585
Michael Kolb5a72f182011-01-13 20:35:06 -0800586 protected void updateNavigationState(Tab tab) {
587 }
588
Michael Kolb11d19782011-03-20 10:17:40 -0700589 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700590 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700591 }
Patrick Scott92066772011-03-10 08:46:27 -0500592
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 /**
594 * Update the lock icon to correspond to our latest state.
595 */
Michael Kolb66706532010-12-12 19:50:22 -0800596 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800597 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100598 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 }
600 }
601
602 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 * Updates the lock-icon image in the title-bar.
604 */
Steve Block2466eff2011-10-03 15:33:09 +0100605 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700606 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100607 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
608 d = mLockIconSecure;
Steve Block4895b012011-10-03 16:26:46 +0100609 } else if (securityState == SecurityState.SECURITY_STATE_MIXED
610 || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
611 // TODO: It would be good to have different icons for insecure vs mixed content.
612 // See http://b/5403800
Steve Block2466eff2011-10-03 15:33:09 +0100613 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 }
John Reck0f602f32011-07-07 15:38:43 -0700615 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 }
617
John Reck30c714c2010-12-16 17:30:34 -0800618 protected void setUrlTitle(Tab tab) {
619 String url = tab.getUrl();
620 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800621 if (TextUtils.isEmpty(title)) {
622 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800623 }
Michael Kolb66706532010-12-12 19:50:22 -0800624 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700625 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800626 }
627 }
628
629 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800630 protected void setFavicon(Tab tab) {
631 if (tab.inForeground()) {
632 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700633 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800634 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 }
636
637 @Override
638 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 }
640
Michael Kolb66706532010-12-12 19:50:22 -0800641 // active tabs page
642
643 public void showActiveTabsPage() {
644 }
645
646 /**
647 * Remove the active tabs page.
648 */
649 public void removeActiveTabsPage() {
650 }
651
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 // menu handling callbacks
653
654 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800655 public boolean onPrepareOptionsMenu(Menu menu) {
656 return true;
657 }
658
659 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700660 public void updateMenuState(Tab tab, Menu menu) {
661 }
662
663 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700664 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 }
666
667 @Override
668 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 }
670
671 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700672 public boolean onOptionsItemSelected(MenuItem item) {
673 return false;
674 }
675
676 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 }
679
680 @Override
681 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
683
684 @Override
685 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
688 @Override
689 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 }
691
692 // error console
693
694 @Override
695 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800696 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
698 if (flag) {
699 // Setting the show state of the console will cause it's the layout
700 // to be inflated.
701 if (errorConsole.numberOfErrors() > 0) {
702 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
703 } else {
704 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
705 }
706 if (errorConsole.getParent() != null) {
707 mErrorConsoleContainer.removeView(errorConsole);
708 }
709 // Now we can add it to the main view.
710 mErrorConsoleContainer.addView(errorConsole,
711 new LinearLayout.LayoutParams(
712 ViewGroup.LayoutParams.MATCH_PARENT,
713 ViewGroup.LayoutParams.WRAP_CONTENT));
714 } else {
715 mErrorConsoleContainer.removeView(errorConsole);
716 }
717 }
718
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 // -------------------------------------------------------------------------
720 // Helper function for WebChromeClient
721 // -------------------------------------------------------------------------
722
723 @Override
724 public Bitmap getDefaultVideoPoster() {
725 if (mDefaultVideoPoster == null) {
726 mDefaultVideoPoster = BitmapFactory.decodeResource(
727 mActivity.getResources(), R.drawable.default_video_poster);
728 }
729 return mDefaultVideoPoster;
730 }
731
732 @Override
733 public View getVideoLoadingProgressView() {
734 if (mVideoProgressView == null) {
735 LayoutInflater inflater = LayoutInflater.from(mActivity);
736 mVideoProgressView = inflater.inflate(
737 R.layout.video_loading_progress, null);
738 }
739 return mVideoProgressView;
740 }
741
Michael Kolb843510f2010-12-09 10:51:49 -0800742 @Override
743 public void showMaxTabsWarning() {
744 Toast warning = Toast.makeText(mActivity,
745 mActivity.getString(R.string.max_tabs_warning),
746 Toast.LENGTH_SHORT);
747 warning.show();
748 }
749
Michael Kolb46f987e2011-04-05 10:39:10 -0700750 protected WebView getWebView() {
751 if (mActiveTab != null) {
752 return mActiveTab.getWebView();
753 } else {
754 return null;
755 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700756 }
757
Michael Kolbfedb4922011-04-20 16:45:33 -0700758 protected Menu getMenu() {
759 MenuBuilder menu = new MenuBuilder(mActivity);
760 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
761 return menu;
762 }
763
Michael Kolbc38c6042011-04-27 10:46:06 -0700764 public void setFullscreen(boolean enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700765 Window win = mActivity.getWindow();
766 WindowManager.LayoutParams winParams = win.getAttributes();
Michael Kolb76dff392011-12-06 09:51:18 -0800767 final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
Michael Kolbc38c6042011-04-27 10:46:06 -0700768 if (enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700769 winParams.flags |= bits;
Michael Kolbc38c6042011-04-27 10:46:06 -0700770 } else {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700771 winParams.flags &= ~bits;
772 if (mCustomView != null) {
773 mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
774 } else {
775 mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
776 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700777 }
Michael Kolbc5675ad2011-10-21 13:34:28 -0700778 win.setAttributes(winParams);
Michael Kolbc38c6042011-04-27 10:46:06 -0700779 }
780
John Reck0f602f32011-07-07 15:38:43 -0700781 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700782 Drawable[] array = new Drawable[3];
783 array[0] = new PaintDrawable(Color.BLACK);
784 PaintDrawable p = new PaintDrawable(Color.WHITE);
785 array[1] = p;
786 if (icon == null) {
787 array[2] = mGenericFavicon;
788 } else {
789 array[2] = new BitmapDrawable(icon);
790 }
791 LayerDrawable d = new LayerDrawable(array);
792 d.setLayerInset(1, 1, 1, 1, 1);
793 d.setLayerInset(2, 2, 2, 2, 2);
794 return d;
795 }
796
John Reck5d43ce82011-06-21 17:16:51 -0700797 public boolean isLoading() {
798 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
799 }
800
801 /**
802 * Suggest to the UI that the title bar can be hidden. The UI will then
803 * decide whether or not to hide based off a number of factors, such
804 * as if the user is editing the URL bar or if the page is loading
805 */
806 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700807 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
808 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700809 hideTitleBar();
810 }
811 }
812
John Reckbc6adb42011-09-01 18:03:20 -0700813 protected final void showTitleBarForDuration() {
814 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
815 }
816
817 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700818 showTitleBar();
819 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700820 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700821 }
822
John Reck9c5004e2011-10-07 16:00:12 -0700823 protected Handler mHandler = new Handler() {
John Reck5d43ce82011-06-21 17:16:51 -0700824
825 @Override
826 public void handleMessage(Message msg) {
827 if (msg.what == MSG_HIDE_TITLEBAR) {
828 suggestHideTitleBar();
829 }
John Reck9c5004e2011-10-07 16:00:12 -0700830 BaseUi.this.handleMessage(msg);
John Reck5d43ce82011-06-21 17:16:51 -0700831 }
832 };
John Reck3ba45532011-08-11 16:26:53 -0700833
John Reck9c5004e2011-10-07 16:00:12 -0700834 protected void handleMessage(Message msg) {}
835
John Reck3ba45532011-08-11 16:26:53 -0700836 @Override
837 public void showWeb(boolean animate) {
838 mUiController.hideCustomView();
839 }
840
Michael Kolb31065b12011-10-06 13:51:32 -0700841 static class FullscreenHolder extends FrameLayout {
Michael Kolb53ed62c2011-10-04 16:18:44 -0700842
Michael Kolb31065b12011-10-06 13:51:32 -0700843 public FullscreenHolder(Context ctx) {
844 super(ctx);
845 setBackgroundColor(ctx.getResources().getColor(R.color.black));
Michael Kolb53ed62c2011-10-04 16:18:44 -0700846 }
847
Michael Kolb31065b12011-10-06 13:51:32 -0700848 @Override
849 public boolean onTouchEvent(MotionEvent evt) {
850 return true;
Michael Kolb53ed62c2011-10-04 16:18:44 -0700851 }
Michael Kolb31065b12011-10-06 13:51:32 -0700852
Michael Kolb53ed62c2011-10-04 16:18:44 -0700853 }
John Reck2711fab2012-05-18 21:38:59 -0700854
855 public void addFixedTitleBar(View view) {
856 mFixedTitlebarContainer.addView(view);
857 }
858
859 public void setContentViewMarginTop(int margin) {
860 LinearLayout.LayoutParams params =
861 (LinearLayout.LayoutParams) mContentView.getLayoutParams();
862 if (params.topMargin != margin) {
863 params.topMargin = margin;
864 mContentView.setLayoutParams(params);
865 }
866 }
Michael Kolbbae0cb22012-05-29 11:15:27 -0700867
868 @Override
869 public boolean blockFocusAnimations() {
870 return mBlockFocusAnimations;
871 }
872
Michael Kolb0b129122012-06-04 16:31:58 -0700873 @Override
874 public void onVoiceResult(String result) {
875 mNavigationBar.onVoiceResult(result);
876 }
877
Michael Kolb8233fac2010-10-26 16:08:53 -0700878}