blob: 2b4b17091a676444629ec0f68bdd9959472a6626 [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;
Bijan Amirzada357ec8a2014-04-08 14:19:10 -070035import android.view.ActionMode;
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080048import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080050import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.widget.LinearLayout;
52import android.widget.Toast;
53
Bijan Amirzada41242f22014-03-21 12:12:18 -070054import com.android.browser.R;
55import com.android.browser.Tab.SecurityState;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080056
57import org.codeaurora.swe.WebView;
John Reckbf2ec202011-06-29 17:47:38 -070058
Michael Kolb1bf23132010-11-19 12:55:12 -080059import java.util.List;
60
Michael Kolb8233fac2010-10-26 16:08:53 -070061/**
62 * UI interface definitions
63 */
John Reck718a24d2011-08-12 11:08:30 -070064public abstract class BaseUi implements UI {
Michael Kolb8233fac2010-10-26 16:08:53 -070065
66 private static final String LOGTAG = "BaseUi";
67
Michael Kolb66706532010-12-12 19:50:22 -080068 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070069 new FrameLayout.LayoutParams(
70 ViewGroup.LayoutParams.MATCH_PARENT,
71 ViewGroup.LayoutParams.MATCH_PARENT);
72
Michael Kolb66706532010-12-12 19:50:22 -080073 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070074 new FrameLayout.LayoutParams(
75 ViewGroup.LayoutParams.MATCH_PARENT,
76 ViewGroup.LayoutParams.MATCH_PARENT,
77 Gravity.CENTER);
78
John Reck5d43ce82011-06-21 17:16:51 -070079 private static final int MSG_HIDE_TITLEBAR = 1;
John Reckef654f12011-07-12 16:42:08 -070080 public static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
John Reck5d43ce82011-06-21 17:16:51 -070081
Michael Kolb8233fac2010-10-26 16:08:53 -070082 Activity mActivity;
83 UiController mUiController;
84 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080085 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080086 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070087
Steve Block2466eff2011-10-03 15:33:09 +010088 private Drawable mLockIconSecure;
89 private Drawable mLockIconMixed;
Michael Kolb5a4372f2011-04-29 13:53:10 -070090 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070091
Michael Kolb66706532010-12-12 19:50:22 -080092 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070093 protected FrameLayout mCustomViewContainer;
Michael Kolb31065b12011-10-06 13:51:32 -070094 protected FrameLayout mFullscreenContainer;
John Reck2711fab2012-05-18 21:38:59 -070095 private FrameLayout mFixedTitlebarContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070096
Michael Kolb31065b12011-10-06 13:51:32 -070097 private View mCustomView;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080098 private CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040099 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -0700100
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 private LinearLayout mErrorConsoleContainer = null;
102
John Reck718a24d2011-08-12 11:08:30 -0700103 private UrlBarAutoShowManager mUrlBarAutoShowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -0700104
John Reck718a24d2011-08-12 11:08:30 -0700105 private Toast mStopToast;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700106
Michael Kolb8233fac2010-10-26 16:08:53 -0700107 // the default <video> poster
108 private Bitmap mDefaultVideoPoster;
109 // the video progress view
110 private View mVideoProgressView;
111
Michael Kolb8233fac2010-10-26 16:08:53 -0700112 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700113 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700114 protected TitleBar mTitleBar;
115 private NavigationBarBase mNavigationBar;
Michael Kolbda580632012-04-16 13:30:28 -0700116 protected PieControl mPieControl;
Michael Kolbbae0cb22012-05-29 11:15:27 -0700117 private boolean mBlockFocusAnimations;
Michael Kolb8233fac2010-10-26 16:08:53 -0700118
119 public BaseUi(Activity browser, UiController controller) {
120 mActivity = browser;
121 mUiController = controller;
122 mTabControl = controller.getTabControl();
123 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800124 mInputManager = (InputMethodManager)
125 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Steve Block2466eff2011-10-03 15:33:09 +0100126 mLockIconSecure = res.getDrawable(R.drawable.ic_secure_holo_dark);
127 mLockIconMixed = res.getDrawable(R.drawable.ic_secure_partial_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
129 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700130 LayoutInflater.from(mActivity)
131 .inflate(R.layout.custom_screen, frameLayout);
John Reck2711fab2012-05-18 21:38:59 -0700132 mFixedTitlebarContainer = (FrameLayout) frameLayout.findViewById(
133 R.id.fixed_titlebar_container);
John Reck7c6e1c92011-06-30 11:55:55 -0700134 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700135 R.id.main_content);
Michael Kolb53ed62c2011-10-04 16:18:44 -0700136 mCustomViewContainer = (FrameLayout) frameLayout.findViewById(
137 R.id.fullscreen_custom_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700138 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700139 .findViewById(R.id.error_console);
Michael Kolbc38c6042011-04-27 10:46:06 -0700140 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700141 mGenericFavicon = res.getDrawable(
142 R.drawable.app_web_browser_sm);
John Reck0f602f32011-07-07 15:38:43 -0700143 mTitleBar = new TitleBar(mActivity, mUiController, this,
144 mContentView);
145 mTitleBar.setProgress(100);
146 mNavigationBar = mTitleBar.getNavigationBar();
John Reck718a24d2011-08-12 11:08:30 -0700147 mUrlBarAutoShowManager = new UrlBarAutoShowManager(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 }
149
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 private void cancelStopToast() {
151 if (mStopToast != null) {
152 mStopToast.cancel();
153 mStopToast = null;
154 }
155 }
156
157 // lifecycle
158
159 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800160 if (isCustomViewShowing()) {
161 onHideCustomView();
162 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 cancelStopToast();
164 mActivityPaused = true;
165 }
166
167 public void onResume() {
168 mActivityPaused = false;
Michael Kolb2ae6ef72011-08-22 14:49:34 -0700169 // check if we exited without setting active tab
170 // b: 5188145
Michael Kolb1a4625a2011-08-24 13:40:44 -0700171 final Tab ct = mTabControl.getCurrentTab();
172 if (ct != null) {
173 setActiveTab(ct);
174 }
John Reck1cc1d1d2012-09-04 18:13:51 -0700175 mTitleBar.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700176 }
177
Michael Kolb66706532010-12-12 19:50:22 -0800178 protected boolean isActivityPaused() {
179 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700180 }
181
182 public void onConfigurationChanged(Configuration config) {
183 }
184
John Reck0f602f32011-07-07 15:38:43 -0700185 public Activity getActivity() {
186 return mActivity;
187 }
188
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 // key handling
190
191 @Override
192 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 if (mCustomView != null) {
194 mUiController.hideCustomView();
195 return true;
196 }
197 return false;
198 }
199
Michael Kolb2814a362011-05-19 15:49:41 -0700200 @Override
201 public boolean onMenuKey() {
202 return false;
203 }
204
Michael Kolbda580632012-04-16 13:30:28 -0700205 @Override
206 public void setUseQuickControls(boolean useQuickControls) {
207 mUseQuickControls = useQuickControls;
208 mTitleBar.setUseQuickControls(mUseQuickControls);
209 if (useQuickControls) {
210 mPieControl = new PieControl(mActivity, mUiController, this);
211 mPieControl.attachToContainer(mContentView);
212 } else {
213 if (mPieControl != null) {
214 mPieControl.removeFromContainer(mContentView);
215 }
216 }
217 updateUrlBarAutoShowManagerTarget();
218 }
219
John Reck30c714c2010-12-16 17:30:34 -0800220 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700221 @Override
John Reck30c714c2010-12-16 17:30:34 -0800222 public void onTabDataChanged(Tab tab) {
223 setUrlTitle(tab);
224 setFavicon(tab);
225 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800226 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700227 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700228 mNavigationBar.onTabDataChanged(tab);
Michael Kolba53c9892011-10-05 13:31:40 -0700229 onProgressChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 }
231
232 @Override
Michael Kolbe8a82332012-04-25 14:14:26 -0700233 public void onProgressChanged(Tab tab) {
234 int progress = tab.getLoadProgress();
235 if (tab.inForeground()) {
236 mTitleBar.setProgress(progress);
237 }
238 }
239
240 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500241 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800242 if (tab.inForeground()) {
243 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700244 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800245 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500246 }
247
248 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 public void onPageStopped(Tab tab) {
250 cancelStopToast();
251 if (tab.inForeground()) {
252 mStopToast = Toast
253 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
254 mStopToast.show();
255 }
256 }
257
258 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800259 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700260 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800261 }
262
263 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 }
266
267 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800268 public void setActiveTab(final Tab tab) {
Michael Kolb7ac63b62011-12-16 09:52:29 -0800269 if (tab == null) return;
Michael Kolbbae0cb22012-05-29 11:15:27 -0700270 // block unnecessary focus change animations during tab switch
271 mBlockFocusAnimations = true;
John Reck5d43ce82011-06-21 17:16:51 -0700272 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800273 if ((tab != mActiveTab) && (mActiveTab != null)) {
274 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700275 WebView web = mActiveTab.getWebView();
276 if (web != null) {
277 web.setOnTouchListener(null);
278 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 }
Michael Kolb77df4562010-11-19 14:49:34 -0800280 mActiveTab = tab;
Michael Kolbda580632012-04-16 13:30:28 -0700281 BrowserWebView web = (BrowserWebView) mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700282 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700283 attachTabToContentView(tab);
Michael Kolbda580632012-04-16 13:30:28 -0700284 if (web != null) {
285 // Request focus on the top window.
286 if (mUseQuickControls) {
287 mPieControl.forceToTop(mContentView);
288 web.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700289 mTitleBar.hide();
Michael Kolbda580632012-04-16 13:30:28 -0700290 } else {
291 web.setTitleBar(mTitleBar);
292 mTitleBar.onScrollChanged();
293 }
294 }
Michael Kolb4923c222012-04-02 16:18:36 -0700295 mTitleBar.bringToFront();
Michael Kolbf8963522012-04-10 10:52:34 -0700296 tab.getTopWindow().requestFocus();
Michael Kolb8233fac2010-10-26 16:08:53 -0700297 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800298 onTabDataChanged(tab);
299 onProgressChanged(tab);
Michael Kolb03b6bc62011-09-02 16:19:53 -0700300 mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
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
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 // -------------------------------------------------------------------------
576
Michael Kolb5a72f182011-01-13 20:35:06 -0800577 protected void updateNavigationState(Tab tab) {
578 }
579
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 /**
581 * Update the lock icon to correspond to our latest state.
582 */
Michael Kolb66706532010-12-12 19:50:22 -0800583 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800584 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100585 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700586 }
587 }
588
589 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 * Updates the lock-icon image in the title-bar.
591 */
Steve Block2466eff2011-10-03 15:33:09 +0100592 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100594 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
595 d = mLockIconSecure;
Steve Block4895b012011-10-03 16:26:46 +0100596 } else if (securityState == SecurityState.SECURITY_STATE_MIXED
597 || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
598 // TODO: It would be good to have different icons for insecure vs mixed content.
599 // See http://b/5403800
Steve Block2466eff2011-10-03 15:33:09 +0100600 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 }
John Reck0f602f32011-07-07 15:38:43 -0700602 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 }
604
John Reck30c714c2010-12-16 17:30:34 -0800605 protected void setUrlTitle(Tab tab) {
606 String url = tab.getUrl();
607 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800608 if (TextUtils.isEmpty(title)) {
609 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800610 }
Michael Kolb66706532010-12-12 19:50:22 -0800611 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700612 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800613 }
614 }
615
616 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800617 protected void setFavicon(Tab tab) {
618 if (tab.inForeground()) {
619 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700620 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800621 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 }
623
Michael Kolb66706532010-12-12 19:50:22 -0800624 // active tabs page
625
626 public void showActiveTabsPage() {
627 }
628
629 /**
630 * Remove the active tabs page.
631 */
632 public void removeActiveTabsPage() {
633 }
634
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 // menu handling callbacks
636
637 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800638 public boolean onPrepareOptionsMenu(Menu menu) {
639 return true;
640 }
641
642 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700643 public void updateMenuState(Tab tab, Menu menu) {
644 }
645
646 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700647 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 }
649
650 @Override
651 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 }
653
654 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700655 public boolean onOptionsItemSelected(MenuItem item) {
656 return false;
657 }
658
659 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 }
662
663 @Override
664 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 }
666
667 @Override
668 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 }
670
671 @Override
672 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 }
674
675 // error console
676
677 @Override
678 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800679 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
681 if (flag) {
682 // Setting the show state of the console will cause it's the layout
683 // to be inflated.
684 if (errorConsole.numberOfErrors() > 0) {
685 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
686 } else {
687 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
688 }
689 if (errorConsole.getParent() != null) {
690 mErrorConsoleContainer.removeView(errorConsole);
691 }
692 // Now we can add it to the main view.
693 mErrorConsoleContainer.addView(errorConsole,
694 new LinearLayout.LayoutParams(
695 ViewGroup.LayoutParams.MATCH_PARENT,
696 ViewGroup.LayoutParams.WRAP_CONTENT));
697 } else {
698 mErrorConsoleContainer.removeView(errorConsole);
699 }
700 }
701
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 // -------------------------------------------------------------------------
703 // Helper function for WebChromeClient
704 // -------------------------------------------------------------------------
705
706 @Override
707 public Bitmap getDefaultVideoPoster() {
708 if (mDefaultVideoPoster == null) {
709 mDefaultVideoPoster = BitmapFactory.decodeResource(
710 mActivity.getResources(), R.drawable.default_video_poster);
711 }
712 return mDefaultVideoPoster;
713 }
714
715 @Override
716 public View getVideoLoadingProgressView() {
717 if (mVideoProgressView == null) {
718 LayoutInflater inflater = LayoutInflater.from(mActivity);
719 mVideoProgressView = inflater.inflate(
720 R.layout.video_loading_progress, null);
721 }
722 return mVideoProgressView;
723 }
724
Michael Kolb843510f2010-12-09 10:51:49 -0800725 @Override
726 public void showMaxTabsWarning() {
727 Toast warning = Toast.makeText(mActivity,
728 mActivity.getString(R.string.max_tabs_warning),
729 Toast.LENGTH_SHORT);
730 warning.show();
731 }
732
Michael Kolb46f987e2011-04-05 10:39:10 -0700733 protected WebView getWebView() {
734 if (mActiveTab != null) {
735 return mActiveTab.getWebView();
736 } else {
737 return null;
738 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700739 }
740
Michael Kolbc38c6042011-04-27 10:46:06 -0700741 public void setFullscreen(boolean enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700742 Window win = mActivity.getWindow();
743 WindowManager.LayoutParams winParams = win.getAttributes();
Michael Kolb76dff392011-12-06 09:51:18 -0800744 final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
Michael Kolbc38c6042011-04-27 10:46:06 -0700745 if (enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700746 winParams.flags |= bits;
Michael Kolbc38c6042011-04-27 10:46:06 -0700747 } else {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700748 winParams.flags &= ~bits;
749 if (mCustomView != null) {
750 mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
751 } else {
752 mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
753 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700754 }
Michael Kolbc5675ad2011-10-21 13:34:28 -0700755 win.setAttributes(winParams);
Michael Kolbc38c6042011-04-27 10:46:06 -0700756 }
757
John Reck0f602f32011-07-07 15:38:43 -0700758 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700759 Drawable[] array = new Drawable[3];
760 array[0] = new PaintDrawable(Color.BLACK);
761 PaintDrawable p = new PaintDrawable(Color.WHITE);
762 array[1] = p;
763 if (icon == null) {
764 array[2] = mGenericFavicon;
765 } else {
766 array[2] = new BitmapDrawable(icon);
767 }
768 LayerDrawable d = new LayerDrawable(array);
769 d.setLayerInset(1, 1, 1, 1, 1);
770 d.setLayerInset(2, 2, 2, 2, 2);
771 return d;
772 }
773
John Reck5d43ce82011-06-21 17:16:51 -0700774 public boolean isLoading() {
775 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
776 }
777
778 /**
779 * Suggest to the UI that the title bar can be hidden. The UI will then
780 * decide whether or not to hide based off a number of factors, such
781 * as if the user is editing the URL bar or if the page is loading
782 */
783 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700784 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
785 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700786 hideTitleBar();
787 }
788 }
789
John Reckbc6adb42011-09-01 18:03:20 -0700790 protected final void showTitleBarForDuration() {
791 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
792 }
793
794 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700795 showTitleBar();
796 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700797 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700798 }
799
John Reck9c5004e2011-10-07 16:00:12 -0700800 protected Handler mHandler = new Handler() {
John Reck5d43ce82011-06-21 17:16:51 -0700801
802 @Override
803 public void handleMessage(Message msg) {
804 if (msg.what == MSG_HIDE_TITLEBAR) {
805 suggestHideTitleBar();
806 }
John Reck9c5004e2011-10-07 16:00:12 -0700807 BaseUi.this.handleMessage(msg);
John Reck5d43ce82011-06-21 17:16:51 -0700808 }
809 };
John Reck3ba45532011-08-11 16:26:53 -0700810
John Reck9c5004e2011-10-07 16:00:12 -0700811 protected void handleMessage(Message msg) {}
812
John Reck3ba45532011-08-11 16:26:53 -0700813 @Override
814 public void showWeb(boolean animate) {
815 mUiController.hideCustomView();
816 }
817
Michael Kolb31065b12011-10-06 13:51:32 -0700818 static class FullscreenHolder extends FrameLayout {
Michael Kolb53ed62c2011-10-04 16:18:44 -0700819
Michael Kolb31065b12011-10-06 13:51:32 -0700820 public FullscreenHolder(Context ctx) {
821 super(ctx);
822 setBackgroundColor(ctx.getResources().getColor(R.color.black));
Michael Kolb53ed62c2011-10-04 16:18:44 -0700823 }
824
Michael Kolb31065b12011-10-06 13:51:32 -0700825 @Override
826 public boolean onTouchEvent(MotionEvent evt) {
827 return true;
Michael Kolb53ed62c2011-10-04 16:18:44 -0700828 }
Michael Kolb31065b12011-10-06 13:51:32 -0700829
Michael Kolb53ed62c2011-10-04 16:18:44 -0700830 }
John Reck2711fab2012-05-18 21:38:59 -0700831
832 public void addFixedTitleBar(View view) {
833 mFixedTitlebarContainer.addView(view);
834 }
835
836 public void setContentViewMarginTop(int margin) {
837 LinearLayout.LayoutParams params =
838 (LinearLayout.LayoutParams) mContentView.getLayoutParams();
839 if (params.topMargin != margin) {
840 params.topMargin = margin;
841 mContentView.setLayoutParams(params);
842 }
843 }
Michael Kolbbae0cb22012-05-29 11:15:27 -0700844
845 @Override
846 public boolean blockFocusAnimations() {
847 return mBlockFocusAnimations;
848 }
849
Michael Kolb0b129122012-06-04 16:31:58 -0700850 @Override
851 public void onVoiceResult(String result) {
852 mNavigationBar.onVoiceResult(result);
853 }
854
kaiyizbb2db872013-08-01 22:24:07 -0400855 protected UiController getUiController() {
856 return mUiController;
857 }
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700858
859 @Override
860 public void onActionModeStarted(ActionMode mode) {
861 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
862 mFixedTitlebarContainer.setY(fixedTbarHeight);
863 setContentViewMarginTop(fixedTbarHeight);
864 }
865
866 @Override
867 public void onActionModeFinished(boolean inLoad) {
868 mFixedTitlebarContainer.setY(0);
869 setContentViewMarginTop(0);
870 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700871}