blob: 858e13e49e5a51bcb1e3b0a6122d063a4a7c5c53 [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;
John Reckef654f12011-07-12 16:42:08 -070077 public static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
John Reck5d43ce82011-06-21 17:16:51 -070078
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);
John Reckef654f12011-07-12 16:42:08 -0700211 mTitleBar.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 }
213
214 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500215 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800216 if (tab.inForeground()) {
217 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700218 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800219 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500220 }
221
222 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 public void onPageStopped(Tab tab) {
224 cancelStopToast();
225 if (tab.inForeground()) {
226 mStopToast = Toast
227 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
228 mStopToast.show();
229 }
230 }
231
232 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800233 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700234 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800235 }
236
237 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700239 }
240
241 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800242 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700243 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800244 if ((tab != mActiveTab) && (mActiveTab != null)) {
245 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700246 WebView web = mActiveTab.getWebView();
247 if (web != null) {
248 web.setOnTouchListener(null);
249 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 }
Michael Kolb77df4562010-11-19 14:49:34 -0800251 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700252 WebView web = mActiveTab.getWebView();
253 if (web != null && !mUseQuickControls) {
254 web.setOnTouchListener(this);
255 }
John Reck5d43ce82011-06-21 17:16:51 -0700256 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800258 onTabDataChanged(tab);
259 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800260 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
John Reck0f602f32011-07-07 15:38:43 -0700261 mNavigationBar.setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500262 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700263 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700264 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700265 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700266 showTitleBarForDuration();
267 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 }
269
Michael Kolbcfa3af52010-12-14 10:36:11 -0800270 Tab getActiveTab() {
271 return mActiveTab;
272 }
273
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800275 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800276 }
277
278 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800280 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800282 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700284 }
285
286 @Override
287 public void detachTab(Tab tab) {
288 removeTabFromContentView(tab);
289 }
290
291 @Override
292 public void attachTab(Tab tab) {
293 attachTabToContentView(tab);
294 }
295
Michael Kolb377ea312011-02-17 14:36:56 -0800296 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800297 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700298 return;
299 }
300 View container = tab.getViewContainer();
301 WebView mainView = tab.getWebView();
302 // Attach the WebView to the container and then attach the
303 // container to the content view.
304 FrameLayout wrapper =
305 (FrameLayout) container.findViewById(R.id.webview_wrapper);
306 ViewGroup parent = (ViewGroup) mainView.getParent();
307 if (parent != wrapper) {
308 if (parent != null) {
309 Log.w(LOGTAG, "mMainView already has a parent in"
310 + " attachTabToContentView!");
311 parent.removeView(mainView);
312 }
313 wrapper.addView(mainView);
314 } else {
315 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
316 + " attachTabToContentView!");
317 }
318 parent = (ViewGroup) container.getParent();
319 if (parent != mContentView) {
320 if (parent != null) {
321 Log.w(LOGTAG, "mContainer already has a parent in"
322 + " attachTabToContentView!");
323 parent.removeView(container);
324 }
325 mContentView.addView(container, COVER_SCREEN_PARAMS);
326 } else {
327 Log.w(LOGTAG, "mContainer is already attached to content in"
328 + " attachTabToContentView!");
329 }
330 mUiController.attachSubWindow(tab);
331 }
332
333 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800334 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 // Remove the container that contains the main WebView.
336 WebView mainView = tab.getWebView();
337 View container = tab.getViewContainer();
338 if (mainView == null) {
339 return;
340 }
341 // Remove the container from the content and then remove the
342 // WebView from the container. This will trigger a focus change
343 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800344 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700345 FrameLayout wrapper =
346 (FrameLayout) container.findViewById(R.id.webview_wrapper);
347 wrapper.removeView(mainView);
348 mContentView.removeView(container);
349 mUiController.endActionMode();
350 mUiController.removeSubWindow(tab);
351 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
352 if (errorConsole != null) {
353 mErrorConsoleContainer.removeView(errorConsole);
354 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
356
Michael Kolba713ec82010-11-29 17:27:06 -0800357 @Override
358 public void onSetWebView(Tab tab, WebView webView) {
359 View container = tab.getViewContainer();
360 if (container == null) {
361 // The tab consists of a container view, which contains the main
362 // WebView, as well as any other UI elements associated with the tab.
363 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700364 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800365 tab.setViewContainer(container);
366 }
367 if (tab.getWebView() != webView) {
368 // Just remove the old one.
369 FrameLayout wrapper =
370 (FrameLayout) container.findViewById(R.id.webview_wrapper);
371 wrapper.removeView(tab.getWebView());
372 }
373 }
374
Michael Kolb8233fac2010-10-26 16:08:53 -0700375 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800376 * create a sub window container and webview for the tab
377 * Note: this methods operates through side-effects for now
378 * it sets both the subView and subViewContainer for the given tab
379 * @param tab tab to create the sub window for
380 * @param subView webview to be set as a subwindow for the tab
381 */
382 @Override
383 public void createSubWindow(Tab tab, WebView subView) {
384 View subViewContainer = mActivity.getLayoutInflater().inflate(
385 R.layout.browser_subwindow, null);
386 ViewGroup inner = (ViewGroup) subViewContainer
387 .findViewById(R.id.inner_container);
388 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
389 LayoutParams.MATCH_PARENT));
390 final ImageButton cancel = (ImageButton) subViewContainer
391 .findViewById(R.id.subwindow_close);
392 final WebView cancelSubView = subView;
393 cancel.setOnClickListener(new OnClickListener() {
394 public void onClick(View v) {
395 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
396 }
397 });
398 tab.setSubWebView(subView);
399 tab.setSubViewContainer(subViewContainer);
400 }
401
402 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 * Remove the sub window from the content view.
404 */
405 @Override
406 public void removeSubWindow(View subviewContainer) {
407 mContentView.removeView(subviewContainer);
408 mUiController.endActionMode();
409 }
410
411 /**
412 * Attach the sub window to the content view.
413 */
414 @Override
415 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800416 if (container.getParent() != null) {
417 // already attached, remove first
418 ((ViewGroup) container.getParent()).removeView(container);
419 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 mContentView.addView(container, COVER_SCREEN_PARAMS);
421 }
422
Michael Kolb11d19782011-03-20 10:17:40 -0700423 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700424 WebView web = getWebView();
425 if (web != null) {
426 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700427 }
428 }
429
Michael Kolb46f987e2011-04-05 10:39:10 -0700430 public void editUrl(boolean clearInput) {
431 if (mUiController.isInCustomActionMode()) {
432 mUiController.endActionMode();
433 }
434 showTitleBar();
John Reckef654f12011-07-12 16:42:08 -0700435 if (!getActiveTab().isSnapshot()) {
436 mNavigationBar.startEditingUrl(clearInput);
437 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700438 }
439
Michael Kolb7cdc4902011-02-03 17:54:40 -0800440 boolean canShowTitleBar() {
441 return !isTitleBarShowing()
442 && !isActivityPaused()
443 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700444 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800445 && !mUiController.isInCustomActionMode();
446 }
447
John Reck0f602f32011-07-07 15:38:43 -0700448 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700449 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700450 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700451 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700452 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800453 }
454
455 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700456 if (mTitleBar.isShowing()) {
457 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700458 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800459 }
460
461 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700462 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800463 }
464
John Reck5d43ce82011-06-21 17:16:51 -0700465 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700466 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700467 }
468
John Reck0f602f32011-07-07 15:38:43 -0700469 public TitleBar getTitleBar() {
470 return mTitleBar;
471 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800472
473 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700474 WebView web = getWebView();
475 if (web != null) {
476 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700477 }
478 }
479
Michael Kolb66706532010-12-12 19:50:22 -0800480 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700481 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700482 mNavigationBar.setInVoiceMode(true, results);
483 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 }
485
Michael Kolb66706532010-12-12 19:50:22 -0800486 @Override
487 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700488 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800489 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700490 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700491 }
492
493 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700494 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700495 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700496 }
497
498 @Override
John Reck2bc80422011-06-30 15:11:49 -0700499 public void showComboView(ComboViews startingView, Bundle extras) {
John Reck439c9a52010-12-14 10:04:39 -0800500 if (mComboView != null) {
501 return;
502 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 mComboView = new CombinedBookmarkHistoryView(mActivity,
504 mUiController,
John Reck2bc80422011-06-30 15:11:49 -0700505 startingView,
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 extras);
Michael Kolb47d63f82011-01-18 10:48:40 -0800507 FrameLayout wrapper =
508 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
509 wrapper.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700510 mNavigationBar.stopEditingUrl();
Michael Kolb3a696282010-12-05 13:23:24 -0800511 dismissIME();
Michael Kolbc16c5952011-03-29 15:37:03 -0700512 hideTitleBar();
Michael Kolb3a696282010-12-05 13:23:24 -0800513 if (mActiveTab != null) {
Michael Kolb3a696282010-12-05 13:23:24 -0800514 mActiveTab.putInBackground();
515 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700516 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
517 }
518
Michael Kolbba238702011-03-08 10:40:50 -0800519 public boolean isComboViewShowing() {
520 return (mComboView != null);
521 }
522
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 /**
524 * dismiss the ComboPage
525 */
526 @Override
527 public void hideComboView() {
528 if (mComboView != null) {
529 mContentView.removeView(mComboView);
Michael Kolb47d63f82011-01-18 10:48:40 -0800530 FrameLayout wrapper =
531 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
532 wrapper.setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 mComboView = null;
534 }
Michael Kolb3a696282010-12-05 13:23:24 -0800535 if (mActiveTab != null) {
536 mActiveTab.putInForeground();
537 }
John Reckb3417f02011-01-14 11:01:05 -0800538 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 }
540
541 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400542 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 WebChromeClient.CustomViewCallback callback) {
544 // if a view already exists then immediately terminate the new one
545 if (mCustomView != null) {
546 callback.onCustomViewHidden();
547 return;
548 }
549
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400550 mOriginalOrientation = mActivity.getRequestedOrientation();
551
Michael Kolb8233fac2010-10-26 16:08:53 -0700552 // Add the custom view to its container.
553 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
554 mCustomView = view;
555 mCustomViewCallback = callback;
556 // Hide the content view.
557 mContentView.setVisibility(View.GONE);
558 // Finally show the custom view container.
559 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400560 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 mCustomViewContainer.setVisibility(View.VISIBLE);
562 mCustomViewContainer.bringToFront();
563 }
564
565 @Override
566 public void onHideCustomView() {
567 if (mCustomView == null)
568 return;
569
570 // Hide the custom view.
571 mCustomView.setVisibility(View.GONE);
572 // Remove the custom view from its container.
573 mCustomViewContainer.removeView(mCustomView);
574 mCustomView = null;
575 mCustomViewContainer.setVisibility(View.GONE);
576 mCustomViewCallback.onCustomViewHidden();
577 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400578 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 setStatusBarVisibility(true);
580 mContentView.setVisibility(View.VISIBLE);
581 }
582
583 @Override
584 public boolean isCustomViewShowing() {
585 return mCustomView != null;
586 }
587
Michael Kolb66706532010-12-12 19:50:22 -0800588 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800589 if (mInputManager.isActive()) {
590 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
591 0);
592 }
593 }
594
Michael Kolb66706532010-12-12 19:50:22 -0800595 @Override
596 public boolean showsWeb() {
597 return mCustomView == null
598 && mComboView == null;
599 }
600
Patrick Scott92066772011-03-10 08:46:27 -0500601 @Override
602 public void showAutoLogin(Tab tab) {
603 updateAutoLogin(tab, true);
604 }
605
606 @Override
607 public void hideAutoLogin(Tab tab) {
608 updateAutoLogin(tab, true);
609 }
610
Michael Kolb8233fac2010-10-26 16:08:53 -0700611 // -------------------------------------------------------------------------
612
Michael Kolb5a72f182011-01-13 20:35:06 -0800613 protected void updateNavigationState(Tab tab) {
614 }
615
Michael Kolb11d19782011-03-20 10:17:40 -0700616 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700617 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700618 }
Patrick Scott92066772011-03-10 08:46:27 -0500619
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 /**
621 * Update the lock icon to correspond to our latest state.
622 */
Michael Kolb66706532010-12-12 19:50:22 -0800623 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800624 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 updateLockIconImage(t.getLockIconType());
626 }
627 }
628
629 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 * Updates the lock-icon image in the title-bar.
631 */
John Reck30c714c2010-12-16 17:30:34 -0800632 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800634 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800636 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 d = mMixLockIcon;
638 }
John Reck0f602f32011-07-07 15:38:43 -0700639 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 }
641
John Reck30c714c2010-12-16 17:30:34 -0800642 protected void setUrlTitle(Tab tab) {
643 String url = tab.getUrl();
644 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800645 if (TextUtils.isEmpty(title)) {
646 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800647 }
Michael Kolb66706532010-12-12 19:50:22 -0800648 if (tab.isInVoiceSearchMode()) return;
649 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700650 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800651 }
652 }
653
654 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800655 protected void setFavicon(Tab tab) {
656 if (tab.inForeground()) {
657 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700658 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800659 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 }
661
662 @Override
663 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700664 }
665
Michael Kolb66706532010-12-12 19:50:22 -0800666 // active tabs page
667
668 public void showActiveTabsPage() {
669 }
670
671 /**
672 * Remove the active tabs page.
673 */
674 public void removeActiveTabsPage() {
675 }
676
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 // menu handling callbacks
678
679 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800680 public boolean onPrepareOptionsMenu(Menu menu) {
681 return true;
682 }
683
684 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700685 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
688 @Override
689 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 }
691
692 @Override
693 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700694 }
695
696 @Override
697 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 }
699
700 @Override
701 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 }
703
704 @Override
705 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 }
707
708 // error console
709
710 @Override
711 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800712 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
714 if (flag) {
715 // Setting the show state of the console will cause it's the layout
716 // to be inflated.
717 if (errorConsole.numberOfErrors() > 0) {
718 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
719 } else {
720 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
721 }
722 if (errorConsole.getParent() != null) {
723 mErrorConsoleContainer.removeView(errorConsole);
724 }
725 // Now we can add it to the main view.
726 mErrorConsoleContainer.addView(errorConsole,
727 new LinearLayout.LayoutParams(
728 ViewGroup.LayoutParams.MATCH_PARENT,
729 ViewGroup.LayoutParams.WRAP_CONTENT));
730 } else {
731 mErrorConsoleContainer.removeView(errorConsole);
732 }
733 }
734
735 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800736 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
737 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
738 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 }
740
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 // -------------------------------------------------------------------------
742 // Helper function for WebChromeClient
743 // -------------------------------------------------------------------------
744
745 @Override
746 public Bitmap getDefaultVideoPoster() {
747 if (mDefaultVideoPoster == null) {
748 mDefaultVideoPoster = BitmapFactory.decodeResource(
749 mActivity.getResources(), R.drawable.default_video_poster);
750 }
751 return mDefaultVideoPoster;
752 }
753
754 @Override
755 public View getVideoLoadingProgressView() {
756 if (mVideoProgressView == null) {
757 LayoutInflater inflater = LayoutInflater.from(mActivity);
758 mVideoProgressView = inflater.inflate(
759 R.layout.video_loading_progress, null);
760 }
761 return mVideoProgressView;
762 }
763
Michael Kolb843510f2010-12-09 10:51:49 -0800764 @Override
765 public void showMaxTabsWarning() {
766 Toast warning = Toast.makeText(mActivity,
767 mActivity.getString(R.string.max_tabs_warning),
768 Toast.LENGTH_SHORT);
769 warning.show();
770 }
771
Michael Kolbfdb70242011-03-24 09:41:11 -0700772 protected void captureTab(final Tab tab) {
773 captureTab(tab,
774 (int) mActivity.getResources()
775 .getDimension(R.dimen.qc_thumb_width),
776 (int) mActivity.getResources()
777 .getDimension(R.dimen.qc_thumb_height));
778 }
779
780 protected void captureTab(final Tab tab, int width, int height) {
781 if ((tab == null) || (tab.getWebView() == null)) return;
782 Bitmap sshot = Controller.createScreenshot(tab, width, height);
783 tab.setScreenshot(sshot);
784 }
785
Michael Kolb46f987e2011-04-05 10:39:10 -0700786 protected WebView getWebView() {
787 if (mActiveTab != null) {
788 return mActiveTab.getWebView();
789 } else {
790 return null;
791 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700792 }
793
Michael Kolbfedb4922011-04-20 16:45:33 -0700794 protected Menu getMenu() {
795 MenuBuilder menu = new MenuBuilder(mActivity);
796 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
797 return menu;
798 }
799
Michael Kolbc38c6042011-04-27 10:46:06 -0700800 public void setFullscreen(boolean enabled) {
801 if (enabled) {
802 mActivity.getWindow().setFlags(
803 WindowManager.LayoutParams.FLAG_FULLSCREEN,
804 WindowManager.LayoutParams.FLAG_FULLSCREEN);
805 } else {
806 mActivity.getWindow().clearFlags(
807 WindowManager.LayoutParams.FLAG_FULLSCREEN);
808 }
809 }
810
John Reck0f602f32011-07-07 15:38:43 -0700811 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700812 Drawable[] array = new Drawable[3];
813 array[0] = new PaintDrawable(Color.BLACK);
814 PaintDrawable p = new PaintDrawable(Color.WHITE);
815 array[1] = p;
816 if (icon == null) {
817 array[2] = mGenericFavicon;
818 } else {
819 array[2] = new BitmapDrawable(icon);
820 }
821 LayerDrawable d = new LayerDrawable(array);
822 d.setLayerInset(1, 1, 1, 1, 1);
823 d.setLayerInset(2, 2, 2, 2, 2);
824 return d;
825 }
826
John Reck5d43ce82011-06-21 17:16:51 -0700827 public boolean isLoading() {
828 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
829 }
830
831 /**
832 * Suggest to the UI that the title bar can be hidden. The UI will then
833 * decide whether or not to hide based off a number of factors, such
834 * as if the user is editing the URL bar or if the page is loading
835 */
836 public void suggestHideTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700837 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()) {
John Reck5d43ce82011-06-21 17:16:51 -0700838 hideTitleBar();
839 }
840 }
841
Michael Kolb017ffab2011-07-11 15:26:47 -0700842 protected void showTitleBarForDuration() {
John Reck6ac5bfd2011-07-01 17:02:55 -0700843 showTitleBar();
844 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
845 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
846 }
847
John Reck5d43ce82011-06-21 17:16:51 -0700848 @Override
John Reckbf2ec202011-06-29 17:47:38 -0700849 public boolean onTouch(View v, MotionEvent event) {
850 switch (event.getAction()) {
851 case MotionEvent.ACTION_DOWN:
852 mInitialY = event.getY();
853 break;
854 case MotionEvent.ACTION_MOVE:
855 WebView web = (WebView) v;
John Reck1e822eb2011-07-07 16:53:12 -0700856 if (event.getPointerCount() == 1
857 && !isTitleBarShowing()
John Reckbf2ec202011-06-29 17:47:38 -0700858 && web.getVisibleTitleHeight() == 0
859 && event.getY() > (mInitialY + mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700860 showTitleBar();
John Reckbf2ec202011-06-29 17:47:38 -0700861 } else if (event.getY() < mInitialY) {
862 mInitialY = event.getY();
863 }
864 break;
865 case MotionEvent.ACTION_CANCEL:
866 case MotionEvent.ACTION_UP:
867 if (isTitleBarShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700868 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
869 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
John Reck5d43ce82011-06-21 17:16:51 -0700870 }
John Reckbf2ec202011-06-29 17:47:38 -0700871 break;
John Reck5d43ce82011-06-21 17:16:51 -0700872 }
John Reckbf2ec202011-06-29 17:47:38 -0700873 return false;
John Reck5d43ce82011-06-21 17:16:51 -0700874 }
875
876 private Handler mHandler = new Handler() {
877
878 @Override
879 public void handleMessage(Message msg) {
880 if (msg.what == MSG_HIDE_TITLEBAR) {
881 suggestHideTitleBar();
882 }
883 }
884 };
Michael Kolb8233fac2010-10-26 16:08:53 -0700885}