blob: 0c236afdbe7299328fa794e468c4f64723f33bae [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 PieControl 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
186 // active tabs page
187
188 @Override
189 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700190 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800191 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
192 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800193 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800194 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
195 mActiveTabsPage.requestFocus();
196 }
197
198 /**
199 * Remove the active tabs page.
200 */
201 @Override
202 public void removeActiveTabsPage() {
203 mContentView.removeView(mActiveTabsPage);
204 mTitleBar.setVisibility(View.VISIBLE);
205 mActiveTabsPage = null;
206 }
207
208 @Override
John Reck2bc80422011-06-30 15:11:49 -0700209 public void showComboView(ComboViews startWith, Bundle extras) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700210 if (mNavScreen != null) {
211 hideNavScreen(false);
212 }
John Reck2bc80422011-06-30 15:11:49 -0700213 super.showComboView(startWith, extras);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700214 }
215
216 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800217 public boolean showsWeb() {
218 return super.showsWeb() && mActiveTabsPage == null;
219 }
220
221 // menu handling callbacks
222
223 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800224 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800225 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800226 }
227
228 @Override
229 public void onContextMenuClosed(Menu menu, boolean inLoad) {
230 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800231 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800232 }
233 }
234
235 // action mode callbacks
236
237 @Override
238 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800239 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800240 }
241
Michael Kolba4183062011-01-16 10:43:21 -0800242 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700243 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700244 if (inLoad) {
245 if (mUseQuickControls) {
246 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700247 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700248 showTitleBar();
249 }
250 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700251 }
252
253 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700254 protected void setTitleGravity(int gravity) {
255 if (mUseQuickControls) {
256 FrameLayout.LayoutParams lp =
257 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
258 lp.gravity = gravity;
259 getTitleBar().setLayoutParams(lp);
260 } else {
261 super.setTitleGravity(gravity);
262 }
263 }
264
265 private void setUseQuickControls(boolean useQuickControls) {
266 mUseQuickControls = useQuickControls;
267 getTitleBar().setUseQuickControls(mUseQuickControls);
268 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700269 mPieControl = new PieControl(mActivity, mUiController, this);
270 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700271 WebView web = getWebView();
272 if (web != null) {
273 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700274 }
275 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700276 if (mPieControl != null) {
277 mPieControl.removeFromContainer(mContentView);
278 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700279 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700280 if (web != null) {
281 web.setEmbeddedTitleBar(mTitleBar);
282 }
283 setTitleGravity(Gravity.NO_GRAVITY);
284 }
285 }
286
287 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700288 protected void captureTab(final Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700289 if (tab == null) return;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700290 BrowserWebView web = (BrowserWebView) tab.getWebView();
291 if (web != null) {
292 tab.setScreenshot(web.capture());
Michael Kolbfdb70242011-03-24 09:41:11 -0700293 }
294 }
Michael Kolb4bd767d2011-05-27 11:33:55 -0700295
Michael Kolbf2055602011-04-09 17:20:03 -0700296 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700297 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700298 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700299 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700300 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700301 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700302 mCustomViewContainer.setVisibility(View.VISIBLE);
303 mCustomViewContainer.bringToFront();
304 }
305
Michael Kolb2814a362011-05-19 15:49:41 -0700306 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700307 if (mNavScreen == null) return;
Michael Kolb2814a362011-05-19 15:49:41 -0700308 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700309 mCustomViewContainer.removeView(mNavScreen);
310 mNavScreen = null;
311 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700312 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700313 // Show the content view.
314 mContentView.setVisibility(View.VISIBLE);
315 }
316
Michael Kolb66706532010-12-12 19:50:22 -0800317}