blob: bce3257ed602e07bc12c0b8fe56592356174d6f1 [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);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700250 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
Patrick Scott92066772011-03-10 08:46:27 -0500251 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700252 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700253 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700254 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700255 showTitleBarForDuration();
256 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 }
258
John Reck718a24d2011-08-12 11:08:30 -0700259 protected void updateUrlBarAutoShowManagerTarget() {
260 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
261 if (!mUseQuickControls && web instanceof BrowserWebView) {
262 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
263 } else {
264 mUrlBarAutoShowManager.setTarget(null);
265 }
266 }
267
Michael Kolbcfa3af52010-12-14 10:36:11 -0800268 Tab getActiveTab() {
269 return mActiveTab;
270 }
271
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800273 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800274 }
275
276 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800278 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800280 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 }
283
284 @Override
285 public void detachTab(Tab tab) {
286 removeTabFromContentView(tab);
287 }
288
289 @Override
290 public void attachTab(Tab tab) {
291 attachTabToContentView(tab);
292 }
293
Michael Kolb377ea312011-02-17 14:36:56 -0800294 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800295 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 return;
297 }
298 View container = tab.getViewContainer();
299 WebView mainView = tab.getWebView();
300 // Attach the WebView to the container and then attach the
301 // container to the content view.
302 FrameLayout wrapper =
303 (FrameLayout) container.findViewById(R.id.webview_wrapper);
304 ViewGroup parent = (ViewGroup) mainView.getParent();
305 if (parent != wrapper) {
306 if (parent != null) {
307 Log.w(LOGTAG, "mMainView already has a parent in"
308 + " attachTabToContentView!");
309 parent.removeView(mainView);
310 }
311 wrapper.addView(mainView);
312 } else {
313 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
314 + " attachTabToContentView!");
315 }
316 parent = (ViewGroup) container.getParent();
317 if (parent != mContentView) {
318 if (parent != null) {
319 Log.w(LOGTAG, "mContainer already has a parent in"
320 + " attachTabToContentView!");
321 parent.removeView(container);
322 }
323 mContentView.addView(container, COVER_SCREEN_PARAMS);
324 } else {
325 Log.w(LOGTAG, "mContainer is already attached to content in"
326 + " attachTabToContentView!");
327 }
328 mUiController.attachSubWindow(tab);
329 }
330
331 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800332 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700333 // Remove the container that contains the main WebView.
334 WebView mainView = tab.getWebView();
335 View container = tab.getViewContainer();
336 if (mainView == null) {
337 return;
338 }
339 // Remove the container from the content and then remove the
340 // WebView from the container. This will trigger a focus change
341 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800342 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 FrameLayout wrapper =
344 (FrameLayout) container.findViewById(R.id.webview_wrapper);
345 wrapper.removeView(mainView);
346 mContentView.removeView(container);
347 mUiController.endActionMode();
348 mUiController.removeSubWindow(tab);
349 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
350 if (errorConsole != null) {
351 mErrorConsoleContainer.removeView(errorConsole);
352 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700353 }
354
Michael Kolba713ec82010-11-29 17:27:06 -0800355 @Override
356 public void onSetWebView(Tab tab, WebView webView) {
357 View container = tab.getViewContainer();
358 if (container == null) {
359 // The tab consists of a container view, which contains the main
360 // WebView, as well as any other UI elements associated with the tab.
361 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700362 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800363 tab.setViewContainer(container);
364 }
365 if (tab.getWebView() != webView) {
366 // Just remove the old one.
367 FrameLayout wrapper =
368 (FrameLayout) container.findViewById(R.id.webview_wrapper);
369 wrapper.removeView(tab.getWebView());
370 }
371 }
372
Michael Kolb8233fac2010-10-26 16:08:53 -0700373 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800374 * create a sub window container and webview for the tab
375 * Note: this methods operates through side-effects for now
376 * it sets both the subView and subViewContainer for the given tab
377 * @param tab tab to create the sub window for
378 * @param subView webview to be set as a subwindow for the tab
379 */
380 @Override
381 public void createSubWindow(Tab tab, WebView subView) {
382 View subViewContainer = mActivity.getLayoutInflater().inflate(
383 R.layout.browser_subwindow, null);
384 ViewGroup inner = (ViewGroup) subViewContainer
385 .findViewById(R.id.inner_container);
386 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
387 LayoutParams.MATCH_PARENT));
388 final ImageButton cancel = (ImageButton) subViewContainer
389 .findViewById(R.id.subwindow_close);
390 final WebView cancelSubView = subView;
391 cancel.setOnClickListener(new OnClickListener() {
392 public void onClick(View v) {
393 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
394 }
395 });
396 tab.setSubWebView(subView);
397 tab.setSubViewContainer(subViewContainer);
398 }
399
400 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 * Remove the sub window from the content view.
402 */
403 @Override
404 public void removeSubWindow(View subviewContainer) {
405 mContentView.removeView(subviewContainer);
406 mUiController.endActionMode();
407 }
408
409 /**
410 * Attach the sub window to the content view.
411 */
412 @Override
413 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800414 if (container.getParent() != null) {
415 // already attached, remove first
416 ((ViewGroup) container.getParent()).removeView(container);
417 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700418 mContentView.addView(container, COVER_SCREEN_PARAMS);
419 }
420
Michael Kolb11d19782011-03-20 10:17:40 -0700421 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700422 WebView web = getWebView();
423 if (web != null) {
424 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700425 }
426 }
427
Michael Kolb46f987e2011-04-05 10:39:10 -0700428 public void editUrl(boolean clearInput) {
429 if (mUiController.isInCustomActionMode()) {
430 mUiController.endActionMode();
431 }
432 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700433 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
John Reckef654f12011-07-12 16:42:08 -0700434 mNavigationBar.startEditingUrl(clearInput);
435 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700436 }
437
Michael Kolb7cdc4902011-02-03 17:54:40 -0800438 boolean canShowTitleBar() {
439 return !isTitleBarShowing()
440 && !isActivityPaused()
441 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700442 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800443 && !mUiController.isInCustomActionMode();
444 }
445
John Reck0f602f32011-07-07 15:38:43 -0700446 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700447 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700448 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700449 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700450 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800451 }
452
453 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700454 if (mTitleBar.isShowing()) {
455 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700456 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800457 }
458
459 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700460 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800461 }
462
John Reck5d43ce82011-06-21 17:16:51 -0700463 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700464 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700465 }
466
John Reck0f602f32011-07-07 15:38:43 -0700467 public TitleBar getTitleBar() {
468 return mTitleBar;
469 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800470
471 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700472 WebView web = getWebView();
473 if (web != null) {
474 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 }
476 }
477
Michael Kolb66706532010-12-12 19:50:22 -0800478 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700479 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700480 mNavigationBar.setInVoiceMode(true, results);
481 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700482 }
483
Michael Kolb66706532010-12-12 19:50:22 -0800484 @Override
485 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700486 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800487 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700488 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700489 }
490
491 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700492 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700493 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700494 }
495
496 @Override
John Reck2bc80422011-06-30 15:11:49 -0700497 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700498 Intent intent = new Intent(mActivity, ComboViewActivity.class);
499 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
500 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
501 Tab t = getActiveTab();
502 if (t != null) {
503 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800504 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700505 intent.putExtra(ComboViewActivity.EXTRA_BOOKMARK_PAGE,
506 mUiController.createBookmarkCurrentPageIntent(false));
507 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 }
509
510 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400511 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 WebChromeClient.CustomViewCallback callback) {
513 // if a view already exists then immediately terminate the new one
514 if (mCustomView != null) {
515 callback.onCustomViewHidden();
516 return;
517 }
518
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400519 mOriginalOrientation = mActivity.getRequestedOrientation();
520
Michael Kolb8233fac2010-10-26 16:08:53 -0700521 // Add the custom view to its container.
522 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
523 mCustomView = view;
524 mCustomViewCallback = callback;
525 // Hide the content view.
526 mContentView.setVisibility(View.GONE);
527 // Finally show the custom view container.
528 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400529 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 mCustomViewContainer.setVisibility(View.VISIBLE);
531 mCustomViewContainer.bringToFront();
532 }
533
534 @Override
535 public void onHideCustomView() {
536 if (mCustomView == null)
537 return;
538
539 // Hide the custom view.
540 mCustomView.setVisibility(View.GONE);
541 // Remove the custom view from its container.
542 mCustomViewContainer.removeView(mCustomView);
543 mCustomView = null;
544 mCustomViewContainer.setVisibility(View.GONE);
545 mCustomViewCallback.onCustomViewHidden();
546 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400547 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700548 setStatusBarVisibility(true);
549 mContentView.setVisibility(View.VISIBLE);
550 }
551
552 @Override
553 public boolean isCustomViewShowing() {
554 return mCustomView != null;
555 }
556
Michael Kolb66706532010-12-12 19:50:22 -0800557 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800558 if (mInputManager.isActive()) {
559 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
560 0);
561 }
562 }
563
Michael Kolb66706532010-12-12 19:50:22 -0800564 @Override
John Reck3ba45532011-08-11 16:26:53 -0700565 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700566 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800567 }
568
Patrick Scott92066772011-03-10 08:46:27 -0500569 @Override
570 public void showAutoLogin(Tab tab) {
571 updateAutoLogin(tab, true);
572 }
573
574 @Override
575 public void hideAutoLogin(Tab tab) {
576 updateAutoLogin(tab, true);
577 }
578
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 // -------------------------------------------------------------------------
580
Michael Kolb5a72f182011-01-13 20:35:06 -0800581 protected void updateNavigationState(Tab tab) {
582 }
583
Michael Kolb11d19782011-03-20 10:17:40 -0700584 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700585 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700586 }
Patrick Scott92066772011-03-10 08:46:27 -0500587
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 /**
589 * Update the lock icon to correspond to our latest state.
590 */
Michael Kolb66706532010-12-12 19:50:22 -0800591 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800592 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 updateLockIconImage(t.getLockIconType());
594 }
595 }
596
597 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 * Updates the lock-icon image in the title-bar.
599 */
John Reck30c714c2010-12-16 17:30:34 -0800600 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800602 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800604 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700605 d = mMixLockIcon;
606 }
John Reck0f602f32011-07-07 15:38:43 -0700607 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700608 }
609
John Reck30c714c2010-12-16 17:30:34 -0800610 protected void setUrlTitle(Tab tab) {
611 String url = tab.getUrl();
612 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800613 if (TextUtils.isEmpty(title)) {
614 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800615 }
Michael Kolb66706532010-12-12 19:50:22 -0800616 if (tab.isInVoiceSearchMode()) return;
617 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700618 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800619 }
620 }
621
622 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800623 protected void setFavicon(Tab tab) {
624 if (tab.inForeground()) {
625 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700626 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800627 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700628 }
629
630 @Override
631 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700632 }
633
Michael Kolb66706532010-12-12 19:50:22 -0800634 // active tabs page
635
636 public void showActiveTabsPage() {
637 }
638
639 /**
640 * Remove the active tabs page.
641 */
642 public void removeActiveTabsPage() {
643 }
644
Michael Kolb8233fac2010-10-26 16:08:53 -0700645 // menu handling callbacks
646
647 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800648 public boolean onPrepareOptionsMenu(Menu menu) {
649 return true;
650 }
651
652 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700653 public void updateMenuState(Tab tab, Menu menu) {
654 }
655
656 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700657 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 }
659
660 @Override
661 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700662 }
663
664 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700665 public boolean onOptionsItemSelected(MenuItem item) {
666 return false;
667 }
668
669 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 }
672
673 @Override
674 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 }
676
677 @Override
678 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 }
680
681 @Override
682 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 }
684
685 // error console
686
687 @Override
688 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800689 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
691 if (flag) {
692 // Setting the show state of the console will cause it's the layout
693 // to be inflated.
694 if (errorConsole.numberOfErrors() > 0) {
695 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
696 } else {
697 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
698 }
699 if (errorConsole.getParent() != null) {
700 mErrorConsoleContainer.removeView(errorConsole);
701 }
702 // Now we can add it to the main view.
703 mErrorConsoleContainer.addView(errorConsole,
704 new LinearLayout.LayoutParams(
705 ViewGroup.LayoutParams.MATCH_PARENT,
706 ViewGroup.LayoutParams.WRAP_CONTENT));
707 } else {
708 mErrorConsoleContainer.removeView(errorConsole);
709 }
710 }
711
712 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800713 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
714 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
715 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700716 }
717
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 // -------------------------------------------------------------------------
719 // Helper function for WebChromeClient
720 // -------------------------------------------------------------------------
721
722 @Override
723 public Bitmap getDefaultVideoPoster() {
724 if (mDefaultVideoPoster == null) {
725 mDefaultVideoPoster = BitmapFactory.decodeResource(
726 mActivity.getResources(), R.drawable.default_video_poster);
727 }
728 return mDefaultVideoPoster;
729 }
730
731 @Override
732 public View getVideoLoadingProgressView() {
733 if (mVideoProgressView == null) {
734 LayoutInflater inflater = LayoutInflater.from(mActivity);
735 mVideoProgressView = inflater.inflate(
736 R.layout.video_loading_progress, null);
737 }
738 return mVideoProgressView;
739 }
740
Michael Kolb843510f2010-12-09 10:51:49 -0800741 @Override
742 public void showMaxTabsWarning() {
743 Toast warning = Toast.makeText(mActivity,
744 mActivity.getString(R.string.max_tabs_warning),
745 Toast.LENGTH_SHORT);
746 warning.show();
747 }
748
Michael Kolb46f987e2011-04-05 10:39:10 -0700749 protected WebView getWebView() {
750 if (mActiveTab != null) {
751 return mActiveTab.getWebView();
752 } else {
753 return null;
754 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700755 }
756
Michael Kolbfedb4922011-04-20 16:45:33 -0700757 protected Menu getMenu() {
758 MenuBuilder menu = new MenuBuilder(mActivity);
759 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
760 return menu;
761 }
762
Michael Kolbc38c6042011-04-27 10:46:06 -0700763 public void setFullscreen(boolean enabled) {
764 if (enabled) {
765 mActivity.getWindow().setFlags(
766 WindowManager.LayoutParams.FLAG_FULLSCREEN,
767 WindowManager.LayoutParams.FLAG_FULLSCREEN);
768 } else {
769 mActivity.getWindow().clearFlags(
770 WindowManager.LayoutParams.FLAG_FULLSCREEN);
771 }
772 }
773
John Reck0f602f32011-07-07 15:38:43 -0700774 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700775 Drawable[] array = new Drawable[3];
776 array[0] = new PaintDrawable(Color.BLACK);
777 PaintDrawable p = new PaintDrawable(Color.WHITE);
778 array[1] = p;
779 if (icon == null) {
780 array[2] = mGenericFavicon;
781 } else {
782 array[2] = new BitmapDrawable(icon);
783 }
784 LayerDrawable d = new LayerDrawable(array);
785 d.setLayerInset(1, 1, 1, 1, 1);
786 d.setLayerInset(2, 2, 2, 2, 2);
787 return d;
788 }
789
John Reck5d43ce82011-06-21 17:16:51 -0700790 public boolean isLoading() {
791 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
792 }
793
794 /**
795 * Suggest to the UI that the title bar can be hidden. The UI will then
796 * decide whether or not to hide based off a number of factors, such
797 * as if the user is editing the URL bar or if the page is loading
798 */
799 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700800 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
801 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700802 hideTitleBar();
803 }
804 }
805
John Reckbc6adb42011-09-01 18:03:20 -0700806 protected final void showTitleBarForDuration() {
807 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
808 }
809
810 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700811 showTitleBar();
812 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700813 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700814 }
815
John Reck5d43ce82011-06-21 17:16:51 -0700816 private Handler mHandler = new Handler() {
817
818 @Override
819 public void handleMessage(Message msg) {
820 if (msg.what == MSG_HIDE_TITLEBAR) {
821 suggestHideTitleBar();
822 }
823 }
824 };
John Reck3ba45532011-08-11 16:26:53 -0700825
826 @Override
827 public void showWeb(boolean animate) {
828 mUiController.hideCustomView();
829 }
830
Michael Kolb8233fac2010-10-26 16:08:53 -0700831}