blob: 9b344ec937610c66dfe083eaf7281ab279ab09cb [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 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;
John Reckd73c5a22010-12-22 10:22:50 -080026import android.view.View;
27import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb66706532010-12-12 19:50:22 -080028import android.webkit.WebView;
Michael Kolb376b5412010-12-15 11:52:57 -080029import android.widget.FrameLayout;
30import android.widget.FrameLayout.LayoutParams;
Michael Kolb66706532010-12-12 19:50:22 -080031
32import java.util.List;
33
34/**
35 * Ui for xlarge screen sizes
36 */
37public class XLargeUi extends BaseUi implements ScrollListener {
38
39 private static final String LOGTAG = "XLargeUi";
40
Michael Kolb376b5412010-12-15 11:52:57 -080041 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080042 private TabBar mTabBar;
43
44 private TitleBarXLarge mTitleBar;
45 private TitleBarXLarge mFakeTitleBar;
46
Michael Kolb376b5412010-12-15 11:52:57 -080047 private boolean mUseQuickControls;
48 private PieControl mPieControl;
49
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);
56 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
57 mTitleBar.setProgress(100);
Michael Kolbcfa3af52010-12-14 10:36:11 -080058 mTitleBar.setEditable(false);
Michael Kolb66706532010-12-12 19:50:22 -080059 mFakeTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
Michael Kolbcfa3af52010-12-14 10:36:11 -080060 mFakeTitleBar.setEditable(true);
Michael Kolb66706532010-12-12 19:50:22 -080061 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080062 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080063 setupActionBar();
64 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
65 }
66
67 private void setupActionBar() {
68 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080069 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
70 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080071 }
72
73 @Override
74 public void hideComboView() {
75 super.hideComboView();
76 // ComboView changes the action bar, set it back up to what we want
77 setupActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -080078 }
79
80 private void setUseQuickControls(boolean useQuickControls) {
81 mUseQuickControls = useQuickControls;
82 if (useQuickControls) {
83 checkTabCount();
84 mPieControl = new PieControl(mActivity, mUiController, this);
85 mPieControl.attachToContainer(mContentView);
86 setFakeTitleBarGravity(Gravity.BOTTOM);
87
88 // remove embedded title bar if present
89 WebView web = mTabControl.getCurrentWebView();
90 if ((web != null) && (web.getVisibleTitleHeight() > 0)) {
91 web.setEmbeddedTitleBar(null);
92 }
93 } else {
94 mActivity.getActionBar().show();
95 if (mPieControl != null) {
96 mPieControl.removeFromContainer(mContentView);
97 }
98 setFakeTitleBarGravity(Gravity.TOP);
99 // remove embedded title bar if present
100 WebView web = mTabControl.getCurrentWebView();
101 if ((web != null) && (web.getVisibleTitleHeight() == 0)) {
102 web.setEmbeddedTitleBar(mTitleBar);
103 }
104 }
105 mTabBar.setUseQuickControls(mUseQuickControls);
106 mFakeTitleBar.setUseQuickControls(mUseQuickControls);
107 }
108
109 private void checkTabCount() {
110 if (mUseQuickControls) {
111 int n = mTabBar.getTabCount();
112 if (n >= 2) {
113 mActivity.getActionBar().show();
114 } else if (n == 1) {
115 mActivity.getActionBar().hide();
116 }
117 }
Michael Kolb66706532010-12-12 19:50:22 -0800118 }
119
120 @Override
121 public void onDestroy() {
122 hideFakeTitleBar();
123 }
124
125 // webview factory
126
127 @Override
128 public WebView createWebView(boolean privateBrowsing) {
129 // Create a new WebView
130 ScrollWebView w = new ScrollWebView(mActivity, null,
131 android.R.attr.webViewStyle, privateBrowsing);
132 initWebViewSettings(w);
133 w.setScrollListener(this);
134 w.getSettings().setDisplayZoomControls(false);
135 return w;
136 }
137
138 @Override
139 public WebView createSubWebView(boolean privateBrowsing) {
140 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
141 // no scroll listener for subview
142 web.setScrollListener(null);
143 return web;
144 }
145
146 @Override
147 public void onScroll(int visibleTitleHeight) {
148 mTabBar.onScroll(visibleTitleHeight);
149 }
150
151 void stopWebViewScrolling() {
152 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
153 if (web != null) {
154 web.stopScroll();
155 }
156 }
157
158 // WebView callbacks
159
160 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800161 public void bookmarkedStatusHasChanged(Tab tab) {
162 if (tab.inForeground()) {
163 boolean isBookmark = tab.isBookmarkedSite();
164 mTitleBar.setCurrentUrlIsBookmark(isBookmark);
165 mFakeTitleBar.setCurrentUrlIsBookmark(isBookmark);
166 }
167 }
168
169 @Override
John Reck30c714c2010-12-16 17:30:34 -0800170 public void onProgressChanged(Tab tab) {
171 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800172 mTabBar.onProgress(tab, progress);
173 if (tab.inForeground()) {
174 mFakeTitleBar.setProgress(progress);
175 if (progress == 100) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800176 if (!mFakeTitleBar.isEditingUrl()) {
177 hideFakeTitleBar();
178 if (mUseQuickControls) {
179 mFakeTitleBar.setShowProgressOnly(false);
180 setFakeTitleBarGravity(Gravity.BOTTOM);
181 }
Michael Kolb376b5412010-12-15 11:52:57 -0800182 }
Michael Kolb66706532010-12-12 19:50:22 -0800183 } else {
Michael Kolbbd018d42010-12-15 15:43:39 -0800184 if (mUseQuickControls && !mFakeTitleBar.isEditingUrl()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800185 mFakeTitleBar.setShowProgressOnly(true);
186 if (!isFakeTitleBarShowing()) {
187 setFakeTitleBarGravity(Gravity.TOP);
188 }
189 }
Michael Kolb66706532010-12-12 19:50:22 -0800190 showFakeTitleBar();
191 }
192 }
193 }
194
195 @Override
196 public boolean needsRestoreAllTabs() {
197 return true;
198 }
199
200 @Override
201 public void addTab(Tab tab) {
202 mTabBar.onNewTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800203 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800204 }
205
206 @Override
207 public void setActiveTab(Tab tab) {
208 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800209 ScrollWebView view = (ScrollWebView) tab.getWebView();
210 // TabControl.setCurrentTab has been called before this,
211 // so the tab is guaranteed to have a webview
212 if (view == null) {
213 Log.e(LOGTAG, "active tab with no webview detected");
214 return;
215 }
216 // Request focus on the top window.
217 if (mUseQuickControls) {
218 mPieControl.forceToTop(mContentView);
219 view.setScrollListener(null);
220 mTabBar.showTitleBarIndicator(false);
221 } else {
222 view.setEmbeddedTitleBar(mTitleBar);
223 view.setScrollListener(this);
224 }
Michael Kolb66706532010-12-12 19:50:22 -0800225 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800226 if (tab.isInVoiceSearchMode()) {
227 showVoiceTitleBar(tab.getVoiceDisplayTitle());
228 } else {
229 revertVoiceTitleBar(tab);
230 }
Michael Kolb376b5412010-12-15 11:52:57 -0800231 updateLockIconToLatest(tab);
232 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800233 }
234
235 @Override
236 public void updateTabs(List<Tab> tabs) {
237 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800238 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800239 }
240
241 @Override
242 public void removeTab(Tab tab) {
243 super.removeTab(tab);
244 mTabBar.onRemoveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800245 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800246 }
247
Michael Kolb376b5412010-12-15 11:52:57 -0800248 int getContentWidth() {
249 if (mContentView != null) {
250 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800251 }
252 return 0;
253 }
254
255 void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800256 if (mUiController.isInCustomActionMode()) {
257 mUiController.endActionMode();
258 }
Michael Kolb66706532010-12-12 19:50:22 -0800259 showFakeTitleBar();
260 mFakeTitleBar.onEditUrl(clearInput);
261 }
262
Michael Kolb376b5412010-12-15 11:52:57 -0800263 void setFakeTitleBarGravity(int gravity) {
264 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
265 mFakeTitleBar.getLayoutParams();
266 if (lp == null) {
267 lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
268 LayoutParams.WRAP_CONTENT);
269 }
270 lp.gravity = gravity;
271 mFakeTitleBar.setLayoutParams(lp);
272 }
273
274 void showFakeTitleBarAndEdit() {
Michael Kolbbd018d42010-12-15 15:43:39 -0800275 mFakeTitleBar.setShowProgressOnly(false);
276 setFakeTitleBarGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800277 showFakeTitleBar();
278 mFakeTitleBar.onEditUrl(false);
279 }
280
Michael Kolb66706532010-12-12 19:50:22 -0800281 @Override
282 protected void attachFakeTitleBar(WebView mainView) {
283 mContentView.addView(mFakeTitleBar);
284 mTabBar.onShowTitleBar();
285 }
286
287 @Override
288 protected void hideFakeTitleBar() {
289 if (isFakeTitleBarShowing()) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800290 mFakeTitleBar.setUrlMode(false);
Michael Kolb66706532010-12-12 19:50:22 -0800291 mContentView.removeView(mFakeTitleBar);
292 mTabBar.onHideTitleBar();
293 }
294 }
295
296 @Override
297 protected boolean isFakeTitleBarShowing() {
298 return (mFakeTitleBar.getParent() != null);
299 }
300
301 @Override
302 protected TitleBarBase getFakeTitleBar() {
303 return mFakeTitleBar;
304 }
305
306 @Override
307 protected TitleBarBase getEmbeddedTitleBar() {
308 return mTitleBar;
309 }
310
311 // action mode callbacks
312
313 @Override
314 public void onActionModeStarted(ActionMode mode) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800315 if (!mFakeTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800316 // hide the fake title bar when CAB is shown
317 hideFakeTitleBar();
318 }
319 }
320
321 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800322 public void onActionModeFinished(boolean inLoad) {
323 checkTabCount();
324 if (inLoad) {
325 // the titlebar was removed when the CAB was shown
326 // if the page is loading, show it again
327 mFakeTitleBar.setShowProgressOnly(true);
328 if (!isFakeTitleBarShowing()) {
329 setFakeTitleBarGravity(Gravity.TOP);
330 }
331 showFakeTitleBar();
332 }
333 }
334
335 @Override
John Reck30c714c2010-12-16 17:30:34 -0800336 public void setUrlTitle(Tab tab) {
337 super.setUrlTitle(tab);
338 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800339 }
340
341 // Set the favicon in the title bar.
342 @Override
John Reck30c714c2010-12-16 17:30:34 -0800343 public void setFavicon(Tab tab) {
344 super.setFavicon(tab);
345 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800346 }
347
Michael Kolbcfa3af52010-12-14 10:36:11 -0800348 @Override
349 public void showVoiceTitleBar(String title) {
350 List<String> vsresults = null;
351 if (getActiveTab() != null) {
352 vsresults = getActiveTab().getVoiceSearchResults();
353 }
354 mTitleBar.setInVoiceMode(true, null);
355 mTitleBar.setDisplayTitle(title);
356 mFakeTitleBar.setInVoiceMode(true, vsresults);
357 mFakeTitleBar.setDisplayTitle(title);
358 }
359
360 @Override
361 public void revertVoiceTitleBar(Tab tab) {
362 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800363 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800364 mTitleBar.setDisplayTitle(url);
365 mFakeTitleBar.setInVoiceMode(false, null);
366 mFakeTitleBar.setDisplayTitle(url);
367 }
368
John Reckd73c5a22010-12-22 10:22:50 -0800369 @Override
370 public void showCustomView(View view, CustomViewCallback callback) {
371 super.showCustomView(view, callback);
372 mActivity.getActionBar().hide();
373 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800374
John Reckd73c5a22010-12-22 10:22:50 -0800375 @Override
376 public void onHideCustomView() {
377 super.onHideCustomView();
378 if (mUseQuickControls) {
379 checkTabCount();
380 } else {
381 mActivity.getActionBar().show();
382 }
383 }
Michael Kolb66706532010-12-12 19:50:22 -0800384}