blob: f32308d79e230bb22cd4795245c51535d301e114 [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 Kolb66706532010-12-12 19:50:22 -080037 private ActiveTabsPage mActiveTabsPage;
Michael Kolb0241e752011-07-07 14:58:50 -070038 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070039 private NavScreen mNavScreen;
John Reck0f602f32011-07-07 15:38:43 -070040 private NavigationBarPhone mNavigationBar;
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);
John Reck285ef042011-02-11 15:44:17 -080052 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070053 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070054 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reck285ef042011-02-11 15:44:17 -080055 }
Michael Kolb66706532010-12-12 19:50:22 -080056
John Reck285ef042011-02-11 15:44:17 -080057 @Override
58 public void hideComboView() {
59 super.hideComboView();
60 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080061 }
62
Michael Kolb66706532010-12-12 19:50:22 -080063 // lifecycle
64
65 @Override
66 public void onPause() {
67 // FIXME: This removes the active tabs page and resets the menu to
68 // MAIN_MENU. A better solution might be to do this work in onNewIntent
69 // but then we would need to save it in onSaveInstanceState and restore
70 // it in onCreate/onRestoreInstanceState
71 if (mActiveTabsPage != null) {
72 mUiController.removeActiveTabsPage(true);
73 }
74 super.onPause();
75 }
76
77 @Override
78 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080079 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080080 }
81
82 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080083 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070084 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070085 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070086 }
87 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080088 }
89
90 @Override
Michael Kolb66706532010-12-12 19:50:22 -080091 public boolean onBackKey() {
92 if (mActiveTabsPage != null) {
93 // if tab page is showing, hide it
94 mUiController.removeActiveTabsPage(true);
95 return true;
Michael Kolbf2055602011-04-09 17:20:03 -070096 } else if (mNavScreen != null) {
97 mNavScreen.close();
98 return true;
Michael Kolb66706532010-12-12 19:50:22 -080099 }
100 return super.onBackKey();
101 }
102
103 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700104 public boolean onMenuKey() {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700105 if (!isComboViewShowing()) {
106 if (mNavScreen == null) {
107 showNavScreen();
108 } else {
109 mNavScreen.close();
110 }
111 return true;
Michael Kolb2814a362011-05-19 15:49:41 -0700112 } else {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700113 return false;
Michael Kolb2814a362011-05-19 15:49:41 -0700114 }
Michael Kolb2814a362011-05-19 15:49:41 -0700115 }
116
117 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700118 public boolean dispatchKey(int code, KeyEvent event) {
119 if (!isComboViewShowing()) {
Michael Kolbf2055602011-04-09 17:20:03 -0700120 }
121 return false;
122 }
123
124 @Override
John Reck30c714c2010-12-16 17:30:34 -0800125 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800126 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800127 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800128 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800129 if (progress == 100) {
130 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700131 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700132 if (mUseQuickControls) {
133 mTitleBar.setShowProgressOnly(false);
134 }
Michael Kolb66706532010-12-12 19:50:22 -0800135 }
136 } else {
137 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700138 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
139 mTitleBar.setShowProgressOnly(true);
140 setTitleGravity(Gravity.TOP);
141 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800142 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800143 }
144 }
145 }
146 }
147
148 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700149 public void setActiveTab(final Tab tab) {
John Reck88a42b72011-03-21 16:30:12 -0700150 captureTab(mActiveTab);
John Reck5d43ce82011-06-21 17:16:51 -0700151 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700152 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800153 // TabControl.setCurrentTab has been called before this,
154 // so the tab is guaranteed to have a webview
155 if (view == null) {
156 Log.e(LOGTAG, "active tab with no webview detected");
157 return;
158 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700159 // Request focus on the top window.
160 if (mUseQuickControls) {
161 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700162 } else {
163 // check if title bar is already attached by animation
164 if (mTitleBar.getParent() == null) {
165 view.setEmbeddedTitleBar(mTitleBar);
166 }
167 }
Michael Kolb376b5412010-12-15 11:52:57 -0800168 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700169 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800170 } else {
171 revertVoiceTitleBar(tab);
172 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700173 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800174 tab.getTopWindow().requestFocus();
175 }
176
Michael Kolb017ffab2011-07-11 15:26:47 -0700177 /**
178 * Suggest to the UI that the title bar can be hidden. The UI will then
179 * decide whether or not to hide based off a number of factors, such
180 * as if the user is editing the URL bar or if the page is loading
181 */
182 @Override
183 public void suggestHideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700184 if (!mNavigationBar.isMenuShowing()) {
185 super.suggestHideTitleBar();
Michael Kolb017ffab2011-07-11 15:26:47 -0700186 }
187 }
188
Michael Kolb66706532010-12-12 19:50:22 -0800189 // active tabs page
190
191 @Override
192 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700193 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800194 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
195 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800196 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800197 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
198 mActiveTabsPage.requestFocus();
199 }
200
201 /**
202 * Remove the active tabs page.
203 */
204 @Override
205 public void removeActiveTabsPage() {
206 mContentView.removeView(mActiveTabsPage);
207 mTitleBar.setVisibility(View.VISIBLE);
208 mActiveTabsPage = null;
209 }
210
211 @Override
John Reck2bc80422011-06-30 15:11:49 -0700212 public void showComboView(ComboViews startWith, Bundle extras) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700213 if (mNavScreen != null) {
214 hideNavScreen(false);
215 }
John Reck2bc80422011-06-30 15:11:49 -0700216 super.showComboView(startWith, extras);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700217 }
218
219 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800220 public boolean showsWeb() {
221 return super.showsWeb() && mActiveTabsPage == null;
222 }
223
224 // menu handling callbacks
225
226 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800227 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800228 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800229 }
230
231 @Override
232 public void onContextMenuClosed(Menu menu, boolean inLoad) {
233 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800234 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800235 }
236 }
237
238 // action mode callbacks
239
240 @Override
241 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800242 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800243 }
244
Michael Kolba4183062011-01-16 10:43:21 -0800245 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700246 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700247 if (inLoad) {
248 if (mUseQuickControls) {
249 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700250 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700251 showTitleBar();
252 }
253 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700254 }
255
256 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700257 protected void setTitleGravity(int gravity) {
258 if (mUseQuickControls) {
259 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700260 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700261 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700262 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700263 } else {
264 super.setTitleGravity(gravity);
265 }
266 }
267
Michael Kolb0241e752011-07-07 14:58:50 -0700268 @Override
269 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700270 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700271 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700272 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700273 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700274 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700275 WebView web = getWebView();
276 if (web != null) {
277 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -0700278 // don't show url bar on scrolling
279 web.setOnTouchListener(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700280 }
281 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700282 if (mPieControl != null) {
283 mPieControl.removeFromContainer(mContentView);
284 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700285 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700286 if (web != null) {
287 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -0700288 // show url bar on scrolling
289 web.setOnTouchListener(this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700290 }
291 setTitleGravity(Gravity.NO_GRAVITY);
292 }
293 }
294
295 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700296 protected void captureTab(final Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700297 if (tab == null) return;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700298 BrowserWebView web = (BrowserWebView) tab.getWebView();
299 if (web != null) {
300 tab.setScreenshot(web.capture());
Michael Kolbfdb70242011-03-24 09:41:11 -0700301 }
302 }
Michael Kolb4bd767d2011-05-27 11:33:55 -0700303
Michael Kolbf2055602011-04-09 17:20:03 -0700304 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700305 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700306 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700307 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700308 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700309 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700310 mCustomViewContainer.setVisibility(View.VISIBLE);
311 mCustomViewContainer.bringToFront();
312 }
313
Michael Kolb2814a362011-05-19 15:49:41 -0700314 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700315 if (mNavScreen == null) return;
Michael Kolb2814a362011-05-19 15:49:41 -0700316 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700317 mCustomViewContainer.removeView(mNavScreen);
318 mNavScreen = null;
319 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700320 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700321 // Show the content view.
322 mContentView.setVisibility(View.VISIBLE);
323 }
324
John Reck6b4bd5f2011-07-12 10:14:38 -0700325 @Override
326 public boolean needsRestoreAllTabs() {
327 return false;
328 }
329
Michael Kolb66706532010-12-12 19:50:22 -0800330}