blob: ee308f98aa380ccf918e35b3bcc6d3949c47b257 [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;
Michael Kolb53ed62c2011-10-04 16:18:44 -070020import android.content.Context;
John Reckd3e4d5b2011-07-13 15:48:43 -070021import android.content.Intent;
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;
Michael Kolb3ca12752011-07-20 13:52:25 -070039import android.view.MenuItem;
Michael Kolb31065b12011-10-06 13:51:32 -070040import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.view.View;
Michael Kolb1514bb72010-11-22 09:11:48 -080042import android.view.View.OnClickListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080044import android.view.ViewGroup.LayoutParams;
Michael Kolbc5675ad2011-10-21 13:34:28 -070045import android.view.Window;
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
Steve Block2466eff2011-10-03 15:33:09 +010055import com.android.browser.Tab.SecurityState;
John Reckbf2ec202011-06-29 17:47:38 -070056import 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 */
John Reck718a24d2011-08-12 11:08:30 -070063public abstract class BaseUi implements UI {
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
Steve Block2466eff2011-10-03 15:33:09 +010087 private Drawable mLockIconSecure;
88 private Drawable mLockIconMixed;
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 Kolb31065b12011-10-06 13:51:32 -070093 protected FrameLayout mFullscreenContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
Michael Kolb31065b12011-10-06 13:51:32 -070095 private View mCustomView;
Michael Kolb8233fac2010-10-26 16:08:53 -070096 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040097 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070098
Michael Kolb8233fac2010-10-26 16:08:53 -070099 private LinearLayout mErrorConsoleContainer = null;
100
John Reck718a24d2011-08-12 11:08:30 -0700101 private UrlBarAutoShowManager mUrlBarAutoShowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -0700102
John Reck718a24d2011-08-12 11:08:30 -0700103 private Toast mStopToast;
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);
Steve Block2466eff2011-10-03 15:33:09 +0100122 mLockIconSecure = res.getDrawable(R.drawable.ic_secure_holo_dark);
123 mLockIconMixed = res.getDrawable(R.drawable.ic_secure_partial_holo_dark);
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);
Michael Kolb53ed62c2011-10-04 16:18:44 -0700130 mCustomViewContainer = (FrameLayout) frameLayout.findViewById(
131 R.id.fullscreen_custom_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700132 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 .findViewById(R.id.error_console);
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 Reck0f602f32011-07-07 15:38:43 -0700137 mTitleBar = new TitleBar(mActivity, mUiController, this,
138 mContentView);
139 mTitleBar.setProgress(100);
140 mNavigationBar = mTitleBar.getNavigationBar();
John Reck718a24d2011-08-12 11:08:30 -0700141 mUrlBarAutoShowManager = new UrlBarAutoShowManager(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 }
143
Michael Kolb8233fac2010-10-26 16:08:53 -0700144 private void cancelStopToast() {
145 if (mStopToast != null) {
146 mStopToast.cancel();
147 mStopToast = null;
148 }
149 }
150
151 // lifecycle
152
153 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800154 if (isCustomViewShowing()) {
155 onHideCustomView();
156 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 cancelStopToast();
158 mActivityPaused = true;
159 }
160
161 public void onResume() {
162 mActivityPaused = false;
Michael Kolb2ae6ef72011-08-22 14:49:34 -0700163 // check if we exited without setting active tab
164 // b: 5188145
Michael Kolb1a4625a2011-08-24 13:40:44 -0700165 final Tab ct = mTabControl.getCurrentTab();
166 if (ct != null) {
167 setActiveTab(ct);
168 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 }
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);
John Reck419f6b42011-08-16 16:10:51 -0700206 mNavigationBar.onTabDataChanged(tab);
Michael Kolba53c9892011-10-05 13:31:40 -0700207 onProgressChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 }
209
210 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500211 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800212 if (tab.inForeground()) {
213 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700214 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800215 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500216 }
217
218 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 public void onPageStopped(Tab tab) {
220 cancelStopToast();
221 if (tab.inForeground()) {
222 mStopToast = Toast
223 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
224 mStopToast.show();
225 }
226 }
227
228 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800229 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700230 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800231 }
232
233 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700234 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700235 }
236
237 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800238 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700239 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800240 if ((tab != mActiveTab) && (mActiveTab != null)) {
241 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700242 WebView web = mActiveTab.getWebView();
243 if (web != null) {
244 web.setOnTouchListener(null);
245 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700246 }
Michael Kolb77df4562010-11-19 14:49:34 -0800247 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700248 WebView web = mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700249 updateUrlBarAutoShowManagerTarget();
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);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700254 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
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
John Reck718a24d2011-08-12 11:08:30 -0700263 protected void updateUrlBarAutoShowManagerTarget() {
264 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
265 if (!mUseQuickControls && web instanceof BrowserWebView) {
266 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
267 } else {
268 mUrlBarAutoShowManager.setTarget(null);
269 }
270 }
271
Michael Kolbcfa3af52010-12-14 10:36:11 -0800272 Tab getActiveTab() {
273 return mActiveTab;
274 }
275
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800277 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800278 }
279
280 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800282 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800284 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 }
287
288 @Override
289 public void detachTab(Tab tab) {
290 removeTabFromContentView(tab);
291 }
292
293 @Override
294 public void attachTab(Tab tab) {
295 attachTabToContentView(tab);
296 }
297
Michael Kolb377ea312011-02-17 14:36:56 -0800298 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800299 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700300 return;
301 }
302 View container = tab.getViewContainer();
303 WebView mainView = tab.getWebView();
304 // Attach the WebView to the container and then attach the
305 // container to the content view.
306 FrameLayout wrapper =
307 (FrameLayout) container.findViewById(R.id.webview_wrapper);
308 ViewGroup parent = (ViewGroup) mainView.getParent();
309 if (parent != wrapper) {
310 if (parent != null) {
311 Log.w(LOGTAG, "mMainView already has a parent in"
312 + " attachTabToContentView!");
313 parent.removeView(mainView);
314 }
315 wrapper.addView(mainView);
316 } else {
317 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
318 + " attachTabToContentView!");
319 }
320 parent = (ViewGroup) container.getParent();
321 if (parent != mContentView) {
322 if (parent != null) {
323 Log.w(LOGTAG, "mContainer already has a parent in"
324 + " attachTabToContentView!");
325 parent.removeView(container);
326 }
327 mContentView.addView(container, COVER_SCREEN_PARAMS);
328 } else {
329 Log.w(LOGTAG, "mContainer is already attached to content in"
330 + " attachTabToContentView!");
331 }
332 mUiController.attachSubWindow(tab);
333 }
334
335 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800336 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700337 // Remove the container that contains the main WebView.
338 WebView mainView = tab.getWebView();
339 View container = tab.getViewContainer();
340 if (mainView == null) {
341 return;
342 }
343 // Remove the container from the content and then remove the
344 // WebView from the container. This will trigger a focus change
345 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800346 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700347 FrameLayout wrapper =
348 (FrameLayout) container.findViewById(R.id.webview_wrapper);
349 wrapper.removeView(mainView);
350 mContentView.removeView(container);
351 mUiController.endActionMode();
352 mUiController.removeSubWindow(tab);
353 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
354 if (errorConsole != null) {
355 mErrorConsoleContainer.removeView(errorConsole);
356 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 }
358
Michael Kolba713ec82010-11-29 17:27:06 -0800359 @Override
360 public void onSetWebView(Tab tab, WebView webView) {
361 View container = tab.getViewContainer();
362 if (container == null) {
363 // The tab consists of a container view, which contains the main
364 // WebView, as well as any other UI elements associated with the tab.
365 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700366 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800367 tab.setViewContainer(container);
368 }
369 if (tab.getWebView() != webView) {
370 // Just remove the old one.
371 FrameLayout wrapper =
372 (FrameLayout) container.findViewById(R.id.webview_wrapper);
373 wrapper.removeView(tab.getWebView());
374 }
375 }
376
Michael Kolb8233fac2010-10-26 16:08:53 -0700377 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800378 * create a sub window container and webview for the tab
379 * Note: this methods operates through side-effects for now
380 * it sets both the subView and subViewContainer for the given tab
381 * @param tab tab to create the sub window for
382 * @param subView webview to be set as a subwindow for the tab
383 */
384 @Override
385 public void createSubWindow(Tab tab, WebView subView) {
386 View subViewContainer = mActivity.getLayoutInflater().inflate(
387 R.layout.browser_subwindow, null);
388 ViewGroup inner = (ViewGroup) subViewContainer
389 .findViewById(R.id.inner_container);
390 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
391 LayoutParams.MATCH_PARENT));
392 final ImageButton cancel = (ImageButton) subViewContainer
393 .findViewById(R.id.subwindow_close);
394 final WebView cancelSubView = subView;
395 cancel.setOnClickListener(new OnClickListener() {
396 public void onClick(View v) {
397 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
398 }
399 });
400 tab.setSubWebView(subView);
401 tab.setSubViewContainer(subViewContainer);
402 }
403
404 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700405 * Remove the sub window from the content view.
406 */
407 @Override
408 public void removeSubWindow(View subviewContainer) {
409 mContentView.removeView(subviewContainer);
410 mUiController.endActionMode();
411 }
412
413 /**
414 * Attach the sub window to the content view.
415 */
416 @Override
417 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800418 if (container.getParent() != null) {
419 // already attached, remove first
420 ((ViewGroup) container.getParent()).removeView(container);
421 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700422 mContentView.addView(container, COVER_SCREEN_PARAMS);
423 }
424
Michael Kolb11d19782011-03-20 10:17:40 -0700425 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700426 WebView web = getWebView();
427 if (web != null) {
428 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700429 }
430 }
431
Michael Kolb46f987e2011-04-05 10:39:10 -0700432 public void editUrl(boolean clearInput) {
433 if (mUiController.isInCustomActionMode()) {
434 mUiController.endActionMode();
435 }
436 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700437 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
John Reckef654f12011-07-12 16:42:08 -0700438 mNavigationBar.startEditingUrl(clearInput);
439 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700440 }
441
Michael Kolb7cdc4902011-02-03 17:54:40 -0800442 boolean canShowTitleBar() {
443 return !isTitleBarShowing()
444 && !isActivityPaused()
445 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700446 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800447 && !mUiController.isInCustomActionMode();
448 }
449
John Reck0f602f32011-07-07 15:38:43 -0700450 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700451 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700452 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700453 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700454 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800455 }
456
457 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700458 if (mTitleBar.isShowing()) {
459 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700460 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800461 }
462
463 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700464 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800465 }
466
John Reck5d43ce82011-06-21 17:16:51 -0700467 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700468 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700469 }
470
John Reck0f602f32011-07-07 15:38:43 -0700471 public TitleBar getTitleBar() {
472 return mTitleBar;
473 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800474
475 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700476 WebView web = getWebView();
477 if (web != null) {
478 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700479 }
480 }
481
Michael Kolb66706532010-12-12 19:50:22 -0800482 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700483 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700484 mNavigationBar.setInVoiceMode(true, results);
485 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700486 }
487
Michael Kolb66706532010-12-12 19:50:22 -0800488 @Override
489 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700490 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800491 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700492 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 }
494
495 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700496 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700497 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700498 }
499
500 @Override
John Reck2bc80422011-06-30 15:11:49 -0700501 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700502 Intent intent = new Intent(mActivity, ComboViewActivity.class);
503 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
504 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
505 Tab t = getActiveTab();
506 if (t != null) {
507 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800508 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700509 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 }
511
512 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400513 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700514 WebChromeClient.CustomViewCallback callback) {
515 // if a view already exists then immediately terminate the new one
516 if (mCustomView != null) {
517 callback.onCustomViewHidden();
518 return;
519 }
520
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400521 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb31065b12011-10-06 13:51:32 -0700522 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
523 mFullscreenContainer = new FullscreenHolder(mActivity);
524 mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
525 decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
526 mCustomView = view;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700527 setFullscreen(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 mCustomViewCallback = callback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400529 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 }
531
532 @Override
533 public void onHideCustomView() {
534 if (mCustomView == null)
535 return;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700536 setFullscreen(false);
Michael Kolb31065b12011-10-06 13:51:32 -0700537 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
538 decor.removeView(mFullscreenContainer);
539 mFullscreenContainer = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700541 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 }
545
546 @Override
547 public boolean isCustomViewShowing() {
548 return mCustomView != null;
549 }
550
Michael Kolb66706532010-12-12 19:50:22 -0800551 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800552 if (mInputManager.isActive()) {
553 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
554 0);
555 }
556 }
557
Michael Kolb66706532010-12-12 19:50:22 -0800558 @Override
John Reck3ba45532011-08-11 16:26:53 -0700559 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700560 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800561 }
562
Patrick Scott92066772011-03-10 08:46:27 -0500563 @Override
564 public void showAutoLogin(Tab tab) {
565 updateAutoLogin(tab, true);
566 }
567
568 @Override
569 public void hideAutoLogin(Tab tab) {
570 updateAutoLogin(tab, true);
571 }
572
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 // -------------------------------------------------------------------------
574
Michael Kolb5a72f182011-01-13 20:35:06 -0800575 protected void updateNavigationState(Tab tab) {
576 }
577
Michael Kolb11d19782011-03-20 10:17:40 -0700578 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700579 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700580 }
Patrick Scott92066772011-03-10 08:46:27 -0500581
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 /**
583 * Update the lock icon to correspond to our latest state.
584 */
Michael Kolb66706532010-12-12 19:50:22 -0800585 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800586 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100587 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 }
589 }
590
591 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700592 * Updates the lock-icon image in the title-bar.
593 */
Steve Block2466eff2011-10-03 15:33:09 +0100594 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700595 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100596 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
597 d = mLockIconSecure;
Steve Block4895b012011-10-03 16:26:46 +0100598 } else if (securityState == SecurityState.SECURITY_STATE_MIXED
599 || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
600 // TODO: It would be good to have different icons for insecure vs mixed content.
601 // See http://b/5403800
Steve Block2466eff2011-10-03 15:33:09 +0100602 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 }
John Reck0f602f32011-07-07 15:38:43 -0700604 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700605 }
606
John Reck30c714c2010-12-16 17:30:34 -0800607 protected void setUrlTitle(Tab tab) {
608 String url = tab.getUrl();
609 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800610 if (TextUtils.isEmpty(title)) {
611 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800612 }
Michael Kolb66706532010-12-12 19:50:22 -0800613 if (tab.isInVoiceSearchMode()) return;
614 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700615 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800616 }
617 }
618
619 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800620 protected void setFavicon(Tab tab) {
621 if (tab.inForeground()) {
622 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700623 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800624 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 }
626
627 @Override
628 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 }
630
Michael Kolb66706532010-12-12 19:50:22 -0800631 // active tabs page
632
633 public void showActiveTabsPage() {
634 }
635
636 /**
637 * Remove the active tabs page.
638 */
639 public void removeActiveTabsPage() {
640 }
641
Michael Kolb8233fac2010-10-26 16:08:53 -0700642 // menu handling callbacks
643
644 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800645 public boolean onPrepareOptionsMenu(Menu menu) {
646 return true;
647 }
648
649 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700650 public void updateMenuState(Tab tab, Menu menu) {
651 }
652
653 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700654 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 }
656
657 @Override
658 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700659 }
660
661 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700662 public boolean onOptionsItemSelected(MenuItem item) {
663 return false;
664 }
665
666 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 }
669
670 @Override
671 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 }
673
674 @Override
675 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 }
677
678 @Override
679 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 }
681
682 // error console
683
684 @Override
685 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800686 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
688 if (flag) {
689 // Setting the show state of the console will cause it's the layout
690 // to be inflated.
691 if (errorConsole.numberOfErrors() > 0) {
692 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
693 } else {
694 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
695 }
696 if (errorConsole.getParent() != null) {
697 mErrorConsoleContainer.removeView(errorConsole);
698 }
699 // Now we can add it to the main view.
700 mErrorConsoleContainer.addView(errorConsole,
701 new LinearLayout.LayoutParams(
702 ViewGroup.LayoutParams.MATCH_PARENT,
703 ViewGroup.LayoutParams.WRAP_CONTENT));
704 } else {
705 mErrorConsoleContainer.removeView(errorConsole);
706 }
707 }
708
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 // -------------------------------------------------------------------------
710 // Helper function for WebChromeClient
711 // -------------------------------------------------------------------------
712
713 @Override
714 public Bitmap getDefaultVideoPoster() {
715 if (mDefaultVideoPoster == null) {
716 mDefaultVideoPoster = BitmapFactory.decodeResource(
717 mActivity.getResources(), R.drawable.default_video_poster);
718 }
719 return mDefaultVideoPoster;
720 }
721
722 @Override
723 public View getVideoLoadingProgressView() {
724 if (mVideoProgressView == null) {
725 LayoutInflater inflater = LayoutInflater.from(mActivity);
726 mVideoProgressView = inflater.inflate(
727 R.layout.video_loading_progress, null);
728 }
729 return mVideoProgressView;
730 }
731
Michael Kolb843510f2010-12-09 10:51:49 -0800732 @Override
733 public void showMaxTabsWarning() {
734 Toast warning = Toast.makeText(mActivity,
735 mActivity.getString(R.string.max_tabs_warning),
736 Toast.LENGTH_SHORT);
737 warning.show();
738 }
739
Michael Kolb46f987e2011-04-05 10:39:10 -0700740 protected WebView getWebView() {
741 if (mActiveTab != null) {
742 return mActiveTab.getWebView();
743 } else {
744 return null;
745 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700746 }
747
Michael Kolbfedb4922011-04-20 16:45:33 -0700748 protected Menu getMenu() {
749 MenuBuilder menu = new MenuBuilder(mActivity);
750 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
751 return menu;
752 }
753
Michael Kolbc38c6042011-04-27 10:46:06 -0700754 public void setFullscreen(boolean enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700755 Window win = mActivity.getWindow();
756 WindowManager.LayoutParams winParams = win.getAttributes();
757 final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
Michael Kolbc38c6042011-04-27 10:46:06 -0700758 if (enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700759 winParams.flags |= bits;
760 if (mCustomView != null) {
761 mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
762 } else {
763 mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
764 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700765 } else {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700766 winParams.flags &= ~bits;
767 if (mCustomView != null) {
768 mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
769 } else {
770 mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
771 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700772 }
Michael Kolbc5675ad2011-10-21 13:34:28 -0700773 win.setAttributes(winParams);
Michael Kolbc38c6042011-04-27 10:46:06 -0700774 }
775
John Reck0f602f32011-07-07 15:38:43 -0700776 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700777 Drawable[] array = new Drawable[3];
778 array[0] = new PaintDrawable(Color.BLACK);
779 PaintDrawable p = new PaintDrawable(Color.WHITE);
780 array[1] = p;
781 if (icon == null) {
782 array[2] = mGenericFavicon;
783 } else {
784 array[2] = new BitmapDrawable(icon);
785 }
786 LayerDrawable d = new LayerDrawable(array);
787 d.setLayerInset(1, 1, 1, 1, 1);
788 d.setLayerInset(2, 2, 2, 2, 2);
789 return d;
790 }
791
John Reck5d43ce82011-06-21 17:16:51 -0700792 public boolean isLoading() {
793 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
794 }
795
796 /**
797 * Suggest to the UI that the title bar can be hidden. The UI will then
798 * decide whether or not to hide based off a number of factors, such
799 * as if the user is editing the URL bar or if the page is loading
800 */
801 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700802 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
803 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700804 hideTitleBar();
805 }
806 }
807
John Reckbc6adb42011-09-01 18:03:20 -0700808 protected final void showTitleBarForDuration() {
809 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
810 }
811
812 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700813 showTitleBar();
814 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700815 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700816 }
817
John Reck9c5004e2011-10-07 16:00:12 -0700818 protected Handler mHandler = new Handler() {
John Reck5d43ce82011-06-21 17:16:51 -0700819
820 @Override
821 public void handleMessage(Message msg) {
822 if (msg.what == MSG_HIDE_TITLEBAR) {
823 suggestHideTitleBar();
824 }
John Reck9c5004e2011-10-07 16:00:12 -0700825 BaseUi.this.handleMessage(msg);
John Reck5d43ce82011-06-21 17:16:51 -0700826 }
827 };
John Reck3ba45532011-08-11 16:26:53 -0700828
John Reck9c5004e2011-10-07 16:00:12 -0700829 protected void handleMessage(Message msg) {}
830
John Reck3ba45532011-08-11 16:26:53 -0700831 @Override
832 public void showWeb(boolean animate) {
833 mUiController.hideCustomView();
834 }
835
Michael Kolb31065b12011-10-06 13:51:32 -0700836 static class FullscreenHolder extends FrameLayout {
Michael Kolb53ed62c2011-10-04 16:18:44 -0700837
Michael Kolb31065b12011-10-06 13:51:32 -0700838 public FullscreenHolder(Context ctx) {
839 super(ctx);
840 setBackgroundColor(ctx.getResources().getColor(R.color.black));
Michael Kolb53ed62c2011-10-04 16:18:44 -0700841 }
842
Michael Kolb31065b12011-10-06 13:51:32 -0700843 @Override
844 public boolean onTouchEvent(MotionEvent evt) {
845 return true;
Michael Kolb53ed62c2011-10-04 16:18:44 -0700846 }
Michael Kolb31065b12011-10-06 13:51:32 -0700847
Michael Kolb53ed62c2011-10-04 16:18:44 -0700848 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700849}