blob: 42c720647ed1489cdd31e1f1a2f92c29e2b4b985 [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;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
Michael Kolb31065b12011-10-06 13:51:32 -070095 private View mCustomView;
Michael Kolb8233fac2010-10-26 16:08:53 -070096 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040097 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070098
Michael Kolb8233fac2010-10-26 16:08:53 -070099 private LinearLayout mErrorConsoleContainer = null;
100
John Reck718a24d2011-08-12 11:08:30 -0700101 private UrlBarAutoShowManager mUrlBarAutoShowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -0700102
John Reck718a24d2011-08-12 11:08:30 -0700103 private Toast mStopToast;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700104
Michael Kolb8233fac2010-10-26 16:08:53 -0700105 // the default <video> poster
106 private Bitmap mDefaultVideoPoster;
107 // the video progress view
108 private View mVideoProgressView;
109
Michael Kolb8233fac2010-10-26 16:08:53 -0700110 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700111 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700112 protected TitleBar mTitleBar;
113 private NavigationBarBase mNavigationBar;
Michael Kolbda580632012-04-16 13:30:28 -0700114 protected PieControl mPieControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115
116 public BaseUi(Activity browser, UiController controller) {
117 mActivity = browser;
118 mUiController = controller;
119 mTabControl = controller.getTabControl();
120 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800121 mInputManager = (InputMethodManager)
122 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Steve Block2466eff2011-10-03 15:33:09 +0100123 mLockIconSecure = res.getDrawable(R.drawable.ic_secure_holo_dark);
124 mLockIconMixed = res.getDrawable(R.drawable.ic_secure_partial_holo_dark);
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);
Michael Kolb53ed62c2011-10-04 16:18:44 -0700131 mCustomViewContainer = (FrameLayout) frameLayout.findViewById(
132 R.id.fullscreen_custom_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700133 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 .findViewById(R.id.error_console);
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 Reck0f602f32011-07-07 15:38:43 -0700138 mTitleBar = new TitleBar(mActivity, mUiController, this,
139 mContentView);
140 mTitleBar.setProgress(100);
141 mNavigationBar = mTitleBar.getNavigationBar();
John Reck718a24d2011-08-12 11:08:30 -0700142 mUrlBarAutoShowManager = new UrlBarAutoShowManager(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 }
144
Michael Kolb8233fac2010-10-26 16:08:53 -0700145 private void cancelStopToast() {
146 if (mStopToast != null) {
147 mStopToast.cancel();
148 mStopToast = null;
149 }
150 }
151
152 // lifecycle
153
154 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800155 if (isCustomViewShowing()) {
156 onHideCustomView();
157 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 cancelStopToast();
159 mActivityPaused = true;
160 }
161
162 public void onResume() {
163 mActivityPaused = false;
Michael Kolb2ae6ef72011-08-22 14:49:34 -0700164 // check if we exited without setting active tab
165 // b: 5188145
Michael Kolb1a4625a2011-08-24 13:40:44 -0700166 final Tab ct = mTabControl.getCurrentTab();
167 if (ct != null) {
168 setActiveTab(ct);
169 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 }
171
Michael Kolb66706532010-12-12 19:50:22 -0800172 protected boolean isActivityPaused() {
173 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 }
175
176 public void onConfigurationChanged(Configuration config) {
177 }
178
John Reck0f602f32011-07-07 15:38:43 -0700179 public Activity getActivity() {
180 return mActivity;
181 }
182
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 // key handling
184
185 @Override
186 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700187 if (mCustomView != null) {
188 mUiController.hideCustomView();
189 return true;
190 }
191 return false;
192 }
193
Michael Kolb2814a362011-05-19 15:49:41 -0700194 @Override
195 public boolean onMenuKey() {
196 return false;
197 }
198
Michael Kolbda580632012-04-16 13:30:28 -0700199 @Override
200 public void setUseQuickControls(boolean useQuickControls) {
201 mUseQuickControls = useQuickControls;
202 mTitleBar.setUseQuickControls(mUseQuickControls);
203 if (useQuickControls) {
204 mPieControl = new PieControl(mActivity, mUiController, this);
205 mPieControl.attachToContainer(mContentView);
206 } else {
207 if (mPieControl != null) {
208 mPieControl.removeFromContainer(mContentView);
209 }
210 }
211 updateUrlBarAutoShowManagerTarget();
212 }
213
John Reck30c714c2010-12-16 17:30:34 -0800214 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 @Override
John Reck30c714c2010-12-16 17:30:34 -0800216 public void onTabDataChanged(Tab tab) {
217 setUrlTitle(tab);
218 setFavicon(tab);
219 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800220 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700221 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700222 mNavigationBar.onTabDataChanged(tab);
Michael Kolba53c9892011-10-05 13:31:40 -0700223 onProgressChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700224 }
225
226 @Override
Michael Kolbe8a82332012-04-25 14:14:26 -0700227 public void onProgressChanged(Tab tab) {
228 int progress = tab.getLoadProgress();
229 if (tab.inForeground()) {
230 mTitleBar.setProgress(progress);
231 }
232 }
233
234 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500235 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800236 if (tab.inForeground()) {
237 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700238 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800239 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500240 }
241
242 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 public void onPageStopped(Tab tab) {
244 cancelStopToast();
245 if (tab.inForeground()) {
246 mStopToast = Toast
247 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
248 mStopToast.show();
249 }
250 }
251
252 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800253 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700254 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800255 }
256
257 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 }
260
261 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800262 public void setActiveTab(final Tab tab) {
Michael Kolb7ac63b62011-12-16 09:52:29 -0800263 if (tab == null) return;
John Reck5d43ce82011-06-21 17:16:51 -0700264 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800265 if ((tab != mActiveTab) && (mActiveTab != null)) {
266 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700267 WebView web = mActiveTab.getWebView();
268 if (web != null) {
269 web.setOnTouchListener(null);
270 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 }
Michael Kolb77df4562010-11-19 14:49:34 -0800272 mActiveTab = tab;
Michael Kolbda580632012-04-16 13:30:28 -0700273 BrowserWebView web = (BrowserWebView) mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700274 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700275 attachTabToContentView(tab);
Michael Kolbda580632012-04-16 13:30:28 -0700276 if (web != null) {
277 // Request focus on the top window.
278 if (mUseQuickControls) {
279 mPieControl.forceToTop(mContentView);
280 web.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700281 mTitleBar.hide();
Michael Kolbda580632012-04-16 13:30:28 -0700282 } else {
283 web.setTitleBar(mTitleBar);
284 mTitleBar.onScrollChanged();
285 }
286 }
Michael Kolb4923c222012-04-02 16:18:36 -0700287 mTitleBar.bringToFront();
Michael Kolbf8963522012-04-10 10:52:34 -0700288 tab.getTopWindow().requestFocus();
Michael Kolb8233fac2010-10-26 16:08:53 -0700289 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800290 onTabDataChanged(tab);
291 onProgressChanged(tab);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700292 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
Patrick Scott92066772011-03-10 08:46:27 -0500293 updateAutoLogin(tab, false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700294 }
295
John Reck718a24d2011-08-12 11:08:30 -0700296 protected void updateUrlBarAutoShowManagerTarget() {
297 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
298 if (!mUseQuickControls && web instanceof BrowserWebView) {
299 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
300 } else {
301 mUrlBarAutoShowManager.setTarget(null);
302 }
303 }
304
Michael Kolbcfa3af52010-12-14 10:36:11 -0800305 Tab getActiveTab() {
306 return mActiveTab;
307 }
308
Michael Kolb8233fac2010-10-26 16:08:53 -0700309 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800310 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800311 }
312
313 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700314 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800315 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700316 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800317 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700318 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700319 }
320
321 @Override
322 public void detachTab(Tab tab) {
323 removeTabFromContentView(tab);
324 }
325
326 @Override
327 public void attachTab(Tab tab) {
328 attachTabToContentView(tab);
329 }
330
Michael Kolb377ea312011-02-17 14:36:56 -0800331 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800332 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700333 return;
334 }
335 View container = tab.getViewContainer();
336 WebView mainView = tab.getWebView();
337 // Attach the WebView to the container and then attach the
338 // container to the content view.
339 FrameLayout wrapper =
340 (FrameLayout) container.findViewById(R.id.webview_wrapper);
341 ViewGroup parent = (ViewGroup) mainView.getParent();
342 if (parent != wrapper) {
343 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 parent.removeView(mainView);
345 }
346 wrapper.addView(mainView);
Michael Kolb8233fac2010-10-26 16:08:53 -0700347 }
348 parent = (ViewGroup) container.getParent();
349 if (parent != mContentView) {
350 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 parent.removeView(container);
352 }
353 mContentView.addView(container, COVER_SCREEN_PARAMS);
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 }
355 mUiController.attachSubWindow(tab);
356 }
357
358 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800359 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700360 // Remove the container that contains the main WebView.
361 WebView mainView = tab.getWebView();
362 View container = tab.getViewContainer();
363 if (mainView == null) {
364 return;
365 }
366 // Remove the container from the content and then remove the
367 // WebView from the container. This will trigger a focus change
368 // needed by WebView.
369 FrameLayout wrapper =
370 (FrameLayout) container.findViewById(R.id.webview_wrapper);
371 wrapper.removeView(mainView);
372 mContentView.removeView(container);
373 mUiController.endActionMode();
374 mUiController.removeSubWindow(tab);
375 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
376 if (errorConsole != null) {
377 mErrorConsoleContainer.removeView(errorConsole);
378 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 }
380
Michael Kolba713ec82010-11-29 17:27:06 -0800381 @Override
382 public void onSetWebView(Tab tab, WebView webView) {
383 View container = tab.getViewContainer();
384 if (container == null) {
385 // The tab consists of a container view, which contains the main
386 // WebView, as well as any other UI elements associated with the tab.
387 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700388 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800389 tab.setViewContainer(container);
390 }
391 if (tab.getWebView() != webView) {
392 // Just remove the old one.
393 FrameLayout wrapper =
394 (FrameLayout) container.findViewById(R.id.webview_wrapper);
395 wrapper.removeView(tab.getWebView());
396 }
397 }
398
Michael Kolb8233fac2010-10-26 16:08:53 -0700399 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800400 * create a sub window container and webview for the tab
401 * Note: this methods operates through side-effects for now
402 * it sets both the subView and subViewContainer for the given tab
403 * @param tab tab to create the sub window for
404 * @param subView webview to be set as a subwindow for the tab
405 */
406 @Override
407 public void createSubWindow(Tab tab, WebView subView) {
408 View subViewContainer = mActivity.getLayoutInflater().inflate(
409 R.layout.browser_subwindow, null);
410 ViewGroup inner = (ViewGroup) subViewContainer
411 .findViewById(R.id.inner_container);
412 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
413 LayoutParams.MATCH_PARENT));
414 final ImageButton cancel = (ImageButton) subViewContainer
415 .findViewById(R.id.subwindow_close);
416 final WebView cancelSubView = subView;
417 cancel.setOnClickListener(new OnClickListener() {
418 public void onClick(View v) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000419 WebViewClassic.fromWebView(cancelSubView).getWebChromeClient().onCloseWindow(
420 cancelSubView);
Michael Kolb1514bb72010-11-22 09:11:48 -0800421 }
422 });
423 tab.setSubWebView(subView);
424 tab.setSubViewContainer(subViewContainer);
425 }
426
427 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700428 * Remove the sub window from the content view.
429 */
430 @Override
431 public void removeSubWindow(View subviewContainer) {
432 mContentView.removeView(subviewContainer);
433 mUiController.endActionMode();
434 }
435
436 /**
437 * Attach the sub window to the content view.
438 */
439 @Override
440 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800441 if (container.getParent() != null) {
442 // already attached, remove first
443 ((ViewGroup) container.getParent()).removeView(container);
444 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700445 mContentView.addView(container, COVER_SCREEN_PARAMS);
446 }
447
Michael Kolb11d19782011-03-20 10:17:40 -0700448 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700449 WebView web = getWebView();
450 if (web != null) {
451 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700452 }
453 }
454
Michael Kolb1f9b3562012-04-24 14:38:34 -0700455 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700456 if (mUiController.isInCustomActionMode()) {
457 mUiController.endActionMode();
458 }
459 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700460 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700461 mNavigationBar.startEditingUrl(clearInput, forceIME);
John Reckef654f12011-07-12 16:42:08 -0700462 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700463 }
464
Michael Kolb7cdc4902011-02-03 17:54:40 -0800465 boolean canShowTitleBar() {
466 return !isTitleBarShowing()
467 && !isActivityPaused()
468 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700469 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800470 && !mUiController.isInCustomActionMode();
471 }
472
John Reck0f602f32011-07-07 15:38:43 -0700473 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700474 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700475 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700476 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700477 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800478 }
479
480 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700481 if (mTitleBar.isShowing()) {
482 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700483 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800484 }
485
486 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700487 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800488 }
489
John Reck5d43ce82011-06-21 17:16:51 -0700490 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700491 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700492 }
493
Michael Kolb80f75082012-04-10 10:50:06 -0700494 public void stopEditingUrl() {
495 mTitleBar.getNavigationBar().stopEditingUrl();
496 }
497
John Reck0f602f32011-07-07 15:38:43 -0700498 public TitleBar getTitleBar() {
499 return mTitleBar;
500 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800501
Michael Kolb66706532010-12-12 19:50:22 -0800502 @Override
John Reck2bc80422011-06-30 15:11:49 -0700503 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700504 Intent intent = new Intent(mActivity, ComboViewActivity.class);
505 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
506 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
507 Tab t = getActiveTab();
508 if (t != null) {
509 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800510 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700511 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 }
513
514 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400515 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700516 WebChromeClient.CustomViewCallback callback) {
517 // if a view already exists then immediately terminate the new one
518 if (mCustomView != null) {
519 callback.onCustomViewHidden();
520 return;
521 }
522
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400523 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb31065b12011-10-06 13:51:32 -0700524 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
525 mFullscreenContainer = new FullscreenHolder(mActivity);
526 mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
527 decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
528 mCustomView = view;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700529 setFullscreen(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 mCustomViewCallback = callback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400531 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 }
533
534 @Override
535 public void onHideCustomView() {
536 if (mCustomView == null)
537 return;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700538 setFullscreen(false);
Michael Kolb31065b12011-10-06 13:51:32 -0700539 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
540 decor.removeView(mFullscreenContainer);
541 mFullscreenContainer = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 mCustomViewCallback.onCustomViewHidden();
544 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400545 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 }
547
548 @Override
549 public boolean isCustomViewShowing() {
550 return mCustomView != null;
551 }
552
Michael Kolb66706532010-12-12 19:50:22 -0800553 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800554 if (mInputManager.isActive()) {
555 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
556 0);
557 }
558 }
559
Michael Kolb66706532010-12-12 19:50:22 -0800560 @Override
John Reck3ba45532011-08-11 16:26:53 -0700561 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700562 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800563 }
564
Patrick Scott92066772011-03-10 08:46:27 -0500565 @Override
566 public void showAutoLogin(Tab tab) {
567 updateAutoLogin(tab, true);
568 }
569
570 @Override
571 public void hideAutoLogin(Tab tab) {
572 updateAutoLogin(tab, true);
573 }
574
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 // -------------------------------------------------------------------------
576
Michael Kolb5a72f182011-01-13 20:35:06 -0800577 protected void updateNavigationState(Tab tab) {
578 }
579
Michael Kolb11d19782011-03-20 10:17:40 -0700580 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700581 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700582 }
Patrick Scott92066772011-03-10 08:46:27 -0500583
Michael Kolb8233fac2010-10-26 16:08:53 -0700584 /**
585 * Update the lock icon to correspond to our latest state.
586 */
Michael Kolb66706532010-12-12 19:50:22 -0800587 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800588 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100589 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 }
591 }
592
593 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 * Updates the lock-icon image in the title-bar.
595 */
Steve Block2466eff2011-10-03 15:33:09 +0100596 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700597 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100598 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
599 d = mLockIconSecure;
Steve Block4895b012011-10-03 16:26:46 +0100600 } else if (securityState == SecurityState.SECURITY_STATE_MIXED
601 || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
602 // TODO: It would be good to have different icons for insecure vs mixed content.
603 // See http://b/5403800
Steve Block2466eff2011-10-03 15:33:09 +0100604 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700605 }
John Reck0f602f32011-07-07 15:38:43 -0700606 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700607 }
608
John Reck30c714c2010-12-16 17:30:34 -0800609 protected void setUrlTitle(Tab tab) {
610 String url = tab.getUrl();
611 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800612 if (TextUtils.isEmpty(title)) {
613 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800614 }
Michael Kolb66706532010-12-12 19:50:22 -0800615 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700616 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800617 }
618 }
619
620 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800621 protected void setFavicon(Tab tab) {
622 if (tab.inForeground()) {
623 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700624 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800625 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 }
627
628 @Override
629 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 }
631
Michael Kolb66706532010-12-12 19:50:22 -0800632 // active tabs page
633
634 public void showActiveTabsPage() {
635 }
636
637 /**
638 * Remove the active tabs page.
639 */
640 public void removeActiveTabsPage() {
641 }
642
Michael Kolb8233fac2010-10-26 16:08:53 -0700643 // menu handling callbacks
644
645 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800646 public boolean onPrepareOptionsMenu(Menu menu) {
647 return true;
648 }
649
650 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700651 public void updateMenuState(Tab tab, Menu menu) {
652 }
653
654 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 }
657
658 @Override
659 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 }
661
662 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700663 public boolean onOptionsItemSelected(MenuItem item) {
664 return false;
665 }
666
667 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 }
670
671 @Override
672 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 }
674
675 @Override
676 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 }
678
679 @Override
680 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700681 }
682
683 // error console
684
685 @Override
686 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800687 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
689 if (flag) {
690 // Setting the show state of the console will cause it's the layout
691 // to be inflated.
692 if (errorConsole.numberOfErrors() > 0) {
693 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
694 } else {
695 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
696 }
697 if (errorConsole.getParent() != null) {
698 mErrorConsoleContainer.removeView(errorConsole);
699 }
700 // Now we can add it to the main view.
701 mErrorConsoleContainer.addView(errorConsole,
702 new LinearLayout.LayoutParams(
703 ViewGroup.LayoutParams.MATCH_PARENT,
704 ViewGroup.LayoutParams.WRAP_CONTENT));
705 } else {
706 mErrorConsoleContainer.removeView(errorConsole);
707 }
708 }
709
Michael Kolb8233fac2010-10-26 16:08:53 -0700710 // -------------------------------------------------------------------------
711 // Helper function for WebChromeClient
712 // -------------------------------------------------------------------------
713
714 @Override
715 public Bitmap getDefaultVideoPoster() {
716 if (mDefaultVideoPoster == null) {
717 mDefaultVideoPoster = BitmapFactory.decodeResource(
718 mActivity.getResources(), R.drawable.default_video_poster);
719 }
720 return mDefaultVideoPoster;
721 }
722
723 @Override
724 public View getVideoLoadingProgressView() {
725 if (mVideoProgressView == null) {
726 LayoutInflater inflater = LayoutInflater.from(mActivity);
727 mVideoProgressView = inflater.inflate(
728 R.layout.video_loading_progress, null);
729 }
730 return mVideoProgressView;
731 }
732
Michael Kolb843510f2010-12-09 10:51:49 -0800733 @Override
734 public void showMaxTabsWarning() {
735 Toast warning = Toast.makeText(mActivity,
736 mActivity.getString(R.string.max_tabs_warning),
737 Toast.LENGTH_SHORT);
738 warning.show();
739 }
740
Michael Kolb46f987e2011-04-05 10:39:10 -0700741 protected WebView getWebView() {
742 if (mActiveTab != null) {
743 return mActiveTab.getWebView();
744 } else {
745 return null;
746 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700747 }
748
Michael Kolbfedb4922011-04-20 16:45:33 -0700749 protected Menu getMenu() {
750 MenuBuilder menu = new MenuBuilder(mActivity);
751 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
752 return menu;
753 }
754
Michael Kolbc38c6042011-04-27 10:46:06 -0700755 public void setFullscreen(boolean enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700756 Window win = mActivity.getWindow();
757 WindowManager.LayoutParams winParams = win.getAttributes();
Michael Kolb76dff392011-12-06 09:51:18 -0800758 final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
Michael Kolbc38c6042011-04-27 10:46:06 -0700759 if (enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700760 winParams.flags |= bits;
Michael Kolbc38c6042011-04-27 10:46:06 -0700761 } else {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700762 winParams.flags &= ~bits;
763 if (mCustomView != null) {
764 mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
765 } else {
766 mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
767 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700768 }
Michael Kolbc5675ad2011-10-21 13:34:28 -0700769 win.setAttributes(winParams);
Michael Kolbc38c6042011-04-27 10:46:06 -0700770 }
771
John Reck0f602f32011-07-07 15:38:43 -0700772 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700773 Drawable[] array = new Drawable[3];
774 array[0] = new PaintDrawable(Color.BLACK);
775 PaintDrawable p = new PaintDrawable(Color.WHITE);
776 array[1] = p;
777 if (icon == null) {
778 array[2] = mGenericFavicon;
779 } else {
780 array[2] = new BitmapDrawable(icon);
781 }
782 LayerDrawable d = new LayerDrawable(array);
783 d.setLayerInset(1, 1, 1, 1, 1);
784 d.setLayerInset(2, 2, 2, 2, 2);
785 return d;
786 }
787
John Reck5d43ce82011-06-21 17:16:51 -0700788 public boolean isLoading() {
789 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
790 }
791
792 /**
793 * Suggest to the UI that the title bar can be hidden. The UI will then
794 * decide whether or not to hide based off a number of factors, such
795 * as if the user is editing the URL bar or if the page is loading
796 */
797 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700798 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
799 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700800 hideTitleBar();
801 }
802 }
803
John Reckbc6adb42011-09-01 18:03:20 -0700804 protected final void showTitleBarForDuration() {
805 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
806 }
807
808 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700809 showTitleBar();
810 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700811 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700812 }
813
John Reck9c5004e2011-10-07 16:00:12 -0700814 protected Handler mHandler = new Handler() {
John Reck5d43ce82011-06-21 17:16:51 -0700815
816 @Override
817 public void handleMessage(Message msg) {
818 if (msg.what == MSG_HIDE_TITLEBAR) {
819 suggestHideTitleBar();
820 }
John Reck9c5004e2011-10-07 16:00:12 -0700821 BaseUi.this.handleMessage(msg);
John Reck5d43ce82011-06-21 17:16:51 -0700822 }
823 };
John Reck3ba45532011-08-11 16:26:53 -0700824
John Reck9c5004e2011-10-07 16:00:12 -0700825 protected void handleMessage(Message msg) {}
826
John Reck3ba45532011-08-11 16:26:53 -0700827 @Override
828 public void showWeb(boolean animate) {
829 mUiController.hideCustomView();
830 }
831
Michael Kolb31065b12011-10-06 13:51:32 -0700832 static class FullscreenHolder extends FrameLayout {
Michael Kolb53ed62c2011-10-04 16:18:44 -0700833
Michael Kolb31065b12011-10-06 13:51:32 -0700834 public FullscreenHolder(Context ctx) {
835 super(ctx);
836 setBackgroundColor(ctx.getResources().getColor(R.color.black));
Michael Kolb53ed62c2011-10-04 16:18:44 -0700837 }
838
Michael Kolb31065b12011-10-06 13:51:32 -0700839 @Override
840 public boolean onTouchEvent(MotionEvent evt) {
841 return true;
Michael Kolb53ed62c2011-10-04 16:18:44 -0700842 }
Michael Kolb31065b12011-10-06 13:51:32 -0700843
Michael Kolb53ed62c2011-10-04 16:18:44 -0700844 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700845}