blob: d0b0f77b78af75a86b31181aafb3182ee18de31c [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 Kolbfdb70242011-03-24 09:41:11 -070039 private boolean mUseQuickControls;
40 private PieControl mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070041 private NavScreen mNavScreen;
Michael Kolb66706532010-12-12 19:50:22 -080042
43 boolean mExtendedMenuOpen;
44 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070045 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080046
47 /**
48 * @param browser
49 * @param controller
50 */
51 public PhoneUi(Activity browser, UiController controller) {
52 super(browser, controller);
Michael Kolb46f987e2011-04-05 10:39:10 -070053 mTitleBar = new TitleBarPhone(mActivity, mUiController, this,
54 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080055 // mTitleBar will be always be shown in the fully loaded mode on
56 // phone
57 mTitleBar.setProgress(100);
John Reck285ef042011-02-11 15:44:17 -080058 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070059 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck285ef042011-02-11 15:44:17 -080060 }
Michael Kolb66706532010-12-12 19:50:22 -080061
John Reck285ef042011-02-11 15:44:17 -080062 @Override
63 public void hideComboView() {
64 super.hideComboView();
65 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080066 }
67
Michael Kolb66706532010-12-12 19:50:22 -080068 // lifecycle
69
70 @Override
71 public void onPause() {
72 // FIXME: This removes the active tabs page and resets the menu to
73 // MAIN_MENU. A better solution might be to do this work in onNewIntent
74 // but then we would need to save it in onSaveInstanceState and restore
75 // it in onCreate/onRestoreInstanceState
76 if (mActiveTabsPage != null) {
77 mUiController.removeActiveTabsPage(true);
78 }
79 super.onPause();
80 }
81
82 @Override
83 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080084 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080085 }
86
87 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080088 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070089 if (mUseQuickControls) {
90 getTitleBar().setShowProgressOnly(false);
91 }
92 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080093 }
94
95 @Override
Michael Kolb66706532010-12-12 19:50:22 -080096 public boolean onBackKey() {
97 if (mActiveTabsPage != null) {
98 // if tab page is showing, hide it
99 mUiController.removeActiveTabsPage(true);
100 return true;
Michael Kolbf2055602011-04-09 17:20:03 -0700101 } else if (mNavScreen != null) {
102 mNavScreen.close();
103 return true;
Michael Kolb66706532010-12-12 19:50:22 -0800104 }
105 return super.onBackKey();
106 }
107
108 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700109 public boolean onMenuKey() {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700110 if (!isComboViewShowing()) {
111 if (mNavScreen == null) {
112 showNavScreen();
113 } else {
114 mNavScreen.close();
115 }
116 return true;
Michael Kolb2814a362011-05-19 15:49:41 -0700117 } else {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700118 return false;
Michael Kolb2814a362011-05-19 15:49:41 -0700119 }
Michael Kolb2814a362011-05-19 15:49:41 -0700120 }
121
122 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700123 public boolean dispatchKey(int code, KeyEvent event) {
124 if (!isComboViewShowing()) {
Michael Kolbf2055602011-04-09 17:20:03 -0700125 }
126 return false;
127 }
128
129 @Override
John Reck30c714c2010-12-16 17:30:34 -0800130 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800131 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800132 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800133 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800134 if (progress == 100) {
135 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700136 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700137 if (mUseQuickControls) {
138 mTitleBar.setShowProgressOnly(false);
139 }
Michael Kolb66706532010-12-12 19:50:22 -0800140 }
141 } else {
142 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700143 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
144 mTitleBar.setShowProgressOnly(true);
145 setTitleGravity(Gravity.TOP);
146 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800147 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800148 }
149 }
150 }
151 }
152
153 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700154 public void setActiveTab(final Tab tab) {
John Reck88a42b72011-03-21 16:30:12 -0700155 captureTab(mActiveTab);
John Reck5d43ce82011-06-21 17:16:51 -0700156 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700157 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800158 // TabControl.setCurrentTab has been called before this,
159 // so the tab is guaranteed to have a webview
160 if (view == null) {
161 Log.e(LOGTAG, "active tab with no webview detected");
162 return;
163 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700164 // Request focus on the top window.
165 if (mUseQuickControls) {
166 mPieControl.forceToTop(mContentView);
167 view.setScrollListener(null);
168 } else {
169 // check if title bar is already attached by animation
170 if (mTitleBar.getParent() == null) {
171 view.setEmbeddedTitleBar(mTitleBar);
172 }
John Reck5d43ce82011-06-21 17:16:51 -0700173 view.setScrollListener(this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700174 }
Michael Kolb376b5412010-12-15 11:52:57 -0800175 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700176 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800177 } else {
178 revertVoiceTitleBar(tab);
179 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700180 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800181 tab.getTopWindow().requestFocus();
182 }
183
184 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800185 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800186 return mTitleBar;
187 }
188
189 // 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
Michael Kolb4bd767d2011-05-27 11:33:55 -0700212 public void showComboView(boolean startWithHistory, Bundle extras) {
213 if (mNavScreen != null) {
214 hideNavScreen(false);
215 }
216 super.showComboView(startWithHistory, extras);
217 }
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 =
260 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
261 lp.gravity = gravity;
262 getTitleBar().setLayoutParams(lp);
263 } else {
264 super.setTitleGravity(gravity);
265 }
266 }
267
268 private void setUseQuickControls(boolean useQuickControls) {
269 mUseQuickControls = useQuickControls;
270 getTitleBar().setUseQuickControls(mUseQuickControls);
271 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700272 mPieControl = new PieControl(mActivity, mUiController, this);
273 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700274 WebView web = getWebView();
275 if (web != null) {
276 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700277 }
278 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700279 if (mPieControl != null) {
280 mPieControl.removeFromContainer(mContentView);
281 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700282 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700283 if (web != null) {
284 web.setEmbeddedTitleBar(mTitleBar);
285 }
286 setTitleGravity(Gravity.NO_GRAVITY);
287 }
288 }
289
290 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700291 protected void captureTab(final Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700292 if (tab == null) return;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700293 BrowserWebView web = (BrowserWebView) tab.getWebView();
294 if (web != null) {
295 tab.setScreenshot(web.capture());
Michael Kolbfdb70242011-03-24 09:41:11 -0700296 }
297 }
Michael Kolb4bd767d2011-05-27 11:33:55 -0700298
Michael Kolbf2055602011-04-09 17:20:03 -0700299 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700300 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700301 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700302 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700303 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700304 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700305 mCustomViewContainer.setVisibility(View.VISIBLE);
306 mCustomViewContainer.bringToFront();
307 }
308
Michael Kolb2814a362011-05-19 15:49:41 -0700309 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700310 if (mNavScreen == null) return;
Michael Kolb2814a362011-05-19 15:49:41 -0700311 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700312 mCustomViewContainer.removeView(mNavScreen);
313 mNavScreen = null;
314 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700315 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700316 // Show the content view.
317 mContentView.setVisibility(View.VISIBLE);
318 }
319
Michael Kolb66706532010-12-12 19:50:22 -0800320}