blob: 33151f7231be6e459dfc64ee382a87c7cc5f2139 [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;
Narayan Kamath5119edd2011-02-23 15:49:17 +000020import com.android.browser.UI.DropdownChangeListener;
Michael Kolb66706532010-12-12 19:50:22 -080021
Michael Kolb377ea312011-02-17 14:36:56 -080022import android.animation.Animator;
23import android.animation.Animator.AnimatorListener;
24import android.animation.ObjectAnimator;
Michael Kolb66706532010-12-12 19:50:22 -080025import android.app.ActionBar;
26import android.app.Activity;
Michael Kolb24915222011-02-24 11:38:49 -080027import android.content.pm.PackageManager;
Michael Kolbac35bdc2011-01-17 17:06:04 -080028import android.os.Bundle;
Michael Kolb376b5412010-12-15 11:52:57 -080029import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080030import android.view.ActionMode;
Michael Kolb376b5412010-12-15 11:52:57 -080031import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080032import android.view.KeyEvent;
John Reckd73c5a22010-12-22 10:22:50 -080033import android.view.View;
34import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080035import android.webkit.WebView;
Michael Kolb2a56eca2011-02-25 09:45:06 -080036import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080037
38import java.util.List;
39
40/**
41 * Ui for xlarge screen sizes
42 */
43public class XLargeUi extends BaseUi implements ScrollListener {
44
45 private static final String LOGTAG = "XLargeUi";
46
Michael Kolb376b5412010-12-15 11:52:57 -080047 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080048 private TabBar mTabBar;
49
50 private TitleBarXLarge mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080051
Michael Kolb376b5412010-12-15 11:52:57 -080052 private boolean mUseQuickControls;
53 private PieControl mPieControl;
54
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);
61 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 Kolb8814d732011-01-26 11:22:30 -080085 checkTabCount();
John Reckb3417f02011-01-14 11:01:05 -080086 super.hideComboView();
87 // ComboView changes the action bar, set it back up to what we want
88 setupActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -080089 }
90
91 private void setUseQuickControls(boolean useQuickControls) {
92 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080093 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080094 if (useQuickControls) {
95 checkTabCount();
96 mPieControl = new PieControl(mActivity, mUiController, this);
97 mPieControl.attachToContainer(mContentView);
Michael Kolb7cdc4902011-02-03 17:54:40 -080098 Tab tab = getActiveTab();
99 if ((tab != null) && (tab.getWebView() != null)) {
100 tab.getWebView().setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -0800101 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800102 setTitleGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800103 } else {
104 mActivity.getActionBar().show();
105 if (mPieControl != null) {
106 mPieControl.removeFromContainer(mContentView);
107 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800108 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800109 WebView web = mTabControl.getCurrentWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800110 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800111 web.setEmbeddedTitleBar(mTitleBar);
112 }
113 }
114 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800115 }
116
117 private void checkTabCount() {
118 if (mUseQuickControls) {
119 int n = mTabBar.getTabCount();
120 if (n >= 2) {
121 mActivity.getActionBar().show();
122 } else if (n == 1) {
123 mActivity.getActionBar().hide();
124 }
125 }
Michael Kolb66706532010-12-12 19:50:22 -0800126 }
127
128 @Override
129 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800130 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800131 }
132
133 // webview factory
134
135 @Override
136 public WebView createWebView(boolean privateBrowsing) {
137 // Create a new WebView
138 ScrollWebView w = new ScrollWebView(mActivity, null,
139 android.R.attr.webViewStyle, privateBrowsing);
140 initWebViewSettings(w);
141 w.setScrollListener(this);
Michael Kolb24915222011-02-24 11:38:49 -0800142 boolean supportsMultiTouch = mActivity.getPackageManager()
143 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
144 w.getSettings().setDisplayZoomControls(!supportsMultiTouch);
Dave Burke20b6f1f2011-02-01 12:59:04 +0000145 w.setExpandedTileBounds(true); // smoother scrolling
Michael Kolb66706532010-12-12 19:50:22 -0800146 return w;
147 }
148
149 @Override
150 public WebView createSubWebView(boolean privateBrowsing) {
151 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
152 // no scroll listener for subview
153 web.setScrollListener(null);
154 return web;
155 }
156
157 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800158 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
159 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800160 }
161
162 void stopWebViewScrolling() {
163 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
164 if (web != null) {
165 web.stopScroll();
166 }
167 }
168
169 // WebView callbacks
170
171 @Override
John Reck30c714c2010-12-16 17:30:34 -0800172 public void onProgressChanged(Tab tab) {
173 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800174 mTabBar.onProgress(tab, progress);
175 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800176 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800177 if (progress == 100) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800178 if (!mTitleBar.isEditingUrl()) {
179 hideTitleBar();
Michael Kolbbd018d42010-12-15 15:43:39 -0800180 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800181 mTitleBar.setShowProgressOnly(false);
182 setTitleGravity(Gravity.BOTTOM);
Michael Kolbbd018d42010-12-15 15:43:39 -0800183 }
Michael Kolb376b5412010-12-15 11:52:57 -0800184 }
Michael Kolb66706532010-12-12 19:50:22 -0800185 } else {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800186 if (!isTitleBarShowing()) {
187 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
188 mTitleBar.setShowProgressOnly(true);
189 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800190 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800191 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800192 }
Michael Kolb66706532010-12-12 19:50:22 -0800193 }
194 }
195 }
196
197 @Override
198 public boolean needsRestoreAllTabs() {
199 return true;
200 }
201
202 @Override
203 public void addTab(Tab tab) {
204 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800205 }
206
207 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800208 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800209 }
210
211 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800212 public void setActiveTab(final Tab tab) {
213 if ((tab != mActiveTab) && (mActiveTab != null)) {
214 // animate between the two
215 final ScrollWebView fromWV = (ScrollWebView) mActiveTab.getWebView();
216 fromWV.setDrawCached(true);
217 fromWV.setEmbeddedTitleBar(null);
218 final ScrollWebView toWV = (ScrollWebView) tab.getWebView();
219 if (!mUseQuickControls) {
220 if (mTitleBar.getParent() == null) {
221 toWV.setEmbeddedTitleBar(mTitleBar);
222 }
223 }
224 toWV.setDrawCached(true);
225 attachTabToContentView(tab);
226 super.setActiveTab(tab, false);
227 ObjectAnimator transition = ObjectAnimator.ofFloat(
228 toWV, "alpha", 0f, 1f);
229 transition.setDuration(mActivity.getResources()
230 .getInteger(R.integer.tabFadeDuration));
231 transition.addListener(new AnimatorListener() {
232 @Override
233 public void onAnimationCancel(Animator animation) {
234 fromWV.setDrawCached(false);
235 toWV.setDrawCached(false);
236 setActiveTab(tab, false);
237 }
238
239 @Override
240 public void onAnimationEnd(Animator animation) {
241 fromWV.setDrawCached(false);
242 toWV.setDrawCached(false);
243 setActiveTab(tab, false);
244 }
245
246 @Override
247 public void onAnimationRepeat(Animator animation) {
248 }
249
250 @Override
251 public void onAnimationStart(Animator animation) {
252 }
253 });
254 transition.start();
255 } else {
256 super.setActiveTab(tab, true);
257 setActiveTab(tab, true);
Michael Kolb36a1dc92011-02-22 16:14:59 -0800258 }
Michael Kolb377ea312011-02-17 14:36:56 -0800259 }
260
261 @Override
262 void setActiveTab(Tab tab, boolean needsAttaching) {
Michael Kolb376b5412010-12-15 11:52:57 -0800263 ScrollWebView view = (ScrollWebView) tab.getWebView();
264 // TabControl.setCurrentTab has been called before this,
265 // so the tab is guaranteed to have a webview
266 if (view == null) {
267 Log.e(LOGTAG, "active tab with no webview detected");
268 return;
269 }
270 // Request focus on the top window.
271 if (mUseQuickControls) {
272 mPieControl.forceToTop(mContentView);
273 view.setScrollListener(null);
274 mTabBar.showTitleBarIndicator(false);
275 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800276 // check if title bar is already attached by animation
277 if (mTitleBar.getParent() == null) {
278 view.setEmbeddedTitleBar(mTitleBar);
279 }
Michael Kolb376b5412010-12-15 11:52:57 -0800280 view.setScrollListener(this);
281 }
Michael Kolb66706532010-12-12 19:50:22 -0800282 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800283 if (tab.isInVoiceSearchMode()) {
284 showVoiceTitleBar(tab.getVoiceDisplayTitle());
285 } else {
286 revertVoiceTitleBar(tab);
287 }
Michael Kolb376b5412010-12-15 11:52:57 -0800288 updateLockIconToLatest(tab);
289 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800290 }
291
292 @Override
293 public void updateTabs(List<Tab> tabs) {
294 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800295 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800296 }
297
298 @Override
299 public void removeTab(Tab tab) {
300 super.removeTab(tab);
301 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800302 }
303
304 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800305 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800306 }
307
Michael Kolb376b5412010-12-15 11:52:57 -0800308 int getContentWidth() {
309 if (mContentView != null) {
310 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800311 }
312 return 0;
313 }
314
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800315 @Override
316 public void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800317 if (mUiController.isInCustomActionMode()) {
318 mUiController.endActionMode();
319 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800320 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800321 mTitleBar.startEditingUrl(clearInput);
Michael Kolb66706532010-12-12 19:50:22 -0800322 }
323
Michael Kolb7cdc4902011-02-03 17:54:40 -0800324 void showTitleBarAndEdit() {
325 mTitleBar.setShowProgressOnly(false);
326 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800327 mTitleBar.startEditingUrl(false);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800328 }
329
330 void stopEditingUrl() {
331 mTitleBar.stopEditingUrl();
332 }
333
334 @Override
335 protected void showTitleBar() {
336 if (canShowTitleBar()) {
337 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800338 mContentView.addView(mTitleBar);
339 } else {
340 setTitleGravity(Gravity.TOP);
341 }
342 super.showTitleBar();
343 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800344 }
Michael Kolb376b5412010-12-15 11:52:57 -0800345 }
346
Michael Kolb66706532010-12-12 19:50:22 -0800347 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800348 protected void hideTitleBar() {
349 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800350 mTabBar.onHideTitleBar();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800351 if (mUseQuickControls) {
352 mContentView.removeView(mTitleBar);
Michael Kolb2a56eca2011-02-25 09:45:06 -0800353 } else {
354 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800355 }
356 super.hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800357 }
358 }
359
Michael Kolb5ee018e2011-02-18 15:47:21 -0800360 public boolean isEditingUrl() {
361 return mTitleBar.isEditingUrl();
362 }
363
Michael Kolb66706532010-12-12 19:50:22 -0800364 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800365 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800366 return mTitleBar;
367 }
368
Michael Kolb2a56eca2011-02-25 09:45:06 -0800369 @Override
370 protected void setTitleGravity(int gravity) {
371 if (mUseQuickControls) {
372 FrameLayout.LayoutParams lp =
373 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
374 lp.gravity = gravity;
375 mTitleBar.setLayoutParams(lp);
376 } else {
377 super.setTitleGravity(gravity);
378 }
379 }
380
Michael Kolb66706532010-12-12 19:50:22 -0800381 // action mode callbacks
382
383 @Override
384 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800385 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800386 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800387 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800388 }
389 }
390
391 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800392 public void onActionModeFinished(boolean inLoad) {
393 checkTabCount();
394 if (inLoad) {
395 // the titlebar was removed when the CAB was shown
396 // if the page is loading, show it again
Michael Kolb7cdc4902011-02-03 17:54:40 -0800397 mTitleBar.setShowProgressOnly(true);
398 if (!isTitleBarShowing()) {
399 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800400 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800401 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800402 }
403 }
404
405 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800406 protected void updateNavigationState(Tab tab) {
407 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800408 }
409
410 @Override
John Reck30c714c2010-12-16 17:30:34 -0800411 public void setUrlTitle(Tab tab) {
412 super.setUrlTitle(tab);
413 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800414 }
415
416 // Set the favicon in the title bar.
417 @Override
John Reck30c714c2010-12-16 17:30:34 -0800418 public void setFavicon(Tab tab) {
419 super.setFavicon(tab);
420 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800421 }
422
Michael Kolbcfa3af52010-12-14 10:36:11 -0800423 @Override
424 public void showVoiceTitleBar(String title) {
425 List<String> vsresults = null;
426 if (getActiveTab() != null) {
427 vsresults = getActiveTab().getVoiceSearchResults();
428 }
Michael Kolb3a2a1642011-02-15 10:52:07 -0800429 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800430 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800431 }
432
433 @Override
434 public void revertVoiceTitleBar(Tab tab) {
435 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800436 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800437 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800438 }
439
John Reckd73c5a22010-12-22 10:22:50 -0800440 @Override
441 public void showCustomView(View view, CustomViewCallback callback) {
442 super.showCustomView(view, callback);
443 mActivity.getActionBar().hide();
444 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800445
John Reckd73c5a22010-12-22 10:22:50 -0800446 @Override
447 public void onHideCustomView() {
448 super.onHideCustomView();
449 if (mUseQuickControls) {
450 checkTabCount();
451 } else {
452 mActivity.getActionBar().show();
453 }
454 }
Michael Kolba4183062011-01-16 10:43:21 -0800455
456 @Override
457 public boolean dispatchKey(int code, KeyEvent event) {
458 WebView web = getActiveTab().getWebView();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800459 if (event.getAction() == KeyEvent.ACTION_DOWN) {
460
461 switch (code) {
462 case KeyEvent.KEYCODE_TAB:
463 case KeyEvent.KEYCODE_DPAD_UP:
464 case KeyEvent.KEYCODE_DPAD_LEFT:
465 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
466 editUrl(false);
467 return true;
468 }
469 }
470 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
471 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
472 editUrl(true);
473 return mContentView.dispatchKeyEvent(event);
474 }
Michael Kolba4183062011-01-16 10:43:21 -0800475 }
476 return false;
477 }
478
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800479 private boolean isTypingKey(KeyEvent evt) {
480 return evt.getUnicodeChar() > 0;
481 }
482
483 TabBar getTabBar() {
484 return mTabBar;
485 }
486
Narayan Kamath5119edd2011-02-23 15:49:17 +0000487 @Override
488 public void registerDropdownChangeListener(DropdownChangeListener d) {
489 mTitleBar.registerDropdownChangeListener(d);
490 }
Michael Kolb66706532010-12-12 19:50:22 -0800491}