blob: 9de2c5798bd2ea95f4f7643c1e866a0b236aadbb [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()
Michael Kolb0241e752011-07-07 14:58:50 -0700254 != getTitleBar().getEmbeddedHeight()
255 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700256 showTitleBarForDuration();
257 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 }
259
Michael Kolbcfa3af52010-12-14 10:36:11 -0800260 Tab getActiveTab() {
261 return mActiveTab;
262 }
263
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800265 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800266 }
267
268 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800270 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800272 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 }
275
276 @Override
277 public void detachTab(Tab tab) {
278 removeTabFromContentView(tab);
279 }
280
281 @Override
282 public void attachTab(Tab tab) {
283 attachTabToContentView(tab);
284 }
285
Michael Kolb377ea312011-02-17 14:36:56 -0800286 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800287 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700288 return;
289 }
290 View container = tab.getViewContainer();
291 WebView mainView = tab.getWebView();
292 // Attach the WebView to the container and then attach the
293 // container to the content view.
294 FrameLayout wrapper =
295 (FrameLayout) container.findViewById(R.id.webview_wrapper);
296 ViewGroup parent = (ViewGroup) mainView.getParent();
297 if (parent != wrapper) {
298 if (parent != null) {
299 Log.w(LOGTAG, "mMainView already has a parent in"
300 + " attachTabToContentView!");
301 parent.removeView(mainView);
302 }
303 wrapper.addView(mainView);
304 } else {
305 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
306 + " attachTabToContentView!");
307 }
308 parent = (ViewGroup) container.getParent();
309 if (parent != mContentView) {
310 if (parent != null) {
311 Log.w(LOGTAG, "mContainer already has a parent in"
312 + " attachTabToContentView!");
313 parent.removeView(container);
314 }
315 mContentView.addView(container, COVER_SCREEN_PARAMS);
316 } else {
317 Log.w(LOGTAG, "mContainer is already attached to content in"
318 + " attachTabToContentView!");
319 }
320 mUiController.attachSubWindow(tab);
321 }
322
323 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800324 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700325 // Remove the container that contains the main WebView.
326 WebView mainView = tab.getWebView();
327 View container = tab.getViewContainer();
328 if (mainView == null) {
329 return;
330 }
331 // Remove the container from the content and then remove the
332 // WebView from the container. This will trigger a focus change
333 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800334 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 FrameLayout wrapper =
336 (FrameLayout) container.findViewById(R.id.webview_wrapper);
337 wrapper.removeView(mainView);
338 mContentView.removeView(container);
339 mUiController.endActionMode();
340 mUiController.removeSubWindow(tab);
341 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
342 if (errorConsole != null) {
343 mErrorConsoleContainer.removeView(errorConsole);
344 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700345 }
346
Michael Kolba713ec82010-11-29 17:27:06 -0800347 @Override
348 public void onSetWebView(Tab tab, WebView webView) {
349 View container = tab.getViewContainer();
350 if (container == null) {
351 // The tab consists of a container view, which contains the main
352 // WebView, as well as any other UI elements associated with the tab.
353 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700354 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800355 tab.setViewContainer(container);
356 }
357 if (tab.getWebView() != webView) {
358 // Just remove the old one.
359 FrameLayout wrapper =
360 (FrameLayout) container.findViewById(R.id.webview_wrapper);
361 wrapper.removeView(tab.getWebView());
362 }
363 }
364
Michael Kolb8233fac2010-10-26 16:08:53 -0700365 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800366 * create a sub window container and webview for the tab
367 * Note: this methods operates through side-effects for now
368 * it sets both the subView and subViewContainer for the given tab
369 * @param tab tab to create the sub window for
370 * @param subView webview to be set as a subwindow for the tab
371 */
372 @Override
373 public void createSubWindow(Tab tab, WebView subView) {
374 View subViewContainer = mActivity.getLayoutInflater().inflate(
375 R.layout.browser_subwindow, null);
376 ViewGroup inner = (ViewGroup) subViewContainer
377 .findViewById(R.id.inner_container);
378 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
379 LayoutParams.MATCH_PARENT));
380 final ImageButton cancel = (ImageButton) subViewContainer
381 .findViewById(R.id.subwindow_close);
382 final WebView cancelSubView = subView;
383 cancel.setOnClickListener(new OnClickListener() {
384 public void onClick(View v) {
385 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
386 }
387 });
388 tab.setSubWebView(subView);
389 tab.setSubViewContainer(subViewContainer);
390 }
391
392 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 * Remove the sub window from the content view.
394 */
395 @Override
396 public void removeSubWindow(View subviewContainer) {
397 mContentView.removeView(subviewContainer);
398 mUiController.endActionMode();
399 }
400
401 /**
402 * Attach the sub window to the content view.
403 */
404 @Override
405 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800406 if (container.getParent() != null) {
407 // already attached, remove first
408 ((ViewGroup) container.getParent()).removeView(container);
409 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700410 mContentView.addView(container, COVER_SCREEN_PARAMS);
411 }
412
Michael Kolb11d19782011-03-20 10:17:40 -0700413 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700414 WebView web = getWebView();
415 if (web != null) {
416 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700417 }
418 }
419
Michael Kolb46f987e2011-04-05 10:39:10 -0700420 public void editUrl(boolean clearInput) {
421 if (mUiController.isInCustomActionMode()) {
422 mUiController.endActionMode();
423 }
424 showTitleBar();
425 getTitleBar().startEditingUrl(clearInput);
426 }
427
Michael Kolb7cdc4902011-02-03 17:54:40 -0800428 boolean canShowTitleBar() {
429 return !isTitleBarShowing()
430 && !isActivityPaused()
431 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700432 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800433 && !mUiController.isInCustomActionMode();
434 }
435
436 void showTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700437 if (canShowTitleBar()) {
438 getTitleBar().show();
439 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800440 }
441
442 protected void hideTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700443 if (getTitleBar().isShowing()) {
444 getTitleBar().hide();
445 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800446 }
447
448 protected boolean isTitleBarShowing() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700449 return getTitleBar().isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800450 }
451
John Reck5d43ce82011-06-21 17:16:51 -0700452 public boolean isEditingUrl() {
453 return getTitleBar().isEditingUrl();
454 }
455
Michael Kolb7cdc4902011-02-03 17:54:40 -0800456 protected abstract TitleBarBase getTitleBar();
457
458 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700459 WebView web = getWebView();
460 if (web != null) {
461 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700462 }
463 }
464
Michael Kolb66706532010-12-12 19:50:22 -0800465 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700466 public void showVoiceTitleBar(String title, List<String> results) {
467 getTitleBar().setInVoiceMode(true, results);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800468 getTitleBar().setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700469 }
470
Michael Kolb66706532010-12-12 19:50:22 -0800471 @Override
472 public void revertVoiceTitleBar(Tab tab) {
Michael Kolb11d19782011-03-20 10:17:40 -0700473 getTitleBar().setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800474 String url = tab.getUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800475 getTitleBar().setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700476 }
477
478 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700479 public void registerDropdownChangeListener(DropdownChangeListener d) {
480 getTitleBar().registerDropdownChangeListener(d);
481 }
482
483 @Override
John Reck2bc80422011-06-30 15:11:49 -0700484 public void showComboView(ComboViews startingView, Bundle extras) {
John Reck439c9a52010-12-14 10:04:39 -0800485 if (mComboView != null) {
486 return;
487 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700488 mComboView = new CombinedBookmarkHistoryView(mActivity,
489 mUiController,
John Reck2bc80422011-06-30 15:11:49 -0700490 startingView,
Michael Kolb8233fac2010-10-26 16:08:53 -0700491 extras);
Michael Kolb47d63f82011-01-18 10:48:40 -0800492 FrameLayout wrapper =
493 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
494 wrapper.setVisibility(View.GONE);
Michael Kolbc16c5952011-03-29 15:37:03 -0700495 getTitleBar().stopEditingUrl();
Michael Kolb3a696282010-12-05 13:23:24 -0800496 dismissIME();
Michael Kolbc16c5952011-03-29 15:37:03 -0700497 hideTitleBar();
Michael Kolb3a696282010-12-05 13:23:24 -0800498 if (mActiveTab != null) {
Michael Kolb3a696282010-12-05 13:23:24 -0800499 mActiveTab.putInBackground();
500 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700501 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
502 }
503
Michael Kolbba238702011-03-08 10:40:50 -0800504 public boolean isComboViewShowing() {
505 return (mComboView != null);
506 }
507
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 /**
509 * dismiss the ComboPage
510 */
511 @Override
512 public void hideComboView() {
513 if (mComboView != null) {
514 mContentView.removeView(mComboView);
Michael Kolb47d63f82011-01-18 10:48:40 -0800515 FrameLayout wrapper =
516 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
517 wrapper.setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 mComboView = null;
519 }
Michael Kolb3a696282010-12-05 13:23:24 -0800520 if (mActiveTab != null) {
521 mActiveTab.putInForeground();
522 }
John Reckb3417f02011-01-14 11:01:05 -0800523 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 }
525
526 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400527 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 WebChromeClient.CustomViewCallback callback) {
529 // if a view already exists then immediately terminate the new one
530 if (mCustomView != null) {
531 callback.onCustomViewHidden();
532 return;
533 }
534
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400535 mOriginalOrientation = mActivity.getRequestedOrientation();
536
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 // Add the custom view to its container.
538 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
539 mCustomView = view;
540 mCustomViewCallback = callback;
541 // Hide the content view.
542 mContentView.setVisibility(View.GONE);
543 // Finally show the custom view container.
544 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400545 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 mCustomViewContainer.setVisibility(View.VISIBLE);
547 mCustomViewContainer.bringToFront();
548 }
549
550 @Override
551 public void onHideCustomView() {
552 if (mCustomView == null)
553 return;
554
555 // Hide the custom view.
556 mCustomView.setVisibility(View.GONE);
557 // Remove the custom view from its container.
558 mCustomViewContainer.removeView(mCustomView);
559 mCustomView = null;
560 mCustomViewContainer.setVisibility(View.GONE);
561 mCustomViewCallback.onCustomViewHidden();
562 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400563 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700564 setStatusBarVisibility(true);
565 mContentView.setVisibility(View.VISIBLE);
566 }
567
568 @Override
569 public boolean isCustomViewShowing() {
570 return mCustomView != null;
571 }
572
Michael Kolb66706532010-12-12 19:50:22 -0800573 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800574 if (mInputManager.isActive()) {
575 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
576 0);
577 }
578 }
579
Michael Kolb66706532010-12-12 19:50:22 -0800580 @Override
581 public boolean showsWeb() {
582 return mCustomView == null
583 && mComboView == null;
584 }
585
Patrick Scott92066772011-03-10 08:46:27 -0500586 @Override
587 public void showAutoLogin(Tab tab) {
588 updateAutoLogin(tab, true);
589 }
590
591 @Override
592 public void hideAutoLogin(Tab tab) {
593 updateAutoLogin(tab, true);
594 }
595
Michael Kolb8233fac2010-10-26 16:08:53 -0700596 // -------------------------------------------------------------------------
597
Michael Kolb5a72f182011-01-13 20:35:06 -0800598 protected void updateNavigationState(Tab tab) {
599 }
600
Michael Kolb11d19782011-03-20 10:17:40 -0700601 protected void updateAutoLogin(Tab tab, boolean animate) {
602 getTitleBar().updateAutoLogin(tab, animate);
603 }
Patrick Scott92066772011-03-10 08:46:27 -0500604
Michael Kolb8233fac2010-10-26 16:08:53 -0700605 /**
606 * Update the lock icon to correspond to our latest state.
607 */
Michael Kolb66706532010-12-12 19:50:22 -0800608 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800609 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700610 updateLockIconImage(t.getLockIconType());
611 }
612 }
613
614 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 * Updates the lock-icon image in the title-bar.
616 */
John Reck30c714c2010-12-16 17:30:34 -0800617 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800619 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800621 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 d = mMixLockIcon;
623 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800624 getTitleBar().setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 }
626
John Reck30c714c2010-12-16 17:30:34 -0800627 protected void setUrlTitle(Tab tab) {
628 String url = tab.getUrl();
629 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800630 if (TextUtils.isEmpty(title)) {
631 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800632 }
Michael Kolb66706532010-12-12 19:50:22 -0800633 if (tab.isInVoiceSearchMode()) return;
634 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800635 getTitleBar().setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800636 }
637 }
638
639 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800640 protected void setFavicon(Tab tab) {
641 if (tab.inForeground()) {
642 Bitmap icon = tab.getFavicon();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800643 getTitleBar().setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800644 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700645 }
646
647 @Override
648 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700649 }
650
Michael Kolb66706532010-12-12 19:50:22 -0800651 // active tabs page
652
653 public void showActiveTabsPage() {
654 }
655
656 /**
657 * Remove the active tabs page.
658 */
659 public void removeActiveTabsPage() {
660 }
661
Michael Kolb8233fac2010-10-26 16:08:53 -0700662 // menu handling callbacks
663
664 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800665 public boolean onPrepareOptionsMenu(Menu menu) {
666 return true;
667 }
668
669 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 }
672
673 @Override
674 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 }
676
677 @Override
678 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 }
680
681 @Override
682 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 }
684
685 @Override
686 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 }
688
689 @Override
690 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 }
692
693 // error console
694
695 @Override
696 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800697 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
699 if (flag) {
700 // Setting the show state of the console will cause it's the layout
701 // to be inflated.
702 if (errorConsole.numberOfErrors() > 0) {
703 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
704 } else {
705 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
706 }
707 if (errorConsole.getParent() != null) {
708 mErrorConsoleContainer.removeView(errorConsole);
709 }
710 // Now we can add it to the main view.
711 mErrorConsoleContainer.addView(errorConsole,
712 new LinearLayout.LayoutParams(
713 ViewGroup.LayoutParams.MATCH_PARENT,
714 ViewGroup.LayoutParams.WRAP_CONTENT));
715 } else {
716 mErrorConsoleContainer.removeView(errorConsole);
717 }
718 }
719
720 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800721 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
722 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
723 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700724 }
725
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 // -------------------------------------------------------------------------
727 // Helper function for WebChromeClient
728 // -------------------------------------------------------------------------
729
730 @Override
731 public Bitmap getDefaultVideoPoster() {
732 if (mDefaultVideoPoster == null) {
733 mDefaultVideoPoster = BitmapFactory.decodeResource(
734 mActivity.getResources(), R.drawable.default_video_poster);
735 }
736 return mDefaultVideoPoster;
737 }
738
739 @Override
740 public View getVideoLoadingProgressView() {
741 if (mVideoProgressView == null) {
742 LayoutInflater inflater = LayoutInflater.from(mActivity);
743 mVideoProgressView = inflater.inflate(
744 R.layout.video_loading_progress, null);
745 }
746 return mVideoProgressView;
747 }
748
Michael Kolb843510f2010-12-09 10:51:49 -0800749 @Override
750 public void showMaxTabsWarning() {
751 Toast warning = Toast.makeText(mActivity,
752 mActivity.getString(R.string.max_tabs_warning),
753 Toast.LENGTH_SHORT);
754 warning.show();
755 }
756
Michael Kolbfdb70242011-03-24 09:41:11 -0700757 protected void captureTab(final Tab tab) {
758 captureTab(tab,
759 (int) mActivity.getResources()
760 .getDimension(R.dimen.qc_thumb_width),
761 (int) mActivity.getResources()
762 .getDimension(R.dimen.qc_thumb_height));
763 }
764
765 protected void captureTab(final Tab tab, int width, int height) {
766 if ((tab == null) || (tab.getWebView() == null)) return;
767 Bitmap sshot = Controller.createScreenshot(tab, width, height);
768 tab.setScreenshot(sshot);
769 }
770
Michael Kolb46f987e2011-04-05 10:39:10 -0700771 protected WebView getWebView() {
772 if (mActiveTab != null) {
773 return mActiveTab.getWebView();
774 } else {
775 return null;
776 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700777 }
778
Michael Kolbfedb4922011-04-20 16:45:33 -0700779 protected Menu getMenu() {
780 MenuBuilder menu = new MenuBuilder(mActivity);
781 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
782 return menu;
783 }
784
Michael Kolbc38c6042011-04-27 10:46:06 -0700785 public void setFullscreen(boolean enabled) {
786 if (enabled) {
787 mActivity.getWindow().setFlags(
788 WindowManager.LayoutParams.FLAG_FULLSCREEN,
789 WindowManager.LayoutParams.FLAG_FULLSCREEN);
790 } else {
791 mActivity.getWindow().clearFlags(
792 WindowManager.LayoutParams.FLAG_FULLSCREEN);
793 }
794 }
795
Michael Kolb5a4372f2011-04-29 13:53:10 -0700796 protected Drawable getFaviconDrawable(Bitmap icon) {
797 Drawable[] array = new Drawable[3];
798 array[0] = new PaintDrawable(Color.BLACK);
799 PaintDrawable p = new PaintDrawable(Color.WHITE);
800 array[1] = p;
801 if (icon == null) {
802 array[2] = mGenericFavicon;
803 } else {
804 array[2] = new BitmapDrawable(icon);
805 }
806 LayerDrawable d = new LayerDrawable(array);
807 d.setLayerInset(1, 1, 1, 1, 1);
808 d.setLayerInset(2, 2, 2, 2, 2);
809 return d;
810 }
811
John Reck5d43ce82011-06-21 17:16:51 -0700812 public boolean isLoading() {
813 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
814 }
815
816 /**
817 * Suggest to the UI that the title bar can be hidden. The UI will then
818 * decide whether or not to hide based off a number of factors, such
819 * as if the user is editing the URL bar or if the page is loading
820 */
821 public void suggestHideTitleBar() {
822 if (!isLoading() && !isEditingUrl()) {
823 hideTitleBar();
824 }
825 }
826
Michael Kolb017ffab2011-07-11 15:26:47 -0700827 protected void showTitleBarForDuration() {
John Reck6ac5bfd2011-07-01 17:02:55 -0700828 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
829 showTitleBar();
830 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
831 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
832 }
833
John Reck5d43ce82011-06-21 17:16:51 -0700834 @Override
John Reckbf2ec202011-06-29 17:47:38 -0700835 public boolean onTouch(View v, MotionEvent event) {
836 switch (event.getAction()) {
837 case MotionEvent.ACTION_DOWN:
838 mInitialY = event.getY();
839 break;
840 case MotionEvent.ACTION_MOVE:
841 WebView web = (WebView) v;
John Reck1e822eb2011-07-07 16:53:12 -0700842 if (event.getPointerCount() == 1
843 && !isTitleBarShowing()
John Reckbf2ec202011-06-29 17:47:38 -0700844 && web.getVisibleTitleHeight() == 0
845 && event.getY() > (mInitialY + mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700846 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
847 showTitleBar();
John Reckbf2ec202011-06-29 17:47:38 -0700848 } else if (event.getY() < mInitialY) {
849 mInitialY = event.getY();
850 }
851 break;
852 case MotionEvent.ACTION_CANCEL:
853 case MotionEvent.ACTION_UP:
854 if (isTitleBarShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700855 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
856 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
John Reck5d43ce82011-06-21 17:16:51 -0700857 }
John Reckbf2ec202011-06-29 17:47:38 -0700858 break;
John Reck5d43ce82011-06-21 17:16:51 -0700859 }
John Reckbf2ec202011-06-29 17:47:38 -0700860 return false;
John Reck5d43ce82011-06-21 17:16:51 -0700861 }
862
863 private Handler mHandler = new Handler() {
864
865 @Override
866 public void handleMessage(Message msg) {
867 if (msg.what == MSG_HIDE_TITLEBAR) {
868 suggestHideTitleBar();
869 }
870 }
871 };
Michael Kolb8233fac2010-10-26 16:08:53 -0700872}