blob: 3e200ea6dc68ee0f8cbc41e7fa610ec50a742fde [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
150 public void onScroll(int visibleTitleHeight) {
151 mTabBar.onScroll(visibleTitleHeight);
152 }
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
Michael Kolb66706532010-12-12 19:50:22 -0800164 public void bookmarkedStatusHasChanged(Tab tab) {
165 if (tab.inForeground()) {
166 boolean isBookmark = tab.isBookmarkedSite();
167 mTitleBar.setCurrentUrlIsBookmark(isBookmark);
Michael Kolb66706532010-12-12 19:50:22 -0800168 }
169 }
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
212 public void setActiveTab(Tab tab) {
213 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800214 ScrollWebView view = (ScrollWebView) tab.getWebView();
215 // TabControl.setCurrentTab has been called before this,
216 // so the tab is guaranteed to have a webview
217 if (view == null) {
218 Log.e(LOGTAG, "active tab with no webview detected");
219 return;
220 }
221 // Request focus on the top window.
222 if (mUseQuickControls) {
223 mPieControl.forceToTop(mContentView);
224 view.setScrollListener(null);
225 mTabBar.showTitleBarIndicator(false);
226 } else {
227 view.setEmbeddedTitleBar(mTitleBar);
228 view.setScrollListener(this);
229 }
Michael Kolb66706532010-12-12 19:50:22 -0800230 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800231 if (tab.isInVoiceSearchMode()) {
232 showVoiceTitleBar(tab.getVoiceDisplayTitle());
233 } else {
234 revertVoiceTitleBar(tab);
235 }
Michael Kolb376b5412010-12-15 11:52:57 -0800236 updateLockIconToLatest(tab);
237 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800238 }
239
240 @Override
241 public void updateTabs(List<Tab> tabs) {
242 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800243 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800244 }
245
246 @Override
247 public void removeTab(Tab tab) {
248 super.removeTab(tab);
249 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800250 }
251
252 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800253 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800254 }
255
Michael Kolb376b5412010-12-15 11:52:57 -0800256 int getContentWidth() {
257 if (mContentView != null) {
258 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800259 }
260 return 0;
261 }
262
263 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();
268 mTitleBar.onEditUrl(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();
274 mTitleBar.onEditUrl(false);
275 }
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
307 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800308 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800309 return mTitleBar;
310 }
311
312 // action mode callbacks
313
314 @Override
315 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800316 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800317 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800318 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800319 }
320 }
321
322 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800323 public void onActionModeFinished(boolean inLoad) {
324 checkTabCount();
325 if (inLoad) {
326 // the titlebar was removed when the CAB was shown
327 // if the page is loading, show it again
Michael Kolb7cdc4902011-02-03 17:54:40 -0800328 mTitleBar.setShowProgressOnly(true);
329 if (!isTitleBarShowing()) {
330 setTitleGravity(Gravity.TOP);
Michael Kolb376b5412010-12-15 11:52:57 -0800331 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800332 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800333 }
334 }
335
336 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800337 protected void updateNavigationState(Tab tab) {
338 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800339 }
340
341 @Override
John Reck30c714c2010-12-16 17:30:34 -0800342 public void setUrlTitle(Tab tab) {
343 super.setUrlTitle(tab);
344 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800345 }
346
347 // Set the favicon in the title bar.
348 @Override
John Reck30c714c2010-12-16 17:30:34 -0800349 public void setFavicon(Tab tab) {
350 super.setFavicon(tab);
351 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800352 }
353
Michael Kolbcfa3af52010-12-14 10:36:11 -0800354 @Override
355 public void showVoiceTitleBar(String title) {
356 List<String> vsresults = null;
357 if (getActiveTab() != null) {
358 vsresults = getActiveTab().getVoiceSearchResults();
359 }
360 mTitleBar.setInVoiceMode(true, null);
361 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800362 }
363
364 @Override
365 public void revertVoiceTitleBar(Tab tab) {
366 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800367 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800368 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800369 }
370
John Reckd73c5a22010-12-22 10:22:50 -0800371 @Override
372 public void showCustomView(View view, CustomViewCallback callback) {
373 super.showCustomView(view, callback);
374 mActivity.getActionBar().hide();
375 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800376
John Reckd73c5a22010-12-22 10:22:50 -0800377 @Override
378 public void onHideCustomView() {
379 super.onHideCustomView();
380 if (mUseQuickControls) {
381 checkTabCount();
382 } else {
383 mActivity.getActionBar().show();
384 }
385 }
Michael Kolba4183062011-01-16 10:43:21 -0800386
387 @Override
388 public boolean dispatchKey(int code, KeyEvent event) {
389 WebView web = getActiveTab().getWebView();
390 switch (code) {
391 case KeyEvent.KEYCODE_TAB:
392 case KeyEvent.KEYCODE_DPAD_UP:
393 case KeyEvent.KEYCODE_DPAD_LEFT:
394 if ((web != null) && web.hasFocus()) {
395 editUrl(true);
396 return true;
397 }
398 }
Michael Kolb47171d82011-01-28 10:34:15 -0800399 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800400 if (!ctrl && event.isPrintingKey() && !mTitleBar.isEditingUrl()) {
Michael Kolba4183062011-01-16 10:43:21 -0800401 editUrl(true);
402 return mContentView.dispatchKeyEvent(event);
403 }
404 return false;
405 }
406
Michael Kolb66706532010-12-12 19:50:22 -0800407}