blob: d1db57860f235a3e9e1b2cba4b8b9af2ff7578aa [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 android.app.Activity;
Michael Kolb4bd767d2011-05-27 11:33:55 -070020import android.os.Bundle;
Michael Kolb376b5412010-12-15 11:52:57 -080021import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080022import android.view.ActionMode;
23import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080024import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080025import android.view.Menu;
26import android.view.View;
Michael Kolb66706532010-12-12 19:50:22 -080027import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070028import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080029
30/**
31 * Ui for regular phone screen sizes
32 */
33public class PhoneUi extends BaseUi {
34
35 private static final String LOGTAG = "PhoneUi";
36
Michael Kolb11d19782011-03-20 10:17:40 -070037 private TitleBarPhone mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080038 private ActiveTabsPage mActiveTabsPage;
Michael Kolb0241e752011-07-07 14:58:50 -070039 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070040 private NavScreen mNavScreen;
Michael Kolb66706532010-12-12 19:50:22 -080041
42 boolean mExtendedMenuOpen;
43 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070044 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080045
46 /**
47 * @param browser
48 * @param controller
49 */
50 public PhoneUi(Activity browser, UiController controller) {
51 super(browser, controller);
Michael Kolb46f987e2011-04-05 10:39:10 -070052 mTitleBar = new TitleBarPhone(mActivity, mUiController, this,
53 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080054 // mTitleBar will be always be shown in the fully loaded mode on
55 // phone
56 mTitleBar.setProgress(100);
John Reck285ef042011-02-11 15:44:17 -080057 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070058 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck285ef042011-02-11 15:44:17 -080059 }
Michael Kolb66706532010-12-12 19:50:22 -080060
John Reck285ef042011-02-11 15:44:17 -080061 @Override
62 public void hideComboView() {
63 super.hideComboView();
64 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080065 }
66
Michael Kolb66706532010-12-12 19:50:22 -080067 // lifecycle
68
69 @Override
70 public void onPause() {
71 // FIXME: This removes the active tabs page and resets the menu to
72 // MAIN_MENU. A better solution might be to do this work in onNewIntent
73 // but then we would need to save it in onSaveInstanceState and restore
74 // it in onCreate/onRestoreInstanceState
75 if (mActiveTabsPage != null) {
76 mUiController.removeActiveTabsPage(true);
77 }
78 super.onPause();
79 }
80
81 @Override
82 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080083 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080084 }
85
86 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080087 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070088 if (mUseQuickControls) {
89 getTitleBar().setShowProgressOnly(false);
90 }
91 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080092 }
93
94 @Override
Michael Kolb66706532010-12-12 19:50:22 -080095 public boolean onBackKey() {
96 if (mActiveTabsPage != null) {
97 // if tab page is showing, hide it
98 mUiController.removeActiveTabsPage(true);
99 return true;
Michael Kolbf2055602011-04-09 17:20:03 -0700100 } else if (mNavScreen != null) {
101 mNavScreen.close();
102 return true;
Michael Kolb66706532010-12-12 19:50:22 -0800103 }
104 return super.onBackKey();
105 }
106
107 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700108 public boolean onMenuKey() {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700109 if (!isComboViewShowing()) {
110 if (mNavScreen == null) {
111 showNavScreen();
112 } else {
113 mNavScreen.close();
114 }
115 return true;
Michael Kolb2814a362011-05-19 15:49:41 -0700116 } else {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700117 return false;
Michael Kolb2814a362011-05-19 15:49:41 -0700118 }
Michael Kolb2814a362011-05-19 15:49:41 -0700119 }
120
121 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700122 public boolean dispatchKey(int code, KeyEvent event) {
123 if (!isComboViewShowing()) {
Michael Kolbf2055602011-04-09 17:20:03 -0700124 }
125 return false;
126 }
127
128 @Override
John Reck30c714c2010-12-16 17:30:34 -0800129 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800130 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800131 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800132 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800133 if (progress == 100) {
134 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700135 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700136 if (mUseQuickControls) {
137 mTitleBar.setShowProgressOnly(false);
138 }
Michael Kolb66706532010-12-12 19:50:22 -0800139 }
140 } else {
141 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700142 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
143 mTitleBar.setShowProgressOnly(true);
144 setTitleGravity(Gravity.TOP);
145 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800146 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800147 }
148 }
149 }
150 }
151
152 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700153 public void setActiveTab(final Tab tab) {
John Reck88a42b72011-03-21 16:30:12 -0700154 captureTab(mActiveTab);
John Reck5d43ce82011-06-21 17:16:51 -0700155 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700156 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800157 // TabControl.setCurrentTab has been called before this,
158 // so the tab is guaranteed to have a webview
159 if (view == null) {
160 Log.e(LOGTAG, "active tab with no webview detected");
161 return;
162 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700163 // Request focus on the top window.
164 if (mUseQuickControls) {
165 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700166 } else {
167 // check if title bar is already attached by animation
168 if (mTitleBar.getParent() == null) {
169 view.setEmbeddedTitleBar(mTitleBar);
170 }
171 }
Michael Kolb376b5412010-12-15 11:52:57 -0800172 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700173 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800174 } else {
175 revertVoiceTitleBar(tab);
176 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700177 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800178 tab.getTopWindow().requestFocus();
179 }
180
181 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800182 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800183 return mTitleBar;
184 }
185
Michael Kolb017ffab2011-07-11 15:26:47 -0700186 /**
187 * Suggest to the UI that the title bar can be hidden. The UI will then
188 * decide whether or not to hide based off a number of factors, such
189 * as if the user is editing the URL bar or if the page is loading
190 */
191 @Override
192 public void suggestHideTitleBar() {
193 if (!isLoading() && !isEditingUrl() && !mTitleBar.isMenuShowing()) {
194 hideTitleBar();
195 }
196 }
197
Michael Kolb66706532010-12-12 19:50:22 -0800198 // active tabs page
199
200 @Override
201 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700202 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800203 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
204 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800205 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800206 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
207 mActiveTabsPage.requestFocus();
208 }
209
210 /**
211 * Remove the active tabs page.
212 */
213 @Override
214 public void removeActiveTabsPage() {
215 mContentView.removeView(mActiveTabsPage);
216 mTitleBar.setVisibility(View.VISIBLE);
217 mActiveTabsPage = null;
218 }
219
220 @Override
John Reck2bc80422011-06-30 15:11:49 -0700221 public void showComboView(ComboViews startWith, Bundle extras) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700222 if (mNavScreen != null) {
223 hideNavScreen(false);
224 }
John Reck2bc80422011-06-30 15:11:49 -0700225 super.showComboView(startWith, extras);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700226 }
227
228 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800229 public boolean showsWeb() {
230 return super.showsWeb() && mActiveTabsPage == null;
231 }
232
233 // menu handling callbacks
234
235 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800236 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800237 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800238 }
239
240 @Override
241 public void onContextMenuClosed(Menu menu, boolean inLoad) {
242 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800243 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800244 }
245 }
246
247 // action mode callbacks
248
249 @Override
250 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800251 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800252 }
253
Michael Kolba4183062011-01-16 10:43:21 -0800254 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700255 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700256 if (inLoad) {
257 if (mUseQuickControls) {
258 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700259 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700260 showTitleBar();
261 }
262 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700263 }
264
265 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700266 protected void setTitleGravity(int gravity) {
267 if (mUseQuickControls) {
268 FrameLayout.LayoutParams lp =
269 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
270 lp.gravity = gravity;
271 getTitleBar().setLayoutParams(lp);
272 } else {
273 super.setTitleGravity(gravity);
274 }
275 }
276
Michael Kolb0241e752011-07-07 14:58:50 -0700277 @Override
278 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700279 mUseQuickControls = useQuickControls;
280 getTitleBar().setUseQuickControls(mUseQuickControls);
281 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700282 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700283 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700284 WebView web = getWebView();
285 if (web != null) {
286 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -0700287 // don't show url bar on scrolling
288 web.setOnTouchListener(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700289 }
290 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700291 if (mPieControl != null) {
292 mPieControl.removeFromContainer(mContentView);
293 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700294 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700295 if (web != null) {
296 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -0700297 // show url bar on scrolling
298 web.setOnTouchListener(this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700299 }
300 setTitleGravity(Gravity.NO_GRAVITY);
301 }
302 }
303
304 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700305 protected void captureTab(final Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700306 if (tab == null) return;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700307 BrowserWebView web = (BrowserWebView) tab.getWebView();
308 if (web != null) {
309 tab.setScreenshot(web.capture());
Michael Kolbfdb70242011-03-24 09:41:11 -0700310 }
311 }
Michael Kolb4bd767d2011-05-27 11:33:55 -0700312
Michael Kolbf2055602011-04-09 17:20:03 -0700313 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700314 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700315 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700316 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700317 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700318 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700319 mCustomViewContainer.setVisibility(View.VISIBLE);
320 mCustomViewContainer.bringToFront();
321 }
322
Michael Kolb2814a362011-05-19 15:49:41 -0700323 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700324 if (mNavScreen == null) return;
Michael Kolb2814a362011-05-19 15:49:41 -0700325 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700326 mCustomViewContainer.removeView(mNavScreen);
327 mNavScreen = null;
328 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700329 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700330 // Show the content view.
331 mContentView.setVisibility(View.VISIBLE);
332 }
333
Michael Kolb66706532010-12-12 19:50:22 -0800334}