blob: 562705b3622df15b89a893030af4704375183925 [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;
Michael Kolb376b5412010-12-15 11:52:57 -080031import android.widget.FrameLayout;
32import android.widget.FrameLayout.LayoutParams;
Michael Kolb66706532010-12-12 19:50:22 -080033
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;
47 private TitleBarXLarge mFakeTitleBar;
48
Michael Kolb376b5412010-12-15 11:52:57 -080049 private boolean mUseQuickControls;
50 private PieControl mPieControl;
51
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);
58 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
59 mTitleBar.setProgress(100);
Michael Kolbcfa3af52010-12-14 10:36:11 -080060 mTitleBar.setEditable(false);
Michael Kolb66706532010-12-12 19:50:22 -080061 mFakeTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
Michael Kolbcfa3af52010-12-14 10:36:11 -080062 mFakeTitleBar.setEditable(true);
Michael Kolb66706532010-12-12 19:50:22 -080063 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080064 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080065 setupActionBar();
66 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
67 }
68
69 private void setupActionBar() {
70 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080071 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
72 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080073 }
74
75 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080076 public void showComboView(boolean startWithHistory, Bundle extras) {
77 super.showComboView(startWithHistory, extras);
78 if (mUseQuickControls) {
79 mActionBar.show();
80 }
81 }
82
83 @Override
John Reckb3417f02011-01-14 11:01:05 -080084 public void hideComboView() {
Michael Kolb8814d732011-01-26 11:22:30 -080085 checkTabCount();
John Reckb3417f02011-01-14 11:01:05 -080086 super.hideComboView();
87 // ComboView changes the action bar, set it back up to what we want
88 setupActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -080089 }
90
91 private void setUseQuickControls(boolean useQuickControls) {
92 mUseQuickControls = useQuickControls;
93 if (useQuickControls) {
94 checkTabCount();
95 mPieControl = new PieControl(mActivity, mUiController, this);
96 mPieControl.attachToContainer(mContentView);
97 setFakeTitleBarGravity(Gravity.BOTTOM);
98
99 // remove embedded title bar if present
100 WebView web = mTabControl.getCurrentWebView();
101 if ((web != null) && (web.getVisibleTitleHeight() > 0)) {
102 web.setEmbeddedTitleBar(null);
103 }
104 } else {
105 mActivity.getActionBar().show();
106 if (mPieControl != null) {
107 mPieControl.removeFromContainer(mContentView);
108 }
109 setFakeTitleBarGravity(Gravity.TOP);
110 // remove embedded title bar if present
111 WebView web = mTabControl.getCurrentWebView();
112 if ((web != null) && (web.getVisibleTitleHeight() == 0)) {
113 web.setEmbeddedTitleBar(mTitleBar);
114 }
115 }
116 mTabBar.setUseQuickControls(mUseQuickControls);
117 mFakeTitleBar.setUseQuickControls(mUseQuickControls);
118 }
119
120 private void checkTabCount() {
121 if (mUseQuickControls) {
122 int n = mTabBar.getTabCount();
123 if (n >= 2) {
124 mActivity.getActionBar().show();
125 } else if (n == 1) {
126 mActivity.getActionBar().hide();
127 }
128 }
Michael Kolb66706532010-12-12 19:50:22 -0800129 }
130
131 @Override
132 public void onDestroy() {
133 hideFakeTitleBar();
134 }
135
136 // webview factory
137
138 @Override
139 public WebView createWebView(boolean privateBrowsing) {
140 // Create a new WebView
141 ScrollWebView w = new ScrollWebView(mActivity, null,
142 android.R.attr.webViewStyle, privateBrowsing);
143 initWebViewSettings(w);
144 w.setScrollListener(this);
145 w.getSettings().setDisplayZoomControls(false);
Dave Burke20b6f1f2011-02-01 12:59:04 +0000146 w.setExpandedTileBounds(true); // smoother scrolling
Michael Kolb66706532010-12-12 19:50:22 -0800147 return w;
148 }
149
150 @Override
151 public WebView createSubWebView(boolean privateBrowsing) {
152 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
153 // no scroll listener for subview
154 web.setScrollListener(null);
155 return web;
156 }
157
158 @Override
159 public void onScroll(int visibleTitleHeight) {
160 mTabBar.onScroll(visibleTitleHeight);
161 }
162
163 void stopWebViewScrolling() {
164 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
165 if (web != null) {
166 web.stopScroll();
167 }
168 }
169
170 // WebView callbacks
171
172 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800173 public void bookmarkedStatusHasChanged(Tab tab) {
174 if (tab.inForeground()) {
175 boolean isBookmark = tab.isBookmarkedSite();
176 mTitleBar.setCurrentUrlIsBookmark(isBookmark);
177 mFakeTitleBar.setCurrentUrlIsBookmark(isBookmark);
178 }
179 }
180
181 @Override
John Reck30c714c2010-12-16 17:30:34 -0800182 public void onProgressChanged(Tab tab) {
183 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800184 mTabBar.onProgress(tab, progress);
185 if (tab.inForeground()) {
186 mFakeTitleBar.setProgress(progress);
187 if (progress == 100) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800188 if (!mFakeTitleBar.isEditingUrl()) {
189 hideFakeTitleBar();
190 if (mUseQuickControls) {
191 mFakeTitleBar.setShowProgressOnly(false);
192 setFakeTitleBarGravity(Gravity.BOTTOM);
193 }
Michael Kolb376b5412010-12-15 11:52:57 -0800194 }
Michael Kolb66706532010-12-12 19:50:22 -0800195 } else {
Michael Kolbbd018d42010-12-15 15:43:39 -0800196 if (mUseQuickControls && !mFakeTitleBar.isEditingUrl()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800197 mFakeTitleBar.setShowProgressOnly(true);
198 if (!isFakeTitleBarShowing()) {
199 setFakeTitleBarGravity(Gravity.TOP);
200 }
201 }
Michael Kolb66706532010-12-12 19:50:22 -0800202 showFakeTitleBar();
203 }
204 }
205 }
206
207 @Override
208 public boolean needsRestoreAllTabs() {
209 return true;
210 }
211
212 @Override
213 public void addTab(Tab tab) {
214 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800215 }
216
217 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800218 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800219 }
220
221 @Override
222 public void setActiveTab(Tab tab) {
223 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800224 ScrollWebView view = (ScrollWebView) tab.getWebView();
225 // TabControl.setCurrentTab has been called before this,
226 // so the tab is guaranteed to have a webview
227 if (view == null) {
228 Log.e(LOGTAG, "active tab with no webview detected");
229 return;
230 }
231 // Request focus on the top window.
232 if (mUseQuickControls) {
233 mPieControl.forceToTop(mContentView);
234 view.setScrollListener(null);
235 mTabBar.showTitleBarIndicator(false);
236 } else {
237 view.setEmbeddedTitleBar(mTitleBar);
238 view.setScrollListener(this);
239 }
Michael Kolb66706532010-12-12 19:50:22 -0800240 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800241 if (tab.isInVoiceSearchMode()) {
242 showVoiceTitleBar(tab.getVoiceDisplayTitle());
243 } else {
244 revertVoiceTitleBar(tab);
245 }
Michael Kolb376b5412010-12-15 11:52:57 -0800246 updateLockIconToLatest(tab);
247 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800248 }
249
250 @Override
251 public void updateTabs(List<Tab> tabs) {
252 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800253 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800254 }
255
256 @Override
257 public void removeTab(Tab tab) {
258 super.removeTab(tab);
259 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800260 }
261
262 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800263 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800264 }
265
Michael Kolb376b5412010-12-15 11:52:57 -0800266 int getContentWidth() {
267 if (mContentView != null) {
268 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800269 }
270 return 0;
271 }
272
273 void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800274 if (mUiController.isInCustomActionMode()) {
275 mUiController.endActionMode();
276 }
Michael Kolb66706532010-12-12 19:50:22 -0800277 showFakeTitleBar();
278 mFakeTitleBar.onEditUrl(clearInput);
279 }
280
Michael Kolb376b5412010-12-15 11:52:57 -0800281 void setFakeTitleBarGravity(int gravity) {
282 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
283 mFakeTitleBar.getLayoutParams();
284 if (lp == null) {
285 lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
286 LayoutParams.WRAP_CONTENT);
287 }
288 lp.gravity = gravity;
289 mFakeTitleBar.setLayoutParams(lp);
290 }
291
292 void showFakeTitleBarAndEdit() {
Michael Kolbbd018d42010-12-15 15:43:39 -0800293 mFakeTitleBar.setShowProgressOnly(false);
294 setFakeTitleBarGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800295 showFakeTitleBar();
296 mFakeTitleBar.onEditUrl(false);
297 }
298
Michael Kolb66706532010-12-12 19:50:22 -0800299 @Override
300 protected void attachFakeTitleBar(WebView mainView) {
301 mContentView.addView(mFakeTitleBar);
302 mTabBar.onShowTitleBar();
303 }
304
305 @Override
306 protected void hideFakeTitleBar() {
307 if (isFakeTitleBarShowing()) {
Michael Kolb47171d82011-01-28 10:34:15 -0800308 mFakeTitleBar.setEditMode(false);
Michael Kolb66706532010-12-12 19:50:22 -0800309 mTabBar.onHideTitleBar();
Michael Kolbcfea5512011-02-04 20:24:56 -0800310 mContentView.removeView(mFakeTitleBar);
Michael Kolb66706532010-12-12 19:50:22 -0800311 }
312 }
313
314 @Override
315 protected boolean isFakeTitleBarShowing() {
316 return (mFakeTitleBar.getParent() != null);
317 }
318
319 @Override
320 protected TitleBarBase getFakeTitleBar() {
321 return mFakeTitleBar;
322 }
323
324 @Override
325 protected TitleBarBase getEmbeddedTitleBar() {
326 return mTitleBar;
327 }
328
329 // action mode callbacks
330
331 @Override
332 public void onActionModeStarted(ActionMode mode) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800333 if (!mFakeTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800334 // hide the fake title bar when CAB is shown
335 hideFakeTitleBar();
336 }
337 }
338
339 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800340 public void onActionModeFinished(boolean inLoad) {
341 checkTabCount();
342 if (inLoad) {
343 // the titlebar was removed when the CAB was shown
344 // if the page is loading, show it again
345 mFakeTitleBar.setShowProgressOnly(true);
346 if (!isFakeTitleBarShowing()) {
347 setFakeTitleBarGravity(Gravity.TOP);
348 }
349 showFakeTitleBar();
350 }
351 }
352
353 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800354 protected void updateNavigationState(Tab tab) {
355 mTitleBar.updateNavigationState(tab);
356 mFakeTitleBar.updateNavigationState(tab);
357 }
358
359 @Override
John Reck30c714c2010-12-16 17:30:34 -0800360 public void setUrlTitle(Tab tab) {
361 super.setUrlTitle(tab);
362 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800363 }
364
365 // Set the favicon in the title bar.
366 @Override
John Reck30c714c2010-12-16 17:30:34 -0800367 public void setFavicon(Tab tab) {
368 super.setFavicon(tab);
369 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800370 }
371
Michael Kolbcfa3af52010-12-14 10:36:11 -0800372 @Override
373 public void showVoiceTitleBar(String title) {
374 List<String> vsresults = null;
375 if (getActiveTab() != null) {
376 vsresults = getActiveTab().getVoiceSearchResults();
377 }
378 mTitleBar.setInVoiceMode(true, null);
379 mTitleBar.setDisplayTitle(title);
380 mFakeTitleBar.setInVoiceMode(true, vsresults);
381 mFakeTitleBar.setDisplayTitle(title);
382 }
383
384 @Override
385 public void revertVoiceTitleBar(Tab tab) {
386 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800387 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800388 mTitleBar.setDisplayTitle(url);
389 mFakeTitleBar.setInVoiceMode(false, null);
390 mFakeTitleBar.setDisplayTitle(url);
391 }
392
John Reckd73c5a22010-12-22 10:22:50 -0800393 @Override
394 public void showCustomView(View view, CustomViewCallback callback) {
395 super.showCustomView(view, callback);
396 mActivity.getActionBar().hide();
397 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800398
John Reckd73c5a22010-12-22 10:22:50 -0800399 @Override
400 public void onHideCustomView() {
401 super.onHideCustomView();
402 if (mUseQuickControls) {
403 checkTabCount();
404 } else {
405 mActivity.getActionBar().show();
406 }
407 }
Michael Kolba4183062011-01-16 10:43:21 -0800408
409 @Override
410 public boolean dispatchKey(int code, KeyEvent event) {
411 WebView web = getActiveTab().getWebView();
412 switch (code) {
413 case KeyEvent.KEYCODE_TAB:
414 case KeyEvent.KEYCODE_DPAD_UP:
415 case KeyEvent.KEYCODE_DPAD_LEFT:
416 if ((web != null) && web.hasFocus()) {
417 editUrl(true);
418 return true;
419 }
420 }
Michael Kolb47171d82011-01-28 10:34:15 -0800421 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
422 if (!ctrl && event.isPrintingKey() && !mFakeTitleBar.isEditingUrl()) {
Michael Kolba4183062011-01-16 10:43:21 -0800423 editUrl(true);
424 return mContentView.dispatchKeyEvent(event);
425 }
426 return false;
427 }
428
Michael Kolb66706532010-12-12 19:50:22 -0800429}