blob: 196997862ce2c6b1768dc6ae554b7a7eea03ef1a [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
John Reck30c714c2010-12-16 17:30:34 -0800138 public void onProgressChanged(Tab tab) {
139 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800140 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800141 mTitleBar.setProgress(progress);
Michael Kolb1c3a6d22011-12-15 15:28:49 -0800142 if (mUseQuickControls) {
143 if (progress == 100) {
144 mTitleBar.setShowProgressOnly(false);
145 } else if (!mTitleBar.isEditingUrl()) {
146 mTitleBar.setShowProgressOnly(true);
147 }
148 }
Michael Kolb66706532010-12-12 19:50:22 -0800149 }
150 }
151
152 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800153 public void addTab(Tab tab) {
154 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800155 }
156
157 protected void onAddTabCompleted(Tab tab) {
Michael Kolbda580632012-04-16 13:30:28 -0700158 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800159 }
160
161 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800162 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700163 mTitleBar.cancelTitleBarAnimation(true);
164 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700165 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700166 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800167 // TabControl.setCurrentTab has been called before this,
168 // so the tab is guaranteed to have a webview
169 if (view == null) {
170 Log.e(LOGTAG, "active tab with no webview detected");
171 return;
172 }
Michael Kolb66706532010-12-12 19:50:22 -0800173 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800174 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700175 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800176 } else {
177 revertVoiceTitleBar(tab);
178 }
Michael Kolb376b5412010-12-15 11:52:57 -0800179 updateLockIconToLatest(tab);
John Reck5d43ce82011-06-21 17:16:51 -0700180 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800181 }
182
183 @Override
184 public void updateTabs(List<Tab> tabs) {
185 mTabBar.updateTabs(tabs);
Michael Kolbda580632012-04-16 13:30:28 -0700186 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800187 }
188
189 @Override
190 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700191 mTitleBar.cancelTitleBarAnimation(true);
192 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800193 super.removeTab(tab);
194 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700195 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800196 }
197
198 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolbda580632012-04-16 13:30:28 -0700199 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800200 }
201
Michael Kolb376b5412010-12-15 11:52:57 -0800202 int getContentWidth() {
203 if (mContentView != null) {
204 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800205 }
206 return 0;
207 }
208
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800209 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -0700210 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700211 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700212 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800213 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700214 super.editUrl(clearInput, forceIME);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800215 }
216
Michael Kolb7cdc4902011-02-03 17:54:40 -0800217 @Override
218 protected void showTitleBar() {
219 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700220 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800221 }
Michael Kolb376b5412010-12-15 11:52:57 -0800222 }
223
Michael Kolb66706532010-12-12 19:50:22 -0800224 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800225 protected void hideTitleBar() {
226 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700227 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800228 }
229 }
230
Michael Kolb66706532010-12-12 19:50:22 -0800231 // action mode callbacks
232
233 @Override
234 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700236 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800237 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800238 }
239 }
240
241 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800242 public void onActionModeFinished(boolean inLoad) {
Michael Kolbda580632012-04-16 13:30:28 -0700243 checkHideActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800244 if (inLoad) {
245 // the titlebar was removed when the CAB was shown
246 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800247 if (mUseQuickControls) {
248 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800249 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800250 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800251 }
252 }
253
254 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800255 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700256 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800257 }
258
259 @Override
John Reck30c714c2010-12-16 17:30:34 -0800260 public void setUrlTitle(Tab tab) {
261 super.setUrlTitle(tab);
262 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800263 }
264
265 // Set the favicon in the title bar.
266 @Override
John Reck30c714c2010-12-16 17:30:34 -0800267 public void setFavicon(Tab tab) {
268 super.setFavicon(tab);
269 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800270 }
271
Michael Kolbcfa3af52010-12-14 10:36:11 -0800272 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800273 public void onHideCustomView() {
274 super.onHideCustomView();
Michael Kolbda580632012-04-16 13:30:28 -0700275 checkHideActionBar();
John Reckd73c5a22010-12-22 10:22:50 -0800276 }
Michael Kolba4183062011-01-16 10:43:21 -0800277
278 @Override
279 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800280 if (mActiveTab != null) {
281 WebView web = mActiveTab.getWebView();
282 if (event.getAction() == KeyEvent.ACTION_DOWN) {
283 switch (code) {
284 case KeyEvent.KEYCODE_TAB:
285 case KeyEvent.KEYCODE_DPAD_UP:
286 case KeyEvent.KEYCODE_DPAD_LEFT:
287 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700288 editUrl(false, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800289 return true;
290 }
291 }
292 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
293 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700294 editUrl(true, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800295 return mContentView.dispatchKeyEvent(event);
296 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800297 }
Michael Kolba4183062011-01-16 10:43:21 -0800298 }
299 return false;
300 }
301
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800302 private boolean isTypingKey(KeyEvent evt) {
303 return evt.getUnicodeChar() > 0;
304 }
305
306 TabBar getTabBar() {
307 return mTabBar;
308 }
309
John Reck1cf4b792011-07-26 10:22:22 -0700310 @Override
311 public boolean shouldCaptureThumbnails() {
312 return mUseQuickControls;
313 }
314
John Reck034637c2011-08-11 11:34:44 -0700315 private Drawable getFaviconBackground() {
316 if (mFaviconBackground == null) {
317 mFaviconBackground = new PaintDrawable();
318 Resources res = mActivity.getResources();
319 mFaviconBackground.getPaint().setColor(
320 res.getColor(R.color.tabFaviconBackground));
321 mFaviconBackground.setCornerRadius(
322 res.getDimension(R.dimen.tab_favicon_corner_radius));
323 }
324 return mFaviconBackground;
325 }
326
327 @Override
328 public Drawable getFaviconDrawable(Bitmap icon) {
329 Drawable[] array = new Drawable[2];
330 array[0] = getFaviconBackground();
331 if (icon == null) {
332 array[1] = mGenericFavicon;
333 } else {
334 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
335 }
336 LayerDrawable d = new LayerDrawable(array);
337 d.setLayerInset(1, 2, 2, 2, 2);
338 return d;
339 }
340
Michael Kolb66706532010-12-12 19:50:22 -0800341}