blob: 6f87463910d7a1b43671753866088eaf6649c2ba [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
John Reckd3e4d5b2011-07-13 15:48:43 -070020import android.content.Intent;
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.content.res.Configuration;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
Michael Kolb5a4372f2011-04-29 13:53:10 -070025import android.graphics.Color;
26import android.graphics.drawable.BitmapDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.graphics.drawable.Drawable;
Michael Kolb5a4372f2011-04-29 13:53:10 -070028import android.graphics.drawable.LayerDrawable;
29import android.graphics.drawable.PaintDrawable;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.os.Bundle;
John Reck5d43ce82011-06-21 17:16:51 -070031import android.os.Handler;
32import android.os.Message;
Michael Kolb8233fac2010-10-26 16:08:53 -070033import android.text.TextUtils;
34import android.util.Log;
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 Kolb8233fac2010-10-26 16:08:53 -070039import android.view.View;
Michael Kolb1514bb72010-11-22 09:11:48 -080040import android.view.View.OnClickListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080042import android.view.ViewGroup.LayoutParams;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.view.WindowManager;
Michael Kolb3a696282010-12-05 13:23:24 -080044import android.view.inputmethod.InputMethodManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070045import android.webkit.WebChromeClient;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.webkit.WebView;
47import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080048import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.widget.LinearLayout;
50import android.widget.Toast;
51
John Reckbf2ec202011-06-29 17:47:38 -070052import com.android.browser.Tab.LockIcon;
53import com.android.internal.view.menu.MenuBuilder;
54
Michael Kolb1bf23132010-11-19 12:55:12 -080055import java.util.List;
56
Michael Kolb8233fac2010-10-26 16:08:53 -070057/**
58 * UI interface definitions
59 */
John Reck718a24d2011-08-12 11:08:30 -070060public abstract class BaseUi implements UI {
Michael Kolb8233fac2010-10-26 16:08:53 -070061
62 private static final String LOGTAG = "BaseUi";
63
Michael Kolb66706532010-12-12 19:50:22 -080064 protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
Michael Kolb8233fac2010-10-26 16:08:53 -070065 new FrameLayout.LayoutParams(
66 ViewGroup.LayoutParams.MATCH_PARENT,
67 ViewGroup.LayoutParams.MATCH_PARENT);
68
Michael Kolb66706532010-12-12 19:50:22 -080069 protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
Michael Kolb8233fac2010-10-26 16:08:53 -070070 new FrameLayout.LayoutParams(
71 ViewGroup.LayoutParams.MATCH_PARENT,
72 ViewGroup.LayoutParams.MATCH_PARENT,
73 Gravity.CENTER);
74
John Reck5d43ce82011-06-21 17:16:51 -070075 private static final int MSG_HIDE_TITLEBAR = 1;
John Reckef654f12011-07-12 16:42:08 -070076 public static final int HIDE_TITLEBAR_DELAY = 1500; // in ms
John Reck5d43ce82011-06-21 17:16:51 -070077
Michael Kolb8233fac2010-10-26 16:08:53 -070078 Activity mActivity;
79 UiController mUiController;
80 TabControl mTabControl;
Michael Kolb377ea312011-02-17 14:36:56 -080081 protected Tab mActiveTab;
Michael Kolb3a696282010-12-05 13:23:24 -080082 private InputMethodManager mInputManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
84 private Drawable mSecLockIcon;
85 private Drawable mMixLockIcon;
Michael Kolb5a4372f2011-04-29 13:53:10 -070086 protected Drawable mGenericFavicon;
Michael Kolb8233fac2010-10-26 16:08:53 -070087
Michael Kolb66706532010-12-12 19:50:22 -080088 protected FrameLayout mContentView;
Michael Kolbf2055602011-04-09 17:20:03 -070089 protected FrameLayout mCustomViewContainer;
Michael Kolb8233fac2010-10-26 16:08:53 -070090
91 private View mCustomView;
92 private WebChromeClient.CustomViewCallback mCustomViewCallback;
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -040093 private int mOriginalOrientation;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
Michael Kolb8233fac2010-10-26 16:08:53 -070095 private LinearLayout mErrorConsoleContainer = null;
96
John Reck718a24d2011-08-12 11:08:30 -070097 private UrlBarAutoShowManager mUrlBarAutoShowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070098
John Reck718a24d2011-08-12 11:08:30 -070099 private Toast mStopToast;
Michael Kolb5a4372f2011-04-29 13:53:10 -0700100
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 // the default <video> poster
102 private Bitmap mDefaultVideoPoster;
103 // the video progress view
104 private View mVideoProgressView;
105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106 private boolean mActivityPaused;
John Reckbf2ec202011-06-29 17:47:38 -0700107 protected boolean mUseQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700108 protected TitleBar mTitleBar;
109 private NavigationBarBase mNavigationBar;
Michael Kolb8233fac2010-10-26 16:08:53 -0700110
111 public BaseUi(Activity browser, UiController controller) {
112 mActivity = browser;
113 mUiController = controller;
114 mTabControl = controller.getTabControl();
115 Resources res = mActivity.getResources();
Michael Kolb3a696282010-12-05 13:23:24 -0800116 mInputManager = (InputMethodManager)
117 browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
Michael Kolb5a72f182011-01-13 20:35:06 -0800118 mSecLockIcon = res.getDrawable(R.drawable.ic_secure_holo_dark);
Michael Kolb8233fac2010-10-26 16:08:53 -0700119 mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure);
120
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
122 .getDecorView().findViewById(android.R.id.content);
John Reck7c6e1c92011-06-30 11:55:55 -0700123 LayoutInflater.from(mActivity)
124 .inflate(R.layout.custom_screen, frameLayout);
125 mContentView = (FrameLayout) frameLayout.findViewById(
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 R.id.main_content);
John Reck7c6e1c92011-06-30 11:55:55 -0700127 mErrorConsoleContainer = (LinearLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 .findViewById(R.id.error_console);
John Reck7c6e1c92011-06-30 11:55:55 -0700129 mCustomViewContainer = (FrameLayout) frameLayout
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 .findViewById(R.id.fullscreen_custom_content);
Michael Kolbc38c6042011-04-27 10:46:06 -0700131 setFullscreen(BrowserSettings.getInstance().useFullscreen());
Michael Kolb5a4372f2011-04-29 13:53:10 -0700132 mGenericFavicon = res.getDrawable(
133 R.drawable.app_web_browser_sm);
John Reck0f602f32011-07-07 15:38:43 -0700134 mTitleBar = new TitleBar(mActivity, mUiController, this,
135 mContentView);
136 mTitleBar.setProgress(100);
137 mNavigationBar = mTitleBar.getNavigationBar();
John Reck718a24d2011-08-12 11:08:30 -0700138 mUrlBarAutoShowManager = new UrlBarAutoShowManager(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700139 }
140
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 private void cancelStopToast() {
142 if (mStopToast != null) {
143 mStopToast.cancel();
144 mStopToast = null;
145 }
146 }
147
148 // lifecycle
149
150 public void onPause() {
Michael Kolb7a5cf472010-11-30 13:23:53 -0800151 if (isCustomViewShowing()) {
152 onHideCustomView();
153 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700154 cancelStopToast();
155 mActivityPaused = true;
156 }
157
158 public void onResume() {
159 mActivityPaused = false;
160 }
161
Michael Kolb66706532010-12-12 19:50:22 -0800162 protected boolean isActivityPaused() {
163 return mActivityPaused;
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 }
165
166 public void onConfigurationChanged(Configuration config) {
167 }
168
John Reck0f602f32011-07-07 15:38:43 -0700169 public Activity getActivity() {
170 return mActivity;
171 }
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 // key handling
174
175 @Override
176 public boolean onBackKey() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700177 if (mCustomView != null) {
178 mUiController.hideCustomView();
179 return true;
180 }
181 return false;
182 }
183
Michael Kolb2814a362011-05-19 15:49:41 -0700184 @Override
185 public boolean onMenuKey() {
186 return false;
187 }
188
John Reck30c714c2010-12-16 17:30:34 -0800189 // Tab callbacks
Michael Kolb8233fac2010-10-26 16:08:53 -0700190 @Override
John Reck30c714c2010-12-16 17:30:34 -0800191 public void onTabDataChanged(Tab tab) {
192 setUrlTitle(tab);
193 setFavicon(tab);
194 updateLockIconToLatest(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800195 updateNavigationState(tab);
John Reckef654f12011-07-12 16:42:08 -0700196 mTitleBar.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700197 mNavigationBar.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 }
199
200 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500201 public void bookmarkedStatusHasChanged(Tab tab) {
John Reck94b7e042011-02-15 15:02:33 -0800202 if (tab.inForeground()) {
203 boolean isBookmark = tab.isBookmarkedSite();
John Reck0f602f32011-07-07 15:38:43 -0700204 mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
John Reck94b7e042011-02-15 15:02:33 -0800205 }
Leon Scroggins4cd97792010-12-03 15:31:56 -0500206 }
207
208 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 public void onPageStopped(Tab tab) {
210 cancelStopToast();
211 if (tab.inForeground()) {
212 mStopToast = Toast
213 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
214 mStopToast.show();
215 }
216 }
217
218 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800219 public boolean needsRestoreAllTabs() {
John Reck847b5322011-04-14 17:02:18 -0700220 return true;
Michael Kolb1bf23132010-11-19 12:55:12 -0800221 }
222
223 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700224 public void addTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700225 }
226
227 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800228 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700229 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb77df4562010-11-19 14:49:34 -0800230 if ((tab != mActiveTab) && (mActiveTab != null)) {
231 removeTabFromContentView(mActiveTab);
John Reckbf2ec202011-06-29 17:47:38 -0700232 WebView web = mActiveTab.getWebView();
233 if (web != null) {
234 web.setOnTouchListener(null);
235 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 }
Michael Kolb77df4562010-11-19 14:49:34 -0800237 mActiveTab = tab;
John Reckbf2ec202011-06-29 17:47:38 -0700238 WebView web = mActiveTab.getWebView();
John Reck718a24d2011-08-12 11:08:30 -0700239 updateUrlBarAutoShowManagerTarget();
John Reck5d43ce82011-06-21 17:16:51 -0700240 attachTabToContentView(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700241 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
John Reck30c714c2010-12-16 17:30:34 -0800242 onTabDataChanged(tab);
243 onProgressChanged(tab);
John Reck117f07d2011-01-24 09:39:03 -0800244 boolean incognito = mActiveTab.getWebView().isPrivateBrowsingEnabled();
John Reck0f602f32011-07-07 15:38:43 -0700245 mNavigationBar.setIncognitoMode(incognito);
Patrick Scott92066772011-03-10 08:46:27 -0500246 updateAutoLogin(tab, false);
John Reck6ac5bfd2011-07-01 17:02:55 -0700247 if (web != null && web.getVisibleTitleHeight()
John Reck0f602f32011-07-07 15:38:43 -0700248 != mTitleBar.getEmbeddedHeight()
Michael Kolb0241e752011-07-07 14:58:50 -0700249 && !mUseQuickControls) {
John Reck6ac5bfd2011-07-01 17:02:55 -0700250 showTitleBarForDuration();
251 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700252 }
253
John Reck718a24d2011-08-12 11:08:30 -0700254 protected void updateUrlBarAutoShowManagerTarget() {
255 WebView web = mActiveTab != null ? mActiveTab.getWebView() : null;
256 if (!mUseQuickControls && web instanceof BrowserWebView) {
257 mUrlBarAutoShowManager.setTarget((BrowserWebView) web);
258 } else {
259 mUrlBarAutoShowManager.setTarget(null);
260 }
261 }
262
Michael Kolbcfa3af52010-12-14 10:36:11 -0800263 Tab getActiveTab() {
264 return mActiveTab;
265 }
266
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800268 public void updateTabs(List<Tab> tabs) {
Michael Kolb1bf23132010-11-19 12:55:12 -0800269 }
270
271 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800273 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800275 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 }
278
279 @Override
280 public void detachTab(Tab tab) {
281 removeTabFromContentView(tab);
282 }
283
284 @Override
285 public void attachTab(Tab tab) {
286 attachTabToContentView(tab);
287 }
288
Michael Kolb377ea312011-02-17 14:36:56 -0800289 protected void attachTabToContentView(Tab tab) {
Michael Kolbd1e2ccc2011-01-24 11:38:31 -0800290 if ((tab == null) || (tab.getWebView() == null)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700291 return;
292 }
293 View container = tab.getViewContainer();
294 WebView mainView = tab.getWebView();
295 // Attach the WebView to the container and then attach the
296 // container to the content view.
297 FrameLayout wrapper =
298 (FrameLayout) container.findViewById(R.id.webview_wrapper);
299 ViewGroup parent = (ViewGroup) mainView.getParent();
300 if (parent != wrapper) {
301 if (parent != null) {
302 Log.w(LOGTAG, "mMainView already has a parent in"
303 + " attachTabToContentView!");
304 parent.removeView(mainView);
305 }
306 wrapper.addView(mainView);
307 } else {
308 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
309 + " attachTabToContentView!");
310 }
311 parent = (ViewGroup) container.getParent();
312 if (parent != mContentView) {
313 if (parent != null) {
314 Log.w(LOGTAG, "mContainer already has a parent in"
315 + " attachTabToContentView!");
316 parent.removeView(container);
317 }
318 mContentView.addView(container, COVER_SCREEN_PARAMS);
319 } else {
320 Log.w(LOGTAG, "mContainer is already attached to content in"
321 + " attachTabToContentView!");
322 }
323 mUiController.attachSubWindow(tab);
324 }
325
326 private void removeTabFromContentView(Tab tab) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800327 hideTitleBar();
Michael Kolb8233fac2010-10-26 16:08:53 -0700328 // Remove the container that contains the main WebView.
329 WebView mainView = tab.getWebView();
330 View container = tab.getViewContainer();
331 if (mainView == null) {
332 return;
333 }
334 // Remove the container from the content and then remove the
335 // WebView from the container. This will trigger a focus change
336 // needed by WebView.
Michael Kolb20776cc2011-01-31 10:19:05 -0800337 mainView.setEmbeddedTitleBar(null);
Michael Kolb8233fac2010-10-26 16:08:53 -0700338 FrameLayout wrapper =
339 (FrameLayout) container.findViewById(R.id.webview_wrapper);
340 wrapper.removeView(mainView);
341 mContentView.removeView(container);
342 mUiController.endActionMode();
343 mUiController.removeSubWindow(tab);
344 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
345 if (errorConsole != null) {
346 mErrorConsoleContainer.removeView(errorConsole);
347 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700348 }
349
Michael Kolba713ec82010-11-29 17:27:06 -0800350 @Override
351 public void onSetWebView(Tab tab, WebView webView) {
352 View container = tab.getViewContainer();
353 if (container == null) {
354 // The tab consists of a container view, which contains the main
355 // WebView, as well as any other UI elements associated with the tab.
356 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
John Reck7c6e1c92011-06-30 11:55:55 -0700357 mContentView, false);
Michael Kolba713ec82010-11-29 17:27:06 -0800358 tab.setViewContainer(container);
359 }
360 if (tab.getWebView() != webView) {
361 // Just remove the old one.
362 FrameLayout wrapper =
363 (FrameLayout) container.findViewById(R.id.webview_wrapper);
364 wrapper.removeView(tab.getWebView());
365 }
366 }
367
Michael Kolb8233fac2010-10-26 16:08:53 -0700368 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800369 * create a sub window container and webview for the tab
370 * Note: this methods operates through side-effects for now
371 * it sets both the subView and subViewContainer for the given tab
372 * @param tab tab to create the sub window for
373 * @param subView webview to be set as a subwindow for the tab
374 */
375 @Override
376 public void createSubWindow(Tab tab, WebView subView) {
377 View subViewContainer = mActivity.getLayoutInflater().inflate(
378 R.layout.browser_subwindow, null);
379 ViewGroup inner = (ViewGroup) subViewContainer
380 .findViewById(R.id.inner_container);
381 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
382 LayoutParams.MATCH_PARENT));
383 final ImageButton cancel = (ImageButton) subViewContainer
384 .findViewById(R.id.subwindow_close);
385 final WebView cancelSubView = subView;
386 cancel.setOnClickListener(new OnClickListener() {
387 public void onClick(View v) {
388 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
389 }
390 });
391 tab.setSubWebView(subView);
392 tab.setSubViewContainer(subViewContainer);
393 }
394
395 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 * Remove the sub window from the content view.
397 */
398 @Override
399 public void removeSubWindow(View subviewContainer) {
400 mContentView.removeView(subviewContainer);
401 mUiController.endActionMode();
402 }
403
404 /**
405 * Attach the sub window to the content view.
406 */
407 @Override
408 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800409 if (container.getParent() != null) {
410 // already attached, remove first
411 ((ViewGroup) container.getParent()).removeView(container);
412 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700413 mContentView.addView(container, COVER_SCREEN_PARAMS);
414 }
415
Michael Kolb11d19782011-03-20 10:17:40 -0700416 protected void refreshWebView() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700417 WebView web = getWebView();
418 if (web != null) {
419 web.invalidate();
Michael Kolb11d19782011-03-20 10:17:40 -0700420 }
421 }
422
Michael Kolb46f987e2011-04-05 10:39:10 -0700423 public void editUrl(boolean clearInput) {
424 if (mUiController.isInCustomActionMode()) {
425 mUiController.endActionMode();
426 }
427 showTitleBar();
Michael Kolbace2ff82011-08-12 13:36:07 -0700428 if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
John Reckef654f12011-07-12 16:42:08 -0700429 mNavigationBar.startEditingUrl(clearInput);
430 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700431 }
432
Michael Kolb7cdc4902011-02-03 17:54:40 -0800433 boolean canShowTitleBar() {
434 return !isTitleBarShowing()
435 && !isActivityPaused()
436 && (getActiveTab() != null)
Michael Kolb46f987e2011-04-05 10:39:10 -0700437 && (getWebView() != null)
Michael Kolb7cdc4902011-02-03 17:54:40 -0800438 && !mUiController.isInCustomActionMode();
439 }
440
John Reck0f602f32011-07-07 15:38:43 -0700441 protected void showTitleBar() {
John Reckef654f12011-07-12 16:42:08 -0700442 mHandler.removeMessages(MSG_HIDE_TITLEBAR);
Michael Kolb46f987e2011-04-05 10:39:10 -0700443 if (canShowTitleBar()) {
John Reck0f602f32011-07-07 15:38:43 -0700444 mTitleBar.show();
Michael Kolb46f987e2011-04-05 10:39:10 -0700445 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800446 }
447
448 protected void hideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700449 if (mTitleBar.isShowing()) {
450 mTitleBar.hide();
Michael Kolb46f987e2011-04-05 10:39:10 -0700451 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800452 }
453
454 protected boolean isTitleBarShowing() {
John Reck0f602f32011-07-07 15:38:43 -0700455 return mTitleBar.isShowing();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800456 }
457
John Reck5d43ce82011-06-21 17:16:51 -0700458 public boolean isEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700459 return mTitleBar.isEditingUrl();
John Reck5d43ce82011-06-21 17:16:51 -0700460 }
461
John Reck0f602f32011-07-07 15:38:43 -0700462 public TitleBar getTitleBar() {
463 return mTitleBar;
464 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800465
466 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700467 WebView web = getWebView();
468 if (web != null) {
469 web.setTitleBarGravity(gravity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700470 }
471 }
472
Michael Kolb66706532010-12-12 19:50:22 -0800473 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700474 public void showVoiceTitleBar(String title, List<String> results) {
John Reck0f602f32011-07-07 15:38:43 -0700475 mNavigationBar.setInVoiceMode(true, results);
476 mNavigationBar.setDisplayTitle(title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700477 }
478
Michael Kolb66706532010-12-12 19:50:22 -0800479 @Override
480 public void revertVoiceTitleBar(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700481 mNavigationBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800482 String url = tab.getUrl();
John Reck0f602f32011-07-07 15:38:43 -0700483 mNavigationBar.setDisplayTitle(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 }
485
486 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700487 public void registerDropdownChangeListener(DropdownChangeListener d) {
John Reck0f602f32011-07-07 15:38:43 -0700488 mNavigationBar.registerDropdownChangeListener(d);
Michael Kolb11d19782011-03-20 10:17:40 -0700489 }
490
491 @Override
John Reck2bc80422011-06-30 15:11:49 -0700492 public void showComboView(ComboViews startingView, Bundle extras) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700493 Intent intent = new Intent(mActivity, ComboViewActivity.class);
494 intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
495 intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
496 Tab t = getActiveTab();
497 if (t != null) {
498 intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
John Reck439c9a52010-12-14 10:04:39 -0800499 }
John Reckd3e4d5b2011-07-13 15:48:43 -0700500 intent.putExtra(ComboViewActivity.EXTRA_BOOKMARK_PAGE,
501 mUiController.createBookmarkCurrentPageIntent(false));
502 mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 }
504
505 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400506 public void showCustomView(View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -0700507 WebChromeClient.CustomViewCallback callback) {
508 // if a view already exists then immediately terminate the new one
509 if (mCustomView != null) {
510 callback.onCustomViewHidden();
511 return;
512 }
513
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400514 mOriginalOrientation = mActivity.getRequestedOrientation();
515
Michael Kolb8233fac2010-10-26 16:08:53 -0700516 // Add the custom view to its container.
517 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
518 mCustomView = view;
519 mCustomViewCallback = callback;
520 // Hide the content view.
521 mContentView.setVisibility(View.GONE);
522 // Finally show the custom view container.
523 setStatusBarVisibility(false);
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400524 mActivity.setRequestedOrientation(requestedOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 mCustomViewContainer.setVisibility(View.VISIBLE);
526 mCustomViewContainer.bringToFront();
527 }
528
529 @Override
530 public void onHideCustomView() {
531 if (mCustomView == null)
532 return;
533
534 // Hide the custom view.
535 mCustomView.setVisibility(View.GONE);
536 // Remove the custom view from its container.
537 mCustomViewContainer.removeView(mCustomView);
538 mCustomView = null;
539 mCustomViewContainer.setVisibility(View.GONE);
540 mCustomViewCallback.onCustomViewHidden();
541 // Show the content view.
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400542 mActivity.setRequestedOrientation(mOriginalOrientation);
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 setStatusBarVisibility(true);
544 mContentView.setVisibility(View.VISIBLE);
545 }
546
547 @Override
548 public boolean isCustomViewShowing() {
549 return mCustomView != null;
550 }
551
Michael Kolb66706532010-12-12 19:50:22 -0800552 protected void dismissIME() {
Michael Kolb3a696282010-12-05 13:23:24 -0800553 if (mInputManager.isActive()) {
554 mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
555 0);
556 }
557 }
558
Michael Kolb66706532010-12-12 19:50:22 -0800559 @Override
John Reck3ba45532011-08-11 16:26:53 -0700560 public boolean isWebShowing() {
John Reckd3e4d5b2011-07-13 15:48:43 -0700561 return mCustomView == null;
Michael Kolb66706532010-12-12 19:50:22 -0800562 }
563
Patrick Scott92066772011-03-10 08:46:27 -0500564 @Override
565 public void showAutoLogin(Tab tab) {
566 updateAutoLogin(tab, true);
567 }
568
569 @Override
570 public void hideAutoLogin(Tab tab) {
571 updateAutoLogin(tab, true);
572 }
573
Michael Kolb8233fac2010-10-26 16:08:53 -0700574 // -------------------------------------------------------------------------
575
Michael Kolb5a72f182011-01-13 20:35:06 -0800576 protected void updateNavigationState(Tab tab) {
577 }
578
Michael Kolb11d19782011-03-20 10:17:40 -0700579 protected void updateAutoLogin(Tab tab, boolean animate) {
John Reck0f602f32011-07-07 15:38:43 -0700580 mTitleBar.updateAutoLogin(tab, animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700581 }
Patrick Scott92066772011-03-10 08:46:27 -0500582
Michael Kolb8233fac2010-10-26 16:08:53 -0700583 /**
584 * Update the lock icon to correspond to our latest state.
585 */
Michael Kolb66706532010-12-12 19:50:22 -0800586 protected void updateLockIconToLatest(Tab t) {
John Reck30c714c2010-12-16 17:30:34 -0800587 if (t != null && t.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 updateLockIconImage(t.getLockIconType());
589 }
590 }
591
592 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 * Updates the lock-icon image in the title-bar.
594 */
John Reck30c714c2010-12-16 17:30:34 -0800595 private void updateLockIconImage(LockIcon lockIconType) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700596 Drawable d = null;
John Reck30c714c2010-12-16 17:30:34 -0800597 if (lockIconType == LockIcon.LOCK_ICON_SECURE) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 d = mSecLockIcon;
John Reck30c714c2010-12-16 17:30:34 -0800599 } else if (lockIconType == LockIcon.LOCK_ICON_MIXED) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700600 d = mMixLockIcon;
601 }
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.isInVoiceSearchMode()) return;
612 if (tab.inForeground()) {
John Reck0f602f32011-07-07 15:38:43 -0700613 mNavigationBar.setDisplayTitle(url);
Michael Kolb66706532010-12-12 19:50:22 -0800614 }
615 }
616
617 // Set the favicon in the title bar.
John Reck30c714c2010-12-16 17:30:34 -0800618 protected void setFavicon(Tab tab) {
619 if (tab.inForeground()) {
620 Bitmap icon = tab.getFavicon();
John Reck0f602f32011-07-07 15:38:43 -0700621 mNavigationBar.setFavicon(icon);
John Reck30c714c2010-12-16 17:30:34 -0800622 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700623 }
624
625 @Override
626 public void onActionModeFinished(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 }
628
Michael Kolb66706532010-12-12 19:50:22 -0800629 // active tabs page
630
631 public void showActiveTabsPage() {
632 }
633
634 /**
635 * Remove the active tabs page.
636 */
637 public void removeActiveTabsPage() {
638 }
639
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 // menu handling callbacks
641
642 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800643 public boolean onPrepareOptionsMenu(Menu menu) {
644 return true;
645 }
646
647 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700648 public void updateMenuState(Tab tab, Menu menu) {
649 }
650
651 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 public void onOptionsMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 }
654
655 @Override
656 public void onExtendedMenuOpened() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700657 }
658
659 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700660 public boolean onOptionsItemSelected(MenuItem item) {
661 return false;
662 }
663
664 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 public void onOptionsMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 }
667
668 @Override
669 public void onExtendedMenuClosed(boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 }
671
672 @Override
673 public void onContextMenuCreated(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700674 }
675
676 @Override
677 public void onContextMenuClosed(Menu menu, boolean inLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 }
679
680 // error console
681
682 @Override
683 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
Michael Kolb9fcefd12011-02-17 10:55:59 -0800684 if (tab == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700685 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
686 if (flag) {
687 // Setting the show state of the console will cause it's the layout
688 // to be inflated.
689 if (errorConsole.numberOfErrors() > 0) {
690 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
691 } else {
692 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
693 }
694 if (errorConsole.getParent() != null) {
695 mErrorConsoleContainer.removeView(errorConsole);
696 }
697 // Now we can add it to the main view.
698 mErrorConsoleContainer.addView(errorConsole,
699 new LinearLayout.LayoutParams(
700 ViewGroup.LayoutParams.MATCH_PARENT,
701 ViewGroup.LayoutParams.WRAP_CONTENT));
702 } else {
703 mErrorConsoleContainer.removeView(errorConsole);
704 }
705 }
706
707 private void setStatusBarVisibility(boolean visible) {
Joe Onoratod3bf86f2011-01-25 20:07:10 -0800708 WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
709 params.systemUiVisibility = visible ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN;
710 mActivity.getWindow().setAttributes(params);
Michael Kolb8233fac2010-10-26 16:08:53 -0700711 }
712
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 // -------------------------------------------------------------------------
714 // Helper function for WebChromeClient
715 // -------------------------------------------------------------------------
716
717 @Override
718 public Bitmap getDefaultVideoPoster() {
719 if (mDefaultVideoPoster == null) {
720 mDefaultVideoPoster = BitmapFactory.decodeResource(
721 mActivity.getResources(), R.drawable.default_video_poster);
722 }
723 return mDefaultVideoPoster;
724 }
725
726 @Override
727 public View getVideoLoadingProgressView() {
728 if (mVideoProgressView == null) {
729 LayoutInflater inflater = LayoutInflater.from(mActivity);
730 mVideoProgressView = inflater.inflate(
731 R.layout.video_loading_progress, null);
732 }
733 return mVideoProgressView;
734 }
735
Michael Kolb843510f2010-12-09 10:51:49 -0800736 @Override
737 public void showMaxTabsWarning() {
738 Toast warning = Toast.makeText(mActivity,
739 mActivity.getString(R.string.max_tabs_warning),
740 Toast.LENGTH_SHORT);
741 warning.show();
742 }
743
Michael Kolb46f987e2011-04-05 10:39:10 -0700744 protected WebView getWebView() {
745 if (mActiveTab != null) {
746 return mActiveTab.getWebView();
747 } else {
748 return null;
749 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700750 }
751
Michael Kolbfedb4922011-04-20 16:45:33 -0700752 protected Menu getMenu() {
753 MenuBuilder menu = new MenuBuilder(mActivity);
754 mActivity.getMenuInflater().inflate(R.menu.browser, menu);
755 return menu;
756 }
757
Michael Kolbc38c6042011-04-27 10:46:06 -0700758 public void setFullscreen(boolean enabled) {
759 if (enabled) {
760 mActivity.getWindow().setFlags(
761 WindowManager.LayoutParams.FLAG_FULLSCREEN,
762 WindowManager.LayoutParams.FLAG_FULLSCREEN);
763 } else {
764 mActivity.getWindow().clearFlags(
765 WindowManager.LayoutParams.FLAG_FULLSCREEN);
766 }
767 }
768
John Reck0f602f32011-07-07 15:38:43 -0700769 public Drawable getFaviconDrawable(Bitmap icon) {
Michael Kolb5a4372f2011-04-29 13:53:10 -0700770 Drawable[] array = new Drawable[3];
771 array[0] = new PaintDrawable(Color.BLACK);
772 PaintDrawable p = new PaintDrawable(Color.WHITE);
773 array[1] = p;
774 if (icon == null) {
775 array[2] = mGenericFavicon;
776 } else {
777 array[2] = new BitmapDrawable(icon);
778 }
779 LayerDrawable d = new LayerDrawable(array);
780 d.setLayerInset(1, 1, 1, 1, 1);
781 d.setLayerInset(2, 2, 2, 2, 2);
782 return d;
783 }
784
John Reck5d43ce82011-06-21 17:16:51 -0700785 public boolean isLoading() {
786 return mActiveTab != null ? mActiveTab.inPageLoad() : false;
787 }
788
789 /**
790 * Suggest to the UI that the title bar can be hidden. The UI will then
791 * decide whether or not to hide based off a number of factors, such
792 * as if the user is editing the URL bar or if the page is loading
793 */
794 public void suggestHideTitleBar() {
John Reck58891902011-08-11 17:48:53 -0700795 if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
796 && !mNavigationBar.isMenuShowing()) {
John Reck5d43ce82011-06-21 17:16:51 -0700797 hideTitleBar();
798 }
799 }
800
Michael Kolb017ffab2011-07-11 15:26:47 -0700801 protected void showTitleBarForDuration() {
John Reck6ac5bfd2011-07-01 17:02:55 -0700802 showTitleBar();
803 Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
804 mHandler.sendMessageDelayed(msg, HIDE_TITLEBAR_DELAY);
805 }
806
John Reck5d43ce82011-06-21 17:16:51 -0700807 private Handler mHandler = new Handler() {
808
809 @Override
810 public void handleMessage(Message msg) {
811 if (msg.what == MSG_HIDE_TITLEBAR) {
812 suggestHideTitleBar();
813 }
814 }
815 };
John Reck3ba45532011-08-11 16:26:53 -0700816
817 @Override
818 public void showWeb(boolean animate) {
819 mUiController.hideCustomView();
820 }
821
Michael Kolb8233fac2010-10-26 16:08:53 -0700822}