blob: 5127ff3e2bc040de7b98b5a642215387fbaa30ca [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
22import android.app.ActionBar;
23import android.app.Activity;
Michael Kolb24915222011-02-24 11:38:49 -080024import android.content.pm.PackageManager;
Michael Kolbac35bdc2011-01-17 17:06:04 -080025import android.os.Bundle;
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;
John Reckd73c5a22010-12-22 10:22:50 -080030import android.view.View;
31import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080032import android.webkit.WebView;
33
34import java.util.List;
35
36/**
37 * Ui for xlarge screen sizes
38 */
39public class XLargeUi extends BaseUi implements ScrollListener {
40
41 private static final String LOGTAG = "XLargeUi";
42
Michael Kolb376b5412010-12-15 11:52:57 -080043 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080044 private TabBar mTabBar;
45
46 private TitleBarXLarge mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080047
Michael Kolb376b5412010-12-15 11:52:57 -080048 private boolean mUseQuickControls;
49 private PieControl mPieControl;
50
Michael Kolb66706532010-12-12 19:50:22 -080051 /**
52 * @param browser
53 * @param controller
54 */
55 public XLargeUi(Activity browser, UiController controller) {
56 super(browser, controller);
57 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
58 mTitleBar.setProgress(100);
Michael Kolb66706532010-12-12 19:50:22 -080059 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080060 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080061 setupActionBar();
62 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
63 }
64
65 private void setupActionBar() {
66 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080067 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
68 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080069 }
70
71 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080072 public void showComboView(boolean startWithHistory, Bundle extras) {
73 super.showComboView(startWithHistory, extras);
74 if (mUseQuickControls) {
75 mActionBar.show();
76 }
77 }
78
79 @Override
John Reckb3417f02011-01-14 11:01:05 -080080 public void hideComboView() {
Michael Kolb8814d732011-01-26 11:22:30 -080081 checkTabCount();
John Reckb3417f02011-01-14 11:01:05 -080082 super.hideComboView();
83 // ComboView changes the action bar, set it back up to what we want
84 setupActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -080085 }
86
87 private void setUseQuickControls(boolean useQuickControls) {
88 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080089 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080090 if (useQuickControls) {
91 checkTabCount();
92 mPieControl = new PieControl(mActivity, mUiController, this);
93 mPieControl.attachToContainer(mContentView);
Michael Kolb7cdc4902011-02-03 17:54:40 -080094 Tab tab = getActiveTab();
95 if ((tab != null) && (tab.getWebView() != null)) {
96 tab.getWebView().setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -080097 }
Michael Kolb7cdc4902011-02-03 17:54:40 -080098 setTitleGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -080099 } else {
100 mActivity.getActionBar().show();
101 if (mPieControl != null) {
102 mPieControl.removeFromContainer(mContentView);
103 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800104 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800105 WebView web = mTabControl.getCurrentWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800106 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800107 web.setEmbeddedTitleBar(mTitleBar);
108 }
109 }
110 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800111 }
112
113 private void checkTabCount() {
114 if (mUseQuickControls) {
115 int n = mTabBar.getTabCount();
116 if (n >= 2) {
117 mActivity.getActionBar().show();
118 } else if (n == 1) {
119 mActivity.getActionBar().hide();
120 }
121 }
Michael Kolb66706532010-12-12 19:50:22 -0800122 }
123
124 @Override
125 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800126 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800127 }
128
129 // webview factory
130
131 @Override
132 public WebView createWebView(boolean privateBrowsing) {
133 // Create a new WebView
134 ScrollWebView w = new ScrollWebView(mActivity, null,
135 android.R.attr.webViewStyle, privateBrowsing);
136 initWebViewSettings(w);
137 w.setScrollListener(this);
Michael Kolb24915222011-02-24 11:38:49 -0800138 boolean supportsMultiTouch = mActivity.getPackageManager()
139 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
140 w.getSettings().setDisplayZoomControls(!supportsMultiTouch);
Dave Burke20b6f1f2011-02-01 12:59:04 +0000141 w.setExpandedTileBounds(true); // smoother scrolling
Michael Kolb66706532010-12-12 19:50:22 -0800142 return w;
143 }
144
145 @Override
146 public WebView createSubWebView(boolean privateBrowsing) {
147 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
148 // no scroll listener for subview
149 web.setScrollListener(null);
150 return web;
151 }
152
153 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800154 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
155 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800156 }
157
158 void stopWebViewScrolling() {
159 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
160 if (web != null) {
161 web.stopScroll();
162 }
163 }
164
165 // WebView callbacks
166
167 @Override
John Reck30c714c2010-12-16 17:30:34 -0800168 public void onProgressChanged(Tab tab) {
169 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800170 mTabBar.onProgress(tab, progress);
171 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800172 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800173 if (progress == 100) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800174 if (!mTitleBar.isEditingUrl()) {
175 hideTitleBar();
Michael Kolbbd018d42010-12-15 15:43:39 -0800176 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800177 mTitleBar.setShowProgressOnly(false);
178 setTitleGravity(Gravity.BOTTOM);
Michael Kolbbd018d42010-12-15 15:43:39 -0800179 }
Michael Kolb376b5412010-12-15 11:52:57 -0800180 }
Michael Kolb66706532010-12-12 19:50:22 -0800181 } else {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800182 if (!isTitleBarShowing()) {
183 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
184 mTitleBar.setShowProgressOnly(true);
185 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800186 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800187 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800188 }
Michael Kolb66706532010-12-12 19:50:22 -0800189 }
190 }
191 }
192
193 @Override
194 public boolean needsRestoreAllTabs() {
195 return true;
196 }
197
198 @Override
199 public void addTab(Tab tab) {
200 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800201 }
202
203 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800204 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800205 }
206
207 @Override
208 public void setActiveTab(Tab tab) {
Michael Kolb36a1dc92011-02-22 16:14:59 -0800209 if (mTitleBar.isEditingUrl()) {
210 mTitleBar.stopEditingUrl();
211 }
Michael Kolb66706532010-12-12 19:50:22 -0800212 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800213 ScrollWebView view = (ScrollWebView) tab.getWebView();
214 // TabControl.setCurrentTab has been called before this,
215 // so the tab is guaranteed to have a webview
216 if (view == null) {
217 Log.e(LOGTAG, "active tab with no webview detected");
218 return;
219 }
220 // Request focus on the top window.
221 if (mUseQuickControls) {
222 mPieControl.forceToTop(mContentView);
223 view.setScrollListener(null);
224 mTabBar.showTitleBarIndicator(false);
225 } else {
226 view.setEmbeddedTitleBar(mTitleBar);
227 view.setScrollListener(this);
228 }
Michael Kolb66706532010-12-12 19:50:22 -0800229 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800230 if (tab.isInVoiceSearchMode()) {
231 showVoiceTitleBar(tab.getVoiceDisplayTitle());
232 } else {
233 revertVoiceTitleBar(tab);
234 }
Michael Kolb376b5412010-12-15 11:52:57 -0800235 updateLockIconToLatest(tab);
236 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800237 }
238
239 @Override
240 public void updateTabs(List<Tab> tabs) {
241 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800242 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800243 }
244
245 @Override
246 public void removeTab(Tab tab) {
247 super.removeTab(tab);
248 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800249 }
250
251 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800252 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800253 }
254
Michael Kolb376b5412010-12-15 11:52:57 -0800255 int getContentWidth() {
256 if (mContentView != null) {
257 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800258 }
259 return 0;
260 }
261
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800262 @Override
263 public void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800264 if (mUiController.isInCustomActionMode()) {
265 mUiController.endActionMode();
266 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800267 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800268 mTitleBar.startEditingUrl(clearInput);
Michael Kolb66706532010-12-12 19:50:22 -0800269 }
270
Michael Kolb7cdc4902011-02-03 17:54:40 -0800271 void showTitleBarAndEdit() {
272 mTitleBar.setShowProgressOnly(false);
273 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800274 mTitleBar.startEditingUrl(false);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800275 }
276
277 void stopEditingUrl() {
278 mTitleBar.stopEditingUrl();
279 }
280
281 @Override
282 protected void showTitleBar() {
283 if (canShowTitleBar()) {
284 if (mUseQuickControls) {
285 setTitleGravity(Gravity.BOTTOM);
286 mContentView.addView(mTitleBar);
287 } else {
288 setTitleGravity(Gravity.TOP);
289 }
290 super.showTitleBar();
291 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800292 }
Michael Kolb376b5412010-12-15 11:52:57 -0800293 }
294
Michael Kolb66706532010-12-12 19:50:22 -0800295 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800296 protected void hideTitleBar() {
297 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800298 mTabBar.onHideTitleBar();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800299 setTitleGravity(Gravity.NO_GRAVITY);
300 if (mUseQuickControls) {
301 mContentView.removeView(mTitleBar);
302 }
303 super.hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800304 }
305 }
306
Michael Kolb5ee018e2011-02-18 15:47:21 -0800307 public boolean isEditingUrl() {
308 return mTitleBar.isEditingUrl();
309 }
310
Michael Kolb66706532010-12-12 19:50:22 -0800311 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800312 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800313 return mTitleBar;
314 }
315
316 // action mode callbacks
317
318 @Override
319 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800320 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800321 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800322 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800323 }
324 }
325
326 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800327 public void onActionModeFinished(boolean inLoad) {
328 checkTabCount();
329 if (inLoad) {
330 // the titlebar was removed when the CAB was shown
331 // if the page is loading, show it again
Michael Kolb7cdc4902011-02-03 17:54:40 -0800332 mTitleBar.setShowProgressOnly(true);
333 if (!isTitleBarShowing()) {
334 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800335 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800336 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800337 }
338 }
339
340 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800341 protected void updateNavigationState(Tab tab) {
342 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800343 }
344
345 @Override
John Reck30c714c2010-12-16 17:30:34 -0800346 public void setUrlTitle(Tab tab) {
347 super.setUrlTitle(tab);
348 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800349 }
350
351 // Set the favicon in the title bar.
352 @Override
John Reck30c714c2010-12-16 17:30:34 -0800353 public void setFavicon(Tab tab) {
354 super.setFavicon(tab);
355 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800356 }
357
Michael Kolbcfa3af52010-12-14 10:36:11 -0800358 @Override
359 public void showVoiceTitleBar(String title) {
360 List<String> vsresults = null;
361 if (getActiveTab() != null) {
362 vsresults = getActiveTab().getVoiceSearchResults();
363 }
Michael Kolb3a2a1642011-02-15 10:52:07 -0800364 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800365 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800366 }
367
368 @Override
369 public void revertVoiceTitleBar(Tab tab) {
370 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800371 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800372 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800373 }
374
John Reckd73c5a22010-12-22 10:22:50 -0800375 @Override
376 public void showCustomView(View view, CustomViewCallback callback) {
377 super.showCustomView(view, callback);
378 mActivity.getActionBar().hide();
379 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800380
John Reckd73c5a22010-12-22 10:22:50 -0800381 @Override
382 public void onHideCustomView() {
383 super.onHideCustomView();
384 if (mUseQuickControls) {
385 checkTabCount();
386 } else {
387 mActivity.getActionBar().show();
388 }
389 }
Michael Kolba4183062011-01-16 10:43:21 -0800390
391 @Override
392 public boolean dispatchKey(int code, KeyEvent event) {
393 WebView web = getActiveTab().getWebView();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800394 if (event.getAction() == KeyEvent.ACTION_DOWN) {
395
396 switch (code) {
397 case KeyEvent.KEYCODE_TAB:
398 case KeyEvent.KEYCODE_DPAD_UP:
399 case KeyEvent.KEYCODE_DPAD_LEFT:
400 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
401 editUrl(false);
402 return true;
403 }
404 }
405 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
406 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
407 editUrl(true);
408 return mContentView.dispatchKeyEvent(event);
409 }
Michael Kolba4183062011-01-16 10:43:21 -0800410 }
411 return false;
412 }
413
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800414 private boolean isTypingKey(KeyEvent evt) {
415 return evt.getUnicodeChar() > 0;
416 }
417
418 TabBar getTabBar() {
419 return mTabBar;
420 }
421
Narayan Kamath5119edd2011-02-23 15:49:17 +0000422 @Override
423 public void registerDropdownChangeListener(DropdownChangeListener d) {
424 mTitleBar.registerDropdownChangeListener(d);
425 }
Michael Kolb66706532010-12-12 19:50:22 -0800426}