blob: e440d2cad42545bbee522dcbe4d3d7f2cc359c8e [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 intent.putExtra(ComboViewActivity.EXTRA_BOOKMARK_PAGE,
509 mUiController.createBookmarkCurrentPageIntent(false));
510 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 }
512
513 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400514 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700515 WebChromeClient.CustomViewCallback callback) {
516 // if a view already exists then immediately terminate the new one
517 if (mCustomView != null) {
518 callback.onCustomViewHidden();
519 return;
520 }
521
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400522 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb31065b12011-10-06 13:51:32 -0700523 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
524 mFullscreenContainer = new FullscreenHolder(mActivity);
525 mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
526 decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
527 mCustomView = view;
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 mCustomViewCallback = callback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400529 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 }
531
532 @Override
533 public void onHideCustomView() {
534 if (mCustomView == null)
535 return;
Michael Kolb31065b12011-10-06 13:51:32 -0700536 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
537 decor.removeView(mFullscreenContainer);
538 mFullscreenContainer = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 mCustomViewCallback.onCustomViewHidden();
541 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400542 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 }
544
545 @Override
546 public boolean isCustomViewShowing() {
547 return mCustomView != null;
548 }
549
Michael Kolb66706532010-12-12 19:50:22 -0800550 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800551 if (mInputManager.isActive()) {
552 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
553 0);
554 }
555 }
556
Michael Kolb66706532010-12-12 19:50:22 -0800557 @Override
John Reck3ba45532011-08-11 16:26:53 -0700558 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700559 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800560 }
561
Patrick Scott92066772011-03-10 08:46:27 -0500562 @Override
563 public void showAutoLogin(Tab tab) {
564 updateAutoLogin(tab, true);
565 }
566
567 @Override
568 public void hideAutoLogin(Tab tab) {
569 updateAutoLogin(tab, true);
570 }
571
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 // -------------------------------------------------------------------------
573
Michael Kolb5a72f182011-01-13 20:35:06 -0800574 protected void updateNavigationState(Tab tab) {
575 }
576
Michael Kolb11d19782011-03-20 10:17:40 -0700577 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700578 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700579 }
Patrick Scott92066772011-03-10 08:46:27 -0500580
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 /**
582 * Update the lock icon to correspond to our latest state.
583 */
Michael Kolb66706532010-12-12 19:50:22 -0800584 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800585 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100586 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 }
588 }
589
590 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700591 * Updates the lock-icon image in the title-bar.
592 */
Steve Block2466eff2011-10-03 15:33:09 +0100593 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100595 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
596 d = mLockIconSecure;
597 } else if (securityState == SecurityState.SECURITY_STATE_MIXED) {
598 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 }
John Reck0f602f32011-07-07 15:38:43 -0700600 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 }
602
John Reck30c714c2010-12-16 17:30:34 -0800603 protected void setUrlTitle(Tab tab) {
604 String url = tab.getUrl();
605 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800606 if (TextUtils.isEmpty(title)) {
607 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800608 }
Michael Kolb66706532010-12-12 19:50:22 -0800609 if (tab.isInVoiceSearchMode()) return;
610 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700611 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800612 }
613 }
614
615 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800616 protected void setFavicon(Tab tab) {
617 if (tab.inForeground()) {
618 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700619 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800620 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 }
622
623 @Override
624 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 }
626
Michael Kolb66706532010-12-12 19:50:22 -0800627 // active tabs page
628
629 public void showActiveTabsPage() {
630 }
631
632 /**
633 * Remove the active tabs page.
634 */
635 public void removeActiveTabsPage() {
636 }
637
Michael Kolb8233fac2010-10-26 16:08:53 -0700638 // menu handling callbacks
639
640 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800641 public boolean onPrepareOptionsMenu(Menu menu) {
642 return true;
643 }
644
645 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700646 public void updateMenuState(Tab tab, Menu menu) {
647 }
648
649 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700650 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700651 }
652
653 @Override
654 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 }
656
657 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700658 public boolean onOptionsItemSelected(MenuItem item) {
659 return false;
660 }
661
662 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700664 }
665
666 @Override
667 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 }
669
670 @Override
671 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 }
673
674 @Override
675 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 }
677
678 // error console
679
680 @Override
681 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800682 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
684 if (flag) {
685 // Setting the show state of the console will cause it's the layout
686 // to be inflated.
687 if (errorConsole.numberOfErrors() > 0) {
688 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
689 } else {
690 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
691 }
692 if (errorConsole.getParent() != null) {
693 mErrorConsoleContainer.removeView(errorConsole);
694 }
695 // Now we can add it to the main view.
696 mErrorConsoleContainer.addView(errorConsole,
697 new LinearLayout.LayoutParams(
698 ViewGroup.LayoutParams.MATCH_PARENT,
699 ViewGroup.LayoutParams.WRAP_CONTENT));
700 } else {
701 mErrorConsoleContainer.removeView(errorConsole);
702 }
703 }
704
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 // -------------------------------------------------------------------------
706 // Helper function for WebChromeClient
707 // -------------------------------------------------------------------------
708
709 @Override
710 public Bitmap getDefaultVideoPoster() {
711 if (mDefaultVideoPoster == null) {
712 mDefaultVideoPoster = BitmapFactory.decodeResource(
713 mActivity.getResources(), R.drawable.default_video_poster);
714 }
715 return mDefaultVideoPoster;
716 }
717
718 @Override
719 public View getVideoLoadingProgressView() {
720 if (mVideoProgressView == null) {
721 LayoutInflater inflater = LayoutInflater.from(mActivity);
722 mVideoProgressView = inflater.inflate(
723 R.layout.video_loading_progress, null);
724 }
725 return mVideoProgressView;
726 }
727
Michael Kolb843510f2010-12-09 10:51:49 -0800728 @Override
729 public void showMaxTabsWarning() {
730 Toast warning = Toast.makeText(mActivity,
731 mActivity.getString(R.string.max_tabs_warning),
732 Toast.LENGTH_SHORT);
733 warning.show();
734 }
735
Michael Kolb46f987e2011-04-05 10:39:10 -0700736 protected WebView getWebView() {
737 if (mActiveTab != null) {
738 return mActiveTab.getWebView();
739 } else {
740 return null;
741 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700742 }
743
Michael Kolbfedb4922011-04-20 16:45:33 -0700744 protected Menu getMenu() {
745 MenuBuilder menu = new MenuBuilder(mActivity);
746 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
747 return menu;
748 }
749
Michael Kolbc38c6042011-04-27 10:46:06 -0700750 public void setFullscreen(boolean enabled) {
751 if (enabled) {
752 mActivity.getWindow().setFlags(
753 WindowManager.LayoutParams.FLAG_FULLSCREEN,
754 WindowManager.LayoutParams.FLAG_FULLSCREEN);
755 } else {
756 mActivity.getWindow().clearFlags(
757 WindowManager.LayoutParams.FLAG_FULLSCREEN);
758 }
759 }
760
John Reck0f602f32011-07-07 15:38:43 -0700761 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700762 Drawable[] array = new Drawable[3];
763 array[0] = new PaintDrawable(Color.BLACK);
764 PaintDrawable p = new PaintDrawable(Color.WHITE);
765 array[1] = p;
766 if (icon == null) {
767 array[2] = mGenericFavicon;
768 } else {
769 array[2] = new BitmapDrawable(icon);
770 }
771 LayerDrawable d = new LayerDrawable(array);
772 d.setLayerInset(1, 1, 1, 1, 1);
773 d.setLayerInset(2, 2, 2, 2, 2);
774 return d;
775 }
776
John Reck5d43ce82011-06-21 17:16:51 -0700777 public boolean isLoading() {
778 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
779 }
780
781 /**
782 * Suggest to the UI that the title bar can be hidden. The UI will then
783 * decide whether or not to hide based off a number of factors, such
784 * as if the user is editing the URL bar or if the page is loading
785 */
786 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700787 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
788 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700789 hideTitleBar();
790 }
791 }
792
John Reckbc6adb42011-09-01 18:03:20 -0700793 protected final void showTitleBarForDuration() {
794 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
795 }
796
797 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700798 showTitleBar();
799 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700800 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700801 }
802
John Reck5d43ce82011-06-21 17:16:51 -0700803 private Handler mHandler = new Handler() {
804
805 @Override
806 public void handleMessage(Message msg) {
807 if (msg.what == MSG_HIDE_TITLEBAR) {
808 suggestHideTitleBar();
809 }
810 }
811 };
John Reck3ba45532011-08-11 16:26:53 -0700812
813 @Override
814 public void showWeb(boolean animate) {
815 mUiController.hideCustomView();
816 }
817
Michael Kolb31065b12011-10-06 13:51:32 -0700818 static class FullscreenHolder extends FrameLayout {
Michael Kolb53ed62c2011-10-04 16:18:44 -0700819
Michael Kolb31065b12011-10-06 13:51:32 -0700820 public FullscreenHolder(Context ctx) {
821 super(ctx);
822 setBackgroundColor(ctx.getResources().getColor(R.color.black));
Michael Kolb53ed62c2011-10-04 16:18:44 -0700823 }
824
Michael Kolb31065b12011-10-06 13:51:32 -0700825 @Override
826 public boolean onTouchEvent(MotionEvent evt) {
827 return true;
Michael Kolb53ed62c2011-10-04 16:18:44 -0700828 }
Michael Kolb31065b12011-10-06 13:51:32 -0700829
Michael Kolb53ed62c2011-10-04 16:18:44 -0700830 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700831}