blob: 556e88d1d6959baea48e56d9b98e74d356d3cb3f [file] [log] [blame]
Michael Kolb66706532010-12-12 19:50:22 -08001/*
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 Kolb66706532010-12-12 19:50:22 -080019import android.app.ActionBar;
20import android.app.Activity;
John Reck034637c2011-08-11 11:34:44 -070021import android.content.res.Resources;
22import android.graphics.Bitmap;
23import android.graphics.drawable.BitmapDrawable;
24import android.graphics.drawable.Drawable;
25import android.graphics.drawable.LayerDrawable;
26import android.graphics.drawable.PaintDrawable;
Michael Kolbac35bdc2011-01-17 17:06:04 -080027import android.os.Bundle;
Michael Kolbba238702011-03-08 10:40:50 -080028import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080029import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080030import android.view.ActionMode;
Michael Kolb376b5412010-12-15 11:52:57 -080031import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080032import android.view.KeyEvent;
John Reckd73c5a22010-12-22 10:22:50 -080033import android.view.View;
Michael Kolb52051eb2011-08-02 13:06:43 -070034import android.view.ViewGroup;
John Reckd73c5a22010-12-22 10:22:50 -080035import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080036import android.webkit.WebView;
37
38import java.util.List;
39
40/**
41 * Ui for xlarge screen sizes
42 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070043public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080044
45 private static final String LOGTAG = "XLargeUi";
46
John Reck034637c2011-08-11 11:34:44 -070047 private PaintDrawable mFaviconBackground;
48
Michael Kolb376b5412010-12-15 11:52:57 -080049 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080050 private TabBar mTabBar;
51
John Reck0f602f32011-07-07 15:38:43 -070052 private NavigationBarTablet mNavBar;
Michael Kolb66706532010-12-12 19:50:22 -080053
Michael Kolb0241e752011-07-07 14:58:50 -070054 private PieControlXLarge mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080055 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080056
Michael Kolb66706532010-12-12 19:50:22 -080057 /**
58 * @param browser
59 * @param controller
60 */
61 public XLargeUi(Activity browser, UiController controller) {
62 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080063 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070064 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080065 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080066 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080067 setupActionBar();
68 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
69 }
70
71 private void setupActionBar() {
72 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080073 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
74 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080075 }
76
John Reck2bc80422011-06-30 15:11:49 -070077 public void showComboView(ComboViews startWith, Bundle extras) {
78 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080079 if (mUseQuickControls) {
80 mActionBar.show();
81 }
82 }
83
84 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070085 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolb376b5412010-12-15 11:52:57 -080086 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080087 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080088 if (useQuickControls) {
89 checkTabCount();
Michael Kolb0241e752011-07-07 14:58:50 -070090 mPieControl = new PieControlXLarge(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080091 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070092 WebView web = getWebView();
93 if (web != null) {
94 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -070095
Michael Kolb376b5412010-12-15 11:52:57 -080096 }
97 } else {
98 mActivity.getActionBar().show();
99 if (mPieControl != null) {
100 mPieControl.removeFromContainer(mContentView);
101 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700102 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800103 if (web != null) {
Michael Kolb52051eb2011-08-02 13:06:43 -0700104 if (mTitleBar.getParent() != null) {
105 ViewGroup p = (ViewGroup) mTitleBar.getParent();
106 p.removeView(mTitleBar);
107 }
Michael Kolb376b5412010-12-15 11:52:57 -0800108 web.setEmbeddedTitleBar(mTitleBar);
109 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700110 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800111 }
112 mTabBar.setUseQuickControls(mUseQuickControls);
John Reck1cf4b792011-07-26 10:22:22 -0700113 // We need to update the tabs with this change
114 for (Tab t : mTabControl.getTabs()) {
115 t.updateShouldCaptureThumbnails();
116 }
John Reck718a24d2011-08-12 11:08:30 -0700117 updateUrlBarAutoShowManagerTarget();
Michael Kolb376b5412010-12-15 11:52:57 -0800118 }
119
120 private void checkTabCount() {
121 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800122 mHandler.post(new Runnable() {
123 public void run() {
124 mActionBar.hide();
125 }
126 });
Michael Kolb376b5412010-12-15 11:52:57 -0800127 }
Michael Kolb66706532010-12-12 19:50:22 -0800128 }
129
130 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000131 public void onResume() {
132 super.onResume();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000133 mNavBar.clearCompletions();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000134 }
135
136 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800137 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800138 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800139 }
140
Michael Kolb66706532010-12-12 19:50:22 -0800141 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700142 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800143 if (web != null) {
144 web.stopScroll();
145 }
146 }
147
148 // WebView callbacks
149
150 @Override
John Reck30c714c2010-12-16 17:30:34 -0800151 public void onProgressChanged(Tab tab) {
152 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800153 mTabBar.onProgress(tab, progress);
154 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800155 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800156 }
157 }
158
159 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800160 public void addTab(Tab tab) {
161 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800162 }
163
164 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800165 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800166 }
167
168 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800169 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700170 mTitleBar.cancelTitleBarAnimation(true);
171 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700172 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700173 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800174 // TabControl.setCurrentTab has been called before this,
175 // so the tab is guaranteed to have a webview
176 if (view == null) {
177 Log.e(LOGTAG, "active tab with no webview detected");
178 return;
179 }
180 // Request focus on the top window.
181 if (mUseQuickControls) {
182 mPieControl.forceToTop(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -0800183 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800184 // check if title bar is already attached by animation
John Reck0f602f32011-07-07 15:38:43 -0700185 if (mTitleBar.getParent() == null) {
Michael Kolb377ea312011-02-17 14:36:56 -0800186 view.setEmbeddedTitleBar(mTitleBar);
187 }
Michael Kolb376b5412010-12-15 11:52:57 -0800188 }
Michael Kolb66706532010-12-12 19:50:22 -0800189 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800190 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700191 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800192 } else {
193 revertVoiceTitleBar(tab);
194 }
Michael Kolb376b5412010-12-15 11:52:57 -0800195 updateLockIconToLatest(tab);
196 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700197 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800198 }
199
200 @Override
201 public void updateTabs(List<Tab> tabs) {
202 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800203 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800204 }
205
206 @Override
207 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700208 mTitleBar.cancelTitleBarAnimation(true);
209 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800210 super.removeTab(tab);
211 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700212 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800213 }
214
215 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800216 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800217 }
218
Michael Kolb376b5412010-12-15 11:52:57 -0800219 int getContentWidth() {
220 if (mContentView != null) {
221 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800222 }
223 return 0;
224 }
225
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800226 @Override
227 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700228 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700229 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800230 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700231 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800232 }
233
234 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700235 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800236 }
237
238 @Override
239 protected void showTitleBar() {
240 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700241 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800242 }
Michael Kolb376b5412010-12-15 11:52:57 -0800243 }
244
Michael Kolb66706532010-12-12 19:50:22 -0800245 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800246 protected void hideTitleBar() {
247 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700248 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800249 }
250 }
251
252 @Override
Michael Kolb2a56eca2011-02-25 09:45:06 -0800253 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700254 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800255 super.setTitleGravity(gravity);
256 }
257 }
258
Michael Kolb66706532010-12-12 19:50:22 -0800259 // action mode callbacks
260
261 @Override
262 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800263 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700264 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800265 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800266 }
267 }
268
269 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800270 public void onActionModeFinished(boolean inLoad) {
271 checkTabCount();
272 if (inLoad) {
273 // the titlebar was removed when the CAB was shown
274 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800275 if (mUseQuickControls) {
276 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800277 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800278 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800279 }
280 }
281
282 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800283 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700284 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800285 }
286
287 @Override
John Reck30c714c2010-12-16 17:30:34 -0800288 public void setUrlTitle(Tab tab) {
289 super.setUrlTitle(tab);
290 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800291 }
292
293 // Set the favicon in the title bar.
294 @Override
John Reck30c714c2010-12-16 17:30:34 -0800295 public void setFavicon(Tab tab) {
296 super.setFavicon(tab);
297 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800298 }
299
Michael Kolbcfa3af52010-12-14 10:36:11 -0800300 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800301 public void onHideCustomView() {
302 super.onHideCustomView();
303 if (mUseQuickControls) {
304 checkTabCount();
John Reckd73c5a22010-12-22 10:22:50 -0800305 }
306 }
Michael Kolba4183062011-01-16 10:43:21 -0800307
308 @Override
309 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800310 if (mActiveTab != null) {
311 WebView web = mActiveTab.getWebView();
312 if (event.getAction() == KeyEvent.ACTION_DOWN) {
313 switch (code) {
314 case KeyEvent.KEYCODE_TAB:
315 case KeyEvent.KEYCODE_DPAD_UP:
316 case KeyEvent.KEYCODE_DPAD_LEFT:
317 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
318 editUrl(false);
319 return true;
320 }
321 }
322 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
323 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
324 editUrl(true);
325 return mContentView.dispatchKeyEvent(event);
326 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800327 }
Michael Kolba4183062011-01-16 10:43:21 -0800328 }
329 return false;
330 }
331
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800332 private boolean isTypingKey(KeyEvent evt) {
333 return evt.getUnicodeChar() > 0;
334 }
335
336 TabBar getTabBar() {
337 return mTabBar;
338 }
339
John Reck1cf4b792011-07-26 10:22:22 -0700340 @Override
341 public boolean shouldCaptureThumbnails() {
342 return mUseQuickControls;
343 }
344
John Reck034637c2011-08-11 11:34:44 -0700345 private Drawable getFaviconBackground() {
346 if (mFaviconBackground == null) {
347 mFaviconBackground = new PaintDrawable();
348 Resources res = mActivity.getResources();
349 mFaviconBackground.getPaint().setColor(
350 res.getColor(R.color.tabFaviconBackground));
351 mFaviconBackground.setCornerRadius(
352 res.getDimension(R.dimen.tab_favicon_corner_radius));
353 }
354 return mFaviconBackground;
355 }
356
357 @Override
358 public Drawable getFaviconDrawable(Bitmap icon) {
359 Drawable[] array = new Drawable[2];
360 array[0] = getFaviconBackground();
361 if (icon == null) {
362 array[1] = mGenericFavicon;
363 } else {
364 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
365 }
366 LayerDrawable d = new LayerDrawable(array);
367 d.setLayerInset(1, 2, 2, 2, 2);
368 return d;
369 }
370
Michael Kolb66706532010-12-12 19:50:22 -0800371}