blob: a9a55e8a5de6860e4bfecc40b38d85d43a5d8e86 [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 Kolbeb95db42011-03-03 10:38:40 -080024import android.graphics.Bitmap;
Michael Kolbac35bdc2011-01-17 17:06:04 -080025import android.os.Bundle;
Michael Kolbba238702011-03-08 10:40:50 -080026import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080027import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080028import android.view.ActionMode;
Michael Kolb376b5412010-12-15 11:52:57 -080029import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080030import android.view.KeyEvent;
Michael Kolb1acef692011-03-08 14:12:06 -080031import android.view.Menu;
John Reckd73c5a22010-12-22 10:22:50 -080032import android.view.View;
33import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080034import android.webkit.WebView;
Michael Kolb2a56eca2011-02-25 09:45:06 -080035import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080036
37import java.util.List;
38
39/**
40 * Ui for xlarge screen sizes
41 */
42public class XLargeUi extends BaseUi implements ScrollListener {
43
44 private static final String LOGTAG = "XLargeUi";
45
Michael Kolb376b5412010-12-15 11:52:57 -080046 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080047 private TabBar mTabBar;
48
49 private TitleBarXLarge mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080050
Michael Kolb376b5412010-12-15 11:52:57 -080051 private boolean mUseQuickControls;
52 private PieControl mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080053 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080054
Michael Kolb66706532010-12-12 19:50:22 -080055 /**
56 * @param browser
57 * @param controller
58 */
59 public XLargeUi(Activity browser, UiController controller) {
60 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080061 mHandler = new Handler();
Michael Kolb66706532010-12-12 19:50:22 -080062 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
63 mTitleBar.setProgress(100);
Michael Kolb66706532010-12-12 19:50:22 -080064 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080065 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080066 setupActionBar();
67 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
68 }
69
70 private void setupActionBar() {
71 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080072 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
73 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080074 }
75
76 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080077 public void showComboView(boolean startWithHistory, Bundle extras) {
78 super.showComboView(startWithHistory, extras);
79 if (mUseQuickControls) {
80 mActionBar.show();
81 }
82 }
83
84 @Override
John Reckb3417f02011-01-14 11:01:05 -080085 public void hideComboView() {
Michael Kolbba238702011-03-08 10:40:50 -080086 if (isComboViewShowing()) {
87 super.hideComboView();
88 // ComboView changes the action bar, set it back up to what we want
89 setupActionBar();
90 checkTabCount();
91 }
Michael Kolb376b5412010-12-15 11:52:57 -080092 }
93
94 private void setUseQuickControls(boolean useQuickControls) {
95 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080096 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080097 if (useQuickControls) {
98 checkTabCount();
99 mPieControl = new PieControl(mActivity, mUiController, this);
100 mPieControl.attachToContainer(mContentView);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800101 Tab tab = getActiveTab();
102 if ((tab != null) && (tab.getWebView() != null)) {
103 tab.getWebView().setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -0800104 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800105 setTitleGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800106 } else {
107 mActivity.getActionBar().show();
108 if (mPieControl != null) {
109 mPieControl.removeFromContainer(mContentView);
110 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800111 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800112 WebView web = mTabControl.getCurrentWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800113 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800114 web.setEmbeddedTitleBar(mTitleBar);
115 }
116 }
117 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800118 }
119
120 private void checkTabCount() {
121 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800122 mHandler.post(new Runnable() {
123 public void run() {
124 mActionBar.hide();
125 }
126 });
Michael Kolb376b5412010-12-15 11:52:57 -0800127 }
Michael Kolb66706532010-12-12 19:50:22 -0800128 }
129
130 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000131 public void onResume() {
132 super.onResume();
133 if (!BrowserSettings.getInstance().useInstant()) {
134 mTitleBar.clearCompletions();
135 }
136 }
137
138 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800139 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800140 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800141 }
142
143 // webview factory
144
145 @Override
146 public WebView createWebView(boolean privateBrowsing) {
147 // Create a new WebView
148 ScrollWebView w = new ScrollWebView(mActivity, null,
149 android.R.attr.webViewStyle, privateBrowsing);
150 initWebViewSettings(w);
151 w.setScrollListener(this);
Michael Kolb24915222011-02-24 11:38:49 -0800152 boolean supportsMultiTouch = mActivity.getPackageManager()
153 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
154 w.getSettings().setDisplayZoomControls(!supportsMultiTouch);
Dave Burke20b6f1f2011-02-01 12:59:04 +0000155 w.setExpandedTileBounds(true); // smoother scrolling
Michael Kolb66706532010-12-12 19:50:22 -0800156 return w;
157 }
158
159 @Override
160 public WebView createSubWebView(boolean privateBrowsing) {
161 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
162 // no scroll listener for subview
163 web.setScrollListener(null);
164 return web;
165 }
166
167 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800168 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
169 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800170 }
171
172 void stopWebViewScrolling() {
173 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
174 if (web != null) {
175 web.stopScroll();
176 }
177 }
178
179 // WebView callbacks
180
181 @Override
John Reck30c714c2010-12-16 17:30:34 -0800182 public void onProgressChanged(Tab tab) {
183 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800184 mTabBar.onProgress(tab, progress);
185 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800186 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800187 if (progress == 100) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800188 if (!mTitleBar.isEditingUrl()) {
189 hideTitleBar();
Michael Kolbbd018d42010-12-15 15:43:39 -0800190 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800191 mTitleBar.setShowProgressOnly(false);
192 setTitleGravity(Gravity.BOTTOM);
Michael Kolbbd018d42010-12-15 15:43:39 -0800193 }
Michael Kolb376b5412010-12-15 11:52:57 -0800194 }
Michael Kolb66706532010-12-12 19:50:22 -0800195 } else {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800196 if (!isTitleBarShowing()) {
197 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
198 mTitleBar.setShowProgressOnly(true);
199 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800200 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800201 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800202 }
Michael Kolb66706532010-12-12 19:50:22 -0800203 }
204 }
205 }
206
207 @Override
208 public boolean needsRestoreAllTabs() {
209 return true;
210 }
211
212 @Override
213 public void addTab(Tab tab) {
214 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800215 }
216
217 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800218 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800219 }
220
221 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800222 public void setActiveTab(final Tab tab) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800223 if (mUseQuickControls) {
224 if (mActiveTab != null) {
225 captureTab(mActiveTab);
226 }
227 }
Michael Kolbf2628922011-03-09 17:15:28 -0800228 super.setActiveTab(tab, true);
229 setActiveTab(tab, true);
Michael Kolb377ea312011-02-17 14:36:56 -0800230 }
231
232 @Override
233 void setActiveTab(Tab tab, boolean needsAttaching) {
Michael Kolb376b5412010-12-15 11:52:57 -0800234 ScrollWebView view = (ScrollWebView) tab.getWebView();
235 // TabControl.setCurrentTab has been called before this,
236 // so the tab is guaranteed to have a webview
237 if (view == null) {
238 Log.e(LOGTAG, "active tab with no webview detected");
239 return;
240 }
241 // Request focus on the top window.
242 if (mUseQuickControls) {
243 mPieControl.forceToTop(mContentView);
244 view.setScrollListener(null);
245 mTabBar.showTitleBarIndicator(false);
246 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800247 // check if title bar is already attached by animation
248 if (mTitleBar.getParent() == null) {
249 view.setEmbeddedTitleBar(mTitleBar);
250 }
Michael Kolb376b5412010-12-15 11:52:57 -0800251 view.setScrollListener(this);
252 }
Michael Kolb66706532010-12-12 19:50:22 -0800253 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800254 if (tab.isInVoiceSearchMode()) {
255 showVoiceTitleBar(tab.getVoiceDisplayTitle());
256 } else {
257 revertVoiceTitleBar(tab);
258 }
Michael Kolb376b5412010-12-15 11:52:57 -0800259 updateLockIconToLatest(tab);
260 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800261 }
262
Michael Kolbeb95db42011-03-03 10:38:40 -0800263 public void captureTab(final Tab tab) {
264 Bitmap sshot = Controller.createScreenshot(tab,
265 (int) mActivity.getResources()
266 .getDimension(R.dimen.qc_thumb_width),
267 (int) mActivity.getResources()
268 .getDimension(R.dimen.qc_thumb_height));
269 tab.setScreenshot(sshot);
270 }
271
Michael Kolb66706532010-12-12 19:50:22 -0800272 @Override
273 public void updateTabs(List<Tab> tabs) {
274 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800275 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800276 }
277
278 @Override
279 public void removeTab(Tab tab) {
280 super.removeTab(tab);
281 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800282 }
283
284 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800285 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800286 }
287
Michael Kolb376b5412010-12-15 11:52:57 -0800288 int getContentWidth() {
289 if (mContentView != null) {
290 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800291 }
292 return 0;
293 }
294
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800295 @Override
296 public void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800297 if (mUiController.isInCustomActionMode()) {
298 mUiController.endActionMode();
299 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800300 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800301 mTitleBar.startEditingUrl(clearInput);
Michael Kolb66706532010-12-12 19:50:22 -0800302 }
303
Michael Kolb7cdc4902011-02-03 17:54:40 -0800304 void showTitleBarAndEdit() {
305 mTitleBar.setShowProgressOnly(false);
306 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800307 mTitleBar.startEditingUrl(false);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800308 }
309
310 void stopEditingUrl() {
311 mTitleBar.stopEditingUrl();
312 }
313
314 @Override
315 protected void showTitleBar() {
316 if (canShowTitleBar()) {
317 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800318 mContentView.addView(mTitleBar);
319 } else {
320 setTitleGravity(Gravity.TOP);
321 }
322 super.showTitleBar();
323 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800324 }
Michael Kolb376b5412010-12-15 11:52:57 -0800325 }
326
Michael Kolb66706532010-12-12 19:50:22 -0800327 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800328 protected void hideTitleBar() {
329 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800330 mTabBar.onHideTitleBar();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800331 if (mUseQuickControls) {
332 mContentView.removeView(mTitleBar);
Michael Kolb2a56eca2011-02-25 09:45:06 -0800333 } else {
334 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800335 }
336 super.hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800337 }
338 }
339
Michael Kolb5ee018e2011-02-18 15:47:21 -0800340 public boolean isEditingUrl() {
341 return mTitleBar.isEditingUrl();
342 }
343
Michael Kolb66706532010-12-12 19:50:22 -0800344 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800345 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800346 return mTitleBar;
347 }
348
Michael Kolb2a56eca2011-02-25 09:45:06 -0800349 @Override
350 protected void setTitleGravity(int gravity) {
351 if (mUseQuickControls) {
352 FrameLayout.LayoutParams lp =
353 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
354 lp.gravity = gravity;
355 mTitleBar.setLayoutParams(lp);
356 } else {
357 super.setTitleGravity(gravity);
358 }
359 }
360
Michael Kolb66706532010-12-12 19:50:22 -0800361 // action mode callbacks
362
363 @Override
364 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800365 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800366 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800367 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800368 }
369 }
370
371 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800372 public void onActionModeFinished(boolean inLoad) {
373 checkTabCount();
374 if (inLoad) {
375 // the titlebar was removed when the CAB was shown
376 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800377 if (mUseQuickControls) {
378 mTitleBar.setShowProgressOnly(true);
379 if (!isTitleBarShowing()) {
380 setTitleGravity(Gravity.TOP);
381 }
Michael Kolb376b5412010-12-15 11:52:57 -0800382 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800383 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800384 }
385 }
386
387 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800388 protected void updateNavigationState(Tab tab) {
389 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800390 }
391
392 @Override
John Reck30c714c2010-12-16 17:30:34 -0800393 public void setUrlTitle(Tab tab) {
394 super.setUrlTitle(tab);
395 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800396 }
397
398 // Set the favicon in the title bar.
399 @Override
John Reck30c714c2010-12-16 17:30:34 -0800400 public void setFavicon(Tab tab) {
401 super.setFavicon(tab);
402 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800403 }
404
Michael Kolbcfa3af52010-12-14 10:36:11 -0800405 @Override
406 public void showVoiceTitleBar(String title) {
407 List<String> vsresults = null;
408 if (getActiveTab() != null) {
409 vsresults = getActiveTab().getVoiceSearchResults();
410 }
Michael Kolb3a2a1642011-02-15 10:52:07 -0800411 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800412 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800413 }
414
415 @Override
416 public void revertVoiceTitleBar(Tab tab) {
417 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800418 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800419 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800420 }
421
John Reckd73c5a22010-12-22 10:22:50 -0800422 @Override
423 public void showCustomView(View view, CustomViewCallback callback) {
424 super.showCustomView(view, callback);
425 mActivity.getActionBar().hide();
426 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800427
John Reckd73c5a22010-12-22 10:22:50 -0800428 @Override
429 public void onHideCustomView() {
430 super.onHideCustomView();
431 if (mUseQuickControls) {
432 checkTabCount();
433 } else {
434 mActivity.getActionBar().show();
435 }
436 }
Michael Kolba4183062011-01-16 10:43:21 -0800437
438 @Override
439 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800440 if (mActiveTab != null) {
441 WebView web = mActiveTab.getWebView();
442 if (event.getAction() == KeyEvent.ACTION_DOWN) {
443 switch (code) {
444 case KeyEvent.KEYCODE_TAB:
445 case KeyEvent.KEYCODE_DPAD_UP:
446 case KeyEvent.KEYCODE_DPAD_LEFT:
447 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
448 editUrl(false);
449 return true;
450 }
451 }
452 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
453 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
454 editUrl(true);
455 return mContentView.dispatchKeyEvent(event);
456 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800457 }
Michael Kolba4183062011-01-16 10:43:21 -0800458 }
459 return false;
460 }
461
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800462 private boolean isTypingKey(KeyEvent evt) {
463 return evt.getUnicodeChar() > 0;
464 }
465
466 TabBar getTabBar() {
467 return mTabBar;
468 }
469
Narayan Kamath5119edd2011-02-23 15:49:17 +0000470 @Override
471 public void registerDropdownChangeListener(DropdownChangeListener d) {
472 mTitleBar.registerDropdownChangeListener(d);
473 }
Michael Kolb0860d992011-03-07 15:26:33 -0800474
Michael Kolb1acef692011-03-08 14:12:06 -0800475 @Override
476 public boolean onPrepareOptionsMenu(Menu menu) {
477 if (mUseQuickControls) {
478 mPieControl.onMenuOpened(menu);
479 return false;
480 } else {
481 return true;
482 }
483 }
484
Michael Kolb66706532010-12-12 19:50:22 -0800485}