blob: 868404211c431c8930a5b2cff401493688269339 [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 Kolbac35bdc2011-01-17 17:06:04 -080023import android.os.Bundle;
Michael Kolb376b5412010-12-15 11:52:57 -080024import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080025import android.view.ActionMode;
Michael Kolb376b5412010-12-15 11:52:57 -080026import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080027import android.view.KeyEvent;
John Reckd73c5a22010-12-22 10:22:50 -080028import android.view.View;
29import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080030import android.webkit.WebView;
31
32import java.util.List;
33
34/**
35 * Ui for xlarge screen sizes
36 */
37public class XLargeUi extends BaseUi implements ScrollListener {
38
39 private static final String LOGTAG = "XLargeUi";
40
Michael Kolb376b5412010-12-15 11:52:57 -080041 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080042 private TabBar mTabBar;
43
44 private TitleBarXLarge mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080045
Michael Kolb376b5412010-12-15 11:52:57 -080046 private boolean mUseQuickControls;
47 private PieControl mPieControl;
48
Michael Kolb66706532010-12-12 19:50:22 -080049 /**
50 * @param browser
51 * @param controller
52 */
53 public XLargeUi(Activity browser, UiController controller) {
54 super(browser, controller);
55 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
56 mTitleBar.setProgress(100);
Michael Kolb66706532010-12-12 19:50:22 -080057 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080058 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080059 setupActionBar();
60 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
61 }
62
63 private void setupActionBar() {
64 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080065 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
66 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080067 }
68
69 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080070 public void showComboView(boolean startWithHistory, Bundle extras) {
71 super.showComboView(startWithHistory, extras);
72 if (mUseQuickControls) {
73 mActionBar.show();
74 }
75 }
76
77 @Override
John Reckb3417f02011-01-14 11:01:05 -080078 public void hideComboView() {
Michael Kolb8814d732011-01-26 11:22:30 -080079 checkTabCount();
John Reckb3417f02011-01-14 11:01:05 -080080 super.hideComboView();
81 // ComboView changes the action bar, set it back up to what we want
82 setupActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -080083 }
84
85 private void setUseQuickControls(boolean useQuickControls) {
86 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080087 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080088 if (useQuickControls) {
89 checkTabCount();
90 mPieControl = new PieControl(mActivity, mUiController, this);
91 mPieControl.attachToContainer(mContentView);
Michael Kolb7cdc4902011-02-03 17:54:40 -080092 Tab tab = getActiveTab();
93 if ((tab != null) && (tab.getWebView() != null)) {
94 tab.getWebView().setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -080095 }
Michael Kolb7cdc4902011-02-03 17:54:40 -080096 setTitleGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -080097 } else {
98 mActivity.getActionBar().show();
99 if (mPieControl != null) {
100 mPieControl.removeFromContainer(mContentView);
101 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800102 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800103 WebView web = mTabControl.getCurrentWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800104 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800105 web.setEmbeddedTitleBar(mTitleBar);
106 }
107 }
108 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800109 }
110
111 private void checkTabCount() {
112 if (mUseQuickControls) {
113 int n = mTabBar.getTabCount();
114 if (n >= 2) {
115 mActivity.getActionBar().show();
116 } else if (n == 1) {
117 mActivity.getActionBar().hide();
118 }
119 }
Michael Kolb66706532010-12-12 19:50:22 -0800120 }
121
122 @Override
123 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800124 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800125 }
126
127 // webview factory
128
129 @Override
130 public WebView createWebView(boolean privateBrowsing) {
131 // Create a new WebView
132 ScrollWebView w = new ScrollWebView(mActivity, null,
133 android.R.attr.webViewStyle, privateBrowsing);
134 initWebViewSettings(w);
135 w.setScrollListener(this);
136 w.getSettings().setDisplayZoomControls(false);
Dave Burke20b6f1f2011-02-01 12:59:04 +0000137 w.setExpandedTileBounds(true); // smoother scrolling
Michael Kolb66706532010-12-12 19:50:22 -0800138 return w;
139 }
140
141 @Override
142 public WebView createSubWebView(boolean privateBrowsing) {
143 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
144 // no scroll listener for subview
145 web.setScrollListener(null);
146 return web;
147 }
148
149 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800150 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
151 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800152 }
153
154 void stopWebViewScrolling() {
155 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
156 if (web != null) {
157 web.stopScroll();
158 }
159 }
160
161 // WebView callbacks
162
163 @Override
John Reck30c714c2010-12-16 17:30:34 -0800164 public void onProgressChanged(Tab tab) {
165 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800166 mTabBar.onProgress(tab, progress);
167 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800168 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800169 if (progress == 100) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800170 if (!mTitleBar.isEditingUrl()) {
171 hideTitleBar();
Michael Kolbbd018d42010-12-15 15:43:39 -0800172 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800173 mTitleBar.setShowProgressOnly(false);
174 setTitleGravity(Gravity.BOTTOM);
Michael Kolbbd018d42010-12-15 15:43:39 -0800175 }
Michael Kolb376b5412010-12-15 11:52:57 -0800176 }
Michael Kolb66706532010-12-12 19:50:22 -0800177 } else {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800178 if (!isTitleBarShowing()) {
179 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
180 mTitleBar.setShowProgressOnly(true);
181 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800182 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800183 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800184 }
Michael Kolb66706532010-12-12 19:50:22 -0800185 }
186 }
187 }
188
189 @Override
190 public boolean needsRestoreAllTabs() {
191 return true;
192 }
193
194 @Override
195 public void addTab(Tab tab) {
196 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800197 }
198
199 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800200 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800201 }
202
203 @Override
204 public void setActiveTab(Tab tab) {
205 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800206 ScrollWebView view = (ScrollWebView) tab.getWebView();
207 // TabControl.setCurrentTab has been called before this,
208 // so the tab is guaranteed to have a webview
209 if (view == null) {
210 Log.e(LOGTAG, "active tab with no webview detected");
211 return;
212 }
213 // Request focus on the top window.
214 if (mUseQuickControls) {
215 mPieControl.forceToTop(mContentView);
216 view.setScrollListener(null);
217 mTabBar.showTitleBarIndicator(false);
218 } else {
219 view.setEmbeddedTitleBar(mTitleBar);
220 view.setScrollListener(this);
221 }
Michael Kolb66706532010-12-12 19:50:22 -0800222 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800223 if (tab.isInVoiceSearchMode()) {
224 showVoiceTitleBar(tab.getVoiceDisplayTitle());
225 } else {
226 revertVoiceTitleBar(tab);
227 }
Michael Kolb376b5412010-12-15 11:52:57 -0800228 updateLockIconToLatest(tab);
229 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231
232 @Override
233 public void updateTabs(List<Tab> tabs) {
234 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800235 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
238 @Override
239 public void removeTab(Tab tab) {
240 super.removeTab(tab);
241 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800242 }
243
244 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800245 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800246 }
247
Michael Kolb376b5412010-12-15 11:52:57 -0800248 int getContentWidth() {
249 if (mContentView != null) {
250 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800251 }
252 return 0;
253 }
254
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800255 @Override
256 public void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800257 if (mUiController.isInCustomActionMode()) {
258 mUiController.endActionMode();
259 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800260 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800261 mTitleBar.startEditingUrl(clearInput);
Michael Kolb66706532010-12-12 19:50:22 -0800262 }
263
Michael Kolb7cdc4902011-02-03 17:54:40 -0800264 void showTitleBarAndEdit() {
265 mTitleBar.setShowProgressOnly(false);
266 showTitleBar();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800267 mTitleBar.startEditingUrl(false);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800268 }
269
270 void stopEditingUrl() {
271 mTitleBar.stopEditingUrl();
272 }
273
274 @Override
275 protected void showTitleBar() {
276 if (canShowTitleBar()) {
277 if (mUseQuickControls) {
278 setTitleGravity(Gravity.BOTTOM);
279 mContentView.addView(mTitleBar);
280 } else {
281 setTitleGravity(Gravity.TOP);
282 }
283 super.showTitleBar();
284 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800285 }
Michael Kolb376b5412010-12-15 11:52:57 -0800286 }
287
Michael Kolb66706532010-12-12 19:50:22 -0800288 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800289 protected void hideTitleBar() {
290 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800291 mTabBar.onHideTitleBar();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800292 setTitleGravity(Gravity.NO_GRAVITY);
293 if (mUseQuickControls) {
294 mContentView.removeView(mTitleBar);
295 }
296 super.hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800297 }
298 }
299
Michael Kolb5ee018e2011-02-18 15:47:21 -0800300 public boolean isEditingUrl() {
301 return mTitleBar.isEditingUrl();
302 }
303
Michael Kolb66706532010-12-12 19:50:22 -0800304 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800305 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800306 return mTitleBar;
307 }
308
309 // action mode callbacks
310
311 @Override
312 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800313 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800314 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800315 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800316 }
317 }
318
319 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800320 public void onActionModeFinished(boolean inLoad) {
321 checkTabCount();
322 if (inLoad) {
323 // the titlebar was removed when the CAB was shown
324 // if the page is loading, show it again
Michael Kolb7cdc4902011-02-03 17:54:40 -0800325 mTitleBar.setShowProgressOnly(true);
326 if (!isTitleBarShowing()) {
327 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800328 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800329 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800330 }
331 }
332
333 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800334 protected void updateNavigationState(Tab tab) {
335 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800336 }
337
338 @Override
John Reck30c714c2010-12-16 17:30:34 -0800339 public void setUrlTitle(Tab tab) {
340 super.setUrlTitle(tab);
341 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800342 }
343
344 // Set the favicon in the title bar.
345 @Override
John Reck30c714c2010-12-16 17:30:34 -0800346 public void setFavicon(Tab tab) {
347 super.setFavicon(tab);
348 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800349 }
350
Michael Kolbcfa3af52010-12-14 10:36:11 -0800351 @Override
352 public void showVoiceTitleBar(String title) {
353 List<String> vsresults = null;
354 if (getActiveTab() != null) {
355 vsresults = getActiveTab().getVoiceSearchResults();
356 }
Michael Kolb3a2a1642011-02-15 10:52:07 -0800357 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800358 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800359 }
360
361 @Override
362 public void revertVoiceTitleBar(Tab tab) {
363 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800364 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800365 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800366 }
367
John Reckd73c5a22010-12-22 10:22:50 -0800368 @Override
369 public void showCustomView(View view, CustomViewCallback callback) {
370 super.showCustomView(view, callback);
371 mActivity.getActionBar().hide();
372 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800373
John Reckd73c5a22010-12-22 10:22:50 -0800374 @Override
375 public void onHideCustomView() {
376 super.onHideCustomView();
377 if (mUseQuickControls) {
378 checkTabCount();
379 } else {
380 mActivity.getActionBar().show();
381 }
382 }
Michael Kolba4183062011-01-16 10:43:21 -0800383
384 @Override
385 public boolean dispatchKey(int code, KeyEvent event) {
386 WebView web = getActiveTab().getWebView();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800387 if (event.getAction() == KeyEvent.ACTION_DOWN) {
388
389 switch (code) {
390 case KeyEvent.KEYCODE_TAB:
391 case KeyEvent.KEYCODE_DPAD_UP:
392 case KeyEvent.KEYCODE_DPAD_LEFT:
393 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
394 editUrl(false);
395 return true;
396 }
397 }
398 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
399 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
400 editUrl(true);
401 return mContentView.dispatchKeyEvent(event);
402 }
Michael Kolba4183062011-01-16 10:43:21 -0800403 }
404 return false;
405 }
406
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800407 private boolean isTypingKey(KeyEvent evt) {
408 return evt.getUnicodeChar() > 0;
409 }
410
411 TabBar getTabBar() {
412 return mTabBar;
413 }
414
Michael Kolb66706532010-12-12 19:50:22 -0800415}