blob: 8e2435302e4d9bbf9722796e011a8ea599238737 [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 Reckb9a051b2011-03-18 11:55:48 -070020import android.content.pm.PackageManager;
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;
John Reckbf2ec202011-06-29 17:47:38 -070038import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070039import android.view.View;
Michael Kolb1514bb72010-11-22 09:11:48 -080040import android.view.View.OnClickListener;
John Reckbf2ec202011-06-29 17:47:38 -070041import android.view.View.OnTouchListener;
42import android.view.ViewConfiguration;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080044import android.view.ViewGroup.LayoutParams;
Michael Kolb8233fac2010-10-26 16:08:53 -070045import android.view.WindowManager;
Michael Kolb3a696282010-12-05 13:23:24 -080046import android.view.inputmethod.InputMethodManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070047import android.webkit.WebChromeClient;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.webkit.WebView;
49import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080050import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.widget.LinearLayout;
52import android.widget.Toast;
53
John Reckbf2ec202011-06-29 17:47:38 -070054import com.android.browser.Tab.LockIcon;
55import com.android.internal.view.menu.MenuBuilder;
56
Michael Kolb1bf23132010-11-19 12:55:12 -080057import java.util.List;
58
Michael Kolb8233fac2010-10-26 16:08:53 -070059/**
60 * UI interface definitions
61 */
Michael Kolb14612442011-06-24 13:06:29 -070062public abstract class BaseUi implements UI, OnTouchListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070063
64 private static final String LOGTAG = "BaseUi";
65
Michael Kolb66706532010-12-12 19:50:22 -080066 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070067 new FrameLayout.LayoutParams(
68 ViewGroup.LayoutParams.MATCH_PARENT,
69 ViewGroup.LayoutParams.MATCH_PARENT);
70
Michael Kolb66706532010-12-12 19:50:22 -080071 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070072 new FrameLayout.LayoutParams(
73 ViewGroup.LayoutParams.MATCH_PARENT,
74 ViewGroup.LayoutParams.MATCH_PARENT,
75 Gravity.CENTER);
76
John Reck5d43ce82011-06-21 17:16:51 -070077 private static final int MSG_HIDE_TITLEBAR = 1;
78 private static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
79
Michael Kolb8233fac2010-10-26 16:08:53 -070080 Activity mActivity;
81 UiController mUiController;
82 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080083 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080084 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070085
86 private Drawable mSecLockIcon;
87 private Drawable mMixLockIcon;
Michael Kolb5a4372f2011-04-29 13:53:10 -070088 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070089
Michael Kolb66706532010-12-12 19:50:22 -080090 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070091 protected FrameLayout mCustomViewContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070092
93 private View mCustomView;
94 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040095 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070096
97 private CombinedBookmarkHistoryView mComboView;
98
99 private LinearLayout mErrorConsoleContainer = null;
100
101 private Toast mStopToast;
Michael Kolb8233fac2010-10-26 16:08:53 -0700102
John Reckbf2ec202011-06-29 17:47:38 -0700103 private float mInitialY;
John Reck5d43ce82011-06-21 17:16:51 -0700104 private int mTitlebarScrollTriggerSlop;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106 // the default <video> poster
107 private Bitmap mDefaultVideoPoster;
108 // the video progress view
109 private View mVideoProgressView;
110
Michael Kolb8233fac2010-10-26 16:08:53 -0700111 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700112 protected boolean mUseQuickControls;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113
114 public BaseUi(Activity browser, UiController controller) {
115 mActivity = browser;
116 mUiController = controller;
117 mTabControl = controller.getTabControl();
118 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800119 mInputManager = (InputMethodManager)
120 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Michael Kolb5a72f182011-01-13 20:35:06 -0800121 mSecLockIcon = res.getDrawable(R.drawable.ic_secure_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure);
123
Michael Kolb8233fac2010-10-26 16:08:53 -0700124 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
125 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700126 LayoutInflater.from(mActivity)
127 .inflate(R.layout.custom_screen, frameLayout);
128 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700129 R.id.main_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700130 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700131 .findViewById(R.id.error_console);
John Reck7c6e1c92011-06-30 11:55:55 -0700132 mCustomViewContainer = (FrameLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 .findViewById(R.id.fullscreen_custom_content);
Michael Kolbc38c6042011-04-27 10:46:06 -0700134 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700135 mGenericFavicon = res.getDrawable(
136 R.drawable.app_web_browser_sm);
John Reck5d43ce82011-06-21 17:16:51 -0700137 ViewConfiguration config = ViewConfiguration.get(browser);
138 mTitlebarScrollTriggerSlop = Math.max(
139 config.getScaledOverflingDistance(),
140 config.getScaledOverscrollDistance());
141 mTitlebarScrollTriggerSlop = Math.max(mTitlebarScrollTriggerSlop,
142 config.getScaledTouchSlop());
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 }
144
Michael Kolb8233fac2010-10-26 16:08:53 -0700145 private void cancelStopToast() {
146 if (mStopToast != null) {
147 mStopToast.cancel();
148 mStopToast = null;
149 }
150 }
151
152 // lifecycle
153
154 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800155 if (isCustomViewShowing()) {
156 onHideCustomView();
157 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 cancelStopToast();
159 mActivityPaused = true;
160 }
161
162 public void onResume() {
163 mActivityPaused = false;
164 }
165
Michael Kolb66706532010-12-12 19:50:22 -0800166 protected boolean isActivityPaused() {
167 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700168 }
169
170 public void onConfigurationChanged(Configuration config) {
171 }
172
173 // key handling
174
175 @Override
176 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700177 if (mComboView != null) {
178 if (!mComboView.onBackPressed()) {
179 mUiController.removeComboView();
180 }
181 return true;
182 }
183 if (mCustomView != null) {
184 mUiController.hideCustomView();
185 return true;
186 }
187 return false;
188 }
189
Michael Kolb2814a362011-05-19 15:49:41 -0700190 @Override
191 public boolean onMenuKey() {
192 return false;
193 }
194
John Reck30c714c2010-12-16 17:30:34 -0800195 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 @Override
John Reck30c714c2010-12-16 17:30:34 -0800197 public void onTabDataChanged(Tab tab) {
198 setUrlTitle(tab);
199 setFavicon(tab);
200 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800201 updateNavigationState(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700202 }
203
204 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500205 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800206 if (tab.inForeground()) {
207 boolean isBookmark = tab.isBookmarkedSite();
208 getTitleBar().setCurrentUrlIsBookmark(isBookmark);
209 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500210 }
211
212 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 public void onPageStopped(Tab tab) {
214 cancelStopToast();
215 if (tab.inForeground()) {
216 mStopToast = Toast
217 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
218 mStopToast.show();
219 }
220 }
221
222 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800223 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700224 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800225 }
226
227 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700228 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 }
230
231 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800232 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700233 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800234 if ((tab != mActiveTab) && (mActiveTab != null)) {
235 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700236 WebView web = mActiveTab.getWebView();
237 if (web != null) {
238 web.setOnTouchListener(null);
239 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 }
Michael Kolb77df4562010-11-19 14:49:34 -0800241 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700242 WebView web = mActiveTab.getWebView();
243 if (web != null && !mUseQuickControls) {
244 web.setOnTouchListener(this);
245 }
John Reck5d43ce82011-06-21 17:16:51 -0700246 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700247 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800248 onTabDataChanged(tab);
249 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800250 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800251 getTitleBar().setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500252 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700253 if (web != null && web.getVisibleTitleHeight()
254 != getTitleBar().getEmbeddedHeight()) {
255 showTitleBarForDuration();
256 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 }
258
Michael Kolbcfa3af52010-12-14 10:36:11 -0800259 Tab getActiveTab() {
260 return mActiveTab;
261 }
262
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800264 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800265 }
266
267 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800269 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800271 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 }
274
275 @Override
276 public void detachTab(Tab tab) {
277 removeTabFromContentView(tab);
278 }
279
280 @Override
281 public void attachTab(Tab tab) {
282 attachTabToContentView(tab);
283 }
284
Michael Kolb377ea312011-02-17 14:36:56 -0800285 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800286 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 return;
288 }
289 View container = tab.getViewContainer();
290 WebView mainView = tab.getWebView();
291 // Attach the WebView to the container and then attach the
292 // container to the content view.
293 FrameLayout wrapper =
294 (FrameLayout) container.findViewById(R.id.webview_wrapper);
295 ViewGroup parent = (ViewGroup) mainView.getParent();
296 if (parent != wrapper) {
297 if (parent != null) {
298 Log.w(LOGTAG, "mMainView already has a parent in"
299 + " attachTabToContentView!");
300 parent.removeView(mainView);
301 }
302 wrapper.addView(mainView);
303 } else {
304 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
305 + " attachTabToContentView!");
306 }
307 parent = (ViewGroup) container.getParent();
308 if (parent != mContentView) {
309 if (parent != null) {
310 Log.w(LOGTAG, "mContainer already has a parent in"
311 + " attachTabToContentView!");
312 parent.removeView(container);
313 }
314 mContentView.addView(container, COVER_SCREEN_PARAMS);
315 } else {
316 Log.w(LOGTAG, "mContainer is already attached to content in"
317 + " attachTabToContentView!");
318 }
319 mUiController.attachSubWindow(tab);
320 }
321
322 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800323 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700324 // Remove the container that contains the main WebView.
325 WebView mainView = tab.getWebView();
326 View container = tab.getViewContainer();
327 if (mainView == null) {
328 return;
329 }
330 // Remove the container from the content and then remove the
331 // WebView from the container. This will trigger a focus change
332 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800333 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 FrameLayout wrapper =
335 (FrameLayout) container.findViewById(R.id.webview_wrapper);
336 wrapper.removeView(mainView);
337 mContentView.removeView(container);
338 mUiController.endActionMode();
339 mUiController.removeSubWindow(tab);
340 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
341 if (errorConsole != null) {
342 mErrorConsoleContainer.removeView(errorConsole);
343 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 }
345
Michael Kolba713ec82010-11-29 17:27:06 -0800346 @Override
347 public void onSetWebView(Tab tab, WebView webView) {
348 View container = tab.getViewContainer();
349 if (container == null) {
350 // The tab consists of a container view, which contains the main
351 // WebView, as well as any other UI elements associated with the tab.
352 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700353 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800354 tab.setViewContainer(container);
355 }
356 if (tab.getWebView() != webView) {
357 // Just remove the old one.
358 FrameLayout wrapper =
359 (FrameLayout) container.findViewById(R.id.webview_wrapper);
360 wrapper.removeView(tab.getWebView());
361 }
362 }
363
Michael Kolb8233fac2010-10-26 16:08:53 -0700364 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800365 * create a sub window container and webview for the tab
366 * Note: this methods operates through side-effects for now
367 * it sets both the subView and subViewContainer for the given tab
368 * @param tab tab to create the sub window for
369 * @param subView webview to be set as a subwindow for the tab
370 */
371 @Override
372 public void createSubWindow(Tab tab, WebView subView) {
373 View subViewContainer = mActivity.getLayoutInflater().inflate(
374 R.layout.browser_subwindow, null);
375 ViewGroup inner = (ViewGroup) subViewContainer
376 .findViewById(R.id.inner_container);
377 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
378 LayoutParams.MATCH_PARENT));
379 final ImageButton cancel = (ImageButton) subViewContainer
380 .findViewById(R.id.subwindow_close);
381 final WebView cancelSubView = subView;
382 cancel.setOnClickListener(new OnClickListener() {
383 public void onClick(View v) {
384 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
385 }
386 });
387 tab.setSubWebView(subView);
388 tab.setSubViewContainer(subViewContainer);
389 }
390
391 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700392 * Remove the sub window from the content view.
393 */
394 @Override
395 public void removeSubWindow(View subviewContainer) {
396 mContentView.removeView(subviewContainer);
397 mUiController.endActionMode();
398 }
399
400 /**
401 * Attach the sub window to the content view.
402 */
403 @Override
404 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800405 if (container.getParent() != null) {
406 // already attached, remove first
407 ((ViewGroup) container.getParent()).removeView(container);
408 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700409 mContentView.addView(container, COVER_SCREEN_PARAMS);
410 }
411
Michael Kolb11d19782011-03-20 10:17:40 -0700412 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700413 WebView web = getWebView();
414 if (web != null) {
415 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700416 }
417 }
418
Michael Kolb46f987e2011-04-05 10:39:10 -0700419 public void editUrl(boolean clearInput) {
420 if (mUiController.isInCustomActionMode()) {
421 mUiController.endActionMode();
422 }
423 showTitleBar();
424 getTitleBar().startEditingUrl(clearInput);
425 }
426
Michael Kolb7cdc4902011-02-03 17:54:40 -0800427 boolean canShowTitleBar() {
428 return !isTitleBarShowing()
429 && !isActivityPaused()
430 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700431 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800432 && !mUiController.isInCustomActionMode();
433 }
434
435 void showTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700436 if (canShowTitleBar()) {
437 getTitleBar().show();
438 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800439 }
440
441 protected void hideTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700442 if (getTitleBar().isShowing()) {
443 getTitleBar().hide();
444 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800445 }
446
447 protected boolean isTitleBarShowing() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700448 return getTitleBar().isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800449 }
450
John Reck5d43ce82011-06-21 17:16:51 -0700451 public boolean isEditingUrl() {
452 return getTitleBar().isEditingUrl();
453 }
454
Michael Kolb7cdc4902011-02-03 17:54:40 -0800455 protected abstract TitleBarBase getTitleBar();
456
457 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700458 WebView web = getWebView();
459 if (web != null) {
460 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 }
462 }
463
Michael Kolb66706532010-12-12 19:50:22 -0800464 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700465 public void showVoiceTitleBar(String title, List<String> results) {
466 getTitleBar().setInVoiceMode(true, results);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800467 getTitleBar().setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700468 }
469
Michael Kolb66706532010-12-12 19:50:22 -0800470 @Override
471 public void revertVoiceTitleBar(Tab tab) {
Michael Kolb11d19782011-03-20 10:17:40 -0700472 getTitleBar().setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800473 String url = tab.getUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800474 getTitleBar().setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 }
476
477 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700478 public void registerDropdownChangeListener(DropdownChangeListener d) {
479 getTitleBar().registerDropdownChangeListener(d);
480 }
481
482 @Override
John Reck2bc80422011-06-30 15:11:49 -0700483 public void showComboView(ComboViews startingView, Bundle extras) {
John Reck439c9a52010-12-14 10:04:39 -0800484 if (mComboView != null) {
485 return;
486 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 mComboView = new CombinedBookmarkHistoryView(mActivity,
488 mUiController,
John Reck2bc80422011-06-30 15:11:49 -0700489 startingView,
Michael Kolb8233fac2010-10-26 16:08:53 -0700490 extras);
Michael Kolb47d63f82011-01-18 10:48:40 -0800491 FrameLayout wrapper =
492 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
493 wrapper.setVisibility(View.GONE);
Michael Kolbc16c5952011-03-29 15:37:03 -0700494 getTitleBar().stopEditingUrl();
Michael Kolb3a696282010-12-05 13:23:24 -0800495 dismissIME();
Michael Kolbc16c5952011-03-29 15:37:03 -0700496 hideTitleBar();
Michael Kolb3a696282010-12-05 13:23:24 -0800497 if (mActiveTab != null) {
Michael Kolb3a696282010-12-05 13:23:24 -0800498 mActiveTab.putInBackground();
499 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700500 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
501 }
502
Michael Kolbba238702011-03-08 10:40:50 -0800503 public boolean isComboViewShowing() {
504 return (mComboView != null);
505 }
506
Michael Kolb8233fac2010-10-26 16:08:53 -0700507 /**
508 * dismiss the ComboPage
509 */
510 @Override
511 public void hideComboView() {
512 if (mComboView != null) {
513 mContentView.removeView(mComboView);
Michael Kolb47d63f82011-01-18 10:48:40 -0800514 FrameLayout wrapper =
515 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
516 wrapper.setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 mComboView = null;
518 }
Michael Kolb3a696282010-12-05 13:23:24 -0800519 if (mActiveTab != null) {
520 mActiveTab.putInForeground();
521 }
John Reckb3417f02011-01-14 11:01:05 -0800522 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 }
524
525 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400526 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 WebChromeClient.CustomViewCallback callback) {
528 // if a view already exists then immediately terminate the new one
529 if (mCustomView != null) {
530 callback.onCustomViewHidden();
531 return;
532 }
533
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400534 mOriginalOrientation = mActivity.getRequestedOrientation();
535
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 // Add the custom view to its container.
537 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
538 mCustomView = view;
539 mCustomViewCallback = callback;
540 // Hide the content view.
541 mContentView.setVisibility(View.GONE);
542 // Finally show the custom view container.
543 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400544 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 mCustomViewContainer.setVisibility(View.VISIBLE);
546 mCustomViewContainer.bringToFront();
547 }
548
549 @Override
550 public void onHideCustomView() {
551 if (mCustomView == null)
552 return;
553
554 // Hide the custom view.
555 mCustomView.setVisibility(View.GONE);
556 // Remove the custom view from its container.
557 mCustomViewContainer.removeView(mCustomView);
558 mCustomView = null;
559 mCustomViewContainer.setVisibility(View.GONE);
560 mCustomViewCallback.onCustomViewHidden();
561 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400562 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 setStatusBarVisibility(true);
564 mContentView.setVisibility(View.VISIBLE);
565 }
566
567 @Override
568 public boolean isCustomViewShowing() {
569 return mCustomView != null;
570 }
571
Michael Kolb66706532010-12-12 19:50:22 -0800572 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800573 if (mInputManager.isActive()) {
574 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
575 0);
576 }
577 }
578
Michael Kolb66706532010-12-12 19:50:22 -0800579 @Override
580 public boolean showsWeb() {
581 return mCustomView == null
582 && mComboView == null;
583 }
584
Patrick Scott92066772011-03-10 08:46:27 -0500585 @Override
586 public void showAutoLogin(Tab tab) {
587 updateAutoLogin(tab, true);
588 }
589
590 @Override
591 public void hideAutoLogin(Tab tab) {
592 updateAutoLogin(tab, true);
593 }
594
Michael Kolb8233fac2010-10-26 16:08:53 -0700595 // -------------------------------------------------------------------------
596
Michael Kolb5a72f182011-01-13 20:35:06 -0800597 protected void updateNavigationState(Tab tab) {
598 }
599
Michael Kolb11d19782011-03-20 10:17:40 -0700600 protected void updateAutoLogin(Tab tab, boolean animate) {
601 getTitleBar().updateAutoLogin(tab, animate);
602 }
Patrick Scott92066772011-03-10 08:46:27 -0500603
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 /**
605 * Update the lock icon to correspond to our latest state.
606 */
Michael Kolb66706532010-12-12 19:50:22 -0800607 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800608 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700609 updateLockIconImage(t.getLockIconType());
610 }
611 }
612
613 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 * Updates the lock-icon image in the title-bar.
615 */
John Reck30c714c2010-12-16 17:30:34 -0800616 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800618 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700619 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800620 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 d = mMixLockIcon;
622 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800623 getTitleBar().setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700624 }
625
John Reck30c714c2010-12-16 17:30:34 -0800626 protected void setUrlTitle(Tab tab) {
627 String url = tab.getUrl();
628 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800629 if (TextUtils.isEmpty(title)) {
630 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800631 }
Michael Kolb66706532010-12-12 19:50:22 -0800632 if (tab.isInVoiceSearchMode()) return;
633 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800634 getTitleBar().setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800635 }
636 }
637
638 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800639 protected void setFavicon(Tab tab) {
640 if (tab.inForeground()) {
641 Bitmap icon = tab.getFavicon();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800642 getTitleBar().setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800643 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700644 }
645
646 @Override
647 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 }
649
Michael Kolb66706532010-12-12 19:50:22 -0800650 // active tabs page
651
652 public void showActiveTabsPage() {
653 }
654
655 /**
656 * Remove the active tabs page.
657 */
658 public void removeActiveTabsPage() {
659 }
660
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 // menu handling callbacks
662
663 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800664 public boolean onPrepareOptionsMenu(Menu menu) {
665 return true;
666 }
667
668 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 }
671
672 @Override
673 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700674 }
675
676 @Override
677 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 }
679
680 @Override
681 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
683
684 @Override
685 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
688 @Override
689 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 }
691
692 // error console
693
694 @Override
695 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800696 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
698 if (flag) {
699 // Setting the show state of the console will cause it's the layout
700 // to be inflated.
701 if (errorConsole.numberOfErrors() > 0) {
702 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
703 } else {
704 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
705 }
706 if (errorConsole.getParent() != null) {
707 mErrorConsoleContainer.removeView(errorConsole);
708 }
709 // Now we can add it to the main view.
710 mErrorConsoleContainer.addView(errorConsole,
711 new LinearLayout.LayoutParams(
712 ViewGroup.LayoutParams.MATCH_PARENT,
713 ViewGroup.LayoutParams.WRAP_CONTENT));
714 } else {
715 mErrorConsoleContainer.removeView(errorConsole);
716 }
717 }
718
719 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800720 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
721 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
722 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700723 }
724
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 // -------------------------------------------------------------------------
726 // Helper function for WebChromeClient
727 // -------------------------------------------------------------------------
728
729 @Override
730 public Bitmap getDefaultVideoPoster() {
731 if (mDefaultVideoPoster == null) {
732 mDefaultVideoPoster = BitmapFactory.decodeResource(
733 mActivity.getResources(), R.drawable.default_video_poster);
734 }
735 return mDefaultVideoPoster;
736 }
737
738 @Override
739 public View getVideoLoadingProgressView() {
740 if (mVideoProgressView == null) {
741 LayoutInflater inflater = LayoutInflater.from(mActivity);
742 mVideoProgressView = inflater.inflate(
743 R.layout.video_loading_progress, null);
744 }
745 return mVideoProgressView;
746 }
747
Michael Kolb843510f2010-12-09 10:51:49 -0800748 @Override
749 public void showMaxTabsWarning() {
750 Toast warning = Toast.makeText(mActivity,
751 mActivity.getString(R.string.max_tabs_warning),
752 Toast.LENGTH_SHORT);
753 warning.show();
754 }
755
Michael Kolbfdb70242011-03-24 09:41:11 -0700756 protected void captureTab(final Tab tab) {
757 captureTab(tab,
758 (int) mActivity.getResources()
759 .getDimension(R.dimen.qc_thumb_width),
760 (int) mActivity.getResources()
761 .getDimension(R.dimen.qc_thumb_height));
762 }
763
764 protected void captureTab(final Tab tab, int width, int height) {
765 if ((tab == null) || (tab.getWebView() == null)) return;
766 Bitmap sshot = Controller.createScreenshot(tab, width, height);
767 tab.setScreenshot(sshot);
768 }
769
Michael Kolb46f987e2011-04-05 10:39:10 -0700770 protected WebView getWebView() {
771 if (mActiveTab != null) {
772 return mActiveTab.getWebView();
773 } else {
774 return null;
775 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700776 }
777
Michael Kolbfedb4922011-04-20 16:45:33 -0700778 protected Menu getMenu() {
779 MenuBuilder menu = new MenuBuilder(mActivity);
780 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
781 return menu;
782 }
783
Michael Kolbc38c6042011-04-27 10:46:06 -0700784 public void setFullscreen(boolean enabled) {
785 if (enabled) {
786 mActivity.getWindow().setFlags(
787 WindowManager.LayoutParams.FLAG_FULLSCREEN,
788 WindowManager.LayoutParams.FLAG_FULLSCREEN);
789 } else {
790 mActivity.getWindow().clearFlags(
791 WindowManager.LayoutParams.FLAG_FULLSCREEN);
792 }
793 }
794
Michael Kolb5a4372f2011-04-29 13:53:10 -0700795 protected Drawable getFaviconDrawable(Bitmap icon) {
796 Drawable[] array = new Drawable[3];
797 array[0] = new PaintDrawable(Color.BLACK);
798 PaintDrawable p = new PaintDrawable(Color.WHITE);
799 array[1] = p;
800 if (icon == null) {
801 array[2] = mGenericFavicon;
802 } else {
803 array[2] = new BitmapDrawable(icon);
804 }
805 LayerDrawable d = new LayerDrawable(array);
806 d.setLayerInset(1, 1, 1, 1, 1);
807 d.setLayerInset(2, 2, 2, 2, 2);
808 return d;
809 }
810
John Reck5d43ce82011-06-21 17:16:51 -0700811 public boolean isLoading() {
812 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
813 }
814
815 /**
816 * Suggest to the UI that the title bar can be hidden. The UI will then
817 * decide whether or not to hide based off a number of factors, such
818 * as if the user is editing the URL bar or if the page is loading
819 */
820 public void suggestHideTitleBar() {
821 if (!isLoading() && !isEditingUrl()) {
822 hideTitleBar();
823 }
824 }
825
John Reck6ac5bfd2011-07-01 17:02:55 -0700826 private void showTitleBarForDuration() {
827 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
828 showTitleBar();
829 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
830 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
831 }
832
John Reck5d43ce82011-06-21 17:16:51 -0700833 @Override
John Reckbf2ec202011-06-29 17:47:38 -0700834 public boolean onTouch(View v, MotionEvent event) {
835 switch (event.getAction()) {
836 case MotionEvent.ACTION_DOWN:
837 mInitialY = event.getY();
838 break;
839 case MotionEvent.ACTION_MOVE:
840 WebView web = (WebView) v;
John Reck1e822eb2011-07-07 16:53:12 -0700841 if (event.getPointerCount() == 1
842 && !isTitleBarShowing()
John Reckbf2ec202011-06-29 17:47:38 -0700843 && web.getVisibleTitleHeight() == 0
844 && event.getY() > (mInitialY + mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700845 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
846 showTitleBar();
John Reckbf2ec202011-06-29 17:47:38 -0700847 } else if (event.getY() < mInitialY) {
848 mInitialY = event.getY();
849 }
850 break;
851 case MotionEvent.ACTION_CANCEL:
852 case MotionEvent.ACTION_UP:
853 if (isTitleBarShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700854 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
855 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
John Reck5d43ce82011-06-21 17:16:51 -0700856 }
John Reckbf2ec202011-06-29 17:47:38 -0700857 break;
John Reck5d43ce82011-06-21 17:16:51 -0700858 }
John Reckbf2ec202011-06-29 17:47:38 -0700859 return false;
John Reck5d43ce82011-06-21 17:16:51 -0700860 }
861
862 private Handler mHandler = new Handler() {
863
864 @Override
865 public void handleMessage(Message msg) {
866 if (msg.what == MSG_HIDE_TITLEBAR) {
867 suggestHideTitleBar();
868 }
869 }
870 };
Michael Kolb8233fac2010-10-26 16:08:53 -0700871}