blob: 8e874e98ab3b6d5eeccac0029ad82b2808933a4a [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 Kolbba238702011-03-08 10:40:50 -080053 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080054
Michael Kolb66706532010-12-12 19:50:22 -080055 /**
56 * @param browser
57 * @param controller
58 */
59 public XLargeUi(Activity browser, UiController controller) {
60 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080061 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070062 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080063 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080064 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080065 setupActionBar();
66 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
67 }
68
69 private void setupActionBar() {
70 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080071 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
72 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080073 }
74
John Reck2bc80422011-06-30 15:11:49 -070075 public void showComboView(ComboViews startWith, Bundle extras) {
76 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080077 if (mUseQuickControls) {
78 mActionBar.show();
79 }
80 }
81
82 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070083 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbda580632012-04-16 13:30:28 -070084 super.setUseQuickControls(useQuickControls);
85 checkHideActionBar();
86 if (!useQuickControls) {
87 mActionBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -080088 }
89 mTabBar.setUseQuickControls(mUseQuickControls);
John Reck1cf4b792011-07-26 10:22:22 -070090 // We need to update the tabs with this change
91 for (Tab t : mTabControl.getTabs()) {
92 t.updateShouldCaptureThumbnails();
93 }
Michael Kolb376b5412010-12-15 11:52:57 -080094 }
95
Michael Kolbda580632012-04-16 13:30:28 -070096 private void checkHideActionBar() {
Michael Kolb376b5412010-12-15 11:52:57 -080097 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -080098 mHandler.post(new Runnable() {
99 public void run() {
100 mActionBar.hide();
101 }
102 });
Michael Kolb376b5412010-12-15 11:52:57 -0800103 }
Michael Kolb66706532010-12-12 19:50:22 -0800104 }
105
106 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000107 public void onResume() {
108 super.onResume();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000109 mNavBar.clearCompletions();
Michael Kolbda580632012-04-16 13:30:28 -0700110 checkHideActionBar();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000111 }
112
113 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800114 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800115 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800116 }
117
Michael Kolb66706532010-12-12 19:50:22 -0800118 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700119 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800120 if (web != null) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000121 WebViewClassic.fromWebView(web).stopScroll();
Michael Kolb66706532010-12-12 19:50:22 -0800122 }
123 }
124
Michael Kolb0d0245f2011-12-05 16:36:05 -0800125 @Override
126 public boolean onPrepareOptionsMenu(Menu menu) {
127 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
128 if (bm != null) {
129 bm.setVisible(false);
130 }
131 return true;
132 }
133
134
Michael Kolb66706532010-12-12 19:50:22 -0800135 // WebView callbacks
136
137 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800138 public void addTab(Tab tab) {
139 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800140 }
141
142 protected void onAddTabCompleted(Tab tab) {
Michael Kolbda580632012-04-16 13:30:28 -0700143 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800144 }
145
146 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800147 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700148 mTitleBar.cancelTitleBarAnimation(true);
149 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700150 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700151 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800152 // TabControl.setCurrentTab has been called before this,
153 // so the tab is guaranteed to have a webview
154 if (view == null) {
155 Log.e(LOGTAG, "active tab with no webview detected");
156 return;
157 }
Michael Kolb66706532010-12-12 19:50:22 -0800158 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800159 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700160 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800161 } else {
162 revertVoiceTitleBar(tab);
163 }
Michael Kolb376b5412010-12-15 11:52:57 -0800164 updateLockIconToLatest(tab);
John Reck5d43ce82011-06-21 17:16:51 -0700165 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800166 }
167
168 @Override
169 public void updateTabs(List<Tab> tabs) {
170 mTabBar.updateTabs(tabs);
Michael Kolbda580632012-04-16 13:30:28 -0700171 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800172 }
173
174 @Override
175 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700176 mTitleBar.cancelTitleBarAnimation(true);
177 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800178 super.removeTab(tab);
179 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700180 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800181 }
182
183 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolbda580632012-04-16 13:30:28 -0700184 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800185 }
186
Michael Kolb376b5412010-12-15 11:52:57 -0800187 int getContentWidth() {
188 if (mContentView != null) {
189 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800190 }
191 return 0;
192 }
193
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800194 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -0700195 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700196 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700197 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800198 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700199 super.editUrl(clearInput, forceIME);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 }
201
Michael Kolb66706532010-12-12 19:50:22 -0800202 // action mode callbacks
203
204 @Override
205 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800206 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700207 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800208 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800209 }
210 }
211
212 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800213 public void onActionModeFinished(boolean inLoad) {
Michael Kolbda580632012-04-16 13:30:28 -0700214 checkHideActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800215 if (inLoad) {
216 // the titlebar was removed when the CAB was shown
217 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800218 if (mUseQuickControls) {
219 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800220 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800221 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800222 }
223 }
224
225 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800226 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700227 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800228 }
229
230 @Override
John Reck30c714c2010-12-16 17:30:34 -0800231 public void setUrlTitle(Tab tab) {
232 super.setUrlTitle(tab);
233 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800234 }
235
236 // Set the favicon in the title bar.
237 @Override
John Reck30c714c2010-12-16 17:30:34 -0800238 public void setFavicon(Tab tab) {
239 super.setFavicon(tab);
240 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242
Michael Kolbcfa3af52010-12-14 10:36:11 -0800243 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800244 public void onHideCustomView() {
245 super.onHideCustomView();
Michael Kolbda580632012-04-16 13:30:28 -0700246 checkHideActionBar();
John Reckd73c5a22010-12-22 10:22:50 -0800247 }
Michael Kolba4183062011-01-16 10:43:21 -0800248
249 @Override
250 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800251 if (mActiveTab != null) {
252 WebView web = mActiveTab.getWebView();
253 if (event.getAction() == KeyEvent.ACTION_DOWN) {
254 switch (code) {
255 case KeyEvent.KEYCODE_TAB:
256 case KeyEvent.KEYCODE_DPAD_UP:
257 case KeyEvent.KEYCODE_DPAD_LEFT:
258 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700259 editUrl(false, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800260 return true;
261 }
262 }
263 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
264 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700265 editUrl(true, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800266 return mContentView.dispatchKeyEvent(event);
267 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800268 }
Michael Kolba4183062011-01-16 10:43:21 -0800269 }
270 return false;
271 }
272
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800273 private boolean isTypingKey(KeyEvent evt) {
274 return evt.getUnicodeChar() > 0;
275 }
276
277 TabBar getTabBar() {
278 return mTabBar;
279 }
280
John Reck1cf4b792011-07-26 10:22:22 -0700281 @Override
282 public boolean shouldCaptureThumbnails() {
283 return mUseQuickControls;
284 }
285
John Reck034637c2011-08-11 11:34:44 -0700286 private Drawable getFaviconBackground() {
287 if (mFaviconBackground == null) {
288 mFaviconBackground = new PaintDrawable();
289 Resources res = mActivity.getResources();
290 mFaviconBackground.getPaint().setColor(
291 res.getColor(R.color.tabFaviconBackground));
292 mFaviconBackground.setCornerRadius(
293 res.getDimension(R.dimen.tab_favicon_corner_radius));
294 }
295 return mFaviconBackground;
296 }
297
298 @Override
299 public Drawable getFaviconDrawable(Bitmap icon) {
300 Drawable[] array = new Drawable[2];
301 array[0] = getFaviconBackground();
302 if (icon == null) {
303 array[1] = mGenericFavicon;
304 } else {
305 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
306 }
307 LayerDrawable d = new LayerDrawable(array);
308 d.setLayerInset(1, 2, 2, 2, 2);
309 return d;
310 }
311
Michael Kolb66706532010-12-12 19:50:22 -0800312}