blob: 80f4de8ff0373140a85a4f22a24b44399c6e8825 [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 Kolb8233fac2010-10-26 16:08:53 -0700119 mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure);
120
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
162 setActiveTab(mTabControl.getCurrentTab());
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 }
164
Michael Kolb66706532010-12-12 19:50:22 -0800165 protected boolean isActivityPaused() {
166 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 }
168
169 public void onConfigurationChanged(Configuration config) {
170 }
171
John Reck0f602f32011-07-07 15:38:43 -0700172 public Activity getActivity() {
173 return mActivity;
174 }
175
Michael Kolb8233fac2010-10-26 16:08:53 -0700176 // key handling
177
178 @Override
179 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700180 if (mCustomView != null) {
181 mUiController.hideCustomView();
182 return true;
183 }
184 return false;
185 }
186
Michael Kolb2814a362011-05-19 15:49:41 -0700187 @Override
188 public boolean onMenuKey() {
189 return false;
190 }
191
John Reck30c714c2010-12-16 17:30:34 -0800192 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 @Override
John Reck30c714c2010-12-16 17:30:34 -0800194 public void onTabDataChanged(Tab tab) {
195 setUrlTitle(tab);
196 setFavicon(tab);
197 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800198 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700199 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700200 mNavigationBar.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 }
202
203 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500204 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800205 if (tab.inForeground()) {
206 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700207 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800208 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500209 }
210
211 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 public void onPageStopped(Tab tab) {
213 cancelStopToast();
214 if (tab.inForeground()) {
215 mStopToast = Toast
216 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
217 mStopToast.show();
218 }
219 }
220
221 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800222 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700223 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800224 }
225
226 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700228 }
229
230 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800231 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700232 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800233 if ((tab != mActiveTab) && (mActiveTab != null)) {
234 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700235 WebView web = mActiveTab.getWebView();
236 if (web != null) {
237 web.setOnTouchListener(null);
238 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700239 }
Michael Kolb77df4562010-11-19 14:49:34 -0800240 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700241 WebView web = mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700242 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700243 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700244 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800245 onTabDataChanged(tab);
246 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800247 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
John Reck0f602f32011-07-07 15:38:43 -0700248 mNavigationBar.setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500249 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700250 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700251 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700252 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700253 showTitleBarForDuration();
254 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700255 }
256
John Reck718a24d2011-08-12 11:08:30 -0700257 protected void updateUrlBarAutoShowManagerTarget() {
258 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
259 if (!mUseQuickControls && web instanceof BrowserWebView) {
260 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
261 } else {
262 mUrlBarAutoShowManager.setTarget(null);
263 }
264 }
265
Michael Kolbcfa3af52010-12-14 10:36:11 -0800266 Tab getActiveTab() {
267 return mActiveTab;
268 }
269
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800271 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800272 }
273
274 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800276 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800278 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 }
281
282 @Override
283 public void detachTab(Tab tab) {
284 removeTabFromContentView(tab);
285 }
286
287 @Override
288 public void attachTab(Tab tab) {
289 attachTabToContentView(tab);
290 }
291
Michael Kolb377ea312011-02-17 14:36:56 -0800292 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800293 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700294 return;
295 }
296 View container = tab.getViewContainer();
297 WebView mainView = tab.getWebView();
298 // Attach the WebView to the container and then attach the
299 // container to the content view.
300 FrameLayout wrapper =
301 (FrameLayout) container.findViewById(R.id.webview_wrapper);
302 ViewGroup parent = (ViewGroup) mainView.getParent();
303 if (parent != wrapper) {
304 if (parent != null) {
305 Log.w(LOGTAG, "mMainView already has a parent in"
306 + " attachTabToContentView!");
307 parent.removeView(mainView);
308 }
309 wrapper.addView(mainView);
310 } else {
311 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
312 + " attachTabToContentView!");
313 }
314 parent = (ViewGroup) container.getParent();
315 if (parent != mContentView) {
316 if (parent != null) {
317 Log.w(LOGTAG, "mContainer already has a parent in"
318 + " attachTabToContentView!");
319 parent.removeView(container);
320 }
321 mContentView.addView(container, COVER_SCREEN_PARAMS);
322 } else {
323 Log.w(LOGTAG, "mContainer is already attached to content in"
324 + " attachTabToContentView!");
325 }
326 mUiController.attachSubWindow(tab);
327 }
328
329 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800330 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700331 // Remove the container that contains the main WebView.
332 WebView mainView = tab.getWebView();
333 View container = tab.getViewContainer();
334 if (mainView == null) {
335 return;
336 }
337 // Remove the container from the content and then remove the
338 // WebView from the container. This will trigger a focus change
339 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800340 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 FrameLayout wrapper =
342 (FrameLayout) container.findViewById(R.id.webview_wrapper);
343 wrapper.removeView(mainView);
344 mContentView.removeView(container);
345 mUiController.endActionMode();
346 mUiController.removeSubWindow(tab);
347 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
348 if (errorConsole != null) {
349 mErrorConsoleContainer.removeView(errorConsole);
350 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 }
352
Michael Kolba713ec82010-11-29 17:27:06 -0800353 @Override
354 public void onSetWebView(Tab tab, WebView webView) {
355 View container = tab.getViewContainer();
356 if (container == null) {
357 // The tab consists of a container view, which contains the main
358 // WebView, as well as any other UI elements associated with the tab.
359 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700360 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800361 tab.setViewContainer(container);
362 }
363 if (tab.getWebView() != webView) {
364 // Just remove the old one.
365 FrameLayout wrapper =
366 (FrameLayout) container.findViewById(R.id.webview_wrapper);
367 wrapper.removeView(tab.getWebView());
368 }
369 }
370
Michael Kolb8233fac2010-10-26 16:08:53 -0700371 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800372 * create a sub window container and webview for the tab
373 * Note: this methods operates through side-effects for now
374 * it sets both the subView and subViewContainer for the given tab
375 * @param tab tab to create the sub window for
376 * @param subView webview to be set as a subwindow for the tab
377 */
378 @Override
379 public void createSubWindow(Tab tab, WebView subView) {
380 View subViewContainer = mActivity.getLayoutInflater().inflate(
381 R.layout.browser_subwindow, null);
382 ViewGroup inner = (ViewGroup) subViewContainer
383 .findViewById(R.id.inner_container);
384 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
385 LayoutParams.MATCH_PARENT));
386 final ImageButton cancel = (ImageButton) subViewContainer
387 .findViewById(R.id.subwindow_close);
388 final WebView cancelSubView = subView;
389 cancel.setOnClickListener(new OnClickListener() {
390 public void onClick(View v) {
391 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
392 }
393 });
394 tab.setSubWebView(subView);
395 tab.setSubViewContainer(subViewContainer);
396 }
397
398 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700399 * Remove the sub window from the content view.
400 */
401 @Override
402 public void removeSubWindow(View subviewContainer) {
403 mContentView.removeView(subviewContainer);
404 mUiController.endActionMode();
405 }
406
407 /**
408 * Attach the sub window to the content view.
409 */
410 @Override
411 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800412 if (container.getParent() != null) {
413 // already attached, remove first
414 ((ViewGroup) container.getParent()).removeView(container);
415 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700416 mContentView.addView(container, COVER_SCREEN_PARAMS);
417 }
418
Michael Kolb11d19782011-03-20 10:17:40 -0700419 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700420 WebView web = getWebView();
421 if (web != null) {
422 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700423 }
424 }
425
Michael Kolb46f987e2011-04-05 10:39:10 -0700426 public void editUrl(boolean clearInput) {
427 if (mUiController.isInCustomActionMode()) {
428 mUiController.endActionMode();
429 }
430 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700431 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
John Reckef654f12011-07-12 16:42:08 -0700432 mNavigationBar.startEditingUrl(clearInput);
433 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700434 }
435
Michael Kolb7cdc4902011-02-03 17:54:40 -0800436 boolean canShowTitleBar() {
437 return !isTitleBarShowing()
438 && !isActivityPaused()
439 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700440 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800441 && !mUiController.isInCustomActionMode();
442 }
443
John Reck0f602f32011-07-07 15:38:43 -0700444 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700445 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700446 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700447 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700448 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800449 }
450
451 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700452 if (mTitleBar.isShowing()) {
453 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700454 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800455 }
456
457 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700458 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800459 }
460
John Reck5d43ce82011-06-21 17:16:51 -0700461 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700462 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700463 }
464
John Reck0f602f32011-07-07 15:38:43 -0700465 public TitleBar getTitleBar() {
466 return mTitleBar;
467 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800468
469 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700470 WebView web = getWebView();
471 if (web != null) {
472 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700473 }
474 }
475
Michael Kolb66706532010-12-12 19:50:22 -0800476 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700477 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700478 mNavigationBar.setInVoiceMode(true, results);
479 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700480 }
481
Michael Kolb66706532010-12-12 19:50:22 -0800482 @Override
483 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700484 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800485 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700486 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 }
488
489 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700490 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700491 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700492 }
493
494 @Override
John Reck2bc80422011-06-30 15:11:49 -0700495 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700496 Intent intent = new Intent(mActivity, ComboViewActivity.class);
497 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
498 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
499 Tab t = getActiveTab();
500 if (t != null) {
501 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800502 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700503 intent.putExtra(ComboViewActivity.EXTRA_BOOKMARK_PAGE,
504 mUiController.createBookmarkCurrentPageIntent(false));
505 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 }
507
508 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400509 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 WebChromeClient.CustomViewCallback callback) {
511 // if a view already exists then immediately terminate the new one
512 if (mCustomView != null) {
513 callback.onCustomViewHidden();
514 return;
515 }
516
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400517 mOriginalOrientation = mActivity.getRequestedOrientation();
518
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 // Add the custom view to its container.
520 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
521 mCustomView = view;
522 mCustomViewCallback = callback;
523 // Hide the content view.
524 mContentView.setVisibility(View.GONE);
525 // Finally show the custom view container.
526 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400527 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 mCustomViewContainer.setVisibility(View.VISIBLE);
529 mCustomViewContainer.bringToFront();
530 }
531
532 @Override
533 public void onHideCustomView() {
534 if (mCustomView == null)
535 return;
536
537 // Hide the custom view.
538 mCustomView.setVisibility(View.GONE);
539 // Remove the custom view from its container.
540 mCustomViewContainer.removeView(mCustomView);
541 mCustomView = null;
542 mCustomViewContainer.setVisibility(View.GONE);
543 mCustomViewCallback.onCustomViewHidden();
544 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400545 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 setStatusBarVisibility(true);
547 mContentView.setVisibility(View.VISIBLE);
548 }
549
550 @Override
551 public boolean isCustomViewShowing() {
552 return mCustomView != null;
553 }
554
Michael Kolb66706532010-12-12 19:50:22 -0800555 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800556 if (mInputManager.isActive()) {
557 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
558 0);
559 }
560 }
561
Michael Kolb66706532010-12-12 19:50:22 -0800562 @Override
John Reck3ba45532011-08-11 16:26:53 -0700563 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700564 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800565 }
566
Patrick Scott92066772011-03-10 08:46:27 -0500567 @Override
568 public void showAutoLogin(Tab tab) {
569 updateAutoLogin(tab, true);
570 }
571
572 @Override
573 public void hideAutoLogin(Tab tab) {
574 updateAutoLogin(tab, true);
575 }
576
Michael Kolb8233fac2010-10-26 16:08:53 -0700577 // -------------------------------------------------------------------------
578
Michael Kolb5a72f182011-01-13 20:35:06 -0800579 protected void updateNavigationState(Tab tab) {
580 }
581
Michael Kolb11d19782011-03-20 10:17:40 -0700582 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700583 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700584 }
Patrick Scott92066772011-03-10 08:46:27 -0500585
Michael Kolb8233fac2010-10-26 16:08:53 -0700586 /**
587 * Update the lock icon to correspond to our latest state.
588 */
Michael Kolb66706532010-12-12 19:50:22 -0800589 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800590 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700591 updateLockIconImage(t.getLockIconType());
592 }
593 }
594
595 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700596 * Updates the lock-icon image in the title-bar.
597 */
John Reck30c714c2010-12-16 17:30:34 -0800598 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800600 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800602 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 d = mMixLockIcon;
604 }
John Reck0f602f32011-07-07 15:38:43 -0700605 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700606 }
607
John Reck30c714c2010-12-16 17:30:34 -0800608 protected void setUrlTitle(Tab tab) {
609 String url = tab.getUrl();
610 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800611 if (TextUtils.isEmpty(title)) {
612 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800613 }
Michael Kolb66706532010-12-12 19:50:22 -0800614 if (tab.isInVoiceSearchMode()) return;
615 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700616 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800617 }
618 }
619
620 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800621 protected void setFavicon(Tab tab) {
622 if (tab.inForeground()) {
623 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700624 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800625 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 }
627
628 @Override
629 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 }
631
Michael Kolb66706532010-12-12 19:50:22 -0800632 // active tabs page
633
634 public void showActiveTabsPage() {
635 }
636
637 /**
638 * Remove the active tabs page.
639 */
640 public void removeActiveTabsPage() {
641 }
642
Michael Kolb8233fac2010-10-26 16:08:53 -0700643 // menu handling callbacks
644
645 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800646 public boolean onPrepareOptionsMenu(Menu menu) {
647 return true;
648 }
649
650 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700651 public void updateMenuState(Tab tab, Menu menu) {
652 }
653
654 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 }
657
658 @Override
659 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 }
661
662 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700663 public boolean onOptionsItemSelected(MenuItem item) {
664 return false;
665 }
666
667 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 }
670
671 @Override
672 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 }
674
675 @Override
676 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 }
678
679 @Override
680 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700681 }
682
683 // error console
684
685 @Override
686 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800687 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
689 if (flag) {
690 // Setting the show state of the console will cause it's the layout
691 // to be inflated.
692 if (errorConsole.numberOfErrors() > 0) {
693 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
694 } else {
695 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
696 }
697 if (errorConsole.getParent() != null) {
698 mErrorConsoleContainer.removeView(errorConsole);
699 }
700 // Now we can add it to the main view.
701 mErrorConsoleContainer.addView(errorConsole,
702 new LinearLayout.LayoutParams(
703 ViewGroup.LayoutParams.MATCH_PARENT,
704 ViewGroup.LayoutParams.WRAP_CONTENT));
705 } else {
706 mErrorConsoleContainer.removeView(errorConsole);
707 }
708 }
709
710 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800711 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
712 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
713 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700714 }
715
Michael Kolb8233fac2010-10-26 16:08:53 -0700716 // -------------------------------------------------------------------------
717 // Helper function for WebChromeClient
718 // -------------------------------------------------------------------------
719
720 @Override
721 public Bitmap getDefaultVideoPoster() {
722 if (mDefaultVideoPoster == null) {
723 mDefaultVideoPoster = BitmapFactory.decodeResource(
724 mActivity.getResources(), R.drawable.default_video_poster);
725 }
726 return mDefaultVideoPoster;
727 }
728
729 @Override
730 public View getVideoLoadingProgressView() {
731 if (mVideoProgressView == null) {
732 LayoutInflater inflater = LayoutInflater.from(mActivity);
733 mVideoProgressView = inflater.inflate(
734 R.layout.video_loading_progress, null);
735 }
736 return mVideoProgressView;
737 }
738
Michael Kolb843510f2010-12-09 10:51:49 -0800739 @Override
740 public void showMaxTabsWarning() {
741 Toast warning = Toast.makeText(mActivity,
742 mActivity.getString(R.string.max_tabs_warning),
743 Toast.LENGTH_SHORT);
744 warning.show();
745 }
746
Michael Kolb46f987e2011-04-05 10:39:10 -0700747 protected WebView getWebView() {
748 if (mActiveTab != null) {
749 return mActiveTab.getWebView();
750 } else {
751 return null;
752 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700753 }
754
Michael Kolbfedb4922011-04-20 16:45:33 -0700755 protected Menu getMenu() {
756 MenuBuilder menu = new MenuBuilder(mActivity);
757 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
758 return menu;
759 }
760
Michael Kolbc38c6042011-04-27 10:46:06 -0700761 public void setFullscreen(boolean enabled) {
762 if (enabled) {
763 mActivity.getWindow().setFlags(
764 WindowManager.LayoutParams.FLAG_FULLSCREEN,
765 WindowManager.LayoutParams.FLAG_FULLSCREEN);
766 } else {
767 mActivity.getWindow().clearFlags(
768 WindowManager.LayoutParams.FLAG_FULLSCREEN);
769 }
770 }
771
John Reck0f602f32011-07-07 15:38:43 -0700772 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700773 Drawable[] array = new Drawable[3];
774 array[0] = new PaintDrawable(Color.BLACK);
775 PaintDrawable p = new PaintDrawable(Color.WHITE);
776 array[1] = p;
777 if (icon == null) {
778 array[2] = mGenericFavicon;
779 } else {
780 array[2] = new BitmapDrawable(icon);
781 }
782 LayerDrawable d = new LayerDrawable(array);
783 d.setLayerInset(1, 1, 1, 1, 1);
784 d.setLayerInset(2, 2, 2, 2, 2);
785 return d;
786 }
787
John Reck5d43ce82011-06-21 17:16:51 -0700788 public boolean isLoading() {
789 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
790 }
791
792 /**
793 * Suggest to the UI that the title bar can be hidden. The UI will then
794 * decide whether or not to hide based off a number of factors, such
795 * as if the user is editing the URL bar or if the page is loading
796 */
797 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700798 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
799 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700800 hideTitleBar();
801 }
802 }
803
Michael Kolb017ffab2011-07-11 15:26:47 -0700804 protected void showTitleBarForDuration() {
John Reck6ac5bfd2011-07-01 17:02:55 -0700805 showTitleBar();
806 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
807 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
808 }
809
John Reck5d43ce82011-06-21 17:16:51 -0700810 private Handler mHandler = new Handler() {
811
812 @Override
813 public void handleMessage(Message msg) {
814 if (msg.what == MSG_HIDE_TITLEBAR) {
815 suggestHideTitleBar();
816 }
817 }
818 };
John Reck3ba45532011-08-11 16:26:53 -0700819
820 @Override
821 public void showWeb(boolean animate) {
822 mUiController.hideCustomView();
823 }
824
Michael Kolb8233fac2010-10-26 16:08:53 -0700825}