blob: c828df42080c1602f101b3554dd4be1ac5875c8f [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;
39
40import java.util.List;
41
42/**
43 * Ui for xlarge screen sizes
44 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070045public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080046
47 private static final String LOGTAG = "XLargeUi";
48
John Reck034637c2011-08-11 11:34:44 -070049 private PaintDrawable mFaviconBackground;
50
Michael Kolb376b5412010-12-15 11:52:57 -080051 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080052 private TabBar mTabBar;
53
John Reck0f602f32011-07-07 15:38:43 -070054 private NavigationBarTablet mNavBar;
Michael Kolb66706532010-12-12 19:50:22 -080055
Michael Kolb0241e752011-07-07 14:58:50 -070056 private PieControlXLarge mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080057 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080058
Michael Kolb66706532010-12-12 19:50:22 -080059 /**
60 * @param browser
61 * @param controller
62 */
63 public XLargeUi(Activity browser, UiController controller) {
64 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080065 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070066 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080067 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080068 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080069 setupActionBar();
70 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
71 }
72
73 private void setupActionBar() {
74 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080075 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
76 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080077 }
78
John Reck2bc80422011-06-30 15:11:49 -070079 public void showComboView(ComboViews startWith, Bundle extras) {
80 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080081 if (mUseQuickControls) {
82 mActionBar.show();
83 }
84 }
85
86 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070087 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolb376b5412010-12-15 11:52:57 -080088 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080089 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080090 if (useQuickControls) {
91 checkTabCount();
Michael Kolb0241e752011-07-07 14:58:50 -070092 mPieControl = new PieControlXLarge(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080093 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070094 WebView web = getWebView();
95 if (web != null) {
96 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -070097
Michael Kolb376b5412010-12-15 11:52:57 -080098 }
99 } else {
100 mActivity.getActionBar().show();
101 if (mPieControl != null) {
102 mPieControl.removeFromContainer(mContentView);
103 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700104 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800105 if (web != null) {
Michael Kolb52051eb2011-08-02 13:06:43 -0700106 if (mTitleBar.getParent() != null) {
107 ViewGroup p = (ViewGroup) mTitleBar.getParent();
108 p.removeView(mTitleBar);
109 }
Michael Kolb376b5412010-12-15 11:52:57 -0800110 web.setEmbeddedTitleBar(mTitleBar);
111 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700112 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800113 }
114 mTabBar.setUseQuickControls(mUseQuickControls);
John Reck1cf4b792011-07-26 10:22:22 -0700115 // We need to update the tabs with this change
116 for (Tab t : mTabControl.getTabs()) {
117 t.updateShouldCaptureThumbnails();
118 }
John Reck718a24d2011-08-12 11:08:30 -0700119 updateUrlBarAutoShowManagerTarget();
Michael Kolb376b5412010-12-15 11:52:57 -0800120 }
121
122 private void checkTabCount() {
123 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800124 mHandler.post(new Runnable() {
125 public void run() {
126 mActionBar.hide();
127 }
128 });
Michael Kolb376b5412010-12-15 11:52:57 -0800129 }
Michael Kolb66706532010-12-12 19:50:22 -0800130 }
131
132 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000133 public void onResume() {
134 super.onResume();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000135 mNavBar.clearCompletions();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000136 }
137
138 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800139 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800140 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800141 }
142
Michael Kolb66706532010-12-12 19:50:22 -0800143 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700144 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800145 if (web != null) {
146 web.stopScroll();
147 }
148 }
149
Michael Kolb0d0245f2011-12-05 16:36:05 -0800150 @Override
151 public boolean onPrepareOptionsMenu(Menu menu) {
152 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
153 if (bm != null) {
154 bm.setVisible(false);
155 }
156 return true;
157 }
158
159
Michael Kolb66706532010-12-12 19:50:22 -0800160 // WebView callbacks
161
162 @Override
John Reck30c714c2010-12-16 17:30:34 -0800163 public void onProgressChanged(Tab tab) {
164 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800165 mTabBar.onProgress(tab, progress);
166 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800167 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800168 }
169 }
170
171 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800172 public void addTab(Tab tab) {
173 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800174 }
175
176 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800177 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800178 }
179
180 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800181 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700182 mTitleBar.cancelTitleBarAnimation(true);
183 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700184 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700185 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800186 // TabControl.setCurrentTab has been called before this,
187 // so the tab is guaranteed to have a webview
188 if (view == null) {
189 Log.e(LOGTAG, "active tab with no webview detected");
190 return;
191 }
192 // Request focus on the top window.
193 if (mUseQuickControls) {
194 mPieControl.forceToTop(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -0800195 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800196 // check if title bar is already attached by animation
John Reck0f602f32011-07-07 15:38:43 -0700197 if (mTitleBar.getParent() == null) {
Michael Kolb377ea312011-02-17 14:36:56 -0800198 view.setEmbeddedTitleBar(mTitleBar);
199 }
Michael Kolb376b5412010-12-15 11:52:57 -0800200 }
Michael Kolb66706532010-12-12 19:50:22 -0800201 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800202 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700203 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800204 } else {
205 revertVoiceTitleBar(tab);
206 }
Michael Kolb376b5412010-12-15 11:52:57 -0800207 updateLockIconToLatest(tab);
208 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700209 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800210 }
211
212 @Override
213 public void updateTabs(List<Tab> tabs) {
214 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800215 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800216 }
217
218 @Override
219 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700220 mTitleBar.cancelTitleBarAnimation(true);
221 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800222 super.removeTab(tab);
223 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700224 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800225 }
226
227 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800228 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800229 }
230
Michael Kolb376b5412010-12-15 11:52:57 -0800231 int getContentWidth() {
232 if (mContentView != null) {
233 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800234 }
235 return 0;
236 }
237
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800238 @Override
239 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700240 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700241 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800242 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700243 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800244 }
245
246 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700247 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800248 }
249
250 @Override
251 protected void showTitleBar() {
252 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700253 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800254 }
Michael Kolb376b5412010-12-15 11:52:57 -0800255 }
256
Michael Kolb66706532010-12-12 19:50:22 -0800257 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800258 protected void hideTitleBar() {
259 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700260 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800261 }
262 }
263
264 @Override
Michael Kolb2a56eca2011-02-25 09:45:06 -0800265 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700266 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800267 super.setTitleGravity(gravity);
268 }
269 }
270
Michael Kolb66706532010-12-12 19:50:22 -0800271 // action mode callbacks
272
273 @Override
274 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800275 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700276 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800277 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800278 }
279 }
280
281 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800282 public void onActionModeFinished(boolean inLoad) {
283 checkTabCount();
284 if (inLoad) {
285 // the titlebar was removed when the CAB was shown
286 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800287 if (mUseQuickControls) {
288 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800289 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800290 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800291 }
292 }
293
294 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800295 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700296 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800297 }
298
299 @Override
John Reck30c714c2010-12-16 17:30:34 -0800300 public void setUrlTitle(Tab tab) {
301 super.setUrlTitle(tab);
302 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800303 }
304
305 // Set the favicon in the title bar.
306 @Override
John Reck30c714c2010-12-16 17:30:34 -0800307 public void setFavicon(Tab tab) {
308 super.setFavicon(tab);
309 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800310 }
311
Michael Kolbcfa3af52010-12-14 10:36:11 -0800312 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800313 public void onHideCustomView() {
314 super.onHideCustomView();
315 if (mUseQuickControls) {
316 checkTabCount();
John Reckd73c5a22010-12-22 10:22:50 -0800317 }
318 }
Michael Kolba4183062011-01-16 10:43:21 -0800319
320 @Override
321 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800322 if (mActiveTab != null) {
323 WebView web = mActiveTab.getWebView();
324 if (event.getAction() == KeyEvent.ACTION_DOWN) {
325 switch (code) {
326 case KeyEvent.KEYCODE_TAB:
327 case KeyEvent.KEYCODE_DPAD_UP:
328 case KeyEvent.KEYCODE_DPAD_LEFT:
329 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
330 editUrl(false);
331 return true;
332 }
333 }
334 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
335 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
336 editUrl(true);
337 return mContentView.dispatchKeyEvent(event);
338 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800339 }
Michael Kolba4183062011-01-16 10:43:21 -0800340 }
341 return false;
342 }
343
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800344 private boolean isTypingKey(KeyEvent evt) {
345 return evt.getUnicodeChar() > 0;
346 }
347
348 TabBar getTabBar() {
349 return mTabBar;
350 }
351
John Reck1cf4b792011-07-26 10:22:22 -0700352 @Override
353 public boolean shouldCaptureThumbnails() {
354 return mUseQuickControls;
355 }
356
John Reck034637c2011-08-11 11:34:44 -0700357 private Drawable getFaviconBackground() {
358 if (mFaviconBackground == null) {
359 mFaviconBackground = new PaintDrawable();
360 Resources res = mActivity.getResources();
361 mFaviconBackground.getPaint().setColor(
362 res.getColor(R.color.tabFaviconBackground));
363 mFaviconBackground.setCornerRadius(
364 res.getDimension(R.dimen.tab_favicon_corner_radius));
365 }
366 return mFaviconBackground;
367 }
368
369 @Override
370 public Drawable getFaviconDrawable(Bitmap icon) {
371 Drawable[] array = new Drawable[2];
372 array[0] = getFaviconBackground();
373 if (icon == null) {
374 array[1] = mGenericFavicon;
375 } else {
376 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
377 }
378 LayerDrawable d = new LayerDrawable(array);
379 d.setLayerInset(1, 2, 2, 2, 2);
380 return d;
381 }
382
Michael Kolb66706532010-12-12 19:50:22 -0800383}