blob: dcba39bbe5c2cc232a445a599f3ab31d3a4e5f1d [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;
Michael Kolb66706532010-12-12 19:50:22 -080027import android.webkit.WebView;
Michael Kolb376b5412010-12-15 11:52:57 -080028import android.widget.FrameLayout;
29import android.widget.FrameLayout.LayoutParams;
Michael Kolb66706532010-12-12 19:50:22 -080030
31import java.util.List;
32
33/**
34 * Ui for xlarge screen sizes
35 */
36public class XLargeUi extends BaseUi implements ScrollListener {
37
38 private static final String LOGTAG = "XLargeUi";
39
Michael Kolb376b5412010-12-15 11:52:57 -080040 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080041 private TabBar mTabBar;
42
43 private TitleBarXLarge mTitleBar;
44 private TitleBarXLarge mFakeTitleBar;
45
Michael Kolb376b5412010-12-15 11:52:57 -080046 private boolean mUseQuickControls;
47 private PieControl mPieControl;
48
Michael Kolb66706532010-12-12 19:50:22 -080049 /**
50 * @param browser
51 * @param controller
52 */
53 public XLargeUi(Activity browser, UiController controller) {
54 super(browser, controller);
55 mTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
56 mTitleBar.setProgress(100);
Michael Kolbcfa3af52010-12-14 10:36:11 -080057 mTitleBar.setEditable(false);
Michael Kolb66706532010-12-12 19:50:22 -080058 mFakeTitleBar = new TitleBarXLarge(mActivity, mUiController, this);
Michael Kolbcfa3af52010-12-14 10:36:11 -080059 mFakeTitleBar.setEditable(true);
Michael Kolb66706532010-12-12 19:50:22 -080060 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080061 mActionBar = mActivity.getActionBar();
62 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
63 mActionBar.setCustomView(mTabBar);
64 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
65 }
66
67 private void setUseQuickControls(boolean useQuickControls) {
68 mUseQuickControls = useQuickControls;
69 if (useQuickControls) {
70 checkTabCount();
71 mPieControl = new PieControl(mActivity, mUiController, this);
72 mPieControl.attachToContainer(mContentView);
73 setFakeTitleBarGravity(Gravity.BOTTOM);
74
75 // remove embedded title bar if present
76 WebView web = mTabControl.getCurrentWebView();
77 if ((web != null) && (web.getVisibleTitleHeight() > 0)) {
78 web.setEmbeddedTitleBar(null);
79 }
80 } else {
81 mActivity.getActionBar().show();
82 if (mPieControl != null) {
83 mPieControl.removeFromContainer(mContentView);
84 }
85 setFakeTitleBarGravity(Gravity.TOP);
86 // remove embedded title bar if present
87 WebView web = mTabControl.getCurrentWebView();
88 if ((web != null) && (web.getVisibleTitleHeight() == 0)) {
89 web.setEmbeddedTitleBar(mTitleBar);
90 }
91 }
92 mTabBar.setUseQuickControls(mUseQuickControls);
93 mFakeTitleBar.setUseQuickControls(mUseQuickControls);
94 }
95
96 private void checkTabCount() {
97 if (mUseQuickControls) {
98 int n = mTabBar.getTabCount();
99 if (n >= 2) {
100 mActivity.getActionBar().show();
101 } else if (n == 1) {
102 mActivity.getActionBar().hide();
103 }
104 }
Michael Kolb66706532010-12-12 19:50:22 -0800105 }
106
107 @Override
108 public void onDestroy() {
109 hideFakeTitleBar();
110 }
111
112 // webview factory
113
114 @Override
115 public WebView createWebView(boolean privateBrowsing) {
116 // Create a new WebView
117 ScrollWebView w = new ScrollWebView(mActivity, null,
118 android.R.attr.webViewStyle, privateBrowsing);
119 initWebViewSettings(w);
120 w.setScrollListener(this);
121 w.getSettings().setDisplayZoomControls(false);
122 return w;
123 }
124
125 @Override
126 public WebView createSubWebView(boolean privateBrowsing) {
127 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
128 // no scroll listener for subview
129 web.setScrollListener(null);
130 return web;
131 }
132
133 @Override
134 public void onScroll(int visibleTitleHeight) {
135 mTabBar.onScroll(visibleTitleHeight);
136 }
137
138 void stopWebViewScrolling() {
139 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
140 if (web != null) {
141 web.stopScroll();
142 }
143 }
144
145 // WebView callbacks
146
147 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800148 public void bookmarkedStatusHasChanged(Tab tab) {
149 if (tab.inForeground()) {
150 boolean isBookmark = tab.isBookmarkedSite();
151 mTitleBar.setCurrentUrlIsBookmark(isBookmark);
152 mFakeTitleBar.setCurrentUrlIsBookmark(isBookmark);
153 }
154 }
155
156 @Override
John Reck30c714c2010-12-16 17:30:34 -0800157 public void onProgressChanged(Tab tab) {
158 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800159 mTabBar.onProgress(tab, progress);
160 if (tab.inForeground()) {
161 mFakeTitleBar.setProgress(progress);
162 if (progress == 100) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800163 if (!mFakeTitleBar.isEditingUrl()) {
164 hideFakeTitleBar();
165 if (mUseQuickControls) {
166 mFakeTitleBar.setShowProgressOnly(false);
167 setFakeTitleBarGravity(Gravity.BOTTOM);
168 }
Michael Kolb376b5412010-12-15 11:52:57 -0800169 }
Michael Kolb66706532010-12-12 19:50:22 -0800170 } else {
Michael Kolbbd018d42010-12-15 15:43:39 -0800171 if (mUseQuickControls && !mFakeTitleBar.isEditingUrl()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800172 mFakeTitleBar.setShowProgressOnly(true);
173 if (!isFakeTitleBarShowing()) {
174 setFakeTitleBarGravity(Gravity.TOP);
175 }
176 }
Michael Kolb66706532010-12-12 19:50:22 -0800177 showFakeTitleBar();
178 }
179 }
180 }
181
182 @Override
183 public boolean needsRestoreAllTabs() {
184 return true;
185 }
186
187 @Override
188 public void addTab(Tab tab) {
189 mTabBar.onNewTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800190 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800191 }
192
193 @Override
194 public void setActiveTab(Tab tab) {
195 super.setActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800196 ScrollWebView view = (ScrollWebView) tab.getWebView();
197 // TabControl.setCurrentTab has been called before this,
198 // so the tab is guaranteed to have a webview
199 if (view == null) {
200 Log.e(LOGTAG, "active tab with no webview detected");
201 return;
202 }
203 // Request focus on the top window.
204 if (mUseQuickControls) {
205 mPieControl.forceToTop(mContentView);
206 view.setScrollListener(null);
207 mTabBar.showTitleBarIndicator(false);
208 } else {
209 view.setEmbeddedTitleBar(mTitleBar);
210 view.setScrollListener(this);
211 }
Michael Kolb66706532010-12-12 19:50:22 -0800212 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800213 if (tab.isInVoiceSearchMode()) {
214 showVoiceTitleBar(tab.getVoiceDisplayTitle());
215 } else {
216 revertVoiceTitleBar(tab);
217 }
Michael Kolb376b5412010-12-15 11:52:57 -0800218 updateLockIconToLatest(tab);
219 tab.getTopWindow().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800220 }
221
222 @Override
223 public void updateTabs(List<Tab> tabs) {
224 mTabBar.updateTabs(tabs);
Michael Kolb376b5412010-12-15 11:52:57 -0800225 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800226 }
227
228 @Override
229 public void removeTab(Tab tab) {
230 super.removeTab(tab);
231 mTabBar.onRemoveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800232 checkTabCount();
Michael Kolb66706532010-12-12 19:50:22 -0800233 }
234
Michael Kolb376b5412010-12-15 11:52:57 -0800235 int getContentWidth() {
236 if (mContentView != null) {
237 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800238 }
239 return 0;
240 }
241
242 void editUrl(boolean clearInput) {
243 showFakeTitleBar();
244 mFakeTitleBar.onEditUrl(clearInput);
245 }
246
Michael Kolb376b5412010-12-15 11:52:57 -0800247 void setFakeTitleBarGravity(int gravity) {
248 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
249 mFakeTitleBar.getLayoutParams();
250 if (lp == null) {
251 lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
252 LayoutParams.WRAP_CONTENT);
253 }
254 lp.gravity = gravity;
255 mFakeTitleBar.setLayoutParams(lp);
256 }
257
258 void showFakeTitleBarAndEdit() {
Michael Kolbbd018d42010-12-15 15:43:39 -0800259 mFakeTitleBar.setShowProgressOnly(false);
260 setFakeTitleBarGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800261 showFakeTitleBar();
262 mFakeTitleBar.onEditUrl(false);
263 }
264
Michael Kolb66706532010-12-12 19:50:22 -0800265 @Override
266 protected void attachFakeTitleBar(WebView mainView) {
267 mContentView.addView(mFakeTitleBar);
268 mTabBar.onShowTitleBar();
269 }
270
271 @Override
272 protected void hideFakeTitleBar() {
273 if (isFakeTitleBarShowing()) {
274 mContentView.removeView(mFakeTitleBar);
275 mTabBar.onHideTitleBar();
276 }
277 }
278
279 @Override
280 protected boolean isFakeTitleBarShowing() {
281 return (mFakeTitleBar.getParent() != null);
282 }
283
284 @Override
285 protected TitleBarBase getFakeTitleBar() {
286 return mFakeTitleBar;
287 }
288
289 @Override
290 protected TitleBarBase getEmbeddedTitleBar() {
291 return mTitleBar;
292 }
293
294 // action mode callbacks
295
296 @Override
297 public void onActionModeStarted(ActionMode mode) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800298 if (!mFakeTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800299 // hide the fake title bar when CAB is shown
300 hideFakeTitleBar();
301 }
302 }
303
304 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800305 public void onActionModeFinished(boolean inLoad) {
306 checkTabCount();
307 if (inLoad) {
308 // the titlebar was removed when the CAB was shown
309 // if the page is loading, show it again
310 mFakeTitleBar.setShowProgressOnly(true);
311 if (!isFakeTitleBarShowing()) {
312 setFakeTitleBarGravity(Gravity.TOP);
313 }
314 showFakeTitleBar();
315 }
316 }
317
318 @Override
John Reck30c714c2010-12-16 17:30:34 -0800319 public void setUrlTitle(Tab tab) {
320 super.setUrlTitle(tab);
321 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800322 }
323
324 // Set the favicon in the title bar.
325 @Override
John Reck30c714c2010-12-16 17:30:34 -0800326 public void setFavicon(Tab tab) {
327 super.setFavicon(tab);
328 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800329 }
330
Michael Kolbcfa3af52010-12-14 10:36:11 -0800331 @Override
332 public void showVoiceTitleBar(String title) {
333 List<String> vsresults = null;
334 if (getActiveTab() != null) {
335 vsresults = getActiveTab().getVoiceSearchResults();
336 }
337 mTitleBar.setInVoiceMode(true, null);
338 mTitleBar.setDisplayTitle(title);
339 mFakeTitleBar.setInVoiceMode(true, vsresults);
340 mFakeTitleBar.setDisplayTitle(title);
341 }
342
343 @Override
344 public void revertVoiceTitleBar(Tab tab) {
345 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800346 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800347 mTitleBar.setDisplayTitle(url);
348 mFakeTitleBar.setInVoiceMode(false, null);
349 mFakeTitleBar.setDisplayTitle(url);
350 }
351
352
Michael Kolb66706532010-12-12 19:50:22 -0800353}