blob: 5551ecae6eb15925e2d5529466a451e517da9c96 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Michael Kolb8233fac2010-10-26 16:08:53 -070020import android.content.res.Configuration;
21import android.content.res.Resources;
22import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
Michael Kolb5a4372f2011-04-29 13:53:10 -070024import android.graphics.Color;
25import android.graphics.drawable.BitmapDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.graphics.drawable.Drawable;
Michael Kolb5a4372f2011-04-29 13:53:10 -070027import android.graphics.drawable.LayerDrawable;
28import android.graphics.drawable.PaintDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.os.Bundle;
John Reck5d43ce82011-06-21 17:16:51 -070030import android.os.Handler;
31import android.os.Message;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.text.TextUtils;
33import android.util.Log;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.view.Gravity;
35import android.view.LayoutInflater;
36import android.view.Menu;
John Reckbf2ec202011-06-29 17:47:38 -070037import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.view.View;
Michael Kolb1514bb72010-11-22 09:11:48 -080039import android.view.View.OnClickListener;
John Reckbf2ec202011-06-29 17:47:38 -070040import android.view.View.OnTouchListener;
41import android.view.ViewConfiguration;
Michael Kolb8233fac2010-10-26 16:08:53 -070042import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080043import android.view.ViewGroup.LayoutParams;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import android.view.WindowManager;
Michael Kolb3a696282010-12-05 13:23:24 -080045import android.view.inputmethod.InputMethodManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.webkit.WebChromeClient;
Michael Kolb8233fac2010-10-26 16:08:53 -070047import android.webkit.WebView;
48import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080049import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.widget.LinearLayout;
51import android.widget.Toast;
52
John Reckbf2ec202011-06-29 17:47:38 -070053import com.android.browser.Tab.LockIcon;
54import com.android.internal.view.menu.MenuBuilder;
55
Michael Kolb1bf23132010-11-19 12:55:12 -080056import java.util.List;
57
Michael Kolb8233fac2010-10-26 16:08:53 -070058/**
59 * UI interface definitions
60 */
Michael Kolb14612442011-06-24 13:06:29 -070061public abstract class BaseUi implements UI, OnTouchListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070062
63 private static final String LOGTAG = "BaseUi";
64
Michael Kolb66706532010-12-12 19:50:22 -080065 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070066 new FrameLayout.LayoutParams(
67 ViewGroup.LayoutParams.MATCH_PARENT,
68 ViewGroup.LayoutParams.MATCH_PARENT);
69
Michael Kolb66706532010-12-12 19:50:22 -080070 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070071 new FrameLayout.LayoutParams(
72 ViewGroup.LayoutParams.MATCH_PARENT,
73 ViewGroup.LayoutParams.MATCH_PARENT,
74 Gravity.CENTER);
75
John Reck5d43ce82011-06-21 17:16:51 -070076 private static final int MSG_HIDE_TITLEBAR = 1;
77 private static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
78
Michael Kolb8233fac2010-10-26 16:08:53 -070079 Activity mActivity;
80 UiController mUiController;
81 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080082 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080083 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070084
85 private Drawable mSecLockIcon;
86 private Drawable mMixLockIcon;
Michael Kolb5a4372f2011-04-29 13:53:10 -070087 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070088
Michael Kolb66706532010-12-12 19:50:22 -080089 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070090 protected FrameLayout mCustomViewContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070091
92 private View mCustomView;
93 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040094 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070095
96 private CombinedBookmarkHistoryView mComboView;
97
98 private LinearLayout mErrorConsoleContainer = null;
99
100 private Toast mStopToast;
Michael Kolb8233fac2010-10-26 16:08:53 -0700101
John Reckbf2ec202011-06-29 17:47:38 -0700102 private float mInitialY;
John Reck5d43ce82011-06-21 17:16:51 -0700103 private int mTitlebarScrollTriggerSlop;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700104
Michael Kolb8233fac2010-10-26 16:08:53 -0700105 // the default <video> poster
106 private Bitmap mDefaultVideoPoster;
107 // the video progress view
108 private View mVideoProgressView;
109
Michael Kolb8233fac2010-10-26 16:08:53 -0700110 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700111 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700112 protected TitleBar mTitleBar;
113 private NavigationBarBase mNavigationBar;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114
115 public BaseUi(Activity browser, UiController controller) {
116 mActivity = browser;
117 mUiController = controller;
118 mTabControl = controller.getTabControl();
119 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800120 mInputManager = (InputMethodManager)
121 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Michael Kolb5a72f182011-01-13 20:35:06 -0800122 mSecLockIcon = res.getDrawable(R.drawable.ic_secure_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure);
124
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
126 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700127 LayoutInflater.from(mActivity)
128 .inflate(R.layout.custom_screen, frameLayout);
129 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 R.id.main_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700131 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 .findViewById(R.id.error_console);
John Reck7c6e1c92011-06-30 11:55:55 -0700133 mCustomViewContainer = (FrameLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 .findViewById(R.id.fullscreen_custom_content);
Michael Kolbc38c6042011-04-27 10:46:06 -0700135 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700136 mGenericFavicon = res.getDrawable(
137 R.drawable.app_web_browser_sm);
John Reck5d43ce82011-06-21 17:16:51 -0700138 ViewConfiguration config = ViewConfiguration.get(browser);
139 mTitlebarScrollTriggerSlop = Math.max(
140 config.getScaledOverflingDistance(),
141 config.getScaledOverscrollDistance());
142 mTitlebarScrollTriggerSlop = Math.max(mTitlebarScrollTriggerSlop,
143 config.getScaledTouchSlop());
John Reck0f602f32011-07-07 15:38:43 -0700144 mTitleBar = new TitleBar(mActivity, mUiController, this,
145 mContentView);
146 mTitleBar.setProgress(100);
147 mNavigationBar = mTitleBar.getNavigationBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 }
149
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 private void cancelStopToast() {
151 if (mStopToast != null) {
152 mStopToast.cancel();
153 mStopToast = null;
154 }
155 }
156
157 // lifecycle
158
159 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800160 if (isCustomViewShowing()) {
161 onHideCustomView();
162 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 cancelStopToast();
164 mActivityPaused = true;
165 }
166
167 public void onResume() {
168 mActivityPaused = false;
169 }
170
Michael Kolb66706532010-12-12 19:50:22 -0800171 protected boolean isActivityPaused() {
172 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 }
174
175 public void onConfigurationChanged(Configuration config) {
176 }
177
John Reck0f602f32011-07-07 15:38:43 -0700178 public Activity getActivity() {
179 return mActivity;
180 }
181
Michael Kolb8233fac2010-10-26 16:08:53 -0700182 // key handling
183
184 @Override
185 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700186 if (mComboView != null) {
187 if (!mComboView.onBackPressed()) {
188 mUiController.removeComboView();
189 }
190 return true;
191 }
192 if (mCustomView != null) {
193 mUiController.hideCustomView();
194 return true;
195 }
196 return false;
197 }
198
Michael Kolb2814a362011-05-19 15:49:41 -0700199 @Override
200 public boolean onMenuKey() {
201 return false;
202 }
203
John Reck30c714c2010-12-16 17:30:34 -0800204 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 @Override
John Reck30c714c2010-12-16 17:30:34 -0800206 public void onTabDataChanged(Tab tab) {
207 setUrlTitle(tab);
208 setFavicon(tab);
209 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800210 updateNavigationState(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 }
212
213 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500214 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800215 if (tab.inForeground()) {
216 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700217 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800218 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500219 }
220
221 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700222 public void onPageStopped(Tab tab) {
223 cancelStopToast();
224 if (tab.inForeground()) {
225 mStopToast = Toast
226 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
227 mStopToast.show();
228 }
229 }
230
231 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800232 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700233 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800234 }
235
236 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 }
239
240 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800241 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700242 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800243 if ((tab != mActiveTab) && (mActiveTab != null)) {
244 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700245 WebView web = mActiveTab.getWebView();
246 if (web != null) {
247 web.setOnTouchListener(null);
248 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 }
Michael Kolb77df4562010-11-19 14:49:34 -0800250 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700251 WebView web = mActiveTab.getWebView();
252 if (web != null && !mUseQuickControls) {
253 web.setOnTouchListener(this);
254 }
John Reck5d43ce82011-06-21 17:16:51 -0700255 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800257 onTabDataChanged(tab);
258 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800259 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
John Reck0f602f32011-07-07 15:38:43 -0700260 mNavigationBar.setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500261 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700262 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700263 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700264 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700265 showTitleBarForDuration();
266 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 }
268
Michael Kolbcfa3af52010-12-14 10:36:11 -0800269 Tab getActiveTab() {
270 return mActiveTab;
271 }
272
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800274 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800275 }
276
277 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800279 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800281 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 }
284
285 @Override
286 public void detachTab(Tab tab) {
287 removeTabFromContentView(tab);
288 }
289
290 @Override
291 public void attachTab(Tab tab) {
292 attachTabToContentView(tab);
293 }
294
Michael Kolb377ea312011-02-17 14:36:56 -0800295 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800296 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700297 return;
298 }
299 View container = tab.getViewContainer();
300 WebView mainView = tab.getWebView();
301 // Attach the WebView to the container and then attach the
302 // container to the content view.
303 FrameLayout wrapper =
304 (FrameLayout) container.findViewById(R.id.webview_wrapper);
305 ViewGroup parent = (ViewGroup) mainView.getParent();
306 if (parent != wrapper) {
307 if (parent != null) {
308 Log.w(LOGTAG, "mMainView already has a parent in"
309 + " attachTabToContentView!");
310 parent.removeView(mainView);
311 }
312 wrapper.addView(mainView);
313 } else {
314 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
315 + " attachTabToContentView!");
316 }
317 parent = (ViewGroup) container.getParent();
318 if (parent != mContentView) {
319 if (parent != null) {
320 Log.w(LOGTAG, "mContainer already has a parent in"
321 + " attachTabToContentView!");
322 parent.removeView(container);
323 }
324 mContentView.addView(container, COVER_SCREEN_PARAMS);
325 } else {
326 Log.w(LOGTAG, "mContainer is already attached to content in"
327 + " attachTabToContentView!");
328 }
329 mUiController.attachSubWindow(tab);
330 }
331
332 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800333 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 // Remove the container that contains the main WebView.
335 WebView mainView = tab.getWebView();
336 View container = tab.getViewContainer();
337 if (mainView == null) {
338 return;
339 }
340 // Remove the container from the content and then remove the
341 // WebView from the container. This will trigger a focus change
342 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800343 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 FrameLayout wrapper =
345 (FrameLayout) container.findViewById(R.id.webview_wrapper);
346 wrapper.removeView(mainView);
347 mContentView.removeView(container);
348 mUiController.endActionMode();
349 mUiController.removeSubWindow(tab);
350 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
351 if (errorConsole != null) {
352 mErrorConsoleContainer.removeView(errorConsole);
353 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 }
355
Michael Kolba713ec82010-11-29 17:27:06 -0800356 @Override
357 public void onSetWebView(Tab tab, WebView webView) {
358 View container = tab.getViewContainer();
359 if (container == null) {
360 // The tab consists of a container view, which contains the main
361 // WebView, as well as any other UI elements associated with the tab.
362 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700363 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800364 tab.setViewContainer(container);
365 }
366 if (tab.getWebView() != webView) {
367 // Just remove the old one.
368 FrameLayout wrapper =
369 (FrameLayout) container.findViewById(R.id.webview_wrapper);
370 wrapper.removeView(tab.getWebView());
371 }
372 }
373
Michael Kolb8233fac2010-10-26 16:08:53 -0700374 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800375 * create a sub window container and webview for the tab
376 * Note: this methods operates through side-effects for now
377 * it sets both the subView and subViewContainer for the given tab
378 * @param tab tab to create the sub window for
379 * @param subView webview to be set as a subwindow for the tab
380 */
381 @Override
382 public void createSubWindow(Tab tab, WebView subView) {
383 View subViewContainer = mActivity.getLayoutInflater().inflate(
384 R.layout.browser_subwindow, null);
385 ViewGroup inner = (ViewGroup) subViewContainer
386 .findViewById(R.id.inner_container);
387 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
388 LayoutParams.MATCH_PARENT));
389 final ImageButton cancel = (ImageButton) subViewContainer
390 .findViewById(R.id.subwindow_close);
391 final WebView cancelSubView = subView;
392 cancel.setOnClickListener(new OnClickListener() {
393 public void onClick(View v) {
394 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
395 }
396 });
397 tab.setSubWebView(subView);
398 tab.setSubViewContainer(subViewContainer);
399 }
400
401 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 * Remove the sub window from the content view.
403 */
404 @Override
405 public void removeSubWindow(View subviewContainer) {
406 mContentView.removeView(subviewContainer);
407 mUiController.endActionMode();
408 }
409
410 /**
411 * Attach the sub window to the content view.
412 */
413 @Override
414 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800415 if (container.getParent() != null) {
416 // already attached, remove first
417 ((ViewGroup) container.getParent()).removeView(container);
418 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700419 mContentView.addView(container, COVER_SCREEN_PARAMS);
420 }
421
Michael Kolb11d19782011-03-20 10:17:40 -0700422 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700423 WebView web = getWebView();
424 if (web != null) {
425 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700426 }
427 }
428
Michael Kolb46f987e2011-04-05 10:39:10 -0700429 public void editUrl(boolean clearInput) {
430 if (mUiController.isInCustomActionMode()) {
431 mUiController.endActionMode();
432 }
433 showTitleBar();
John Reck0f602f32011-07-07 15:38:43 -0700434 mNavigationBar.startEditingUrl(clearInput);
Michael Kolb46f987e2011-04-05 10:39:10 -0700435 }
436
Michael Kolb7cdc4902011-02-03 17:54:40 -0800437 boolean canShowTitleBar() {
438 return !isTitleBarShowing()
439 && !isActivityPaused()
440 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700441 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800442 && !mUiController.isInCustomActionMode();
443 }
444
John Reck0f602f32011-07-07 15:38:43 -0700445 protected void showTitleBar() {
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 Reck439c9a52010-12-14 10:04:39 -0800496 if (mComboView != null) {
497 return;
498 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700499 mComboView = new CombinedBookmarkHistoryView(mActivity,
500 mUiController,
John Reck2bc80422011-06-30 15:11:49 -0700501 startingView,
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 extras);
Michael Kolb47d63f82011-01-18 10:48:40 -0800503 FrameLayout wrapper =
504 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
505 wrapper.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700506 mNavigationBar.stopEditingUrl();
Michael Kolb3a696282010-12-05 13:23:24 -0800507 dismissIME();
Michael Kolbc16c5952011-03-29 15:37:03 -0700508 hideTitleBar();
Michael Kolb3a696282010-12-05 13:23:24 -0800509 if (mActiveTab != null) {
Michael Kolb3a696282010-12-05 13:23:24 -0800510 mActiveTab.putInBackground();
511 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
513 }
514
Michael Kolbba238702011-03-08 10:40:50 -0800515 public boolean isComboViewShowing() {
516 return (mComboView != null);
517 }
518
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 /**
520 * dismiss the ComboPage
521 */
522 @Override
523 public void hideComboView() {
524 if (mComboView != null) {
525 mContentView.removeView(mComboView);
Michael Kolb47d63f82011-01-18 10:48:40 -0800526 FrameLayout wrapper =
527 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
528 wrapper.setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 mComboView = null;
530 }
Michael Kolb3a696282010-12-05 13:23:24 -0800531 if (mActiveTab != null) {
532 mActiveTab.putInForeground();
533 }
John Reckb3417f02011-01-14 11:01:05 -0800534 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700535 }
536
537 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400538 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 WebChromeClient.CustomViewCallback callback) {
540 // if a view already exists then immediately terminate the new one
541 if (mCustomView != null) {
542 callback.onCustomViewHidden();
543 return;
544 }
545
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400546 mOriginalOrientation = mActivity.getRequestedOrientation();
547
Michael Kolb8233fac2010-10-26 16:08:53 -0700548 // Add the custom view to its container.
549 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
550 mCustomView = view;
551 mCustomViewCallback = callback;
552 // Hide the content view.
553 mContentView.setVisibility(View.GONE);
554 // Finally show the custom view container.
555 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400556 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700557 mCustomViewContainer.setVisibility(View.VISIBLE);
558 mCustomViewContainer.bringToFront();
559 }
560
561 @Override
562 public void onHideCustomView() {
563 if (mCustomView == null)
564 return;
565
566 // Hide the custom view.
567 mCustomView.setVisibility(View.GONE);
568 // Remove the custom view from its container.
569 mCustomViewContainer.removeView(mCustomView);
570 mCustomView = null;
571 mCustomViewContainer.setVisibility(View.GONE);
572 mCustomViewCallback.onCustomViewHidden();
573 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400574 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 setStatusBarVisibility(true);
576 mContentView.setVisibility(View.VISIBLE);
577 }
578
579 @Override
580 public boolean isCustomViewShowing() {
581 return mCustomView != null;
582 }
583
Michael Kolb66706532010-12-12 19:50:22 -0800584 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800585 if (mInputManager.isActive()) {
586 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
587 0);
588 }
589 }
590
Michael Kolb66706532010-12-12 19:50:22 -0800591 @Override
592 public boolean showsWeb() {
593 return mCustomView == null
594 && mComboView == null;
595 }
596
Patrick Scott92066772011-03-10 08:46:27 -0500597 @Override
598 public void showAutoLogin(Tab tab) {
599 updateAutoLogin(tab, true);
600 }
601
602 @Override
603 public void hideAutoLogin(Tab tab) {
604 updateAutoLogin(tab, true);
605 }
606
Michael Kolb8233fac2010-10-26 16:08:53 -0700607 // -------------------------------------------------------------------------
608
Michael Kolb5a72f182011-01-13 20:35:06 -0800609 protected void updateNavigationState(Tab tab) {
610 }
611
Michael Kolb11d19782011-03-20 10:17:40 -0700612 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700613 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700614 }
Patrick Scott92066772011-03-10 08:46:27 -0500615
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 /**
617 * Update the lock icon to correspond to our latest state.
618 */
Michael Kolb66706532010-12-12 19:50:22 -0800619 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800620 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 updateLockIconImage(t.getLockIconType());
622 }
623 }
624
625 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 * Updates the lock-icon image in the title-bar.
627 */
John Reck30c714c2010-12-16 17:30:34 -0800628 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800630 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700631 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800632 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 d = mMixLockIcon;
634 }
John Reck0f602f32011-07-07 15:38:43 -0700635 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 }
637
John Reck30c714c2010-12-16 17:30:34 -0800638 protected void setUrlTitle(Tab tab) {
639 String url = tab.getUrl();
640 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800641 if (TextUtils.isEmpty(title)) {
642 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800643 }
Michael Kolb66706532010-12-12 19:50:22 -0800644 if (tab.isInVoiceSearchMode()) return;
645 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700646 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800647 }
648 }
649
650 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800651 protected void setFavicon(Tab tab) {
652 if (tab.inForeground()) {
653 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700654 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800655 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 }
657
658 @Override
659 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 }
661
Michael Kolb66706532010-12-12 19:50:22 -0800662 // active tabs page
663
664 public void showActiveTabsPage() {
665 }
666
667 /**
668 * Remove the active tabs page.
669 */
670 public void removeActiveTabsPage() {
671 }
672
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 // menu handling callbacks
674
675 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800676 public boolean onPrepareOptionsMenu(Menu menu) {
677 return true;
678 }
679
680 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700681 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
683
684 @Override
685 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
688 @Override
689 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 }
691
692 @Override
693 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700694 }
695
696 @Override
697 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 }
699
700 @Override
701 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 }
703
704 // error console
705
706 @Override
707 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800708 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
710 if (flag) {
711 // Setting the show state of the console will cause it's the layout
712 // to be inflated.
713 if (errorConsole.numberOfErrors() > 0) {
714 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
715 } else {
716 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
717 }
718 if (errorConsole.getParent() != null) {
719 mErrorConsoleContainer.removeView(errorConsole);
720 }
721 // Now we can add it to the main view.
722 mErrorConsoleContainer.addView(errorConsole,
723 new LinearLayout.LayoutParams(
724 ViewGroup.LayoutParams.MATCH_PARENT,
725 ViewGroup.LayoutParams.WRAP_CONTENT));
726 } else {
727 mErrorConsoleContainer.removeView(errorConsole);
728 }
729 }
730
731 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800732 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
733 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
734 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700735 }
736
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 // -------------------------------------------------------------------------
738 // Helper function for WebChromeClient
739 // -------------------------------------------------------------------------
740
741 @Override
742 public Bitmap getDefaultVideoPoster() {
743 if (mDefaultVideoPoster == null) {
744 mDefaultVideoPoster = BitmapFactory.decodeResource(
745 mActivity.getResources(), R.drawable.default_video_poster);
746 }
747 return mDefaultVideoPoster;
748 }
749
750 @Override
751 public View getVideoLoadingProgressView() {
752 if (mVideoProgressView == null) {
753 LayoutInflater inflater = LayoutInflater.from(mActivity);
754 mVideoProgressView = inflater.inflate(
755 R.layout.video_loading_progress, null);
756 }
757 return mVideoProgressView;
758 }
759
Michael Kolb843510f2010-12-09 10:51:49 -0800760 @Override
761 public void showMaxTabsWarning() {
762 Toast warning = Toast.makeText(mActivity,
763 mActivity.getString(R.string.max_tabs_warning),
764 Toast.LENGTH_SHORT);
765 warning.show();
766 }
767
Michael Kolbfdb70242011-03-24 09:41:11 -0700768 protected void captureTab(final Tab tab) {
769 captureTab(tab,
770 (int) mActivity.getResources()
771 .getDimension(R.dimen.qc_thumb_width),
772 (int) mActivity.getResources()
773 .getDimension(R.dimen.qc_thumb_height));
774 }
775
776 protected void captureTab(final Tab tab, int width, int height) {
777 if ((tab == null) || (tab.getWebView() == null)) return;
778 Bitmap sshot = Controller.createScreenshot(tab, width, height);
779 tab.setScreenshot(sshot);
780 }
781
Michael Kolb46f987e2011-04-05 10:39:10 -0700782 protected WebView getWebView() {
783 if (mActiveTab != null) {
784 return mActiveTab.getWebView();
785 } else {
786 return null;
787 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700788 }
789
Michael Kolbfedb4922011-04-20 16:45:33 -0700790 protected Menu getMenu() {
791 MenuBuilder menu = new MenuBuilder(mActivity);
792 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
793 return menu;
794 }
795
Michael Kolbc38c6042011-04-27 10:46:06 -0700796 public void setFullscreen(boolean enabled) {
797 if (enabled) {
798 mActivity.getWindow().setFlags(
799 WindowManager.LayoutParams.FLAG_FULLSCREEN,
800 WindowManager.LayoutParams.FLAG_FULLSCREEN);
801 } else {
802 mActivity.getWindow().clearFlags(
803 WindowManager.LayoutParams.FLAG_FULLSCREEN);
804 }
805 }
806
John Reck0f602f32011-07-07 15:38:43 -0700807 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700808 Drawable[] array = new Drawable[3];
809 array[0] = new PaintDrawable(Color.BLACK);
810 PaintDrawable p = new PaintDrawable(Color.WHITE);
811 array[1] = p;
812 if (icon == null) {
813 array[2] = mGenericFavicon;
814 } else {
815 array[2] = new BitmapDrawable(icon);
816 }
817 LayerDrawable d = new LayerDrawable(array);
818 d.setLayerInset(1, 1, 1, 1, 1);
819 d.setLayerInset(2, 2, 2, 2, 2);
820 return d;
821 }
822
John Reck5d43ce82011-06-21 17:16:51 -0700823 public boolean isLoading() {
824 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
825 }
826
827 /**
828 * Suggest to the UI that the title bar can be hidden. The UI will then
829 * decide whether or not to hide based off a number of factors, such
830 * as if the user is editing the URL bar or if the page is loading
831 */
832 public void suggestHideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700833 if (!isLoading() && !isEditingUrl() && !mTitleBar.inAutoLogin()) {
John Reck5d43ce82011-06-21 17:16:51 -0700834 hideTitleBar();
835 }
836 }
837
Michael Kolb017ffab2011-07-11 15:26:47 -0700838 protected void showTitleBarForDuration() {
John Reck6ac5bfd2011-07-01 17:02:55 -0700839 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
840 showTitleBar();
841 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
842 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
843 }
844
John Reck5d43ce82011-06-21 17:16:51 -0700845 @Override
John Reckbf2ec202011-06-29 17:47:38 -0700846 public boolean onTouch(View v, MotionEvent event) {
847 switch (event.getAction()) {
848 case MotionEvent.ACTION_DOWN:
849 mInitialY = event.getY();
850 break;
851 case MotionEvent.ACTION_MOVE:
852 WebView web = (WebView) v;
John Reck1e822eb2011-07-07 16:53:12 -0700853 if (event.getPointerCount() == 1
854 && !isTitleBarShowing()
John Reckbf2ec202011-06-29 17:47:38 -0700855 && web.getVisibleTitleHeight() == 0
856 && event.getY() > (mInitialY + mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700857 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
858 showTitleBar();
John Reckbf2ec202011-06-29 17:47:38 -0700859 } else if (event.getY() < mInitialY) {
860 mInitialY = event.getY();
861 }
862 break;
863 case MotionEvent.ACTION_CANCEL:
864 case MotionEvent.ACTION_UP:
865 if (isTitleBarShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700866 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
867 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
John Reck5d43ce82011-06-21 17:16:51 -0700868 }
John Reckbf2ec202011-06-29 17:47:38 -0700869 break;
John Reck5d43ce82011-06-21 17:16:51 -0700870 }
John Reckbf2ec202011-06-29 17:47:38 -0700871 return false;
John Reck5d43ce82011-06-21 17:16:51 -0700872 }
873
874 private Handler mHandler = new Handler() {
875
876 @Override
877 public void handleMessage(Message msg) {
878 if (msg.what == MSG_HIDE_TITLEBAR) {
879 suggestHideTitleBar();
880 }
881 }
882 };
Michael Kolb8233fac2010-10-26 16:08:53 -0700883}