blob: 26f90b66908558d0b171d5b11db5e66fdd0e7e66 [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 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;
49import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080050import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.widget.LinearLayout;
52import android.widget.Toast;
53
Steve Block2466eff2011-10-03 15:33:09 +010054import com.android.browser.Tab.SecurityState;
John Reckbf2ec202011-06-29 17:47:38 -070055import com.android.internal.view.menu.MenuBuilder;
56
Michael Kolb1bf23132010-11-19 12:55:12 -080057import java.util.List;
58
Michael Kolb8233fac2010-10-26 16:08:53 -070059/**
60 * UI interface definitions
61 */
John Reck718a24d2011-08-12 11:08:30 -070062public abstract class BaseUi implements UI {
Michael Kolb8233fac2010-10-26 16:08:53 -070063
64 private static final String LOGTAG = "BaseUi";
65
Michael Kolb66706532010-12-12 19:50:22 -080066 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070067 new FrameLayout.LayoutParams(
68 ViewGroup.LayoutParams.MATCH_PARENT,
69 ViewGroup.LayoutParams.MATCH_PARENT);
70
Michael Kolb66706532010-12-12 19:50:22 -080071 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070072 new FrameLayout.LayoutParams(
73 ViewGroup.LayoutParams.MATCH_PARENT,
74 ViewGroup.LayoutParams.MATCH_PARENT,
75 Gravity.CENTER);
76
John Reck5d43ce82011-06-21 17:16:51 -070077 private static final int MSG_HIDE_TITLEBAR = 1;
John Reckef654f12011-07-12 16:42:08 -070078 public static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
John Reck5d43ce82011-06-21 17:16:51 -070079
Michael Kolb8233fac2010-10-26 16:08:53 -070080 Activity mActivity;
81 UiController mUiController;
82 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080083 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080084 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070085
Steve Block2466eff2011-10-03 15:33:09 +010086 private Drawable mLockIconSecure;
87 private Drawable mLockIconMixed;
Michael Kolb5a4372f2011-04-29 13:53:10 -070088 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070089
Michael Kolb66706532010-12-12 19:50:22 -080090 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070091 protected FrameLayout mCustomViewContainer;
Michael Kolb31065b12011-10-06 13:51:32 -070092 protected FrameLayout mFullscreenContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070093
Michael Kolb31065b12011-10-06 13:51:32 -070094 private View mCustomView;
Michael Kolb8233fac2010-10-26 16:08:53 -070095 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040096 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070097
Michael Kolb8233fac2010-10-26 16:08:53 -070098 private LinearLayout mErrorConsoleContainer = null;
99
John Reck718a24d2011-08-12 11:08:30 -0700100 private UrlBarAutoShowManager mUrlBarAutoShowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -0700101
John Reck718a24d2011-08-12 11:08:30 -0700102 private Toast mStopToast;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700103
Michael Kolb8233fac2010-10-26 16:08:53 -0700104 // the default <video> poster
105 private Bitmap mDefaultVideoPoster;
106 // the video progress view
107 private View mVideoProgressView;
108
Michael Kolb8233fac2010-10-26 16:08:53 -0700109 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700110 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700111 protected TitleBar mTitleBar;
112 private NavigationBarBase mNavigationBar;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113
114 public BaseUi(Activity browser, UiController controller) {
115 mActivity = browser;
116 mUiController = controller;
117 mTabControl = controller.getTabControl();
118 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800119 mInputManager = (InputMethodManager)
120 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Steve Block2466eff2011-10-03 15:33:09 +0100121 mLockIconSecure = res.getDrawable(R.drawable.ic_secure_holo_dark);
122 mLockIconMixed = res.getDrawable(R.drawable.ic_secure_partial_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
124 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700125 LayoutInflater.from(mActivity)
126 .inflate(R.layout.custom_screen, frameLayout);
127 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 R.id.main_content);
Michael Kolb53ed62c2011-10-04 16:18:44 -0700129 mCustomViewContainer = (FrameLayout) frameLayout.findViewById(
130 R.id.fullscreen_custom_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700131 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 .findViewById(R.id.error_console);
Michael Kolbc38c6042011-04-27 10:46:06 -0700133 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700134 mGenericFavicon = res.getDrawable(
135 R.drawable.app_web_browser_sm);
John Reck0f602f32011-07-07 15:38:43 -0700136 mTitleBar = new TitleBar(mActivity, mUiController, this,
137 mContentView);
138 mTitleBar.setProgress(100);
139 mNavigationBar = mTitleBar.getNavigationBar();
John Reck718a24d2011-08-12 11:08:30 -0700140 mUrlBarAutoShowManager = new UrlBarAutoShowManager(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 }
142
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 private void cancelStopToast() {
144 if (mStopToast != null) {
145 mStopToast.cancel();
146 mStopToast = null;
147 }
148 }
149
150 // lifecycle
151
152 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800153 if (isCustomViewShowing()) {
154 onHideCustomView();
155 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 cancelStopToast();
157 mActivityPaused = true;
158 }
159
160 public void onResume() {
161 mActivityPaused = false;
Michael Kolb2ae6ef72011-08-22 14:49:34 -0700162 // check if we exited without setting active tab
163 // b: 5188145
Michael Kolb1a4625a2011-08-24 13:40:44 -0700164 final Tab ct = mTabControl.getCurrentTab();
165 if (ct != null) {
166 setActiveTab(ct);
167 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700168 }
169
Michael Kolb66706532010-12-12 19:50:22 -0800170 protected boolean isActivityPaused() {
171 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700172 }
173
174 public void onConfigurationChanged(Configuration config) {
175 }
176
John Reck0f602f32011-07-07 15:38:43 -0700177 public Activity getActivity() {
178 return mActivity;
179 }
180
Michael Kolb8233fac2010-10-26 16:08:53 -0700181 // key handling
182
183 @Override
184 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 if (mCustomView != null) {
186 mUiController.hideCustomView();
187 return true;
188 }
189 return false;
190 }
191
Michael Kolb2814a362011-05-19 15:49:41 -0700192 @Override
193 public boolean onMenuKey() {
194 return false;
195 }
196
John Reck30c714c2010-12-16 17:30:34 -0800197 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 @Override
John Reck30c714c2010-12-16 17:30:34 -0800199 public void onTabDataChanged(Tab tab) {
200 setUrlTitle(tab);
201 setFavicon(tab);
202 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800203 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700204 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700205 mNavigationBar.onTabDataChanged(tab);
Michael Kolba53c9892011-10-05 13:31:40 -0700206 onProgressChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 }
208
209 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500210 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800211 if (tab.inForeground()) {
212 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700213 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800214 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500215 }
216
217 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 public void onPageStopped(Tab tab) {
219 cancelStopToast();
220 if (tab.inForeground()) {
221 mStopToast = Toast
222 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
223 mStopToast.show();
224 }
225 }
226
227 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800228 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700229 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800230 }
231
232 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700234 }
235
236 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800237 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700238 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800239 if ((tab != mActiveTab) && (mActiveTab != null)) {
240 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700241 WebView web = mActiveTab.getWebView();
242 if (web != null) {
243 web.setOnTouchListener(null);
244 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700245 }
Michael Kolb77df4562010-11-19 14:49:34 -0800246 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700247 WebView web = mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700248 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700249 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800251 onTabDataChanged(tab);
252 onProgressChanged(tab);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700253 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
Patrick Scott92066772011-03-10 08:46:27 -0500254 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700255 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700256 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700257 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700258 showTitleBarForDuration();
259 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 }
261
John Reck718a24d2011-08-12 11:08:30 -0700262 protected void updateUrlBarAutoShowManagerTarget() {
263 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
264 if (!mUseQuickControls && web instanceof BrowserWebView) {
265 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
266 } else {
267 mUrlBarAutoShowManager.setTarget(null);
268 }
269 }
270
Michael Kolbcfa3af52010-12-14 10:36:11 -0800271 Tab getActiveTab() {
272 return mActiveTab;
273 }
274
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800276 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800277 }
278
279 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800281 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800283 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700284 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 }
286
287 @Override
288 public void detachTab(Tab tab) {
289 removeTabFromContentView(tab);
290 }
291
292 @Override
293 public void attachTab(Tab tab) {
294 attachTabToContentView(tab);
295 }
296
Michael Kolb377ea312011-02-17 14:36:56 -0800297 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800298 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700299 return;
300 }
301 View container = tab.getViewContainer();
302 WebView mainView = tab.getWebView();
303 // Attach the WebView to the container and then attach the
304 // container to the content view.
305 FrameLayout wrapper =
306 (FrameLayout) container.findViewById(R.id.webview_wrapper);
307 ViewGroup parent = (ViewGroup) mainView.getParent();
308 if (parent != wrapper) {
309 if (parent != null) {
310 Log.w(LOGTAG, "mMainView already has a parent in"
311 + " attachTabToContentView!");
312 parent.removeView(mainView);
313 }
314 wrapper.addView(mainView);
315 } else {
316 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
317 + " attachTabToContentView!");
318 }
319 parent = (ViewGroup) container.getParent();
320 if (parent != mContentView) {
321 if (parent != null) {
322 Log.w(LOGTAG, "mContainer already has a parent in"
323 + " attachTabToContentView!");
324 parent.removeView(container);
325 }
326 mContentView.addView(container, COVER_SCREEN_PARAMS);
327 } else {
328 Log.w(LOGTAG, "mContainer is already attached to content in"
329 + " attachTabToContentView!");
330 }
331 mUiController.attachSubWindow(tab);
332 }
333
334 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800335 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700336 // Remove the container that contains the main WebView.
337 WebView mainView = tab.getWebView();
338 View container = tab.getViewContainer();
339 if (mainView == null) {
340 return;
341 }
342 // Remove the container from the content and then remove the
343 // WebView from the container. This will trigger a focus change
344 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800345 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700346 FrameLayout wrapper =
347 (FrameLayout) container.findViewById(R.id.webview_wrapper);
348 wrapper.removeView(mainView);
349 mContentView.removeView(container);
350 mUiController.endActionMode();
351 mUiController.removeSubWindow(tab);
352 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
353 if (errorConsole != null) {
354 mErrorConsoleContainer.removeView(errorConsole);
355 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 }
357
Michael Kolba713ec82010-11-29 17:27:06 -0800358 @Override
359 public void onSetWebView(Tab tab, WebView webView) {
360 View container = tab.getViewContainer();
361 if (container == null) {
362 // The tab consists of a container view, which contains the main
363 // WebView, as well as any other UI elements associated with the tab.
364 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700365 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800366 tab.setViewContainer(container);
367 }
368 if (tab.getWebView() != webView) {
369 // Just remove the old one.
370 FrameLayout wrapper =
371 (FrameLayout) container.findViewById(R.id.webview_wrapper);
372 wrapper.removeView(tab.getWebView());
373 }
374 }
375
Michael Kolb8233fac2010-10-26 16:08:53 -0700376 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800377 * create a sub window container and webview for the tab
378 * Note: this methods operates through side-effects for now
379 * it sets both the subView and subViewContainer for the given tab
380 * @param tab tab to create the sub window for
381 * @param subView webview to be set as a subwindow for the tab
382 */
383 @Override
384 public void createSubWindow(Tab tab, WebView subView) {
385 View subViewContainer = mActivity.getLayoutInflater().inflate(
386 R.layout.browser_subwindow, null);
387 ViewGroup inner = (ViewGroup) subViewContainer
388 .findViewById(R.id.inner_container);
389 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
390 LayoutParams.MATCH_PARENT));
391 final ImageButton cancel = (ImageButton) subViewContainer
392 .findViewById(R.id.subwindow_close);
393 final WebView cancelSubView = subView;
394 cancel.setOnClickListener(new OnClickListener() {
395 public void onClick(View v) {
396 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
397 }
398 });
399 tab.setSubWebView(subView);
400 tab.setSubViewContainer(subViewContainer);
401 }
402
403 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700404 * Remove the sub window from the content view.
405 */
406 @Override
407 public void removeSubWindow(View subviewContainer) {
408 mContentView.removeView(subviewContainer);
409 mUiController.endActionMode();
410 }
411
412 /**
413 * Attach the sub window to the content view.
414 */
415 @Override
416 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800417 if (container.getParent() != null) {
418 // already attached, remove first
419 ((ViewGroup) container.getParent()).removeView(container);
420 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700421 mContentView.addView(container, COVER_SCREEN_PARAMS);
422 }
423
Michael Kolb11d19782011-03-20 10:17:40 -0700424 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700425 WebView web = getWebView();
426 if (web != null) {
427 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700428 }
429 }
430
Michael Kolb46f987e2011-04-05 10:39:10 -0700431 public void editUrl(boolean clearInput) {
432 if (mUiController.isInCustomActionMode()) {
433 mUiController.endActionMode();
434 }
435 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700436 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
John Reckef654f12011-07-12 16:42:08 -0700437 mNavigationBar.startEditingUrl(clearInput);
438 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700439 }
440
Michael Kolb7cdc4902011-02-03 17:54:40 -0800441 boolean canShowTitleBar() {
442 return !isTitleBarShowing()
443 && !isActivityPaused()
444 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700445 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800446 && !mUiController.isInCustomActionMode();
447 }
448
John Reck0f602f32011-07-07 15:38:43 -0700449 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700450 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700451 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700452 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700453 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800454 }
455
456 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700457 if (mTitleBar.isShowing()) {
458 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700459 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800460 }
461
462 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700463 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800464 }
465
John Reck5d43ce82011-06-21 17:16:51 -0700466 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700467 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700468 }
469
John Reck0f602f32011-07-07 15:38:43 -0700470 public TitleBar getTitleBar() {
471 return mTitleBar;
472 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800473
474 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700475 WebView web = getWebView();
476 if (web != null) {
477 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700478 }
479 }
480
Michael Kolb66706532010-12-12 19:50:22 -0800481 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700482 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700483 mNavigationBar.setInVoiceMode(true, results);
484 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700485 }
486
Michael Kolb66706532010-12-12 19:50:22 -0800487 @Override
488 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700489 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800490 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700491 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700492 }
493
494 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700495 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700496 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700497 }
498
499 @Override
John Reck2bc80422011-06-30 15:11:49 -0700500 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700501 Intent intent = new Intent(mActivity, ComboViewActivity.class);
502 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
503 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
504 Tab t = getActiveTab();
505 if (t != null) {
506 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800507 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700508 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700509 }
510
511 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400512 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700513 WebChromeClient.CustomViewCallback callback) {
514 // if a view already exists then immediately terminate the new one
515 if (mCustomView != null) {
516 callback.onCustomViewHidden();
517 return;
518 }
519
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400520 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb31065b12011-10-06 13:51:32 -0700521 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
522 mFullscreenContainer = new FullscreenHolder(mActivity);
523 mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
524 decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
525 mCustomView = view;
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 Kolb31065b12011-10-06 13:51:32 -0700534 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
535 decor.removeView(mFullscreenContainer);
536 mFullscreenContainer = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700538 mCustomViewCallback.onCustomViewHidden();
539 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400540 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700541 }
542
543 @Override
544 public boolean isCustomViewShowing() {
545 return mCustomView != null;
546 }
547
Michael Kolb66706532010-12-12 19:50:22 -0800548 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800549 if (mInputManager.isActive()) {
550 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
551 0);
552 }
553 }
554
Michael Kolb66706532010-12-12 19:50:22 -0800555 @Override
John Reck3ba45532011-08-11 16:26:53 -0700556 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700557 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800558 }
559
Patrick Scott92066772011-03-10 08:46:27 -0500560 @Override
561 public void showAutoLogin(Tab tab) {
562 updateAutoLogin(tab, true);
563 }
564
565 @Override
566 public void hideAutoLogin(Tab tab) {
567 updateAutoLogin(tab, true);
568 }
569
Michael Kolb8233fac2010-10-26 16:08:53 -0700570 // -------------------------------------------------------------------------
571
Michael Kolb5a72f182011-01-13 20:35:06 -0800572 protected void updateNavigationState(Tab tab) {
573 }
574
Michael Kolb11d19782011-03-20 10:17:40 -0700575 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700576 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700577 }
Patrick Scott92066772011-03-10 08:46:27 -0500578
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 /**
580 * Update the lock icon to correspond to our latest state.
581 */
Michael Kolb66706532010-12-12 19:50:22 -0800582 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800583 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100584 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700585 }
586 }
587
588 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 * Updates the lock-icon image in the title-bar.
590 */
Steve Block2466eff2011-10-03 15:33:09 +0100591 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700592 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100593 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
594 d = mLockIconSecure;
Steve Block4895b012011-10-03 16:26:46 +0100595 } else if (securityState == SecurityState.SECURITY_STATE_MIXED
596 || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
597 // TODO: It would be good to have different icons for insecure vs mixed content.
598 // See http://b/5403800
Steve Block2466eff2011-10-03 15:33:09 +0100599 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700600 }
John Reck0f602f32011-07-07 15:38:43 -0700601 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700602 }
603
John Reck30c714c2010-12-16 17:30:34 -0800604 protected void setUrlTitle(Tab tab) {
605 String url = tab.getUrl();
606 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800607 if (TextUtils.isEmpty(title)) {
608 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800609 }
Michael Kolb66706532010-12-12 19:50:22 -0800610 if (tab.isInVoiceSearchMode()) return;
611 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700612 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800613 }
614 }
615
616 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800617 protected void setFavicon(Tab tab) {
618 if (tab.inForeground()) {
619 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700620 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800621 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 }
623
624 @Override
625 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 }
627
Michael Kolb66706532010-12-12 19:50:22 -0800628 // active tabs page
629
630 public void showActiveTabsPage() {
631 }
632
633 /**
634 * Remove the active tabs page.
635 */
636 public void removeActiveTabsPage() {
637 }
638
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 // menu handling callbacks
640
641 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800642 public boolean onPrepareOptionsMenu(Menu menu) {
643 return true;
644 }
645
646 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700647 public void updateMenuState(Tab tab, Menu menu) {
648 }
649
650 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700651 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 }
653
654 @Override
655 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 }
657
658 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700659 public boolean onOptionsItemSelected(MenuItem item) {
660 return false;
661 }
662
663 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700664 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 }
666
667 @Override
668 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 }
670
671 @Override
672 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 }
674
675 @Override
676 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 }
678
679 // error console
680
681 @Override
682 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800683 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
685 if (flag) {
686 // Setting the show state of the console will cause it's the layout
687 // to be inflated.
688 if (errorConsole.numberOfErrors() > 0) {
689 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
690 } else {
691 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
692 }
693 if (errorConsole.getParent() != null) {
694 mErrorConsoleContainer.removeView(errorConsole);
695 }
696 // Now we can add it to the main view.
697 mErrorConsoleContainer.addView(errorConsole,
698 new LinearLayout.LayoutParams(
699 ViewGroup.LayoutParams.MATCH_PARENT,
700 ViewGroup.LayoutParams.WRAP_CONTENT));
701 } else {
702 mErrorConsoleContainer.removeView(errorConsole);
703 }
704 }
705
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 // -------------------------------------------------------------------------
707 // Helper function for WebChromeClient
708 // -------------------------------------------------------------------------
709
710 @Override
711 public Bitmap getDefaultVideoPoster() {
712 if (mDefaultVideoPoster == null) {
713 mDefaultVideoPoster = BitmapFactory.decodeResource(
714 mActivity.getResources(), R.drawable.default_video_poster);
715 }
716 return mDefaultVideoPoster;
717 }
718
719 @Override
720 public View getVideoLoadingProgressView() {
721 if (mVideoProgressView == null) {
722 LayoutInflater inflater = LayoutInflater.from(mActivity);
723 mVideoProgressView = inflater.inflate(
724 R.layout.video_loading_progress, null);
725 }
726 return mVideoProgressView;
727 }
728
Michael Kolb843510f2010-12-09 10:51:49 -0800729 @Override
730 public void showMaxTabsWarning() {
731 Toast warning = Toast.makeText(mActivity,
732 mActivity.getString(R.string.max_tabs_warning),
733 Toast.LENGTH_SHORT);
734 warning.show();
735 }
736
Michael Kolb46f987e2011-04-05 10:39:10 -0700737 protected WebView getWebView() {
738 if (mActiveTab != null) {
739 return mActiveTab.getWebView();
740 } else {
741 return null;
742 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700743 }
744
Michael Kolbfedb4922011-04-20 16:45:33 -0700745 protected Menu getMenu() {
746 MenuBuilder menu = new MenuBuilder(mActivity);
747 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
748 return menu;
749 }
750
Michael Kolbc38c6042011-04-27 10:46:06 -0700751 public void setFullscreen(boolean enabled) {
752 if (enabled) {
753 mActivity.getWindow().setFlags(
754 WindowManager.LayoutParams.FLAG_FULLSCREEN,
755 WindowManager.LayoutParams.FLAG_FULLSCREEN);
756 } else {
757 mActivity.getWindow().clearFlags(
758 WindowManager.LayoutParams.FLAG_FULLSCREEN);
759 }
760 }
761
John Reck0f602f32011-07-07 15:38:43 -0700762 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700763 Drawable[] array = new Drawable[3];
764 array[0] = new PaintDrawable(Color.BLACK);
765 PaintDrawable p = new PaintDrawable(Color.WHITE);
766 array[1] = p;
767 if (icon == null) {
768 array[2] = mGenericFavicon;
769 } else {
770 array[2] = new BitmapDrawable(icon);
771 }
772 LayerDrawable d = new LayerDrawable(array);
773 d.setLayerInset(1, 1, 1, 1, 1);
774 d.setLayerInset(2, 2, 2, 2, 2);
775 return d;
776 }
777
John Reck5d43ce82011-06-21 17:16:51 -0700778 public boolean isLoading() {
779 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
780 }
781
782 /**
783 * Suggest to the UI that the title bar can be hidden. The UI will then
784 * decide whether or not to hide based off a number of factors, such
785 * as if the user is editing the URL bar or if the page is loading
786 */
787 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700788 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
789 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700790 hideTitleBar();
791 }
792 }
793
John Reckbc6adb42011-09-01 18:03:20 -0700794 protected final void showTitleBarForDuration() {
795 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
796 }
797
798 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700799 showTitleBar();
800 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700801 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700802 }
803
John Reck9c5004e2011-10-07 16:00:12 -0700804 protected Handler mHandler = new Handler() {
John Reck5d43ce82011-06-21 17:16:51 -0700805
806 @Override
807 public void handleMessage(Message msg) {
808 if (msg.what == MSG_HIDE_TITLEBAR) {
809 suggestHideTitleBar();
810 }
John Reck9c5004e2011-10-07 16:00:12 -0700811 BaseUi.this.handleMessage(msg);
John Reck5d43ce82011-06-21 17:16:51 -0700812 }
813 };
John Reck3ba45532011-08-11 16:26:53 -0700814
John Reck9c5004e2011-10-07 16:00:12 -0700815 protected void handleMessage(Message msg) {}
816
John Reck3ba45532011-08-11 16:26:53 -0700817 @Override
818 public void showWeb(boolean animate) {
819 mUiController.hideCustomView();
820 }
821
Michael Kolb31065b12011-10-06 13:51:32 -0700822 static class FullscreenHolder extends FrameLayout {
Michael Kolb53ed62c2011-10-04 16:18:44 -0700823
Michael Kolb31065b12011-10-06 13:51:32 -0700824 public FullscreenHolder(Context ctx) {
825 super(ctx);
826 setBackgroundColor(ctx.getResources().getColor(R.color.black));
Michael Kolb53ed62c2011-10-04 16:18:44 -0700827 }
828
Michael Kolb31065b12011-10-06 13:51:32 -0700829 @Override
830 public boolean onTouchEvent(MotionEvent evt) {
831 return true;
Michael Kolb53ed62c2011-10-04 16:18:44 -0700832 }
Michael Kolb31065b12011-10-06 13:51:32 -0700833
Michael Kolb53ed62c2011-10-04 16:18:44 -0700834 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700835}