blob: 39981b073f6e59635e98aa66845ee99db4813383 [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
John Reckb9a051b2011-03-18 11:55:48 -070019import com.android.browser.BrowserWebView.ScrollListener;
Michael Kolb66706532010-12-12 19:50:22 -080020
21import android.app.ActionBar;
22import android.app.Activity;
Michael Kolbac35bdc2011-01-17 17:06:04 -080023import android.os.Bundle;
Michael Kolbba238702011-03-08 10:40:50 -080024import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080025import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080026import android.view.ActionMode;
Michael Kolb376b5412010-12-15 11:52:57 -080027import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080028import android.view.KeyEvent;
Michael Kolb1acef692011-03-08 14:12:06 -080029import android.view.Menu;
John Reckd73c5a22010-12-22 10:22:50 -080030import android.view.View;
31import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080032import android.webkit.WebView;
33
34import java.util.List;
35
36/**
37 * Ui for xlarge screen sizes
38 */
39public class XLargeUi extends BaseUi implements ScrollListener {
40
41 private static final String LOGTAG = "XLargeUi";
42
Michael Kolb376b5412010-12-15 11:52:57 -080043 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080044 private TabBar mTabBar;
45
46 private TitleBarXLarge mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080047
Michael Kolb376b5412010-12-15 11:52:57 -080048 private boolean mUseQuickControls;
49 private PieControl mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080050 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080051
Michael Kolb66706532010-12-12 19:50:22 -080052 /**
53 * @param browser
54 * @param controller
55 */
56 public XLargeUi(Activity browser, UiController controller) {
57 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080058 mHandler = new Handler();
Michael Kolb46f987e2011-04-05 10:39:10 -070059 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this,
60 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080061 mTitleBar.setProgress(100);
Michael Kolb66706532010-12-12 19:50:22 -080062 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080063 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080064 setupActionBar();
65 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
66 }
67
68 private void setupActionBar() {
69 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080070 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
71 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080072 }
73
74 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080075 public void showComboView(boolean startWithHistory, Bundle extras) {
76 super.showComboView(startWithHistory, extras);
77 if (mUseQuickControls) {
78 mActionBar.show();
79 }
80 }
81
82 @Override
John Reckb3417f02011-01-14 11:01:05 -080083 public void hideComboView() {
Michael Kolbba238702011-03-08 10:40:50 -080084 if (isComboViewShowing()) {
85 super.hideComboView();
86 // ComboView changes the action bar, set it back up to what we want
87 setupActionBar();
88 checkTabCount();
89 }
Michael Kolb376b5412010-12-15 11:52:57 -080090 }
91
92 private void setUseQuickControls(boolean useQuickControls) {
93 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080094 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080095 if (useQuickControls) {
96 checkTabCount();
97 mPieControl = new PieControl(mActivity, mUiController, this);
98 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070099 WebView web = getWebView();
100 if (web != null) {
101 web.setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -0800102 }
103 } else {
104 mActivity.getActionBar().show();
105 if (mPieControl != null) {
106 mPieControl.removeFromContainer(mContentView);
107 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700108 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800109 if (web != null) {
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);
Michael Kolb376b5412010-12-15 11:52:57 -0800115 }
116
117 private void checkTabCount() {
118 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800119 mHandler.post(new Runnable() {
120 public void run() {
121 mActionBar.hide();
122 }
123 });
Michael Kolb376b5412010-12-15 11:52:57 -0800124 }
Michael Kolb66706532010-12-12 19:50:22 -0800125 }
126
127 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000128 public void onResume() {
129 super.onResume();
130 if (!BrowserSettings.getInstance().useInstant()) {
131 mTitleBar.clearCompletions();
132 }
133 }
134
135 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800136 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800137 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800138 }
139
140 // webview factory
141
142 @Override
143 public WebView createWebView(boolean privateBrowsing) {
144 // Create a new WebView
John Reckb9a051b2011-03-18 11:55:48 -0700145 BrowserWebView w = (BrowserWebView) super.createWebView(privateBrowsing);
Michael Kolb66706532010-12-12 19:50:22 -0800146 w.setScrollListener(this);
Michael Kolb66706532010-12-12 19:50:22 -0800147 return w;
148 }
149
150 @Override
151 public WebView createSubWebView(boolean privateBrowsing) {
John Reckb9a051b2011-03-18 11:55:48 -0700152 return super.createWebView(privateBrowsing);
Michael Kolb66706532010-12-12 19:50:22 -0800153 }
154
155 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800156 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
157 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800158 }
159
160 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700161 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800162 if (web != null) {
163 web.stopScroll();
164 }
165 }
166
167 // WebView callbacks
168
169 @Override
John Reck30c714c2010-12-16 17:30:34 -0800170 public void onProgressChanged(Tab tab) {
171 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800172 mTabBar.onProgress(tab, progress);
173 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800174 mTitleBar.setProgress(progress);
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);
Michael Kolbeb95db42011-03-03 10:38:40 -0800191 if (mUseQuickControls) {
192 if (mActiveTab != null) {
193 captureTab(mActiveTab);
194 }
195 }
Michael Kolbf2628922011-03-09 17:15:28 -0800196 super.setActiveTab(tab, true);
197 setActiveTab(tab, true);
Michael Kolb46f987e2011-04-05 10:39:10 -0700198 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb377ea312011-02-17 14:36:56 -0800199 }
200
201 @Override
202 void setActiveTab(Tab tab, boolean needsAttaching) {
John Reckb9a051b2011-03-18 11:55:48 -0700203 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800204 // TabControl.setCurrentTab has been called before this,
205 // so the tab is guaranteed to have a webview
206 if (view == null) {
207 Log.e(LOGTAG, "active tab with no webview detected");
208 return;
209 }
210 // Request focus on the top window.
211 if (mUseQuickControls) {
212 mPieControl.forceToTop(mContentView);
213 view.setScrollListener(null);
214 mTabBar.showTitleBarIndicator(false);
215 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800216 // check if title bar is already attached by animation
217 if (mTitleBar.getParent() == null) {
218 view.setEmbeddedTitleBar(mTitleBar);
219 }
Michael Kolb376b5412010-12-15 11:52:57 -0800220 view.setScrollListener(this);
221 }
Michael Kolb66706532010-12-12 19:50:22 -0800222 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800223 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700224 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800225 } else {
226 revertVoiceTitleBar(tab);
227 }
Michael Kolb376b5412010-12-15 11:52:57 -0800228 updateLockIconToLatest(tab);
229 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231
232 @Override
233 public void updateTabs(List<Tab> tabs) {
234 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800235 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
238 @Override
239 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700240 mTitleBar.cancelTitleBarAnimation(true);
241 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800242 super.removeTab(tab);
243 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700244 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800245 }
246
247 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800248 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800249 }
250
Michael Kolb376b5412010-12-15 11:52:57 -0800251 int getContentWidth() {
252 if (mContentView != null) {
253 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800254 }
255 return 0;
256 }
257
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800258 @Override
259 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700260 if (mUseQuickControls) {
261 getTitleBar().setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800262 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700263 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800264 }
265
266 void stopEditingUrl() {
267 mTitleBar.stopEditingUrl();
268 }
269
270 @Override
271 protected void showTitleBar() {
272 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700273 mTitleBar.show();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800274 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800275 }
Michael Kolb376b5412010-12-15 11:52:57 -0800276 }
277
Michael Kolb66706532010-12-12 19:50:22 -0800278 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800279 protected void hideTitleBar() {
280 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800281 mTabBar.onHideTitleBar();
Michael Kolb46f987e2011-04-05 10:39:10 -0700282 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800283 }
284 }
285
Michael Kolb5ee018e2011-02-18 15:47:21 -0800286 public boolean isEditingUrl() {
287 return mTitleBar.isEditingUrl();
288 }
289
Michael Kolb66706532010-12-12 19:50:22 -0800290 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800291 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800292 return mTitleBar;
293 }
294
Michael Kolb2a56eca2011-02-25 09:45:06 -0800295 @Override
296 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700297 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800298 super.setTitleGravity(gravity);
299 }
300 }
301
Michael Kolb66706532010-12-12 19:50:22 -0800302 // action mode callbacks
303
304 @Override
305 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800306 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800307 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800308 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800309 }
310 }
311
312 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800313 public void onActionModeFinished(boolean inLoad) {
314 checkTabCount();
315 if (inLoad) {
316 // the titlebar was removed when the CAB was shown
317 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800318 if (mUseQuickControls) {
319 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800320 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800321 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800322 }
323 }
324
325 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800326 protected void updateNavigationState(Tab tab) {
327 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800328 }
329
330 @Override
John Reck30c714c2010-12-16 17:30:34 -0800331 public void setUrlTitle(Tab tab) {
332 super.setUrlTitle(tab);
333 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800334 }
335
336 // Set the favicon in the title bar.
337 @Override
John Reck30c714c2010-12-16 17:30:34 -0800338 public void setFavicon(Tab tab) {
339 super.setFavicon(tab);
340 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800341 }
342
Michael Kolbcfa3af52010-12-14 10:36:11 -0800343 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700344 public void showVoiceTitleBar(String title, List<String> vsresults) {
Michael Kolb3a2a1642011-02-15 10:52:07 -0800345 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800346 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800347 }
348
349 @Override
350 public void revertVoiceTitleBar(Tab tab) {
351 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800352 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800353 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800354 }
355
John Reckd73c5a22010-12-22 10:22:50 -0800356 @Override
357 public void showCustomView(View view, CustomViewCallback callback) {
358 super.showCustomView(view, callback);
359 mActivity.getActionBar().hide();
360 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800361
John Reckd73c5a22010-12-22 10:22:50 -0800362 @Override
363 public void onHideCustomView() {
364 super.onHideCustomView();
365 if (mUseQuickControls) {
366 checkTabCount();
367 } else {
368 mActivity.getActionBar().show();
369 }
370 }
Michael Kolba4183062011-01-16 10:43:21 -0800371
372 @Override
373 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800374 if (mActiveTab != null) {
375 WebView web = mActiveTab.getWebView();
376 if (event.getAction() == KeyEvent.ACTION_DOWN) {
377 switch (code) {
378 case KeyEvent.KEYCODE_TAB:
379 case KeyEvent.KEYCODE_DPAD_UP:
380 case KeyEvent.KEYCODE_DPAD_LEFT:
381 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
382 editUrl(false);
383 return true;
384 }
385 }
386 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
387 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
388 editUrl(true);
389 return mContentView.dispatchKeyEvent(event);
390 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800391 }
Michael Kolba4183062011-01-16 10:43:21 -0800392 }
393 return false;
394 }
395
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800396 private boolean isTypingKey(KeyEvent evt) {
397 return evt.getUnicodeChar() > 0;
398 }
399
400 TabBar getTabBar() {
401 return mTabBar;
402 }
403
Narayan Kamath5119edd2011-02-23 15:49:17 +0000404 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800405 public boolean onPrepareOptionsMenu(Menu menu) {
406 if (mUseQuickControls) {
407 mPieControl.onMenuOpened(menu);
408 return false;
409 } else {
410 return true;
411 }
412 }
413
Michael Kolb66706532010-12-12 19:50:22 -0800414}