blob: 2aaa0293b10d4e6c4de892c361eaf5bc722ea199 [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);
146 return w;
147 }
148
149 @Override
150 public WebView createSubWebView(boolean privateBrowsing) {
151 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
152 // no scroll listener for subview
153 web.setScrollListener(null);
154 return web;
155 }
156
157 @Override
158 public void onScroll(int visibleTitleHeight) {
159 mTabBar.onScroll(visibleTitleHeight);
160 }
161
162 void stopWebViewScrolling() {
163 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
164 if (web != null) {
165 web.stopScroll();
166 }
167 }
168
169 // WebView callbacks
170
171 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800172 public void bookmarkedStatusHasChanged(Tab tab) {
173 if (tab.inForeground()) {
174 boolean isBookmark = tab.isBookmarkedSite();
175 mTitleBar.setCurrentUrlIsBookmark(isBookmark);
176 mFakeTitleBar.setCurrentUrlIsBookmark(isBookmark);
177 }
178 }
179
180 @Override
John Reck30c714c2010-12-16 17:30:34 -0800181 public void onProgressChanged(Tab tab) {
182 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800183 mTabBar.onProgress(tab, progress);
184 if (tab.inForeground()) {
185 mFakeTitleBar.setProgress(progress);
186 if (progress == 100) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800187 if (!mFakeTitleBar.isEditingUrl()) {
188 hideFakeTitleBar();
189 if (mUseQuickControls) {
190 mFakeTitleBar.setShowProgressOnly(false);
191 setFakeTitleBarGravity(Gravity.BOTTOM);
192 }
Michael Kolb376b5412010-12-15 11:52:57 -0800193 }
Michael Kolb66706532010-12-12 19:50:22 -0800194 } else {
Michael Kolbbd018d42010-12-15 15:43:39 -0800195 if (mUseQuickControls && !mFakeTitleBar.isEditingUrl()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800196 mFakeTitleBar.setShowProgressOnly(true);
197 if (!isFakeTitleBarShowing()) {
198 setFakeTitleBarGravity(Gravity.TOP);
199 }
200 }
Michael Kolb66706532010-12-12 19:50:22 -0800201 showFakeTitleBar();
202 }
203 }
204 }
205
206 @Override
207 public boolean needsRestoreAllTabs() {
208 return true;
209 }
210
211 @Override
212 public void addTab(Tab tab) {
213 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800214 }
215
216 protected void onAddTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800217 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800218 }
219
220 @Override
221 public void setActiveTab(Tab tab) {
222 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800223 ScrollWebView view = (ScrollWebView) tab.getWebView();
224 // TabControl.setCurrentTab has been called before this,
225 // so the tab is guaranteed to have a webview
226 if (view == null) {
227 Log.e(LOGTAG, "active tab with no webview detected");
228 return;
229 }
230 // Request focus on the top window.
231 if (mUseQuickControls) {
232 mPieControl.forceToTop(mContentView);
233 view.setScrollListener(null);
234 mTabBar.showTitleBarIndicator(false);
235 } else {
236 view.setEmbeddedTitleBar(mTitleBar);
237 view.setScrollListener(this);
238 }
Michael Kolb66706532010-12-12 19:50:22 -0800239 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800240 if (tab.isInVoiceSearchMode()) {
241 showVoiceTitleBar(tab.getVoiceDisplayTitle());
242 } else {
243 revertVoiceTitleBar(tab);
244 }
Michael Kolb376b5412010-12-15 11:52:57 -0800245 updateLockIconToLatest(tab);
246 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800247 }
248
249 @Override
250 public void updateTabs(List<Tab> tabs) {
251 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800252 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800253 }
254
255 @Override
256 public void removeTab(Tab tab) {
257 super.removeTab(tab);
258 mTabBar.onRemoveTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800259 }
260
261 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolb376b5412010-12-15 11:52:57 -0800262 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800263 }
264
Michael Kolb376b5412010-12-15 11:52:57 -0800265 int getContentWidth() {
266 if (mContentView != null) {
267 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800268 }
269 return 0;
270 }
271
272 void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800273 if (mUiController.isInCustomActionMode()) {
274 mUiController.endActionMode();
275 }
Michael Kolb66706532010-12-12 19:50:22 -0800276 showFakeTitleBar();
277 mFakeTitleBar.onEditUrl(clearInput);
278 }
279
Michael Kolb376b5412010-12-15 11:52:57 -0800280 void setFakeTitleBarGravity(int gravity) {
281 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
282 mFakeTitleBar.getLayoutParams();
283 if (lp == null) {
284 lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
285 LayoutParams.WRAP_CONTENT);
286 }
287 lp.gravity = gravity;
288 mFakeTitleBar.setLayoutParams(lp);
289 }
290
291 void showFakeTitleBarAndEdit() {
Michael Kolbbd018d42010-12-15 15:43:39 -0800292 mFakeTitleBar.setShowProgressOnly(false);
293 setFakeTitleBarGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800294 showFakeTitleBar();
295 mFakeTitleBar.onEditUrl(false);
296 }
297
Michael Kolb66706532010-12-12 19:50:22 -0800298 @Override
299 protected void attachFakeTitleBar(WebView mainView) {
300 mContentView.addView(mFakeTitleBar);
301 mTabBar.onShowTitleBar();
302 }
303
304 @Override
305 protected void hideFakeTitleBar() {
306 if (isFakeTitleBarShowing()) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800307 mFakeTitleBar.setUrlMode(false);
Michael Kolb66706532010-12-12 19:50:22 -0800308 mContentView.removeView(mFakeTitleBar);
309 mTabBar.onHideTitleBar();
310 }
311 }
312
313 @Override
314 protected boolean isFakeTitleBarShowing() {
315 return (mFakeTitleBar.getParent() != null);
316 }
317
318 @Override
319 protected TitleBarBase getFakeTitleBar() {
320 return mFakeTitleBar;
321 }
322
323 @Override
324 protected TitleBarBase getEmbeddedTitleBar() {
325 return mTitleBar;
326 }
327
328 // action mode callbacks
329
330 @Override
331 public void onActionModeStarted(ActionMode mode) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800332 if (!mFakeTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800333 // hide the fake title bar when CAB is shown
334 hideFakeTitleBar();
335 }
336 }
337
338 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800339 public void onActionModeFinished(boolean inLoad) {
340 checkTabCount();
341 if (inLoad) {
342 // the titlebar was removed when the CAB was shown
343 // if the page is loading, show it again
344 mFakeTitleBar.setShowProgressOnly(true);
345 if (!isFakeTitleBarShowing()) {
346 setFakeTitleBarGravity(Gravity.TOP);
347 }
348 showFakeTitleBar();
349 }
350 }
351
352 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800353 protected void updateNavigationState(Tab tab) {
354 mTitleBar.updateNavigationState(tab);
355 mFakeTitleBar.updateNavigationState(tab);
356 }
357
358 @Override
John Reck30c714c2010-12-16 17:30:34 -0800359 public void setUrlTitle(Tab tab) {
360 super.setUrlTitle(tab);
361 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800362 }
363
364 // Set the favicon in the title bar.
365 @Override
John Reck30c714c2010-12-16 17:30:34 -0800366 public void setFavicon(Tab tab) {
367 super.setFavicon(tab);
368 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800369 }
370
Michael Kolbcfa3af52010-12-14 10:36:11 -0800371 @Override
372 public void showVoiceTitleBar(String title) {
373 List<String> vsresults = null;
374 if (getActiveTab() != null) {
375 vsresults = getActiveTab().getVoiceSearchResults();
376 }
377 mTitleBar.setInVoiceMode(true, null);
378 mTitleBar.setDisplayTitle(title);
379 mFakeTitleBar.setInVoiceMode(true, vsresults);
380 mFakeTitleBar.setDisplayTitle(title);
381 }
382
383 @Override
384 public void revertVoiceTitleBar(Tab tab) {
385 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800386 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800387 mTitleBar.setDisplayTitle(url);
388 mFakeTitleBar.setInVoiceMode(false, null);
389 mFakeTitleBar.setDisplayTitle(url);
390 }
391
John Reckd73c5a22010-12-22 10:22:50 -0800392 @Override
393 public void showCustomView(View view, CustomViewCallback callback) {
394 super.showCustomView(view, callback);
395 mActivity.getActionBar().hide();
396 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800397
John Reckd73c5a22010-12-22 10:22:50 -0800398 @Override
399 public void onHideCustomView() {
400 super.onHideCustomView();
401 if (mUseQuickControls) {
402 checkTabCount();
403 } else {
404 mActivity.getActionBar().show();
405 }
406 }
Michael Kolba4183062011-01-16 10:43:21 -0800407
408 @Override
409 public boolean dispatchKey(int code, KeyEvent event) {
410 WebView web = getActiveTab().getWebView();
411 switch (code) {
412 case KeyEvent.KEYCODE_TAB:
413 case KeyEvent.KEYCODE_DPAD_UP:
414 case KeyEvent.KEYCODE_DPAD_LEFT:
415 if ((web != null) && web.hasFocus()) {
416 editUrl(true);
417 return true;
418 }
419 }
420 if (event.isPrintingKey() && !mFakeTitleBar.isEditingUrl()) {
421 editUrl(true);
422 return mContentView.dispatchKeyEvent(event);
423 }
424 return false;
425 }
426
Michael Kolb66706532010-12-12 19:50:22 -0800427}