blob: a86833140066b33c1bee5d32f8ac57a3bd85c0ed [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
John Reckb3417f02011-01-14 11:01:05 -080076 public void hideComboView() {
Michael Kolbba238702011-03-08 10:40:50 -080077 if (isComboViewShowing()) {
78 super.hideComboView();
79 // ComboView changes the action bar, set it back up to what we want
80 setupActionBar();
81 checkTabCount();
82 }
Michael Kolb376b5412010-12-15 11:52:57 -080083 }
84
Michael Kolb0241e752011-07-07 14:58:50 -070085 @Override
86 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolb376b5412010-12-15 11:52:57 -080087 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080088 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080089 if (useQuickControls) {
90 checkTabCount();
Michael Kolb0241e752011-07-07 14:58:50 -070091 mPieControl = new PieControlXLarge(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080092 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070093 WebView web = getWebView();
94 if (web != null) {
95 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -070096 // don't show url bar on scrolling
97 web.setOnTouchListener(null);
98
Michael Kolb376b5412010-12-15 11:52:57 -080099 }
100 } else {
101 mActivity.getActionBar().show();
102 if (mPieControl != null) {
103 mPieControl.removeFromContainer(mContentView);
104 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700105 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800106 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800107 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -0700108 // show url bar on scrolling
109 web.setOnTouchListener(this);
Michael Kolb376b5412010-12-15 11:52:57 -0800110 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700111 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800112 }
113 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800114 }
115
116 private void checkTabCount() {
117 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800118 mHandler.post(new Runnable() {
119 public void run() {
120 mActionBar.hide();
121 }
122 });
Michael Kolb376b5412010-12-15 11:52:57 -0800123 }
Michael Kolb66706532010-12-12 19:50:22 -0800124 }
125
126 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000127 public void onResume() {
128 super.onResume();
John Reck35e9dd62011-04-25 09:01:54 -0700129 if (!BrowserSettings.getInstance().useInstantSearch()) {
John Reck0f602f32011-07-07 15:38:43 -0700130 mNavBar.clearCompletions();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000131 }
132 }
133
134 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800135 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800136 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800137 }
138
Michael Kolb66706532010-12-12 19:50:22 -0800139 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700140 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800141 if (web != null) {
142 web.stopScroll();
143 }
144 }
145
146 // WebView callbacks
147
148 @Override
John Reck30c714c2010-12-16 17:30:34 -0800149 public void onProgressChanged(Tab tab) {
150 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800151 mTabBar.onProgress(tab, progress);
152 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800153 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800154 }
155 }
156
157 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800158 public void addTab(Tab tab) {
159 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800160 }
161
162 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800163 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800164 }
165
166 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800167 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700168 mTitleBar.cancelTitleBarAnimation(true);
169 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolbeb95db42011-03-03 10:38:40 -0800170 if (mUseQuickControls) {
171 if (mActiveTab != null) {
172 captureTab(mActiveTab);
173 }
174 }
John Reck5d43ce82011-06-21 17:16:51 -0700175 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700176 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800177 // TabControl.setCurrentTab has been called before this,
178 // so the tab is guaranteed to have a webview
179 if (view == null) {
180 Log.e(LOGTAG, "active tab with no webview detected");
181 return;
182 }
183 // Request focus on the top window.
184 if (mUseQuickControls) {
185 mPieControl.forceToTop(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -0800186 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800187 // check if title bar is already attached by animation
John Reck0f602f32011-07-07 15:38:43 -0700188 if (mTitleBar.getParent() == null) {
Michael Kolb377ea312011-02-17 14:36:56 -0800189 view.setEmbeddedTitleBar(mTitleBar);
190 }
Michael Kolb376b5412010-12-15 11:52:57 -0800191 }
Michael Kolb66706532010-12-12 19:50:22 -0800192 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800193 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700194 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800195 } else {
196 revertVoiceTitleBar(tab);
197 }
Michael Kolb376b5412010-12-15 11:52:57 -0800198 updateLockIconToLatest(tab);
199 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700200 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800201 }
202
203 @Override
204 public void updateTabs(List<Tab> tabs) {
205 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800206 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800207 }
208
209 @Override
210 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700211 mTitleBar.cancelTitleBarAnimation(true);
212 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800213 super.removeTab(tab);
214 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700215 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800216 }
217
218 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800219 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800220 }
221
Michael Kolb376b5412010-12-15 11:52:57 -0800222 int getContentWidth() {
223 if (mContentView != null) {
224 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800225 }
226 return 0;
227 }
228
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800229 @Override
230 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700231 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700232 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800233 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700234 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 }
236
237 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700238 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800239 }
240
241 @Override
242 protected void showTitleBar() {
243 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700244 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800245 }
Michael Kolb376b5412010-12-15 11:52:57 -0800246 }
247
Michael Kolb66706532010-12-12 19:50:22 -0800248 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800249 protected void hideTitleBar() {
250 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700251 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800252 }
253 }
254
255 @Override
Michael Kolb2a56eca2011-02-25 09:45:06 -0800256 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700257 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800258 super.setTitleGravity(gravity);
259 }
260 }
261
Michael Kolb66706532010-12-12 19:50:22 -0800262 // action mode callbacks
263
264 @Override
265 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800266 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800267 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800268 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800269 }
270 }
271
272 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800273 public void onActionModeFinished(boolean inLoad) {
274 checkTabCount();
275 if (inLoad) {
276 // the titlebar was removed when the CAB was shown
277 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800278 if (mUseQuickControls) {
279 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800280 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800281 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800282 }
283 }
284
285 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800286 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700287 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800288 }
289
290 @Override
John Reck30c714c2010-12-16 17:30:34 -0800291 public void setUrlTitle(Tab tab) {
292 super.setUrlTitle(tab);
293 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800294 }
295
296 // Set the favicon in the title bar.
297 @Override
John Reck30c714c2010-12-16 17:30:34 -0800298 public void setFavicon(Tab tab) {
299 super.setFavicon(tab);
300 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800301 }
302
Michael Kolbcfa3af52010-12-14 10:36:11 -0800303 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400304 public void showCustomView(View view, int requestedOrientation,
305 CustomViewCallback callback) {
306 super.showCustomView(view, requestedOrientation, callback);
John Reckd73c5a22010-12-22 10:22:50 -0800307 mActivity.getActionBar().hide();
308 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800309
John Reckd73c5a22010-12-22 10:22:50 -0800310 @Override
311 public void onHideCustomView() {
312 super.onHideCustomView();
313 if (mUseQuickControls) {
314 checkTabCount();
315 } else {
316 mActivity.getActionBar().show();
317 }
318 }
Michael Kolba4183062011-01-16 10:43:21 -0800319
320 @Override
321 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800322 if (mActiveTab != null) {
323 WebView web = mActiveTab.getWebView();
324 if (event.getAction() == KeyEvent.ACTION_DOWN) {
325 switch (code) {
326 case KeyEvent.KEYCODE_TAB:
327 case KeyEvent.KEYCODE_DPAD_UP:
328 case KeyEvent.KEYCODE_DPAD_LEFT:
329 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
330 editUrl(false);
331 return true;
332 }
333 }
334 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
335 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
336 editUrl(true);
337 return mContentView.dispatchKeyEvent(event);
338 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800339 }
Michael Kolba4183062011-01-16 10:43:21 -0800340 }
341 return false;
342 }
343
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800344 private boolean isTypingKey(KeyEvent evt) {
345 return evt.getUnicodeChar() > 0;
346 }
347
348 TabBar getTabBar() {
349 return mTabBar;
350 }
351
Michael Kolb66706532010-12-12 19:50:22 -0800352}