blob: a52bc567611b26a19b66f39cf78f13ce97650d36 [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;
160 }
161
Michael Kolb66706532010-12-12 19:50:22 -0800162 protected boolean isActivityPaused() {
163 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 }
165
166 public void onConfigurationChanged(Configuration config) {
167 }
168
John Reck0f602f32011-07-07 15:38:43 -0700169 public Activity getActivity() {
170 return mActivity;
171 }
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 // key handling
174
175 @Override
176 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700177 if (mCustomView != null) {
178 mUiController.hideCustomView();
179 return true;
180 }
181 return false;
182 }
183
Michael Kolb2814a362011-05-19 15:49:41 -0700184 @Override
185 public boolean onMenuKey() {
186 return false;
187 }
188
John Reck30c714c2010-12-16 17:30:34 -0800189 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700190 @Override
John Reck30c714c2010-12-16 17:30:34 -0800191 public void onTabDataChanged(Tab tab) {
192 setUrlTitle(tab);
193 setFavicon(tab);
194 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800195 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700196 mTitleBar.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 }
198
199 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500200 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800201 if (tab.inForeground()) {
202 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700203 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800204 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500205 }
206
207 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 public void onPageStopped(Tab tab) {
209 cancelStopToast();
210 if (tab.inForeground()) {
211 mStopToast = Toast
212 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
213 mStopToast.show();
214 }
215 }
216
217 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800218 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700219 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800220 }
221
222 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700224 }
225
226 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800227 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700228 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800229 if ((tab != mActiveTab) && (mActiveTab != null)) {
230 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700231 WebView web = mActiveTab.getWebView();
232 if (web != null) {
233 web.setOnTouchListener(null);
234 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700235 }
Michael Kolb77df4562010-11-19 14:49:34 -0800236 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700237 WebView web = mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700238 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700239 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800241 onTabDataChanged(tab);
242 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800243 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
John Reck0f602f32011-07-07 15:38:43 -0700244 mNavigationBar.setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500245 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700246 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700247 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700248 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700249 showTitleBarForDuration();
250 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700251 }
252
John Reck718a24d2011-08-12 11:08:30 -0700253 protected void updateUrlBarAutoShowManagerTarget() {
254 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
255 if (!mUseQuickControls && web instanceof BrowserWebView) {
256 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
257 } else {
258 mUrlBarAutoShowManager.setTarget(null);
259 }
260 }
261
Michael Kolbcfa3af52010-12-14 10:36:11 -0800262 Tab getActiveTab() {
263 return mActiveTab;
264 }
265
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800267 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800268 }
269
270 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800272 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800274 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 }
277
278 @Override
279 public void detachTab(Tab tab) {
280 removeTabFromContentView(tab);
281 }
282
283 @Override
284 public void attachTab(Tab tab) {
285 attachTabToContentView(tab);
286 }
287
Michael Kolb377ea312011-02-17 14:36:56 -0800288 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800289 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700290 return;
291 }
292 View container = tab.getViewContainer();
293 WebView mainView = tab.getWebView();
294 // Attach the WebView to the container and then attach the
295 // container to the content view.
296 FrameLayout wrapper =
297 (FrameLayout) container.findViewById(R.id.webview_wrapper);
298 ViewGroup parent = (ViewGroup) mainView.getParent();
299 if (parent != wrapper) {
300 if (parent != null) {
301 Log.w(LOGTAG, "mMainView already has a parent in"
302 + " attachTabToContentView!");
303 parent.removeView(mainView);
304 }
305 wrapper.addView(mainView);
306 } else {
307 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
308 + " attachTabToContentView!");
309 }
310 parent = (ViewGroup) container.getParent();
311 if (parent != mContentView) {
312 if (parent != null) {
313 Log.w(LOGTAG, "mContainer already has a parent in"
314 + " attachTabToContentView!");
315 parent.removeView(container);
316 }
317 mContentView.addView(container, COVER_SCREEN_PARAMS);
318 } else {
319 Log.w(LOGTAG, "mContainer is already attached to content in"
320 + " attachTabToContentView!");
321 }
322 mUiController.attachSubWindow(tab);
323 }
324
325 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800326 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700327 // Remove the container that contains the main WebView.
328 WebView mainView = tab.getWebView();
329 View container = tab.getViewContainer();
330 if (mainView == null) {
331 return;
332 }
333 // Remove the container from the content and then remove the
334 // WebView from the container. This will trigger a focus change
335 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800336 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700337 FrameLayout wrapper =
338 (FrameLayout) container.findViewById(R.id.webview_wrapper);
339 wrapper.removeView(mainView);
340 mContentView.removeView(container);
341 mUiController.endActionMode();
342 mUiController.removeSubWindow(tab);
343 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
344 if (errorConsole != null) {
345 mErrorConsoleContainer.removeView(errorConsole);
346 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700347 }
348
Michael Kolba713ec82010-11-29 17:27:06 -0800349 @Override
350 public void onSetWebView(Tab tab, WebView webView) {
351 View container = tab.getViewContainer();
352 if (container == null) {
353 // The tab consists of a container view, which contains the main
354 // WebView, as well as any other UI elements associated with the tab.
355 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700356 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800357 tab.setViewContainer(container);
358 }
359 if (tab.getWebView() != webView) {
360 // Just remove the old one.
361 FrameLayout wrapper =
362 (FrameLayout) container.findViewById(R.id.webview_wrapper);
363 wrapper.removeView(tab.getWebView());
364 }
365 }
366
Michael Kolb8233fac2010-10-26 16:08:53 -0700367 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800368 * create a sub window container and webview for the tab
369 * Note: this methods operates through side-effects for now
370 * it sets both the subView and subViewContainer for the given tab
371 * @param tab tab to create the sub window for
372 * @param subView webview to be set as a subwindow for the tab
373 */
374 @Override
375 public void createSubWindow(Tab tab, WebView subView) {
376 View subViewContainer = mActivity.getLayoutInflater().inflate(
377 R.layout.browser_subwindow, null);
378 ViewGroup inner = (ViewGroup) subViewContainer
379 .findViewById(R.id.inner_container);
380 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
381 LayoutParams.MATCH_PARENT));
382 final ImageButton cancel = (ImageButton) subViewContainer
383 .findViewById(R.id.subwindow_close);
384 final WebView cancelSubView = subView;
385 cancel.setOnClickListener(new OnClickListener() {
386 public void onClick(View v) {
387 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
388 }
389 });
390 tab.setSubWebView(subView);
391 tab.setSubViewContainer(subViewContainer);
392 }
393
394 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700395 * Remove the sub window from the content view.
396 */
397 @Override
398 public void removeSubWindow(View subviewContainer) {
399 mContentView.removeView(subviewContainer);
400 mUiController.endActionMode();
401 }
402
403 /**
404 * Attach the sub window to the content view.
405 */
406 @Override
407 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800408 if (container.getParent() != null) {
409 // already attached, remove first
410 ((ViewGroup) container.getParent()).removeView(container);
411 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700412 mContentView.addView(container, COVER_SCREEN_PARAMS);
413 }
414
Michael Kolb11d19782011-03-20 10:17:40 -0700415 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700416 WebView web = getWebView();
417 if (web != null) {
418 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700419 }
420 }
421
Michael Kolb46f987e2011-04-05 10:39:10 -0700422 public void editUrl(boolean clearInput) {
423 if (mUiController.isInCustomActionMode()) {
424 mUiController.endActionMode();
425 }
426 showTitleBar();
John Reckef654f12011-07-12 16:42:08 -0700427 if (!getActiveTab().isSnapshot()) {
428 mNavigationBar.startEditingUrl(clearInput);
429 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700430 }
431
Michael Kolb7cdc4902011-02-03 17:54:40 -0800432 boolean canShowTitleBar() {
433 return !isTitleBarShowing()
434 && !isActivityPaused()
435 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700436 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800437 && !mUiController.isInCustomActionMode();
438 }
439
John Reck0f602f32011-07-07 15:38:43 -0700440 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700441 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700442 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700443 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700444 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800445 }
446
447 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700448 if (mTitleBar.isShowing()) {
449 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700450 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800451 }
452
453 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700454 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800455 }
456
John Reck5d43ce82011-06-21 17:16:51 -0700457 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700458 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700459 }
460
John Reck0f602f32011-07-07 15:38:43 -0700461 public TitleBar getTitleBar() {
462 return mTitleBar;
463 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800464
465 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700466 WebView web = getWebView();
467 if (web != null) {
468 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700469 }
470 }
471
Michael Kolb66706532010-12-12 19:50:22 -0800472 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700473 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700474 mNavigationBar.setInVoiceMode(true, results);
475 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700476 }
477
Michael Kolb66706532010-12-12 19:50:22 -0800478 @Override
479 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700480 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800481 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700482 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 }
484
485 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700486 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700487 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700488 }
489
490 @Override
John Reck2bc80422011-06-30 15:11:49 -0700491 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700492 Intent intent = new Intent(mActivity, ComboViewActivity.class);
493 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
494 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
495 Tab t = getActiveTab();
496 if (t != null) {
497 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800498 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700499 intent.putExtra(ComboViewActivity.EXTRA_BOOKMARK_PAGE,
500 mUiController.createBookmarkCurrentPageIntent(false));
501 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 }
503
504 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400505 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 WebChromeClient.CustomViewCallback callback) {
507 // if a view already exists then immediately terminate the new one
508 if (mCustomView != null) {
509 callback.onCustomViewHidden();
510 return;
511 }
512
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400513 mOriginalOrientation = mActivity.getRequestedOrientation();
514
Michael Kolb8233fac2010-10-26 16:08:53 -0700515 // Add the custom view to its container.
516 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
517 mCustomView = view;
518 mCustomViewCallback = callback;
519 // Hide the content view.
520 mContentView.setVisibility(View.GONE);
521 // Finally show the custom view container.
522 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400523 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 mCustomViewContainer.setVisibility(View.VISIBLE);
525 mCustomViewContainer.bringToFront();
526 }
527
528 @Override
529 public void onHideCustomView() {
530 if (mCustomView == null)
531 return;
532
533 // Hide the custom view.
534 mCustomView.setVisibility(View.GONE);
535 // Remove the custom view from its container.
536 mCustomViewContainer.removeView(mCustomView);
537 mCustomView = null;
538 mCustomViewContainer.setVisibility(View.GONE);
539 mCustomViewCallback.onCustomViewHidden();
540 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400541 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 setStatusBarVisibility(true);
543 mContentView.setVisibility(View.VISIBLE);
544 }
545
546 @Override
547 public boolean isCustomViewShowing() {
548 return mCustomView != null;
549 }
550
Michael Kolb66706532010-12-12 19:50:22 -0800551 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800552 if (mInputManager.isActive()) {
553 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
554 0);
555 }
556 }
557
Michael Kolb66706532010-12-12 19:50:22 -0800558 @Override
John Reck3ba45532011-08-11 16:26:53 -0700559 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700560 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800561 }
562
Patrick Scott92066772011-03-10 08:46:27 -0500563 @Override
564 public void showAutoLogin(Tab tab) {
565 updateAutoLogin(tab, true);
566 }
567
568 @Override
569 public void hideAutoLogin(Tab tab) {
570 updateAutoLogin(tab, true);
571 }
572
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 // -------------------------------------------------------------------------
574
Michael Kolb5a72f182011-01-13 20:35:06 -0800575 protected void updateNavigationState(Tab tab) {
576 }
577
Michael Kolb11d19782011-03-20 10:17:40 -0700578 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700579 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700580 }
Patrick Scott92066772011-03-10 08:46:27 -0500581
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 /**
583 * Update the lock icon to correspond to our latest state.
584 */
Michael Kolb66706532010-12-12 19:50:22 -0800585 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800586 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 updateLockIconImage(t.getLockIconType());
588 }
589 }
590
591 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700592 * Updates the lock-icon image in the title-bar.
593 */
John Reck30c714c2010-12-16 17:30:34 -0800594 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700595 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800596 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700597 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800598 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 d = mMixLockIcon;
600 }
John Reck0f602f32011-07-07 15:38:43 -0700601 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700602 }
603
John Reck30c714c2010-12-16 17:30:34 -0800604 protected void setUrlTitle(Tab tab) {
605 String url = tab.getUrl();
606 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800607 if (TextUtils.isEmpty(title)) {
608 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800609 }
Michael Kolb66706532010-12-12 19:50:22 -0800610 if (tab.isInVoiceSearchMode()) return;
611 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700612 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800613 }
614 }
615
616 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800617 protected void setFavicon(Tab tab) {
618 if (tab.inForeground()) {
619 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700620 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800621 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 }
623
624 @Override
625 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 }
627
Michael Kolb66706532010-12-12 19:50:22 -0800628 // active tabs page
629
630 public void showActiveTabsPage() {
631 }
632
633 /**
634 * Remove the active tabs page.
635 */
636 public void removeActiveTabsPage() {
637 }
638
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 // menu handling callbacks
640
641 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800642 public boolean onPrepareOptionsMenu(Menu menu) {
643 return true;
644 }
645
646 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700647 public void updateMenuState(Tab tab, Menu menu) {
648 }
649
650 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700651 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 }
653
654 @Override
655 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 }
657
658 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700659 public boolean onOptionsItemSelected(MenuItem item) {
660 return false;
661 }
662
663 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700664 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 }
666
667 @Override
668 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 }
670
671 @Override
672 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 }
674
675 @Override
676 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 }
678
679 // error console
680
681 @Override
682 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800683 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
685 if (flag) {
686 // Setting the show state of the console will cause it's the layout
687 // to be inflated.
688 if (errorConsole.numberOfErrors() > 0) {
689 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
690 } else {
691 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
692 }
693 if (errorConsole.getParent() != null) {
694 mErrorConsoleContainer.removeView(errorConsole);
695 }
696 // Now we can add it to the main view.
697 mErrorConsoleContainer.addView(errorConsole,
698 new LinearLayout.LayoutParams(
699 ViewGroup.LayoutParams.MATCH_PARENT,
700 ViewGroup.LayoutParams.WRAP_CONTENT));
701 } else {
702 mErrorConsoleContainer.removeView(errorConsole);
703 }
704 }
705
706 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800707 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
708 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
709 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700710 }
711
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 // -------------------------------------------------------------------------
713 // Helper function for WebChromeClient
714 // -------------------------------------------------------------------------
715
716 @Override
717 public Bitmap getDefaultVideoPoster() {
718 if (mDefaultVideoPoster == null) {
719 mDefaultVideoPoster = BitmapFactory.decodeResource(
720 mActivity.getResources(), R.drawable.default_video_poster);
721 }
722 return mDefaultVideoPoster;
723 }
724
725 @Override
726 public View getVideoLoadingProgressView() {
727 if (mVideoProgressView == null) {
728 LayoutInflater inflater = LayoutInflater.from(mActivity);
729 mVideoProgressView = inflater.inflate(
730 R.layout.video_loading_progress, null);
731 }
732 return mVideoProgressView;
733 }
734
Michael Kolb843510f2010-12-09 10:51:49 -0800735 @Override
736 public void showMaxTabsWarning() {
737 Toast warning = Toast.makeText(mActivity,
738 mActivity.getString(R.string.max_tabs_warning),
739 Toast.LENGTH_SHORT);
740 warning.show();
741 }
742
Michael Kolb46f987e2011-04-05 10:39:10 -0700743 protected WebView getWebView() {
744 if (mActiveTab != null) {
745 return mActiveTab.getWebView();
746 } else {
747 return null;
748 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700749 }
750
Michael Kolbfedb4922011-04-20 16:45:33 -0700751 protected Menu getMenu() {
752 MenuBuilder menu = new MenuBuilder(mActivity);
753 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
754 return menu;
755 }
756
Michael Kolbc38c6042011-04-27 10:46:06 -0700757 public void setFullscreen(boolean enabled) {
758 if (enabled) {
759 mActivity.getWindow().setFlags(
760 WindowManager.LayoutParams.FLAG_FULLSCREEN,
761 WindowManager.LayoutParams.FLAG_FULLSCREEN);
762 } else {
763 mActivity.getWindow().clearFlags(
764 WindowManager.LayoutParams.FLAG_FULLSCREEN);
765 }
766 }
767
John Reck0f602f32011-07-07 15:38:43 -0700768 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700769 Drawable[] array = new Drawable[3];
770 array[0] = new PaintDrawable(Color.BLACK);
771 PaintDrawable p = new PaintDrawable(Color.WHITE);
772 array[1] = p;
773 if (icon == null) {
774 array[2] = mGenericFavicon;
775 } else {
776 array[2] = new BitmapDrawable(icon);
777 }
778 LayerDrawable d = new LayerDrawable(array);
779 d.setLayerInset(1, 1, 1, 1, 1);
780 d.setLayerInset(2, 2, 2, 2, 2);
781 return d;
782 }
783
John Reck5d43ce82011-06-21 17:16:51 -0700784 public boolean isLoading() {
785 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
786 }
787
788 /**
789 * Suggest to the UI that the title bar can be hidden. The UI will then
790 * decide whether or not to hide based off a number of factors, such
791 * as if the user is editing the URL bar or if the page is loading
792 */
793 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700794 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
795 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700796 hideTitleBar();
797 }
798 }
799
Michael Kolb017ffab2011-07-11 15:26:47 -0700800 protected void showTitleBarForDuration() {
John Reck6ac5bfd2011-07-01 17:02:55 -0700801 showTitleBar();
802 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
803 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
804 }
805
John Reck5d43ce82011-06-21 17:16:51 -0700806 private Handler mHandler = new Handler() {
807
808 @Override
809 public void handleMessage(Message msg) {
810 if (msg.what == MSG_HIDE_TITLEBAR) {
811 suggestHideTitleBar();
812 }
813 }
814 };
John Reck3ba45532011-08-11 16:26:53 -0700815
816 @Override
817 public void showWeb(boolean animate) {
818 mUiController.hideCustomView();
819 }
820
Michael Kolb8233fac2010-10-26 16:08:53 -0700821}