blob: 892b1187cc67cb2f5c391d89e8b03d4d8ca73bd1 [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
19import com.android.browser.ScrollWebView.ScrollListener;
20
21import android.app.ActionBar;
22import android.app.Activity;
Michael Kolb24915222011-02-24 11:38:49 -080023import android.content.pm.PackageManager;
Michael Kolbac35bdc2011-01-17 17:06:04 -080024import android.os.Bundle;
Michael Kolbba238702011-03-08 10:40:50 -080025import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080026import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080027import android.view.ActionMode;
Michael Kolb376b5412010-12-15 11:52:57 -080028import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080029import android.view.KeyEvent;
Michael Kolb1acef692011-03-08 14:12:06 -080030import android.view.Menu;
John Reckd73c5a22010-12-22 10:22:50 -080031import android.view.View;
32import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080033import android.webkit.WebView;
Michael Kolb2a56eca2011-02-25 09:45:06 -080034import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080035
36import java.util.List;
37
38/**
39 * Ui for xlarge screen sizes
40 */
41public class XLargeUi extends BaseUi implements ScrollListener {
42
43 private static final String LOGTAG = "XLargeUi";
44
Michael Kolb376b5412010-12-15 11:52:57 -080045 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080046 private TabBar mTabBar;
47
48 private TitleBarXLarge mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080049
Michael Kolb376b5412010-12-15 11:52:57 -080050 private boolean mUseQuickControls;
51 private PieControl mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080052 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080053
Michael Kolb66706532010-12-12 19:50:22 -080054 /**
55 * @param browser
56 * @param controller
57 */
58 public XLargeUi(Activity browser, UiController controller) {
59 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080060 mHandler = new Handler();
Michael Kolb66706532010-12-12 19:50:22 -080061 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
62 mTitleBar.setProgress(100);
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
75 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080076 public void showComboView(boolean startWithHistory, Bundle extras) {
77 super.showComboView(startWithHistory, extras);
78 if (mUseQuickControls) {
79 mActionBar.show();
80 }
81 }
82
83 @Override
John Reckb3417f02011-01-14 11:01:05 -080084 public void hideComboView() {
Michael Kolbba238702011-03-08 10:40:50 -080085 if (isComboViewShowing()) {
86 super.hideComboView();
87 // ComboView changes the action bar, set it back up to what we want
88 setupActionBar();
89 checkTabCount();
90 }
Michael Kolb376b5412010-12-15 11:52:57 -080091 }
92
93 private void setUseQuickControls(boolean useQuickControls) {
94 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080095 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080096 if (useQuickControls) {
97 checkTabCount();
98 mPieControl = new PieControl(mActivity, mUiController, this);
99 mPieControl.attachToContainer(mContentView);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800100 Tab tab = getActiveTab();
101 if ((tab != null) && (tab.getWebView() != null)) {
102 tab.getWebView().setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -0800103 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800104 setTitleGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800105 } else {
106 mActivity.getActionBar().show();
107 if (mPieControl != null) {
108 mPieControl.removeFromContainer(mContentView);
109 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800110 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800111 WebView web = mTabControl.getCurrentWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800112 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800113 web.setEmbeddedTitleBar(mTitleBar);
114 }
115 }
116 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800117 }
118
119 private void checkTabCount() {
120 if (mUseQuickControls) {
Michael Kolb1acef692011-03-08 14:12:06 -0800121 if (mTabControl.getTabCount() == 1) {
Michael Kolbba238702011-03-08 10:40:50 -0800122 mHandler.post(new Runnable() {
123 public void run() {
124 mActionBar.hide();
125 }
126 });
Michael Kolb1acef692011-03-08 14:12:06 -0800127 } else {
128 mActionBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -0800129 }
130 }
Michael Kolb66706532010-12-12 19:50:22 -0800131 }
132
133 @Override
134 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800135 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800136 }
137
138 // webview factory
139
140 @Override
141 public WebView createWebView(boolean privateBrowsing) {
142 // Create a new WebView
143 ScrollWebView w = new ScrollWebView(mActivity, null,
144 android.R.attr.webViewStyle, privateBrowsing);
145 initWebViewSettings(w);
146 w.setScrollListener(this);
Michael Kolb24915222011-02-24 11:38:49 -0800147 boolean supportsMultiTouch = mActivity.getPackageManager()
148 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
149 w.getSettings().setDisplayZoomControls(!supportsMultiTouch);
Dave Burke20b6f1f2011-02-01 12:59:04 +0000150 w.setExpandedTileBounds(true); // smoother scrolling
Michael Kolb66706532010-12-12 19:50:22 -0800151 return w;
152 }
153
154 @Override
155 public WebView createSubWebView(boolean privateBrowsing) {
156 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
157 // no scroll listener for subview
158 web.setScrollListener(null);
159 return web;
160 }
161
162 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800163 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
164 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800165 }
166
167 void stopWebViewScrolling() {
168 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
169 if (web != null) {
170 web.stopScroll();
171 }
172 }
173
174 // WebView callbacks
175
176 @Override
John Reck30c714c2010-12-16 17:30:34 -0800177 public void onProgressChanged(Tab tab) {
178 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800179 mTabBar.onProgress(tab, progress);
180 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800181 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800182 if (progress == 100) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800183 if (!mTitleBar.isEditingUrl()) {
184 hideTitleBar();
Michael Kolbbd018d42010-12-15 15:43:39 -0800185 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800186 mTitleBar.setShowProgressOnly(false);
187 setTitleGravity(Gravity.BOTTOM);
Michael Kolbbd018d42010-12-15 15:43:39 -0800188 }
Michael Kolb376b5412010-12-15 11:52:57 -0800189 }
Michael Kolb66706532010-12-12 19:50:22 -0800190 } else {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800191 if (!isTitleBarShowing()) {
192 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
193 mTitleBar.setShowProgressOnly(true);
194 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800195 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800196 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800197 }
Michael Kolb66706532010-12-12 19:50:22 -0800198 }
199 }
200 }
201
202 @Override
203 public boolean needsRestoreAllTabs() {
204 return true;
205 }
206
207 @Override
208 public void addTab(Tab tab) {
209 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800210 }
211
212 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800213 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800214 }
215
216 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800217 public void setActiveTab(final Tab tab) {
Michael Kolbf2628922011-03-09 17:15:28 -0800218 super.setActiveTab(tab, true);
219 setActiveTab(tab, true);
Michael Kolb377ea312011-02-17 14:36:56 -0800220 }
221
222 @Override
223 void setActiveTab(Tab tab, boolean needsAttaching) {
Michael Kolb376b5412010-12-15 11:52:57 -0800224 ScrollWebView view = (ScrollWebView) tab.getWebView();
225 // TabControl.setCurrentTab has been called before this,
226 // so the tab is guaranteed to have a webview
227 if (view == null) {
228 Log.e(LOGTAG, "active tab with no webview detected");
229 return;
230 }
231 // Request focus on the top window.
232 if (mUseQuickControls) {
233 mPieControl.forceToTop(mContentView);
234 view.setScrollListener(null);
235 mTabBar.showTitleBarIndicator(false);
236 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800237 // check if title bar is already attached by animation
238 if (mTitleBar.getParent() == null) {
239 view.setEmbeddedTitleBar(mTitleBar);
240 }
Michael Kolb376b5412010-12-15 11:52:57 -0800241 view.setScrollListener(this);
242 }
Michael Kolb66706532010-12-12 19:50:22 -0800243 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800244 if (tab.isInVoiceSearchMode()) {
245 showVoiceTitleBar(tab.getVoiceDisplayTitle());
246 } else {
247 revertVoiceTitleBar(tab);
248 }
Michael Kolb376b5412010-12-15 11:52:57 -0800249 updateLockIconToLatest(tab);
250 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800251 }
252
253 @Override
254 public void updateTabs(List<Tab> tabs) {
255 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800256 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800257 }
258
259 @Override
260 public void removeTab(Tab tab) {
261 super.removeTab(tab);
262 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800263 }
264
265 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800266 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800267 }
268
Michael Kolb376b5412010-12-15 11:52:57 -0800269 int getContentWidth() {
270 if (mContentView != null) {
271 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800272 }
273 return 0;
274 }
275
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800276 @Override
277 public void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800278 if (mUiController.isInCustomActionMode()) {
279 mUiController.endActionMode();
280 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800281 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800282 mTitleBar.startEditingUrl(clearInput);
Michael Kolb66706532010-12-12 19:50:22 -0800283 }
284
Michael Kolb7cdc4902011-02-03 17:54:40 -0800285 void showTitleBarAndEdit() {
286 mTitleBar.setShowProgressOnly(false);
287 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800288 mTitleBar.startEditingUrl(false);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800289 }
290
291 void stopEditingUrl() {
292 mTitleBar.stopEditingUrl();
293 }
294
295 @Override
296 protected void showTitleBar() {
297 if (canShowTitleBar()) {
298 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800299 mContentView.addView(mTitleBar);
300 } else {
301 setTitleGravity(Gravity.TOP);
302 }
303 super.showTitleBar();
304 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800305 }
Michael Kolb376b5412010-12-15 11:52:57 -0800306 }
307
Michael Kolb66706532010-12-12 19:50:22 -0800308 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800309 protected void hideTitleBar() {
310 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800311 mTabBar.onHideTitleBar();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800312 if (mUseQuickControls) {
313 mContentView.removeView(mTitleBar);
Michael Kolb2a56eca2011-02-25 09:45:06 -0800314 } else {
315 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800316 }
317 super.hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800318 }
319 }
320
Michael Kolb5ee018e2011-02-18 15:47:21 -0800321 public boolean isEditingUrl() {
322 return mTitleBar.isEditingUrl();
323 }
324
Michael Kolb66706532010-12-12 19:50:22 -0800325 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800326 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800327 return mTitleBar;
328 }
329
Michael Kolb2a56eca2011-02-25 09:45:06 -0800330 @Override
331 protected void setTitleGravity(int gravity) {
332 if (mUseQuickControls) {
333 FrameLayout.LayoutParams lp =
334 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
335 lp.gravity = gravity;
336 mTitleBar.setLayoutParams(lp);
337 } else {
338 super.setTitleGravity(gravity);
339 }
340 }
341
Michael Kolb66706532010-12-12 19:50:22 -0800342 // action mode callbacks
343
344 @Override
345 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800346 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800347 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800348 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800349 }
350 }
351
352 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800353 public void onActionModeFinished(boolean inLoad) {
354 checkTabCount();
355 if (inLoad) {
356 // the titlebar was removed when the CAB was shown
357 // if the page is loading, show it again
Michael Kolb7cdc4902011-02-03 17:54:40 -0800358 mTitleBar.setShowProgressOnly(true);
359 if (!isTitleBarShowing()) {
360 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800361 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800362 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800363 }
364 }
365
366 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800367 protected void updateNavigationState(Tab tab) {
368 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800369 }
370
371 @Override
John Reck30c714c2010-12-16 17:30:34 -0800372 public void setUrlTitle(Tab tab) {
373 super.setUrlTitle(tab);
374 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800375 }
376
377 // Set the favicon in the title bar.
378 @Override
John Reck30c714c2010-12-16 17:30:34 -0800379 public void setFavicon(Tab tab) {
380 super.setFavicon(tab);
381 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800382 }
383
Michael Kolbcfa3af52010-12-14 10:36:11 -0800384 @Override
385 public void showVoiceTitleBar(String title) {
386 List<String> vsresults = null;
387 if (getActiveTab() != null) {
388 vsresults = getActiveTab().getVoiceSearchResults();
389 }
Michael Kolb3a2a1642011-02-15 10:52:07 -0800390 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800391 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800392 }
393
394 @Override
395 public void revertVoiceTitleBar(Tab tab) {
396 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800397 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800398 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800399 }
400
John Reckd73c5a22010-12-22 10:22:50 -0800401 @Override
402 public void showCustomView(View view, CustomViewCallback callback) {
403 super.showCustomView(view, callback);
404 mActivity.getActionBar().hide();
405 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800406
John Reckd73c5a22010-12-22 10:22:50 -0800407 @Override
408 public void onHideCustomView() {
409 super.onHideCustomView();
410 if (mUseQuickControls) {
411 checkTabCount();
412 } else {
413 mActivity.getActionBar().show();
414 }
415 }
Michael Kolba4183062011-01-16 10:43:21 -0800416
417 @Override
418 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800419 if (mActiveTab != null) {
420 WebView web = mActiveTab.getWebView();
421 if (event.getAction() == KeyEvent.ACTION_DOWN) {
422 switch (code) {
423 case KeyEvent.KEYCODE_TAB:
424 case KeyEvent.KEYCODE_DPAD_UP:
425 case KeyEvent.KEYCODE_DPAD_LEFT:
426 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
427 editUrl(false);
428 return true;
429 }
430 }
431 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
432 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
433 editUrl(true);
434 return mContentView.dispatchKeyEvent(event);
435 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800436 }
Michael Kolba4183062011-01-16 10:43:21 -0800437 }
438 return false;
439 }
440
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800441 private boolean isTypingKey(KeyEvent evt) {
442 return evt.getUnicodeChar() > 0;
443 }
444
445 TabBar getTabBar() {
446 return mTabBar;
447 }
448
Narayan Kamath5119edd2011-02-23 15:49:17 +0000449 @Override
450 public void registerDropdownChangeListener(DropdownChangeListener d) {
451 mTitleBar.registerDropdownChangeListener(d);
452 }
Michael Kolb0860d992011-03-07 15:26:33 -0800453
Michael Kolb1acef692011-03-08 14:12:06 -0800454 @Override
455 public boolean onPrepareOptionsMenu(Menu menu) {
456 if (mUseQuickControls) {
457 mPieControl.onMenuOpened(menu);
458 return false;
459 } else {
460 return true;
461 }
462 }
463
Michael Kolb66706532010-12-12 19:50:22 -0800464}