blob: f91418315f7806176415d28b0fe85e89a6e82bbc [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 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;
John Reckd73c5a22010-12-22 10:22:50 -080029import android.view.View;
30import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080031import android.webkit.WebView;
32
33import java.util.List;
34
35/**
36 * Ui for xlarge screen sizes
37 */
38public class XLargeUi extends BaseUi implements ScrollListener {
39
40 private static final String LOGTAG = "XLargeUi";
41
Michael Kolb376b5412010-12-15 11:52:57 -080042 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080043 private TabBar mTabBar;
44
45 private TitleBarXLarge mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080046
Michael Kolb376b5412010-12-15 11:52:57 -080047 private boolean mUseQuickControls;
48 private PieControl mPieControl;
49
Michael Kolb66706532010-12-12 19:50:22 -080050 /**
51 * @param browser
52 * @param controller
53 */
54 public XLargeUi(Activity browser, UiController controller) {
55 super(browser, controller);
56 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
57 mTitleBar.setProgress(100);
Michael Kolb66706532010-12-12 19:50:22 -080058 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080059 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080060 setupActionBar();
61 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
62 }
63
64 private void setupActionBar() {
65 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080066 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
67 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080068 }
69
70 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080071 public void showComboView(boolean startWithHistory, Bundle extras) {
72 super.showComboView(startWithHistory, extras);
73 if (mUseQuickControls) {
74 mActionBar.show();
75 }
76 }
77
78 @Override
John Reckb3417f02011-01-14 11:01:05 -080079 public void hideComboView() {
Michael Kolb8814d732011-01-26 11:22:30 -080080 checkTabCount();
John Reckb3417f02011-01-14 11:01:05 -080081 super.hideComboView();
82 // ComboView changes the action bar, set it back up to what we want
83 setupActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -080084 }
85
86 private void setUseQuickControls(boolean useQuickControls) {
87 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080088 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080089 if (useQuickControls) {
90 checkTabCount();
91 mPieControl = new PieControl(mActivity, mUiController, this);
92 mPieControl.attachToContainer(mContentView);
Michael Kolb7cdc4902011-02-03 17:54:40 -080093 Tab tab = getActiveTab();
94 if ((tab != null) && (tab.getWebView() != null)) {
95 tab.getWebView().setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -080096 }
Michael Kolb7cdc4902011-02-03 17:54:40 -080097 setTitleGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -080098 } else {
99 mActivity.getActionBar().show();
100 if (mPieControl != null) {
101 mPieControl.removeFromContainer(mContentView);
102 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800103 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800104 WebView web = mTabControl.getCurrentWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800105 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800106 web.setEmbeddedTitleBar(mTitleBar);
107 }
108 }
109 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800110 }
111
112 private void checkTabCount() {
113 if (mUseQuickControls) {
114 int n = mTabBar.getTabCount();
115 if (n >= 2) {
116 mActivity.getActionBar().show();
117 } else if (n == 1) {
118 mActivity.getActionBar().hide();
119 }
120 }
Michael Kolb66706532010-12-12 19:50:22 -0800121 }
122
123 @Override
124 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800125 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800126 }
127
128 // webview factory
129
130 @Override
131 public WebView createWebView(boolean privateBrowsing) {
132 // Create a new WebView
133 ScrollWebView w = new ScrollWebView(mActivity, null,
134 android.R.attr.webViewStyle, privateBrowsing);
135 initWebViewSettings(w);
136 w.setScrollListener(this);
Michael Kolb24915222011-02-24 11:38:49 -0800137 boolean supportsMultiTouch = mActivity.getPackageManager()
138 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
139 w.getSettings().setDisplayZoomControls(!supportsMultiTouch);
Dave Burke20b6f1f2011-02-01 12:59:04 +0000140 w.setExpandedTileBounds(true); // smoother scrolling
Michael Kolb66706532010-12-12 19:50:22 -0800141 return w;
142 }
143
144 @Override
145 public WebView createSubWebView(boolean privateBrowsing) {
146 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
147 // no scroll listener for subview
148 web.setScrollListener(null);
149 return web;
150 }
151
152 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800153 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
154 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800155 }
156
157 void stopWebViewScrolling() {
158 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
159 if (web != null) {
160 web.stopScroll();
161 }
162 }
163
164 // WebView callbacks
165
166 @Override
John Reck30c714c2010-12-16 17:30:34 -0800167 public void onProgressChanged(Tab tab) {
168 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800169 mTabBar.onProgress(tab, progress);
170 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800171 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800172 if (progress == 100) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800173 if (!mTitleBar.isEditingUrl()) {
174 hideTitleBar();
Michael Kolbbd018d42010-12-15 15:43:39 -0800175 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800176 mTitleBar.setShowProgressOnly(false);
177 setTitleGravity(Gravity.BOTTOM);
Michael Kolbbd018d42010-12-15 15:43:39 -0800178 }
Michael Kolb376b5412010-12-15 11:52:57 -0800179 }
Michael Kolb66706532010-12-12 19:50:22 -0800180 } else {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800181 if (!isTitleBarShowing()) {
182 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
183 mTitleBar.setShowProgressOnly(true);
184 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800185 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800186 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800187 }
Michael Kolb66706532010-12-12 19:50:22 -0800188 }
189 }
190 }
191
192 @Override
193 public boolean needsRestoreAllTabs() {
194 return true;
195 }
196
197 @Override
198 public void addTab(Tab tab) {
199 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800200 }
201
202 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800203 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800204 }
205
206 @Override
207 public void setActiveTab(Tab tab) {
Michael Kolb36a1dc92011-02-22 16:14:59 -0800208 if (mTitleBar.isEditingUrl()) {
209 mTitleBar.stopEditingUrl();
210 }
Michael Kolb66706532010-12-12 19:50:22 -0800211 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800212 ScrollWebView view = (ScrollWebView) tab.getWebView();
213 // TabControl.setCurrentTab has been called before this,
214 // so the tab is guaranteed to have a webview
215 if (view == null) {
216 Log.e(LOGTAG, "active tab with no webview detected");
217 return;
218 }
219 // Request focus on the top window.
220 if (mUseQuickControls) {
221 mPieControl.forceToTop(mContentView);
222 view.setScrollListener(null);
223 mTabBar.showTitleBarIndicator(false);
224 } else {
225 view.setEmbeddedTitleBar(mTitleBar);
226 view.setScrollListener(this);
227 }
Michael Kolb66706532010-12-12 19:50:22 -0800228 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800229 if (tab.isInVoiceSearchMode()) {
230 showVoiceTitleBar(tab.getVoiceDisplayTitle());
231 } else {
232 revertVoiceTitleBar(tab);
233 }
Michael Kolb376b5412010-12-15 11:52:57 -0800234 updateLockIconToLatest(tab);
235 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
238 @Override
239 public void updateTabs(List<Tab> tabs) {
240 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800241 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800242 }
243
244 @Override
245 public void removeTab(Tab tab) {
246 super.removeTab(tab);
247 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800248 }
249
250 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800251 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800252 }
253
Michael Kolb376b5412010-12-15 11:52:57 -0800254 int getContentWidth() {
255 if (mContentView != null) {
256 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800257 }
258 return 0;
259 }
260
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800261 @Override
262 public void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800263 if (mUiController.isInCustomActionMode()) {
264 mUiController.endActionMode();
265 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800266 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800267 mTitleBar.startEditingUrl(clearInput);
Michael Kolb66706532010-12-12 19:50:22 -0800268 }
269
Michael Kolb7cdc4902011-02-03 17:54:40 -0800270 void showTitleBarAndEdit() {
271 mTitleBar.setShowProgressOnly(false);
272 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800273 mTitleBar.startEditingUrl(false);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800274 }
275
276 void stopEditingUrl() {
277 mTitleBar.stopEditingUrl();
278 }
279
280 @Override
281 protected void showTitleBar() {
282 if (canShowTitleBar()) {
283 if (mUseQuickControls) {
284 setTitleGravity(Gravity.BOTTOM);
285 mContentView.addView(mTitleBar);
286 } else {
287 setTitleGravity(Gravity.TOP);
288 }
289 super.showTitleBar();
290 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800291 }
Michael Kolb376b5412010-12-15 11:52:57 -0800292 }
293
Michael Kolb66706532010-12-12 19:50:22 -0800294 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800295 protected void hideTitleBar() {
296 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800297 mTabBar.onHideTitleBar();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800298 setTitleGravity(Gravity.NO_GRAVITY);
299 if (mUseQuickControls) {
300 mContentView.removeView(mTitleBar);
301 }
302 super.hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800303 }
304 }
305
Michael Kolb5ee018e2011-02-18 15:47:21 -0800306 public boolean isEditingUrl() {
307 return mTitleBar.isEditingUrl();
308 }
309
Michael Kolb66706532010-12-12 19:50:22 -0800310 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800311 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800312 return mTitleBar;
313 }
314
315 // action mode callbacks
316
317 @Override
318 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800319 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800320 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800321 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800322 }
323 }
324
325 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800326 public void onActionModeFinished(boolean inLoad) {
327 checkTabCount();
328 if (inLoad) {
329 // the titlebar was removed when the CAB was shown
330 // if the page is loading, show it again
Michael Kolb7cdc4902011-02-03 17:54:40 -0800331 mTitleBar.setShowProgressOnly(true);
332 if (!isTitleBarShowing()) {
333 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800334 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800335 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800336 }
337 }
338
339 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800340 protected void updateNavigationState(Tab tab) {
341 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800342 }
343
344 @Override
John Reck30c714c2010-12-16 17:30:34 -0800345 public void setUrlTitle(Tab tab) {
346 super.setUrlTitle(tab);
347 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800348 }
349
350 // Set the favicon in the title bar.
351 @Override
John Reck30c714c2010-12-16 17:30:34 -0800352 public void setFavicon(Tab tab) {
353 super.setFavicon(tab);
354 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800355 }
356
Michael Kolbcfa3af52010-12-14 10:36:11 -0800357 @Override
358 public void showVoiceTitleBar(String title) {
359 List<String> vsresults = null;
360 if (getActiveTab() != null) {
361 vsresults = getActiveTab().getVoiceSearchResults();
362 }
Michael Kolb3a2a1642011-02-15 10:52:07 -0800363 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800364 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800365 }
366
367 @Override
368 public void revertVoiceTitleBar(Tab tab) {
369 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800370 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800371 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800372 }
373
John Reckd73c5a22010-12-22 10:22:50 -0800374 @Override
375 public void showCustomView(View view, CustomViewCallback callback) {
376 super.showCustomView(view, callback);
377 mActivity.getActionBar().hide();
378 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800379
John Reckd73c5a22010-12-22 10:22:50 -0800380 @Override
381 public void onHideCustomView() {
382 super.onHideCustomView();
383 if (mUseQuickControls) {
384 checkTabCount();
385 } else {
386 mActivity.getActionBar().show();
387 }
388 }
Michael Kolba4183062011-01-16 10:43:21 -0800389
390 @Override
391 public boolean dispatchKey(int code, KeyEvent event) {
392 WebView web = getActiveTab().getWebView();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800393 if (event.getAction() == KeyEvent.ACTION_DOWN) {
394
395 switch (code) {
396 case KeyEvent.KEYCODE_TAB:
397 case KeyEvent.KEYCODE_DPAD_UP:
398 case KeyEvent.KEYCODE_DPAD_LEFT:
399 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
400 editUrl(false);
401 return true;
402 }
403 }
404 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
405 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
406 editUrl(true);
407 return mContentView.dispatchKeyEvent(event);
408 }
Michael Kolba4183062011-01-16 10:43:21 -0800409 }
410 return false;
411 }
412
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800413 private boolean isTypingKey(KeyEvent evt) {
414 return evt.getUnicodeChar() > 0;
415 }
416
417 TabBar getTabBar() {
418 return mTabBar;
419 }
420
Michael Kolb66706532010-12-12 19:50:22 -0800421}