blob: 4bfd3cf233f42b0077b1e3d0aadd002708a3c642 [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 */
38public class XLargeUi extends BaseUi implements ScrollListener {
39
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) {
156 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800157 }
158
159 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700160 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800161 if (web != null) {
162 web.stopScroll();
163 }
164 }
165
166 // WebView callbacks
167
168 @Override
John Reck30c714c2010-12-16 17:30:34 -0800169 public void onProgressChanged(Tab tab) {
170 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800171 mTabBar.onProgress(tab, progress);
172 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800173 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800174 }
175 }
176
177 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800178 public void addTab(Tab tab) {
179 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800180 }
181
182 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800183 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800184 }
185
186 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800187 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700188 mTitleBar.cancelTitleBarAnimation(true);
189 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolbeb95db42011-03-03 10:38:40 -0800190 if (mUseQuickControls) {
191 if (mActiveTab != null) {
192 captureTab(mActiveTab);
193 }
194 }
Michael Kolbf2628922011-03-09 17:15:28 -0800195 super.setActiveTab(tab, true);
196 setActiveTab(tab, true);
Michael Kolb46f987e2011-04-05 10:39:10 -0700197 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb377ea312011-02-17 14:36:56 -0800198 }
199
200 @Override
201 void setActiveTab(Tab tab, boolean needsAttaching) {
John Reckb9a051b2011-03-18 11:55:48 -0700202 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800203 // TabControl.setCurrentTab has been called before this,
204 // so the tab is guaranteed to have a webview
205 if (view == null) {
206 Log.e(LOGTAG, "active tab with no webview detected");
207 return;
208 }
209 // Request focus on the top window.
210 if (mUseQuickControls) {
211 mPieControl.forceToTop(mContentView);
212 view.setScrollListener(null);
213 mTabBar.showTitleBarIndicator(false);
214 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800215 // check if title bar is already attached by animation
216 if (mTitleBar.getParent() == null) {
217 view.setEmbeddedTitleBar(mTitleBar);
218 }
Michael Kolb376b5412010-12-15 11:52:57 -0800219 view.setScrollListener(this);
220 }
Michael Kolb66706532010-12-12 19:50:22 -0800221 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800222 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700223 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800224 } else {
225 revertVoiceTitleBar(tab);
226 }
Michael Kolb376b5412010-12-15 11:52:57 -0800227 updateLockIconToLatest(tab);
228 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800229 }
230
231 @Override
232 public void updateTabs(List<Tab> tabs) {
233 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800234 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800235 }
236
237 @Override
238 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700239 mTitleBar.cancelTitleBarAnimation(true);
240 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800241 super.removeTab(tab);
242 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700243 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800244 }
245
246 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800247 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800248 }
249
Michael Kolb376b5412010-12-15 11:52:57 -0800250 int getContentWidth() {
251 if (mContentView != null) {
252 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800253 }
254 return 0;
255 }
256
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800257 @Override
258 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700259 if (mUseQuickControls) {
260 getTitleBar().setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800261 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700262 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800263 }
264
265 void stopEditingUrl() {
266 mTitleBar.stopEditingUrl();
267 }
268
269 @Override
270 protected void showTitleBar() {
271 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700272 mTitleBar.show();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800273 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800274 }
Michael Kolb376b5412010-12-15 11:52:57 -0800275 }
276
Michael Kolb66706532010-12-12 19:50:22 -0800277 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800278 protected void hideTitleBar() {
279 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800280 mTabBar.onHideTitleBar();
Michael Kolb46f987e2011-04-05 10:39:10 -0700281 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800282 }
283 }
284
Michael Kolb5ee018e2011-02-18 15:47:21 -0800285 public boolean isEditingUrl() {
286 return mTitleBar.isEditingUrl();
287 }
288
Michael Kolb66706532010-12-12 19:50:22 -0800289 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800290 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800291 return mTitleBar;
292 }
293
Michael Kolb2a56eca2011-02-25 09:45:06 -0800294 @Override
295 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700296 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800297 super.setTitleGravity(gravity);
298 }
299 }
300
Michael Kolb66706532010-12-12 19:50:22 -0800301 // action mode callbacks
302
303 @Override
304 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800305 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800306 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800307 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800308 }
309 }
310
311 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800312 public void onActionModeFinished(boolean inLoad) {
313 checkTabCount();
314 if (inLoad) {
315 // the titlebar was removed when the CAB was shown
316 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800317 if (mUseQuickControls) {
318 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800319 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800320 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800321 }
322 }
323
324 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800325 protected void updateNavigationState(Tab tab) {
326 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800327 }
328
329 @Override
John Reck30c714c2010-12-16 17:30:34 -0800330 public void setUrlTitle(Tab tab) {
331 super.setUrlTitle(tab);
332 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800333 }
334
335 // Set the favicon in the title bar.
336 @Override
John Reck30c714c2010-12-16 17:30:34 -0800337 public void setFavicon(Tab tab) {
338 super.setFavicon(tab);
339 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800340 }
341
Michael Kolbcfa3af52010-12-14 10:36:11 -0800342 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700343 public void showVoiceTitleBar(String title, List<String> vsresults) {
Michael Kolb3a2a1642011-02-15 10:52:07 -0800344 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800345 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800346 }
347
348 @Override
349 public void revertVoiceTitleBar(Tab tab) {
350 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800351 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800352 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800353 }
354
John Reckd73c5a22010-12-22 10:22:50 -0800355 @Override
356 public void showCustomView(View view, CustomViewCallback callback) {
357 super.showCustomView(view, callback);
358 mActivity.getActionBar().hide();
359 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800360
John Reckd73c5a22010-12-22 10:22:50 -0800361 @Override
362 public void onHideCustomView() {
363 super.onHideCustomView();
364 if (mUseQuickControls) {
365 checkTabCount();
366 } else {
367 mActivity.getActionBar().show();
368 }
369 }
Michael Kolba4183062011-01-16 10:43:21 -0800370
371 @Override
372 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800373 if (mActiveTab != null) {
374 WebView web = mActiveTab.getWebView();
375 if (event.getAction() == KeyEvent.ACTION_DOWN) {
376 switch (code) {
377 case KeyEvent.KEYCODE_TAB:
378 case KeyEvent.KEYCODE_DPAD_UP:
379 case KeyEvent.KEYCODE_DPAD_LEFT:
380 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
381 editUrl(false);
382 return true;
383 }
384 }
385 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
386 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
387 editUrl(true);
388 return mContentView.dispatchKeyEvent(event);
389 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800390 }
Michael Kolba4183062011-01-16 10:43:21 -0800391 }
392 return false;
393 }
394
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800395 private boolean isTypingKey(KeyEvent evt) {
396 return evt.getUnicodeChar() > 0;
397 }
398
399 TabBar getTabBar() {
400 return mTabBar;
401 }
402
Michael Kolb66706532010-12-12 19:50:22 -0800403}