blob: b372fab102c6072a23239ab49f7dd4150f347c10 [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 Kolb0241e752011-07-07 14:58:50 -070053 private PieControlXLarge 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 Kolb0241e752011-07-07 14:58:50 -070089 mPieControl = new PieControlXLarge(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
233 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700234 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 }
236
237 @Override
238 protected void showTitleBar() {
239 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700240 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800241 }
Michael Kolb376b5412010-12-15 11:52:57 -0800242 }
243
Michael Kolb66706532010-12-12 19:50:22 -0800244 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800245 protected void hideTitleBar() {
246 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700247 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800248 }
249 }
250
Michael Kolb66706532010-12-12 19:50:22 -0800251 // action mode callbacks
252
253 @Override
254 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800255 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700256 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800257 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800258 }
259 }
260
261 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800262 public void onActionModeFinished(boolean inLoad) {
263 checkTabCount();
264 if (inLoad) {
265 // the titlebar was removed when the CAB was shown
266 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800267 if (mUseQuickControls) {
268 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800269 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800270 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800271 }
272 }
273
274 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800275 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700276 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800277 }
278
279 @Override
John Reck30c714c2010-12-16 17:30:34 -0800280 public void setUrlTitle(Tab tab) {
281 super.setUrlTitle(tab);
282 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800283 }
284
285 // Set the favicon in the title bar.
286 @Override
John Reck30c714c2010-12-16 17:30:34 -0800287 public void setFavicon(Tab tab) {
288 super.setFavicon(tab);
289 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800290 }
291
Michael Kolbcfa3af52010-12-14 10:36:11 -0800292 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800293 public void onHideCustomView() {
294 super.onHideCustomView();
295 if (mUseQuickControls) {
296 checkTabCount();
John Reckd73c5a22010-12-22 10:22:50 -0800297 }
298 }
Michael Kolba4183062011-01-16 10:43:21 -0800299
300 @Override
301 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800302 if (mActiveTab != null) {
303 WebView web = mActiveTab.getWebView();
304 if (event.getAction() == KeyEvent.ACTION_DOWN) {
305 switch (code) {
306 case KeyEvent.KEYCODE_TAB:
307 case KeyEvent.KEYCODE_DPAD_UP:
308 case KeyEvent.KEYCODE_DPAD_LEFT:
309 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
310 editUrl(false);
311 return true;
312 }
313 }
314 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
315 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
316 editUrl(true);
317 return mContentView.dispatchKeyEvent(event);
318 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800319 }
Michael Kolba4183062011-01-16 10:43:21 -0800320 }
321 return false;
322 }
323
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800324 private boolean isTypingKey(KeyEvent evt) {
325 return evt.getUnicodeChar() > 0;
326 }
327
328 TabBar getTabBar() {
329 return mTabBar;
330 }
331
John Reck1cf4b792011-07-26 10:22:22 -0700332 @Override
333 public boolean shouldCaptureThumbnails() {
334 return mUseQuickControls;
335 }
336
John Reck034637c2011-08-11 11:34:44 -0700337 private Drawable getFaviconBackground() {
338 if (mFaviconBackground == null) {
339 mFaviconBackground = new PaintDrawable();
340 Resources res = mActivity.getResources();
341 mFaviconBackground.getPaint().setColor(
342 res.getColor(R.color.tabFaviconBackground));
343 mFaviconBackground.setCornerRadius(
344 res.getDimension(R.dimen.tab_favicon_corner_radius));
345 }
346 return mFaviconBackground;
347 }
348
349 @Override
350 public Drawable getFaviconDrawable(Bitmap icon) {
351 Drawable[] array = new Drawable[2];
352 array[0] = getFaviconBackground();
353 if (icon == null) {
354 array[1] = mGenericFavicon;
355 } else {
356 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
357 }
358 LayerDrawable d = new LayerDrawable(array);
359 d.setLayerInset(1, 2, 2, 2, 2);
360 return d;
361 }
362
Michael Kolb66706532010-12-12 19:50:22 -0800363}