blob: f94a8abcdbb8df5b723106f61d29c97f25c55f01 [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";
Michael Kolbf2055602011-04-09 17:20:03 -070036 private static final float NAV_TAB_SCALE = 0.75f;
Michael Kolb66706532010-12-12 19:50:22 -080037
Michael Kolb11d19782011-03-20 10:17:40 -070038 private TitleBarPhone mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080039 private ActiveTabsPage mActiveTabsPage;
Michael Kolbfdb70242011-03-24 09:41:11 -070040 private boolean mUseQuickControls;
41 private PieControl mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070042 private NavScreen mNavScreen;
Michael Kolb66706532010-12-12 19:50:22 -080043
44 boolean mExtendedMenuOpen;
45 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070046 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080047
48 /**
49 * @param browser
50 * @param controller
51 */
52 public PhoneUi(Activity browser, UiController controller) {
53 super(browser, controller);
Michael Kolb46f987e2011-04-05 10:39:10 -070054 mTitleBar = new TitleBarPhone(mActivity, mUiController, this,
55 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080056 // mTitleBar will be always be shown in the fully loaded mode on
57 // phone
58 mTitleBar.setProgress(100);
John Reck285ef042011-02-11 15:44:17 -080059 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070060 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck285ef042011-02-11 15:44:17 -080061 }
Michael Kolb66706532010-12-12 19:50:22 -080062
John Reck285ef042011-02-11 15:44:17 -080063 @Override
64 public void hideComboView() {
65 super.hideComboView();
66 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080067 }
68
Michael Kolb66706532010-12-12 19:50:22 -080069 // lifecycle
70
71 @Override
72 public void onPause() {
73 // FIXME: This removes the active tabs page and resets the menu to
74 // MAIN_MENU. A better solution might be to do this work in onNewIntent
75 // but then we would need to save it in onSaveInstanceState and restore
76 // it in onCreate/onRestoreInstanceState
77 if (mActiveTabsPage != null) {
78 mUiController.removeActiveTabsPage(true);
79 }
80 super.onPause();
81 }
82
83 @Override
84 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080085 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080086 }
87
88 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080089 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070090 if (mUseQuickControls) {
91 getTitleBar().setShowProgressOnly(false);
92 }
93 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080094 }
95
96 @Override
Michael Kolb66706532010-12-12 19:50:22 -080097 public boolean onBackKey() {
98 if (mActiveTabsPage != null) {
99 // if tab page is showing, hide it
100 mUiController.removeActiveTabsPage(true);
101 return true;
Michael Kolbf2055602011-04-09 17:20:03 -0700102 } else if (mNavScreen != null) {
103 mNavScreen.close();
104 return true;
Michael Kolb66706532010-12-12 19:50:22 -0800105 }
106 return super.onBackKey();
107 }
108
109 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700110 public boolean onMenuKey() {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700111 if (!isComboViewShowing()) {
112 if (mNavScreen == null) {
113 showNavScreen();
114 } else {
115 mNavScreen.close();
116 }
117 return true;
Michael Kolb2814a362011-05-19 15:49:41 -0700118 } else {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700119 return false;
Michael Kolb2814a362011-05-19 15:49:41 -0700120 }
Michael Kolb2814a362011-05-19 15:49:41 -0700121 }
122
123 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700124 public boolean dispatchKey(int code, KeyEvent event) {
125 if (!isComboViewShowing()) {
Michael Kolbf2055602011-04-09 17:20:03 -0700126 }
127 return false;
128 }
129
130 @Override
John Reck30c714c2010-12-16 17:30:34 -0800131 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800132 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800133 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800134 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800135 if (progress == 100) {
136 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800137 hideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700138 if (mUseQuickControls) {
139 mTitleBar.setShowProgressOnly(false);
140 }
Michael Kolb66706532010-12-12 19:50:22 -0800141 }
142 } else {
143 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700144 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
145 mTitleBar.setShowProgressOnly(true);
146 setTitleGravity(Gravity.TOP);
147 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800148 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800149 }
150 }
151 }
152 }
153
154 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700155 public void setActiveTab(final Tab tab) {
John Reck88a42b72011-03-21 16:30:12 -0700156 captureTab(mActiveTab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700157 super.setActiveTab(tab, true);
158 setActiveTab(tab, true);
159 }
160
161 @Override
162 void setActiveTab(Tab tab, boolean needsAttaching) {
163 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800164 // TabControl.setCurrentTab has been called before this,
165 // so the tab is guaranteed to have a webview
166 if (view == null) {
167 Log.e(LOGTAG, "active tab with no webview detected");
168 return;
169 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700170 // Request focus on the top window.
171 if (mUseQuickControls) {
172 mPieControl.forceToTop(mContentView);
173 view.setScrollListener(null);
174 } else {
175 // check if title bar is already attached by animation
176 if (mTitleBar.getParent() == null) {
177 view.setEmbeddedTitleBar(mTitleBar);
178 }
179 }
Michael Kolb376b5412010-12-15 11:52:57 -0800180 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700181 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800182 } else {
183 revertVoiceTitleBar(tab);
184 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700185 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800186 tab.getTopWindow().requestFocus();
187 }
188
189 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800190 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800191 return mTitleBar;
192 }
193
194 // active tabs page
195
196 @Override
197 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700198 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800199 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
200 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800201 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800202 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
203 mActiveTabsPage.requestFocus();
204 }
205
206 /**
207 * Remove the active tabs page.
208 */
209 @Override
210 public void removeActiveTabsPage() {
211 mContentView.removeView(mActiveTabsPage);
212 mTitleBar.setVisibility(View.VISIBLE);
213 mActiveTabsPage = null;
214 }
215
216 @Override
Michael Kolb4bd767d2011-05-27 11:33:55 -0700217 public void showComboView(boolean startWithHistory, Bundle extras) {
218 if (mNavScreen != null) {
219 hideNavScreen(false);
220 }
221 super.showComboView(startWithHistory, extras);
222 }
223
224 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800225 public boolean showsWeb() {
226 return super.showsWeb() && mActiveTabsPage == null;
227 }
228
229 // menu handling callbacks
230
231 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800232 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800233 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800234 }
235
236 @Override
237 public void onContextMenuClosed(Menu menu, boolean inLoad) {
238 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800239 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800240 }
241 }
242
243 // action mode callbacks
244
245 @Override
246 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800247 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800248 }
249
Michael Kolba4183062011-01-16 10:43:21 -0800250 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700251 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700252 if (inLoad) {
253 if (mUseQuickControls) {
254 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700255 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700256 showTitleBar();
257 }
258 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700259 }
260
261 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700262 protected void setTitleGravity(int gravity) {
263 if (mUseQuickControls) {
264 FrameLayout.LayoutParams lp =
265 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
266 lp.gravity = gravity;
267 getTitleBar().setLayoutParams(lp);
268 } else {
269 super.setTitleGravity(gravity);
270 }
271 }
272
273 private void setUseQuickControls(boolean useQuickControls) {
274 mUseQuickControls = useQuickControls;
275 getTitleBar().setUseQuickControls(mUseQuickControls);
276 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700277 mPieControl = new PieControl(mActivity, mUiController, this);
278 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700279 WebView web = getWebView();
280 if (web != null) {
281 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700282 }
283 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700284 if (mPieControl != null) {
285 mPieControl.removeFromContainer(mContentView);
286 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700287 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700288 if (web != null) {
289 web.setEmbeddedTitleBar(mTitleBar);
290 }
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) {
315 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700316 mCustomViewContainer.removeView(mNavScreen);
317 mNavScreen = null;
318 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700319 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700320 // Show the content view.
321 mContentView.setVisibility(View.VISIBLE);
322 }
323
Michael Kolb66706532010-12-12 19:50:22 -0800324}