blob: ffb2928cf626f956ab666a2b2628dadc72a40b3d [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;
35import android.util.Log;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.view.Gravity;
37import android.view.LayoutInflater;
38import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070039import android.view.MenuItem;
Michael Kolb31065b12011-10-06 13:51:32 -070040import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.view.View;
Michael Kolb1514bb72010-11-22 09:11:48 -080042import android.view.View.OnClickListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080044import android.view.ViewGroup.LayoutParams;
Michael Kolbc5675ad2011-10-21 13:34:28 -070045import android.view.Window;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.view.WindowManager;
Michael Kolb3a696282010-12-05 13:23:24 -080047import android.view.inputmethod.InputMethodManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.webkit.WebChromeClient;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000050import android.webkit.WebViewClassic;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080052import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.widget.LinearLayout;
54import android.widget.Toast;
55
Steve Block2466eff2011-10-03 15:33:09 +010056import com.android.browser.Tab.SecurityState;
John Reckbf2ec202011-06-29 17:47:38 -070057import com.android.internal.view.menu.MenuBuilder;
58
Michael Kolb1bf23132010-11-19 12:55:12 -080059import java.util.List;
60
Michael Kolb8233fac2010-10-26 16:08:53 -070061/**
62 * UI interface definitions
63 */
John Reck718a24d2011-08-12 11:08:30 -070064public abstract class BaseUi implements UI {
Michael Kolb8233fac2010-10-26 16:08:53 -070065
66 private static final String LOGTAG = "BaseUi";
67
Michael Kolb66706532010-12-12 19:50:22 -080068 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070069 new FrameLayout.LayoutParams(
70 ViewGroup.LayoutParams.MATCH_PARENT,
71 ViewGroup.LayoutParams.MATCH_PARENT);
72
Michael Kolb66706532010-12-12 19:50:22 -080073 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070074 new FrameLayout.LayoutParams(
75 ViewGroup.LayoutParams.MATCH_PARENT,
76 ViewGroup.LayoutParams.MATCH_PARENT,
77 Gravity.CENTER);
78
John Reck5d43ce82011-06-21 17:16:51 -070079 private static final int MSG_HIDE_TITLEBAR = 1;
John Reckef654f12011-07-12 16:42:08 -070080 public static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
John Reck5d43ce82011-06-21 17:16:51 -070081
Michael Kolb8233fac2010-10-26 16:08:53 -070082 Activity mActivity;
83 UiController mUiController;
84 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080085 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080086 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070087
Steve Block2466eff2011-10-03 15:33:09 +010088 private Drawable mLockIconSecure;
89 private Drawable mLockIconMixed;
Michael Kolb5a4372f2011-04-29 13:53:10 -070090 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070091
Michael Kolb66706532010-12-12 19:50:22 -080092 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070093 protected FrameLayout mCustomViewContainer;
Michael Kolb31065b12011-10-06 13:51:32 -070094 protected FrameLayout mFullscreenContainer;
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 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
John Reck30c714c2010-12-16 17:30:34 -0800199 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 @Override
John Reck30c714c2010-12-16 17:30:34 -0800201 public void onTabDataChanged(Tab tab) {
202 setUrlTitle(tab);
203 setFavicon(tab);
204 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800205 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700206 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700207 mNavigationBar.onTabDataChanged(tab);
Michael Kolba53c9892011-10-05 13:31:40 -0700208 onProgressChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 }
210
211 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500212 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800213 if (tab.inForeground()) {
214 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700215 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800216 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500217 }
218
219 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 public void onPageStopped(Tab tab) {
221 cancelStopToast();
222 if (tab.inForeground()) {
223 mStopToast = Toast
224 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
225 mStopToast.show();
226 }
227 }
228
229 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800230 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700231 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800232 }
233
234 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700235 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 }
237
238 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800239 public void setActiveTab(final Tab tab) {
Michael Kolb7ac63b62011-12-16 09:52:29 -0800240 if (tab == null) return;
John Reck5d43ce82011-06-21 17:16:51 -0700241 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800242 if ((tab != mActiveTab) && (mActiveTab != null)) {
243 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700244 WebView web = mActiveTab.getWebView();
245 if (web != null) {
246 web.setOnTouchListener(null);
247 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 }
Michael Kolb77df4562010-11-19 14:49:34 -0800249 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700250 WebView web = mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700251 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700252 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700253 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800254 onTabDataChanged(tab);
255 onProgressChanged(tab);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700256 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
Patrick Scott92066772011-03-10 08:46:27 -0500257 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700258 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700259 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700260 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700261 showTitleBarForDuration();
262 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 }
264
John Reck718a24d2011-08-12 11:08:30 -0700265 protected void updateUrlBarAutoShowManagerTarget() {
266 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
267 if (!mUseQuickControls && web instanceof BrowserWebView) {
268 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
269 } else {
270 mUrlBarAutoShowManager.setTarget(null);
271 }
272 }
273
Michael Kolbcfa3af52010-12-14 10:36:11 -0800274 Tab getActiveTab() {
275 return mActiveTab;
276 }
277
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800279 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800280 }
281
282 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800284 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800286 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700288 }
289
290 @Override
291 public void detachTab(Tab tab) {
292 removeTabFromContentView(tab);
293 }
294
295 @Override
296 public void attachTab(Tab tab) {
297 attachTabToContentView(tab);
298 }
299
Michael Kolb377ea312011-02-17 14:36:56 -0800300 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800301 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700302 return;
303 }
304 View container = tab.getViewContainer();
305 WebView mainView = tab.getWebView();
306 // Attach the WebView to the container and then attach the
307 // container to the content view.
308 FrameLayout wrapper =
309 (FrameLayout) container.findViewById(R.id.webview_wrapper);
310 ViewGroup parent = (ViewGroup) mainView.getParent();
311 if (parent != wrapper) {
312 if (parent != null) {
313 Log.w(LOGTAG, "mMainView already has a parent in"
314 + " attachTabToContentView!");
315 parent.removeView(mainView);
316 }
317 wrapper.addView(mainView);
318 } else {
319 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
320 + " attachTabToContentView!");
321 }
322 parent = (ViewGroup) container.getParent();
323 if (parent != mContentView) {
324 if (parent != null) {
325 Log.w(LOGTAG, "mContainer already has a parent in"
326 + " attachTabToContentView!");
327 parent.removeView(container);
328 }
329 mContentView.addView(container, COVER_SCREEN_PARAMS);
330 } else {
331 Log.w(LOGTAG, "mContainer is already attached to content in"
332 + " attachTabToContentView!");
333 }
334 mUiController.attachSubWindow(tab);
335 }
336
337 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800338 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 // Remove the container that contains the main WebView.
340 WebView mainView = tab.getWebView();
341 View container = tab.getViewContainer();
342 if (mainView == null) {
343 return;
344 }
345 // Remove the container from the content and then remove the
346 // WebView from the container. This will trigger a focus change
347 // needed by WebView.
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000348 WebViewClassic.fromWebView(mainView).setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700349 FrameLayout wrapper =
350 (FrameLayout) container.findViewById(R.id.webview_wrapper);
351 wrapper.removeView(mainView);
352 mContentView.removeView(container);
353 mUiController.endActionMode();
354 mUiController.removeSubWindow(tab);
355 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
356 if (errorConsole != null) {
357 mErrorConsoleContainer.removeView(errorConsole);
358 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 }
360
Michael Kolba713ec82010-11-29 17:27:06 -0800361 @Override
362 public void onSetWebView(Tab tab, WebView webView) {
363 View container = tab.getViewContainer();
364 if (container == null) {
365 // The tab consists of a container view, which contains the main
366 // WebView, as well as any other UI elements associated with the tab.
367 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700368 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800369 tab.setViewContainer(container);
370 }
371 if (tab.getWebView() != webView) {
372 // Just remove the old one.
373 FrameLayout wrapper =
374 (FrameLayout) container.findViewById(R.id.webview_wrapper);
375 wrapper.removeView(tab.getWebView());
376 }
377 }
378
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800380 * create a sub window container and webview for the tab
381 * Note: this methods operates through side-effects for now
382 * it sets both the subView and subViewContainer for the given tab
383 * @param tab tab to create the sub window for
384 * @param subView webview to be set as a subwindow for the tab
385 */
386 @Override
387 public void createSubWindow(Tab tab, WebView subView) {
388 View subViewContainer = mActivity.getLayoutInflater().inflate(
389 R.layout.browser_subwindow, null);
390 ViewGroup inner = (ViewGroup) subViewContainer
391 .findViewById(R.id.inner_container);
392 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
393 LayoutParams.MATCH_PARENT));
394 final ImageButton cancel = (ImageButton) subViewContainer
395 .findViewById(R.id.subwindow_close);
396 final WebView cancelSubView = subView;
397 cancel.setOnClickListener(new OnClickListener() {
398 public void onClick(View v) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000399 WebViewClassic.fromWebView(cancelSubView).getWebChromeClient().onCloseWindow(
400 cancelSubView);
Michael Kolb1514bb72010-11-22 09:11:48 -0800401 }
402 });
403 tab.setSubWebView(subView);
404 tab.setSubViewContainer(subViewContainer);
405 }
406
407 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700408 * Remove the sub window from the content view.
409 */
410 @Override
411 public void removeSubWindow(View subviewContainer) {
412 mContentView.removeView(subviewContainer);
413 mUiController.endActionMode();
414 }
415
416 /**
417 * Attach the sub window to the content view.
418 */
419 @Override
420 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800421 if (container.getParent() != null) {
422 // already attached, remove first
423 ((ViewGroup) container.getParent()).removeView(container);
424 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700425 mContentView.addView(container, COVER_SCREEN_PARAMS);
426 }
427
Michael Kolb11d19782011-03-20 10:17:40 -0700428 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700429 WebView web = getWebView();
430 if (web != null) {
431 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700432 }
433 }
434
Michael Kolb46f987e2011-04-05 10:39:10 -0700435 public void editUrl(boolean clearInput) {
436 if (mUiController.isInCustomActionMode()) {
437 mUiController.endActionMode();
438 }
439 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700440 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
John Reckef654f12011-07-12 16:42:08 -0700441 mNavigationBar.startEditingUrl(clearInput);
442 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700443 }
444
Michael Kolb7cdc4902011-02-03 17:54:40 -0800445 boolean canShowTitleBar() {
446 return !isTitleBarShowing()
447 && !isActivityPaused()
448 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700449 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800450 && !mUiController.isInCustomActionMode();
451 }
452
John Reck0f602f32011-07-07 15:38:43 -0700453 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700454 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700455 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700456 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700457 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800458 }
459
460 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700461 if (mTitleBar.isShowing()) {
462 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700463 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800464 }
465
466 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700467 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800468 }
469
John Reck5d43ce82011-06-21 17:16:51 -0700470 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700471 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700472 }
473
John Reck0f602f32011-07-07 15:38:43 -0700474 public TitleBar getTitleBar() {
475 return mTitleBar;
476 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800477
478 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700479 WebView web = getWebView();
480 if (web != null) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000481 WebViewClassic.fromWebView(web).setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700482 }
483 }
484
Michael Kolb66706532010-12-12 19:50:22 -0800485 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700486 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700487 mNavigationBar.setInVoiceMode(true, results);
488 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700489 }
490
Michael Kolb66706532010-12-12 19:50:22 -0800491 @Override
492 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700493 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800494 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700495 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 }
497
498 @Override
John Reck2bc80422011-06-30 15:11:49 -0700499 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700500 Intent intent = new Intent(mActivity, ComboViewActivity.class);
501 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
502 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
503 Tab t = getActiveTab();
504 if (t != null) {
505 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800506 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700507 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 }
509
510 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400511 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 WebChromeClient.CustomViewCallback callback) {
513 // if a view already exists then immediately terminate the new one
514 if (mCustomView != null) {
515 callback.onCustomViewHidden();
516 return;
517 }
518
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400519 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb31065b12011-10-06 13:51:32 -0700520 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
521 mFullscreenContainer = new FullscreenHolder(mActivity);
522 mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
523 decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
524 mCustomView = view;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700525 setFullscreen(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 mCustomViewCallback = callback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400527 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 }
529
530 @Override
531 public void onHideCustomView() {
532 if (mCustomView == null)
533 return;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700534 setFullscreen(false);
Michael Kolb31065b12011-10-06 13:51:32 -0700535 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
536 decor.removeView(mFullscreenContainer);
537 mFullscreenContainer = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700538 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 mCustomViewCallback.onCustomViewHidden();
540 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400541 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 }
543
544 @Override
545 public boolean isCustomViewShowing() {
546 return mCustomView != null;
547 }
548
Michael Kolb66706532010-12-12 19:50:22 -0800549 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800550 if (mInputManager.isActive()) {
551 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
552 0);
553 }
554 }
555
Michael Kolb66706532010-12-12 19:50:22 -0800556 @Override
John Reck3ba45532011-08-11 16:26:53 -0700557 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700558 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800559 }
560
Patrick Scott92066772011-03-10 08:46:27 -0500561 @Override
562 public void showAutoLogin(Tab tab) {
563 updateAutoLogin(tab, true);
564 }
565
566 @Override
567 public void hideAutoLogin(Tab tab) {
568 updateAutoLogin(tab, true);
569 }
570
Michael Kolb8233fac2010-10-26 16:08:53 -0700571 // -------------------------------------------------------------------------
572
Michael Kolb5a72f182011-01-13 20:35:06 -0800573 protected void updateNavigationState(Tab tab) {
574 }
575
Michael Kolb11d19782011-03-20 10:17:40 -0700576 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700577 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700578 }
Patrick Scott92066772011-03-10 08:46:27 -0500579
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 /**
581 * Update the lock icon to correspond to our latest state.
582 */
Michael Kolb66706532010-12-12 19:50:22 -0800583 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800584 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100585 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700586 }
587 }
588
589 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 * Updates the lock-icon image in the title-bar.
591 */
Steve Block2466eff2011-10-03 15:33:09 +0100592 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100594 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
595 d = mLockIconSecure;
Steve Block4895b012011-10-03 16:26:46 +0100596 } else if (securityState == SecurityState.SECURITY_STATE_MIXED
597 || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
598 // TODO: It would be good to have different icons for insecure vs mixed content.
599 // See http://b/5403800
Steve Block2466eff2011-10-03 15:33:09 +0100600 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 }
John Reck0f602f32011-07-07 15:38:43 -0700602 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 }
604
John Reck30c714c2010-12-16 17:30:34 -0800605 protected void setUrlTitle(Tab tab) {
606 String url = tab.getUrl();
607 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800608 if (TextUtils.isEmpty(title)) {
609 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800610 }
Michael Kolb66706532010-12-12 19:50:22 -0800611 if (tab.isInVoiceSearchMode()) return;
612 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700613 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800614 }
615 }
616
617 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800618 protected void setFavicon(Tab tab) {
619 if (tab.inForeground()) {
620 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700621 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800622 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700623 }
624
625 @Override
626 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 }
628
Michael Kolb66706532010-12-12 19:50:22 -0800629 // active tabs page
630
631 public void showActiveTabsPage() {
632 }
633
634 /**
635 * Remove the active tabs page.
636 */
637 public void removeActiveTabsPage() {
638 }
639
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 // menu handling callbacks
641
642 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800643 public boolean onPrepareOptionsMenu(Menu menu) {
644 return true;
645 }
646
647 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700648 public void updateMenuState(Tab tab, Menu menu) {
649 }
650
651 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 }
654
655 @Override
656 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700657 }
658
659 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700660 public boolean onOptionsItemSelected(MenuItem item) {
661 return false;
662 }
663
664 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 }
667
668 @Override
669 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 }
671
672 @Override
673 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700674 }
675
676 @Override
677 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 }
679
680 // error console
681
682 @Override
683 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800684 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700685 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
686 if (flag) {
687 // Setting the show state of the console will cause it's the layout
688 // to be inflated.
689 if (errorConsole.numberOfErrors() > 0) {
690 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
691 } else {
692 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
693 }
694 if (errorConsole.getParent() != null) {
695 mErrorConsoleContainer.removeView(errorConsole);
696 }
697 // Now we can add it to the main view.
698 mErrorConsoleContainer.addView(errorConsole,
699 new LinearLayout.LayoutParams(
700 ViewGroup.LayoutParams.MATCH_PARENT,
701 ViewGroup.LayoutParams.WRAP_CONTENT));
702 } else {
703 mErrorConsoleContainer.removeView(errorConsole);
704 }
705 }
706
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 // -------------------------------------------------------------------------
708 // Helper function for WebChromeClient
709 // -------------------------------------------------------------------------
710
711 @Override
712 public Bitmap getDefaultVideoPoster() {
713 if (mDefaultVideoPoster == null) {
714 mDefaultVideoPoster = BitmapFactory.decodeResource(
715 mActivity.getResources(), R.drawable.default_video_poster);
716 }
717 return mDefaultVideoPoster;
718 }
719
720 @Override
721 public View getVideoLoadingProgressView() {
722 if (mVideoProgressView == null) {
723 LayoutInflater inflater = LayoutInflater.from(mActivity);
724 mVideoProgressView = inflater.inflate(
725 R.layout.video_loading_progress, null);
726 }
727 return mVideoProgressView;
728 }
729
Michael Kolb843510f2010-12-09 10:51:49 -0800730 @Override
731 public void showMaxTabsWarning() {
732 Toast warning = Toast.makeText(mActivity,
733 mActivity.getString(R.string.max_tabs_warning),
734 Toast.LENGTH_SHORT);
735 warning.show();
736 }
737
Michael Kolb46f987e2011-04-05 10:39:10 -0700738 protected WebView getWebView() {
739 if (mActiveTab != null) {
740 return mActiveTab.getWebView();
741 } else {
742 return null;
743 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700744 }
745
Michael Kolbfedb4922011-04-20 16:45:33 -0700746 protected Menu getMenu() {
747 MenuBuilder menu = new MenuBuilder(mActivity);
748 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
749 return menu;
750 }
751
Michael Kolbc38c6042011-04-27 10:46:06 -0700752 public void setFullscreen(boolean enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700753 Window win = mActivity.getWindow();
754 WindowManager.LayoutParams winParams = win.getAttributes();
Michael Kolb76dff392011-12-06 09:51:18 -0800755 final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
Michael Kolbc38c6042011-04-27 10:46:06 -0700756 if (enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700757 winParams.flags |= bits;
Michael Kolbc38c6042011-04-27 10:46:06 -0700758 } else {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700759 winParams.flags &= ~bits;
760 if (mCustomView != null) {
761 mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
762 } else {
763 mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
764 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700765 }
Michael Kolbc5675ad2011-10-21 13:34:28 -0700766 win.setAttributes(winParams);
Michael Kolbc38c6042011-04-27 10:46:06 -0700767 }
768
John Reck0f602f32011-07-07 15:38:43 -0700769 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700770 Drawable[] array = new Drawable[3];
771 array[0] = new PaintDrawable(Color.BLACK);
772 PaintDrawable p = new PaintDrawable(Color.WHITE);
773 array[1] = p;
774 if (icon == null) {
775 array[2] = mGenericFavicon;
776 } else {
777 array[2] = new BitmapDrawable(icon);
778 }
779 LayerDrawable d = new LayerDrawable(array);
780 d.setLayerInset(1, 1, 1, 1, 1);
781 d.setLayerInset(2, 2, 2, 2, 2);
782 return d;
783 }
784
John Reck5d43ce82011-06-21 17:16:51 -0700785 public boolean isLoading() {
786 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
787 }
788
789 /**
790 * Suggest to the UI that the title bar can be hidden. The UI will then
791 * decide whether or not to hide based off a number of factors, such
792 * as if the user is editing the URL bar or if the page is loading
793 */
794 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700795 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
796 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700797 hideTitleBar();
798 }
799 }
800
John Reckbc6adb42011-09-01 18:03:20 -0700801 protected final void showTitleBarForDuration() {
802 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
803 }
804
805 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700806 showTitleBar();
807 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700808 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700809 }
810
John Reck9c5004e2011-10-07 16:00:12 -0700811 protected Handler mHandler = new Handler() {
John Reck5d43ce82011-06-21 17:16:51 -0700812
813 @Override
814 public void handleMessage(Message msg) {
815 if (msg.what == MSG_HIDE_TITLEBAR) {
816 suggestHideTitleBar();
817 }
John Reck9c5004e2011-10-07 16:00:12 -0700818 BaseUi.this.handleMessage(msg);
John Reck5d43ce82011-06-21 17:16:51 -0700819 }
820 };
John Reck3ba45532011-08-11 16:26:53 -0700821
John Reck9c5004e2011-10-07 16:00:12 -0700822 protected void handleMessage(Message msg) {}
823
John Reck3ba45532011-08-11 16:26:53 -0700824 @Override
825 public void showWeb(boolean animate) {
826 mUiController.hideCustomView();
827 }
828
Michael Kolb31065b12011-10-06 13:51:32 -0700829 static class FullscreenHolder extends FrameLayout {
Michael Kolb53ed62c2011-10-04 16:18:44 -0700830
Michael Kolb31065b12011-10-06 13:51:32 -0700831 public FullscreenHolder(Context ctx) {
832 super(ctx);
833 setBackgroundColor(ctx.getResources().getColor(R.color.black));
Michael Kolb53ed62c2011-10-04 16:18:44 -0700834 }
835
Michael Kolb31065b12011-10-06 13:51:32 -0700836 @Override
837 public boolean onTouchEvent(MotionEvent evt) {
838 return true;
Michael Kolb53ed62c2011-10-04 16:18:44 -0700839 }
Michael Kolb31065b12011-10-06 13:51:32 -0700840
Michael Kolb53ed62c2011-10-04 16:18:44 -0700841 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700842}