blob: 5c11b588dd43bbc66d02c7cfbb9ce27e8e86fcf7 [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;
Michael Kolb52051eb2011-08-02 13:06:43 -070028import android.view.ViewGroup;
John Reckd73c5a22010-12-22 10:22:50 -080029import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080030import android.webkit.WebView;
31
32import java.util.List;
33
34/**
35 * Ui for xlarge screen sizes
36 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070037public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080038
39 private static final String LOGTAG = "XLargeUi";
40
Michael Kolb376b5412010-12-15 11:52:57 -080041 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080042 private TabBar mTabBar;
43
John Reck0f602f32011-07-07 15:38:43 -070044 private NavigationBarTablet mNavBar;
Michael Kolb66706532010-12-12 19:50:22 -080045
Michael Kolb0241e752011-07-07 14:58:50 -070046 private PieControlXLarge mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080047 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080048
Michael Kolb66706532010-12-12 19:50:22 -080049 /**
50 * @param browser
51 * @param controller
52 */
53 public XLargeUi(Activity browser, UiController controller) {
54 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080055 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070056 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080057 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080058 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080059 setupActionBar();
60 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
61 }
62
63 private void setupActionBar() {
64 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080065 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
66 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080067 }
68
John Reck2bc80422011-06-30 15:11:49 -070069 public void showComboView(ComboViews startWith, Bundle extras) {
70 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080071 if (mUseQuickControls) {
72 mActionBar.show();
73 }
74 }
75
76 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070077 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolb376b5412010-12-15 11:52:57 -080078 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080079 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080080 if (useQuickControls) {
81 checkTabCount();
Michael Kolb0241e752011-07-07 14:58:50 -070082 mPieControl = new PieControlXLarge(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080083 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070084 WebView web = getWebView();
85 if (web != null) {
86 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -070087 // don't show url bar on scrolling
88 web.setOnTouchListener(null);
89
Michael Kolb376b5412010-12-15 11:52:57 -080090 }
91 } else {
92 mActivity.getActionBar().show();
93 if (mPieControl != null) {
94 mPieControl.removeFromContainer(mContentView);
95 }
Michael Kolb46f987e2011-04-05 10:39:10 -070096 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -080097 if (web != null) {
Michael Kolb52051eb2011-08-02 13:06:43 -070098 if (mTitleBar.getParent() != null) {
99 ViewGroup p = (ViewGroup) mTitleBar.getParent();
100 p.removeView(mTitleBar);
101 }
Michael Kolb376b5412010-12-15 11:52:57 -0800102 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -0700103 // show url bar on scrolling
104 web.setOnTouchListener(this);
Michael Kolb376b5412010-12-15 11:52:57 -0800105 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700106 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800107 }
108 mTabBar.setUseQuickControls(mUseQuickControls);
John Reck1cf4b792011-07-26 10:22:22 -0700109 // We need to update the tabs with this change
110 for (Tab t : mTabControl.getTabs()) {
111 t.updateShouldCaptureThumbnails();
112 }
Michael Kolb376b5412010-12-15 11:52:57 -0800113 }
114
115 private void checkTabCount() {
116 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800117 mHandler.post(new Runnable() {
118 public void run() {
119 mActionBar.hide();
120 }
121 });
Michael Kolb376b5412010-12-15 11:52:57 -0800122 }
Michael Kolb66706532010-12-12 19:50:22 -0800123 }
124
125 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000126 public void onResume() {
127 super.onResume();
John Reck35e9dd62011-04-25 09:01:54 -0700128 if (!BrowserSettings.getInstance().useInstantSearch()) {
John Reck0f602f32011-07-07 15:38:43 -0700129 mNavBar.clearCompletions();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000130 }
131 }
132
133 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800134 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800135 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800136 }
137
Michael Kolb66706532010-12-12 19:50:22 -0800138 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700139 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800140 if (web != null) {
141 web.stopScroll();
142 }
143 }
144
145 // WebView callbacks
146
147 @Override
John Reck30c714c2010-12-16 17:30:34 -0800148 public void onProgressChanged(Tab tab) {
149 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800150 mTabBar.onProgress(tab, progress);
151 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800152 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800153 }
154 }
155
156 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800157 public void addTab(Tab tab) {
158 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800159 }
160
161 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800162 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800163 }
164
165 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800166 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700167 mTitleBar.cancelTitleBarAnimation(true);
168 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700169 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700170 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800171 // TabControl.setCurrentTab has been called before this,
172 // so the tab is guaranteed to have a webview
173 if (view == null) {
174 Log.e(LOGTAG, "active tab with no webview detected");
175 return;
176 }
177 // Request focus on the top window.
178 if (mUseQuickControls) {
179 mPieControl.forceToTop(mContentView);
Michael Kolb376b5412010-12-15 11:52:57 -0800180 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800181 // check if title bar is already attached by animation
John Reck0f602f32011-07-07 15:38:43 -0700182 if (mTitleBar.getParent() == null) {
Michael Kolb377ea312011-02-17 14:36:56 -0800183 view.setEmbeddedTitleBar(mTitleBar);
184 }
Michael Kolb376b5412010-12-15 11:52:57 -0800185 }
Michael Kolb66706532010-12-12 19:50:22 -0800186 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800187 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700188 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800189 } else {
190 revertVoiceTitleBar(tab);
191 }
Michael Kolb376b5412010-12-15 11:52:57 -0800192 updateLockIconToLatest(tab);
193 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700194 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800195 }
196
197 @Override
198 public void updateTabs(List<Tab> tabs) {
199 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800200 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800201 }
202
203 @Override
204 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700205 mTitleBar.cancelTitleBarAnimation(true);
206 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800207 super.removeTab(tab);
208 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700209 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800210 }
211
212 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800213 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800214 }
215
Michael Kolb376b5412010-12-15 11:52:57 -0800216 int getContentWidth() {
217 if (mContentView != null) {
218 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800219 }
220 return 0;
221 }
222
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800223 @Override
224 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700225 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700226 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800227 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700228 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800229 }
230
231 void stopEditingUrl() {
John Reck0f602f32011-07-07 15:38:43 -0700232 mTitleBar.getNavigationBar().stopEditingUrl();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800233 }
234
235 @Override
236 protected void showTitleBar() {
237 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700238 mTitleBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800239 }
Michael Kolb376b5412010-12-15 11:52:57 -0800240 }
241
Michael Kolb66706532010-12-12 19:50:22 -0800242 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800243 protected void hideTitleBar() {
244 if (isTitleBarShowing()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700245 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800246 }
247 }
248
249 @Override
Michael Kolb2a56eca2011-02-25 09:45:06 -0800250 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700251 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800252 super.setTitleGravity(gravity);
253 }
254 }
255
Michael Kolb66706532010-12-12 19:50:22 -0800256 // action mode callbacks
257
258 @Override
259 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800260 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700261 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800262 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800263 }
264 }
265
266 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800267 public void onActionModeFinished(boolean inLoad) {
268 checkTabCount();
269 if (inLoad) {
270 // the titlebar was removed when the CAB was shown
271 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800272 if (mUseQuickControls) {
273 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800274 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800275 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800276 }
277 }
278
279 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800280 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700281 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800282 }
283
284 @Override
John Reck30c714c2010-12-16 17:30:34 -0800285 public void setUrlTitle(Tab tab) {
286 super.setUrlTitle(tab);
287 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800288 }
289
290 // Set the favicon in the title bar.
291 @Override
John Reck30c714c2010-12-16 17:30:34 -0800292 public void setFavicon(Tab tab) {
293 super.setFavicon(tab);
294 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800295 }
296
Michael Kolbcfa3af52010-12-14 10:36:11 -0800297 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400298 public void showCustomView(View view, int requestedOrientation,
299 CustomViewCallback callback) {
300 super.showCustomView(view, requestedOrientation, callback);
John Reckd73c5a22010-12-22 10:22:50 -0800301 mActivity.getActionBar().hide();
302 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800303
John Reckd73c5a22010-12-22 10:22:50 -0800304 @Override
305 public void onHideCustomView() {
306 super.onHideCustomView();
307 if (mUseQuickControls) {
308 checkTabCount();
309 } else {
310 mActivity.getActionBar().show();
311 }
312 }
Michael Kolba4183062011-01-16 10:43:21 -0800313
314 @Override
315 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800316 if (mActiveTab != null) {
317 WebView web = mActiveTab.getWebView();
318 if (event.getAction() == KeyEvent.ACTION_DOWN) {
319 switch (code) {
320 case KeyEvent.KEYCODE_TAB:
321 case KeyEvent.KEYCODE_DPAD_UP:
322 case KeyEvent.KEYCODE_DPAD_LEFT:
323 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
324 editUrl(false);
325 return true;
326 }
327 }
328 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
329 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
330 editUrl(true);
331 return mContentView.dispatchKeyEvent(event);
332 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800333 }
Michael Kolba4183062011-01-16 10:43:21 -0800334 }
335 return false;
336 }
337
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800338 private boolean isTypingKey(KeyEvent evt) {
339 return evt.getUnicodeChar() > 0;
340 }
341
342 TabBar getTabBar() {
343 return mTabBar;
344 }
345
John Reck1cf4b792011-07-26 10:22:22 -0700346 @Override
347 public boolean shouldCaptureThumbnails() {
348 return mUseQuickControls;
349 }
350
Michael Kolb66706532010-12-12 19:50:22 -0800351}