blob: e577b1b6b9715c1ca7dbce8210fee9611f22075b [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 Kolb66706532010-12-12 19:50:22 -080091 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070092 protected FrameLayout mCustomViewContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070093
94 private View mCustomView;
95 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040096 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070097
98 private CombinedBookmarkHistoryView mComboView;
99
100 private LinearLayout mErrorConsoleContainer = null;
101
102 private Toast mStopToast;
Michael Kolb8233fac2010-10-26 16:08:53 -0700103
John Reckbf2ec202011-06-29 17:47:38 -0700104 private float mInitialY;
John Reck5d43ce82011-06-21 17:16:51 -0700105 private int mTitlebarScrollTriggerSlop;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700106
Michael Kolb8233fac2010-10-26 16:08:53 -0700107 // the default <video> poster
108 private Bitmap mDefaultVideoPoster;
109 // the video progress view
110 private View mVideoProgressView;
111
Michael Kolb8233fac2010-10-26 16:08:53 -0700112 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700113 protected boolean mUseQuickControls;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114
115 public BaseUi(Activity browser, UiController controller) {
116 mActivity = browser;
117 mUiController = controller;
118 mTabControl = controller.getTabControl();
119 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800120 mInputManager = (InputMethodManager)
121 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Michael Kolb5a72f182011-01-13 20:35:06 -0800122 mSecLockIcon = res.getDrawable(R.drawable.ic_secure_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure);
124
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
126 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700127 LayoutInflater.from(mActivity)
128 .inflate(R.layout.custom_screen, frameLayout);
129 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 R.id.main_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700131 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 .findViewById(R.id.error_console);
John Reck7c6e1c92011-06-30 11:55:55 -0700133 mCustomViewContainer = (FrameLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 .findViewById(R.id.fullscreen_custom_content);
Michael Kolbc38c6042011-04-27 10:46:06 -0700135 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700136 mGenericFavicon = res.getDrawable(
137 R.drawable.app_web_browser_sm);
John Reck5d43ce82011-06-21 17:16:51 -0700138 ViewConfiguration config = ViewConfiguration.get(browser);
139 mTitlebarScrollTriggerSlop = Math.max(
140 config.getScaledOverflingDistance(),
141 config.getScaledOverscrollDistance());
142 mTitlebarScrollTriggerSlop = Math.max(mTitlebarScrollTriggerSlop,
143 config.getScaledTouchSlop());
Michael Kolb8233fac2010-10-26 16:08:53 -0700144 }
145
Michael Kolb8233fac2010-10-26 16:08:53 -0700146 private void cancelStopToast() {
147 if (mStopToast != null) {
148 mStopToast.cancel();
149 mStopToast = null;
150 }
151 }
152
153 // lifecycle
154
155 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800156 if (isCustomViewShowing()) {
157 onHideCustomView();
158 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 cancelStopToast();
160 mActivityPaused = true;
161 }
162
163 public void onResume() {
164 mActivityPaused = false;
165 }
166
Michael Kolb66706532010-12-12 19:50:22 -0800167 protected boolean isActivityPaused() {
168 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 }
170
171 public void onConfigurationChanged(Configuration config) {
172 }
173
174 // key handling
175
176 @Override
177 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700178 if (mComboView != null) {
179 if (!mComboView.onBackPressed()) {
180 mUiController.removeComboView();
181 }
182 return true;
183 }
184 if (mCustomView != null) {
185 mUiController.hideCustomView();
186 return true;
187 }
188 return false;
189 }
190
Michael Kolb2814a362011-05-19 15:49:41 -0700191 @Override
192 public boolean onMenuKey() {
193 return false;
194 }
195
John Reck30c714c2010-12-16 17:30:34 -0800196 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 @Override
John Reck30c714c2010-12-16 17:30:34 -0800198 public void onTabDataChanged(Tab tab) {
199 setUrlTitle(tab);
200 setFavicon(tab);
201 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800202 updateNavigationState(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 }
204
205 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500206 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800207 if (tab.inForeground()) {
208 boolean isBookmark = tab.isBookmarkedSite();
209 getTitleBar().setCurrentUrlIsBookmark(isBookmark);
210 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500211 }
212
213 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 public void onPageStopped(Tab tab) {
215 cancelStopToast();
216 if (tab.inForeground()) {
217 mStopToast = Toast
218 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
219 mStopToast.show();
220 }
221 }
222
223 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800224 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700225 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800226 }
227
228 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 }
231
232 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800233 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700234 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800235 if ((tab != mActiveTab) && (mActiveTab != null)) {
236 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700237 WebView web = mActiveTab.getWebView();
238 if (web != null) {
239 web.setOnTouchListener(null);
240 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700241 }
Michael Kolb77df4562010-11-19 14:49:34 -0800242 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700243 WebView web = mActiveTab.getWebView();
244 if (web != null && !mUseQuickControls) {
245 web.setOnTouchListener(this);
246 }
John Reck5d43ce82011-06-21 17:16:51 -0700247 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800249 onTabDataChanged(tab);
250 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800251 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800252 getTitleBar().setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500253 updateAutoLogin(tab, false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700254 }
255
Michael Kolbcfa3af52010-12-14 10:36:11 -0800256 Tab getActiveTab() {
257 return mActiveTab;
258 }
259
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800261 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800262 }
263
264 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800266 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800268 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 }
271
272 @Override
273 public void detachTab(Tab tab) {
274 removeTabFromContentView(tab);
275 }
276
277 @Override
278 public void attachTab(Tab tab) {
279 attachTabToContentView(tab);
280 }
281
Michael Kolb377ea312011-02-17 14:36:56 -0800282 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800283 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700284 return;
285 }
286 View container = tab.getViewContainer();
287 WebView mainView = tab.getWebView();
288 // Attach the WebView to the container and then attach the
289 // container to the content view.
290 FrameLayout wrapper =
291 (FrameLayout) container.findViewById(R.id.webview_wrapper);
292 ViewGroup parent = (ViewGroup) mainView.getParent();
293 if (parent != wrapper) {
294 if (parent != null) {
295 Log.w(LOGTAG, "mMainView already has a parent in"
296 + " attachTabToContentView!");
297 parent.removeView(mainView);
298 }
299 wrapper.addView(mainView);
300 } else {
301 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
302 + " attachTabToContentView!");
303 }
304 parent = (ViewGroup) container.getParent();
305 if (parent != mContentView) {
306 if (parent != null) {
307 Log.w(LOGTAG, "mContainer already has a parent in"
308 + " attachTabToContentView!");
309 parent.removeView(container);
310 }
311 mContentView.addView(container, COVER_SCREEN_PARAMS);
312 } else {
313 Log.w(LOGTAG, "mContainer is already attached to content in"
314 + " attachTabToContentView!");
315 }
316 mUiController.attachSubWindow(tab);
317 }
318
319 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800320 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700321 // Remove the container that contains the main WebView.
322 WebView mainView = tab.getWebView();
323 View container = tab.getViewContainer();
324 if (mainView == null) {
325 return;
326 }
327 // Remove the container from the content and then remove the
328 // WebView from the container. This will trigger a focus change
329 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800330 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700331 FrameLayout wrapper =
332 (FrameLayout) container.findViewById(R.id.webview_wrapper);
333 wrapper.removeView(mainView);
334 mContentView.removeView(container);
335 mUiController.endActionMode();
336 mUiController.removeSubWindow(tab);
337 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
338 if (errorConsole != null) {
339 mErrorConsoleContainer.removeView(errorConsole);
340 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 }
342
Michael Kolba713ec82010-11-29 17:27:06 -0800343 @Override
344 public void onSetWebView(Tab tab, WebView webView) {
345 View container = tab.getViewContainer();
346 if (container == null) {
347 // The tab consists of a container view, which contains the main
348 // WebView, as well as any other UI elements associated with the tab.
349 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700350 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800351 tab.setViewContainer(container);
352 }
353 if (tab.getWebView() != webView) {
354 // Just remove the old one.
355 FrameLayout wrapper =
356 (FrameLayout) container.findViewById(R.id.webview_wrapper);
357 wrapper.removeView(tab.getWebView());
358 }
359 }
360
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800362 * create a sub window container and webview for the tab
363 * Note: this methods operates through side-effects for now
364 * it sets both the subView and subViewContainer for the given tab
365 * @param tab tab to create the sub window for
366 * @param subView webview to be set as a subwindow for the tab
367 */
368 @Override
369 public void createSubWindow(Tab tab, WebView subView) {
370 View subViewContainer = mActivity.getLayoutInflater().inflate(
371 R.layout.browser_subwindow, null);
372 ViewGroup inner = (ViewGroup) subViewContainer
373 .findViewById(R.id.inner_container);
374 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
375 LayoutParams.MATCH_PARENT));
376 final ImageButton cancel = (ImageButton) subViewContainer
377 .findViewById(R.id.subwindow_close);
378 final WebView cancelSubView = subView;
379 cancel.setOnClickListener(new OnClickListener() {
380 public void onClick(View v) {
381 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
382 }
383 });
384 tab.setSubWebView(subView);
385 tab.setSubViewContainer(subViewContainer);
386 }
387
388 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700389 * Remove the sub window from the content view.
390 */
391 @Override
392 public void removeSubWindow(View subviewContainer) {
393 mContentView.removeView(subviewContainer);
394 mUiController.endActionMode();
395 }
396
397 /**
398 * Attach the sub window to the content view.
399 */
400 @Override
401 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800402 if (container.getParent() != null) {
403 // already attached, remove first
404 ((ViewGroup) container.getParent()).removeView(container);
405 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700406 mContentView.addView(container, COVER_SCREEN_PARAMS);
407 }
408
Michael Kolb11d19782011-03-20 10:17:40 -0700409 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700410 WebView web = getWebView();
411 if (web != null) {
412 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700413 }
414 }
415
Michael Kolb46f987e2011-04-05 10:39:10 -0700416 public void editUrl(boolean clearInput) {
417 if (mUiController.isInCustomActionMode()) {
418 mUiController.endActionMode();
419 }
420 showTitleBar();
421 getTitleBar().startEditingUrl(clearInput);
422 }
423
Michael Kolb7cdc4902011-02-03 17:54:40 -0800424 boolean canShowTitleBar() {
425 return !isTitleBarShowing()
426 && !isActivityPaused()
427 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700428 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800429 && !mUiController.isInCustomActionMode();
430 }
431
432 void showTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700433 if (canShowTitleBar()) {
434 getTitleBar().show();
435 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800436 }
437
438 protected void hideTitleBar() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700439 if (getTitleBar().isShowing()) {
440 getTitleBar().hide();
441 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800442 }
443
444 protected boolean isTitleBarShowing() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700445 return getTitleBar().isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800446 }
447
John Reck5d43ce82011-06-21 17:16:51 -0700448 public boolean isEditingUrl() {
449 return getTitleBar().isEditingUrl();
450 }
451
Michael Kolb7cdc4902011-02-03 17:54:40 -0800452 protected abstract TitleBarBase getTitleBar();
453
454 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700455 WebView web = getWebView();
456 if (web != null) {
457 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700458 }
459 }
460
Michael Kolb66706532010-12-12 19:50:22 -0800461 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700462 public void showVoiceTitleBar(String title, List<String> results) {
463 getTitleBar().setInVoiceMode(true, results);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800464 getTitleBar().setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700465 }
466
Michael Kolb66706532010-12-12 19:50:22 -0800467 @Override
468 public void revertVoiceTitleBar(Tab tab) {
Michael Kolb11d19782011-03-20 10:17:40 -0700469 getTitleBar().setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800470 String url = tab.getUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800471 getTitleBar().setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700472 }
473
474 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700475 public void registerDropdownChangeListener(DropdownChangeListener d) {
476 getTitleBar().registerDropdownChangeListener(d);
477 }
478
479 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700480 public void showComboView(boolean startWithHistory, Bundle extras) {
John Reck439c9a52010-12-14 10:04:39 -0800481 if (mComboView != null) {
482 return;
483 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 mComboView = new CombinedBookmarkHistoryView(mActivity,
485 mUiController,
486 startWithHistory ?
487 CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY
488 : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS,
489 extras);
Michael Kolb47d63f82011-01-18 10:48:40 -0800490 FrameLayout wrapper =
491 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
492 wrapper.setVisibility(View.GONE);
Michael Kolbc16c5952011-03-29 15:37:03 -0700493 getTitleBar().stopEditingUrl();
Michael Kolb3a696282010-12-05 13:23:24 -0800494 dismissIME();
Michael Kolbc16c5952011-03-29 15:37:03 -0700495 hideTitleBar();
Michael Kolb3a696282010-12-05 13:23:24 -0800496 if (mActiveTab != null) {
Michael Kolb3a696282010-12-05 13:23:24 -0800497 mActiveTab.putInBackground();
498 }
John Reck74830e12011-03-14 11:43:05 -0700499 mComboView.setAlpha(0f);
500 ObjectAnimator anim = ObjectAnimator.ofFloat(mComboView, "alpha", 0f, 1f);
501 Resources res = mActivity.getResources();
502 anim.setDuration(res.getInteger(R.integer.comboViewFadeInDuration));
503 anim.start();
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
505 }
506
Michael Kolbba238702011-03-08 10:40:50 -0800507 public boolean isComboViewShowing() {
508 return (mComboView != null);
509 }
510
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 /**
512 * dismiss the ComboPage
513 */
514 @Override
515 public void hideComboView() {
516 if (mComboView != null) {
517 mContentView.removeView(mComboView);
Michael Kolb47d63f82011-01-18 10:48:40 -0800518 FrameLayout wrapper =
519 (FrameLayout) mContentView.findViewById(R.id.webview_wrapper);
520 wrapper.setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700521 mComboView = null;
522 }
Michael Kolb3a696282010-12-05 13:23:24 -0800523 if (mActiveTab != null) {
524 mActiveTab.putInForeground();
525 }
John Reckb3417f02011-01-14 11:01:05 -0800526 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 }
528
529 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400530 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 WebChromeClient.CustomViewCallback callback) {
532 // if a view already exists then immediately terminate the new one
533 if (mCustomView != null) {
534 callback.onCustomViewHidden();
535 return;
536 }
537
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400538 mOriginalOrientation = mActivity.getRequestedOrientation();
539
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 // Add the custom view to its container.
541 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
542 mCustomView = view;
543 mCustomViewCallback = callback;
544 // Hide the content view.
545 mContentView.setVisibility(View.GONE);
546 // Finally show the custom view container.
547 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400548 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700549 mCustomViewContainer.setVisibility(View.VISIBLE);
550 mCustomViewContainer.bringToFront();
551 }
552
553 @Override
554 public void onHideCustomView() {
555 if (mCustomView == null)
556 return;
557
558 // Hide the custom view.
559 mCustomView.setVisibility(View.GONE);
560 // Remove the custom view from its container.
561 mCustomViewContainer.removeView(mCustomView);
562 mCustomView = null;
563 mCustomViewContainer.setVisibility(View.GONE);
564 mCustomViewCallback.onCustomViewHidden();
565 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400566 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700567 setStatusBarVisibility(true);
568 mContentView.setVisibility(View.VISIBLE);
569 }
570
571 @Override
572 public boolean isCustomViewShowing() {
573 return mCustomView != null;
574 }
575
Michael Kolb66706532010-12-12 19:50:22 -0800576 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800577 if (mInputManager.isActive()) {
578 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
579 0);
580 }
581 }
582
Michael Kolb66706532010-12-12 19:50:22 -0800583 @Override
584 public boolean showsWeb() {
585 return mCustomView == null
586 && mComboView == null;
587 }
588
Patrick Scott92066772011-03-10 08:46:27 -0500589 @Override
590 public void showAutoLogin(Tab tab) {
591 updateAutoLogin(tab, true);
592 }
593
594 @Override
595 public void hideAutoLogin(Tab tab) {
596 updateAutoLogin(tab, true);
597 }
598
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 // -------------------------------------------------------------------------
600
Michael Kolb5a72f182011-01-13 20:35:06 -0800601 protected void updateNavigationState(Tab tab) {
602 }
603
Michael Kolb11d19782011-03-20 10:17:40 -0700604 protected void updateAutoLogin(Tab tab, boolean animate) {
605 getTitleBar().updateAutoLogin(tab, animate);
606 }
Patrick Scott92066772011-03-10 08:46:27 -0500607
Michael Kolb8233fac2010-10-26 16:08:53 -0700608 /**
609 * Update the lock icon to correspond to our latest state.
610 */
Michael Kolb66706532010-12-12 19:50:22 -0800611 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800612 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 updateLockIconImage(t.getLockIconType());
614 }
615 }
616
617 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 * Updates the lock-icon image in the title-bar.
619 */
John Reck30c714c2010-12-16 17:30:34 -0800620 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800622 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700623 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800624 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 d = mMixLockIcon;
626 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800627 getTitleBar().setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700628 }
629
John Reck30c714c2010-12-16 17:30:34 -0800630 protected void setUrlTitle(Tab tab) {
631 String url = tab.getUrl();
632 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800633 if (TextUtils.isEmpty(title)) {
634 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800635 }
Michael Kolb66706532010-12-12 19:50:22 -0800636 if (tab.isInVoiceSearchMode()) return;
637 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800638 getTitleBar().setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800639 }
640 }
641
642 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800643 protected void setFavicon(Tab tab) {
644 if (tab.inForeground()) {
645 Bitmap icon = tab.getFavicon();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800646 getTitleBar().setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800647 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 }
649
650 @Override
651 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 }
653
Michael Kolb66706532010-12-12 19:50:22 -0800654 // active tabs page
655
656 public void showActiveTabsPage() {
657 }
658
659 /**
660 * Remove the active tabs page.
661 */
662 public void removeActiveTabsPage() {
663 }
664
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 // menu handling callbacks
666
667 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800668 public boolean onPrepareOptionsMenu(Menu menu) {
669 return true;
670 }
671
672 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700674 }
675
676 @Override
677 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 }
679
680 @Override
681 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
683
684 @Override
685 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
688 @Override
689 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 }
691
692 @Override
693 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700694 }
695
696 // error console
697
698 @Override
699 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800700 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
702 if (flag) {
703 // Setting the show state of the console will cause it's the layout
704 // to be inflated.
705 if (errorConsole.numberOfErrors() > 0) {
706 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
707 } else {
708 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
709 }
710 if (errorConsole.getParent() != null) {
711 mErrorConsoleContainer.removeView(errorConsole);
712 }
713 // Now we can add it to the main view.
714 mErrorConsoleContainer.addView(errorConsole,
715 new LinearLayout.LayoutParams(
716 ViewGroup.LayoutParams.MATCH_PARENT,
717 ViewGroup.LayoutParams.WRAP_CONTENT));
718 } else {
719 mErrorConsoleContainer.removeView(errorConsole);
720 }
721 }
722
723 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800724 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
725 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
726 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 }
728
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 // -------------------------------------------------------------------------
730 // Helper function for WebChromeClient
731 // -------------------------------------------------------------------------
732
733 @Override
734 public Bitmap getDefaultVideoPoster() {
735 if (mDefaultVideoPoster == null) {
736 mDefaultVideoPoster = BitmapFactory.decodeResource(
737 mActivity.getResources(), R.drawable.default_video_poster);
738 }
739 return mDefaultVideoPoster;
740 }
741
742 @Override
743 public View getVideoLoadingProgressView() {
744 if (mVideoProgressView == null) {
745 LayoutInflater inflater = LayoutInflater.from(mActivity);
746 mVideoProgressView = inflater.inflate(
747 R.layout.video_loading_progress, null);
748 }
749 return mVideoProgressView;
750 }
751
Michael Kolb843510f2010-12-09 10:51:49 -0800752 @Override
753 public void showMaxTabsWarning() {
754 Toast warning = Toast.makeText(mActivity,
755 mActivity.getString(R.string.max_tabs_warning),
756 Toast.LENGTH_SHORT);
757 warning.show();
758 }
759
Michael Kolbfdb70242011-03-24 09:41:11 -0700760 protected void captureTab(final Tab tab) {
761 captureTab(tab,
762 (int) mActivity.getResources()
763 .getDimension(R.dimen.qc_thumb_width),
764 (int) mActivity.getResources()
765 .getDimension(R.dimen.qc_thumb_height));
766 }
767
768 protected void captureTab(final Tab tab, int width, int height) {
769 if ((tab == null) || (tab.getWebView() == null)) return;
770 Bitmap sshot = Controller.createScreenshot(tab, width, height);
771 tab.setScreenshot(sshot);
772 }
773
Michael Kolb46f987e2011-04-05 10:39:10 -0700774 protected WebView getWebView() {
775 if (mActiveTab != null) {
776 return mActiveTab.getWebView();
777 } else {
778 return null;
779 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700780 }
781
Michael Kolbfedb4922011-04-20 16:45:33 -0700782 protected Menu getMenu() {
783 MenuBuilder menu = new MenuBuilder(mActivity);
784 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
785 return menu;
786 }
787
Michael Kolbc38c6042011-04-27 10:46:06 -0700788 public void setFullscreen(boolean enabled) {
789 if (enabled) {
790 mActivity.getWindow().setFlags(
791 WindowManager.LayoutParams.FLAG_FULLSCREEN,
792 WindowManager.LayoutParams.FLAG_FULLSCREEN);
793 } else {
794 mActivity.getWindow().clearFlags(
795 WindowManager.LayoutParams.FLAG_FULLSCREEN);
796 }
797 }
798
Michael Kolb5a4372f2011-04-29 13:53:10 -0700799 protected Drawable getFaviconDrawable(Bitmap icon) {
800 Drawable[] array = new Drawable[3];
801 array[0] = new PaintDrawable(Color.BLACK);
802 PaintDrawable p = new PaintDrawable(Color.WHITE);
803 array[1] = p;
804 if (icon == null) {
805 array[2] = mGenericFavicon;
806 } else {
807 array[2] = new BitmapDrawable(icon);
808 }
809 LayerDrawable d = new LayerDrawable(array);
810 d.setLayerInset(1, 1, 1, 1, 1);
811 d.setLayerInset(2, 2, 2, 2, 2);
812 return d;
813 }
814
John Reck5d43ce82011-06-21 17:16:51 -0700815 public boolean isLoading() {
816 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
817 }
818
819 /**
820 * Suggest to the UI that the title bar can be hidden. The UI will then
821 * decide whether or not to hide based off a number of factors, such
822 * as if the user is editing the URL bar or if the page is loading
823 */
824 public void suggestHideTitleBar() {
825 if (!isLoading() && !isEditingUrl()) {
826 hideTitleBar();
827 }
828 }
829
830 @Override
John Reckbf2ec202011-06-29 17:47:38 -0700831 public boolean onTouch(View v, MotionEvent event) {
832 switch (event.getAction()) {
833 case MotionEvent.ACTION_DOWN:
834 mInitialY = event.getY();
835 break;
836 case MotionEvent.ACTION_MOVE:
837 WebView web = (WebView) v;
838 if (!isTitleBarShowing()
839 && web.getVisibleTitleHeight() == 0
840 && event.getY() > (mInitialY + mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700841 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
842 showTitleBar();
John Reckbf2ec202011-06-29 17:47:38 -0700843 } else if (event.getY() < mInitialY) {
844 mInitialY = event.getY();
845 }
846 break;
847 case MotionEvent.ACTION_CANCEL:
848 case MotionEvent.ACTION_UP:
849 if (isTitleBarShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700850 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
851 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
John Reck5d43ce82011-06-21 17:16:51 -0700852 }
John Reckbf2ec202011-06-29 17:47:38 -0700853 break;
John Reck5d43ce82011-06-21 17:16:51 -0700854 }
John Reckbf2ec202011-06-29 17:47:38 -0700855 return false;
John Reck5d43ce82011-06-21 17:16:51 -0700856 }
857
858 private Handler mHandler = new Handler() {
859
860 @Override
861 public void handleMessage(Message msg) {
862 if (msg.what == MSG_HIDE_TITLEBAR) {
863 suggestHideTitleBar();
864 }
865 }
866 };
Michael Kolb8233fac2010-10-26 16:08:53 -0700867}