blob: 8b43a7486e7865a3c26cd5ca0158ea99103ca2f3 [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
Michael Kolb66706532010-12-12 19:50:22 -080019import android.app.ActionBar;
20import android.app.Activity;
Michael Kolbac35bdc2011-01-17 17:06:04 -080021import android.os.Bundle;
Michael Kolbba238702011-03-08 10:40:50 -080022import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080023import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080024import android.view.ActionMode;
Michael Kolb376b5412010-12-15 11:52:57 -080025import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080026import android.view.KeyEvent;
John Reckd73c5a22010-12-22 10:22:50 -080027import android.view.View;
28import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080029import android.webkit.WebView;
30
John Reckbf2ec202011-06-29 17:47:38 -070031import com.android.browser.BrowserWebView.ScrollListener;
32
Michael Kolb66706532010-12-12 19:50:22 -080033import java.util.List;
34
35/**
36 * Ui for xlarge screen sizes
37 */
John Reckbf2ec202011-06-29 17:47:38 -070038public class XLargeUi extends BaseUi implements ScrollListener {
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 PieControl mPieControl;
Michael Kolbba238702011-03-08 10:40:50 -080048 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080049
Michael Kolb66706532010-12-12 19:50:22 -080050 /**
51 * @param browser
52 * @param controller
53 */
54 public XLargeUi(Activity browser, UiController controller) {
55 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080056 mHandler = new Handler();
Michael Kolb46f987e2011-04-05 10:39:10 -070057 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this,
58 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080059 mTitleBar.setProgress(100);
Michael Kolb66706532010-12-12 19:50:22 -080060 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080061 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080062 setupActionBar();
63 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
64 }
65
66 private void setupActionBar() {
67 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080068 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
69 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080070 }
71
72 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080073 public void showComboView(boolean startWithHistory, Bundle extras) {
74 super.showComboView(startWithHistory, extras);
75 if (mUseQuickControls) {
76 mActionBar.show();
77 }
78 }
79
80 @Override
John Reckb3417f02011-01-14 11:01:05 -080081 public void hideComboView() {
Michael Kolbba238702011-03-08 10:40:50 -080082 if (isComboViewShowing()) {
83 super.hideComboView();
84 // ComboView changes the action bar, set it back up to what we want
85 setupActionBar();
86 checkTabCount();
87 }
Michael Kolb376b5412010-12-15 11:52:57 -080088 }
89
90 private void setUseQuickControls(boolean useQuickControls) {
91 mUseQuickControls = useQuickControls;
Michael Kolb7cdc4902011-02-03 17:54:40 -080092 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -080093 if (useQuickControls) {
94 checkTabCount();
95 mPieControl = new PieControl(mActivity, mUiController, this);
96 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -070097 WebView web = getWebView();
98 if (web != null) {
99 web.setEmbeddedTitleBar(null);
Michael Kolb376b5412010-12-15 11:52:57 -0800100 }
101 } else {
102 mActivity.getActionBar().show();
103 if (mPieControl != null) {
104 mPieControl.removeFromContainer(mContentView);
105 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700106 WebView web = getWebView();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800107 if (web != null) {
Michael Kolb376b5412010-12-15 11:52:57 -0800108 web.setEmbeddedTitleBar(mTitleBar);
109 }
Michael Kolb8a4c3822011-03-15 14:52:05 -0700110 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb376b5412010-12-15 11:52:57 -0800111 }
112 mTabBar.setUseQuickControls(mUseQuickControls);
Michael Kolb376b5412010-12-15 11:52:57 -0800113 }
114
115 private void checkTabCount() {
116 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -0800117 mHandler.post(new Runnable() {
118 public void run() {
119 mActionBar.hide();
120 }
121 });
Michael Kolb376b5412010-12-15 11:52:57 -0800122 }
Michael Kolb66706532010-12-12 19:50:22 -0800123 }
124
125 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000126 public void onResume() {
127 super.onResume();
John Reck35e9dd62011-04-25 09:01:54 -0700128 if (!BrowserSettings.getInstance().useInstantSearch()) {
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000129 mTitleBar.clearCompletions();
130 }
131 }
132
133 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800134 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800135 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800136 }
137
138 // webview factory
139
140 @Override
141 public WebView createWebView(boolean privateBrowsing) {
142 // Create a new WebView
John Reckb9a051b2011-03-18 11:55:48 -0700143 BrowserWebView w = (BrowserWebView) super.createWebView(privateBrowsing);
Michael Kolb66706532010-12-12 19:50:22 -0800144 w.setScrollListener(this);
Michael Kolb66706532010-12-12 19:50:22 -0800145 return w;
146 }
147
148 @Override
149 public WebView createSubWebView(boolean privateBrowsing) {
John Reckb9a051b2011-03-18 11:55:48 -0700150 return super.createWebView(privateBrowsing);
Michael Kolb66706532010-12-12 19:50:22 -0800151 }
152
153 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800154 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
155 mTabBar.onScroll(visibleTitleHeight, userInitiated);
Michael Kolb66706532010-12-12 19:50:22 -0800156 }
157
158 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700159 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Michael Kolb66706532010-12-12 19:50:22 -0800160 if (web != null) {
161 web.stopScroll();
162 }
163 }
164
165 // WebView callbacks
166
167 @Override
John Reck30c714c2010-12-16 17:30:34 -0800168 public void onProgressChanged(Tab tab) {
169 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800170 mTabBar.onProgress(tab, progress);
171 if (tab.inForeground()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800172 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800173 }
174 }
175
176 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800177 public void addTab(Tab tab) {
178 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800179 }
180
181 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800182 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800183 }
184
185 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800186 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700187 mTitleBar.cancelTitleBarAnimation(true);
188 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolbeb95db42011-03-03 10:38:40 -0800189 if (mUseQuickControls) {
190 if (mActiveTab != null) {
191 captureTab(mActiveTab);
192 }
193 }
John Reck5d43ce82011-06-21 17:16:51 -0700194 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700195 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800196 // TabControl.setCurrentTab has been called before this,
197 // so the tab is guaranteed to have a webview
198 if (view == null) {
199 Log.e(LOGTAG, "active tab with no webview detected");
200 return;
201 }
202 // Request focus on the top window.
203 if (mUseQuickControls) {
204 mPieControl.forceToTop(mContentView);
205 view.setScrollListener(null);
206 mTabBar.showTitleBarIndicator(false);
207 } else {
Michael Kolb377ea312011-02-17 14:36:56 -0800208 // check if title bar is already attached by animation
John Reck541f55a2011-06-07 16:34:43 -0700209 if (mTitleBar.getParent() == null && !tab.isSnapshot()) {
Michael Kolb377ea312011-02-17 14:36:56 -0800210 view.setEmbeddedTitleBar(mTitleBar);
211 }
Michael Kolb376b5412010-12-15 11:52:57 -0800212 view.setScrollListener(this);
213 }
Michael Kolb66706532010-12-12 19:50:22 -0800214 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800215 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700216 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800217 } else {
218 revertVoiceTitleBar(tab);
219 }
Michael Kolb376b5412010-12-15 11:52:57 -0800220 updateLockIconToLatest(tab);
221 tab.getTopWindow().requestFocus();
John Reck5d43ce82011-06-21 17:16:51 -0700222 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800223 }
224
225 @Override
226 public void updateTabs(List<Tab> tabs) {
227 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800228 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800229 }
230
231 @Override
232 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700233 mTitleBar.cancelTitleBarAnimation(true);
234 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800235 super.removeTab(tab);
236 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700237 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800238 }
239
240 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800241 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800242 }
243
Michael Kolb376b5412010-12-15 11:52:57 -0800244 int getContentWidth() {
245 if (mContentView != null) {
246 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800247 }
248 return 0;
249 }
250
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800251 @Override
252 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700253 if (mUseQuickControls) {
254 getTitleBar().setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800255 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700256 super.editUrl(clearInput);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800257 }
258
259 void stopEditingUrl() {
260 mTitleBar.stopEditingUrl();
261 }
262
263 @Override
264 protected void showTitleBar() {
265 if (canShowTitleBar()) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700266 mTitleBar.show();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800267 mTabBar.onShowTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800268 }
Michael Kolb376b5412010-12-15 11:52:57 -0800269 }
270
Michael Kolb66706532010-12-12 19:50:22 -0800271 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800272 protected void hideTitleBar() {
273 if (isTitleBarShowing()) {
Michael Kolb66706532010-12-12 19:50:22 -0800274 mTabBar.onHideTitleBar();
Michael Kolb46f987e2011-04-05 10:39:10 -0700275 mTitleBar.hide();
Michael Kolb66706532010-12-12 19:50:22 -0800276 }
277 }
278
279 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800280 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800281 return mTitleBar;
282 }
283
Michael Kolb2a56eca2011-02-25 09:45:06 -0800284 @Override
285 protected void setTitleGravity(int gravity) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700286 if (!mUseQuickControls) {
Michael Kolb2a56eca2011-02-25 09:45:06 -0800287 super.setTitleGravity(gravity);
288 }
289 }
290
Michael Kolb66706532010-12-12 19:50:22 -0800291 // action mode callbacks
292
293 @Override
294 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800295 if (!mTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800296 // hide the fake title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800297 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800298 }
299 }
300
301 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800302 public void onActionModeFinished(boolean inLoad) {
303 checkTabCount();
304 if (inLoad) {
305 // the titlebar was removed when the CAB was shown
306 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800307 if (mUseQuickControls) {
308 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800309 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800310 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800311 }
312 }
313
314 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800315 protected void updateNavigationState(Tab tab) {
316 mTitleBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800317 }
318
319 @Override
John Reck30c714c2010-12-16 17:30:34 -0800320 public void setUrlTitle(Tab tab) {
321 super.setUrlTitle(tab);
322 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800323 }
324
325 // Set the favicon in the title bar.
326 @Override
John Reck30c714c2010-12-16 17:30:34 -0800327 public void setFavicon(Tab tab) {
328 super.setFavicon(tab);
329 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800330 }
331
Michael Kolbcfa3af52010-12-14 10:36:11 -0800332 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700333 public void showVoiceTitleBar(String title, List<String> vsresults) {
Michael Kolb3a2a1642011-02-15 10:52:07 -0800334 mTitleBar.setInVoiceMode(true, vsresults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800335 mTitleBar.setDisplayTitle(title);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800336 }
337
338 @Override
339 public void revertVoiceTitleBar(Tab tab) {
340 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800341 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800342 mTitleBar.setDisplayTitle(url);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800343 }
344
John Reckd73c5a22010-12-22 10:22:50 -0800345 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400346 public void showCustomView(View view, int requestedOrientation,
347 CustomViewCallback callback) {
348 super.showCustomView(view, requestedOrientation, callback);
John Reckd73c5a22010-12-22 10:22:50 -0800349 mActivity.getActionBar().hide();
350 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800351
John Reckd73c5a22010-12-22 10:22:50 -0800352 @Override
353 public void onHideCustomView() {
354 super.onHideCustomView();
355 if (mUseQuickControls) {
356 checkTabCount();
357 } else {
358 mActivity.getActionBar().show();
359 }
360 }
Michael Kolba4183062011-01-16 10:43:21 -0800361
362 @Override
363 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800364 if (mActiveTab != null) {
365 WebView web = mActiveTab.getWebView();
366 if (event.getAction() == KeyEvent.ACTION_DOWN) {
367 switch (code) {
368 case KeyEvent.KEYCODE_TAB:
369 case KeyEvent.KEYCODE_DPAD_UP:
370 case KeyEvent.KEYCODE_DPAD_LEFT:
371 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
372 editUrl(false);
373 return true;
374 }
375 }
376 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
377 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
378 editUrl(true);
379 return mContentView.dispatchKeyEvent(event);
380 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800381 }
Michael Kolba4183062011-01-16 10:43:21 -0800382 }
383 return false;
384 }
385
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800386 private boolean isTypingKey(KeyEvent evt) {
387 return evt.getUnicodeChar() > 0;
388 }
389
390 TabBar getTabBar() {
391 return mTabBar;
392 }
393
Michael Kolb66706532010-12-12 19:50:22 -0800394}