blob: 17153b6bbb05380a60f86b02cd6b2fd2b0857df6 [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 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800166 mTitleBar.setProgress(progress);
Michael Kolb1c3a6d22011-12-15 15:28:49 -0800167 if (mUseQuickControls) {
168 if (progress == 100) {
169 mTitleBar.setShowProgressOnly(false);
170 } else if (!mTitleBar.isEditingUrl()) {
171 mTitleBar.setShowProgressOnly(true);
172 }
173 }
Michael Kolb66706532010-12-12 19:50:22 -0800174 }
175 }
176
177 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800178 public void addTab(Tab tab) {
179 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800180 }
181
182 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800183 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800184 }
185
186 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800187 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700188 mTitleBar.cancelTitleBarAnimation(true);
189 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700190 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700191 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800192 // TabControl.setCurrentTab has been called before this,
193 // so the tab is guaranteed to have a webview
194 if (view == null) {
195 Log.e(LOGTAG, "active tab with no webview detected");
196 return;
197 }
198 // Request focus on the top window.
199 if (mUseQuickControls) {
200 mPieControl.forceToTop(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -0800201 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800202 // check if title bar is already attached by animation
John Reck0f602f32011-07-07 15:38:43 -0700203 if (mTitleBar.getParent() == null) {
Michael Kolb377ea312011-02-17 14:36:56 -0800204 view.setEmbeddedTitleBar(mTitleBar);
205 }
Michael Kolb376b5412010-12-15 11:52:57 -0800206 }
Michael Kolb66706532010-12-12 19:50:22 -0800207 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800208 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700209 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800210 } else {
211 revertVoiceTitleBar(tab);
212 }
Michael Kolb376b5412010-12-15 11:52:57 -0800213 updateLockIconToLatest(tab);
214 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700215 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800216 }
217
218 @Override
219 public void updateTabs(List<Tab> tabs) {
220 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800221 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800222 }
223
224 @Override
225 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700226 mTitleBar.cancelTitleBarAnimation(true);
227 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800228 super.removeTab(tab);
229 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700230 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800231 }
232
233 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800234 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800235 }
236
Michael Kolb376b5412010-12-15 11:52:57 -0800237 int getContentWidth() {
238 if (mContentView != null) {
239 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800240 }
241 return 0;
242 }
243
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800244 @Override
245 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700246 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700247 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800248 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700249 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800250 }
251
252 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700253 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800254 }
255
256 @Override
257 protected void showTitleBar() {
258 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700259 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800260 }
Michael Kolb376b5412010-12-15 11:52:57 -0800261 }
262
Michael Kolb66706532010-12-12 19:50:22 -0800263 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800264 protected void hideTitleBar() {
265 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700266 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800267 }
268 }
269
270 @Override
Michael Kolb2a56eca2011-02-25 09:45:06 -0800271 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700272 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800273 super.setTitleGravity(gravity);
274 }
275 }
276
Michael Kolb66706532010-12-12 19:50:22 -0800277 // action mode callbacks
278
279 @Override
280 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800281 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700282 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800283 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800284 }
285 }
286
287 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800288 public void onActionModeFinished(boolean inLoad) {
289 checkTabCount();
290 if (inLoad) {
291 // the titlebar was removed when the CAB was shown
292 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800293 if (mUseQuickControls) {
294 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800295 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800296 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800297 }
298 }
299
300 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800301 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700302 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800303 }
304
305 @Override
John Reck30c714c2010-12-16 17:30:34 -0800306 public void setUrlTitle(Tab tab) {
307 super.setUrlTitle(tab);
308 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800309 }
310
311 // Set the favicon in the title bar.
312 @Override
John Reck30c714c2010-12-16 17:30:34 -0800313 public void setFavicon(Tab tab) {
314 super.setFavicon(tab);
315 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800316 }
317
Michael Kolbcfa3af52010-12-14 10:36:11 -0800318 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800319 public void onHideCustomView() {
320 super.onHideCustomView();
321 if (mUseQuickControls) {
322 checkTabCount();
John Reckd73c5a22010-12-22 10:22:50 -0800323 }
324 }
Michael Kolba4183062011-01-16 10:43:21 -0800325
326 @Override
327 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800328 if (mActiveTab != null) {
329 WebView web = mActiveTab.getWebView();
330 if (event.getAction() == KeyEvent.ACTION_DOWN) {
331 switch (code) {
332 case KeyEvent.KEYCODE_TAB:
333 case KeyEvent.KEYCODE_DPAD_UP:
334 case KeyEvent.KEYCODE_DPAD_LEFT:
335 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
336 editUrl(false);
337 return true;
338 }
339 }
340 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
341 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
342 editUrl(true);
343 return mContentView.dispatchKeyEvent(event);
344 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800345 }
Michael Kolba4183062011-01-16 10:43:21 -0800346 }
347 return false;
348 }
349
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800350 private boolean isTypingKey(KeyEvent evt) {
351 return evt.getUnicodeChar() > 0;
352 }
353
354 TabBar getTabBar() {
355 return mTabBar;
356 }
357
John Reck1cf4b792011-07-26 10:22:22 -0700358 @Override
359 public boolean shouldCaptureThumbnails() {
360 return mUseQuickControls;
361 }
362
John Reck034637c2011-08-11 11:34:44 -0700363 private Drawable getFaviconBackground() {
364 if (mFaviconBackground == null) {
365 mFaviconBackground = new PaintDrawable();
366 Resources res = mActivity.getResources();
367 mFaviconBackground.getPaint().setColor(
368 res.getColor(R.color.tabFaviconBackground));
369 mFaviconBackground.setCornerRadius(
370 res.getDimension(R.dimen.tab_favicon_corner_radius));
371 }
372 return mFaviconBackground;
373 }
374
375 @Override
376 public Drawable getFaviconDrawable(Bitmap icon) {
377 Drawable[] array = new Drawable[2];
378 array[0] = getFaviconBackground();
379 if (icon == null) {
380 array[1] = mGenericFavicon;
381 } else {
382 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
383 }
384 LayerDrawable d = new LayerDrawable(array);
385 d.setLayerInset(1, 2, 2, 2, 2);
386 return d;
387 }
388
Michael Kolb66706532010-12-12 19:50:22 -0800389}