blob: f5ccb4274f9818bccdd8d36118dbadfb1a9a32c8 [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());
Patrick Scott92066772011-03-10 08:46:27 -0500301 updateAutoLogin(tab, false);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700302 mBlockFocusAnimations = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700303 }
304
John Reck718a24d2011-08-12 11:08:30 -0700305 protected void updateUrlBarAutoShowManagerTarget() {
306 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
307 if (!mUseQuickControls && web instanceof BrowserWebView) {
308 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
309 } else {
310 mUrlBarAutoShowManager.setTarget(null);
311 }
312 }
313
Michael Kolbcfa3af52010-12-14 10:36:11 -0800314 Tab getActiveTab() {
315 return mActiveTab;
316 }
317
Michael Kolb8233fac2010-10-26 16:08:53 -0700318 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800319 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800320 }
321
322 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700323 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800324 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700325 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800326 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700327 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700328 }
329
330 @Override
331 public void detachTab(Tab tab) {
332 removeTabFromContentView(tab);
333 }
334
335 @Override
336 public void attachTab(Tab tab) {
337 attachTabToContentView(tab);
338 }
339
Michael Kolb377ea312011-02-17 14:36:56 -0800340 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800341 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700342 return;
343 }
344 View container = tab.getViewContainer();
345 WebView mainView = tab.getWebView();
346 // Attach the WebView to the container and then attach the
347 // container to the content view.
348 FrameLayout wrapper =
349 (FrameLayout) container.findViewById(R.id.webview_wrapper);
350 ViewGroup parent = (ViewGroup) mainView.getParent();
351 if (parent != wrapper) {
352 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700353 parent.removeView(mainView);
354 }
355 wrapper.addView(mainView);
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 }
357 parent = (ViewGroup) container.getParent();
358 if (parent != mContentView) {
359 if (parent != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700360 parent.removeView(container);
361 }
362 mContentView.addView(container, COVER_SCREEN_PARAMS);
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 }
364 mUiController.attachSubWindow(tab);
365 }
366
367 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800368 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700369 // Remove the container that contains the main WebView.
370 WebView mainView = tab.getWebView();
371 View container = tab.getViewContainer();
372 if (mainView == null) {
373 return;
374 }
375 // Remove the container from the content and then remove the
376 // WebView from the container. This will trigger a focus change
377 // needed by WebView.
378 FrameLayout wrapper =
379 (FrameLayout) container.findViewById(R.id.webview_wrapper);
380 wrapper.removeView(mainView);
381 mContentView.removeView(container);
382 mUiController.endActionMode();
383 mUiController.removeSubWindow(tab);
384 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
385 if (errorConsole != null) {
386 mErrorConsoleContainer.removeView(errorConsole);
387 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 }
389
Michael Kolba713ec82010-11-29 17:27:06 -0800390 @Override
391 public void onSetWebView(Tab tab, WebView webView) {
392 View container = tab.getViewContainer();
393 if (container == null) {
394 // The tab consists of a container view, which contains the main
395 // WebView, as well as any other UI elements associated with the tab.
396 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700397 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800398 tab.setViewContainer(container);
399 }
400 if (tab.getWebView() != webView) {
401 // Just remove the old one.
402 FrameLayout wrapper =
403 (FrameLayout) container.findViewById(R.id.webview_wrapper);
404 wrapper.removeView(tab.getWebView());
405 }
406 }
407
Michael Kolb8233fac2010-10-26 16:08:53 -0700408 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800409 * create a sub window container and webview for the tab
410 * Note: this methods operates through side-effects for now
411 * it sets both the subView and subViewContainer for the given tab
412 * @param tab tab to create the sub window for
413 * @param subView webview to be set as a subwindow for the tab
414 */
415 @Override
416 public void createSubWindow(Tab tab, WebView subView) {
417 View subViewContainer = mActivity.getLayoutInflater().inflate(
418 R.layout.browser_subwindow, null);
419 ViewGroup inner = (ViewGroup) subViewContainer
420 .findViewById(R.id.inner_container);
421 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
422 LayoutParams.MATCH_PARENT));
423 final ImageButton cancel = (ImageButton) subViewContainer
424 .findViewById(R.id.subwindow_close);
425 final WebView cancelSubView = subView;
426 cancel.setOnClickListener(new OnClickListener() {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800427 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800428 public void onClick(View v) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800429 ((BrowserWebView) cancelSubView).getWebChromeClient().onCloseWindow(cancelSubView);
Michael Kolb1514bb72010-11-22 09:11:48 -0800430 }
431 });
432 tab.setSubWebView(subView);
433 tab.setSubViewContainer(subViewContainer);
434 }
435
436 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700437 * Remove the sub window from the content view.
438 */
439 @Override
440 public void removeSubWindow(View subviewContainer) {
441 mContentView.removeView(subviewContainer);
442 mUiController.endActionMode();
443 }
444
445 /**
446 * Attach the sub window to the content view.
447 */
448 @Override
449 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800450 if (container.getParent() != null) {
451 // already attached, remove first
452 ((ViewGroup) container.getParent()).removeView(container);
453 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700454 mContentView.addView(container, COVER_SCREEN_PARAMS);
455 }
456
Michael Kolb11d19782011-03-20 10:17:40 -0700457 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700458 WebView web = getWebView();
459 if (web != null) {
460 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700461 }
462 }
463
Michael Kolb1f9b3562012-04-24 14:38:34 -0700464 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700465 if (mUiController.isInCustomActionMode()) {
466 mUiController.endActionMode();
467 }
468 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700469 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700470 mNavigationBar.startEditingUrl(clearInput, forceIME);
John Reckef654f12011-07-12 16:42:08 -0700471 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700472 }
473
Michael Kolb7cdc4902011-02-03 17:54:40 -0800474 boolean canShowTitleBar() {
475 return !isTitleBarShowing()
476 && !isActivityPaused()
477 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700478 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800479 && !mUiController.isInCustomActionMode();
480 }
481
John Reck0f602f32011-07-07 15:38:43 -0700482 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700483 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700484 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700485 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700486 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800487 }
488
489 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700490 if (mTitleBar.isShowing()) {
491 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700492 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800493 }
494
495 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700496 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800497 }
498
John Reck5d43ce82011-06-21 17:16:51 -0700499 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700500 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700501 }
502
Michael Kolb80f75082012-04-10 10:50:06 -0700503 public void stopEditingUrl() {
504 mTitleBar.getNavigationBar().stopEditingUrl();
505 }
506
John Reck0f602f32011-07-07 15:38:43 -0700507 public TitleBar getTitleBar() {
508 return mTitleBar;
509 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800510
Michael Kolb66706532010-12-12 19:50:22 -0800511 @Override
John Reck2bc80422011-06-30 15:11:49 -0700512 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700513 Intent intent = new Intent(mActivity, ComboViewActivity.class);
514 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
515 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
516 Tab t = getActiveTab();
517 if (t != null) {
518 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800519 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700520 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700521 }
522
523 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400524 public void showCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800525 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 // if a view already exists then immediately terminate the new one
527 if (mCustomView != null) {
528 callback.onCustomViewHidden();
529 return;
530 }
531
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400532 mOriginalOrientation = mActivity.getRequestedOrientation();
Michael Kolb31065b12011-10-06 13:51:32 -0700533 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
534 mFullscreenContainer = new FullscreenHolder(mActivity);
535 mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
536 decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
537 mCustomView = view;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700538 setFullscreen(true);
Michael Kolb54217b32012-05-15 13:24:24 -0700539 ((BrowserWebView) getWebView()).setVisibility(View.INVISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 mCustomViewCallback = callback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400541 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 }
543
544 @Override
545 public void onHideCustomView() {
Michael Kolb54217b32012-05-15 13:24:24 -0700546 ((BrowserWebView) getWebView()).setVisibility(View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700547 if (mCustomView == null)
548 return;
Michael Kolbc5675ad2011-10-21 13:34:28 -0700549 setFullscreen(false);
Michael Kolb31065b12011-10-06 13:51:32 -0700550 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
551 decor.removeView(mFullscreenContainer);
552 mFullscreenContainer = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700553 mCustomView = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 mCustomViewCallback.onCustomViewHidden();
555 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400556 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700557 }
558
559 @Override
560 public boolean isCustomViewShowing() {
561 return mCustomView != null;
562 }
563
Michael Kolb66706532010-12-12 19:50:22 -0800564 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800565 if (mInputManager.isActive()) {
566 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
567 0);
568 }
569 }
570
Michael Kolb66706532010-12-12 19:50:22 -0800571 @Override
John Reck3ba45532011-08-11 16:26:53 -0700572 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700573 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800574 }
575
Patrick Scott92066772011-03-10 08:46:27 -0500576 @Override
577 public void showAutoLogin(Tab tab) {
578 updateAutoLogin(tab, true);
579 }
580
581 @Override
582 public void hideAutoLogin(Tab tab) {
583 updateAutoLogin(tab, true);
584 }
585
Michael Kolb8233fac2010-10-26 16:08:53 -0700586 // -------------------------------------------------------------------------
587
Michael Kolb5a72f182011-01-13 20:35:06 -0800588 protected void updateNavigationState(Tab tab) {
589 }
590
Michael Kolb11d19782011-03-20 10:17:40 -0700591 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700592 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700593 }
Patrick Scott92066772011-03-10 08:46:27 -0500594
Michael Kolb8233fac2010-10-26 16:08:53 -0700595 /**
596 * Update the lock icon to correspond to our latest state.
597 */
Michael Kolb66706532010-12-12 19:50:22 -0800598 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800599 if (t != null && t.inForeground()) {
Steve Block2466eff2011-10-03 15:33:09 +0100600 updateLockIconImage(t.getSecurityState());
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 }
602 }
603
604 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700605 * Updates the lock-icon image in the title-bar.
606 */
Steve Block2466eff2011-10-03 15:33:09 +0100607 private void updateLockIconImage(SecurityState securityState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700608 Drawable d = null;
Steve Block2466eff2011-10-03 15:33:09 +0100609 if (securityState == SecurityState.SECURITY_STATE_SECURE) {
610 d = mLockIconSecure;
Steve Block4895b012011-10-03 16:26:46 +0100611 } else if (securityState == SecurityState.SECURITY_STATE_MIXED
612 || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
613 // TODO: It would be good to have different icons for insecure vs mixed content.
614 // See http://b/5403800
Steve Block2466eff2011-10-03 15:33:09 +0100615 d = mLockIconMixed;
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 }
John Reck0f602f32011-07-07 15:38:43 -0700617 mNavigationBar.setLock(d);
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 }
619
John Reck30c714c2010-12-16 17:30:34 -0800620 protected void setUrlTitle(Tab tab) {
621 String url = tab.getUrl();
622 String title = tab.getTitle();
Michael Kolb66706532010-12-12 19:50:22 -0800623 if (TextUtils.isEmpty(title)) {
624 title = url;
Michael Kolb81b6f832010-12-12 12:44:27 -0800625 }
Michael Kolb66706532010-12-12 19:50:22 -0800626 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700627 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800628 }
629 }
630
631 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800632 protected void setFavicon(Tab tab) {
633 if (tab.inForeground()) {
634 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700635 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800636 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 }
638
Michael Kolb66706532010-12-12 19:50:22 -0800639 // active tabs page
640
641 public void showActiveTabsPage() {
642 }
643
644 /**
645 * Remove the active tabs page.
646 */
647 public void removeActiveTabsPage() {
648 }
649
Michael Kolb8233fac2010-10-26 16:08:53 -0700650 // menu handling callbacks
651
652 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800653 public boolean onPrepareOptionsMenu(Menu menu) {
654 return true;
655 }
656
657 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700658 public void updateMenuState(Tab tab, Menu menu) {
659 }
660
661 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700662 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 }
664
665 @Override
666 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 }
668
669 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700670 public boolean onOptionsItemSelected(MenuItem item) {
671 return false;
672 }
673
674 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 }
677
678 @Override
679 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 }
681
682 @Override
683 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 }
685
686 @Override
687 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 }
689
690 // error console
691
692 @Override
693 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800694 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
696 if (flag) {
697 // Setting the show state of the console will cause it's the layout
698 // to be inflated.
699 if (errorConsole.numberOfErrors() > 0) {
700 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
701 } else {
702 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
703 }
704 if (errorConsole.getParent() != null) {
705 mErrorConsoleContainer.removeView(errorConsole);
706 }
707 // Now we can add it to the main view.
708 mErrorConsoleContainer.addView(errorConsole,
709 new LinearLayout.LayoutParams(
710 ViewGroup.LayoutParams.MATCH_PARENT,
711 ViewGroup.LayoutParams.WRAP_CONTENT));
712 } else {
713 mErrorConsoleContainer.removeView(errorConsole);
714 }
715 }
716
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 // -------------------------------------------------------------------------
718 // Helper function for WebChromeClient
719 // -------------------------------------------------------------------------
720
721 @Override
722 public Bitmap getDefaultVideoPoster() {
723 if (mDefaultVideoPoster == null) {
724 mDefaultVideoPoster = BitmapFactory.decodeResource(
725 mActivity.getResources(), R.drawable.default_video_poster);
726 }
727 return mDefaultVideoPoster;
728 }
729
730 @Override
731 public View getVideoLoadingProgressView() {
732 if (mVideoProgressView == null) {
733 LayoutInflater inflater = LayoutInflater.from(mActivity);
734 mVideoProgressView = inflater.inflate(
735 R.layout.video_loading_progress, null);
736 }
737 return mVideoProgressView;
738 }
739
Michael Kolb843510f2010-12-09 10:51:49 -0800740 @Override
741 public void showMaxTabsWarning() {
742 Toast warning = Toast.makeText(mActivity,
743 mActivity.getString(R.string.max_tabs_warning),
744 Toast.LENGTH_SHORT);
745 warning.show();
746 }
747
Michael Kolb46f987e2011-04-05 10:39:10 -0700748 protected WebView getWebView() {
749 if (mActiveTab != null) {
750 return mActiveTab.getWebView();
751 } else {
752 return null;
753 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700754 }
755
Michael Kolbc38c6042011-04-27 10:46:06 -0700756 public void setFullscreen(boolean enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700757 Window win = mActivity.getWindow();
758 WindowManager.LayoutParams winParams = win.getAttributes();
Michael Kolb76dff392011-12-06 09:51:18 -0800759 final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
Michael Kolbc38c6042011-04-27 10:46:06 -0700760 if (enabled) {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700761 winParams.flags |= bits;
Michael Kolbc38c6042011-04-27 10:46:06 -0700762 } else {
Michael Kolbc5675ad2011-10-21 13:34:28 -0700763 winParams.flags &= ~bits;
764 if (mCustomView != null) {
765 mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
766 } else {
767 mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
768 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700769 }
Michael Kolbc5675ad2011-10-21 13:34:28 -0700770 win.setAttributes(winParams);
Michael Kolbc38c6042011-04-27 10:46:06 -0700771 }
772
John Reck0f602f32011-07-07 15:38:43 -0700773 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700774 Drawable[] array = new Drawable[3];
775 array[0] = new PaintDrawable(Color.BLACK);
776 PaintDrawable p = new PaintDrawable(Color.WHITE);
777 array[1] = p;
778 if (icon == null) {
779 array[2] = mGenericFavicon;
780 } else {
781 array[2] = new BitmapDrawable(icon);
782 }
783 LayerDrawable d = new LayerDrawable(array);
784 d.setLayerInset(1, 1, 1, 1, 1);
785 d.setLayerInset(2, 2, 2, 2, 2);
786 return d;
787 }
788
John Reck5d43ce82011-06-21 17:16:51 -0700789 public boolean isLoading() {
790 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
791 }
792
793 /**
794 * Suggest to the UI that the title bar can be hidden. The UI will then
795 * decide whether or not to hide based off a number of factors, such
796 * as if the user is editing the URL bar or if the page is loading
797 */
798 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700799 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
800 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700801 hideTitleBar();
802 }
803 }
804
John Reckbc6adb42011-09-01 18:03:20 -0700805 protected final void showTitleBarForDuration() {
806 showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
807 }
808
809 protected final void showTitleBarForDuration(long duration) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700810 showTitleBar();
811 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
John Reckbc6adb42011-09-01 18:03:20 -0700812 mHandler.sendMessageDelayed(msg, duration);
John Reck6ac5bfd2011-07-01 17:02:55 -0700813 }
814
John Reck9c5004e2011-10-07 16:00:12 -0700815 protected Handler mHandler = new Handler() {
John Reck5d43ce82011-06-21 17:16:51 -0700816
817 @Override
818 public void handleMessage(Message msg) {
819 if (msg.what == MSG_HIDE_TITLEBAR) {
820 suggestHideTitleBar();
821 }
John Reck9c5004e2011-10-07 16:00:12 -0700822 BaseUi.this.handleMessage(msg);
John Reck5d43ce82011-06-21 17:16:51 -0700823 }
824 };
John Reck3ba45532011-08-11 16:26:53 -0700825
John Reck9c5004e2011-10-07 16:00:12 -0700826 protected void handleMessage(Message msg) {}
827
John Reck3ba45532011-08-11 16:26:53 -0700828 @Override
829 public void showWeb(boolean animate) {
830 mUiController.hideCustomView();
831 }
832
Michael Kolb31065b12011-10-06 13:51:32 -0700833 static class FullscreenHolder extends FrameLayout {
Michael Kolb53ed62c2011-10-04 16:18:44 -0700834
Michael Kolb31065b12011-10-06 13:51:32 -0700835 public FullscreenHolder(Context ctx) {
836 super(ctx);
837 setBackgroundColor(ctx.getResources().getColor(R.color.black));
Michael Kolb53ed62c2011-10-04 16:18:44 -0700838 }
839
Michael Kolb31065b12011-10-06 13:51:32 -0700840 @Override
841 public boolean onTouchEvent(MotionEvent evt) {
842 return true;
Michael Kolb53ed62c2011-10-04 16:18:44 -0700843 }
Michael Kolb31065b12011-10-06 13:51:32 -0700844
Michael Kolb53ed62c2011-10-04 16:18:44 -0700845 }
John Reck2711fab2012-05-18 21:38:59 -0700846
847 public void addFixedTitleBar(View view) {
848 mFixedTitlebarContainer.addView(view);
849 }
850
851 public void setContentViewMarginTop(int margin) {
852 LinearLayout.LayoutParams params =
853 (LinearLayout.LayoutParams) mContentView.getLayoutParams();
854 if (params.topMargin != margin) {
855 params.topMargin = margin;
856 mContentView.setLayoutParams(params);
857 }
858 }
Michael Kolbbae0cb22012-05-29 11:15:27 -0700859
860 @Override
861 public boolean blockFocusAnimations() {
862 return mBlockFocusAnimations;
863 }
864
Michael Kolb0b129122012-06-04 16:31:58 -0700865 @Override
866 public void onVoiceResult(String result) {
867 mNavigationBar.onVoiceResult(result);
868 }
869
kaiyizbb2db872013-08-01 22:24:07 -0400870 protected UiController getUiController() {
871 return mUiController;
872 }
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700873
874 @Override
875 public void onActionModeStarted(ActionMode mode) {
876 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
877 mFixedTitlebarContainer.setY(fixedTbarHeight);
878 setContentViewMarginTop(fixedTbarHeight);
879 }
880
881 @Override
882 public void onActionModeFinished(boolean inLoad) {
883 mFixedTitlebarContainer.setY(0);
884 setContentViewMarginTop(0);
885 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700886}