blob: d1445d8fbc4ff6beebcaac42ff4d0dc63bbf8014 [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 Reckd3e4d5b2011-07-13 15:48:43 -070020import android.content.Intent;
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;
John Reckef654f12011-07-12 16:42:08 -070078 public static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
John Reck5d43ce82011-06-21 17:16:51 -070079
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
Michael Kolb8233fac2010-10-26 16:08:53 -070097 private LinearLayout mErrorConsoleContainer = null;
98
99 private Toast mStopToast;
Michael Kolb8233fac2010-10-26 16:08:53 -0700100
John Reckbf2ec202011-06-29 17:47:38 -0700101 private float mInitialY;
John Reck5d43ce82011-06-21 17:16:51 -0700102 private int mTitlebarScrollTriggerSlop;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700103
Michael Kolb8233fac2010-10-26 16:08:53 -0700104 // the default <video> poster
105 private Bitmap mDefaultVideoPoster;
106 // the video progress view
107 private View mVideoProgressView;
108
Michael Kolb8233fac2010-10-26 16:08:53 -0700109 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700110 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700111 protected TitleBar mTitleBar;
112 private NavigationBarBase mNavigationBar;
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());
John Reck0f602f32011-07-07 15:38:43 -0700143 mTitleBar = new TitleBar(mActivity, mUiController, this,
144 mContentView);
145 mTitleBar.setProgress(100);
146 mNavigationBar = mTitleBar.getNavigationBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700147 }
148
Michael Kolb8233fac2010-10-26 16:08:53 -0700149 private void cancelStopToast() {
150 if (mStopToast != null) {
151 mStopToast.cancel();
152 mStopToast = null;
153 }
154 }
155
156 // lifecycle
157
158 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800159 if (isCustomViewShowing()) {
160 onHideCustomView();
161 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 cancelStopToast();
163 mActivityPaused = true;
164 }
165
166 public void onResume() {
167 mActivityPaused = false;
168 }
169
Michael Kolb66706532010-12-12 19:50:22 -0800170 protected boolean isActivityPaused() {
171 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700172 }
173
174 public void onConfigurationChanged(Configuration config) {
175 }
176
John Reck0f602f32011-07-07 15:38:43 -0700177 public Activity getActivity() {
178 return mActivity;
179 }
180
Michael Kolb8233fac2010-10-26 16:08:53 -0700181 // key handling
182
183 @Override
184 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 if (mCustomView != null) {
186 mUiController.hideCustomView();
187 return true;
188 }
189 return false;
190 }
191
Michael Kolb2814a362011-05-19 15:49:41 -0700192 @Override
193 public boolean onMenuKey() {
194 return false;
195 }
196
John Reck30c714c2010-12-16 17:30:34 -0800197 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 @Override
John Reck30c714c2010-12-16 17:30:34 -0800199 public void onTabDataChanged(Tab tab) {
200 setUrlTitle(tab);
201 setFavicon(tab);
202 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800203 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700204 mTitleBar.onTabDataChanged(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();
John Reck0f602f32011-07-07 15:38:43 -0700211 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800212 }
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();
John Reck0f602f32011-07-07 15:38:43 -0700254 mNavigationBar.setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500255 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700256 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700257 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700258 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700259 showTitleBarForDuration();
260 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 }
262
Michael Kolbcfa3af52010-12-14 10:36:11 -0800263 Tab getActiveTab() {
264 return mActiveTab;
265 }
266
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800268 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800269 }
270
271 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800273 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800275 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 }
278
279 @Override
280 public void detachTab(Tab tab) {
281 removeTabFromContentView(tab);
282 }
283
284 @Override
285 public void attachTab(Tab tab) {
286 attachTabToContentView(tab);
287 }
288
Michael Kolb377ea312011-02-17 14:36:56 -0800289 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800290 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700291 return;
292 }
293 View container = tab.getViewContainer();
294 WebView mainView = tab.getWebView();
295 // Attach the WebView to the container and then attach the
296 // container to the content view.
297 FrameLayout wrapper =
298 (FrameLayout) container.findViewById(R.id.webview_wrapper);
299 ViewGroup parent = (ViewGroup) mainView.getParent();
300 if (parent != wrapper) {
301 if (parent != null) {
302 Log.w(LOGTAG, "mMainView already has a parent in"
303 + " attachTabToContentView!");
304 parent.removeView(mainView);
305 }
306 wrapper.addView(mainView);
307 } else {
308 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
309 + " attachTabToContentView!");
310 }
311 parent = (ViewGroup) container.getParent();
312 if (parent != mContentView) {
313 if (parent != null) {
314 Log.w(LOGTAG, "mContainer already has a parent in"
315 + " attachTabToContentView!");
316 parent.removeView(container);
317 }
318 mContentView.addView(container, COVER_SCREEN_PARAMS);
319 } else {
320 Log.w(LOGTAG, "mContainer is already attached to content in"
321 + " attachTabToContentView!");
322 }
323 mUiController.attachSubWindow(tab);
324 }
325
326 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800327 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700328 // Remove the container that contains the main WebView.
329 WebView mainView = tab.getWebView();
330 View container = tab.getViewContainer();
331 if (mainView == null) {
332 return;
333 }
334 // Remove the container from the content and then remove the
335 // WebView from the container. This will trigger a focus change
336 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800337 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700338 FrameLayout wrapper =
339 (FrameLayout) container.findViewById(R.id.webview_wrapper);
340 wrapper.removeView(mainView);
341 mContentView.removeView(container);
342 mUiController.endActionMode();
343 mUiController.removeSubWindow(tab);
344 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
345 if (errorConsole != null) {
346 mErrorConsoleContainer.removeView(errorConsole);
347 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700348 }
349
Michael Kolba713ec82010-11-29 17:27:06 -0800350 @Override
351 public void onSetWebView(Tab tab, WebView webView) {
352 View container = tab.getViewContainer();
353 if (container == null) {
354 // The tab consists of a container view, which contains the main
355 // WebView, as well as any other UI elements associated with the tab.
356 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700357 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800358 tab.setViewContainer(container);
359 }
360 if (tab.getWebView() != webView) {
361 // Just remove the old one.
362 FrameLayout wrapper =
363 (FrameLayout) container.findViewById(R.id.webview_wrapper);
364 wrapper.removeView(tab.getWebView());
365 }
366 }
367
Michael Kolb8233fac2010-10-26 16:08:53 -0700368 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800369 * create a sub window container and webview for the tab
370 * Note: this methods operates through side-effects for now
371 * it sets both the subView and subViewContainer for the given tab
372 * @param tab tab to create the sub window for
373 * @param subView webview to be set as a subwindow for the tab
374 */
375 @Override
376 public void createSubWindow(Tab tab, WebView subView) {
377 View subViewContainer = mActivity.getLayoutInflater().inflate(
378 R.layout.browser_subwindow, null);
379 ViewGroup inner = (ViewGroup) subViewContainer
380 .findViewById(R.id.inner_container);
381 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
382 LayoutParams.MATCH_PARENT));
383 final ImageButton cancel = (ImageButton) subViewContainer
384 .findViewById(R.id.subwindow_close);
385 final WebView cancelSubView = subView;
386 cancel.setOnClickListener(new OnClickListener() {
387 public void onClick(View v) {
388 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
389 }
390 });
391 tab.setSubWebView(subView);
392 tab.setSubViewContainer(subViewContainer);
393 }
394
395 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 * Remove the sub window from the content view.
397 */
398 @Override
399 public void removeSubWindow(View subviewContainer) {
400 mContentView.removeView(subviewContainer);
401 mUiController.endActionMode();
402 }
403
404 /**
405 * Attach the sub window to the content view.
406 */
407 @Override
408 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800409 if (container.getParent() != null) {
410 // already attached, remove first
411 ((ViewGroup) container.getParent()).removeView(container);
412 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700413 mContentView.addView(container, COVER_SCREEN_PARAMS);
414 }
415
Michael Kolb11d19782011-03-20 10:17:40 -0700416 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700417 WebView web = getWebView();
418 if (web != null) {
419 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700420 }
421 }
422
Michael Kolb46f987e2011-04-05 10:39:10 -0700423 public void editUrl(boolean clearInput) {
424 if (mUiController.isInCustomActionMode()) {
425 mUiController.endActionMode();
426 }
427 showTitleBar();
John Reckef654f12011-07-12 16:42:08 -0700428 if (!getActiveTab().isSnapshot()) {
429 mNavigationBar.startEditingUrl(clearInput);
430 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700431 }
432
Michael Kolb7cdc4902011-02-03 17:54:40 -0800433 boolean canShowTitleBar() {
434 return !isTitleBarShowing()
435 && !isActivityPaused()
436 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700437 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800438 && !mUiController.isInCustomActionMode();
439 }
440
John Reck0f602f32011-07-07 15:38:43 -0700441 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700442 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700443 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700444 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700445 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800446 }
447
448 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700449 if (mTitleBar.isShowing()) {
450 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700451 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800452 }
453
454 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700455 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800456 }
457
John Reck5d43ce82011-06-21 17:16:51 -0700458 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700459 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700460 }
461
John Reck0f602f32011-07-07 15:38:43 -0700462 public TitleBar getTitleBar() {
463 return mTitleBar;
464 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800465
466 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700467 WebView web = getWebView();
468 if (web != null) {
469 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700470 }
471 }
472
Michael Kolb66706532010-12-12 19:50:22 -0800473 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700474 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700475 mNavigationBar.setInVoiceMode(true, results);
476 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700477 }
478
Michael Kolb66706532010-12-12 19:50:22 -0800479 @Override
480 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700481 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800482 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700483 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 }
485
486 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700487 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700488 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700489 }
490
491 @Override
John Reck2bc80422011-06-30 15:11:49 -0700492 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700493 Intent intent = new Intent(mActivity, ComboViewActivity.class);
494 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
495 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
496 Tab t = getActiveTab();
497 if (t != null) {
498 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800499 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700500 intent.putExtra(ComboViewActivity.EXTRA_BOOKMARK_PAGE,
501 mUiController.createBookmarkCurrentPageIntent(false));
502 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 }
504
505 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400506 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700507 WebChromeClient.CustomViewCallback callback) {
508 // if a view already exists then immediately terminate the new one
509 if (mCustomView != null) {
510 callback.onCustomViewHidden();
511 return;
512 }
513
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400514 mOriginalOrientation = mActivity.getRequestedOrientation();
515
Michael Kolb8233fac2010-10-26 16:08:53 -0700516 // Add the custom view to its container.
517 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
518 mCustomView = view;
519 mCustomViewCallback = callback;
520 // Hide the content view.
521 mContentView.setVisibility(View.GONE);
522 // Finally show the custom view container.
523 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400524 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 mCustomViewContainer.setVisibility(View.VISIBLE);
526 mCustomViewContainer.bringToFront();
527 }
528
529 @Override
530 public void onHideCustomView() {
531 if (mCustomView == null)
532 return;
533
534 // Hide the custom view.
535 mCustomView.setVisibility(View.GONE);
536 // Remove the custom view from its container.
537 mCustomViewContainer.removeView(mCustomView);
538 mCustomView = null;
539 mCustomViewContainer.setVisibility(View.GONE);
540 mCustomViewCallback.onCustomViewHidden();
541 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400542 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 setStatusBarVisibility(true);
544 mContentView.setVisibility(View.VISIBLE);
545 }
546
547 @Override
548 public boolean isCustomViewShowing() {
549 return mCustomView != null;
550 }
551
Michael Kolb66706532010-12-12 19:50:22 -0800552 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800553 if (mInputManager.isActive()) {
554 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
555 0);
556 }
557 }
558
Michael Kolb66706532010-12-12 19:50:22 -0800559 @Override
560 public boolean showsWeb() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700561 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800562 }
563
Patrick Scott92066772011-03-10 08:46:27 -0500564 @Override
565 public void showAutoLogin(Tab tab) {
566 updateAutoLogin(tab, true);
567 }
568
569 @Override
570 public void hideAutoLogin(Tab tab) {
571 updateAutoLogin(tab, true);
572 }
573
Michael Kolb8233fac2010-10-26 16:08:53 -0700574 // -------------------------------------------------------------------------
575
Michael Kolb5a72f182011-01-13 20:35:06 -0800576 protected void updateNavigationState(Tab tab) {
577 }
578
Michael Kolb11d19782011-03-20 10:17:40 -0700579 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700580 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700581 }
Patrick Scott92066772011-03-10 08:46:27 -0500582
Michael Kolb8233fac2010-10-26 16:08:53 -0700583 /**
584 * Update the lock icon to correspond to our latest state.
585 */
Michael Kolb66706532010-12-12 19:50:22 -0800586 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800587 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 updateLockIconImage(t.getLockIconType());
589 }
590 }
591
592 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 * Updates the lock-icon image in the title-bar.
594 */
John Reck30c714c2010-12-16 17:30:34 -0800595 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700596 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800597 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800599 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700600 d = mMixLockIcon;
601 }
John Reck0f602f32011-07-07 15:38:43 -0700602 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 }
604
John Reck30c714c2010-12-16 17:30:34 -0800605 protected void setUrlTitle(Tab tab) {
606 String url = tab.getUrl();
607 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800608 if (TextUtils.isEmpty(title)) {
609 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800610 }
Michael Kolb66706532010-12-12 19:50:22 -0800611 if (tab.isInVoiceSearchMode()) return;
612 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700613 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800614 }
615 }
616
617 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800618 protected void setFavicon(Tab tab) {
619 if (tab.inForeground()) {
620 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700621 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800622 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700623 }
624
625 @Override
626 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 }
628
Michael Kolb66706532010-12-12 19:50:22 -0800629 // active tabs page
630
631 public void showActiveTabsPage() {
632 }
633
634 /**
635 * Remove the active tabs page.
636 */
637 public void removeActiveTabsPage() {
638 }
639
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 // menu handling callbacks
641
642 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800643 public boolean onPrepareOptionsMenu(Menu menu) {
644 return true;
645 }
646
647 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700649 }
650
651 @Override
652 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 }
654
655 @Override
656 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700657 }
658
659 @Override
660 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 }
662
663 @Override
664 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 }
666
667 @Override
668 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 }
670
671 // error console
672
673 @Override
674 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800675 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
677 if (flag) {
678 // Setting the show state of the console will cause it's the layout
679 // to be inflated.
680 if (errorConsole.numberOfErrors() > 0) {
681 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
682 } else {
683 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
684 }
685 if (errorConsole.getParent() != null) {
686 mErrorConsoleContainer.removeView(errorConsole);
687 }
688 // Now we can add it to the main view.
689 mErrorConsoleContainer.addView(errorConsole,
690 new LinearLayout.LayoutParams(
691 ViewGroup.LayoutParams.MATCH_PARENT,
692 ViewGroup.LayoutParams.WRAP_CONTENT));
693 } else {
694 mErrorConsoleContainer.removeView(errorConsole);
695 }
696 }
697
698 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800699 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
700 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
701 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 }
703
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 // -------------------------------------------------------------------------
705 // Helper function for WebChromeClient
706 // -------------------------------------------------------------------------
707
708 @Override
709 public Bitmap getDefaultVideoPoster() {
710 if (mDefaultVideoPoster == null) {
711 mDefaultVideoPoster = BitmapFactory.decodeResource(
712 mActivity.getResources(), R.drawable.default_video_poster);
713 }
714 return mDefaultVideoPoster;
715 }
716
717 @Override
718 public View getVideoLoadingProgressView() {
719 if (mVideoProgressView == null) {
720 LayoutInflater inflater = LayoutInflater.from(mActivity);
721 mVideoProgressView = inflater.inflate(
722 R.layout.video_loading_progress, null);
723 }
724 return mVideoProgressView;
725 }
726
Michael Kolb843510f2010-12-09 10:51:49 -0800727 @Override
728 public void showMaxTabsWarning() {
729 Toast warning = Toast.makeText(mActivity,
730 mActivity.getString(R.string.max_tabs_warning),
731 Toast.LENGTH_SHORT);
732 warning.show();
733 }
734
Michael Kolbfdb70242011-03-24 09:41:11 -0700735 protected void captureTab(final Tab tab) {
736 captureTab(tab,
737 (int) mActivity.getResources()
738 .getDimension(R.dimen.qc_thumb_width),
739 (int) mActivity.getResources()
740 .getDimension(R.dimen.qc_thumb_height));
741 }
742
743 protected void captureTab(final Tab tab, int width, int height) {
744 if ((tab == null) || (tab.getWebView() == null)) return;
745 Bitmap sshot = Controller.createScreenshot(tab, width, height);
746 tab.setScreenshot(sshot);
747 }
748
Michael Kolb46f987e2011-04-05 10:39:10 -0700749 protected WebView getWebView() {
750 if (mActiveTab != null) {
751 return mActiveTab.getWebView();
752 } else {
753 return null;
754 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700755 }
756
Michael Kolbfedb4922011-04-20 16:45:33 -0700757 protected Menu getMenu() {
758 MenuBuilder menu = new MenuBuilder(mActivity);
759 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
760 return menu;
761 }
762
Michael Kolbc38c6042011-04-27 10:46:06 -0700763 public void setFullscreen(boolean enabled) {
764 if (enabled) {
765 mActivity.getWindow().setFlags(
766 WindowManager.LayoutParams.FLAG_FULLSCREEN,
767 WindowManager.LayoutParams.FLAG_FULLSCREEN);
768 } else {
769 mActivity.getWindow().clearFlags(
770 WindowManager.LayoutParams.FLAG_FULLSCREEN);
771 }
772 }
773
John Reck0f602f32011-07-07 15:38:43 -0700774 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700775 Drawable[] array = new Drawable[3];
776 array[0] = new PaintDrawable(Color.BLACK);
777 PaintDrawable p = new PaintDrawable(Color.WHITE);
778 array[1] = p;
779 if (icon == null) {
780 array[2] = mGenericFavicon;
781 } else {
782 array[2] = new BitmapDrawable(icon);
783 }
784 LayerDrawable d = new LayerDrawable(array);
785 d.setLayerInset(1, 1, 1, 1, 1);
786 d.setLayerInset(2, 2, 2, 2, 2);
787 return d;
788 }
789
John Reck5d43ce82011-06-21 17:16:51 -0700790 public boolean isLoading() {
791 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
792 }
793
794 /**
795 * Suggest to the UI that the title bar can be hidden. The UI will then
796 * decide whether or not to hide based off a number of factors, such
797 * as if the user is editing the URL bar or if the page is loading
798 */
799 public void suggestHideTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700800 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()) {
John Reck5d43ce82011-06-21 17:16:51 -0700801 hideTitleBar();
802 }
803 }
804
Michael Kolb017ffab2011-07-11 15:26:47 -0700805 protected void showTitleBarForDuration() {
John Reck6ac5bfd2011-07-01 17:02:55 -0700806 showTitleBar();
807 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
808 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
809 }
810
John Reck5d43ce82011-06-21 17:16:51 -0700811 @Override
John Reckbf2ec202011-06-29 17:47:38 -0700812 public boolean onTouch(View v, MotionEvent event) {
813 switch (event.getAction()) {
814 case MotionEvent.ACTION_DOWN:
815 mInitialY = event.getY();
816 break;
817 case MotionEvent.ACTION_MOVE:
818 WebView web = (WebView) v;
John Reck1e822eb2011-07-07 16:53:12 -0700819 if (event.getPointerCount() == 1
820 && !isTitleBarShowing()
John Reckbf2ec202011-06-29 17:47:38 -0700821 && web.getVisibleTitleHeight() == 0
822 && event.getY() > (mInitialY + mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700823 showTitleBar();
John Reckbf2ec202011-06-29 17:47:38 -0700824 } else if (event.getY() < mInitialY) {
825 mInitialY = event.getY();
826 }
827 break;
828 case MotionEvent.ACTION_CANCEL:
829 case MotionEvent.ACTION_UP:
830 if (isTitleBarShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700831 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
832 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
John Reck5d43ce82011-06-21 17:16:51 -0700833 }
John Reckbf2ec202011-06-29 17:47:38 -0700834 break;
John Reck5d43ce82011-06-21 17:16:51 -0700835 }
John Reckbf2ec202011-06-29 17:47:38 -0700836 return false;
John Reck5d43ce82011-06-21 17:16:51 -0700837 }
838
839 private Handler mHandler = new Handler() {
840
841 @Override
842 public void handleMessage(Message msg) {
843 if (msg.what == MSG_HIDE_TITLEBAR) {
844 suggestHideTitleBar();
845 }
846 }
847 };
Michael Kolb8233fac2010-10-26 16:08:53 -0700848}