blob: 2f27aa10bfbf69f95916516b35db00105926a383 [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() {
85 super.hideComboView();
86 // ComboView changes the action bar, set it back up to what we want
87 setupActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -080088 }
89
90 private void setUseQuickControls(boolean useQuickControls) {
91 mUseQuickControls = useQuickControls;
92 if (useQuickControls) {
93 checkTabCount();
94 mPieControl = new PieControl(mActivity, mUiController, this);
95 mPieControl.attachToContainer(mContentView);
96 setFakeTitleBarGravity(Gravity.BOTTOM);
97
98 // remove embedded title bar if present
99 WebView web = mTabControl.getCurrentWebView();
100 if ((web != null) && (web.getVisibleTitleHeight() > 0)) {
101 web.setEmbeddedTitleBar(null);
102 }
103 } else {
104 mActivity.getActionBar().show();
105 if (mPieControl != null) {
106 mPieControl.removeFromContainer(mContentView);
107 }
108 setFakeTitleBarGravity(Gravity.TOP);
109 // remove embedded title bar if present
110 WebView web = mTabControl.getCurrentWebView();
111 if ((web != null) && (web.getVisibleTitleHeight() == 0)) {
112 web.setEmbeddedTitleBar(mTitleBar);
113 }
114 }
115 mTabBar.setUseQuickControls(mUseQuickControls);
116 mFakeTitleBar.setUseQuickControls(mUseQuickControls);
117 }
118
119 private void checkTabCount() {
120 if (mUseQuickControls) {
121 int n = mTabBar.getTabCount();
122 if (n >= 2) {
123 mActivity.getActionBar().show();
124 } else if (n == 1) {
125 mActivity.getActionBar().hide();
126 }
127 }
Michael Kolb66706532010-12-12 19:50:22 -0800128 }
129
130 @Override
131 public void onDestroy() {
132 hideFakeTitleBar();
133 }
134
135 // webview factory
136
137 @Override
138 public WebView createWebView(boolean privateBrowsing) {
139 // Create a new WebView
140 ScrollWebView w = new ScrollWebView(mActivity, null,
141 android.R.attr.webViewStyle, privateBrowsing);
142 initWebViewSettings(w);
143 w.setScrollListener(this);
144 w.getSettings().setDisplayZoomControls(false);
145 return w;
146 }
147
148 @Override
149 public WebView createSubWebView(boolean privateBrowsing) {
150 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
151 // no scroll listener for subview
152 web.setScrollListener(null);
153 return web;
154 }
155
156 @Override
157 public void onScroll(int visibleTitleHeight) {
158 mTabBar.onScroll(visibleTitleHeight);
159 }
160
161 void stopWebViewScrolling() {
162 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
163 if (web != null) {
164 web.stopScroll();
165 }
166 }
167
168 // WebView callbacks
169
170 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800171 public void bookmarkedStatusHasChanged(Tab tab) {
172 if (tab.inForeground()) {
173 boolean isBookmark = tab.isBookmarkedSite();
174 mTitleBar.setCurrentUrlIsBookmark(isBookmark);
175 mFakeTitleBar.setCurrentUrlIsBookmark(isBookmark);
176 }
177 }
178
179 @Override
John Reck30c714c2010-12-16 17:30:34 -0800180 public void onProgressChanged(Tab tab) {
181 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800182 mTabBar.onProgress(tab, progress);
183 if (tab.inForeground()) {
184 mFakeTitleBar.setProgress(progress);
185 if (progress == 100) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800186 if (!mFakeTitleBar.isEditingUrl()) {
187 hideFakeTitleBar();
188 if (mUseQuickControls) {
189 mFakeTitleBar.setShowProgressOnly(false);
190 setFakeTitleBarGravity(Gravity.BOTTOM);
191 }
Michael Kolb376b5412010-12-15 11:52:57 -0800192 }
Michael Kolb66706532010-12-12 19:50:22 -0800193 } else {
Michael Kolbbd018d42010-12-15 15:43:39 -0800194 if (mUseQuickControls && !mFakeTitleBar.isEditingUrl()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800195 mFakeTitleBar.setShowProgressOnly(true);
196 if (!isFakeTitleBarShowing()) {
197 setFakeTitleBarGravity(Gravity.TOP);
198 }
199 }
Michael Kolb66706532010-12-12 19:50:22 -0800200 showFakeTitleBar();
201 }
202 }
203 }
204
205 @Override
206 public boolean needsRestoreAllTabs() {
207 return true;
208 }
209
210 @Override
211 public void addTab(Tab tab) {
212 mTabBar.onNewTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800213 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800214 }
215
216 @Override
217 public void setActiveTab(Tab tab) {
218 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800219 ScrollWebView view = (ScrollWebView) tab.getWebView();
220 // TabControl.setCurrentTab has been called before this,
221 // so the tab is guaranteed to have a webview
222 if (view == null) {
223 Log.e(LOGTAG, "active tab with no webview detected");
224 return;
225 }
226 // Request focus on the top window.
227 if (mUseQuickControls) {
228 mPieControl.forceToTop(mContentView);
229 view.setScrollListener(null);
230 mTabBar.showTitleBarIndicator(false);
231 } else {
232 view.setEmbeddedTitleBar(mTitleBar);
233 view.setScrollListener(this);
234 }
Michael Kolb66706532010-12-12 19:50:22 -0800235 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800236 if (tab.isInVoiceSearchMode()) {
237 showVoiceTitleBar(tab.getVoiceDisplayTitle());
238 } else {
239 revertVoiceTitleBar(tab);
240 }
Michael Kolb376b5412010-12-15 11:52:57 -0800241 updateLockIconToLatest(tab);
242 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800243 }
244
245 @Override
246 public void updateTabs(List<Tab> tabs) {
247 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800248 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800249 }
250
251 @Override
252 public void removeTab(Tab tab) {
253 super.removeTab(tab);
254 mTabBar.onRemoveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800255 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800256 }
257
Michael Kolb376b5412010-12-15 11:52:57 -0800258 int getContentWidth() {
259 if (mContentView != null) {
260 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800261 }
262 return 0;
263 }
264
265 void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800266 if (mUiController.isInCustomActionMode()) {
267 mUiController.endActionMode();
268 }
Michael Kolb66706532010-12-12 19:50:22 -0800269 showFakeTitleBar();
270 mFakeTitleBar.onEditUrl(clearInput);
271 }
272
Michael Kolb376b5412010-12-15 11:52:57 -0800273 void setFakeTitleBarGravity(int gravity) {
274 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
275 mFakeTitleBar.getLayoutParams();
276 if (lp == null) {
277 lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
278 LayoutParams.WRAP_CONTENT);
279 }
280 lp.gravity = gravity;
281 mFakeTitleBar.setLayoutParams(lp);
282 }
283
284 void showFakeTitleBarAndEdit() {
Michael Kolbbd018d42010-12-15 15:43:39 -0800285 mFakeTitleBar.setShowProgressOnly(false);
286 setFakeTitleBarGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800287 showFakeTitleBar();
288 mFakeTitleBar.onEditUrl(false);
289 }
290
Michael Kolb66706532010-12-12 19:50:22 -0800291 @Override
292 protected void attachFakeTitleBar(WebView mainView) {
293 mContentView.addView(mFakeTitleBar);
294 mTabBar.onShowTitleBar();
295 }
296
297 @Override
298 protected void hideFakeTitleBar() {
299 if (isFakeTitleBarShowing()) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800300 mFakeTitleBar.setUrlMode(false);
Michael Kolb66706532010-12-12 19:50:22 -0800301 mContentView.removeView(mFakeTitleBar);
302 mTabBar.onHideTitleBar();
303 }
304 }
305
306 @Override
307 protected boolean isFakeTitleBarShowing() {
308 return (mFakeTitleBar.getParent() != null);
309 }
310
311 @Override
312 protected TitleBarBase getFakeTitleBar() {
313 return mFakeTitleBar;
314 }
315
316 @Override
317 protected TitleBarBase getEmbeddedTitleBar() {
318 return mTitleBar;
319 }
320
321 // action mode callbacks
322
323 @Override
324 public void onActionModeStarted(ActionMode mode) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800325 if (!mFakeTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800326 // hide the fake title bar when CAB is shown
327 hideFakeTitleBar();
328 }
329 }
330
331 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800332 public void onActionModeFinished(boolean inLoad) {
333 checkTabCount();
334 if (inLoad) {
335 // the titlebar was removed when the CAB was shown
336 // if the page is loading, show it again
337 mFakeTitleBar.setShowProgressOnly(true);
338 if (!isFakeTitleBarShowing()) {
339 setFakeTitleBarGravity(Gravity.TOP);
340 }
341 showFakeTitleBar();
342 }
343 }
344
345 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800346 protected void updateNavigationState(Tab tab) {
347 mTitleBar.updateNavigationState(tab);
348 mFakeTitleBar.updateNavigationState(tab);
349 }
350
351 @Override
John Reck30c714c2010-12-16 17:30:34 -0800352 public void setUrlTitle(Tab tab) {
353 super.setUrlTitle(tab);
354 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800355 }
356
357 // Set the favicon in the title bar.
358 @Override
John Reck30c714c2010-12-16 17:30:34 -0800359 public void setFavicon(Tab tab) {
360 super.setFavicon(tab);
361 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800362 }
363
Michael Kolbcfa3af52010-12-14 10:36:11 -0800364 @Override
365 public void showVoiceTitleBar(String title) {
366 List<String> vsresults = null;
367 if (getActiveTab() != null) {
368 vsresults = getActiveTab().getVoiceSearchResults();
369 }
370 mTitleBar.setInVoiceMode(true, null);
371 mTitleBar.setDisplayTitle(title);
372 mFakeTitleBar.setInVoiceMode(true, vsresults);
373 mFakeTitleBar.setDisplayTitle(title);
374 }
375
376 @Override
377 public void revertVoiceTitleBar(Tab tab) {
378 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800379 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800380 mTitleBar.setDisplayTitle(url);
381 mFakeTitleBar.setInVoiceMode(false, null);
382 mFakeTitleBar.setDisplayTitle(url);
383 }
384
John Reckd73c5a22010-12-22 10:22:50 -0800385 @Override
386 public void showCustomView(View view, CustomViewCallback callback) {
387 super.showCustomView(view, callback);
388 mActivity.getActionBar().hide();
389 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800390
John Reckd73c5a22010-12-22 10:22:50 -0800391 @Override
392 public void onHideCustomView() {
393 super.onHideCustomView();
394 if (mUseQuickControls) {
395 checkTabCount();
396 } else {
397 mActivity.getActionBar().show();
398 }
399 }
Michael Kolba4183062011-01-16 10:43:21 -0800400
401 @Override
402 public boolean dispatchKey(int code, KeyEvent event) {
403 WebView web = getActiveTab().getWebView();
404 switch (code) {
405 case KeyEvent.KEYCODE_TAB:
406 case KeyEvent.KEYCODE_DPAD_UP:
407 case KeyEvent.KEYCODE_DPAD_LEFT:
408 if ((web != null) && web.hasFocus()) {
409 editUrl(true);
410 return true;
411 }
412 }
413 if (event.isPrintingKey() && !mFakeTitleBar.isEditingUrl()) {
414 editUrl(true);
415 return mContentView.dispatchKeyEvent(event);
416 }
417 return false;
418 }
419
Michael Kolb66706532010-12-12 19:50:22 -0800420}