blob: d50d94e86b93bc518e4708e7f8f1e61cea3d927f [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;
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;
Michael Kolb376b5412010-12-15 11:52:57 -080030import android.widget.FrameLayout;
31import android.widget.FrameLayout.LayoutParams;
Michael Kolb66706532010-12-12 19:50:22 -080032
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;
46 private TitleBarXLarge mFakeTitleBar;
47
Michael Kolb376b5412010-12-15 11:52:57 -080048 private boolean mUseQuickControls;
49 private PieControl mPieControl;
50
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);
57 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
58 mTitleBar.setProgress(100);
Michael Kolbcfa3af52010-12-14 10:36:11 -080059 mTitleBar.setEditable(false);
Michael Kolb66706532010-12-12 19:50:22 -080060 mFakeTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
Michael Kolbcfa3af52010-12-14 10:36:11 -080061 mFakeTitleBar.setEditable(true);
Michael Kolb66706532010-12-12 19:50:22 -080062 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080063 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080064 setupActionBar();
65 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
66 }
67
68 private void setupActionBar() {
69 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080070 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
71 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080072 }
73
74 @Override
Michael Kolbac35bdc2011-01-17 17:06:04 -080075 public void showComboView(boolean startWithHistory, Bundle extras) {
76 super.showComboView(startWithHistory, extras);
77 if (mUseQuickControls) {
78 mActionBar.show();
79 }
80 }
81
82 @Override
John Reckb3417f02011-01-14 11:01:05 -080083 public void hideComboView() {
84 super.hideComboView();
85 // ComboView changes the action bar, set it back up to what we want
86 setupActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -080087 }
88
89 private void setUseQuickControls(boolean useQuickControls) {
90 mUseQuickControls = useQuickControls;
91 if (useQuickControls) {
92 checkTabCount();
93 mPieControl = new PieControl(mActivity, mUiController, this);
94 mPieControl.attachToContainer(mContentView);
95 setFakeTitleBarGravity(Gravity.BOTTOM);
96
97 // remove embedded title bar if present
98 WebView web = mTabControl.getCurrentWebView();
99 if ((web != null) && (web.getVisibleTitleHeight() > 0)) {
100 web.setEmbeddedTitleBar(null);
101 }
102 } else {
103 mActivity.getActionBar().show();
104 if (mPieControl != null) {
105 mPieControl.removeFromContainer(mContentView);
106 }
107 setFakeTitleBarGravity(Gravity.TOP);
108 // remove embedded title bar if present
109 WebView web = mTabControl.getCurrentWebView();
110 if ((web != null) && (web.getVisibleTitleHeight() == 0)) {
111 web.setEmbeddedTitleBar(mTitleBar);
112 }
113 }
114 mTabBar.setUseQuickControls(mUseQuickControls);
115 mFakeTitleBar.setUseQuickControls(mUseQuickControls);
116 }
117
118 private void checkTabCount() {
119 if (mUseQuickControls) {
120 int n = mTabBar.getTabCount();
121 if (n >= 2) {
122 mActivity.getActionBar().show();
123 } else if (n == 1) {
124 mActivity.getActionBar().hide();
125 }
126 }
Michael Kolb66706532010-12-12 19:50:22 -0800127 }
128
129 @Override
130 public void onDestroy() {
131 hideFakeTitleBar();
132 }
133
134 // webview factory
135
136 @Override
137 public WebView createWebView(boolean privateBrowsing) {
138 // Create a new WebView
139 ScrollWebView w = new ScrollWebView(mActivity, null,
140 android.R.attr.webViewStyle, privateBrowsing);
141 initWebViewSettings(w);
142 w.setScrollListener(this);
143 w.getSettings().setDisplayZoomControls(false);
144 return w;
145 }
146
147 @Override
148 public WebView createSubWebView(boolean privateBrowsing) {
149 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
150 // no scroll listener for subview
151 web.setScrollListener(null);
152 return web;
153 }
154
155 @Override
156 public void onScroll(int visibleTitleHeight) {
157 mTabBar.onScroll(visibleTitleHeight);
158 }
159
160 void stopWebViewScrolling() {
161 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
162 if (web != null) {
163 web.stopScroll();
164 }
165 }
166
167 // WebView callbacks
168
169 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800170 public void bookmarkedStatusHasChanged(Tab tab) {
171 if (tab.inForeground()) {
172 boolean isBookmark = tab.isBookmarkedSite();
173 mTitleBar.setCurrentUrlIsBookmark(isBookmark);
174 mFakeTitleBar.setCurrentUrlIsBookmark(isBookmark);
175 }
176 }
177
178 @Override
John Reck30c714c2010-12-16 17:30:34 -0800179 public void onProgressChanged(Tab tab) {
180 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800181 mTabBar.onProgress(tab, progress);
182 if (tab.inForeground()) {
183 mFakeTitleBar.setProgress(progress);
184 if (progress == 100) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800185 if (!mFakeTitleBar.isEditingUrl()) {
186 hideFakeTitleBar();
187 if (mUseQuickControls) {
188 mFakeTitleBar.setShowProgressOnly(false);
189 setFakeTitleBarGravity(Gravity.BOTTOM);
190 }
Michael Kolb376b5412010-12-15 11:52:57 -0800191 }
Michael Kolb66706532010-12-12 19:50:22 -0800192 } else {
Michael Kolbbd018d42010-12-15 15:43:39 -0800193 if (mUseQuickControls && !mFakeTitleBar.isEditingUrl()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800194 mFakeTitleBar.setShowProgressOnly(true);
195 if (!isFakeTitleBarShowing()) {
196 setFakeTitleBarGravity(Gravity.TOP);
197 }
198 }
Michael Kolb66706532010-12-12 19:50:22 -0800199 showFakeTitleBar();
200 }
201 }
202 }
203
204 @Override
205 public boolean needsRestoreAllTabs() {
206 return true;
207 }
208
209 @Override
210 public void addTab(Tab tab) {
211 mTabBar.onNewTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800212 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800213 }
214
215 @Override
216 public void setActiveTab(Tab tab) {
217 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800218 ScrollWebView view = (ScrollWebView) tab.getWebView();
219 // TabControl.setCurrentTab has been called before this,
220 // so the tab is guaranteed to have a webview
221 if (view == null) {
222 Log.e(LOGTAG, "active tab with no webview detected");
223 return;
224 }
225 // Request focus on the top window.
226 if (mUseQuickControls) {
227 mPieControl.forceToTop(mContentView);
228 view.setScrollListener(null);
229 mTabBar.showTitleBarIndicator(false);
230 } else {
231 view.setEmbeddedTitleBar(mTitleBar);
232 view.setScrollListener(this);
233 }
Michael Kolb66706532010-12-12 19:50:22 -0800234 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800235 if (tab.isInVoiceSearchMode()) {
236 showVoiceTitleBar(tab.getVoiceDisplayTitle());
237 } else {
238 revertVoiceTitleBar(tab);
239 }
Michael Kolb376b5412010-12-15 11:52:57 -0800240 updateLockIconToLatest(tab);
241 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800242 }
243
244 @Override
245 public void updateTabs(List<Tab> tabs) {
246 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800247 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800248 }
249
250 @Override
251 public void removeTab(Tab tab) {
252 super.removeTab(tab);
253 mTabBar.onRemoveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800254 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800255 }
256
Michael Kolb376b5412010-12-15 11:52:57 -0800257 int getContentWidth() {
258 if (mContentView != null) {
259 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800260 }
261 return 0;
262 }
263
264 void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800265 if (mUiController.isInCustomActionMode()) {
266 mUiController.endActionMode();
267 }
Michael Kolb66706532010-12-12 19:50:22 -0800268 showFakeTitleBar();
269 mFakeTitleBar.onEditUrl(clearInput);
270 }
271
Michael Kolb376b5412010-12-15 11:52:57 -0800272 void setFakeTitleBarGravity(int gravity) {
273 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
274 mFakeTitleBar.getLayoutParams();
275 if (lp == null) {
276 lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
277 LayoutParams.WRAP_CONTENT);
278 }
279 lp.gravity = gravity;
280 mFakeTitleBar.setLayoutParams(lp);
281 }
282
283 void showFakeTitleBarAndEdit() {
Michael Kolbbd018d42010-12-15 15:43:39 -0800284 mFakeTitleBar.setShowProgressOnly(false);
285 setFakeTitleBarGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800286 showFakeTitleBar();
287 mFakeTitleBar.onEditUrl(false);
288 }
289
Michael Kolb66706532010-12-12 19:50:22 -0800290 @Override
291 protected void attachFakeTitleBar(WebView mainView) {
292 mContentView.addView(mFakeTitleBar);
293 mTabBar.onShowTitleBar();
294 }
295
296 @Override
297 protected void hideFakeTitleBar() {
298 if (isFakeTitleBarShowing()) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800299 mFakeTitleBar.setUrlMode(false);
Michael Kolb66706532010-12-12 19:50:22 -0800300 mContentView.removeView(mFakeTitleBar);
301 mTabBar.onHideTitleBar();
302 }
303 }
304
305 @Override
306 protected boolean isFakeTitleBarShowing() {
307 return (mFakeTitleBar.getParent() != null);
308 }
309
310 @Override
311 protected TitleBarBase getFakeTitleBar() {
312 return mFakeTitleBar;
313 }
314
315 @Override
316 protected TitleBarBase getEmbeddedTitleBar() {
317 return mTitleBar;
318 }
319
320 // action mode callbacks
321
322 @Override
323 public void onActionModeStarted(ActionMode mode) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800324 if (!mFakeTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800325 // hide the fake title bar when CAB is shown
326 hideFakeTitleBar();
327 }
328 }
329
330 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800331 public void onActionModeFinished(boolean inLoad) {
332 checkTabCount();
333 if (inLoad) {
334 // the titlebar was removed when the CAB was shown
335 // if the page is loading, show it again
336 mFakeTitleBar.setShowProgressOnly(true);
337 if (!isFakeTitleBarShowing()) {
338 setFakeTitleBarGravity(Gravity.TOP);
339 }
340 showFakeTitleBar();
341 }
342 }
343
344 @Override
John Reck30c714c2010-12-16 17:30:34 -0800345 public void setUrlTitle(Tab tab) {
346 super.setUrlTitle(tab);
347 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800348 }
349
350 // Set the favicon in the title bar.
351 @Override
John Reck30c714c2010-12-16 17:30:34 -0800352 public void setFavicon(Tab tab) {
353 super.setFavicon(tab);
354 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800355 }
356
Michael Kolbcfa3af52010-12-14 10:36:11 -0800357 @Override
358 public void showVoiceTitleBar(String title) {
359 List<String> vsresults = null;
360 if (getActiveTab() != null) {
361 vsresults = getActiveTab().getVoiceSearchResults();
362 }
363 mTitleBar.setInVoiceMode(true, null);
364 mTitleBar.setDisplayTitle(title);
365 mFakeTitleBar.setInVoiceMode(true, vsresults);
366 mFakeTitleBar.setDisplayTitle(title);
367 }
368
369 @Override
370 public void revertVoiceTitleBar(Tab tab) {
371 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800372 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800373 mTitleBar.setDisplayTitle(url);
374 mFakeTitleBar.setInVoiceMode(false, null);
375 mFakeTitleBar.setDisplayTitle(url);
376 }
377
John Reckd73c5a22010-12-22 10:22:50 -0800378 @Override
379 public void showCustomView(View view, CustomViewCallback callback) {
380 super.showCustomView(view, callback);
381 mActivity.getActionBar().hide();
382 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800383
John Reckd73c5a22010-12-22 10:22:50 -0800384 @Override
385 public void onHideCustomView() {
386 super.onHideCustomView();
387 if (mUseQuickControls) {
388 checkTabCount();
389 } else {
390 mActivity.getActionBar().show();
391 }
392 }
Michael Kolb66706532010-12-12 19:50:22 -0800393}