blob: f7bd2fbbab6b82e7184d34016f32c38614dd8977 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070018
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080047import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080049import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.widget.LinearLayout;
51import android.widget.Toast;
52
Bijan Amirzada41242f22014-03-21 12:12:18 -070053import com.android.browser.R;
54import com.android.browser.Tab.SecurityState;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080055
56import org.codeaurora.swe.WebView;
John Reckbf2ec202011-06-29 17:47:38 -070057
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080097 private 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 Kolbbae0cb22012-05-29 11:15:27 -0700116 private boolean mBlockFocusAnimations;
Michael Kolb8233fac2010-10-26 16:08:53 -0700117
118 public BaseUi(Activity browser, UiController controller) {
119 mActivity = browser;
120 mUiController = controller;
121 mTabControl = controller.getTabControl();
122 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800123 mInputManager = (InputMethodManager)
124 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Steve Block2466eff2011-10-03 15:33:09 +0100125 mLockIconSecure = res.getDrawable(R.drawable.ic_secure_holo_dark);
126 mLockIconMixed = res.getDrawable(R.drawable.ic_secure_partial_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
128 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700129 LayoutInflater.from(mActivity)
130 .inflate(R.layout.custom_screen, frameLayout);
John Reck2711fab2012-05-18 21:38:59 -0700131 mFixedTitlebarContainer = (FrameLayout) frameLayout.findViewById(
132 R.id.fixed_titlebar_container);
John Reck7c6e1c92011-06-30 11:55:55 -0700133 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 R.id.main_content);
Michael Kolb53ed62c2011-10-04 16:18:44 -0700135 mCustomViewContainer = (FrameLayout) frameLayout.findViewById(
136 R.id.fullscreen_custom_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700137 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700138 .findViewById(R.id.error_console);
Michael Kolbc38c6042011-04-27 10:46:06 -0700139 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700140 mGenericFavicon = res.getDrawable(
141 R.drawable.app_web_browser_sm);
John Reck0f602f32011-07-07 15:38:43 -0700142 mTitleBar = new TitleBar(mActivity, mUiController, this,
143 mContentView);
144 mTitleBar.setProgress(100);
145 mNavigationBar = mTitleBar.getNavigationBar();
John Reck718a24d2011-08-12 11:08:30 -0700146 mUrlBarAutoShowManager = new UrlBarAutoShowManager(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700147 }
148
Michael Kolb8233fac2010-10-26 16:08:53 -0700149 private void cancelStopToast() {
150 if (mStopToast != null) {
151 mStopToast.cancel();
152 mStopToast = null;
153 }
154 }
155
156 // lifecycle
157
158 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800159 if (isCustomViewShowing()) {
160 onHideCustomView();
161 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 cancelStopToast();
163 mActivityPaused = true;
164 }
165
166 public void onResume() {
167 mActivityPaused = false;
Michael Kolb2ae6ef72011-08-22 14:49:34 -0700168 // check if we exited without setting active tab
169 // b: 5188145
Michael Kolb1a4625a2011-08-24 13:40:44 -0700170 final Tab ct = mTabControl.getCurrentTab();
171 if (ct != null) {
172 setActiveTab(ct);
173 }
John Reck1cc1d1d2012-09-04 18:13:51 -0700174 mTitleBar.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700175 }
176
Michael Kolb66706532010-12-12 19:50:22 -0800177 protected boolean isActivityPaused() {
178 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700179 }
180
181 public void onConfigurationChanged(Configuration config) {
182 }
183
John Reck0f602f32011-07-07 15:38:43 -0700184 public Activity getActivity() {
185 return mActivity;
186 }
187
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 // key handling
189
190 @Override
191 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 if (mCustomView != null) {
193 mUiController.hideCustomView();
194 return true;
195 }
196 return false;
197 }
198
Michael Kolb2814a362011-05-19 15:49:41 -0700199 @Override
200 public boolean onMenuKey() {
201 return false;
202 }
203
Michael Kolbda580632012-04-16 13:30:28 -0700204 @Override
205 public void setUseQuickControls(boolean useQuickControls) {
206 mUseQuickControls = useQuickControls;
207 mTitleBar.setUseQuickControls(mUseQuickControls);
208 if (useQuickControls) {
209 mPieControl = new PieControl(mActivity, mUiController, this);
210 mPieControl.attachToContainer(mContentView);
211 } else {
212 if (mPieControl != null) {
213 mPieControl.removeFromContainer(mContentView);
214 }
215 }
216 updateUrlBarAutoShowManagerTarget();
217 }
218
John Reck30c714c2010-12-16 17:30:34 -0800219 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 @Override
John Reck30c714c2010-12-16 17:30:34 -0800221 public void onTabDataChanged(Tab tab) {
222 setUrlTitle(tab);
223 setFavicon(tab);
224 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800225 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700226 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700227 mNavigationBar.onTabDataChanged(tab);
Michael Kolba53c9892011-10-05 13:31:40 -0700228 onProgressChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 }
230
231 @Override
Michael Kolbe8a82332012-04-25 14:14:26 -0700232 public void onProgressChanged(Tab tab) {
233 int progress = tab.getLoadProgress();
234 if (tab.inForeground()) {
235 mTitleBar.setProgress(progress);
236 }
237 }
238
239 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500240 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800241 if (tab.inForeground()) {
242 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700243 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800244 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500245 }
246
247 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 public void onPageStopped(Tab tab) {
249 cancelStopToast();
250 if (tab.inForeground()) {
251 mStopToast = Toast
252 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
253 mStopToast.show();
254 }
255 }
256
257 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800258 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700259 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800260 }
261
262 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 }
265
266 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800267 public void setActiveTab(final Tab tab) {
Michael Kolb7ac63b62011-12-16 09:52:29 -0800268 if (tab == null) return;
Michael Kolbbae0cb22012-05-29 11:15:27 -0700269 // block unnecessary focus change animations during tab switch
270 mBlockFocusAnimations = true;
John Reck5d43ce82011-06-21 17:16:51 -0700271 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800272 if ((tab != mActiveTab) && (mActiveTab != null)) {
273 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700274 WebView web = mActiveTab.getWebView();
275 if (web != null) {
276 web.setOnTouchListener(null);
277 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 }
Michael Kolb77df4562010-11-19 14:49:34 -0800279 mActiveTab = tab;
Michael Kolbda580632012-04-16 13:30:28 -0700280 BrowserWebView web = (BrowserWebView) mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700281 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700282 attachTabToContentView(tab);
Michael Kolbda580632012-04-16 13:30:28 -0700283 if (web != null) {
284 // Request focus on the top window.
285 if (mUseQuickControls) {
286 mPieControl.forceToTop(mContentView);
287 web.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700288 mTitleBar.hide();
Michael Kolbda580632012-04-16 13:30:28 -0700289 } else {
290 web.setTitleBar(mTitleBar);
291 mTitleBar.onScrollChanged();
292 }
293 }
Michael Kolb4923c222012-04-02 16:18:36 -0700294 mTitleBar.bringToFront();
Michael Kolbf8963522012-04-10 10:52:34 -0700295 tab.getTopWindow().requestFocus();
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800297 onTabDataChanged(tab);
298 onProgressChanged(tab);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700299 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
Patrick Scott92066772011-03-10 08:46:27 -0500300 updateAutoLogin(tab, false);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700301 mBlockFocusAnimations = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700302 }
303
John Reck718a24d2011-08-12 11:08:30 -0700304 protected void updateUrlBarAutoShowManagerTarget() {
305 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
306 if (!mUseQuickControls && web instanceof BrowserWebView) {
307 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
308 } else {
309 mUrlBarAutoShowManager.setTarget(null);
310 }
311 }
312
Michael Kolbcfa3af52010-12-14 10:36:11 -0800313 Tab getActiveTab() {
314 return mActiveTab;
315 }
316
Michael Kolb8233fac2010-10-26 16:08:53 -0700317 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800318 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800319 }
320
321 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700322 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800323 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700324 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800325 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700326 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700327 }
328
329 @Override
330 public void detachTab(Tab tab) {
331 removeTabFromContentView(tab);
332 }
333
334 @Override
335 public void attachTab(Tab tab) {
336 attachTabToContentView(tab);
337 }
338
Michael Kolb377ea312011-02-17 14:36:56 -0800339 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800340 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 return;
342 }
343 View container = tab.getViewContainer();
344 WebView mainView = tab.getWebView();
345 // Attach the WebView to the container and then attach the
346 // container to the content view.
347 FrameLayout wrapper =
348 (FrameLayout) container.findViewById(R.id.webview_wrapper);
349 ViewGroup parent = (ViewGroup) mainView.getParent();
350 if (parent != wrapper) {
351 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 parent.removeView(mainView);
353 }
354 wrapper.addView(mainView);
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
356 parent = (ViewGroup) container.getParent();
357 if (parent != mContentView) {
358 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 parent.removeView(container);
360 }
361 mContentView.addView(container, COVER_SCREEN_PARAMS);
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 }
363 mUiController.attachSubWindow(tab);
364 }
365
366 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800367 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700368 // Remove the container that contains the main WebView.
369 WebView mainView = tab.getWebView();
370 View container = tab.getViewContainer();
371 if (mainView == null) {
372 return;
373 }
374 // Remove the container from the content and then remove the
375 // WebView from the container. This will trigger a focus change
376 // needed by WebView.
377 FrameLayout wrapper =
378 (FrameLayout) container.findViewById(R.id.webview_wrapper);
379 wrapper.removeView(mainView);
380 mContentView.removeView(container);
381 mUiController.endActionMode();
382 mUiController.removeSubWindow(tab);
383 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
384 if (errorConsole != null) {
385 mErrorConsoleContainer.removeView(errorConsole);
386 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700387 }
388
Michael Kolba713ec82010-11-29 17:27:06 -0800389 @Override
390 public void onSetWebView(Tab tab, WebView webView) {
391 View container = tab.getViewContainer();
392 if (container == null) {
393 // The tab consists of a container view, which contains the main
394 // WebView, as well as any other UI elements associated with the tab.
395 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700396 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800397 tab.setViewContainer(container);
398 }
399 if (tab.getWebView() != webView) {
400 // Just remove the old one.
401 FrameLayout wrapper =
402 (FrameLayout) container.findViewById(R.id.webview_wrapper);
403 wrapper.removeView(tab.getWebView());
404 }
405 }
406
Michael Kolb8233fac2010-10-26 16:08:53 -0700407 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800408 * create a sub window container and webview for the tab
409 * Note: this methods operates through side-effects for now
410 * it sets both the subView and subViewContainer for the given tab
411 * @param tab tab to create the sub window for
412 * @param subView webview to be set as a subwindow for the tab
413 */
414 @Override
415 public void createSubWindow(Tab tab, WebView subView) {
416 View subViewContainer = mActivity.getLayoutInflater().inflate(
417 R.layout.browser_subwindow, null);
418 ViewGroup inner = (ViewGroup) subViewContainer
419 .findViewById(R.id.inner_container);
420 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
421 LayoutParams.MATCH_PARENT));
422 final ImageButton cancel = (ImageButton) subViewContainer
423 .findViewById(R.id.subwindow_close);
424 final WebView cancelSubView = subView;
425 cancel.setOnClickListener(new OnClickListener() {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800426 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800427 public void onClick(View v) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800428 ((BrowserWebView) cancelSubView).getWebChromeClient().onCloseWindow(cancelSubView);
Michael Kolb1514bb72010-11-22 09:11:48 -0800429 }
430 });
431 tab.setSubWebView(subView);
432 tab.setSubViewContainer(subViewContainer);
433 }
434
435 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 * Remove the sub window from the content view.
437 */
438 @Override
439 public void removeSubWindow(View subviewContainer) {
440 mContentView.removeView(subviewContainer);
441 mUiController.endActionMode();
442 }
443
444 /**
445 * Attach the sub window to the content view.
446 */
447 @Override
448 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800449 if (container.getParent() != null) {
450 // already attached, remove first
451 ((ViewGroup) container.getParent()).removeView(container);
452 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 mContentView.addView(container, COVER_SCREEN_PARAMS);
454 }
455
Michael Kolb11d19782011-03-20 10:17:40 -0700456 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700457 WebView web = getWebView();
458 if (web != null) {
459 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700460 }
461 }
462
Michael Kolb1f9b3562012-04-24 14:38:34 -0700463 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700464 if (mUiController.isInCustomActionMode()) {
465 mUiController.endActionMode();
466 }
467 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700468 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700469 mNavigationBar.startEditingUrl(clearInput, forceIME);
John Reckef654f12011-07-12 16:42:08 -0700470 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700471 }
472
Michael Kolb7cdc4902011-02-03 17:54:40 -0800473 boolean canShowTitleBar() {
474 return !isTitleBarShowing()
475 && !isActivityPaused()
476 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700477 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800478 && !mUiController.isInCustomActionMode();
479 }
480
John Reck0f602f32011-07-07 15:38:43 -0700481 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700482 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700483 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700484 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700485 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800486 }
487
488 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700489 if (mTitleBar.isShowing()) {
490 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700491 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800492 }
493
494 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700495 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800496 }
497
John Reck5d43ce82011-06-21 17:16:51 -0700498 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700499 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700500 }
501
Michael Kolb80f75082012-04-10 10:50:06 -0700502 public void stopEditingUrl() {
503 mTitleBar.getNavigationBar().stopEditingUrl();
504 }
505
John Reck0f602f32011-07-07 15:38:43 -0700506 public TitleBar getTitleBar() {
507 return mTitleBar;
508 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800509
Michael Kolb66706532010-12-12 19:50:22 -0800510 @Override
John Reck2bc80422011-06-30 15:11:49 -0700511 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700512 Intent intent = new Intent(mActivity, ComboViewActivity.class);
513 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
514 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
515 Tab t = getActiveTab();
516 if (t != null) {
517 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800518 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700519 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700520 }
521
522 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400523 public void showCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800524 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 // if a view already exists then immediately terminate the new one
526 if (mCustomView != null) {
527 callback.onCustomViewHidden();
528 return;
529 }
530
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400531 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb31065b12011-10-06 13:51:32 -0700532 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
533 mFullscreenContainer = new FullscreenHolder(mActivity);
534 mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
535 decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
536 mCustomView = view;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700537 setFullscreen(true);
Michael Kolb54217b32012-05-15 13:24:24 -0700538 ((BrowserWebView) getWebView()).setVisibility(View.INVISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 mCustomViewCallback = callback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400540 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700541 }
542
543 @Override
544 public void onHideCustomView() {
Michael Kolb54217b32012-05-15 13:24:24 -0700545 ((BrowserWebView) getWebView()).setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 if (mCustomView == null)
547 return;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700548 setFullscreen(false);
Michael Kolb31065b12011-10-06 13:51:32 -0700549 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
550 decor.removeView(mFullscreenContainer);
551 mFullscreenContainer = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700552 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700553 mCustomViewCallback.onCustomViewHidden();
554 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400555 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 }
557
558 @Override
559 public boolean isCustomViewShowing() {
560 return mCustomView != null;
561 }
562
Michael Kolb66706532010-12-12 19:50:22 -0800563 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800564 if (mInputManager.isActive()) {
565 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
566 0);
567 }
568 }
569
Michael Kolb66706532010-12-12 19:50:22 -0800570 @Override
John Reck3ba45532011-08-11 16:26:53 -0700571 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700572 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800573 }
574
Patrick Scott92066772011-03-10 08:46:27 -0500575 @Override
576 public void showAutoLogin(Tab tab) {
577 updateAutoLogin(tab, true);
578 }
579
580 @Override
581 public void hideAutoLogin(Tab tab) {
582 updateAutoLogin(tab, true);
583 }
584
Michael Kolb8233fac2010-10-26 16:08:53 -0700585 // -------------------------------------------------------------------------
586
Michael Kolb5a72f182011-01-13 20:35:06 -0800587 protected void updateNavigationState(Tab tab) {
588 }
589
Michael Kolb11d19782011-03-20 10:17:40 -0700590 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700591 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700592 }
Patrick Scott92066772011-03-10 08:46:27 -0500593
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 /**
595 * Update the lock icon to correspond to our latest state.
596 */
Michael Kolb66706532010-12-12 19:50:22 -0800597 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800598 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100599 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700600 }
601 }
602
603 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 * Updates the lock-icon image in the title-bar.
605 */
Steve Block2466eff2011-10-03 15:33:09 +0100606 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700607 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100608 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
609 d = mLockIconSecure;
Steve Block4895b012011-10-03 16:26:46 +0100610 } else if (securityState == SecurityState.SECURITY_STATE_MIXED
611 || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
612 // TODO: It would be good to have different icons for insecure vs mixed content.
613 // See http://b/5403800
Steve Block2466eff2011-10-03 15:33:09 +0100614 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 }
John Reck0f602f32011-07-07 15:38:43 -0700616 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 }
618
John Reck30c714c2010-12-16 17:30:34 -0800619 protected void setUrlTitle(Tab tab) {
620 String url = tab.getUrl();
621 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800622 if (TextUtils.isEmpty(title)) {
623 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800624 }
Michael Kolb66706532010-12-12 19:50:22 -0800625 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700626 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800627 }
628 }
629
630 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800631 protected void setFavicon(Tab tab) {
632 if (tab.inForeground()) {
633 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700634 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800635 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 }
637
638 @Override
639 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 }
641
Michael Kolb66706532010-12-12 19:50:22 -0800642 // active tabs page
643
644 public void showActiveTabsPage() {
645 }
646
647 /**
648 * Remove the active tabs page.
649 */
650 public void removeActiveTabsPage() {
651 }
652
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 // menu handling callbacks
654
655 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800656 public boolean onPrepareOptionsMenu(Menu menu) {
657 return true;
658 }
659
660 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700661 public void updateMenuState(Tab tab, Menu menu) {
662 }
663
664 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 }
667
668 @Override
669 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 }
671
672 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700673 public boolean onOptionsItemSelected(MenuItem item) {
674 return false;
675 }
676
677 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 }
680
681 @Override
682 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 }
684
685 @Override
686 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 }
688
689 @Override
690 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 }
692
693 // error console
694
695 @Override
696 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800697 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
699 if (flag) {
700 // Setting the show state of the console will cause it's the layout
701 // to be inflated.
702 if (errorConsole.numberOfErrors() > 0) {
703 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
704 } else {
705 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
706 }
707 if (errorConsole.getParent() != null) {
708 mErrorConsoleContainer.removeView(errorConsole);
709 }
710 // Now we can add it to the main view.
711 mErrorConsoleContainer.addView(errorConsole,
712 new LinearLayout.LayoutParams(
713 ViewGroup.LayoutParams.MATCH_PARENT,
714 ViewGroup.LayoutParams.WRAP_CONTENT));
715 } else {
716 mErrorConsoleContainer.removeView(errorConsole);
717 }
718 }
719
Michael Kolb8233fac2010-10-26 16:08:53 -0700720 // -------------------------------------------------------------------------
721 // Helper function for WebChromeClient
722 // -------------------------------------------------------------------------
723
724 @Override
725 public Bitmap getDefaultVideoPoster() {
726 if (mDefaultVideoPoster == null) {
727 mDefaultVideoPoster = BitmapFactory.decodeResource(
728 mActivity.getResources(), R.drawable.default_video_poster);
729 }
730 return mDefaultVideoPoster;
731 }
732
733 @Override
734 public View getVideoLoadingProgressView() {
735 if (mVideoProgressView == null) {
736 LayoutInflater inflater = LayoutInflater.from(mActivity);
737 mVideoProgressView = inflater.inflate(
738 R.layout.video_loading_progress, null);
739 }
740 return mVideoProgressView;
741 }
742
Michael Kolb843510f2010-12-09 10:51:49 -0800743 @Override
744 public void showMaxTabsWarning() {
745 Toast warning = Toast.makeText(mActivity,
746 mActivity.getString(R.string.max_tabs_warning),
747 Toast.LENGTH_SHORT);
748 warning.show();
749 }
750
Michael Kolb46f987e2011-04-05 10:39:10 -0700751 protected WebView getWebView() {
752 if (mActiveTab != null) {
753 return mActiveTab.getWebView();
754 } else {
755 return null;
756 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700757 }
758
Michael Kolbc38c6042011-04-27 10:46:06 -0700759 public void setFullscreen(boolean enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700760 Window win = mActivity.getWindow();
761 WindowManager.LayoutParams winParams = win.getAttributes();
Michael Kolb76dff392011-12-06 09:51:18 -0800762 final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
Michael Kolbc38c6042011-04-27 10:46:06 -0700763 if (enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700764 winParams.flags |= bits;
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 }
John Reck2711fab2012-05-18 21:38:59 -0700849
850 public void addFixedTitleBar(View view) {
851 mFixedTitlebarContainer.addView(view);
852 }
853
854 public void setContentViewMarginTop(int margin) {
855 LinearLayout.LayoutParams params =
856 (LinearLayout.LayoutParams) mContentView.getLayoutParams();
857 if (params.topMargin != margin) {
858 params.topMargin = margin;
859 mContentView.setLayoutParams(params);
860 }
861 }
Michael Kolbbae0cb22012-05-29 11:15:27 -0700862
863 @Override
864 public boolean blockFocusAnimations() {
865 return mBlockFocusAnimations;
866 }
867
Michael Kolb0b129122012-06-04 16:31:58 -0700868 @Override
869 public void onVoiceResult(String result) {
870 mNavigationBar.onVoiceResult(result);
871 }
872
kaiyizbb2db872013-08-01 22:24:07 -0400873 protected UiController getUiController() {
874 return mUiController;
875 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700876}