blob: 1836e6e26028f87747cde455dc88a343a5111ea0 [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
John Reck74830e12011-03-14 11:43:05 -070019import android.animation.ObjectAnimator;
Michael Kolb8233fac2010-10-26 16:08:53 -070020import android.app.Activity;
John Reckb9a051b2011-03-18 11:55:48 -070021import android.content.pm.PackageManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070022import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Michael Kolb5a4372f2011-04-29 13:53:10 -070026import android.graphics.Color;
27import android.graphics.drawable.BitmapDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.graphics.drawable.Drawable;
Michael Kolb5a4372f2011-04-29 13:53:10 -070029import android.graphics.drawable.LayerDrawable;
30import android.graphics.drawable.PaintDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.os.Bundle;
John Reck5d43ce82011-06-21 17:16:51 -070032import android.os.Handler;
33import android.os.Message;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.text.TextUtils;
35import android.util.Log;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.view.Gravity;
37import android.view.LayoutInflater;
38import android.view.Menu;
John Reckbf2ec202011-06-29 17:47:38 -070039import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070040import android.view.View;
Michael Kolb1514bb72010-11-22 09:11:48 -080041import android.view.View.OnClickListener;
John Reckbf2ec202011-06-29 17:47:38 -070042import android.view.View.OnTouchListener;
43import android.view.ViewConfiguration;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080045import android.view.ViewGroup.LayoutParams;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.view.WindowManager;
Michael Kolb3a696282010-12-05 13:23:24 -080047import android.view.inputmethod.InputMethodManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.webkit.WebChromeClient;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.webkit.WebView;
50import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080051import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.widget.LinearLayout;
53import android.widget.Toast;
54
John Reckbf2ec202011-06-29 17:47:38 -070055import com.android.browser.Tab.LockIcon;
56import com.android.internal.view.menu.MenuBuilder;
57
Michael Kolb1bf23132010-11-19 12:55:12 -080058import java.util.List;
59
Michael Kolb8233fac2010-10-26 16:08:53 -070060/**
61 * UI interface definitions
62 */
Michael Kolb14612442011-06-24 13:06:29 -070063public abstract class BaseUi implements UI, OnTouchListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070064
65 private static final String LOGTAG = "BaseUi";
66
Michael Kolb66706532010-12-12 19:50:22 -080067 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070068 new FrameLayout.LayoutParams(
69 ViewGroup.LayoutParams.MATCH_PARENT,
70 ViewGroup.LayoutParams.MATCH_PARENT);
71
Michael Kolb66706532010-12-12 19:50:22 -080072 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070073 new FrameLayout.LayoutParams(
74 ViewGroup.LayoutParams.MATCH_PARENT,
75 ViewGroup.LayoutParams.MATCH_PARENT,
76 Gravity.CENTER);
77
John Reck5d43ce82011-06-21 17:16:51 -070078 private static final int MSG_HIDE_TITLEBAR = 1;
79 private static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
80
Michael Kolb8233fac2010-10-26 16:08:53 -070081 Activity mActivity;
82 UiController mUiController;
83 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080084 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080085 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070086
87 private Drawable mSecLockIcon;
88 private Drawable mMixLockIcon;
Michael Kolb5a4372f2011-04-29 13:53:10 -070089 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070090
Michael Kolb8233fac2010-10-26 16:08:53 -070091 private FrameLayout mBrowserFrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080092 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070093 protected FrameLayout mCustomViewContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
95 private View mCustomView;
96 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040097 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070098
99 private CombinedBookmarkHistoryView mComboView;
100
101 private LinearLayout mErrorConsoleContainer = null;
102
103 private Toast mStopToast;
Michael Kolb8233fac2010-10-26 16:08:53 -0700104
John Reckbf2ec202011-06-29 17:47:38 -0700105 private float mInitialY;
John Reck5d43ce82011-06-21 17:16:51 -0700106 private int mTitlebarScrollTriggerSlop;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700107
Michael Kolb8233fac2010-10-26 16:08:53 -0700108 // the default <video> poster
109 private Bitmap mDefaultVideoPoster;
110 // the video progress view
111 private View mVideoProgressView;
112
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700114 protected boolean mUseQuickControls;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115
116 public BaseUi(Activity browser, UiController controller) {
117 mActivity = browser;
118 mUiController = controller;
119 mTabControl = controller.getTabControl();
120 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800121 mInputManager = (InputMethodManager)
122 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Michael Kolb5a72f182011-01-13 20:35:06 -0800123 mSecLockIcon = res.getDrawable(R.drawable.ic_secure_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700124 mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure);
125
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
127 .getDecorView().findViewById(android.R.id.content);
128 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(mActivity)
129 .inflate(R.layout.custom_screen, null);
130 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
131 R.id.main_content);
132 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
133 .findViewById(R.id.error_console);
134 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
135 .findViewById(R.id.fullscreen_custom_content);
136 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Michael Kolbc38c6042011-04-27 10:46:06 -0700137 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700138 mGenericFavicon = res.getDrawable(
139 R.drawable.app_web_browser_sm);
John Reck5d43ce82011-06-21 17:16:51 -0700140 ViewConfiguration config = ViewConfiguration.get(browser);
141 mTitlebarScrollTriggerSlop = Math.max(
142 config.getScaledOverflingDistance(),
143 config.getScaledOverscrollDistance());
144 mTitlebarScrollTriggerSlop = Math.max(mTitlebarScrollTriggerSlop,
145 config.getScaledTouchSlop());
Michael Kolb8233fac2010-10-26 16:08:53 -0700146 }
147
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 private void cancelStopToast() {
149 if (mStopToast != null) {
150 mStopToast.cancel();
151 mStopToast = null;
152 }
153 }
154
155 // lifecycle
156
157 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800158 if (isCustomViewShowing()) {
159 onHideCustomView();
160 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 cancelStopToast();
162 mActivityPaused = true;
163 }
164
165 public void onResume() {
166 mActivityPaused = false;
167 }
168
Michael Kolb66706532010-12-12 19:50:22 -0800169 protected boolean isActivityPaused() {
170 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700171 }
172
173 public void onConfigurationChanged(Configuration config) {
174 }
175
176 // key handling
177
178 @Override
179 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700180 if (mComboView != null) {
181 if (!mComboView.onBackPressed()) {
182 mUiController.removeComboView();
183 }
184 return true;
185 }
186 if (mCustomView != null) {
187 mUiController.hideCustomView();
188 return true;
189 }
190 return false;
191 }
192
Michael Kolb2814a362011-05-19 15:49:41 -0700193 @Override
194 public boolean onMenuKey() {
195 return false;
196 }
197
John Reck30c714c2010-12-16 17:30:34 -0800198 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700199 @Override
John Reck30c714c2010-12-16 17:30:34 -0800200 public void onTabDataChanged(Tab tab) {
201 setUrlTitle(tab);
202 setFavicon(tab);
203 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800204 updateNavigationState(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 }
206
207 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500208 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800209 if (tab.inForeground()) {
210 boolean isBookmark = tab.isBookmarkedSite();
211 getTitleBar().setCurrentUrlIsBookmark(isBookmark);
212 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500213 }
214
215 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700216 public void onPageStopped(Tab tab) {
217 cancelStopToast();
218 if (tab.inForeground()) {
219 mStopToast = Toast
220 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
221 mStopToast.show();
222 }
223 }
224
225 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800226 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700227 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800228 }
229
230 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 }
233
234 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800235 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700236 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800237 if ((tab != mActiveTab) && (mActiveTab != null)) {
238 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700239 WebView web = mActiveTab.getWebView();
240 if (web != null) {
241 web.setOnTouchListener(null);
242 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 }
Michael Kolb77df4562010-11-19 14:49:34 -0800244 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700245 WebView web = mActiveTab.getWebView();
246 if (web != null && !mUseQuickControls) {
247 web.setOnTouchListener(this);
248 }
John Reck5d43ce82011-06-21 17:16:51 -0700249 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800251 onTabDataChanged(tab);
252 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800253 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800254 getTitleBar().setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500255 updateAutoLogin(tab, false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 }
257
Michael Kolbcfa3af52010-12-14 10:36:11 -0800258 Tab getActiveTab() {
259 return mActiveTab;
260 }
261
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800263 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800264 }
265
266 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800268 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800270 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 }
273
274 @Override
275 public void detachTab(Tab tab) {
276 removeTabFromContentView(tab);
277 }
278
279 @Override
280 public void attachTab(Tab tab) {
281 attachTabToContentView(tab);
282 }
283
Michael Kolb377ea312011-02-17 14:36:56 -0800284 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800285 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 return;
287 }
288 View container = tab.getViewContainer();
289 WebView mainView = tab.getWebView();
290 // Attach the WebView to the container and then attach the
291 // container to the content view.
292 FrameLayout wrapper =
293 (FrameLayout) container.findViewById(R.id.webview_wrapper);
294 ViewGroup parent = (ViewGroup) mainView.getParent();
295 if (parent != wrapper) {
296 if (parent != null) {
297 Log.w(LOGTAG, "mMainView already has a parent in"
298 + " attachTabToContentView!");
299 parent.removeView(mainView);
300 }
301 wrapper.addView(mainView);
302 } else {
303 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
304 + " attachTabToContentView!");
305 }
306 parent = (ViewGroup) container.getParent();
307 if (parent != mContentView) {
308 if (parent != null) {
309 Log.w(LOGTAG, "mContainer already has a parent in"
310 + " attachTabToContentView!");
311 parent.removeView(container);
312 }
313 mContentView.addView(container, COVER_SCREEN_PARAMS);
314 } else {
315 Log.w(LOGTAG, "mContainer is already attached to content in"
316 + " attachTabToContentView!");
317 }
318 mUiController.attachSubWindow(tab);
319 }
320
321 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800322 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700323 // Remove the container that contains the main WebView.
324 WebView mainView = tab.getWebView();
325 View container = tab.getViewContainer();
326 if (mainView == null) {
327 return;
328 }
329 // Remove the container from the content and then remove the
330 // WebView from the container. This will trigger a focus change
331 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800332 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700333 FrameLayout wrapper =
334 (FrameLayout) container.findViewById(R.id.webview_wrapper);
335 wrapper.removeView(mainView);
336 mContentView.removeView(container);
337 mUiController.endActionMode();
338 mUiController.removeSubWindow(tab);
339 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
340 if (errorConsole != null) {
341 mErrorConsoleContainer.removeView(errorConsole);
342 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 }
344
Michael Kolba713ec82010-11-29 17:27:06 -0800345 @Override
346 public void onSetWebView(Tab tab, WebView webView) {
347 View container = tab.getViewContainer();
348 if (container == null) {
349 // The tab consists of a container view, which contains the main
350 // WebView, as well as any other UI elements associated with the tab.
351 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
352 null);
353 tab.setViewContainer(container);
354 }
355 if (tab.getWebView() != webView) {
356 // Just remove the old one.
357 FrameLayout wrapper =
358 (FrameLayout) container.findViewById(R.id.webview_wrapper);
359 wrapper.removeView(tab.getWebView());
360 }
361 }
362
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800364 * create a sub window container and webview for the tab
365 * Note: this methods operates through side-effects for now
366 * it sets both the subView and subViewContainer for the given tab
367 * @param tab tab to create the sub window for
368 * @param subView webview to be set as a subwindow for the tab
369 */
370 @Override
371 public void createSubWindow(Tab tab, WebView subView) {
372 View subViewContainer = mActivity.getLayoutInflater().inflate(
373 R.layout.browser_subwindow, null);
374 ViewGroup inner = (ViewGroup) subViewContainer
375 .findViewById(R.id.inner_container);
376 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
377 LayoutParams.MATCH_PARENT));
378 final ImageButton cancel = (ImageButton) subViewContainer
379 .findViewById(R.id.subwindow_close);
380 final WebView cancelSubView = subView;
381 cancel.setOnClickListener(new OnClickListener() {
382 public void onClick(View v) {
383 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
384 }
385 });
386 tab.setSubWebView(subView);
387 tab.setSubViewContainer(subViewContainer);
388 }
389
390 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700391 * Remove the sub window from the content view.
392 */
393 @Override
394 public void removeSubWindow(View subviewContainer) {
395 mContentView.removeView(subviewContainer);
396 mUiController.endActionMode();
397 }
398
399 /**
400 * Attach the sub window to the content view.
401 */
402 @Override
403 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800404 if (container.getParent() != null) {
405 // already attached, remove first
406 ((ViewGroup) container.getParent()).removeView(container);
407 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700408 mContentView.addView(container, COVER_SCREEN_PARAMS);
409 }
410
Michael Kolb11d19782011-03-20 10:17:40 -0700411 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700412 WebView web = getWebView();
413 if (web != null) {
414 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700415 }
416 }
417
Michael Kolb46f987e2011-04-05 10:39:10 -0700418 public void editUrl(boolean clearInput) {
419 if (mUiController.isInCustomActionMode()) {
420 mUiController.endActionMode();
421 }
422 showTitleBar();
423 getTitleBar().startEditingUrl(clearInput);
424 }
425
Michael Kolb7cdc4902011-02-03 17:54:40 -0800426 boolean canShowTitleBar() {
427 return !isTitleBarShowing()
428 && !isActivityPaused()
429 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700430 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800431 && !mUiController.isInCustomActionMode();
432 }
433
434 void showTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700435 if (canShowTitleBar()) {
436 getTitleBar().show();
437 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800438 }
439
440 protected void hideTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700441 if (getTitleBar().isShowing()) {
442 getTitleBar().hide();
443 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800444 }
445
446 protected boolean isTitleBarShowing() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700447 return getTitleBar().isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800448 }
449
John Reck5d43ce82011-06-21 17:16:51 -0700450 public boolean isEditingUrl() {
451 return getTitleBar().isEditingUrl();
452 }
453
Michael Kolb7cdc4902011-02-03 17:54:40 -0800454 protected abstract TitleBarBase getTitleBar();
455
456 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700457 WebView web = getWebView();
458 if (web != null) {
459 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700460 }
461 }
462
Michael Kolb66706532010-12-12 19:50:22 -0800463 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700464 public void showVoiceTitleBar(String title, List<String> results) {
465 getTitleBar().setInVoiceMode(true, results);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800466 getTitleBar().setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700467 }
468
Michael Kolb66706532010-12-12 19:50:22 -0800469 @Override
470 public void revertVoiceTitleBar(Tab tab) {
Michael Kolb11d19782011-03-20 10:17:40 -0700471 getTitleBar().setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800472 String url = tab.getUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800473 getTitleBar().setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 }
475
476 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700477 public void registerDropdownChangeListener(DropdownChangeListener d) {
478 getTitleBar().registerDropdownChangeListener(d);
479 }
480
481 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700482 public void showComboView(boolean startWithHistory, Bundle extras) {
John Reck439c9a52010-12-14 10:04:39 -0800483 if (mComboView != null) {
484 return;
485 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700486 mComboView = new CombinedBookmarkHistoryView(mActivity,
487 mUiController,
488 startWithHistory ?
489 CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY
490 : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS,
491 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 }
John Reck74830e12011-03-14 11:43:05 -0700501 mComboView.setAlpha(0f);
502 ObjectAnimator anim = ObjectAnimator.ofFloat(mComboView, "alpha", 0f, 1f);
503 Resources res = mActivity.getResources();
504 anim.setDuration(res.getInteger(R.integer.comboViewFadeInDuration));
505 anim.start();
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
507 }
508
Michael Kolbba238702011-03-08 10:40:50 -0800509 public boolean isComboViewShowing() {
510 return (mComboView != null);
511 }
512
Michael Kolb8233fac2010-10-26 16:08:53 -0700513 /**
514 * dismiss the ComboPage
515 */
516 @Override
517 public void hideComboView() {
518 if (mComboView != null) {
519 mContentView.removeView(mComboView);
Michael Kolb47d63f82011-01-18 10:48:40 -0800520 FrameLayout wrapper =
521 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
522 wrapper.setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 mComboView = null;
524 }
Michael Kolb3a696282010-12-05 13:23:24 -0800525 if (mActiveTab != null) {
526 mActiveTab.putInForeground();
527 }
John Reckb3417f02011-01-14 11:01:05 -0800528 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 }
530
531 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400532 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 WebChromeClient.CustomViewCallback callback) {
534 // if a view already exists then immediately terminate the new one
535 if (mCustomView != null) {
536 callback.onCustomViewHidden();
537 return;
538 }
539
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400540 mOriginalOrientation = mActivity.getRequestedOrientation();
541
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 // Add the custom view to its container.
543 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
544 mCustomView = view;
545 mCustomViewCallback = callback;
546 // Hide the content view.
547 mContentView.setVisibility(View.GONE);
548 // Finally show the custom view container.
549 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400550 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700551 mCustomViewContainer.setVisibility(View.VISIBLE);
552 mCustomViewContainer.bringToFront();
553 }
554
555 @Override
556 public void onHideCustomView() {
557 if (mCustomView == null)
558 return;
559
560 // Hide the custom view.
561 mCustomView.setVisibility(View.GONE);
562 // Remove the custom view from its container.
563 mCustomViewContainer.removeView(mCustomView);
564 mCustomView = null;
565 mCustomViewContainer.setVisibility(View.GONE);
566 mCustomViewCallback.onCustomViewHidden();
567 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400568 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 setStatusBarVisibility(true);
570 mContentView.setVisibility(View.VISIBLE);
571 }
572
573 @Override
574 public boolean isCustomViewShowing() {
575 return mCustomView != null;
576 }
577
Michael Kolb66706532010-12-12 19:50:22 -0800578 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800579 if (mInputManager.isActive()) {
580 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
581 0);
582 }
583 }
584
Michael Kolb66706532010-12-12 19:50:22 -0800585 @Override
586 public boolean showsWeb() {
587 return mCustomView == null
588 && mComboView == null;
589 }
590
Patrick Scott92066772011-03-10 08:46:27 -0500591 @Override
592 public void showAutoLogin(Tab tab) {
593 updateAutoLogin(tab, true);
594 }
595
596 @Override
597 public void hideAutoLogin(Tab tab) {
598 updateAutoLogin(tab, true);
599 }
600
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 // -------------------------------------------------------------------------
602
Michael Kolb5a72f182011-01-13 20:35:06 -0800603 protected void updateNavigationState(Tab tab) {
604 }
605
Michael Kolb11d19782011-03-20 10:17:40 -0700606 protected void updateAutoLogin(Tab tab, boolean animate) {
607 getTitleBar().updateAutoLogin(tab, animate);
608 }
Patrick Scott92066772011-03-10 08:46:27 -0500609
Michael Kolb8233fac2010-10-26 16:08:53 -0700610 /**
611 * Update the lock icon to correspond to our latest state.
612 */
Michael Kolb66706532010-12-12 19:50:22 -0800613 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800614 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 updateLockIconImage(t.getLockIconType());
616 }
617 }
618
619 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 * Updates the lock-icon image in the title-bar.
621 */
John Reck30c714c2010-12-16 17:30:34 -0800622 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700623 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800624 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800626 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 d = mMixLockIcon;
628 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800629 getTitleBar().setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 }
631
John Reck30c714c2010-12-16 17:30:34 -0800632 protected void setUrlTitle(Tab tab) {
633 String url = tab.getUrl();
634 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800635 if (TextUtils.isEmpty(title)) {
636 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800637 }
Michael Kolb66706532010-12-12 19:50:22 -0800638 if (tab.isInVoiceSearchMode()) return;
639 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800640 getTitleBar().setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800641 }
642 }
643
644 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800645 protected void setFavicon(Tab tab) {
646 if (tab.inForeground()) {
647 Bitmap icon = tab.getFavicon();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800648 getTitleBar().setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800649 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700650 }
651
652 @Override
653 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700654 }
655
Michael Kolb66706532010-12-12 19:50:22 -0800656 // active tabs page
657
658 public void showActiveTabsPage() {
659 }
660
661 /**
662 * Remove the active tabs page.
663 */
664 public void removeActiveTabsPage() {
665 }
666
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 // menu handling callbacks
668
669 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800670 public boolean onPrepareOptionsMenu(Menu menu) {
671 return true;
672 }
673
674 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 }
677
678 @Override
679 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 }
681
682 @Override
683 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 }
685
686 @Override
687 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 }
689
690 @Override
691 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 }
693
694 @Override
695 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 }
697
698 // error console
699
700 @Override
701 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800702 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
704 if (flag) {
705 // Setting the show state of the console will cause it's the layout
706 // to be inflated.
707 if (errorConsole.numberOfErrors() > 0) {
708 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
709 } else {
710 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
711 }
712 if (errorConsole.getParent() != null) {
713 mErrorConsoleContainer.removeView(errorConsole);
714 }
715 // Now we can add it to the main view.
716 mErrorConsoleContainer.addView(errorConsole,
717 new LinearLayout.LayoutParams(
718 ViewGroup.LayoutParams.MATCH_PARENT,
719 ViewGroup.LayoutParams.WRAP_CONTENT));
720 } else {
721 mErrorConsoleContainer.removeView(errorConsole);
722 }
723 }
724
725 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800726 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
727 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
728 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 }
730
Michael Kolb8233fac2010-10-26 16:08:53 -0700731 // -------------------------------------------------------------------------
732 // Helper function for WebChromeClient
733 // -------------------------------------------------------------------------
734
735 @Override
736 public Bitmap getDefaultVideoPoster() {
737 if (mDefaultVideoPoster == null) {
738 mDefaultVideoPoster = BitmapFactory.decodeResource(
739 mActivity.getResources(), R.drawable.default_video_poster);
740 }
741 return mDefaultVideoPoster;
742 }
743
744 @Override
745 public View getVideoLoadingProgressView() {
746 if (mVideoProgressView == null) {
747 LayoutInflater inflater = LayoutInflater.from(mActivity);
748 mVideoProgressView = inflater.inflate(
749 R.layout.video_loading_progress, null);
750 }
751 return mVideoProgressView;
752 }
753
Michael Kolb843510f2010-12-09 10:51:49 -0800754 @Override
755 public void showMaxTabsWarning() {
756 Toast warning = Toast.makeText(mActivity,
757 mActivity.getString(R.string.max_tabs_warning),
758 Toast.LENGTH_SHORT);
759 warning.show();
760 }
761
Michael Kolbfdb70242011-03-24 09:41:11 -0700762 protected void captureTab(final Tab tab) {
763 captureTab(tab,
764 (int) mActivity.getResources()
765 .getDimension(R.dimen.qc_thumb_width),
766 (int) mActivity.getResources()
767 .getDimension(R.dimen.qc_thumb_height));
768 }
769
770 protected void captureTab(final Tab tab, int width, int height) {
771 if ((tab == null) || (tab.getWebView() == null)) return;
772 Bitmap sshot = Controller.createScreenshot(tab, width, height);
773 tab.setScreenshot(sshot);
774 }
775
Michael Kolb46f987e2011-04-05 10:39:10 -0700776 protected WebView getWebView() {
777 if (mActiveTab != null) {
778 return mActiveTab.getWebView();
779 } else {
780 return null;
781 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700782 }
783
Michael Kolbfedb4922011-04-20 16:45:33 -0700784 protected Menu getMenu() {
785 MenuBuilder menu = new MenuBuilder(mActivity);
786 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
787 return menu;
788 }
789
Michael Kolbc38c6042011-04-27 10:46:06 -0700790 public void setFullscreen(boolean enabled) {
791 if (enabled) {
792 mActivity.getWindow().setFlags(
793 WindowManager.LayoutParams.FLAG_FULLSCREEN,
794 WindowManager.LayoutParams.FLAG_FULLSCREEN);
795 } else {
796 mActivity.getWindow().clearFlags(
797 WindowManager.LayoutParams.FLAG_FULLSCREEN);
798 }
799 }
800
Michael Kolb5a4372f2011-04-29 13:53:10 -0700801 protected Drawable getFaviconDrawable(Bitmap icon) {
802 Drawable[] array = new Drawable[3];
803 array[0] = new PaintDrawable(Color.BLACK);
804 PaintDrawable p = new PaintDrawable(Color.WHITE);
805 array[1] = p;
806 if (icon == null) {
807 array[2] = mGenericFavicon;
808 } else {
809 array[2] = new BitmapDrawable(icon);
810 }
811 LayerDrawable d = new LayerDrawable(array);
812 d.setLayerInset(1, 1, 1, 1, 1);
813 d.setLayerInset(2, 2, 2, 2, 2);
814 return d;
815 }
816
John Reck5d43ce82011-06-21 17:16:51 -0700817 public boolean isLoading() {
818 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
819 }
820
821 /**
822 * Suggest to the UI that the title bar can be hidden. The UI will then
823 * decide whether or not to hide based off a number of factors, such
824 * as if the user is editing the URL bar or if the page is loading
825 */
826 public void suggestHideTitleBar() {
827 if (!isLoading() && !isEditingUrl()) {
828 hideTitleBar();
829 }
830 }
831
832 @Override
John Reckbf2ec202011-06-29 17:47:38 -0700833 public boolean onTouch(View v, MotionEvent event) {
834 switch (event.getAction()) {
835 case MotionEvent.ACTION_DOWN:
836 mInitialY = event.getY();
837 break;
838 case MotionEvent.ACTION_MOVE:
839 WebView web = (WebView) v;
840 if (!isTitleBarShowing()
841 && web.getVisibleTitleHeight() == 0
842 && event.getY() > (mInitialY + mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700843 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
844 showTitleBar();
John Reckbf2ec202011-06-29 17:47:38 -0700845 } else if (event.getY() < mInitialY) {
846 mInitialY = event.getY();
847 }
848 break;
849 case MotionEvent.ACTION_CANCEL:
850 case MotionEvent.ACTION_UP:
851 if (isTitleBarShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700852 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
853 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
John Reck5d43ce82011-06-21 17:16:51 -0700854 }
John Reckbf2ec202011-06-29 17:47:38 -0700855 break;
John Reck5d43ce82011-06-21 17:16:51 -0700856 }
John Reckbf2ec202011-06-29 17:47:38 -0700857 return false;
John Reck5d43ce82011-06-21 17:16:51 -0700858 }
859
860 private Handler mHandler = new Handler() {
861
862 @Override
863 public void handleMessage(Message msg) {
864 if (msg.what == MSG_HIDE_TITLEBAR) {
865 suggestHideTitleBar();
866 }
867 }
868 };
Michael Kolb8233fac2010-10-26 16:08:53 -0700869}