blob: ce993fddab6f606c988c4c058caff7c28da8ed63 [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 Kolb2814a362011-05-19 15:49:41 -0700170 view.setNavMode(false);
Michael Kolbfdb70242011-03-24 09:41:11 -0700171 // Request focus on the top window.
172 if (mUseQuickControls) {
173 mPieControl.forceToTop(mContentView);
174 view.setScrollListener(null);
175 } else {
176 // check if title bar is already attached by animation
177 if (mTitleBar.getParent() == null) {
178 view.setEmbeddedTitleBar(mTitleBar);
179 }
180 }
Michael Kolb376b5412010-12-15 11:52:57 -0800181 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700182 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800183 } else {
184 revertVoiceTitleBar(tab);
185 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700186 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800187 tab.getTopWindow().requestFocus();
188 }
189
190 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800191 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800192 return mTitleBar;
193 }
194
195 // active tabs page
196
197 @Override
198 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700199 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800200 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
201 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800202 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800203 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
204 mActiveTabsPage.requestFocus();
205 }
206
207 /**
208 * Remove the active tabs page.
209 */
210 @Override
211 public void removeActiveTabsPage() {
212 mContentView.removeView(mActiveTabsPage);
213 mTitleBar.setVisibility(View.VISIBLE);
214 mActiveTabsPage = null;
215 }
216
217 @Override
Michael Kolb4bd767d2011-05-27 11:33:55 -0700218 public void showComboView(boolean startWithHistory, Bundle extras) {
219 if (mNavScreen != null) {
220 hideNavScreen(false);
221 }
222 super.showComboView(startWithHistory, extras);
223 }
224
225 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800226 public boolean showsWeb() {
227 return super.showsWeb() && mActiveTabsPage == null;
228 }
229
230 // menu handling callbacks
231
232 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800233 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800234 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800235 }
236
237 @Override
238 public void onContextMenuClosed(Menu menu, boolean inLoad) {
239 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800240 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242 }
243
244 // action mode callbacks
245
246 @Override
247 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800248 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800249 }
250
Michael Kolba4183062011-01-16 10:43:21 -0800251 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700252 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700253 if (inLoad) {
254 if (mUseQuickControls) {
255 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700256 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700257 showTitleBar();
258 }
259 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700260 }
261
262 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700263 protected void setTitleGravity(int gravity) {
264 if (mUseQuickControls) {
265 FrameLayout.LayoutParams lp =
266 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
267 lp.gravity = gravity;
268 getTitleBar().setLayoutParams(lp);
269 } else {
270 super.setTitleGravity(gravity);
271 }
272 }
273
274 private void setUseQuickControls(boolean useQuickControls) {
275 mUseQuickControls = useQuickControls;
276 getTitleBar().setUseQuickControls(mUseQuickControls);
277 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700278 mPieControl = new PieControl(mActivity, mUiController, this);
279 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700280 WebView web = getWebView();
281 if (web != null) {
282 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700283 }
284 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700285 if (mPieControl != null) {
286 mPieControl.removeFromContainer(mContentView);
287 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700288 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700289 if (web != null) {
290 web.setEmbeddedTitleBar(mTitleBar);
291 }
292 setTitleGravity(Gravity.NO_GRAVITY);
293 }
294 }
295
296 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700297 protected void captureTab(final Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700298 if (tab == null) return;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700299 BrowserWebView web = (BrowserWebView) tab.getWebView();
300 if (web != null) {
301 tab.setScreenshot(web.capture());
Michael Kolbfdb70242011-03-24 09:41:11 -0700302 }
303 }
Michael Kolb4bd767d2011-05-27 11:33:55 -0700304
Michael Kolbf2055602011-04-09 17:20:03 -0700305 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700306 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700307 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700308 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700309 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700310 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700311 mCustomViewContainer.setVisibility(View.VISIBLE);
312 mCustomViewContainer.bringToFront();
313 }
314
Michael Kolb2814a362011-05-19 15:49:41 -0700315 void hideNavScreen(boolean animate) {
316 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
Michael Kolb66706532010-12-12 19:50:22 -0800325}