blob: 46149aace6815f723bc41a9032aa89a39f2a2871 [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 Kolb376b5412010-12-15 11:52:57 -080031import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080032import android.view.KeyEvent;
Michael Kolb0d0245f2011-12-05 16:36:05 -080033import android.view.Menu;
34import android.view.MenuItem;
John Reckd73c5a22010-12-22 10:22:50 -080035import android.view.View;
Michael Kolb52051eb2011-08-02 13:06:43 -070036import android.view.ViewGroup;
John Reckd73c5a22010-12-22 10:22:50 -080037import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080038import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000039import android.webkit.WebViewClassic;
Michael Kolb66706532010-12-12 19:50:22 -080040
41import java.util.List;
42
43/**
44 * Ui for xlarge screen sizes
45 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070046public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080047
48 private static final String LOGTAG = "XLargeUi";
49
John Reck034637c2011-08-11 11:34:44 -070050 private PaintDrawable mFaviconBackground;
51
Michael Kolb376b5412010-12-15 11:52:57 -080052 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080053 private TabBar mTabBar;
54
John Reck0f602f32011-07-07 15:38:43 -070055 private NavigationBarTablet mNavBar;
Michael Kolb66706532010-12-12 19:50:22 -080056
Michael Kolb0241e752011-07-07 14:58:50 -070057 private PieControlXLarge mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080058 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080059
Michael Kolb66706532010-12-12 19:50:22 -080060 /**
61 * @param browser
62 * @param controller
63 */
64 public XLargeUi(Activity browser, UiController controller) {
65 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080066 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070067 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080068 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080069 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080070 setupActionBar();
71 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
72 }
73
74 private void setupActionBar() {
75 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080076 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
77 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080078 }
79
John Reck2bc80422011-06-30 15:11:49 -070080 public void showComboView(ComboViews startWith, Bundle extras) {
81 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080082 if (mUseQuickControls) {
83 mActionBar.show();
84 }
85 }
86
87 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070088 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolb376b5412010-12-15 11:52:57 -080089 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080090 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080091 if (useQuickControls) {
92 checkTabCount();
Michael Kolb0241e752011-07-07 14:58:50 -070093 mPieControl = new PieControlXLarge(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080094 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070095 WebView web = getWebView();
96 if (web != null) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000097 WebViewClassic.fromWebView(web).setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -070098
Michael Kolb376b5412010-12-15 11:52:57 -080099 }
100 } else {
101 mActivity.getActionBar().show();
102 if (mPieControl != null) {
103 mPieControl.removeFromContainer(mContentView);
104 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700105 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800106 if (web != null) {
Michael Kolb52051eb2011-08-02 13:06:43 -0700107 if (mTitleBar.getParent() != null) {
108 ViewGroup p = (ViewGroup) mTitleBar.getParent();
109 p.removeView(mTitleBar);
110 }
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000111 WebViewClassic.fromWebView(web).setEmbeddedTitleBar(mTitleBar);
Michael Kolb376b5412010-12-15 11:52:57 -0800112 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700113 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800114 }
115 mTabBar.setUseQuickControls(mUseQuickControls);
John Reck1cf4b792011-07-26 10:22:22 -0700116 // We need to update the tabs with this change
117 for (Tab t : mTabControl.getTabs()) {
118 t.updateShouldCaptureThumbnails();
119 }
John Reck718a24d2011-08-12 11:08:30 -0700120 updateUrlBarAutoShowManagerTarget();
Michael Kolb376b5412010-12-15 11:52:57 -0800121 }
122
123 private void checkTabCount() {
124 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800125 mHandler.post(new Runnable() {
126 public void run() {
127 mActionBar.hide();
128 }
129 });
Michael Kolb376b5412010-12-15 11:52:57 -0800130 }
Michael Kolb66706532010-12-12 19:50:22 -0800131 }
132
133 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000134 public void onResume() {
135 super.onResume();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000136 mNavBar.clearCompletions();
Michael Kolbd2e35932012-01-19 09:29:16 -0800137 checkTabCount();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000138 }
139
140 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800141 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800142 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800143 }
144
Michael Kolb66706532010-12-12 19:50:22 -0800145 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700146 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800147 if (web != null) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000148 WebViewClassic.fromWebView(web).stopScroll();
Michael Kolb66706532010-12-12 19:50:22 -0800149 }
150 }
151
Michael Kolb0d0245f2011-12-05 16:36:05 -0800152 @Override
153 public boolean onPrepareOptionsMenu(Menu menu) {
154 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
155 if (bm != null) {
156 bm.setVisible(false);
157 }
158 return true;
159 }
160
161
Michael Kolb66706532010-12-12 19:50:22 -0800162 // WebView callbacks
163
164 @Override
John Reck30c714c2010-12-16 17:30:34 -0800165 public void onProgressChanged(Tab tab) {
166 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800167 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800168 mTitleBar.setProgress(progress);
Michael Kolb1c3a6d22011-12-15 15:28:49 -0800169 if (mUseQuickControls) {
170 if (progress == 100) {
171 mTitleBar.setShowProgressOnly(false);
172 } else if (!mTitleBar.isEditingUrl()) {
173 mTitleBar.setShowProgressOnly(true);
174 }
175 }
Michael Kolb66706532010-12-12 19:50:22 -0800176 }
177 }
178
179 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800180 public void addTab(Tab tab) {
181 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800182 }
183
184 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800185 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800186 }
187
188 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800189 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700190 mTitleBar.cancelTitleBarAnimation(true);
191 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700192 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700193 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800194 // TabControl.setCurrentTab has been called before this,
195 // so the tab is guaranteed to have a webview
196 if (view == null) {
197 Log.e(LOGTAG, "active tab with no webview detected");
198 return;
199 }
200 // Request focus on the top window.
201 if (mUseQuickControls) {
202 mPieControl.forceToTop(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -0800203 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800204 // check if title bar is already attached by animation
John Reck0f602f32011-07-07 15:38:43 -0700205 if (mTitleBar.getParent() == null) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000206 WebViewClassic.fromWebView(view).setEmbeddedTitleBar(mTitleBar);
Michael Kolb377ea312011-02-17 14:36:56 -0800207 }
Michael Kolb376b5412010-12-15 11:52:57 -0800208 }
Michael Kolb66706532010-12-12 19:50:22 -0800209 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800210 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700211 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800212 } else {
213 revertVoiceTitleBar(tab);
214 }
Michael Kolb376b5412010-12-15 11:52:57 -0800215 updateLockIconToLatest(tab);
216 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700217 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800218 }
219
220 @Override
221 public void updateTabs(List<Tab> tabs) {
222 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800223 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800224 }
225
226 @Override
227 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700228 mTitleBar.cancelTitleBarAnimation(true);
229 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800230 super.removeTab(tab);
231 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700232 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800233 }
234
235 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800236 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800237 }
238
Michael Kolb376b5412010-12-15 11:52:57 -0800239 int getContentWidth() {
240 if (mContentView != null) {
241 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800242 }
243 return 0;
244 }
245
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800246 @Override
247 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700248 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700249 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800250 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700251 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800252 }
253
254 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700255 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800256 }
257
258 @Override
259 protected void showTitleBar() {
260 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700261 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800262 }
Michael Kolb376b5412010-12-15 11:52:57 -0800263 }
264
Michael Kolb66706532010-12-12 19:50:22 -0800265 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800266 protected void hideTitleBar() {
267 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700268 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800269 }
270 }
271
272 @Override
Michael Kolb2a56eca2011-02-25 09:45:06 -0800273 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700274 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800275 super.setTitleGravity(gravity);
276 }
277 }
278
Michael Kolb66706532010-12-12 19:50:22 -0800279 // action mode callbacks
280
281 @Override
282 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800283 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700284 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800285 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800286 }
287 }
288
289 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800290 public void onActionModeFinished(boolean inLoad) {
291 checkTabCount();
292 if (inLoad) {
293 // the titlebar was removed when the CAB was shown
294 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800295 if (mUseQuickControls) {
296 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800297 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800298 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800299 }
300 }
301
302 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800303 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700304 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800305 }
306
307 @Override
John Reck30c714c2010-12-16 17:30:34 -0800308 public void setUrlTitle(Tab tab) {
309 super.setUrlTitle(tab);
310 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800311 }
312
313 // Set the favicon in the title bar.
314 @Override
John Reck30c714c2010-12-16 17:30:34 -0800315 public void setFavicon(Tab tab) {
316 super.setFavicon(tab);
317 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800318 }
319
Michael Kolbcfa3af52010-12-14 10:36:11 -0800320 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800321 public void onHideCustomView() {
322 super.onHideCustomView();
323 if (mUseQuickControls) {
324 checkTabCount();
John Reckd73c5a22010-12-22 10:22:50 -0800325 }
326 }
Michael Kolba4183062011-01-16 10:43:21 -0800327
328 @Override
329 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800330 if (mActiveTab != null) {
331 WebView web = mActiveTab.getWebView();
332 if (event.getAction() == KeyEvent.ACTION_DOWN) {
333 switch (code) {
334 case KeyEvent.KEYCODE_TAB:
335 case KeyEvent.KEYCODE_DPAD_UP:
336 case KeyEvent.KEYCODE_DPAD_LEFT:
337 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
338 editUrl(false);
339 return true;
340 }
341 }
342 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
343 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
344 editUrl(true);
345 return mContentView.dispatchKeyEvent(event);
346 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800347 }
Michael Kolba4183062011-01-16 10:43:21 -0800348 }
349 return false;
350 }
351
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800352 private boolean isTypingKey(KeyEvent evt) {
353 return evt.getUnicodeChar() > 0;
354 }
355
356 TabBar getTabBar() {
357 return mTabBar;
358 }
359
John Reck1cf4b792011-07-26 10:22:22 -0700360 @Override
361 public boolean shouldCaptureThumbnails() {
362 return mUseQuickControls;
363 }
364
John Reck034637c2011-08-11 11:34:44 -0700365 private Drawable getFaviconBackground() {
366 if (mFaviconBackground == null) {
367 mFaviconBackground = new PaintDrawable();
368 Resources res = mActivity.getResources();
369 mFaviconBackground.getPaint().setColor(
370 res.getColor(R.color.tabFaviconBackground));
371 mFaviconBackground.setCornerRadius(
372 res.getDimension(R.dimen.tab_favicon_corner_radius));
373 }
374 return mFaviconBackground;
375 }
376
377 @Override
378 public Drawable getFaviconDrawable(Bitmap icon) {
379 Drawable[] array = new Drawable[2];
380 array[0] = getFaviconBackground();
381 if (icon == null) {
382 array[1] = mGenericFavicon;
383 } else {
384 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
385 }
386 LayerDrawable d = new LayerDrawable(array);
387 d.setLayerInset(1, 2, 2, 2, 2);
388 return d;
389 }
390
Michael Kolb66706532010-12-12 19:50:22 -0800391}