blob: 17ba4d74b440d7b4fe12a43fe329c62b08e81609 [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;
John Reckd3e4d5b2011-07-13 15:48:43 -070020import android.content.Intent;
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.content.res.Configuration;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
Michael Kolb5a4372f2011-04-29 13:53:10 -070025import android.graphics.Color;
26import android.graphics.drawable.BitmapDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.graphics.drawable.Drawable;
Michael Kolb5a4372f2011-04-29 13:53:10 -070028import android.graphics.drawable.LayerDrawable;
29import android.graphics.drawable.PaintDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.os.Bundle;
John Reck5d43ce82011-06-21 17:16:51 -070031import android.os.Handler;
32import android.os.Message;
Michael Kolb8233fac2010-10-26 16:08:53 -070033import android.text.TextUtils;
34import android.util.Log;
Michael Kolb8233fac2010-10-26 16:08:53 -070035import android.view.Gravity;
36import android.view.LayoutInflater;
37import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070038import android.view.MenuItem;
Michael Kolb8233fac2010-10-26 16:08:53 -070039import android.view.View;
Michael Kolb1514bb72010-11-22 09:11:48 -080040import android.view.View.OnClickListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080042import android.view.ViewGroup.LayoutParams;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.view.WindowManager;
Michael Kolb3a696282010-12-05 13:23:24 -080044import android.view.inputmethod.InputMethodManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070045import android.webkit.WebChromeClient;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.webkit.WebView;
47import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080048import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.widget.LinearLayout;
50import android.widget.Toast;
51
John Reckbf2ec202011-06-29 17:47:38 -070052import com.android.browser.Tab.LockIcon;
53import com.android.internal.view.menu.MenuBuilder;
54
Michael Kolb1bf23132010-11-19 12:55:12 -080055import java.util.List;
56
Michael Kolb8233fac2010-10-26 16:08:53 -070057/**
58 * UI interface definitions
59 */
John Reck718a24d2011-08-12 11:08:30 -070060public abstract class BaseUi implements UI {
Michael Kolb8233fac2010-10-26 16:08:53 -070061
62 private static final String LOGTAG = "BaseUi";
63
Michael Kolb66706532010-12-12 19:50:22 -080064 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070065 new FrameLayout.LayoutParams(
66 ViewGroup.LayoutParams.MATCH_PARENT,
67 ViewGroup.LayoutParams.MATCH_PARENT);
68
Michael Kolb66706532010-12-12 19:50:22 -080069 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070070 new FrameLayout.LayoutParams(
71 ViewGroup.LayoutParams.MATCH_PARENT,
72 ViewGroup.LayoutParams.MATCH_PARENT,
73 Gravity.CENTER);
74
John Reck5d43ce82011-06-21 17:16:51 -070075 private static final int MSG_HIDE_TITLEBAR = 1;
John Reckef654f12011-07-12 16:42:08 -070076 public static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
John Reck5d43ce82011-06-21 17:16:51 -070077
Michael Kolb8233fac2010-10-26 16:08:53 -070078 Activity mActivity;
79 UiController mUiController;
80 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080081 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080082 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
84 private Drawable mSecLockIcon;
85 private Drawable mMixLockIcon;
Michael Kolb5a4372f2011-04-29 13:53:10 -070086 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070087
Michael Kolb66706532010-12-12 19:50:22 -080088 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070089 protected FrameLayout mCustomViewContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070090
91 private View mCustomView;
92 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040093 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
Michael Kolb8233fac2010-10-26 16:08:53 -070095 private LinearLayout mErrorConsoleContainer = null;
96
John Reck718a24d2011-08-12 11:08:30 -070097 private UrlBarAutoShowManager mUrlBarAutoShowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070098
John Reck718a24d2011-08-12 11:08:30 -070099 private Toast mStopToast;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700100
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 // the default <video> poster
102 private Bitmap mDefaultVideoPoster;
103 // the video progress view
104 private View mVideoProgressView;
105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700107 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700108 protected TitleBar mTitleBar;
109 private NavigationBarBase mNavigationBar;
Michael Kolb8233fac2010-10-26 16:08:53 -0700110
111 public BaseUi(Activity browser, UiController controller) {
112 mActivity = browser;
113 mUiController = controller;
114 mTabControl = controller.getTabControl();
115 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800116 mInputManager = (InputMethodManager)
117 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Michael Kolb5a72f182011-01-13 20:35:06 -0800118 mSecLockIcon = res.getDrawable(R.drawable.ic_secure_holo_dark);
Michael Kolb6ced8442011-08-22 13:04:30 -0700119 mMixLockIcon = res.getDrawable(R.drawable.ic_secure_partial_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700120
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
122 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700123 LayoutInflater.from(mActivity)
124 .inflate(R.layout.custom_screen, frameLayout);
125 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 R.id.main_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700127 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 .findViewById(R.id.error_console);
John Reck7c6e1c92011-06-30 11:55:55 -0700129 mCustomViewContainer = (FrameLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 .findViewById(R.id.fullscreen_custom_content);
Michael Kolbc38c6042011-04-27 10:46:06 -0700131 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700132 mGenericFavicon = res.getDrawable(
133 R.drawable.app_web_browser_sm);
John Reck0f602f32011-07-07 15:38:43 -0700134 mTitleBar = new TitleBar(mActivity, mUiController, this,
135 mContentView);
136 mTitleBar.setProgress(100);
137 mNavigationBar = mTitleBar.getNavigationBar();
John Reck718a24d2011-08-12 11:08:30 -0700138 mUrlBarAutoShowManager = new UrlBarAutoShowManager(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700139 }
140
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 private void cancelStopToast() {
142 if (mStopToast != null) {
143 mStopToast.cancel();
144 mStopToast = null;
145 }
146 }
147
148 // lifecycle
149
150 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800151 if (isCustomViewShowing()) {
152 onHideCustomView();
153 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700154 cancelStopToast();
155 mActivityPaused = true;
156 }
157
158 public void onResume() {
159 mActivityPaused = false;
Michael Kolb2ae6ef72011-08-22 14:49:34 -0700160 // check if we exited without setting active tab
161 // b: 5188145
Michael Kolb1a4625a2011-08-24 13:40:44 -0700162 final Tab ct = mTabControl.getCurrentTab();
163 if (ct != null) {
164 setActiveTab(ct);
165 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 }
167
Michael Kolb66706532010-12-12 19:50:22 -0800168 protected boolean isActivityPaused() {
169 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 }
171
172 public void onConfigurationChanged(Configuration config) {
173 }
174
John Reck0f602f32011-07-07 15:38:43 -0700175 public Activity getActivity() {
176 return mActivity;
177 }
178
Michael Kolb8233fac2010-10-26 16:08:53 -0700179 // key handling
180
181 @Override
182 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 if (mCustomView != null) {
184 mUiController.hideCustomView();
185 return true;
186 }
187 return false;
188 }
189
Michael Kolb2814a362011-05-19 15:49:41 -0700190 @Override
191 public boolean onMenuKey() {
192 return false;
193 }
194
John Reck30c714c2010-12-16 17:30:34 -0800195 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 @Override
John Reck30c714c2010-12-16 17:30:34 -0800197 public void onTabDataChanged(Tab tab) {
198 setUrlTitle(tab);
199 setFavicon(tab);
200 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800201 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700202 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700203 mNavigationBar.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 }
205
206 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500207 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800208 if (tab.inForeground()) {
209 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700210 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800211 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500212 }
213
214 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 public void onPageStopped(Tab tab) {
216 cancelStopToast();
217 if (tab.inForeground()) {
218 mStopToast = Toast
219 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
220 mStopToast.show();
221 }
222 }
223
224 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800225 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700226 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800227 }
228
229 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 }
232
233 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800234 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700235 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800236 if ((tab != mActiveTab) && (mActiveTab != null)) {
237 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700238 WebView web = mActiveTab.getWebView();
239 if (web != null) {
240 web.setOnTouchListener(null);
241 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 }
Michael Kolb77df4562010-11-19 14:49:34 -0800243 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700244 WebView web = mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700245 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700246 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700247 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800248 onTabDataChanged(tab);
249 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800250 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
John Reck0f602f32011-07-07 15:38:43 -0700251 mNavigationBar.setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500252 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700253 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700254 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700255 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700256 showTitleBarForDuration();
257 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 }
259
John Reck718a24d2011-08-12 11:08:30 -0700260 protected void updateUrlBarAutoShowManagerTarget() {
261 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
262 if (!mUseQuickControls && web instanceof BrowserWebView) {
263 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
264 } else {
265 mUrlBarAutoShowManager.setTarget(null);
266 }
267 }
268
Michael Kolbcfa3af52010-12-14 10:36:11 -0800269 Tab getActiveTab() {
270 return mActiveTab;
271 }
272
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800274 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800275 }
276
277 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800279 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800281 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 }
284
285 @Override
286 public void detachTab(Tab tab) {
287 removeTabFromContentView(tab);
288 }
289
290 @Override
291 public void attachTab(Tab tab) {
292 attachTabToContentView(tab);
293 }
294
Michael Kolb377ea312011-02-17 14:36:56 -0800295 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800296 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700297 return;
298 }
299 View container = tab.getViewContainer();
300 WebView mainView = tab.getWebView();
301 // Attach the WebView to the container and then attach the
302 // container to the content view.
303 FrameLayout wrapper =
304 (FrameLayout) container.findViewById(R.id.webview_wrapper);
305 ViewGroup parent = (ViewGroup) mainView.getParent();
306 if (parent != wrapper) {
307 if (parent != null) {
308 Log.w(LOGTAG, "mMainView already has a parent in"
309 + " attachTabToContentView!");
310 parent.removeView(mainView);
311 }
312 wrapper.addView(mainView);
313 } else {
314 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
315 + " attachTabToContentView!");
316 }
317 parent = (ViewGroup) container.getParent();
318 if (parent != mContentView) {
319 if (parent != null) {
320 Log.w(LOGTAG, "mContainer already has a parent in"
321 + " attachTabToContentView!");
322 parent.removeView(container);
323 }
324 mContentView.addView(container, COVER_SCREEN_PARAMS);
325 } else {
326 Log.w(LOGTAG, "mContainer is already attached to content in"
327 + " attachTabToContentView!");
328 }
329 mUiController.attachSubWindow(tab);
330 }
331
332 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800333 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 // Remove the container that contains the main WebView.
335 WebView mainView = tab.getWebView();
336 View container = tab.getViewContainer();
337 if (mainView == null) {
338 return;
339 }
340 // Remove the container from the content and then remove the
341 // WebView from the container. This will trigger a focus change
342 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800343 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 FrameLayout wrapper =
345 (FrameLayout) container.findViewById(R.id.webview_wrapper);
346 wrapper.removeView(mainView);
347 mContentView.removeView(container);
348 mUiController.endActionMode();
349 mUiController.removeSubWindow(tab);
350 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
351 if (errorConsole != null) {
352 mErrorConsoleContainer.removeView(errorConsole);
353 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 }
355
Michael Kolba713ec82010-11-29 17:27:06 -0800356 @Override
357 public void onSetWebView(Tab tab, WebView webView) {
358 View container = tab.getViewContainer();
359 if (container == null) {
360 // The tab consists of a container view, which contains the main
361 // WebView, as well as any other UI elements associated with the tab.
362 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700363 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800364 tab.setViewContainer(container);
365 }
366 if (tab.getWebView() != webView) {
367 // Just remove the old one.
368 FrameLayout wrapper =
369 (FrameLayout) container.findViewById(R.id.webview_wrapper);
370 wrapper.removeView(tab.getWebView());
371 }
372 }
373
Michael Kolb8233fac2010-10-26 16:08:53 -0700374 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800375 * create a sub window container and webview for the tab
376 * Note: this methods operates through side-effects for now
377 * it sets both the subView and subViewContainer for the given tab
378 * @param tab tab to create the sub window for
379 * @param subView webview to be set as a subwindow for the tab
380 */
381 @Override
382 public void createSubWindow(Tab tab, WebView subView) {
383 View subViewContainer = mActivity.getLayoutInflater().inflate(
384 R.layout.browser_subwindow, null);
385 ViewGroup inner = (ViewGroup) subViewContainer
386 .findViewById(R.id.inner_container);
387 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
388 LayoutParams.MATCH_PARENT));
389 final ImageButton cancel = (ImageButton) subViewContainer
390 .findViewById(R.id.subwindow_close);
391 final WebView cancelSubView = subView;
392 cancel.setOnClickListener(new OnClickListener() {
393 public void onClick(View v) {
394 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
395 }
396 });
397 tab.setSubWebView(subView);
398 tab.setSubViewContainer(subViewContainer);
399 }
400
401 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 * Remove the sub window from the content view.
403 */
404 @Override
405 public void removeSubWindow(View subviewContainer) {
406 mContentView.removeView(subviewContainer);
407 mUiController.endActionMode();
408 }
409
410 /**
411 * Attach the sub window to the content view.
412 */
413 @Override
414 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800415 if (container.getParent() != null) {
416 // already attached, remove first
417 ((ViewGroup) container.getParent()).removeView(container);
418 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700419 mContentView.addView(container, COVER_SCREEN_PARAMS);
420 }
421
Michael Kolb11d19782011-03-20 10:17:40 -0700422 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700423 WebView web = getWebView();
424 if (web != null) {
425 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700426 }
427 }
428
Michael Kolb46f987e2011-04-05 10:39:10 -0700429 public void editUrl(boolean clearInput) {
430 if (mUiController.isInCustomActionMode()) {
431 mUiController.endActionMode();
432 }
433 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700434 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
John Reckef654f12011-07-12 16:42:08 -0700435 mNavigationBar.startEditingUrl(clearInput);
436 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700437 }
438
Michael Kolb7cdc4902011-02-03 17:54:40 -0800439 boolean canShowTitleBar() {
440 return !isTitleBarShowing()
441 && !isActivityPaused()
442 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700443 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800444 && !mUiController.isInCustomActionMode();
445 }
446
John Reck0f602f32011-07-07 15:38:43 -0700447 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700448 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700449 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700450 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700451 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800452 }
453
454 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700455 if (mTitleBar.isShowing()) {
456 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700457 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800458 }
459
460 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700461 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800462 }
463
John Reck5d43ce82011-06-21 17:16:51 -0700464 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700465 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700466 }
467
John Reck0f602f32011-07-07 15:38:43 -0700468 public TitleBar getTitleBar() {
469 return mTitleBar;
470 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800471
472 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700473 WebView web = getWebView();
474 if (web != null) {
475 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700476 }
477 }
478
Michael Kolb66706532010-12-12 19:50:22 -0800479 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700480 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700481 mNavigationBar.setInVoiceMode(true, results);
482 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 }
484
Michael Kolb66706532010-12-12 19:50:22 -0800485 @Override
486 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700487 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800488 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700489 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700490 }
491
492 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700493 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700494 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700495 }
496
497 @Override
John Reck2bc80422011-06-30 15:11:49 -0700498 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700499 Intent intent = new Intent(mActivity, ComboViewActivity.class);
500 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
501 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
502 Tab t = getActiveTab();
503 if (t != null) {
504 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800505 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700506 intent.putExtra(ComboViewActivity.EXTRA_BOOKMARK_PAGE,
507 mUiController.createBookmarkCurrentPageIntent(false));
508 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();
521
Michael Kolb8233fac2010-10-26 16:08:53 -0700522 // Add the custom view to its container.
523 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
524 mCustomView = view;
525 mCustomViewCallback = callback;
526 // Hide the content view.
527 mContentView.setVisibility(View.GONE);
528 // Finally show the custom view container.
529 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400530 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 mCustomViewContainer.setVisibility(View.VISIBLE);
532 mCustomViewContainer.bringToFront();
533 }
534
535 @Override
536 public void onHideCustomView() {
537 if (mCustomView == null)
538 return;
539
540 // Hide the custom view.
541 mCustomView.setVisibility(View.GONE);
542 // Remove the custom view from its container.
543 mCustomViewContainer.removeView(mCustomView);
544 mCustomView = null;
545 mCustomViewContainer.setVisibility(View.GONE);
546 mCustomViewCallback.onCustomViewHidden();
547 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400548 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700549 setStatusBarVisibility(true);
550 mContentView.setVisibility(View.VISIBLE);
551 }
552
553 @Override
554 public boolean isCustomViewShowing() {
555 return mCustomView != null;
556 }
557
Michael Kolb66706532010-12-12 19:50:22 -0800558 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800559 if (mInputManager.isActive()) {
560 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
561 0);
562 }
563 }
564
Michael Kolb66706532010-12-12 19:50:22 -0800565 @Override
John Reck3ba45532011-08-11 16:26:53 -0700566 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700567 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800568 }
569
Patrick Scott92066772011-03-10 08:46:27 -0500570 @Override
571 public void showAutoLogin(Tab tab) {
572 updateAutoLogin(tab, true);
573 }
574
575 @Override
576 public void hideAutoLogin(Tab tab) {
577 updateAutoLogin(tab, true);
578 }
579
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 // -------------------------------------------------------------------------
581
Michael Kolb5a72f182011-01-13 20:35:06 -0800582 protected void updateNavigationState(Tab tab) {
583 }
584
Michael Kolb11d19782011-03-20 10:17:40 -0700585 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700586 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700587 }
Patrick Scott92066772011-03-10 08:46:27 -0500588
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 /**
590 * Update the lock icon to correspond to our latest state.
591 */
Michael Kolb66706532010-12-12 19:50:22 -0800592 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800593 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 updateLockIconImage(t.getLockIconType());
595 }
596 }
597
598 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 * Updates the lock-icon image in the title-bar.
600 */
John Reck30c714c2010-12-16 17:30:34 -0800601 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700602 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800603 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800605 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700606 d = mMixLockIcon;
607 }
John Reck0f602f32011-07-07 15:38:43 -0700608 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700609 }
610
John Reck30c714c2010-12-16 17:30:34 -0800611 protected void setUrlTitle(Tab tab) {
612 String url = tab.getUrl();
613 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800614 if (TextUtils.isEmpty(title)) {
615 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800616 }
Michael Kolb66706532010-12-12 19:50:22 -0800617 if (tab.isInVoiceSearchMode()) return;
618 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700619 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800620 }
621 }
622
623 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800624 protected void setFavicon(Tab tab) {
625 if (tab.inForeground()) {
626 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700627 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800628 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 }
630
631 @Override
632 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 }
634
Michael Kolb66706532010-12-12 19:50:22 -0800635 // active tabs page
636
637 public void showActiveTabsPage() {
638 }
639
640 /**
641 * Remove the active tabs page.
642 */
643 public void removeActiveTabsPage() {
644 }
645
Michael Kolb8233fac2010-10-26 16:08:53 -0700646 // menu handling callbacks
647
648 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800649 public boolean onPrepareOptionsMenu(Menu menu) {
650 return true;
651 }
652
653 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700654 public void updateMenuState(Tab tab, Menu menu) {
655 }
656
657 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700659 }
660
661 @Override
662 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 }
664
665 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700666 public boolean onOptionsItemSelected(MenuItem item) {
667 return false;
668 }
669
670 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 }
673
674 @Override
675 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 }
677
678 @Override
679 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 }
681
682 @Override
683 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 }
685
686 // error console
687
688 @Override
689 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800690 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
692 if (flag) {
693 // Setting the show state of the console will cause it's the layout
694 // to be inflated.
695 if (errorConsole.numberOfErrors() > 0) {
696 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
697 } else {
698 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
699 }
700 if (errorConsole.getParent() != null) {
701 mErrorConsoleContainer.removeView(errorConsole);
702 }
703 // Now we can add it to the main view.
704 mErrorConsoleContainer.addView(errorConsole,
705 new LinearLayout.LayoutParams(
706 ViewGroup.LayoutParams.MATCH_PARENT,
707 ViewGroup.LayoutParams.WRAP_CONTENT));
708 } else {
709 mErrorConsoleContainer.removeView(errorConsole);
710 }
711 }
712
713 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800714 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
715 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
716 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 }
718
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 // -------------------------------------------------------------------------
720 // Helper function for WebChromeClient
721 // -------------------------------------------------------------------------
722
723 @Override
724 public Bitmap getDefaultVideoPoster() {
725 if (mDefaultVideoPoster == null) {
726 mDefaultVideoPoster = BitmapFactory.decodeResource(
727 mActivity.getResources(), R.drawable.default_video_poster);
728 }
729 return mDefaultVideoPoster;
730 }
731
732 @Override
733 public View getVideoLoadingProgressView() {
734 if (mVideoProgressView == null) {
735 LayoutInflater inflater = LayoutInflater.from(mActivity);
736 mVideoProgressView = inflater.inflate(
737 R.layout.video_loading_progress, null);
738 }
739 return mVideoProgressView;
740 }
741
Michael Kolb843510f2010-12-09 10:51:49 -0800742 @Override
743 public void showMaxTabsWarning() {
744 Toast warning = Toast.makeText(mActivity,
745 mActivity.getString(R.string.max_tabs_warning),
746 Toast.LENGTH_SHORT);
747 warning.show();
748 }
749
Michael Kolb46f987e2011-04-05 10:39:10 -0700750 protected WebView getWebView() {
751 if (mActiveTab != null) {
752 return mActiveTab.getWebView();
753 } else {
754 return null;
755 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700756 }
757
Michael Kolbfedb4922011-04-20 16:45:33 -0700758 protected Menu getMenu() {
759 MenuBuilder menu = new MenuBuilder(mActivity);
760 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
761 return menu;
762 }
763
Michael Kolbc38c6042011-04-27 10:46:06 -0700764 public void setFullscreen(boolean enabled) {
765 if (enabled) {
766 mActivity.getWindow().setFlags(
767 WindowManager.LayoutParams.FLAG_FULLSCREEN,
768 WindowManager.LayoutParams.FLAG_FULLSCREEN);
769 } else {
770 mActivity.getWindow().clearFlags(
771 WindowManager.LayoutParams.FLAG_FULLSCREEN);
772 }
773 }
774
John Reck0f602f32011-07-07 15:38:43 -0700775 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700776 Drawable[] array = new Drawable[3];
777 array[0] = new PaintDrawable(Color.BLACK);
778 PaintDrawable p = new PaintDrawable(Color.WHITE);
779 array[1] = p;
780 if (icon == null) {
781 array[2] = mGenericFavicon;
782 } else {
783 array[2] = new BitmapDrawable(icon);
784 }
785 LayerDrawable d = new LayerDrawable(array);
786 d.setLayerInset(1, 1, 1, 1, 1);
787 d.setLayerInset(2, 2, 2, 2, 2);
788 return d;
789 }
790
John Reck5d43ce82011-06-21 17:16:51 -0700791 public boolean isLoading() {
792 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
793 }
794
795 /**
796 * Suggest to the UI that the title bar can be hidden. The UI will then
797 * decide whether or not to hide based off a number of factors, such
798 * as if the user is editing the URL bar or if the page is loading
799 */
800 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700801 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
802 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700803 hideTitleBar();
804 }
805 }
806
John Reckbc6adb42011-09-01 18:03:20 -0700807 protected final void showTitleBarForDuration() {
808 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
809 }
810
811 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700812 showTitleBar();
813 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700814 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700815 }
816
John Reck5d43ce82011-06-21 17:16:51 -0700817 private Handler mHandler = new Handler() {
818
819 @Override
820 public void handleMessage(Message msg) {
821 if (msg.what == MSG_HIDE_TITLEBAR) {
822 suggestHideTitleBar();
823 }
824 }
825 };
John Reck3ba45532011-08-11 16:26:53 -0700826
827 @Override
828 public void showWeb(boolean animate) {
829 mUiController.hideCustomView();
830 }
831
Michael Kolb8233fac2010-10-26 16:08:53 -0700832}