blob: 31430250a9beb1ccd3776e538973cb980573f07b [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();
Michael Kolbd2e35932012-01-19 09:29:16 -0800136 checkTabCount();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000137 }
138
139 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800140 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800141 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800142 }
143
Michael Kolb66706532010-12-12 19:50:22 -0800144 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700145 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800146 if (web != null) {
147 web.stopScroll();
148 }
149 }
150
Michael Kolb0d0245f2011-12-05 16:36:05 -0800151 @Override
152 public boolean onPrepareOptionsMenu(Menu menu) {
153 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
154 if (bm != null) {
155 bm.setVisible(false);
156 }
157 return true;
158 }
159
160
Michael Kolb66706532010-12-12 19:50:22 -0800161 // WebView callbacks
162
163 @Override
John Reck30c714c2010-12-16 17:30:34 -0800164 public void onProgressChanged(Tab tab) {
165 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800166 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800167 mTitleBar.setProgress(progress);
Michael Kolb1c3a6d22011-12-15 15:28:49 -0800168 if (mUseQuickControls) {
169 if (progress == 100) {
170 mTitleBar.setShowProgressOnly(false);
171 } else if (!mTitleBar.isEditingUrl()) {
172 mTitleBar.setShowProgressOnly(true);
173 }
174 }
Michael Kolb66706532010-12-12 19:50:22 -0800175 }
176 }
177
178 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800179 public void addTab(Tab tab) {
180 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800181 }
182
183 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800184 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800185 }
186
187 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800188 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700189 mTitleBar.cancelTitleBarAnimation(true);
190 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700191 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700192 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800193 // TabControl.setCurrentTab has been called before this,
194 // so the tab is guaranteed to have a webview
195 if (view == null) {
196 Log.e(LOGTAG, "active tab with no webview detected");
197 return;
198 }
199 // Request focus on the top window.
200 if (mUseQuickControls) {
201 mPieControl.forceToTop(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -0800202 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800203 // check if title bar is already attached by animation
John Reck0f602f32011-07-07 15:38:43 -0700204 if (mTitleBar.getParent() == null) {
Michael Kolb377ea312011-02-17 14:36:56 -0800205 view.setEmbeddedTitleBar(mTitleBar);
206 }
Michael Kolb376b5412010-12-15 11:52:57 -0800207 }
Michael Kolb66706532010-12-12 19:50:22 -0800208 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800209 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700210 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800211 } else {
212 revertVoiceTitleBar(tab);
213 }
Michael Kolb376b5412010-12-15 11:52:57 -0800214 updateLockIconToLatest(tab);
215 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700216 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800217 }
218
219 @Override
220 public void updateTabs(List<Tab> tabs) {
221 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800222 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800223 }
224
225 @Override
226 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700227 mTitleBar.cancelTitleBarAnimation(true);
228 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800229 super.removeTab(tab);
230 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700231 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800232 }
233
234 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800235 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
Michael Kolb376b5412010-12-15 11:52:57 -0800238 int getContentWidth() {
239 if (mContentView != null) {
240 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242 return 0;
243 }
244
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800245 @Override
246 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700247 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700248 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800249 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700250 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800251 }
252
253 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700254 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800255 }
256
257 @Override
258 protected void showTitleBar() {
259 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700260 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800261 }
Michael Kolb376b5412010-12-15 11:52:57 -0800262 }
263
Michael Kolb66706532010-12-12 19:50:22 -0800264 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800265 protected void hideTitleBar() {
266 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700267 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800268 }
269 }
270
271 @Override
Michael Kolb2a56eca2011-02-25 09:45:06 -0800272 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700273 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800274 super.setTitleGravity(gravity);
275 }
276 }
277
Michael Kolb66706532010-12-12 19:50:22 -0800278 // action mode callbacks
279
280 @Override
281 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800282 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700283 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800284 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800285 }
286 }
287
288 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800289 public void onActionModeFinished(boolean inLoad) {
290 checkTabCount();
291 if (inLoad) {
292 // the titlebar was removed when the CAB was shown
293 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800294 if (mUseQuickControls) {
295 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800296 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800297 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800298 }
299 }
300
301 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800302 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700303 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800304 }
305
306 @Override
John Reck30c714c2010-12-16 17:30:34 -0800307 public void setUrlTitle(Tab tab) {
308 super.setUrlTitle(tab);
309 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800310 }
311
312 // Set the favicon in the title bar.
313 @Override
John Reck30c714c2010-12-16 17:30:34 -0800314 public void setFavicon(Tab tab) {
315 super.setFavicon(tab);
316 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800317 }
318
Michael Kolbcfa3af52010-12-14 10:36:11 -0800319 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800320 public void onHideCustomView() {
321 super.onHideCustomView();
322 if (mUseQuickControls) {
323 checkTabCount();
John Reckd73c5a22010-12-22 10:22:50 -0800324 }
325 }
Michael Kolba4183062011-01-16 10:43:21 -0800326
327 @Override
328 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800329 if (mActiveTab != null) {
330 WebView web = mActiveTab.getWebView();
331 if (event.getAction() == KeyEvent.ACTION_DOWN) {
332 switch (code) {
333 case KeyEvent.KEYCODE_TAB:
334 case KeyEvent.KEYCODE_DPAD_UP:
335 case KeyEvent.KEYCODE_DPAD_LEFT:
336 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
337 editUrl(false);
338 return true;
339 }
340 }
341 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
342 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
343 editUrl(true);
344 return mContentView.dispatchKeyEvent(event);
345 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800346 }
Michael Kolba4183062011-01-16 10:43:21 -0800347 }
348 return false;
349 }
350
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800351 private boolean isTypingKey(KeyEvent evt) {
352 return evt.getUnicodeChar() > 0;
353 }
354
355 TabBar getTabBar() {
356 return mTabBar;
357 }
358
John Reck1cf4b792011-07-26 10:22:22 -0700359 @Override
360 public boolean shouldCaptureThumbnails() {
361 return mUseQuickControls;
362 }
363
John Reck034637c2011-08-11 11:34:44 -0700364 private Drawable getFaviconBackground() {
365 if (mFaviconBackground == null) {
366 mFaviconBackground = new PaintDrawable();
367 Resources res = mActivity.getResources();
368 mFaviconBackground.getPaint().setColor(
369 res.getColor(R.color.tabFaviconBackground));
370 mFaviconBackground.setCornerRadius(
371 res.getDimension(R.dimen.tab_favicon_corner_radius));
372 }
373 return mFaviconBackground;
374 }
375
376 @Override
377 public Drawable getFaviconDrawable(Bitmap icon) {
378 Drawable[] array = new Drawable[2];
379 array[0] = getFaviconBackground();
380 if (icon == null) {
381 array[1] = mGenericFavicon;
382 } else {
383 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
384 }
385 LayerDrawable d = new LayerDrawable(array);
386 d.setLayerInset(1, 2, 2, 2, 2);
387 return d;
388 }
389
Michael Kolb66706532010-12-12 19:50:22 -0800390}