blob: f93edde59a7501d774a8941bc1574c9a16b2167f [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;
Michael Kolb53ed62c2011-10-04 16:18:44 -070037import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.view.LayoutInflater;
39import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070040import android.view.MenuItem;
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 Kolb8233fac2010-10-26 16:08:53 -070092
Michael Kolb53ed62c2011-10-04 16:18:44 -070093 private CustomViewHolder mCustomView;
Michael Kolb8233fac2010-10-26 16:08:53 -070094 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040095 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070096
Michael Kolb8233fac2010-10-26 16:08:53 -070097 private LinearLayout mErrorConsoleContainer = null;
98
John Reck718a24d2011-08-12 11:08:30 -070099 private UrlBarAutoShowManager mUrlBarAutoShowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -0700100
John Reck718a24d2011-08-12 11:08:30 -0700101 private Toast mStopToast;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700102
Michael Kolb8233fac2010-10-26 16:08:53 -0700103 // the default <video> poster
104 private Bitmap mDefaultVideoPoster;
105 // the video progress view
106 private View mVideoProgressView;
107
Michael Kolb8233fac2010-10-26 16:08:53 -0700108 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700109 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700110 protected TitleBar mTitleBar;
111 private NavigationBarBase mNavigationBar;
Michael Kolb8233fac2010-10-26 16:08:53 -0700112
113 public BaseUi(Activity browser, UiController controller) {
114 mActivity = browser;
115 mUiController = controller;
116 mTabControl = controller.getTabControl();
117 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800118 mInputManager = (InputMethodManager)
119 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Steve Block2466eff2011-10-03 15:33:09 +0100120 mLockIconSecure = res.getDrawable(R.drawable.ic_secure_holo_dark);
121 mLockIconMixed = res.getDrawable(R.drawable.ic_secure_partial_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700122
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 Kolb8233fac2010-10-26 16:08:53 -0700206 }
207
208 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500209 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800210 if (tab.inForeground()) {
211 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700212 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800213 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500214 }
215
216 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 public void onPageStopped(Tab tab) {
218 cancelStopToast();
219 if (tab.inForeground()) {
220 mStopToast = Toast
221 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
222 mStopToast.show();
223 }
224 }
225
226 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800227 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700228 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800229 }
230
231 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 }
234
235 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800236 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700237 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800238 if ((tab != mActiveTab) && (mActiveTab != null)) {
239 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700240 WebView web = mActiveTab.getWebView();
241 if (web != null) {
242 web.setOnTouchListener(null);
243 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700244 }
Michael Kolb77df4562010-11-19 14:49:34 -0800245 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700246 WebView web = mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700247 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700248 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800250 onTabDataChanged(tab);
251 onProgressChanged(tab);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700252 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
Patrick Scott92066772011-03-10 08:46:27 -0500253 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700254 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700255 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700256 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700257 showTitleBarForDuration();
258 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 }
260
John Reck718a24d2011-08-12 11:08:30 -0700261 protected void updateUrlBarAutoShowManagerTarget() {
262 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
263 if (!mUseQuickControls && web instanceof BrowserWebView) {
264 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
265 } else {
266 mUrlBarAutoShowManager.setTarget(null);
267 }
268 }
269
Michael Kolbcfa3af52010-12-14 10:36:11 -0800270 Tab getActiveTab() {
271 return mActiveTab;
272 }
273
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800275 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800276 }
277
278 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800280 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800282 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700284 }
285
286 @Override
287 public void detachTab(Tab tab) {
288 removeTabFromContentView(tab);
289 }
290
291 @Override
292 public void attachTab(Tab tab) {
293 attachTabToContentView(tab);
294 }
295
Michael Kolb377ea312011-02-17 14:36:56 -0800296 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800297 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700298 return;
299 }
300 View container = tab.getViewContainer();
301 WebView mainView = tab.getWebView();
302 // Attach the WebView to the container and then attach the
303 // container to the content view.
304 FrameLayout wrapper =
305 (FrameLayout) container.findViewById(R.id.webview_wrapper);
306 ViewGroup parent = (ViewGroup) mainView.getParent();
307 if (parent != wrapper) {
308 if (parent != null) {
309 Log.w(LOGTAG, "mMainView already has a parent in"
310 + " attachTabToContentView!");
311 parent.removeView(mainView);
312 }
313 wrapper.addView(mainView);
314 } else {
315 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
316 + " attachTabToContentView!");
317 }
318 parent = (ViewGroup) container.getParent();
319 if (parent != mContentView) {
320 if (parent != null) {
321 Log.w(LOGTAG, "mContainer already has a parent in"
322 + " attachTabToContentView!");
323 parent.removeView(container);
324 }
325 mContentView.addView(container, COVER_SCREEN_PARAMS);
326 } else {
327 Log.w(LOGTAG, "mContainer is already attached to content in"
328 + " attachTabToContentView!");
329 }
330 mUiController.attachSubWindow(tab);
331 }
332
333 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800334 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 // Remove the container that contains the main WebView.
336 WebView mainView = tab.getWebView();
337 View container = tab.getViewContainer();
338 if (mainView == null) {
339 return;
340 }
341 // Remove the container from the content and then remove the
342 // WebView from the container. This will trigger a focus change
343 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800344 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700345 FrameLayout wrapper =
346 (FrameLayout) container.findViewById(R.id.webview_wrapper);
347 wrapper.removeView(mainView);
348 mContentView.removeView(container);
349 mUiController.endActionMode();
350 mUiController.removeSubWindow(tab);
351 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
352 if (errorConsole != null) {
353 mErrorConsoleContainer.removeView(errorConsole);
354 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
356
Michael Kolba713ec82010-11-29 17:27:06 -0800357 @Override
358 public void onSetWebView(Tab tab, WebView webView) {
359 View container = tab.getViewContainer();
360 if (container == null) {
361 // The tab consists of a container view, which contains the main
362 // WebView, as well as any other UI elements associated with the tab.
363 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700364 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800365 tab.setViewContainer(container);
366 }
367 if (tab.getWebView() != webView) {
368 // Just remove the old one.
369 FrameLayout wrapper =
370 (FrameLayout) container.findViewById(R.id.webview_wrapper);
371 wrapper.removeView(tab.getWebView());
372 }
373 }
374
Michael Kolb8233fac2010-10-26 16:08:53 -0700375 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800376 * create a sub window container and webview for the tab
377 * Note: this methods operates through side-effects for now
378 * it sets both the subView and subViewContainer for the given tab
379 * @param tab tab to create the sub window for
380 * @param subView webview to be set as a subwindow for the tab
381 */
382 @Override
383 public void createSubWindow(Tab tab, WebView subView) {
384 View subViewContainer = mActivity.getLayoutInflater().inflate(
385 R.layout.browser_subwindow, null);
386 ViewGroup inner = (ViewGroup) subViewContainer
387 .findViewById(R.id.inner_container);
388 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
389 LayoutParams.MATCH_PARENT));
390 final ImageButton cancel = (ImageButton) subViewContainer
391 .findViewById(R.id.subwindow_close);
392 final WebView cancelSubView = subView;
393 cancel.setOnClickListener(new OnClickListener() {
394 public void onClick(View v) {
395 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
396 }
397 });
398 tab.setSubWebView(subView);
399 tab.setSubViewContainer(subViewContainer);
400 }
401
402 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 * Remove the sub window from the content view.
404 */
405 @Override
406 public void removeSubWindow(View subviewContainer) {
407 mContentView.removeView(subviewContainer);
408 mUiController.endActionMode();
409 }
410
411 /**
412 * Attach the sub window to the content view.
413 */
414 @Override
415 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800416 if (container.getParent() != null) {
417 // already attached, remove first
418 ((ViewGroup) container.getParent()).removeView(container);
419 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 mContentView.addView(container, COVER_SCREEN_PARAMS);
421 }
422
Michael Kolb11d19782011-03-20 10:17:40 -0700423 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700424 WebView web = getWebView();
425 if (web != null) {
426 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700427 }
428 }
429
Michael Kolb46f987e2011-04-05 10:39:10 -0700430 public void editUrl(boolean clearInput) {
431 if (mUiController.isInCustomActionMode()) {
432 mUiController.endActionMode();
433 }
434 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700435 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
John Reckef654f12011-07-12 16:42:08 -0700436 mNavigationBar.startEditingUrl(clearInput);
437 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700438 }
439
Michael Kolb7cdc4902011-02-03 17:54:40 -0800440 boolean canShowTitleBar() {
441 return !isTitleBarShowing()
442 && !isActivityPaused()
443 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700444 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800445 && !mUiController.isInCustomActionMode();
446 }
447
John Reck0f602f32011-07-07 15:38:43 -0700448 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700449 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700450 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700451 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700452 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800453 }
454
455 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700456 if (mTitleBar.isShowing()) {
457 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700458 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800459 }
460
461 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700462 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800463 }
464
John Reck5d43ce82011-06-21 17:16:51 -0700465 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700466 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700467 }
468
John Reck0f602f32011-07-07 15:38:43 -0700469 public TitleBar getTitleBar() {
470 return mTitleBar;
471 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800472
473 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700474 WebView web = getWebView();
475 if (web != null) {
476 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700477 }
478 }
479
Michael Kolb66706532010-12-12 19:50:22 -0800480 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700481 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700482 mNavigationBar.setInVoiceMode(true, results);
483 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 }
485
Michael Kolb66706532010-12-12 19:50:22 -0800486 @Override
487 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700488 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800489 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700490 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700491 }
492
493 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700494 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700495 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -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 intent.putExtra(ComboViewActivity.EXTRA_BOOKMARK_PAGE,
508 mUiController.createBookmarkCurrentPageIntent(false));
509 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 }
511
512 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400513 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700514 WebChromeClient.CustomViewCallback callback) {
515 // if a view already exists then immediately terminate the new one
516 if (mCustomView != null) {
517 callback.onCustomViewHidden();
518 return;
519 }
520
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400521 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb53ed62c2011-10-04 16:18:44 -0700522 WindowManager wm = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
523 WindowManager.LayoutParams params = new WindowManager.LayoutParams(
524 WindowManager.LayoutParams.TYPE_APPLICATION,
525 0);
526 params.x = 0;
527 params.y = 0;
528 params.width = LayoutParams.MATCH_PARENT;
529 params.height = LayoutParams.MATCH_PARENT;
530 params.systemUiVisibility = View.STATUS_BAR_HIDDEN;
531 mCustomView = new CustomViewHolder(mActivity);
532 view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
533 LayoutParams.MATCH_PARENT));
534 mCustomView.addView(view);
535 wm.addView(mCustomView, params);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400536
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 mCustomViewCallback = callback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400538 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 }
540
541 @Override
542 public void onHideCustomView() {
543 if (mCustomView == null)
544 return;
545
Michael Kolb53ed62c2011-10-04 16:18:44 -0700546 WindowManager wm = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
547 wm.removeView(mCustomView);
548
Michael Kolb8233fac2010-10-26 16:08:53 -0700549 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700550 mCustomViewCallback.onCustomViewHidden();
551 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400552 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700553 }
554
555 @Override
556 public boolean isCustomViewShowing() {
557 return mCustomView != null;
558 }
559
Michael Kolb66706532010-12-12 19:50:22 -0800560 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800561 if (mInputManager.isActive()) {
562 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
563 0);
564 }
565 }
566
Michael Kolb66706532010-12-12 19:50:22 -0800567 @Override
John Reck3ba45532011-08-11 16:26:53 -0700568 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700569 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800570 }
571
Patrick Scott92066772011-03-10 08:46:27 -0500572 @Override
573 public void showAutoLogin(Tab tab) {
574 updateAutoLogin(tab, true);
575 }
576
577 @Override
578 public void hideAutoLogin(Tab tab) {
579 updateAutoLogin(tab, true);
580 }
581
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 // -------------------------------------------------------------------------
583
Michael Kolb5a72f182011-01-13 20:35:06 -0800584 protected void updateNavigationState(Tab tab) {
585 }
586
Michael Kolb11d19782011-03-20 10:17:40 -0700587 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700588 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700589 }
Patrick Scott92066772011-03-10 08:46:27 -0500590
Michael Kolb8233fac2010-10-26 16:08:53 -0700591 /**
592 * Update the lock icon to correspond to our latest state.
593 */
Michael Kolb66706532010-12-12 19:50:22 -0800594 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800595 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100596 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700597 }
598 }
599
600 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 * Updates the lock-icon image in the title-bar.
602 */
Steve Block2466eff2011-10-03 15:33:09 +0100603 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100605 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
606 d = mLockIconSecure;
607 } else if (securityState == SecurityState.SECURITY_STATE_MIXED) {
608 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700609 }
John Reck0f602f32011-07-07 15:38:43 -0700610 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700611 }
612
John Reck30c714c2010-12-16 17:30:34 -0800613 protected void setUrlTitle(Tab tab) {
614 String url = tab.getUrl();
615 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800616 if (TextUtils.isEmpty(title)) {
617 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800618 }
Michael Kolb66706532010-12-12 19:50:22 -0800619 if (tab.isInVoiceSearchMode()) return;
620 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700621 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800622 }
623 }
624
625 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800626 protected void setFavicon(Tab tab) {
627 if (tab.inForeground()) {
628 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700629 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800630 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700631 }
632
633 @Override
634 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 }
636
Michael Kolb66706532010-12-12 19:50:22 -0800637 // active tabs page
638
639 public void showActiveTabsPage() {
640 }
641
642 /**
643 * Remove the active tabs page.
644 */
645 public void removeActiveTabsPage() {
646 }
647
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 // menu handling callbacks
649
650 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800651 public boolean onPrepareOptionsMenu(Menu menu) {
652 return true;
653 }
654
655 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700656 public void updateMenuState(Tab tab, Menu menu) {
657 }
658
659 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 }
662
663 @Override
664 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 }
666
667 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700668 public boolean onOptionsItemSelected(MenuItem item) {
669 return false;
670 }
671
672 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700674 }
675
676 @Override
677 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 }
679
680 @Override
681 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
683
684 @Override
685 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
688 // error console
689
690 @Override
691 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800692 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
694 if (flag) {
695 // Setting the show state of the console will cause it's the layout
696 // to be inflated.
697 if (errorConsole.numberOfErrors() > 0) {
698 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
699 } else {
700 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
701 }
702 if (errorConsole.getParent() != null) {
703 mErrorConsoleContainer.removeView(errorConsole);
704 }
705 // Now we can add it to the main view.
706 mErrorConsoleContainer.addView(errorConsole,
707 new LinearLayout.LayoutParams(
708 ViewGroup.LayoutParams.MATCH_PARENT,
709 ViewGroup.LayoutParams.WRAP_CONTENT));
710 } else {
711 mErrorConsoleContainer.removeView(errorConsole);
712 }
713 }
714
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 // -------------------------------------------------------------------------
716 // Helper function for WebChromeClient
717 // -------------------------------------------------------------------------
718
719 @Override
720 public Bitmap getDefaultVideoPoster() {
721 if (mDefaultVideoPoster == null) {
722 mDefaultVideoPoster = BitmapFactory.decodeResource(
723 mActivity.getResources(), R.drawable.default_video_poster);
724 }
725 return mDefaultVideoPoster;
726 }
727
728 @Override
729 public View getVideoLoadingProgressView() {
730 if (mVideoProgressView == null) {
731 LayoutInflater inflater = LayoutInflater.from(mActivity);
732 mVideoProgressView = inflater.inflate(
733 R.layout.video_loading_progress, null);
734 }
735 return mVideoProgressView;
736 }
737
Michael Kolb843510f2010-12-09 10:51:49 -0800738 @Override
739 public void showMaxTabsWarning() {
740 Toast warning = Toast.makeText(mActivity,
741 mActivity.getString(R.string.max_tabs_warning),
742 Toast.LENGTH_SHORT);
743 warning.show();
744 }
745
Michael Kolb46f987e2011-04-05 10:39:10 -0700746 protected WebView getWebView() {
747 if (mActiveTab != null) {
748 return mActiveTab.getWebView();
749 } else {
750 return null;
751 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700752 }
753
Michael Kolbfedb4922011-04-20 16:45:33 -0700754 protected Menu getMenu() {
755 MenuBuilder menu = new MenuBuilder(mActivity);
756 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
757 return menu;
758 }
759
Michael Kolbc38c6042011-04-27 10:46:06 -0700760 public void setFullscreen(boolean enabled) {
761 if (enabled) {
762 mActivity.getWindow().setFlags(
763 WindowManager.LayoutParams.FLAG_FULLSCREEN,
764 WindowManager.LayoutParams.FLAG_FULLSCREEN);
765 } else {
766 mActivity.getWindow().clearFlags(
767 WindowManager.LayoutParams.FLAG_FULLSCREEN);
768 }
769 }
770
John Reck0f602f32011-07-07 15:38:43 -0700771 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700772 Drawable[] array = new Drawable[3];
773 array[0] = new PaintDrawable(Color.BLACK);
774 PaintDrawable p = new PaintDrawable(Color.WHITE);
775 array[1] = p;
776 if (icon == null) {
777 array[2] = mGenericFavicon;
778 } else {
779 array[2] = new BitmapDrawable(icon);
780 }
781 LayerDrawable d = new LayerDrawable(array);
782 d.setLayerInset(1, 1, 1, 1, 1);
783 d.setLayerInset(2, 2, 2, 2, 2);
784 return d;
785 }
786
John Reck5d43ce82011-06-21 17:16:51 -0700787 public boolean isLoading() {
788 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
789 }
790
791 /**
792 * Suggest to the UI that the title bar can be hidden. The UI will then
793 * decide whether or not to hide based off a number of factors, such
794 * as if the user is editing the URL bar or if the page is loading
795 */
796 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700797 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
798 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700799 hideTitleBar();
800 }
801 }
802
John Reckbc6adb42011-09-01 18:03:20 -0700803 protected final void showTitleBarForDuration() {
804 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
805 }
806
807 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700808 showTitleBar();
809 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700810 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700811 }
812
John Reck5d43ce82011-06-21 17:16:51 -0700813 private Handler mHandler = new Handler() {
814
815 @Override
816 public void handleMessage(Message msg) {
817 if (msg.what == MSG_HIDE_TITLEBAR) {
818 suggestHideTitleBar();
819 }
820 }
821 };
John Reck3ba45532011-08-11 16:26:53 -0700822
823 @Override
824 public void showWeb(boolean animate) {
825 mUiController.hideCustomView();
826 }
827
Michael Kolb53ed62c2011-10-04 16:18:44 -0700828 // custom FrameLayout to forward key events
829 static class CustomViewHolder extends FrameLayout {
830 Activity mActivity;
831
832 public CustomViewHolder(Activity act) {
833 super(act);
834 mActivity = act;
835 }
836
837 public boolean dispatchKeyEvent(KeyEvent event) {
838 return mActivity.dispatchKeyEvent(event);
839 }
840 }
841
Michael Kolb8233fac2010-10-26 16:08:53 -0700842}