blob: 724708be30b602099c8a91d2c6597d7c4c1aecb1 [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;
John Reckd73c5a22010-12-22 10:22:50 -080033import android.view.View;
Michael Kolb52051eb2011-08-02 13:06:43 -070034import android.view.ViewGroup;
John Reckd73c5a22010-12-22 10:22:50 -080035import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080036import android.webkit.WebView;
37
38import java.util.List;
39
40/**
41 * Ui for xlarge screen sizes
42 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070043public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080044
45 private static final String LOGTAG = "XLargeUi";
46
John Reck034637c2011-08-11 11:34:44 -070047 private PaintDrawable mFaviconBackground;
48
Michael Kolb376b5412010-12-15 11:52:57 -080049 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080050 private TabBar mTabBar;
51
John Reck0f602f32011-07-07 15:38:43 -070052 private NavigationBarTablet mNavBar;
Michael Kolb66706532010-12-12 19:50:22 -080053
Michael Kolb0241e752011-07-07 14:58:50 -070054 private PieControlXLarge mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080055 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080056
Michael Kolb66706532010-12-12 19:50:22 -080057 /**
58 * @param browser
59 * @param controller
60 */
61 public XLargeUi(Activity browser, UiController controller) {
62 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080063 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070064 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080065 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080066 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080067 setupActionBar();
68 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
69 }
70
71 private void setupActionBar() {
72 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080073 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
74 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080075 }
76
John Reck2bc80422011-06-30 15:11:49 -070077 public void showComboView(ComboViews startWith, Bundle extras) {
78 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080079 if (mUseQuickControls) {
80 mActionBar.show();
81 }
82 }
83
84 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070085 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolb376b5412010-12-15 11:52:57 -080086 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080087 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080088 if (useQuickControls) {
89 checkTabCount();
Michael Kolb0241e752011-07-07 14:58:50 -070090 mPieControl = new PieControlXLarge(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080091 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070092 WebView web = getWebView();
93 if (web != null) {
94 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -070095 // don't show url bar on scrolling
96 web.setOnTouchListener(null);
97
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);
Michael Kolb0241e752011-07-07 14:58:50 -0700111 // show url bar on scrolling
112 web.setOnTouchListener(this);
Michael Kolb376b5412010-12-15 11:52:57 -0800113 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700114 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800115 }
116 mTabBar.setUseQuickControls(mUseQuickControls);
John Reck1cf4b792011-07-26 10:22:22 -0700117 // We need to update the tabs with this change
118 for (Tab t : mTabControl.getTabs()) {
119 t.updateShouldCaptureThumbnails();
120 }
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();
John Reck35e9dd62011-04-25 09:01:54 -0700136 if (!BrowserSettings.getInstance().useInstantSearch()) {
John Reck0f602f32011-07-07 15:38:43 -0700137 mNavBar.clearCompletions();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000138 }
139 }
140
141 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800142 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800143 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800144 }
145
Michael Kolb66706532010-12-12 19:50:22 -0800146 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700147 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800148 if (web != null) {
149 web.stopScroll();
150 }
151 }
152
153 // WebView callbacks
154
155 @Override
John Reck30c714c2010-12-16 17:30:34 -0800156 public void onProgressChanged(Tab tab) {
157 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800158 mTabBar.onProgress(tab, progress);
159 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800160 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800161 }
162 }
163
164 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800165 public void addTab(Tab tab) {
166 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800167 }
168
169 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800170 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800171 }
172
173 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800174 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700175 mTitleBar.cancelTitleBarAnimation(true);
176 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700177 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700178 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800179 // TabControl.setCurrentTab has been called before this,
180 // so the tab is guaranteed to have a webview
181 if (view == null) {
182 Log.e(LOGTAG, "active tab with no webview detected");
183 return;
184 }
185 // Request focus on the top window.
186 if (mUseQuickControls) {
187 mPieControl.forceToTop(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -0800188 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800189 // check if title bar is already attached by animation
John Reck0f602f32011-07-07 15:38:43 -0700190 if (mTitleBar.getParent() == null) {
Michael Kolb377ea312011-02-17 14:36:56 -0800191 view.setEmbeddedTitleBar(mTitleBar);
192 }
Michael Kolb376b5412010-12-15 11:52:57 -0800193 }
Michael Kolb66706532010-12-12 19:50:22 -0800194 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800195 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700196 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800197 } else {
198 revertVoiceTitleBar(tab);
199 }
Michael Kolb376b5412010-12-15 11:52:57 -0800200 updateLockIconToLatest(tab);
201 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700202 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800203 }
204
205 @Override
206 public void updateTabs(List<Tab> tabs) {
207 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800208 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800209 }
210
211 @Override
212 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700213 mTitleBar.cancelTitleBarAnimation(true);
214 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800215 super.removeTab(tab);
216 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700217 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800218 }
219
220 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800221 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800222 }
223
Michael Kolb376b5412010-12-15 11:52:57 -0800224 int getContentWidth() {
225 if (mContentView != null) {
226 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800227 }
228 return 0;
229 }
230
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800231 @Override
232 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700233 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700234 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800235 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700236 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800237 }
238
239 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700240 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800241 }
242
243 @Override
244 protected void showTitleBar() {
245 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700246 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800247 }
Michael Kolb376b5412010-12-15 11:52:57 -0800248 }
249
Michael Kolb66706532010-12-12 19:50:22 -0800250 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800251 protected void hideTitleBar() {
252 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700253 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800254 }
255 }
256
257 @Override
Michael Kolb2a56eca2011-02-25 09:45:06 -0800258 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700259 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800260 super.setTitleGravity(gravity);
261 }
262 }
263
Michael Kolb66706532010-12-12 19:50:22 -0800264 // action mode callbacks
265
266 @Override
267 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800268 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700269 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800270 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800271 }
272 }
273
274 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800275 public void onActionModeFinished(boolean inLoad) {
276 checkTabCount();
277 if (inLoad) {
278 // the titlebar was removed when the CAB was shown
279 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800280 if (mUseQuickControls) {
281 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800282 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800283 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800284 }
285 }
286
287 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800288 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700289 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800290 }
291
292 @Override
John Reck30c714c2010-12-16 17:30:34 -0800293 public void setUrlTitle(Tab tab) {
294 super.setUrlTitle(tab);
295 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800296 }
297
298 // Set the favicon in the title bar.
299 @Override
John Reck30c714c2010-12-16 17:30:34 -0800300 public void setFavicon(Tab tab) {
301 super.setFavicon(tab);
302 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800303 }
304
Michael Kolbcfa3af52010-12-14 10:36:11 -0800305 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400306 public void showCustomView(View view, int requestedOrientation,
307 CustomViewCallback callback) {
308 super.showCustomView(view, requestedOrientation, callback);
John Reckd73c5a22010-12-22 10:22:50 -0800309 mActivity.getActionBar().hide();
310 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800311
John Reckd73c5a22010-12-22 10:22:50 -0800312 @Override
313 public void onHideCustomView() {
314 super.onHideCustomView();
315 if (mUseQuickControls) {
316 checkTabCount();
317 } else {
318 mActivity.getActionBar().show();
319 }
320 }
Michael Kolba4183062011-01-16 10:43:21 -0800321
322 @Override
323 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800324 if (mActiveTab != null) {
325 WebView web = mActiveTab.getWebView();
326 if (event.getAction() == KeyEvent.ACTION_DOWN) {
327 switch (code) {
328 case KeyEvent.KEYCODE_TAB:
329 case KeyEvent.KEYCODE_DPAD_UP:
330 case KeyEvent.KEYCODE_DPAD_LEFT:
331 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
332 editUrl(false);
333 return true;
334 }
335 }
336 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
337 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
338 editUrl(true);
339 return mContentView.dispatchKeyEvent(event);
340 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800341 }
Michael Kolba4183062011-01-16 10:43:21 -0800342 }
343 return false;
344 }
345
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800346 private boolean isTypingKey(KeyEvent evt) {
347 return evt.getUnicodeChar() > 0;
348 }
349
350 TabBar getTabBar() {
351 return mTabBar;
352 }
353
John Reck1cf4b792011-07-26 10:22:22 -0700354 @Override
355 public boolean shouldCaptureThumbnails() {
356 return mUseQuickControls;
357 }
358
John Reck034637c2011-08-11 11:34:44 -0700359 private Drawable getFaviconBackground() {
360 if (mFaviconBackground == null) {
361 mFaviconBackground = new PaintDrawable();
362 Resources res = mActivity.getResources();
363 mFaviconBackground.getPaint().setColor(
364 res.getColor(R.color.tabFaviconBackground));
365 mFaviconBackground.setCornerRadius(
366 res.getDimension(R.dimen.tab_favicon_corner_radius));
367 }
368 return mFaviconBackground;
369 }
370
371 @Override
372 public Drawable getFaviconDrawable(Bitmap icon) {
373 Drawable[] array = new Drawable[2];
374 array[0] = getFaviconBackground();
375 if (icon == null) {
376 array[1] = mGenericFavicon;
377 } else {
378 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
379 }
380 LayerDrawable d = new LayerDrawable(array);
381 d.setLayerInset(1, 2, 2, 2, 2);
382 return d;
383 }
384
Michael Kolb66706532010-12-12 19:50:22 -0800385}