blob: f45b0a16f6efca91894867e457975348b1ea5f38 [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) {
245 showFakeTitleBar();
246 mFakeTitleBar.onEditUrl(clearInput);
247 }
248
Michael Kolb376b5412010-12-15 11:52:57 -0800249 void setFakeTitleBarGravity(int gravity) {
250 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
251 mFakeTitleBar.getLayoutParams();
252 if (lp == null) {
253 lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
254 LayoutParams.WRAP_CONTENT);
255 }
256 lp.gravity = gravity;
257 mFakeTitleBar.setLayoutParams(lp);
258 }
259
260 void showFakeTitleBarAndEdit() {
Michael Kolbbd018d42010-12-15 15:43:39 -0800261 mFakeTitleBar.setShowProgressOnly(false);
262 setFakeTitleBarGravity(Gravity.BOTTOM);
Michael Kolb376b5412010-12-15 11:52:57 -0800263 showFakeTitleBar();
264 mFakeTitleBar.onEditUrl(false);
265 }
266
Michael Kolb66706532010-12-12 19:50:22 -0800267 @Override
268 protected void attachFakeTitleBar(WebView mainView) {
269 mContentView.addView(mFakeTitleBar);
270 mTabBar.onShowTitleBar();
271 }
272
273 @Override
274 protected void hideFakeTitleBar() {
275 if (isFakeTitleBarShowing()) {
276 mContentView.removeView(mFakeTitleBar);
277 mTabBar.onHideTitleBar();
278 }
279 }
280
281 @Override
282 protected boolean isFakeTitleBarShowing() {
283 return (mFakeTitleBar.getParent() != null);
284 }
285
286 @Override
287 protected TitleBarBase getFakeTitleBar() {
288 return mFakeTitleBar;
289 }
290
291 @Override
292 protected TitleBarBase getEmbeddedTitleBar() {
293 return mTitleBar;
294 }
295
296 // action mode callbacks
297
298 @Override
299 public void onActionModeStarted(ActionMode mode) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800300 if (!mFakeTitleBar.isEditingUrl()) {
Michael Kolb66706532010-12-12 19:50:22 -0800301 // hide the fake title bar when CAB is shown
302 hideFakeTitleBar();
303 }
304 }
305
306 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800307 public void onActionModeFinished(boolean inLoad) {
308 checkTabCount();
309 if (inLoad) {
310 // the titlebar was removed when the CAB was shown
311 // if the page is loading, show it again
312 mFakeTitleBar.setShowProgressOnly(true);
313 if (!isFakeTitleBarShowing()) {
314 setFakeTitleBarGravity(Gravity.TOP);
315 }
316 showFakeTitleBar();
317 }
318 }
319
320 @Override
John Reck30c714c2010-12-16 17:30:34 -0800321 public void setUrlTitle(Tab tab) {
322 super.setUrlTitle(tab);
323 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800324 }
325
326 // Set the favicon in the title bar.
327 @Override
John Reck30c714c2010-12-16 17:30:34 -0800328 public void setFavicon(Tab tab) {
329 super.setFavicon(tab);
330 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800331 }
332
Michael Kolbcfa3af52010-12-14 10:36:11 -0800333 @Override
334 public void showVoiceTitleBar(String title) {
335 List<String> vsresults = null;
336 if (getActiveTab() != null) {
337 vsresults = getActiveTab().getVoiceSearchResults();
338 }
339 mTitleBar.setInVoiceMode(true, null);
340 mTitleBar.setDisplayTitle(title);
341 mFakeTitleBar.setInVoiceMode(true, vsresults);
342 mFakeTitleBar.setDisplayTitle(title);
343 }
344
345 @Override
346 public void revertVoiceTitleBar(Tab tab) {
347 mTitleBar.setInVoiceMode(false, null);
John Reck30c714c2010-12-16 17:30:34 -0800348 String url = tab.getUrl();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800349 mTitleBar.setDisplayTitle(url);
350 mFakeTitleBar.setInVoiceMode(false, null);
351 mFakeTitleBar.setDisplayTitle(url);
352 }
353
John Reckd73c5a22010-12-22 10:22:50 -0800354 @Override
355 public void showCustomView(View view, CustomViewCallback callback) {
356 super.showCustomView(view, callback);
357 mActivity.getActionBar().hide();
358 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800359
John Reckd73c5a22010-12-22 10:22:50 -0800360 @Override
361 public void onHideCustomView() {
362 super.onHideCustomView();
363 if (mUseQuickControls) {
364 checkTabCount();
365 } else {
366 mActivity.getActionBar().show();
367 }
368 }
Michael Kolb66706532010-12-12 19:50:22 -0800369}