blob: 6e37c1533ee74bb90a64a9e9673d4bffafa1ab76 [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);
196 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700197 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800198 }
199
200 @Override
201 public void updateTabs(List<Tab> tabs) {
202 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800203 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800204 }
205
206 @Override
207 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700208 mTitleBar.cancelTitleBarAnimation(true);
209 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800210 super.removeTab(tab);
211 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700212 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800213 }
214
215 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800216 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800217 }
218
Michael Kolb376b5412010-12-15 11:52:57 -0800219 int getContentWidth() {
220 if (mContentView != null) {
221 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800222 }
223 return 0;
224 }
225
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800226 @Override
227 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700228 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700229 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800230 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700231 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800232 }
233
234 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700235 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800236 }
237
238 @Override
239 protected void showTitleBar() {
240 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700241 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800242 }
Michael Kolb376b5412010-12-15 11:52:57 -0800243 }
244
Michael Kolb66706532010-12-12 19:50:22 -0800245 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800246 protected void hideTitleBar() {
247 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700248 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800249 }
250 }
251
Michael Kolb66706532010-12-12 19:50:22 -0800252 // action mode callbacks
253
254 @Override
255 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800256 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700257 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800258 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800259 }
260 }
261
262 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800263 public void onActionModeFinished(boolean inLoad) {
264 checkTabCount();
265 if (inLoad) {
266 // the titlebar was removed when the CAB was shown
267 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800268 if (mUseQuickControls) {
269 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800270 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800271 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800272 }
273 }
274
275 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800276 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700277 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800278 }
279
280 @Override
John Reck30c714c2010-12-16 17:30:34 -0800281 public void setUrlTitle(Tab tab) {
282 super.setUrlTitle(tab);
283 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800284 }
285
286 // Set the favicon in the title bar.
287 @Override
John Reck30c714c2010-12-16 17:30:34 -0800288 public void setFavicon(Tab tab) {
289 super.setFavicon(tab);
290 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800291 }
292
Michael Kolbcfa3af52010-12-14 10:36:11 -0800293 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800294 public void onHideCustomView() {
295 super.onHideCustomView();
296 if (mUseQuickControls) {
297 checkTabCount();
John Reckd73c5a22010-12-22 10:22:50 -0800298 }
299 }
Michael Kolba4183062011-01-16 10:43:21 -0800300
301 @Override
302 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800303 if (mActiveTab != null) {
304 WebView web = mActiveTab.getWebView();
305 if (event.getAction() == KeyEvent.ACTION_DOWN) {
306 switch (code) {
307 case KeyEvent.KEYCODE_TAB:
308 case KeyEvent.KEYCODE_DPAD_UP:
309 case KeyEvent.KEYCODE_DPAD_LEFT:
310 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
311 editUrl(false);
312 return true;
313 }
314 }
315 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
316 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
317 editUrl(true);
318 return mContentView.dispatchKeyEvent(event);
319 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800320 }
Michael Kolba4183062011-01-16 10:43:21 -0800321 }
322 return false;
323 }
324
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800325 private boolean isTypingKey(KeyEvent evt) {
326 return evt.getUnicodeChar() > 0;
327 }
328
329 TabBar getTabBar() {
330 return mTabBar;
331 }
332
John Reck1cf4b792011-07-26 10:22:22 -0700333 @Override
334 public boolean shouldCaptureThumbnails() {
335 return mUseQuickControls;
336 }
337
John Reck034637c2011-08-11 11:34:44 -0700338 private Drawable getFaviconBackground() {
339 if (mFaviconBackground == null) {
340 mFaviconBackground = new PaintDrawable();
341 Resources res = mActivity.getResources();
342 mFaviconBackground.getPaint().setColor(
343 res.getColor(R.color.tabFaviconBackground));
344 mFaviconBackground.setCornerRadius(
345 res.getDimension(R.dimen.tab_favicon_corner_radius));
346 }
347 return mFaviconBackground;
348 }
349
350 @Override
351 public Drawable getFaviconDrawable(Bitmap icon) {
352 Drawable[] array = new Drawable[2];
353 array[0] = getFaviconBackground();
354 if (icon == null) {
355 array[1] = mGenericFavicon;
356 } else {
357 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
358 }
359 LayerDrawable d = new LayerDrawable(array);
360 d.setLayerInset(1, 2, 2, 2, 2);
361 return d;
362 }
363
Michael Kolb66706532010-12-12 19:50:22 -0800364}