blob: 1e607a7c7c210de7d3b73c18af5255faf0b62231 [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;
23import android.graphics.Bitmap;
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();
64 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
65 mActionBar.setCustomView(mTabBar);
66 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
67 }
68
69 private void setUseQuickControls(boolean useQuickControls) {
70 mUseQuickControls = useQuickControls;
71 if (useQuickControls) {
72 checkTabCount();
73 mPieControl = new PieControl(mActivity, mUiController, this);
74 mPieControl.attachToContainer(mContentView);
75 setFakeTitleBarGravity(Gravity.BOTTOM);
76
77 // remove embedded title bar if present
78 WebView web = mTabControl.getCurrentWebView();
79 if ((web != null) && (web.getVisibleTitleHeight() > 0)) {
80 web.setEmbeddedTitleBar(null);
81 }
82 } else {
83 mActivity.getActionBar().show();
84 if (mPieControl != null) {
85 mPieControl.removeFromContainer(mContentView);
86 }
87 setFakeTitleBarGravity(Gravity.TOP);
88 // remove embedded title bar if present
89 WebView web = mTabControl.getCurrentWebView();
90 if ((web != null) && (web.getVisibleTitleHeight() == 0)) {
91 web.setEmbeddedTitleBar(mTitleBar);
92 }
93 }
94 mTabBar.setUseQuickControls(mUseQuickControls);
95 mFakeTitleBar.setUseQuickControls(mUseQuickControls);
96 }
97
98 private void checkTabCount() {
99 if (mUseQuickControls) {
100 int n = mTabBar.getTabCount();
101 if (n >= 2) {
102 mActivity.getActionBar().show();
103 } else if (n == 1) {
104 mActivity.getActionBar().hide();
105 }
106 }
Michael Kolb66706532010-12-12 19:50:22 -0800107 }
108
109 @Override
110 public void onDestroy() {
111 hideFakeTitleBar();
112 }
113
114 // webview factory
115
116 @Override
117 public WebView createWebView(boolean privateBrowsing) {
118 // Create a new WebView
119 ScrollWebView w = new ScrollWebView(mActivity, null,
120 android.R.attr.webViewStyle, privateBrowsing);
121 initWebViewSettings(w);
122 w.setScrollListener(this);
123 w.getSettings().setDisplayZoomControls(false);
124 return w;
125 }
126
127 @Override
128 public WebView createSubWebView(boolean privateBrowsing) {
129 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
130 // no scroll listener for subview
131 web.setScrollListener(null);
132 return web;
133 }
134
135 @Override
136 public void onScroll(int visibleTitleHeight) {
137 mTabBar.onScroll(visibleTitleHeight);
138 }
139
140 void stopWebViewScrolling() {
141 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
142 if (web != null) {
143 web.stopScroll();
144 }
145 }
146
147 // WebView callbacks
148
149 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800150 public void bookmarkedStatusHasChanged(Tab tab) {
151 if (tab.inForeground()) {
152 boolean isBookmark = tab.isBookmarkedSite();
153 mTitleBar.setCurrentUrlIsBookmark(isBookmark);
154 mFakeTitleBar.setCurrentUrlIsBookmark(isBookmark);
155 }
156 }
157
158 @Override
John Reck30c714c2010-12-16 17:30:34 -0800159 public void onProgressChanged(Tab tab) {
160 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800161 mTabBar.onProgress(tab, progress);
162 if (tab.inForeground()) {
163 mFakeTitleBar.setProgress(progress);
164 if (progress == 100) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800165 if (!mFakeTitleBar.isEditingUrl()) {
166 hideFakeTitleBar();
167 if (mUseQuickControls) {
168 mFakeTitleBar.setShowProgressOnly(false);
169 setFakeTitleBarGravity(Gravity.BOTTOM);
170 }
Michael Kolb376b5412010-12-15 11:52:57 -0800171 }
Michael Kolb66706532010-12-12 19:50:22 -0800172 } else {
Michael Kolbbd018d42010-12-15 15:43:39 -0800173 if (mUseQuickControls && !mFakeTitleBar.isEditingUrl()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800174 mFakeTitleBar.setShowProgressOnly(true);
175 if (!isFakeTitleBarShowing()) {
176 setFakeTitleBarGravity(Gravity.TOP);
177 }
178 }
Michael Kolb66706532010-12-12 19:50:22 -0800179 showFakeTitleBar();
180 }
181 }
182 }
183
184 @Override
185 public boolean needsRestoreAllTabs() {
186 return true;
187 }
188
189 @Override
190 public void addTab(Tab tab) {
191 mTabBar.onNewTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800192 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800193 }
194
195 @Override
196 public void setActiveTab(Tab tab) {
197 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800198 ScrollWebView view = (ScrollWebView) tab.getWebView();
199 // TabControl.setCurrentTab has been called before this,
200 // so the tab is guaranteed to have a webview
201 if (view == null) {
202 Log.e(LOGTAG, "active tab with no webview detected");
203 return;
204 }
205 // Request focus on the top window.
206 if (mUseQuickControls) {
207 mPieControl.forceToTop(mContentView);
208 view.setScrollListener(null);
209 mTabBar.showTitleBarIndicator(false);
210 } else {
211 view.setEmbeddedTitleBar(mTitleBar);
212 view.setScrollListener(this);
213 }
Michael Kolb66706532010-12-12 19:50:22 -0800214 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800215 if (tab.isInVoiceSearchMode()) {
216 showVoiceTitleBar(tab.getVoiceDisplayTitle());
217 } else {
218 revertVoiceTitleBar(tab);
219 }
Michael Kolb376b5412010-12-15 11:52:57 -0800220 updateLockIconToLatest(tab);
221 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800222 }
223
224 @Override
225 public void updateTabs(List<Tab> tabs) {
226 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800227 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800228 }
229
230 @Override
231 public void removeTab(Tab tab) {
232 super.removeTab(tab);
233 mTabBar.onRemoveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800234 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800235 }
236
Michael Kolb376b5412010-12-15 11:52:57 -0800237 int getContentWidth() {
238 if (mContentView != null) {
239 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800240 }
241 return 0;
242 }
243
244 void editUrl(boolean clearInput) {
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800245 if (mUiController.isInCustomActionMode()) {
246 mUiController.endActionMode();
247 }
Michael Kolb66706532010-12-12 19:50:22 -0800248 showFakeTitleBar();
249 mFakeTitleBar.onEditUrl(clearInput);
250 }
251
Michael Kolb376b5412010-12-15 11:52:57 -0800252 void setFakeTitleBarGravity(int gravity) {
253 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
254 mFakeTitleBar.getLayoutParams();
255 if (lp == null) {
256 lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
257 LayoutParams.WRAP_CONTENT);
258 }
259 lp.gravity = gravity;
260 mFakeTitleBar.setLayoutParams(lp);
261 }
262
263 void showFakeTitleBarAndEdit() {
Michael Kolbbd018d42010-12-15 15:43:39 -0800264 mFakeTitleBar.setShowProgressOnly(false);
265 setFakeTitleBarGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800266 showFakeTitleBar();
267 mFakeTitleBar.onEditUrl(false);
268 }
269
Michael Kolb66706532010-12-12 19:50:22 -0800270 @Override
271 protected void attachFakeTitleBar(WebView mainView) {
272 mContentView.addView(mFakeTitleBar);
273 mTabBar.onShowTitleBar();
274 }
275
276 @Override
277 protected void hideFakeTitleBar() {
278 if (isFakeTitleBarShowing()) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800279 mFakeTitleBar.setUrlMode(false);
Michael Kolb66706532010-12-12 19:50:22 -0800280 mContentView.removeView(mFakeTitleBar);
281 mTabBar.onHideTitleBar();
282 }
283 }
284
285 @Override
286 protected boolean isFakeTitleBarShowing() {
287 return (mFakeTitleBar.getParent() != null);
288 }
289
290 @Override
291 protected TitleBarBase getFakeTitleBar() {
292 return mFakeTitleBar;
293 }
294
295 @Override
296 protected TitleBarBase getEmbeddedTitleBar() {
297 return mTitleBar;
298 }
299
300 // action mode callbacks
301
302 @Override
303 public void onActionModeStarted(ActionMode mode) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800304 if (!mFakeTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800305 // hide the fake title bar when CAB is shown
306 hideFakeTitleBar();
307 }
308 }
309
310 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800311 public void onActionModeFinished(boolean inLoad) {
312 checkTabCount();
313 if (inLoad) {
314 // the titlebar was removed when the CAB was shown
315 // if the page is loading, show it again
316 mFakeTitleBar.setShowProgressOnly(true);
317 if (!isFakeTitleBarShowing()) {
318 setFakeTitleBarGravity(Gravity.TOP);
319 }
320 showFakeTitleBar();
321 }
322 }
323
324 @Override
John Reck30c714c2010-12-16 17:30:34 -0800325 public void setUrlTitle(Tab tab) {
326 super.setUrlTitle(tab);
327 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800328 }
329
330 // Set the favicon in the title bar.
331 @Override
John Reck30c714c2010-12-16 17:30:34 -0800332 public void setFavicon(Tab tab) {
333 super.setFavicon(tab);
334 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800335 }
336
Michael Kolbcfa3af52010-12-14 10:36:11 -0800337 @Override
338 public void showVoiceTitleBar(String title) {
339 List<String> vsresults = null;
340 if (getActiveTab() != null) {
341 vsresults = getActiveTab().getVoiceSearchResults();
342 }
343 mTitleBar.setInVoiceMode(true, null);
344 mTitleBar.setDisplayTitle(title);
345 mFakeTitleBar.setInVoiceMode(true, vsresults);
346 mFakeTitleBar.setDisplayTitle(title);
347 }
348
349 @Override
350 public void revertVoiceTitleBar(Tab tab) {
351 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800352 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800353 mTitleBar.setDisplayTitle(url);
354 mFakeTitleBar.setInVoiceMode(false, null);
355 mFakeTitleBar.setDisplayTitle(url);
356 }
357
John Reckd73c5a22010-12-22 10:22:50 -0800358 @Override
359 public void showCustomView(View view, CustomViewCallback callback) {
360 super.showCustomView(view, callback);
361 mActivity.getActionBar().hide();
362 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800363
John Reckd73c5a22010-12-22 10:22:50 -0800364 @Override
365 public void onHideCustomView() {
366 super.onHideCustomView();
367 if (mUseQuickControls) {
368 checkTabCount();
369 } else {
370 mActivity.getActionBar().show();
371 }
372 }
Michael Kolb66706532010-12-12 19:50:22 -0800373}