blob: d7f15597199517ffaf727671714c99a2a149ab90 [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 Kolb7cdc4902011-02-03 17:54:40 -0800202 @Override
203 protected void showTitleBar() {
204 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700205 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800206 }
Michael Kolb376b5412010-12-15 11:52:57 -0800207 }
208
Michael Kolb66706532010-12-12 19:50:22 -0800209 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800210 protected void hideTitleBar() {
211 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700212 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800213 }
214 }
215
Michael Kolb66706532010-12-12 19:50:22 -0800216 // action mode callbacks
217
218 @Override
219 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800220 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700221 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800222 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800223 }
224 }
225
226 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800227 public void onActionModeFinished(boolean inLoad) {
Michael Kolbda580632012-04-16 13:30:28 -0700228 checkHideActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800229 if (inLoad) {
230 // the titlebar was removed when the CAB was shown
231 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800232 if (mUseQuickControls) {
233 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800234 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800236 }
237 }
238
239 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800240 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700241 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800242 }
243
244 @Override
John Reck30c714c2010-12-16 17:30:34 -0800245 public void setUrlTitle(Tab tab) {
246 super.setUrlTitle(tab);
247 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800248 }
249
250 // Set the favicon in the title bar.
251 @Override
John Reck30c714c2010-12-16 17:30:34 -0800252 public void setFavicon(Tab tab) {
253 super.setFavicon(tab);
254 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800255 }
256
Michael Kolbcfa3af52010-12-14 10:36:11 -0800257 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800258 public void onHideCustomView() {
259 super.onHideCustomView();
Michael Kolbda580632012-04-16 13:30:28 -0700260 checkHideActionBar();
John Reckd73c5a22010-12-22 10:22:50 -0800261 }
Michael Kolba4183062011-01-16 10:43:21 -0800262
263 @Override
264 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800265 if (mActiveTab != null) {
266 WebView web = mActiveTab.getWebView();
267 if (event.getAction() == KeyEvent.ACTION_DOWN) {
268 switch (code) {
269 case KeyEvent.KEYCODE_TAB:
270 case KeyEvent.KEYCODE_DPAD_UP:
271 case KeyEvent.KEYCODE_DPAD_LEFT:
272 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700273 editUrl(false, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800274 return true;
275 }
276 }
277 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
278 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700279 editUrl(true, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800280 return mContentView.dispatchKeyEvent(event);
281 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800282 }
Michael Kolba4183062011-01-16 10:43:21 -0800283 }
284 return false;
285 }
286
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800287 private boolean isTypingKey(KeyEvent evt) {
288 return evt.getUnicodeChar() > 0;
289 }
290
291 TabBar getTabBar() {
292 return mTabBar;
293 }
294
John Reck1cf4b792011-07-26 10:22:22 -0700295 @Override
296 public boolean shouldCaptureThumbnails() {
297 return mUseQuickControls;
298 }
299
John Reck034637c2011-08-11 11:34:44 -0700300 private Drawable getFaviconBackground() {
301 if (mFaviconBackground == null) {
302 mFaviconBackground = new PaintDrawable();
303 Resources res = mActivity.getResources();
304 mFaviconBackground.getPaint().setColor(
305 res.getColor(R.color.tabFaviconBackground));
306 mFaviconBackground.setCornerRadius(
307 res.getDimension(R.dimen.tab_favicon_corner_radius));
308 }
309 return mFaviconBackground;
310 }
311
312 @Override
313 public Drawable getFaviconDrawable(Bitmap icon) {
314 Drawable[] array = new Drawable[2];
315 array[0] = getFaviconBackground();
316 if (icon == null) {
317 array[1] = mGenericFavicon;
318 } else {
319 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
320 }
321 LayerDrawable d = new LayerDrawable(array);
322 d.setLayerInset(1, 2, 2, 2, 2);
323 return d;
324 }
325
Michael Kolb66706532010-12-12 19:50:22 -0800326}