blob: 73821bf720cf8201465f503375cb60d04f0ed7ca [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;
Michael Kolbac35bdc2011-01-17 17:06:04 -080021import android.os.Bundle;
Michael Kolbba238702011-03-08 10:40:50 -080022import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080023import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080024import android.view.ActionMode;
Michael Kolb376b5412010-12-15 11:52:57 -080025import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080026import android.view.KeyEvent;
John Reckd73c5a22010-12-22 10:22:50 -080027import android.view.View;
28import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080029import android.webkit.WebView;
30
31import java.util.List;
32
33/**
34 * Ui for xlarge screen sizes
35 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070036public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080037
38 private static final String LOGTAG = "XLargeUi";
39
Michael Kolb376b5412010-12-15 11:52:57 -080040 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080041 private TabBar mTabBar;
42
John Reck0f602f32011-07-07 15:38:43 -070043 private NavigationBarTablet mNavBar;
Michael Kolb66706532010-12-12 19:50:22 -080044
Michael Kolb0241e752011-07-07 14:58:50 -070045 private PieControlXLarge mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080046 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080047
Michael Kolb66706532010-12-12 19:50:22 -080048 /**
49 * @param browser
50 * @param controller
51 */
52 public XLargeUi(Activity browser, UiController controller) {
53 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080054 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070055 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080056 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080057 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080058 setupActionBar();
59 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
60 }
61
62 private void setupActionBar() {
63 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080064 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
65 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080066 }
67
John Reck2bc80422011-06-30 15:11:49 -070068 public void showComboView(ComboViews startWith, Bundle extras) {
69 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080070 if (mUseQuickControls) {
71 mActionBar.show();
72 }
73 }
74
75 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070076 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolb376b5412010-12-15 11:52:57 -080077 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080078 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080079 if (useQuickControls) {
80 checkTabCount();
Michael Kolb0241e752011-07-07 14:58:50 -070081 mPieControl = new PieControlXLarge(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080082 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070083 WebView web = getWebView();
84 if (web != null) {
85 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -070086 // don't show url bar on scrolling
87 web.setOnTouchListener(null);
88
Michael Kolb376b5412010-12-15 11:52:57 -080089 }
90 } else {
91 mActivity.getActionBar().show();
92 if (mPieControl != null) {
93 mPieControl.removeFromContainer(mContentView);
94 }
Michael Kolb46f987e2011-04-05 10:39:10 -070095 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -080096 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -080097 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -070098 // show url bar on scrolling
99 web.setOnTouchListener(this);
Michael Kolb376b5412010-12-15 11:52:57 -0800100 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700101 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800102 }
103 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800104 }
105
106 private void checkTabCount() {
107 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800108 mHandler.post(new Runnable() {
109 public void run() {
110 mActionBar.hide();
111 }
112 });
Michael Kolb376b5412010-12-15 11:52:57 -0800113 }
Michael Kolb66706532010-12-12 19:50:22 -0800114 }
115
116 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000117 public void onResume() {
118 super.onResume();
John Reck35e9dd62011-04-25 09:01:54 -0700119 if (!BrowserSettings.getInstance().useInstantSearch()) {
John Reck0f602f32011-07-07 15:38:43 -0700120 mNavBar.clearCompletions();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000121 }
122 }
123
124 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800125 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800126 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800127 }
128
Michael Kolb66706532010-12-12 19:50:22 -0800129 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700130 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800131 if (web != null) {
132 web.stopScroll();
133 }
134 }
135
136 // WebView callbacks
137
138 @Override
John Reck30c714c2010-12-16 17:30:34 -0800139 public void onProgressChanged(Tab tab) {
140 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800141 mTabBar.onProgress(tab, progress);
142 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800143 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800144 }
145 }
146
147 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800148 public void addTab(Tab tab) {
149 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800150 }
151
152 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800153 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800154 }
155
156 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800157 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700158 mTitleBar.cancelTitleBarAnimation(true);
159 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700160 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700161 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800162 // TabControl.setCurrentTab has been called before this,
163 // so the tab is guaranteed to have a webview
164 if (view == null) {
165 Log.e(LOGTAG, "active tab with no webview detected");
166 return;
167 }
168 // Request focus on the top window.
169 if (mUseQuickControls) {
170 mPieControl.forceToTop(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -0800171 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800172 // check if title bar is already attached by animation
John Reck0f602f32011-07-07 15:38:43 -0700173 if (mTitleBar.getParent() == null) {
Michael Kolb377ea312011-02-17 14:36:56 -0800174 view.setEmbeddedTitleBar(mTitleBar);
175 }
Michael Kolb376b5412010-12-15 11:52:57 -0800176 }
Michael Kolb66706532010-12-12 19:50:22 -0800177 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800178 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700179 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800180 } else {
181 revertVoiceTitleBar(tab);
182 }
Michael Kolb376b5412010-12-15 11:52:57 -0800183 updateLockIconToLatest(tab);
184 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700185 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800186 }
187
188 @Override
189 public void updateTabs(List<Tab> tabs) {
190 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800191 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800192 }
193
194 @Override
195 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700196 mTitleBar.cancelTitleBarAnimation(true);
197 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800198 super.removeTab(tab);
199 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700200 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800201 }
202
203 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800204 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800205 }
206
Michael Kolb376b5412010-12-15 11:52:57 -0800207 int getContentWidth() {
208 if (mContentView != null) {
209 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800210 }
211 return 0;
212 }
213
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800214 @Override
215 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700216 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700217 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800218 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700219 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800220 }
221
222 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700223 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800224 }
225
226 @Override
227 protected void showTitleBar() {
228 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700229 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800230 }
Michael Kolb376b5412010-12-15 11:52:57 -0800231 }
232
Michael Kolb66706532010-12-12 19:50:22 -0800233 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800234 protected void hideTitleBar() {
235 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700236 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800237 }
238 }
239
240 @Override
Michael Kolb2a56eca2011-02-25 09:45:06 -0800241 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700242 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800243 super.setTitleGravity(gravity);
244 }
245 }
246
Michael Kolb66706532010-12-12 19:50:22 -0800247 // action mode callbacks
248
249 @Override
250 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800251 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800252 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800253 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800254 }
255 }
256
257 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800258 public void onActionModeFinished(boolean inLoad) {
259 checkTabCount();
260 if (inLoad) {
261 // the titlebar was removed when the CAB was shown
262 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800263 if (mUseQuickControls) {
264 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800265 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800266 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800267 }
268 }
269
270 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800271 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700272 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800273 }
274
275 @Override
John Reck30c714c2010-12-16 17:30:34 -0800276 public void setUrlTitle(Tab tab) {
277 super.setUrlTitle(tab);
278 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800279 }
280
281 // Set the favicon in the title bar.
282 @Override
John Reck30c714c2010-12-16 17:30:34 -0800283 public void setFavicon(Tab tab) {
284 super.setFavicon(tab);
285 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800286 }
287
Michael Kolbcfa3af52010-12-14 10:36:11 -0800288 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400289 public void showCustomView(View view, int requestedOrientation,
290 CustomViewCallback callback) {
291 super.showCustomView(view, requestedOrientation, callback);
John Reckd73c5a22010-12-22 10:22:50 -0800292 mActivity.getActionBar().hide();
293 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800294
John Reckd73c5a22010-12-22 10:22:50 -0800295 @Override
296 public void onHideCustomView() {
297 super.onHideCustomView();
298 if (mUseQuickControls) {
299 checkTabCount();
300 } else {
301 mActivity.getActionBar().show();
302 }
303 }
Michael Kolba4183062011-01-16 10:43:21 -0800304
305 @Override
306 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800307 if (mActiveTab != null) {
308 WebView web = mActiveTab.getWebView();
309 if (event.getAction() == KeyEvent.ACTION_DOWN) {
310 switch (code) {
311 case KeyEvent.KEYCODE_TAB:
312 case KeyEvent.KEYCODE_DPAD_UP:
313 case KeyEvent.KEYCODE_DPAD_LEFT:
314 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
315 editUrl(false);
316 return true;
317 }
318 }
319 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
320 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
321 editUrl(true);
322 return mContentView.dispatchKeyEvent(event);
323 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800324 }
Michael Kolba4183062011-01-16 10:43:21 -0800325 }
326 return false;
327 }
328
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800329 private boolean isTypingKey(KeyEvent evt) {
330 return evt.getUnicodeChar() > 0;
331 }
332
333 TabBar getTabBar() {
334 return mTabBar;
335 }
336
Michael Kolb66706532010-12-12 19:50:22 -0800337}