blob: 4b06d288fd38f1cc06f3c4cc9247052edd880f0e [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;
Michael Kolb1acef692011-03-08 14:12:06 -080029import android.view.Menu;
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;
Michael Kolbba238702011-03-08 10:40:50 -080050 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080051
Michael Kolb66706532010-12-12 19:50:22 -080052 /**
53 * @param browser
54 * @param controller
55 */
56 public XLargeUi(Activity browser, UiController controller) {
57 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080058 mHandler = new Handler();
Michael Kolb46f987e2011-04-05 10:39:10 -070059 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this,
60 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080061 mTitleBar.setProgress(100);
Michael Kolb66706532010-12-12 19:50:22 -080062 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080063 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080064 setupActionBar();
65 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
66 }
67
68 private void setupActionBar() {
69 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080070 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
71 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080072 }
73
74 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080075 public void showComboView(boolean startWithHistory, Bundle extras) {
76 super.showComboView(startWithHistory, extras);
77 if (mUseQuickControls) {
78 mActionBar.show();
79 }
80 }
81
82 @Override
John Reckb3417f02011-01-14 11:01:05 -080083 public void hideComboView() {
Michael Kolbba238702011-03-08 10:40:50 -080084 if (isComboViewShowing()) {
85 super.hideComboView();
86 // ComboView changes the action bar, set it back up to what we want
87 setupActionBar();
88 checkTabCount();
89 }
Michael Kolb376b5412010-12-15 11:52:57 -080090 }
91
92 private void setUseQuickControls(boolean useQuickControls) {
93 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080094 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080095 if (useQuickControls) {
96 checkTabCount();
97 mPieControl = new PieControl(mActivity, mUiController, this);
98 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070099 WebView web = getWebView();
100 if (web != null) {
101 web.setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -0800102 }
103 } else {
104 mActivity.getActionBar().show();
105 if (mPieControl != null) {
106 mPieControl.removeFromContainer(mContentView);
107 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700108 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800109 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800110 web.setEmbeddedTitleBar(mTitleBar);
111 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700112 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800113 }
114 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800115 }
116
117 private void checkTabCount() {
118 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800119 mHandler.post(new Runnable() {
120 public void run() {
121 mActionBar.hide();
122 }
123 });
Michael Kolb376b5412010-12-15 11:52:57 -0800124 }
Michael Kolb66706532010-12-12 19:50:22 -0800125 }
126
127 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000128 public void onResume() {
129 super.onResume();
130 if (!BrowserSettings.getInstance().useInstant()) {
131 mTitleBar.clearCompletions();
132 }
133 }
134
135 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800136 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800137 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800138 }
139
140 // webview factory
141
142 @Override
143 public WebView createWebView(boolean privateBrowsing) {
144 // Create a new WebView
John Reckb9a051b2011-03-18 11:55:48 -0700145 BrowserWebView w = (BrowserWebView) super.createWebView(privateBrowsing);
Michael Kolb66706532010-12-12 19:50:22 -0800146 w.setScrollListener(this);
Michael Kolb66706532010-12-12 19:50:22 -0800147 return w;
148 }
149
150 @Override
151 public WebView createSubWebView(boolean privateBrowsing) {
John Reckb9a051b2011-03-18 11:55:48 -0700152 return super.createWebView(privateBrowsing);
Michael Kolb66706532010-12-12 19:50:22 -0800153 }
154
155 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800156 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
157 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
179 public boolean needsRestoreAllTabs() {
180 return true;
181 }
182
183 @Override
184 public void addTab(Tab tab) {
185 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800186 }
187
188 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800189 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800190 }
191
192 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800193 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700194 mTitleBar.cancelTitleBarAnimation(true);
195 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolbeb95db42011-03-03 10:38:40 -0800196 if (mUseQuickControls) {
197 if (mActiveTab != null) {
198 captureTab(mActiveTab);
199 }
200 }
Michael Kolbf2628922011-03-09 17:15:28 -0800201 super.setActiveTab(tab, true);
202 setActiveTab(tab, true);
Michael Kolb46f987e2011-04-05 10:39:10 -0700203 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb377ea312011-02-17 14:36:56 -0800204 }
205
206 @Override
207 void setActiveTab(Tab tab, boolean needsAttaching) {
John Reckb9a051b2011-03-18 11:55:48 -0700208 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800209 // TabControl.setCurrentTab has been called before this,
210 // so the tab is guaranteed to have a webview
211 if (view == null) {
212 Log.e(LOGTAG, "active tab with no webview detected");
213 return;
214 }
215 // Request focus on the top window.
216 if (mUseQuickControls) {
217 mPieControl.forceToTop(mContentView);
218 view.setScrollListener(null);
219 mTabBar.showTitleBarIndicator(false);
220 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800221 // check if title bar is already attached by animation
222 if (mTitleBar.getParent() == null) {
223 view.setEmbeddedTitleBar(mTitleBar);
224 }
Michael Kolb376b5412010-12-15 11:52:57 -0800225 view.setScrollListener(this);
226 }
Michael Kolb66706532010-12-12 19:50:22 -0800227 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800228 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700229 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800230 } else {
231 revertVoiceTitleBar(tab);
232 }
Michael Kolb376b5412010-12-15 11:52:57 -0800233 updateLockIconToLatest(tab);
234 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800235 }
236
237 @Override
238 public void updateTabs(List<Tab> tabs) {
239 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800240 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242
243 @Override
244 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700245 mTitleBar.cancelTitleBarAnimation(true);
246 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800247 super.removeTab(tab);
248 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700249 mTitleBar.setSkipTitleBarAnimations(false);
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
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800263 @Override
264 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700265 if (mUseQuickControls) {
266 getTitleBar().setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800267 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700268 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800269 }
270
271 void stopEditingUrl() {
272 mTitleBar.stopEditingUrl();
273 }
274
275 @Override
276 protected void showTitleBar() {
277 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700278 mTitleBar.show();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800279 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800280 }
Michael Kolb376b5412010-12-15 11:52:57 -0800281 }
282
Michael Kolb66706532010-12-12 19:50:22 -0800283 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800284 protected void hideTitleBar() {
285 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800286 mTabBar.onHideTitleBar();
Michael Kolb46f987e2011-04-05 10:39:10 -0700287 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800288 }
289 }
290
Michael Kolb5ee018e2011-02-18 15:47:21 -0800291 public boolean isEditingUrl() {
292 return mTitleBar.isEditingUrl();
293 }
294
Michael Kolb66706532010-12-12 19:50:22 -0800295 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800296 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800297 return mTitleBar;
298 }
299
Michael Kolb2a56eca2011-02-25 09:45:06 -0800300 @Override
301 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700302 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800303 super.setTitleGravity(gravity);
304 }
305 }
306
Michael Kolb66706532010-12-12 19:50:22 -0800307 // action mode callbacks
308
309 @Override
310 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800311 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800312 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800313 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800314 }
315 }
316
317 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800318 public void onActionModeFinished(boolean inLoad) {
319 checkTabCount();
320 if (inLoad) {
321 // the titlebar was removed when the CAB was shown
322 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800323 if (mUseQuickControls) {
324 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800325 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800326 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800327 }
328 }
329
330 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800331 protected void updateNavigationState(Tab tab) {
332 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800333 }
334
335 @Override
John Reck30c714c2010-12-16 17:30:34 -0800336 public void setUrlTitle(Tab tab) {
337 super.setUrlTitle(tab);
338 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800339 }
340
341 // Set the favicon in the title bar.
342 @Override
John Reck30c714c2010-12-16 17:30:34 -0800343 public void setFavicon(Tab tab) {
344 super.setFavicon(tab);
345 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800346 }
347
Michael Kolbcfa3af52010-12-14 10:36:11 -0800348 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700349 public void showVoiceTitleBar(String title, List<String> vsresults) {
Michael Kolb3a2a1642011-02-15 10:52:07 -0800350 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800351 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800352 }
353
354 @Override
355 public void revertVoiceTitleBar(Tab tab) {
356 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800357 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800358 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800359 }
360
John Reckd73c5a22010-12-22 10:22:50 -0800361 @Override
362 public void showCustomView(View view, CustomViewCallback callback) {
363 super.showCustomView(view, callback);
364 mActivity.getActionBar().hide();
365 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800366
John Reckd73c5a22010-12-22 10:22:50 -0800367 @Override
368 public void onHideCustomView() {
369 super.onHideCustomView();
370 if (mUseQuickControls) {
371 checkTabCount();
372 } else {
373 mActivity.getActionBar().show();
374 }
375 }
Michael Kolba4183062011-01-16 10:43:21 -0800376
377 @Override
378 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800379 if (mActiveTab != null) {
380 WebView web = mActiveTab.getWebView();
381 if (event.getAction() == KeyEvent.ACTION_DOWN) {
382 switch (code) {
383 case KeyEvent.KEYCODE_TAB:
384 case KeyEvent.KEYCODE_DPAD_UP:
385 case KeyEvent.KEYCODE_DPAD_LEFT:
386 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
387 editUrl(false);
388 return true;
389 }
390 }
391 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
392 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
393 editUrl(true);
394 return mContentView.dispatchKeyEvent(event);
395 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800396 }
Michael Kolba4183062011-01-16 10:43:21 -0800397 }
398 return false;
399 }
400
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800401 private boolean isTypingKey(KeyEvent evt) {
402 return evt.getUnicodeChar() > 0;
403 }
404
405 TabBar getTabBar() {
406 return mTabBar;
407 }
408
Narayan Kamath5119edd2011-02-23 15:49:17 +0000409 @Override
Michael Kolb1acef692011-03-08 14:12:06 -0800410 public boolean onPrepareOptionsMenu(Menu menu) {
411 if (mUseQuickControls) {
412 mPieControl.onMenuOpened(menu);
413 return false;
414 } else {
415 return true;
416 }
417 }
418
Michael Kolb66706532010-12-12 19:50:22 -0800419}