blob: 84ad6ead50bebc75d2dccddd776bb6b81afb7c6c [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
John Reckb9a051b2011-03-18 11:55:48 -070019import com.android.browser.BrowserWebView.ScrollListener;
Michael Kolb66706532010-12-12 19:50:22 -080020
21import android.app.ActionBar;
22import android.app.Activity;
Michael Kolbac35bdc2011-01-17 17:06:04 -080023import android.os.Bundle;
Michael Kolbba238702011-03-08 10:40:50 -080024import android.os.Handler;
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 */
John Reck5d43ce82011-06-21 17:16:51 -070038public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080039
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;
Michael Kolbba238702011-03-08 10:40:50 -080049 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080050
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);
Michael Kolbba238702011-03-08 10:40:50 -080057 mHandler = new Handler();
Michael Kolb46f987e2011-04-05 10:39:10 -070058 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this,
59 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080060 mTitleBar.setProgress(100);
Michael Kolb66706532010-12-12 19:50:22 -080061 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080062 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080063 setupActionBar();
64 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
65 }
66
67 private void setupActionBar() {
68 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080069 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
70 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080071 }
72
73 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080074 public void showComboView(boolean startWithHistory, Bundle extras) {
75 super.showComboView(startWithHistory, extras);
76 if (mUseQuickControls) {
77 mActionBar.show();
78 }
79 }
80
81 @Override
John Reckb3417f02011-01-14 11:01:05 -080082 public void hideComboView() {
Michael Kolbba238702011-03-08 10:40:50 -080083 if (isComboViewShowing()) {
84 super.hideComboView();
85 // ComboView changes the action bar, set it back up to what we want
86 setupActionBar();
87 checkTabCount();
88 }
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 Kolb46f987e2011-04-05 10:39:10 -070098 WebView web = getWebView();
99 if (web != null) {
100 web.setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -0800101 }
102 } else {
103 mActivity.getActionBar().show();
104 if (mPieControl != null) {
105 mPieControl.removeFromContainer(mContentView);
106 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700107 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800108 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800109 web.setEmbeddedTitleBar(mTitleBar);
110 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700111 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800112 }
113 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800114 }
115
116 private void checkTabCount() {
117 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800118 mHandler.post(new Runnable() {
119 public void run() {
120 mActionBar.hide();
121 }
122 });
Michael Kolb376b5412010-12-15 11:52:57 -0800123 }
Michael Kolb66706532010-12-12 19:50:22 -0800124 }
125
126 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000127 public void onResume() {
128 super.onResume();
John Reck35e9dd62011-04-25 09:01:54 -0700129 if (!BrowserSettings.getInstance().useInstantSearch()) {
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000130 mTitleBar.clearCompletions();
131 }
132 }
133
134 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800135 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800136 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800137 }
138
139 // webview factory
140
141 @Override
142 public WebView createWebView(boolean privateBrowsing) {
143 // Create a new WebView
John Reckb9a051b2011-03-18 11:55:48 -0700144 BrowserWebView w = (BrowserWebView) super.createWebView(privateBrowsing);
Michael Kolb66706532010-12-12 19:50:22 -0800145 w.setScrollListener(this);
Michael Kolb66706532010-12-12 19:50:22 -0800146 return w;
147 }
148
149 @Override
150 public WebView createSubWebView(boolean privateBrowsing) {
John Reckb9a051b2011-03-18 11:55:48 -0700151 return super.createWebView(privateBrowsing);
Michael Kolb66706532010-12-12 19:50:22 -0800152 }
153
154 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800155 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
John Reck5d43ce82011-06-21 17:16:51 -0700156 super.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb5ee018e2011-02-18 15:47:21 -0800157 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800158 }
159
160 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700161 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800162 if (web != null) {
163 web.stopScroll();
164 }
165 }
166
167 // WebView callbacks
168
169 @Override
John Reck30c714c2010-12-16 17:30:34 -0800170 public void onProgressChanged(Tab tab) {
171 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800172 mTabBar.onProgress(tab, progress);
173 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800174 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800175 }
176 }
177
178 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800179 public void addTab(Tab tab) {
180 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800181 }
182
183 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800184 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800185 }
186
187 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800188 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700189 mTitleBar.cancelTitleBarAnimation(true);
190 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolbeb95db42011-03-03 10:38:40 -0800191 if (mUseQuickControls) {
192 if (mActiveTab != null) {
193 captureTab(mActiveTab);
194 }
195 }
John Reck5d43ce82011-06-21 17:16:51 -0700196 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700197 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800198 // TabControl.setCurrentTab has been called before this,
199 // so the tab is guaranteed to have a webview
200 if (view == null) {
201 Log.e(LOGTAG, "active tab with no webview detected");
202 return;
203 }
204 // Request focus on the top window.
205 if (mUseQuickControls) {
206 mPieControl.forceToTop(mContentView);
207 view.setScrollListener(null);
208 mTabBar.showTitleBarIndicator(false);
209 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800210 // check if title bar is already attached by animation
John Reck541f55a2011-06-07 16:34:43 -0700211 if (mTitleBar.getParent() == null && !tab.isSnapshot()) {
Michael Kolb377ea312011-02-17 14:36:56 -0800212 view.setEmbeddedTitleBar(mTitleBar);
213 }
Michael Kolb376b5412010-12-15 11:52:57 -0800214 view.setScrollListener(this);
215 }
Michael Kolb66706532010-12-12 19:50:22 -0800216 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800217 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700218 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800219 } else {
220 revertVoiceTitleBar(tab);
221 }
Michael Kolb376b5412010-12-15 11:52:57 -0800222 updateLockIconToLatest(tab);
223 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700224 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800225 }
226
227 @Override
228 public void updateTabs(List<Tab> tabs) {
229 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800230 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800231 }
232
233 @Override
234 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700235 mTitleBar.cancelTitleBarAnimation(true);
236 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800237 super.removeTab(tab);
238 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700239 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800240 }
241
242 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800243 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800244 }
245
Michael Kolb376b5412010-12-15 11:52:57 -0800246 int getContentWidth() {
247 if (mContentView != null) {
248 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800249 }
250 return 0;
251 }
252
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800253 @Override
254 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700255 if (mUseQuickControls) {
256 getTitleBar().setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800257 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700258 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800259 }
260
261 void stopEditingUrl() {
262 mTitleBar.stopEditingUrl();
263 }
264
265 @Override
266 protected void showTitleBar() {
267 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700268 mTitleBar.show();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800269 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800270 }
Michael Kolb376b5412010-12-15 11:52:57 -0800271 }
272
Michael Kolb66706532010-12-12 19:50:22 -0800273 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800274 protected void hideTitleBar() {
275 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800276 mTabBar.onHideTitleBar();
Michael Kolb46f987e2011-04-05 10:39:10 -0700277 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800278 }
279 }
280
281 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800282 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800283 return mTitleBar;
284 }
285
Michael Kolb2a56eca2011-02-25 09:45:06 -0800286 @Override
287 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700288 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800289 super.setTitleGravity(gravity);
290 }
291 }
292
Michael Kolb66706532010-12-12 19:50:22 -0800293 // action mode callbacks
294
295 @Override
296 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800297 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800298 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800299 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800300 }
301 }
302
303 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800304 public void onActionModeFinished(boolean inLoad) {
305 checkTabCount();
306 if (inLoad) {
307 // the titlebar was removed when the CAB was shown
308 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800309 if (mUseQuickControls) {
310 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800311 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800312 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800313 }
314 }
315
316 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800317 protected void updateNavigationState(Tab tab) {
318 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800319 }
320
321 @Override
John Reck30c714c2010-12-16 17:30:34 -0800322 public void setUrlTitle(Tab tab) {
323 super.setUrlTitle(tab);
324 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800325 }
326
327 // Set the favicon in the title bar.
328 @Override
John Reck30c714c2010-12-16 17:30:34 -0800329 public void setFavicon(Tab tab) {
330 super.setFavicon(tab);
331 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800332 }
333
Michael Kolbcfa3af52010-12-14 10:36:11 -0800334 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700335 public void showVoiceTitleBar(String title, List<String> vsresults) {
Michael Kolb3a2a1642011-02-15 10:52:07 -0800336 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800337 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800338 }
339
340 @Override
341 public void revertVoiceTitleBar(Tab tab) {
342 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800343 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800344 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800345 }
346
John Reckd73c5a22010-12-22 10:22:50 -0800347 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400348 public void showCustomView(View view, int requestedOrientation,
349 CustomViewCallback callback) {
350 super.showCustomView(view, requestedOrientation, callback);
John Reckd73c5a22010-12-22 10:22:50 -0800351 mActivity.getActionBar().hide();
352 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800353
John Reckd73c5a22010-12-22 10:22:50 -0800354 @Override
355 public void onHideCustomView() {
356 super.onHideCustomView();
357 if (mUseQuickControls) {
358 checkTabCount();
359 } else {
360 mActivity.getActionBar().show();
361 }
362 }
Michael Kolba4183062011-01-16 10:43:21 -0800363
364 @Override
365 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800366 if (mActiveTab != null) {
367 WebView web = mActiveTab.getWebView();
368 if (event.getAction() == KeyEvent.ACTION_DOWN) {
369 switch (code) {
370 case KeyEvent.KEYCODE_TAB:
371 case KeyEvent.KEYCODE_DPAD_UP:
372 case KeyEvent.KEYCODE_DPAD_LEFT:
373 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
374 editUrl(false);
375 return true;
376 }
377 }
378 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
379 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
380 editUrl(true);
381 return mContentView.dispatchKeyEvent(event);
382 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800383 }
Michael Kolba4183062011-01-16 10:43:21 -0800384 }
385 return false;
386 }
387
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800388 private boolean isTypingKey(KeyEvent evt) {
389 return evt.getUnicodeChar() > 0;
390 }
391
392 TabBar getTabBar() {
393 return mTabBar;
394 }
395
Michael Kolb66706532010-12-12 19:50:22 -0800396}