blob: df7a586edb2e3575dcb936e6f86c8f830cb0f51c [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 Kolba4183062011-01-16 10:43:21 -080031import android.view.KeyEvent;
Michael Kolb0d0245f2011-12-05 16:36:05 -080032import android.view.Menu;
33import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080034import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000035import android.webkit.WebViewClassic;
Michael Kolb66706532010-12-12 19:50:22 -080036
37import java.util.List;
38
39/**
40 * Ui for xlarge screen sizes
41 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070042public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080043
44 private static final String LOGTAG = "XLargeUi";
45
John Reck034637c2011-08-11 11:34:44 -070046 private PaintDrawable mFaviconBackground;
47
Michael Kolb376b5412010-12-15 11:52:57 -080048 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080049 private TabBar mTabBar;
50
John Reck0f602f32011-07-07 15:38:43 -070051 private NavigationBarTablet mNavBar;
Michael Kolb66706532010-12-12 19:50:22 -080052
Michael Kolbba238702011-03-08 10:40:50 -080053 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080054
Michael Kolb66706532010-12-12 19:50:22 -080055 /**
56 * @param browser
57 * @param controller
58 */
59 public XLargeUi(Activity browser, UiController controller) {
60 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080061 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070062 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080063 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080064 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080065 setupActionBar();
66 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
67 }
68
69 private void setupActionBar() {
70 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080071 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
72 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080073 }
74
John Reck2bc80422011-06-30 15:11:49 -070075 public void showComboView(ComboViews startWith, Bundle extras) {
76 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080077 if (mUseQuickControls) {
78 mActionBar.show();
79 }
80 }
81
82 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070083 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbda580632012-04-16 13:30:28 -070084 super.setUseQuickControls(useQuickControls);
85 checkHideActionBar();
86 if (!useQuickControls) {
87 mActionBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -080088 }
89 mTabBar.setUseQuickControls(mUseQuickControls);
John Reck1cf4b792011-07-26 10:22:22 -070090 // We need to update the tabs with this change
91 for (Tab t : mTabControl.getTabs()) {
92 t.updateShouldCaptureThumbnails();
93 }
Michael Kolb376b5412010-12-15 11:52:57 -080094 }
95
Michael Kolbda580632012-04-16 13:30:28 -070096 private void checkHideActionBar() {
Michael Kolb376b5412010-12-15 11:52:57 -080097 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -080098 mHandler.post(new Runnable() {
99 public void run() {
100 mActionBar.hide();
101 }
102 });
Michael Kolb376b5412010-12-15 11:52:57 -0800103 }
Michael Kolb66706532010-12-12 19:50:22 -0800104 }
105
106 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000107 public void onResume() {
108 super.onResume();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000109 mNavBar.clearCompletions();
Michael Kolbda580632012-04-16 13:30:28 -0700110 checkHideActionBar();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000111 }
112
113 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800114 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800115 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800116 }
117
Michael Kolb66706532010-12-12 19:50:22 -0800118 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700119 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800120 if (web != null && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000121 WebViewClassic.fromWebView(web).stopScroll();
Michael Kolb66706532010-12-12 19:50:22 -0800122 }
123 }
124
Michael Kolb0d0245f2011-12-05 16:36:05 -0800125 @Override
126 public boolean onPrepareOptionsMenu(Menu menu) {
127 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
128 if (bm != null) {
129 bm.setVisible(false);
130 }
131 return true;
132 }
133
134
Michael Kolb66706532010-12-12 19:50:22 -0800135 // WebView callbacks
136
137 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800138 public void addTab(Tab tab) {
139 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800140 }
141
142 protected void onAddTabCompleted(Tab tab) {
Michael Kolbda580632012-04-16 13:30:28 -0700143 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800144 }
145
146 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800147 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700148 mTitleBar.cancelTitleBarAnimation(true);
149 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700150 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700151 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800152 // TabControl.setCurrentTab has been called before this,
153 // so the tab is guaranteed to have a webview
154 if (view == null) {
155 Log.e(LOGTAG, "active tab with no webview detected");
156 return;
157 }
Michael Kolb66706532010-12-12 19:50:22 -0800158 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800159 updateLockIconToLatest(tab);
John Reck5d43ce82011-06-21 17:16:51 -0700160 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800161 }
162
163 @Override
164 public void updateTabs(List<Tab> tabs) {
165 mTabBar.updateTabs(tabs);
Michael Kolbda580632012-04-16 13:30:28 -0700166 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800167 }
168
169 @Override
170 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700171 mTitleBar.cancelTitleBarAnimation(true);
172 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800173 super.removeTab(tab);
174 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700175 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800176 }
177
178 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolbda580632012-04-16 13:30:28 -0700179 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800180 }
181
Michael Kolb376b5412010-12-15 11:52:57 -0800182 int getContentWidth() {
183 if (mContentView != null) {
184 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800185 }
186 return 0;
187 }
188
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800189 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -0700190 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700191 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700192 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800193 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700194 super.editUrl(clearInput, forceIME);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800195 }
196
Michael Kolb66706532010-12-12 19:50:22 -0800197 // action mode callbacks
198
199 @Override
200 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800201 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700202 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800203 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800204 }
205 }
206
207 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800208 public void onActionModeFinished(boolean inLoad) {
Michael Kolbda580632012-04-16 13:30:28 -0700209 checkHideActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800210 if (inLoad) {
211 // the titlebar was removed when the CAB was shown
212 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800213 if (mUseQuickControls) {
214 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800215 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800216 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800217 }
218 }
219
220 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800221 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700222 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800223 }
224
225 @Override
John Reck30c714c2010-12-16 17:30:34 -0800226 public void setUrlTitle(Tab tab) {
227 super.setUrlTitle(tab);
228 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800229 }
230
231 // Set the favicon in the title bar.
232 @Override
John Reck30c714c2010-12-16 17:30:34 -0800233 public void setFavicon(Tab tab) {
234 super.setFavicon(tab);
235 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
Michael Kolbcfa3af52010-12-14 10:36:11 -0800238 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800239 public void onHideCustomView() {
240 super.onHideCustomView();
Michael Kolbda580632012-04-16 13:30:28 -0700241 checkHideActionBar();
John Reckd73c5a22010-12-22 10:22:50 -0800242 }
Michael Kolba4183062011-01-16 10:43:21 -0800243
244 @Override
245 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800246 if (mActiveTab != null) {
247 WebView web = mActiveTab.getWebView();
248 if (event.getAction() == KeyEvent.ACTION_DOWN) {
249 switch (code) {
250 case KeyEvent.KEYCODE_TAB:
251 case KeyEvent.KEYCODE_DPAD_UP:
252 case KeyEvent.KEYCODE_DPAD_LEFT:
253 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700254 editUrl(false, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800255 return true;
256 }
257 }
258 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
259 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700260 editUrl(true, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800261 return mContentView.dispatchKeyEvent(event);
262 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800263 }
Michael Kolba4183062011-01-16 10:43:21 -0800264 }
265 return false;
266 }
267
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800268 private boolean isTypingKey(KeyEvent evt) {
269 return evt.getUnicodeChar() > 0;
270 }
271
272 TabBar getTabBar() {
273 return mTabBar;
274 }
275
John Reck1cf4b792011-07-26 10:22:22 -0700276 @Override
277 public boolean shouldCaptureThumbnails() {
278 return mUseQuickControls;
279 }
280
John Reck034637c2011-08-11 11:34:44 -0700281 private Drawable getFaviconBackground() {
282 if (mFaviconBackground == null) {
283 mFaviconBackground = new PaintDrawable();
284 Resources res = mActivity.getResources();
285 mFaviconBackground.getPaint().setColor(
286 res.getColor(R.color.tabFaviconBackground));
287 mFaviconBackground.setCornerRadius(
288 res.getDimension(R.dimen.tab_favicon_corner_radius));
289 }
290 return mFaviconBackground;
291 }
292
293 @Override
294 public Drawable getFaviconDrawable(Bitmap icon) {
295 Drawable[] array = new Drawable[2];
296 array[0] = getFaviconBackground();
297 if (icon == null) {
298 array[1] = mGenericFavicon;
299 } else {
300 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
301 }
302 LayerDrawable d = new LayerDrawable(array);
303 d.setLayerInset(1, 2, 2, 2, 2);
304 return d;
305 }
306
Michael Kolb66706532010-12-12 19:50:22 -0800307}