blob: 566f7d1312cab5e14feb041237c7e4f05db2f88e [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
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000134 public void onResume() {
135 super.onResume();
136 if (!BrowserSettings.getInstance().useInstant()) {
137 mTitleBar.clearCompletions();
138 }
139 }
140
141 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800142 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800143 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800144 }
145
146 // webview factory
147
148 @Override
149 public WebView createWebView(boolean privateBrowsing) {
150 // Create a new WebView
151 ScrollWebView w = new ScrollWebView(mActivity, null,
152 android.R.attr.webViewStyle, privateBrowsing);
153 initWebViewSettings(w);
154 w.setScrollListener(this);
Michael Kolb24915222011-02-24 11:38:49 -0800155 boolean supportsMultiTouch = mActivity.getPackageManager()
156 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
157 w.getSettings().setDisplayZoomControls(!supportsMultiTouch);
Dave Burke20b6f1f2011-02-01 12:59:04 +0000158 w.setExpandedTileBounds(true); // smoother scrolling
Michael Kolb66706532010-12-12 19:50:22 -0800159 return w;
160 }
161
162 @Override
163 public WebView createSubWebView(boolean privateBrowsing) {
164 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
165 // no scroll listener for subview
166 web.setScrollListener(null);
167 return web;
168 }
169
170 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800171 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
172 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800173 }
174
175 void stopWebViewScrolling() {
176 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
177 if (web != null) {
178 web.stopScroll();
179 }
180 }
181
182 // WebView callbacks
183
184 @Override
John Reck30c714c2010-12-16 17:30:34 -0800185 public void onProgressChanged(Tab tab) {
186 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800187 mTabBar.onProgress(tab, progress);
188 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800189 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800190 if (progress == 100) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800191 if (!mTitleBar.isEditingUrl()) {
192 hideTitleBar();
Michael Kolbbd018d42010-12-15 15:43:39 -0800193 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800194 mTitleBar.setShowProgressOnly(false);
195 setTitleGravity(Gravity.BOTTOM);
Michael Kolbbd018d42010-12-15 15:43:39 -0800196 }
Michael Kolb376b5412010-12-15 11:52:57 -0800197 }
Michael Kolb66706532010-12-12 19:50:22 -0800198 } else {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800199 if (!isTitleBarShowing()) {
200 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
201 mTitleBar.setShowProgressOnly(true);
202 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800203 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800204 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800205 }
Michael Kolb66706532010-12-12 19:50:22 -0800206 }
207 }
208 }
209
210 @Override
211 public boolean needsRestoreAllTabs() {
212 return true;
213 }
214
215 @Override
216 public void addTab(Tab tab) {
217 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800218 }
219
220 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800221 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800222 }
223
224 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800225 public void setActiveTab(final Tab tab) {
Michael Kolbf2628922011-03-09 17:15:28 -0800226 super.setActiveTab(tab, true);
227 setActiveTab(tab, true);
Michael Kolb377ea312011-02-17 14:36:56 -0800228 }
229
230 @Override
231 void setActiveTab(Tab tab, boolean needsAttaching) {
Michael Kolb376b5412010-12-15 11:52:57 -0800232 ScrollWebView view = (ScrollWebView) tab.getWebView();
233 // TabControl.setCurrentTab has been called before this,
234 // so the tab is guaranteed to have a webview
235 if (view == null) {
236 Log.e(LOGTAG, "active tab with no webview detected");
237 return;
238 }
239 // Request focus on the top window.
240 if (mUseQuickControls) {
241 mPieControl.forceToTop(mContentView);
242 view.setScrollListener(null);
243 mTabBar.showTitleBarIndicator(false);
244 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800245 // check if title bar is already attached by animation
246 if (mTitleBar.getParent() == null) {
247 view.setEmbeddedTitleBar(mTitleBar);
248 }
Michael Kolb376b5412010-12-15 11:52:57 -0800249 view.setScrollListener(this);
250 }
Michael Kolb66706532010-12-12 19:50:22 -0800251 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800252 if (tab.isInVoiceSearchMode()) {
253 showVoiceTitleBar(tab.getVoiceDisplayTitle());
254 } else {
255 revertVoiceTitleBar(tab);
256 }
Michael Kolb376b5412010-12-15 11:52:57 -0800257 updateLockIconToLatest(tab);
258 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800259 }
260
261 @Override
262 public void updateTabs(List<Tab> tabs) {
263 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800264 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800265 }
266
267 @Override
268 public void removeTab(Tab tab) {
269 super.removeTab(tab);
270 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800271 }
272
273 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800274 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800275 }
276
Michael Kolb376b5412010-12-15 11:52:57 -0800277 int getContentWidth() {
278 if (mContentView != null) {
279 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800280 }
281 return 0;
282 }
283
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800284 @Override
285 public void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800286 if (mUiController.isInCustomActionMode()) {
287 mUiController.endActionMode();
288 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800289 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800290 mTitleBar.startEditingUrl(clearInput);
Michael Kolb66706532010-12-12 19:50:22 -0800291 }
292
Michael Kolb7cdc4902011-02-03 17:54:40 -0800293 void showTitleBarAndEdit() {
294 mTitleBar.setShowProgressOnly(false);
295 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800296 mTitleBar.startEditingUrl(false);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800297 }
298
299 void stopEditingUrl() {
300 mTitleBar.stopEditingUrl();
301 }
302
303 @Override
304 protected void showTitleBar() {
305 if (canShowTitleBar()) {
306 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800307 mContentView.addView(mTitleBar);
308 } else {
309 setTitleGravity(Gravity.TOP);
310 }
311 super.showTitleBar();
312 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800313 }
Michael Kolb376b5412010-12-15 11:52:57 -0800314 }
315
Michael Kolb66706532010-12-12 19:50:22 -0800316 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800317 protected void hideTitleBar() {
318 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800319 mTabBar.onHideTitleBar();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800320 if (mUseQuickControls) {
321 mContentView.removeView(mTitleBar);
Michael Kolb2a56eca2011-02-25 09:45:06 -0800322 } else {
323 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800324 }
325 super.hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800326 }
327 }
328
Michael Kolb5ee018e2011-02-18 15:47:21 -0800329 public boolean isEditingUrl() {
330 return mTitleBar.isEditingUrl();
331 }
332
Michael Kolb66706532010-12-12 19:50:22 -0800333 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800334 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800335 return mTitleBar;
336 }
337
Michael Kolb2a56eca2011-02-25 09:45:06 -0800338 @Override
339 protected void setTitleGravity(int gravity) {
340 if (mUseQuickControls) {
341 FrameLayout.LayoutParams lp =
342 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
343 lp.gravity = gravity;
344 mTitleBar.setLayoutParams(lp);
345 } else {
346 super.setTitleGravity(gravity);
347 }
348 }
349
Michael Kolb66706532010-12-12 19:50:22 -0800350 // action mode callbacks
351
352 @Override
353 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800354 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800355 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800356 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800357 }
358 }
359
360 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800361 public void onActionModeFinished(boolean inLoad) {
362 checkTabCount();
363 if (inLoad) {
364 // the titlebar was removed when the CAB was shown
365 // if the page is loading, show it again
Michael Kolb7cdc4902011-02-03 17:54:40 -0800366 mTitleBar.setShowProgressOnly(true);
367 if (!isTitleBarShowing()) {
368 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800369 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800370 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800371 }
372 }
373
374 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800375 protected void updateNavigationState(Tab tab) {
376 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800377 }
378
379 @Override
Patrick Scott92066772011-03-10 08:46:27 -0500380 protected void updateAutoLogin(Tab tab, boolean animate) {
381 mTitleBar.updateAutoLogin(tab, animate);
382 }
383
384 @Override
John Reck30c714c2010-12-16 17:30:34 -0800385 public void setUrlTitle(Tab tab) {
386 super.setUrlTitle(tab);
387 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800388 }
389
390 // Set the favicon in the title bar.
391 @Override
John Reck30c714c2010-12-16 17:30:34 -0800392 public void setFavicon(Tab tab) {
393 super.setFavicon(tab);
394 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800395 }
396
Michael Kolbcfa3af52010-12-14 10:36:11 -0800397 @Override
398 public void showVoiceTitleBar(String title) {
399 List<String> vsresults = null;
400 if (getActiveTab() != null) {
401 vsresults = getActiveTab().getVoiceSearchResults();
402 }
Michael Kolb3a2a1642011-02-15 10:52:07 -0800403 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800404 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800405 }
406
407 @Override
408 public void revertVoiceTitleBar(Tab tab) {
409 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800410 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800411 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800412 }
413
John Reckd73c5a22010-12-22 10:22:50 -0800414 @Override
415 public void showCustomView(View view, CustomViewCallback callback) {
416 super.showCustomView(view, callback);
417 mActivity.getActionBar().hide();
418 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800419
John Reckd73c5a22010-12-22 10:22:50 -0800420 @Override
421 public void onHideCustomView() {
422 super.onHideCustomView();
423 if (mUseQuickControls) {
424 checkTabCount();
425 } else {
426 mActivity.getActionBar().show();
427 }
428 }
Michael Kolba4183062011-01-16 10:43:21 -0800429
430 @Override
431 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800432 if (mActiveTab != null) {
433 WebView web = mActiveTab.getWebView();
434 if (event.getAction() == KeyEvent.ACTION_DOWN) {
435 switch (code) {
436 case KeyEvent.KEYCODE_TAB:
437 case KeyEvent.KEYCODE_DPAD_UP:
438 case KeyEvent.KEYCODE_DPAD_LEFT:
439 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
440 editUrl(false);
441 return true;
442 }
443 }
444 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
445 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
446 editUrl(true);
447 return mContentView.dispatchKeyEvent(event);
448 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800449 }
Michael Kolba4183062011-01-16 10:43:21 -0800450 }
451 return false;
452 }
453
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800454 private boolean isTypingKey(KeyEvent evt) {
455 return evt.getUnicodeChar() > 0;
456 }
457
458 TabBar getTabBar() {
459 return mTabBar;
460 }
461
Narayan Kamath5119edd2011-02-23 15:49:17 +0000462 @Override
463 public void registerDropdownChangeListener(DropdownChangeListener d) {
464 mTitleBar.registerDropdownChangeListener(d);
465 }
Michael Kolb0860d992011-03-07 15:26:33 -0800466
Michael Kolb1acef692011-03-08 14:12:06 -0800467 @Override
468 public boolean onPrepareOptionsMenu(Menu menu) {
469 if (mUseQuickControls) {
470 mPieControl.onMenuOpened(menu);
471 return false;
472 } else {
473 return true;
474 }
475 }
476
Michael Kolb66706532010-12-12 19:50:22 -0800477}