blob: e3f5986e44a98efc0038f97be5002245b4a60f0e [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;
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;
Michael Kolb31065b12011-10-06 13:51:32 -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;
Michael Kolb8233fac2010-10-26 16:08:53 -070042import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080043import android.view.ViewGroup.LayoutParams;
Michael Kolbc5675ad2011-10-21 13:34:28 -070044import android.view.Window;
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;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000049import android.webkit.WebViewClassic;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import 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;
John Reck2711fab2012-05-18 21:38:59 -070094 private FrameLayout mFixedTitlebarContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070095
Michael Kolb31065b12011-10-06 13:51:32 -070096 private View mCustomView;
Michael Kolb8233fac2010-10-26 16:08:53 -070097 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040098 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070099
Michael Kolb8233fac2010-10-26 16:08:53 -0700100 private LinearLayout mErrorConsoleContainer = null;
101
John Reck718a24d2011-08-12 11:08:30 -0700102 private UrlBarAutoShowManager mUrlBarAutoShowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -0700103
John Reck718a24d2011-08-12 11:08:30 -0700104 private Toast mStopToast;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106 // the default <video> poster
107 private Bitmap mDefaultVideoPoster;
108 // the video progress view
109 private View mVideoProgressView;
110
Michael Kolb8233fac2010-10-26 16:08:53 -0700111 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700112 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700113 protected TitleBar mTitleBar;
114 private NavigationBarBase mNavigationBar;
Michael Kolbda580632012-04-16 13:30:28 -0700115 protected PieControl mPieControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116
117 public BaseUi(Activity browser, UiController controller) {
118 mActivity = browser;
119 mUiController = controller;
120 mTabControl = controller.getTabControl();
121 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800122 mInputManager = (InputMethodManager)
123 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Steve Block2466eff2011-10-03 15:33:09 +0100124 mLockIconSecure = res.getDrawable(R.drawable.ic_secure_holo_dark);
125 mLockIconMixed = res.getDrawable(R.drawable.ic_secure_partial_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
127 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700128 LayoutInflater.from(mActivity)
129 .inflate(R.layout.custom_screen, frameLayout);
John Reck2711fab2012-05-18 21:38:59 -0700130 mFixedTitlebarContainer = (FrameLayout) frameLayout.findViewById(
131 R.id.fixed_titlebar_container);
John Reck7c6e1c92011-06-30 11:55:55 -0700132 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 R.id.main_content);
Michael Kolb53ed62c2011-10-04 16:18:44 -0700134 mCustomViewContainer = (FrameLayout) frameLayout.findViewById(
135 R.id.fullscreen_custom_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700136 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700137 .findViewById(R.id.error_console);
Michael Kolbc38c6042011-04-27 10:46:06 -0700138 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700139 mGenericFavicon = res.getDrawable(
140 R.drawable.app_web_browser_sm);
John Reck0f602f32011-07-07 15:38:43 -0700141 mTitleBar = new TitleBar(mActivity, mUiController, this,
142 mContentView);
143 mTitleBar.setProgress(100);
144 mNavigationBar = mTitleBar.getNavigationBar();
John Reck718a24d2011-08-12 11:08:30 -0700145 mUrlBarAutoShowManager = new UrlBarAutoShowManager(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700146 }
147
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 private void cancelStopToast() {
149 if (mStopToast != null) {
150 mStopToast.cancel();
151 mStopToast = null;
152 }
153 }
154
155 // lifecycle
156
157 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800158 if (isCustomViewShowing()) {
159 onHideCustomView();
160 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 cancelStopToast();
162 mActivityPaused = true;
163 }
164
165 public void onResume() {
166 mActivityPaused = false;
Michael Kolb2ae6ef72011-08-22 14:49:34 -0700167 // check if we exited without setting active tab
168 // b: 5188145
Michael Kolb1a4625a2011-08-24 13:40:44 -0700169 final Tab ct = mTabControl.getCurrentTab();
170 if (ct != null) {
171 setActiveTab(ct);
172 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 }
174
Michael Kolb66706532010-12-12 19:50:22 -0800175 protected boolean isActivityPaused() {
176 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700177 }
178
179 public void onConfigurationChanged(Configuration config) {
180 }
181
John Reck0f602f32011-07-07 15:38:43 -0700182 public Activity getActivity() {
183 return mActivity;
184 }
185
Michael Kolb8233fac2010-10-26 16:08:53 -0700186 // key handling
187
188 @Override
189 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700190 if (mCustomView != null) {
191 mUiController.hideCustomView();
192 return true;
193 }
194 return false;
195 }
196
Michael Kolb2814a362011-05-19 15:49:41 -0700197 @Override
198 public boolean onMenuKey() {
199 return false;
200 }
201
Michael Kolbda580632012-04-16 13:30:28 -0700202 @Override
203 public void setUseQuickControls(boolean useQuickControls) {
204 mUseQuickControls = useQuickControls;
205 mTitleBar.setUseQuickControls(mUseQuickControls);
206 if (useQuickControls) {
207 mPieControl = new PieControl(mActivity, mUiController, this);
208 mPieControl.attachToContainer(mContentView);
209 } else {
210 if (mPieControl != null) {
211 mPieControl.removeFromContainer(mContentView);
212 }
213 }
214 updateUrlBarAutoShowManagerTarget();
215 }
216
John Reck30c714c2010-12-16 17:30:34 -0800217 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 @Override
John Reck30c714c2010-12-16 17:30:34 -0800219 public void onTabDataChanged(Tab tab) {
220 setUrlTitle(tab);
221 setFavicon(tab);
222 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800223 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700224 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700225 mNavigationBar.onTabDataChanged(tab);
Michael Kolba53c9892011-10-05 13:31:40 -0700226 onProgressChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 }
228
229 @Override
Michael Kolbe8a82332012-04-25 14:14:26 -0700230 public void onProgressChanged(Tab tab) {
231 int progress = tab.getLoadProgress();
232 if (tab.inForeground()) {
233 mTitleBar.setProgress(progress);
234 }
235 }
236
237 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500238 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800239 if (tab.inForeground()) {
240 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700241 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800242 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500243 }
244
245 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700246 public void onPageStopped(Tab tab) {
247 cancelStopToast();
248 if (tab.inForeground()) {
249 mStopToast = Toast
250 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
251 mStopToast.show();
252 }
253 }
254
255 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800256 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700257 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800258 }
259
260 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 }
263
264 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800265 public void setActiveTab(final Tab tab) {
Michael Kolb7ac63b62011-12-16 09:52:29 -0800266 if (tab == null) return;
John Reck5d43ce82011-06-21 17:16:51 -0700267 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800268 if ((tab != mActiveTab) && (mActiveTab != null)) {
269 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700270 WebView web = mActiveTab.getWebView();
271 if (web != null) {
272 web.setOnTouchListener(null);
273 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 }
Michael Kolb77df4562010-11-19 14:49:34 -0800275 mActiveTab = tab;
Michael Kolbda580632012-04-16 13:30:28 -0700276 BrowserWebView web = (BrowserWebView) mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700277 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700278 attachTabToContentView(tab);
Michael Kolbda580632012-04-16 13:30:28 -0700279 if (web != null) {
280 // Request focus on the top window.
281 if (mUseQuickControls) {
282 mPieControl.forceToTop(mContentView);
283 web.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700284 mTitleBar.hide();
Michael Kolbda580632012-04-16 13:30:28 -0700285 } else {
286 web.setTitleBar(mTitleBar);
287 mTitleBar.onScrollChanged();
288 }
289 }
Michael Kolb4923c222012-04-02 16:18:36 -0700290 mTitleBar.bringToFront();
Michael Kolbf8963522012-04-10 10:52:34 -0700291 tab.getTopWindow().requestFocus();
Michael Kolb8233fac2010-10-26 16:08:53 -0700292 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800293 onTabDataChanged(tab);
294 onProgressChanged(tab);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700295 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
Patrick Scott92066772011-03-10 08:46:27 -0500296 updateAutoLogin(tab, false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700297 }
298
John Reck718a24d2011-08-12 11:08:30 -0700299 protected void updateUrlBarAutoShowManagerTarget() {
300 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
301 if (!mUseQuickControls && web instanceof BrowserWebView) {
302 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
303 } else {
304 mUrlBarAutoShowManager.setTarget(null);
305 }
306 }
307
Michael Kolbcfa3af52010-12-14 10:36:11 -0800308 Tab getActiveTab() {
309 return mActiveTab;
310 }
311
Michael Kolb8233fac2010-10-26 16:08:53 -0700312 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800313 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800314 }
315
316 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700317 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800318 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700319 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800320 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700321 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700322 }
323
324 @Override
325 public void detachTab(Tab tab) {
326 removeTabFromContentView(tab);
327 }
328
329 @Override
330 public void attachTab(Tab tab) {
331 attachTabToContentView(tab);
332 }
333
Michael Kolb377ea312011-02-17 14:36:56 -0800334 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800335 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700336 return;
337 }
338 View container = tab.getViewContainer();
339 WebView mainView = tab.getWebView();
340 // Attach the WebView to the container and then attach the
341 // container to the content view.
342 FrameLayout wrapper =
343 (FrameLayout) container.findViewById(R.id.webview_wrapper);
344 ViewGroup parent = (ViewGroup) mainView.getParent();
345 if (parent != wrapper) {
346 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700347 parent.removeView(mainView);
348 }
349 wrapper.addView(mainView);
Michael Kolb8233fac2010-10-26 16:08:53 -0700350 }
351 parent = (ViewGroup) container.getParent();
352 if (parent != mContentView) {
353 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 parent.removeView(container);
355 }
356 mContentView.addView(container, COVER_SCREEN_PARAMS);
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 }
358 mUiController.attachSubWindow(tab);
359 }
360
361 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800362 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 // Remove the container that contains the main WebView.
364 WebView mainView = tab.getWebView();
365 View container = tab.getViewContainer();
366 if (mainView == null) {
367 return;
368 }
369 // Remove the container from the content and then remove the
370 // WebView from the container. This will trigger a focus change
371 // needed by WebView.
372 FrameLayout wrapper =
373 (FrameLayout) container.findViewById(R.id.webview_wrapper);
374 wrapper.removeView(mainView);
375 mContentView.removeView(container);
376 mUiController.endActionMode();
377 mUiController.removeSubWindow(tab);
378 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
379 if (errorConsole != null) {
380 mErrorConsoleContainer.removeView(errorConsole);
381 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700382 }
383
Michael Kolba713ec82010-11-29 17:27:06 -0800384 @Override
385 public void onSetWebView(Tab tab, WebView webView) {
386 View container = tab.getViewContainer();
387 if (container == null) {
388 // The tab consists of a container view, which contains the main
389 // WebView, as well as any other UI elements associated with the tab.
390 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700391 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800392 tab.setViewContainer(container);
393 }
394 if (tab.getWebView() != webView) {
395 // Just remove the old one.
396 FrameLayout wrapper =
397 (FrameLayout) container.findViewById(R.id.webview_wrapper);
398 wrapper.removeView(tab.getWebView());
399 }
400 }
401
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800403 * create a sub window container and webview for the tab
404 * Note: this methods operates through side-effects for now
405 * it sets both the subView and subViewContainer for the given tab
406 * @param tab tab to create the sub window for
407 * @param subView webview to be set as a subwindow for the tab
408 */
409 @Override
410 public void createSubWindow(Tab tab, WebView subView) {
411 View subViewContainer = mActivity.getLayoutInflater().inflate(
412 R.layout.browser_subwindow, null);
413 ViewGroup inner = (ViewGroup) subViewContainer
414 .findViewById(R.id.inner_container);
415 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
416 LayoutParams.MATCH_PARENT));
417 final ImageButton cancel = (ImageButton) subViewContainer
418 .findViewById(R.id.subwindow_close);
419 final WebView cancelSubView = subView;
420 cancel.setOnClickListener(new OnClickListener() {
421 public void onClick(View v) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000422 WebViewClassic.fromWebView(cancelSubView).getWebChromeClient().onCloseWindow(
423 cancelSubView);
Michael Kolb1514bb72010-11-22 09:11:48 -0800424 }
425 });
426 tab.setSubWebView(subView);
427 tab.setSubViewContainer(subViewContainer);
428 }
429
430 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700431 * Remove the sub window from the content view.
432 */
433 @Override
434 public void removeSubWindow(View subviewContainer) {
435 mContentView.removeView(subviewContainer);
436 mUiController.endActionMode();
437 }
438
439 /**
440 * Attach the sub window to the content view.
441 */
442 @Override
443 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800444 if (container.getParent() != null) {
445 // already attached, remove first
446 ((ViewGroup) container.getParent()).removeView(container);
447 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700448 mContentView.addView(container, COVER_SCREEN_PARAMS);
449 }
450
Michael Kolb11d19782011-03-20 10:17:40 -0700451 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700452 WebView web = getWebView();
453 if (web != null) {
454 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700455 }
456 }
457
Michael Kolb1f9b3562012-04-24 14:38:34 -0700458 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700459 if (mUiController.isInCustomActionMode()) {
460 mUiController.endActionMode();
461 }
462 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700463 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700464 mNavigationBar.startEditingUrl(clearInput, forceIME);
John Reckef654f12011-07-12 16:42:08 -0700465 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700466 }
467
Michael Kolb7cdc4902011-02-03 17:54:40 -0800468 boolean canShowTitleBar() {
469 return !isTitleBarShowing()
470 && !isActivityPaused()
471 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700472 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800473 && !mUiController.isInCustomActionMode();
474 }
475
John Reck0f602f32011-07-07 15:38:43 -0700476 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700477 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700478 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700479 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700480 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800481 }
482
483 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700484 if (mTitleBar.isShowing()) {
485 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700486 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800487 }
488
489 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700490 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800491 }
492
John Reck5d43ce82011-06-21 17:16:51 -0700493 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700494 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700495 }
496
Michael Kolb80f75082012-04-10 10:50:06 -0700497 public void stopEditingUrl() {
498 mTitleBar.getNavigationBar().stopEditingUrl();
499 }
500
John Reck0f602f32011-07-07 15:38:43 -0700501 public TitleBar getTitleBar() {
502 return mTitleBar;
503 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800504
Michael Kolb66706532010-12-12 19:50:22 -0800505 @Override
John Reck2bc80422011-06-30 15:11:49 -0700506 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700507 Intent intent = new Intent(mActivity, ComboViewActivity.class);
508 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
509 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
510 Tab t = getActiveTab();
511 if (t != null) {
512 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800513 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700514 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700515 }
516
517 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400518 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 WebChromeClient.CustomViewCallback callback) {
520 // if a view already exists then immediately terminate the new one
521 if (mCustomView != null) {
522 callback.onCustomViewHidden();
523 return;
524 }
525
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400526 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb31065b12011-10-06 13:51:32 -0700527 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
528 mFullscreenContainer = new FullscreenHolder(mActivity);
529 mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
530 decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
531 mCustomView = view;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700532 setFullscreen(true);
Michael Kolb54217b32012-05-15 13:24:24 -0700533 ((BrowserWebView) getWebView()).setVisibility(View.INVISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700534 mCustomViewCallback = callback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400535 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 }
537
538 @Override
539 public void onHideCustomView() {
Michael Kolb54217b32012-05-15 13:24:24 -0700540 ((BrowserWebView) getWebView()).setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700541 if (mCustomView == null)
542 return;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700543 setFullscreen(false);
Michael Kolb31065b12011-10-06 13:51:32 -0700544 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
545 decor.removeView(mFullscreenContainer);
546 mFullscreenContainer = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700547 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700548 mCustomViewCallback.onCustomViewHidden();
549 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400550 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700551 }
552
553 @Override
554 public boolean isCustomViewShowing() {
555 return mCustomView != null;
556 }
557
Michael Kolb66706532010-12-12 19:50:22 -0800558 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800559 if (mInputManager.isActive()) {
560 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
561 0);
562 }
563 }
564
Michael Kolb66706532010-12-12 19:50:22 -0800565 @Override
John Reck3ba45532011-08-11 16:26:53 -0700566 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700567 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800568 }
569
Patrick Scott92066772011-03-10 08:46:27 -0500570 @Override
571 public void showAutoLogin(Tab tab) {
572 updateAutoLogin(tab, true);
573 }
574
575 @Override
576 public void hideAutoLogin(Tab tab) {
577 updateAutoLogin(tab, true);
578 }
579
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 // -------------------------------------------------------------------------
581
Michael Kolb5a72f182011-01-13 20:35:06 -0800582 protected void updateNavigationState(Tab tab) {
583 }
584
Michael Kolb11d19782011-03-20 10:17:40 -0700585 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700586 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700587 }
Patrick Scott92066772011-03-10 08:46:27 -0500588
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 /**
590 * Update the lock icon to correspond to our latest state.
591 */
Michael Kolb66706532010-12-12 19:50:22 -0800592 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800593 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100594 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700595 }
596 }
597
598 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 * Updates the lock-icon image in the title-bar.
600 */
Steve Block2466eff2011-10-03 15:33:09 +0100601 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700602 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100603 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
604 d = mLockIconSecure;
Steve Block4895b012011-10-03 16:26:46 +0100605 } else if (securityState == SecurityState.SECURITY_STATE_MIXED
606 || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
607 // TODO: It would be good to have different icons for insecure vs mixed content.
608 // See http://b/5403800
Steve Block2466eff2011-10-03 15:33:09 +0100609 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700610 }
John Reck0f602f32011-07-07 15:38:43 -0700611 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 }
613
John Reck30c714c2010-12-16 17:30:34 -0800614 protected void setUrlTitle(Tab tab) {
615 String url = tab.getUrl();
616 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800617 if (TextUtils.isEmpty(title)) {
618 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800619 }
Michael Kolb66706532010-12-12 19:50:22 -0800620 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700621 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800622 }
623 }
624
625 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800626 protected void setFavicon(Tab tab) {
627 if (tab.inForeground()) {
628 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700629 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800630 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700631 }
632
633 @Override
634 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 }
636
Michael Kolb66706532010-12-12 19:50:22 -0800637 // active tabs page
638
639 public void showActiveTabsPage() {
640 }
641
642 /**
643 * Remove the active tabs page.
644 */
645 public void removeActiveTabsPage() {
646 }
647
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 // menu handling callbacks
649
650 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800651 public boolean onPrepareOptionsMenu(Menu menu) {
652 return true;
653 }
654
655 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700656 public void updateMenuState(Tab tab, Menu menu) {
657 }
658
659 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 }
662
663 @Override
664 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 }
666
667 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700668 public boolean onOptionsItemSelected(MenuItem item) {
669 return false;
670 }
671
672 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700674 }
675
676 @Override
677 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 }
679
680 @Override
681 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
683
684 @Override
685 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
688 // error console
689
690 @Override
691 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800692 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
694 if (flag) {
695 // Setting the show state of the console will cause it's the layout
696 // to be inflated.
697 if (errorConsole.numberOfErrors() > 0) {
698 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
699 } else {
700 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
701 }
702 if (errorConsole.getParent() != null) {
703 mErrorConsoleContainer.removeView(errorConsole);
704 }
705 // Now we can add it to the main view.
706 mErrorConsoleContainer.addView(errorConsole,
707 new LinearLayout.LayoutParams(
708 ViewGroup.LayoutParams.MATCH_PARENT,
709 ViewGroup.LayoutParams.WRAP_CONTENT));
710 } else {
711 mErrorConsoleContainer.removeView(errorConsole);
712 }
713 }
714
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 // -------------------------------------------------------------------------
716 // Helper function for WebChromeClient
717 // -------------------------------------------------------------------------
718
719 @Override
720 public Bitmap getDefaultVideoPoster() {
721 if (mDefaultVideoPoster == null) {
722 mDefaultVideoPoster = BitmapFactory.decodeResource(
723 mActivity.getResources(), R.drawable.default_video_poster);
724 }
725 return mDefaultVideoPoster;
726 }
727
728 @Override
729 public View getVideoLoadingProgressView() {
730 if (mVideoProgressView == null) {
731 LayoutInflater inflater = LayoutInflater.from(mActivity);
732 mVideoProgressView = inflater.inflate(
733 R.layout.video_loading_progress, null);
734 }
735 return mVideoProgressView;
736 }
737
Michael Kolb843510f2010-12-09 10:51:49 -0800738 @Override
739 public void showMaxTabsWarning() {
740 Toast warning = Toast.makeText(mActivity,
741 mActivity.getString(R.string.max_tabs_warning),
742 Toast.LENGTH_SHORT);
743 warning.show();
744 }
745
Michael Kolb46f987e2011-04-05 10:39:10 -0700746 protected WebView getWebView() {
747 if (mActiveTab != null) {
748 return mActiveTab.getWebView();
749 } else {
750 return null;
751 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700752 }
753
Michael Kolbfedb4922011-04-20 16:45:33 -0700754 protected Menu getMenu() {
755 MenuBuilder menu = new MenuBuilder(mActivity);
756 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
757 return menu;
758 }
759
Michael Kolbc38c6042011-04-27 10:46:06 -0700760 public void setFullscreen(boolean enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700761 Window win = mActivity.getWindow();
762 WindowManager.LayoutParams winParams = win.getAttributes();
Michael Kolb76dff392011-12-06 09:51:18 -0800763 final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
Michael Kolbc38c6042011-04-27 10:46:06 -0700764 if (enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700765 winParams.flags |= bits;
Michael Kolbc38c6042011-04-27 10:46:06 -0700766 } else {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700767 winParams.flags &= ~bits;
768 if (mCustomView != null) {
769 mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
770 } else {
771 mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
772 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700773 }
Michael Kolbc5675ad2011-10-21 13:34:28 -0700774 win.setAttributes(winParams);
Michael Kolbc38c6042011-04-27 10:46:06 -0700775 }
776
John Reck0f602f32011-07-07 15:38:43 -0700777 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700778 Drawable[] array = new Drawable[3];
779 array[0] = new PaintDrawable(Color.BLACK);
780 PaintDrawable p = new PaintDrawable(Color.WHITE);
781 array[1] = p;
782 if (icon == null) {
783 array[2] = mGenericFavicon;
784 } else {
785 array[2] = new BitmapDrawable(icon);
786 }
787 LayerDrawable d = new LayerDrawable(array);
788 d.setLayerInset(1, 1, 1, 1, 1);
789 d.setLayerInset(2, 2, 2, 2, 2);
790 return d;
791 }
792
John Reck5d43ce82011-06-21 17:16:51 -0700793 public boolean isLoading() {
794 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
795 }
796
797 /**
798 * Suggest to the UI that the title bar can be hidden. The UI will then
799 * decide whether or not to hide based off a number of factors, such
800 * as if the user is editing the URL bar or if the page is loading
801 */
802 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700803 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
804 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700805 hideTitleBar();
806 }
807 }
808
John Reckbc6adb42011-09-01 18:03:20 -0700809 protected final void showTitleBarForDuration() {
810 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
811 }
812
813 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700814 showTitleBar();
815 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700816 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700817 }
818
John Reck9c5004e2011-10-07 16:00:12 -0700819 protected Handler mHandler = new Handler() {
John Reck5d43ce82011-06-21 17:16:51 -0700820
821 @Override
822 public void handleMessage(Message msg) {
823 if (msg.what == MSG_HIDE_TITLEBAR) {
824 suggestHideTitleBar();
825 }
John Reck9c5004e2011-10-07 16:00:12 -0700826 BaseUi.this.handleMessage(msg);
John Reck5d43ce82011-06-21 17:16:51 -0700827 }
828 };
John Reck3ba45532011-08-11 16:26:53 -0700829
John Reck9c5004e2011-10-07 16:00:12 -0700830 protected void handleMessage(Message msg) {}
831
John Reck3ba45532011-08-11 16:26:53 -0700832 @Override
833 public void showWeb(boolean animate) {
834 mUiController.hideCustomView();
835 }
836
Michael Kolb31065b12011-10-06 13:51:32 -0700837 static class FullscreenHolder extends FrameLayout {
Michael Kolb53ed62c2011-10-04 16:18:44 -0700838
Michael Kolb31065b12011-10-06 13:51:32 -0700839 public FullscreenHolder(Context ctx) {
840 super(ctx);
841 setBackgroundColor(ctx.getResources().getColor(R.color.black));
Michael Kolb53ed62c2011-10-04 16:18:44 -0700842 }
843
Michael Kolb31065b12011-10-06 13:51:32 -0700844 @Override
845 public boolean onTouchEvent(MotionEvent evt) {
846 return true;
Michael Kolb53ed62c2011-10-04 16:18:44 -0700847 }
Michael Kolb31065b12011-10-06 13:51:32 -0700848
Michael Kolb53ed62c2011-10-04 16:18:44 -0700849 }
John Reck2711fab2012-05-18 21:38:59 -0700850
851 public void addFixedTitleBar(View view) {
852 mFixedTitlebarContainer.addView(view);
853 }
854
855 public void setContentViewMarginTop(int margin) {
856 LinearLayout.LayoutParams params =
857 (LinearLayout.LayoutParams) mContentView.getLayoutParams();
858 if (params.topMargin != margin) {
859 params.topMargin = margin;
860 mContentView.setLayoutParams(params);
861 }
862 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700863}