blob: 391b83ebb1914b538e6f9c6674bb73cb6b42eb06 [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 Kolba4183062011-01-16 10:43:21 -080031import android.view.KeyEvent;
Michael Kolb0d0245f2011-12-05 16:36:05 -080032import android.view.Menu;
33import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080034import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000035import android.webkit.WebViewClassic;
Michael Kolb66706532010-12-12 19:50:22 -080036
37import java.util.List;
38
39/**
40 * Ui for xlarge screen sizes
41 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070042public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080043
44 private static final String LOGTAG = "XLargeUi";
45
John Reck034637c2011-08-11 11:34:44 -070046 private PaintDrawable mFaviconBackground;
47
Michael Kolb376b5412010-12-15 11:52:57 -080048 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080049 private TabBar mTabBar;
50
John Reck0f602f32011-07-07 15:38:43 -070051 private NavigationBarTablet mNavBar;
Michael Kolb66706532010-12-12 19:50:22 -080052
Michael Kolb80f75082012-04-10 10:50:06 -070053 private PieControl mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080054 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080055
Michael Kolb66706532010-12-12 19:50:22 -080056 /**
57 * @param browser
58 * @param controller
59 */
60 public XLargeUi(Activity browser, UiController controller) {
61 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080062 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070063 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080064 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080065 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080066 setupActionBar();
67 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
68 }
69
70 private void setupActionBar() {
71 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080072 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
73 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080074 }
75
John Reck2bc80422011-06-30 15:11:49 -070076 public void showComboView(ComboViews startWith, Bundle extras) {
77 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080078 if (mUseQuickControls) {
79 mActionBar.show();
80 }
81 }
82
83 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070084 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolb376b5412010-12-15 11:52:57 -080085 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080086 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080087 if (useQuickControls) {
88 checkTabCount();
Michael Kolb80f75082012-04-10 10:50:06 -070089 mPieControl = new PieControl(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080090 mPieControl.attachToContainer(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -080091 } else {
92 mActivity.getActionBar().show();
93 if (mPieControl != null) {
94 mPieControl.removeFromContainer(mContentView);
95 }
Michael Kolb376b5412010-12-15 11:52:57 -080096 }
97 mTabBar.setUseQuickControls(mUseQuickControls);
John Reck1cf4b792011-07-26 10:22:22 -070098 // We need to update the tabs with this change
99 for (Tab t : mTabControl.getTabs()) {
100 t.updateShouldCaptureThumbnails();
101 }
John Reck718a24d2011-08-12 11:08:30 -0700102 updateUrlBarAutoShowManagerTarget();
Michael Kolb376b5412010-12-15 11:52:57 -0800103 }
104
105 private void checkTabCount() {
106 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800107 mHandler.post(new Runnable() {
108 public void run() {
109 mActionBar.hide();
110 }
111 });
Michael Kolb376b5412010-12-15 11:52:57 -0800112 }
Michael Kolb66706532010-12-12 19:50:22 -0800113 }
114
115 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000116 public void onResume() {
117 super.onResume();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000118 mNavBar.clearCompletions();
Michael Kolbd2e35932012-01-19 09:29:16 -0800119 checkTabCount();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000120 }
121
122 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800123 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800124 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800125 }
126
Michael Kolb66706532010-12-12 19:50:22 -0800127 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700128 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800129 if (web != null) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000130 WebViewClassic.fromWebView(web).stopScroll();
Michael Kolb66706532010-12-12 19:50:22 -0800131 }
132 }
133
Michael Kolb0d0245f2011-12-05 16:36:05 -0800134 @Override
135 public boolean onPrepareOptionsMenu(Menu menu) {
136 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
137 if (bm != null) {
138 bm.setVisible(false);
139 }
140 return true;
141 }
142
143
Michael Kolb66706532010-12-12 19:50:22 -0800144 // WebView callbacks
145
146 @Override
John Reck30c714c2010-12-16 17:30:34 -0800147 public void onProgressChanged(Tab tab) {
148 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800149 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800150 mTitleBar.setProgress(progress);
Michael Kolb1c3a6d22011-12-15 15:28:49 -0800151 if (mUseQuickControls) {
152 if (progress == 100) {
153 mTitleBar.setShowProgressOnly(false);
154 } else if (!mTitleBar.isEditingUrl()) {
155 mTitleBar.setShowProgressOnly(true);
156 }
157 }
Michael Kolb66706532010-12-12 19:50:22 -0800158 }
159 }
160
161 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800162 public void addTab(Tab tab) {
163 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800164 }
165
166 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800167 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800168 }
169
170 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800171 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700172 mTitleBar.cancelTitleBarAnimation(true);
173 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700174 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700175 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800176 // TabControl.setCurrentTab has been called before this,
177 // so the tab is guaranteed to have a webview
178 if (view == null) {
179 Log.e(LOGTAG, "active tab with no webview detected");
180 return;
181 }
182 // Request focus on the top window.
183 if (mUseQuickControls) {
184 mPieControl.forceToTop(mContentView);
Michael Kolb4923c222012-04-02 16:18:36 -0700185 view.setTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -0800186 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700187 view.setTitleBar(mTitleBar);
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);
John Reck5d43ce82011-06-21 17:16:51 -0700196 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800197 }
198
199 @Override
200 public void updateTabs(List<Tab> tabs) {
201 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800202 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800203 }
204
205 @Override
206 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700207 mTitleBar.cancelTitleBarAnimation(true);
208 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800209 super.removeTab(tab);
210 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700211 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800212 }
213
214 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800215 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800216 }
217
Michael Kolb376b5412010-12-15 11:52:57 -0800218 int getContentWidth() {
219 if (mContentView != null) {
220 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800221 }
222 return 0;
223 }
224
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800225 @Override
226 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700227 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700228 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800229 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700230 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800231 }
232
Michael Kolb7cdc4902011-02-03 17:54:40 -0800233 @Override
234 protected void showTitleBar() {
235 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700236 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800237 }
Michael Kolb376b5412010-12-15 11:52:57 -0800238 }
239
Michael Kolb66706532010-12-12 19:50:22 -0800240 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800241 protected void hideTitleBar() {
242 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700243 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800244 }
245 }
246
Michael Kolb66706532010-12-12 19:50:22 -0800247 // action mode callbacks
248
249 @Override
250 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800251 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700252 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800253 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800254 }
255 }
256
257 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800258 public void onActionModeFinished(boolean inLoad) {
259 checkTabCount();
260 if (inLoad) {
261 // the titlebar was removed when the CAB was shown
262 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800263 if (mUseQuickControls) {
264 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800265 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800266 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800267 }
268 }
269
270 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800271 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700272 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800273 }
274
275 @Override
John Reck30c714c2010-12-16 17:30:34 -0800276 public void setUrlTitle(Tab tab) {
277 super.setUrlTitle(tab);
278 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800279 }
280
281 // Set the favicon in the title bar.
282 @Override
John Reck30c714c2010-12-16 17:30:34 -0800283 public void setFavicon(Tab tab) {
284 super.setFavicon(tab);
285 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800286 }
287
Michael Kolbcfa3af52010-12-14 10:36:11 -0800288 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800289 public void onHideCustomView() {
290 super.onHideCustomView();
291 if (mUseQuickControls) {
292 checkTabCount();
John Reckd73c5a22010-12-22 10:22:50 -0800293 }
294 }
Michael Kolba4183062011-01-16 10:43:21 -0800295
296 @Override
297 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800298 if (mActiveTab != null) {
299 WebView web = mActiveTab.getWebView();
300 if (event.getAction() == KeyEvent.ACTION_DOWN) {
301 switch (code) {
302 case KeyEvent.KEYCODE_TAB:
303 case KeyEvent.KEYCODE_DPAD_UP:
304 case KeyEvent.KEYCODE_DPAD_LEFT:
305 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
306 editUrl(false);
307 return true;
308 }
309 }
310 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
311 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
312 editUrl(true);
313 return mContentView.dispatchKeyEvent(event);
314 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800315 }
Michael Kolba4183062011-01-16 10:43:21 -0800316 }
317 return false;
318 }
319
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800320 private boolean isTypingKey(KeyEvent evt) {
321 return evt.getUnicodeChar() > 0;
322 }
323
324 TabBar getTabBar() {
325 return mTabBar;
326 }
327
John Reck1cf4b792011-07-26 10:22:22 -0700328 @Override
329 public boolean shouldCaptureThumbnails() {
330 return mUseQuickControls;
331 }
332
John Reck034637c2011-08-11 11:34:44 -0700333 private Drawable getFaviconBackground() {
334 if (mFaviconBackground == null) {
335 mFaviconBackground = new PaintDrawable();
336 Resources res = mActivity.getResources();
337 mFaviconBackground.getPaint().setColor(
338 res.getColor(R.color.tabFaviconBackground));
339 mFaviconBackground.setCornerRadius(
340 res.getDimension(R.dimen.tab_favicon_corner_radius));
341 }
342 return mFaviconBackground;
343 }
344
345 @Override
346 public Drawable getFaviconDrawable(Bitmap icon) {
347 Drawable[] array = new Drawable[2];
348 array[0] = getFaviconBackground();
349 if (icon == null) {
350 array[1] = mGenericFavicon;
351 } else {
352 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
353 }
354 LayerDrawable d = new LayerDrawable(array);
355 d.setLayerInset(1, 2, 2, 2, 2);
356 return d;
357 }
358
Michael Kolb66706532010-12-12 19:50:22 -0800359}