blob: f92368e62c8b166957d568e1cd97068a4b05a2ef [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;
Michael Kolb3ca12752011-07-20 13:52:25 -070038import android.view.MenuItem;
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;
John Reckef654f12011-07-12 16:42:08 -070079 public static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
John Reck5d43ce82011-06-21 17:16:51 -070080
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
Michael Kolb8233fac2010-10-26 16:08:53 -070098 private LinearLayout mErrorConsoleContainer = null;
99
100 private Toast mStopToast;
Michael Kolb8233fac2010-10-26 16:08:53 -0700101
John Reckbf2ec202011-06-29 17:47:38 -0700102 private float mInitialY;
John Reck5d43ce82011-06-21 17:16:51 -0700103 private int mTitlebarScrollTriggerSlop;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700104
Michael Kolb8233fac2010-10-26 16:08:53 -0700105 // the default <video> poster
106 private Bitmap mDefaultVideoPoster;
107 // the video progress view
108 private View mVideoProgressView;
109
Michael Kolb8233fac2010-10-26 16:08:53 -0700110 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700111 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700112 protected TitleBar mTitleBar;
113 private NavigationBarBase mNavigationBar;
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());
John Reck0f602f32011-07-07 15:38:43 -0700144 mTitleBar = new TitleBar(mActivity, mUiController, this,
145 mContentView);
146 mTitleBar.setProgress(100);
147 mNavigationBar = mTitleBar.getNavigationBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 }
149
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 private void cancelStopToast() {
151 if (mStopToast != null) {
152 mStopToast.cancel();
153 mStopToast = null;
154 }
155 }
156
157 // lifecycle
158
159 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800160 if (isCustomViewShowing()) {
161 onHideCustomView();
162 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 cancelStopToast();
164 mActivityPaused = true;
165 }
166
167 public void onResume() {
168 mActivityPaused = false;
169 }
170
Michael Kolb66706532010-12-12 19:50:22 -0800171 protected boolean isActivityPaused() {
172 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 }
174
175 public void onConfigurationChanged(Configuration config) {
176 }
177
John Reck0f602f32011-07-07 15:38:43 -0700178 public Activity getActivity() {
179 return mActivity;
180 }
181
Michael Kolb8233fac2010-10-26 16:08:53 -0700182 // key handling
183
184 @Override
185 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700186 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);
John Reckef654f12011-07-12 16:42:08 -0700205 mTitleBar.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700206 }
207
208 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500209 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800210 if (tab.inForeground()) {
211 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700212 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800213 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500214 }
215
216 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 public void onPageStopped(Tab tab) {
218 cancelStopToast();
219 if (tab.inForeground()) {
220 mStopToast = Toast
221 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
222 mStopToast.show();
223 }
224 }
225
226 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800227 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700228 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800229 }
230
231 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 }
234
235 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800236 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700237 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800238 if ((tab != mActiveTab) && (mActiveTab != null)) {
239 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700240 WebView web = mActiveTab.getWebView();
241 if (web != null) {
242 web.setOnTouchListener(null);
243 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700244 }
Michael Kolb77df4562010-11-19 14:49:34 -0800245 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700246 WebView web = mActiveTab.getWebView();
247 if (web != null && !mUseQuickControls) {
248 web.setOnTouchListener(this);
249 }
John Reck5d43ce82011-06-21 17:16:51 -0700250 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700251 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800252 onTabDataChanged(tab);
253 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800254 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
John Reck0f602f32011-07-07 15:38:43 -0700255 mNavigationBar.setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500256 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700257 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700258 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700259 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700260 showTitleBarForDuration();
261 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 }
263
Michael Kolbcfa3af52010-12-14 10:36:11 -0800264 Tab getActiveTab() {
265 return mActiveTab;
266 }
267
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800269 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800270 }
271
272 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800274 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800276 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 }
279
280 @Override
281 public void detachTab(Tab tab) {
282 removeTabFromContentView(tab);
283 }
284
285 @Override
286 public void attachTab(Tab tab) {
287 attachTabToContentView(tab);
288 }
289
Michael Kolb377ea312011-02-17 14:36:56 -0800290 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800291 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700292 return;
293 }
294 View container = tab.getViewContainer();
295 WebView mainView = tab.getWebView();
296 // Attach the WebView to the container and then attach the
297 // container to the content view.
298 FrameLayout wrapper =
299 (FrameLayout) container.findViewById(R.id.webview_wrapper);
300 ViewGroup parent = (ViewGroup) mainView.getParent();
301 if (parent != wrapper) {
302 if (parent != null) {
303 Log.w(LOGTAG, "mMainView already has a parent in"
304 + " attachTabToContentView!");
305 parent.removeView(mainView);
306 }
307 wrapper.addView(mainView);
308 } else {
309 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
310 + " attachTabToContentView!");
311 }
312 parent = (ViewGroup) container.getParent();
313 if (parent != mContentView) {
314 if (parent != null) {
315 Log.w(LOGTAG, "mContainer already has a parent in"
316 + " attachTabToContentView!");
317 parent.removeView(container);
318 }
319 mContentView.addView(container, COVER_SCREEN_PARAMS);
320 } else {
321 Log.w(LOGTAG, "mContainer is already attached to content in"
322 + " attachTabToContentView!");
323 }
324 mUiController.attachSubWindow(tab);
325 }
326
327 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800328 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700329 // Remove the container that contains the main WebView.
330 WebView mainView = tab.getWebView();
331 View container = tab.getViewContainer();
332 if (mainView == null) {
333 return;
334 }
335 // Remove the container from the content and then remove the
336 // WebView from the container. This will trigger a focus change
337 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800338 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 FrameLayout wrapper =
340 (FrameLayout) container.findViewById(R.id.webview_wrapper);
341 wrapper.removeView(mainView);
342 mContentView.removeView(container);
343 mUiController.endActionMode();
344 mUiController.removeSubWindow(tab);
345 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
346 if (errorConsole != null) {
347 mErrorConsoleContainer.removeView(errorConsole);
348 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700349 }
350
Michael Kolba713ec82010-11-29 17:27:06 -0800351 @Override
352 public void onSetWebView(Tab tab, WebView webView) {
353 View container = tab.getViewContainer();
354 if (container == null) {
355 // The tab consists of a container view, which contains the main
356 // WebView, as well as any other UI elements associated with the tab.
357 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700358 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800359 tab.setViewContainer(container);
360 }
361 if (tab.getWebView() != webView) {
362 // Just remove the old one.
363 FrameLayout wrapper =
364 (FrameLayout) container.findViewById(R.id.webview_wrapper);
365 wrapper.removeView(tab.getWebView());
366 }
367 }
368
Michael Kolb8233fac2010-10-26 16:08:53 -0700369 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800370 * create a sub window container and webview for the tab
371 * Note: this methods operates through side-effects for now
372 * it sets both the subView and subViewContainer for the given tab
373 * @param tab tab to create the sub window for
374 * @param subView webview to be set as a subwindow for the tab
375 */
376 @Override
377 public void createSubWindow(Tab tab, WebView subView) {
378 View subViewContainer = mActivity.getLayoutInflater().inflate(
379 R.layout.browser_subwindow, null);
380 ViewGroup inner = (ViewGroup) subViewContainer
381 .findViewById(R.id.inner_container);
382 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
383 LayoutParams.MATCH_PARENT));
384 final ImageButton cancel = (ImageButton) subViewContainer
385 .findViewById(R.id.subwindow_close);
386 final WebView cancelSubView = subView;
387 cancel.setOnClickListener(new OnClickListener() {
388 public void onClick(View v) {
389 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
390 }
391 });
392 tab.setSubWebView(subView);
393 tab.setSubViewContainer(subViewContainer);
394 }
395
396 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 * Remove the sub window from the content view.
398 */
399 @Override
400 public void removeSubWindow(View subviewContainer) {
401 mContentView.removeView(subviewContainer);
402 mUiController.endActionMode();
403 }
404
405 /**
406 * Attach the sub window to the content view.
407 */
408 @Override
409 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800410 if (container.getParent() != null) {
411 // already attached, remove first
412 ((ViewGroup) container.getParent()).removeView(container);
413 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700414 mContentView.addView(container, COVER_SCREEN_PARAMS);
415 }
416
Michael Kolb11d19782011-03-20 10:17:40 -0700417 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700418 WebView web = getWebView();
419 if (web != null) {
420 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700421 }
422 }
423
Michael Kolb46f987e2011-04-05 10:39:10 -0700424 public void editUrl(boolean clearInput) {
425 if (mUiController.isInCustomActionMode()) {
426 mUiController.endActionMode();
427 }
428 showTitleBar();
John Reckef654f12011-07-12 16:42:08 -0700429 if (!getActiveTab().isSnapshot()) {
430 mNavigationBar.startEditingUrl(clearInput);
431 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700432 }
433
Michael Kolb7cdc4902011-02-03 17:54:40 -0800434 boolean canShowTitleBar() {
435 return !isTitleBarShowing()
436 && !isActivityPaused()
437 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700438 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800439 && !mUiController.isInCustomActionMode();
440 }
441
John Reck0f602f32011-07-07 15:38:43 -0700442 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700443 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700444 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700445 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700446 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800447 }
448
449 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700450 if (mTitleBar.isShowing()) {
451 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700452 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800453 }
454
455 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700456 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800457 }
458
John Reck5d43ce82011-06-21 17:16:51 -0700459 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700460 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700461 }
462
John Reck0f602f32011-07-07 15:38:43 -0700463 public TitleBar getTitleBar() {
464 return mTitleBar;
465 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800466
467 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700468 WebView web = getWebView();
469 if (web != null) {
470 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 }
472 }
473
Michael Kolb66706532010-12-12 19:50:22 -0800474 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700475 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700476 mNavigationBar.setInVoiceMode(true, results);
477 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700478 }
479
Michael Kolb66706532010-12-12 19:50:22 -0800480 @Override
481 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700482 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800483 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700484 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700485 }
486
487 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700488 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700489 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700490 }
491
492 @Override
John Reck2bc80422011-06-30 15:11:49 -0700493 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700494 Intent intent = new Intent(mActivity, ComboViewActivity.class);
495 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
496 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
497 Tab t = getActiveTab();
498 if (t != null) {
499 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800500 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700501 intent.putExtra(ComboViewActivity.EXTRA_BOOKMARK_PAGE,
502 mUiController.createBookmarkCurrentPageIntent(false));
503 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 }
505
506 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400507 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 WebChromeClient.CustomViewCallback callback) {
509 // if a view already exists then immediately terminate the new one
510 if (mCustomView != null) {
511 callback.onCustomViewHidden();
512 return;
513 }
514
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400515 mOriginalOrientation = mActivity.getRequestedOrientation();
516
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 // Add the custom view to its container.
518 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
519 mCustomView = view;
520 mCustomViewCallback = callback;
521 // Hide the content view.
522 mContentView.setVisibility(View.GONE);
523 // Finally show the custom view container.
524 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400525 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 mCustomViewContainer.setVisibility(View.VISIBLE);
527 mCustomViewContainer.bringToFront();
528 }
529
530 @Override
531 public void onHideCustomView() {
532 if (mCustomView == null)
533 return;
534
535 // Hide the custom view.
536 mCustomView.setVisibility(View.GONE);
537 // Remove the custom view from its container.
538 mCustomViewContainer.removeView(mCustomView);
539 mCustomView = null;
540 mCustomViewContainer.setVisibility(View.GONE);
541 mCustomViewCallback.onCustomViewHidden();
542 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400543 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 setStatusBarVisibility(true);
545 mContentView.setVisibility(View.VISIBLE);
546 }
547
548 @Override
549 public boolean isCustomViewShowing() {
550 return mCustomView != null;
551 }
552
Michael Kolb66706532010-12-12 19:50:22 -0800553 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800554 if (mInputManager.isActive()) {
555 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
556 0);
557 }
558 }
559
Michael Kolb66706532010-12-12 19:50:22 -0800560 @Override
John Reck3ba45532011-08-11 16:26:53 -0700561 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700562 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800563 }
564
Patrick Scott92066772011-03-10 08:46:27 -0500565 @Override
566 public void showAutoLogin(Tab tab) {
567 updateAutoLogin(tab, true);
568 }
569
570 @Override
571 public void hideAutoLogin(Tab tab) {
572 updateAutoLogin(tab, true);
573 }
574
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 // -------------------------------------------------------------------------
576
Michael Kolb5a72f182011-01-13 20:35:06 -0800577 protected void updateNavigationState(Tab tab) {
578 }
579
Michael Kolb11d19782011-03-20 10:17:40 -0700580 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700581 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700582 }
Patrick Scott92066772011-03-10 08:46:27 -0500583
Michael Kolb8233fac2010-10-26 16:08:53 -0700584 /**
585 * Update the lock icon to correspond to our latest state.
586 */
Michael Kolb66706532010-12-12 19:50:22 -0800587 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800588 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 updateLockIconImage(t.getLockIconType());
590 }
591 }
592
593 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 * Updates the lock-icon image in the title-bar.
595 */
John Reck30c714c2010-12-16 17:30:34 -0800596 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700597 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800598 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800600 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 d = mMixLockIcon;
602 }
John Reck0f602f32011-07-07 15:38:43 -0700603 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 }
605
John Reck30c714c2010-12-16 17:30:34 -0800606 protected void setUrlTitle(Tab tab) {
607 String url = tab.getUrl();
608 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800609 if (TextUtils.isEmpty(title)) {
610 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800611 }
Michael Kolb66706532010-12-12 19:50:22 -0800612 if (tab.isInVoiceSearchMode()) return;
613 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700614 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800615 }
616 }
617
618 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800619 protected void setFavicon(Tab tab) {
620 if (tab.inForeground()) {
621 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700622 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800623 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700624 }
625
626 @Override
627 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700628 }
629
Michael Kolb66706532010-12-12 19:50:22 -0800630 // active tabs page
631
632 public void showActiveTabsPage() {
633 }
634
635 /**
636 * Remove the active tabs page.
637 */
638 public void removeActiveTabsPage() {
639 }
640
Michael Kolb8233fac2010-10-26 16:08:53 -0700641 // menu handling callbacks
642
643 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800644 public boolean onPrepareOptionsMenu(Menu menu) {
645 return true;
646 }
647
648 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700649 public void updateMenuState(Tab tab, Menu menu) {
650 }
651
652 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700654 }
655
656 @Override
657 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 }
659
660 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700661 public boolean onOptionsItemSelected(MenuItem item) {
662 return false;
663 }
664
665 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 }
668
669 @Override
670 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 }
672
673 @Override
674 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 }
676
677 @Override
678 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 }
680
681 // error console
682
683 @Override
684 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800685 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
687 if (flag) {
688 // Setting the show state of the console will cause it's the layout
689 // to be inflated.
690 if (errorConsole.numberOfErrors() > 0) {
691 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
692 } else {
693 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
694 }
695 if (errorConsole.getParent() != null) {
696 mErrorConsoleContainer.removeView(errorConsole);
697 }
698 // Now we can add it to the main view.
699 mErrorConsoleContainer.addView(errorConsole,
700 new LinearLayout.LayoutParams(
701 ViewGroup.LayoutParams.MATCH_PARENT,
702 ViewGroup.LayoutParams.WRAP_CONTENT));
703 } else {
704 mErrorConsoleContainer.removeView(errorConsole);
705 }
706 }
707
708 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800709 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
710 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
711 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 }
713
Michael Kolb8233fac2010-10-26 16:08:53 -0700714 // -------------------------------------------------------------------------
715 // Helper function for WebChromeClient
716 // -------------------------------------------------------------------------
717
718 @Override
719 public Bitmap getDefaultVideoPoster() {
720 if (mDefaultVideoPoster == null) {
721 mDefaultVideoPoster = BitmapFactory.decodeResource(
722 mActivity.getResources(), R.drawable.default_video_poster);
723 }
724 return mDefaultVideoPoster;
725 }
726
727 @Override
728 public View getVideoLoadingProgressView() {
729 if (mVideoProgressView == null) {
730 LayoutInflater inflater = LayoutInflater.from(mActivity);
731 mVideoProgressView = inflater.inflate(
732 R.layout.video_loading_progress, null);
733 }
734 return mVideoProgressView;
735 }
736
Michael Kolb843510f2010-12-09 10:51:49 -0800737 @Override
738 public void showMaxTabsWarning() {
739 Toast warning = Toast.makeText(mActivity,
740 mActivity.getString(R.string.max_tabs_warning),
741 Toast.LENGTH_SHORT);
742 warning.show();
743 }
744
Michael Kolb46f987e2011-04-05 10:39:10 -0700745 protected WebView getWebView() {
746 if (mActiveTab != null) {
747 return mActiveTab.getWebView();
748 } else {
749 return null;
750 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700751 }
752
Michael Kolbfedb4922011-04-20 16:45:33 -0700753 protected Menu getMenu() {
754 MenuBuilder menu = new MenuBuilder(mActivity);
755 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
756 return menu;
757 }
758
Michael Kolbc38c6042011-04-27 10:46:06 -0700759 public void setFullscreen(boolean enabled) {
760 if (enabled) {
761 mActivity.getWindow().setFlags(
762 WindowManager.LayoutParams.FLAG_FULLSCREEN,
763 WindowManager.LayoutParams.FLAG_FULLSCREEN);
764 } else {
765 mActivity.getWindow().clearFlags(
766 WindowManager.LayoutParams.FLAG_FULLSCREEN);
767 }
768 }
769
John Reck0f602f32011-07-07 15:38:43 -0700770 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700771 Drawable[] array = new Drawable[3];
772 array[0] = new PaintDrawable(Color.BLACK);
773 PaintDrawable p = new PaintDrawable(Color.WHITE);
774 array[1] = p;
775 if (icon == null) {
776 array[2] = mGenericFavicon;
777 } else {
778 array[2] = new BitmapDrawable(icon);
779 }
780 LayerDrawable d = new LayerDrawable(array);
781 d.setLayerInset(1, 1, 1, 1, 1);
782 d.setLayerInset(2, 2, 2, 2, 2);
783 return d;
784 }
785
John Reck5d43ce82011-06-21 17:16:51 -0700786 public boolean isLoading() {
787 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
788 }
789
790 /**
791 * Suggest to the UI that the title bar can be hidden. The UI will then
792 * decide whether or not to hide based off a number of factors, such
793 * as if the user is editing the URL bar or if the page is loading
794 */
795 public void suggestHideTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700796 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()) {
John Reck5d43ce82011-06-21 17:16:51 -0700797 hideTitleBar();
798 }
799 }
800
Michael Kolb017ffab2011-07-11 15:26:47 -0700801 protected void showTitleBarForDuration() {
John Reck6ac5bfd2011-07-01 17:02:55 -0700802 showTitleBar();
803 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
804 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
805 }
806
John Reck5d43ce82011-06-21 17:16:51 -0700807 @Override
John Reckbf2ec202011-06-29 17:47:38 -0700808 public boolean onTouch(View v, MotionEvent event) {
809 switch (event.getAction()) {
810 case MotionEvent.ACTION_DOWN:
811 mInitialY = event.getY();
812 break;
813 case MotionEvent.ACTION_MOVE:
814 WebView web = (WebView) v;
John Reck1e822eb2011-07-07 16:53:12 -0700815 if (event.getPointerCount() == 1
816 && !isTitleBarShowing()
John Reckbf2ec202011-06-29 17:47:38 -0700817 && web.getVisibleTitleHeight() == 0
818 && event.getY() > (mInitialY + mTitlebarScrollTriggerSlop)) {
John Reck5d43ce82011-06-21 17:16:51 -0700819 showTitleBar();
John Reckbf2ec202011-06-29 17:47:38 -0700820 } else if (event.getY() < mInitialY) {
821 mInitialY = event.getY();
822 }
823 break;
824 case MotionEvent.ACTION_CANCEL:
825 case MotionEvent.ACTION_UP:
826 if (isTitleBarShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700827 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
828 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
John Reck5d43ce82011-06-21 17:16:51 -0700829 }
John Reckbf2ec202011-06-29 17:47:38 -0700830 break;
John Reck5d43ce82011-06-21 17:16:51 -0700831 }
John Reckbf2ec202011-06-29 17:47:38 -0700832 return false;
John Reck5d43ce82011-06-21 17:16:51 -0700833 }
834
835 private Handler mHandler = new Handler() {
836
837 @Override
838 public void handleMessage(Message msg) {
839 if (msg.what == MSG_HIDE_TITLEBAR) {
840 suggestHideTitleBar();
841 }
842 }
843 };
John Reck3ba45532011-08-11 16:26:53 -0700844
845 @Override
846 public void showWeb(boolean animate) {
847 mUiController.hideCustomView();
848 }
849
Michael Kolb8233fac2010-10-26 16:08:53 -0700850}