blob: 80fdcfa6ea9782bd79e4c89e86f822f8d45bb20a [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 Kolb376b5412010-12-15 11:52:57 -080020import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080021import android.view.ActionMode;
22import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080023import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080024import android.view.Menu;
25import android.view.View;
Michael Kolb66706532010-12-12 19:50:22 -080026import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070027import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080028
29/**
30 * Ui for regular phone screen sizes
31 */
32public class PhoneUi extends BaseUi {
33
34 private static final String LOGTAG = "PhoneUi";
Michael Kolbf2055602011-04-09 17:20:03 -070035 private static final float NAV_TAB_SCALE = 0.75f;
Michael Kolb66706532010-12-12 19:50:22 -080036
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() {
110 if (mNavScreen == null) {
111 showNavScreen();
112 } else {
113 mNavScreen.close();
114 }
115 return true;
116 }
117
118 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700119 public boolean dispatchKey(int code, KeyEvent event) {
120 if (!isComboViewShowing()) {
Michael Kolbf2055602011-04-09 17:20:03 -0700121 }
122 return false;
123 }
124
125 @Override
John Reck30c714c2010-12-16 17:30:34 -0800126 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800127 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800128 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800129 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800130 if (progress == 100) {
131 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800132 hideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700133 if (mUseQuickControls) {
134 mTitleBar.setShowProgressOnly(false);
135 }
Michael Kolb66706532010-12-12 19:50:22 -0800136 }
137 } else {
138 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700139 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
140 mTitleBar.setShowProgressOnly(true);
141 setTitleGravity(Gravity.TOP);
142 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800143 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800144 }
145 }
146 }
147 }
148
149 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700150 public void setActiveTab(final Tab tab) {
John Reck88a42b72011-03-21 16:30:12 -0700151 captureTab(mActiveTab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700152 super.setActiveTab(tab, true);
153 setActiveTab(tab, true);
154 }
155
156 @Override
157 void setActiveTab(Tab tab, boolean needsAttaching) {
158 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800159 // TabControl.setCurrentTab has been called before this,
160 // so the tab is guaranteed to have a webview
161 if (view == null) {
162 Log.e(LOGTAG, "active tab with no webview detected");
163 return;
164 }
Michael Kolb2814a362011-05-19 15:49:41 -0700165 view.setNavMode(false);
Michael Kolbfdb70242011-03-24 09:41:11 -0700166 // Request focus on the top window.
167 if (mUseQuickControls) {
168 mPieControl.forceToTop(mContentView);
169 view.setScrollListener(null);
170 } else {
171 // check if title bar is already attached by animation
172 if (mTitleBar.getParent() == null) {
173 view.setEmbeddedTitleBar(mTitleBar);
174 }
175 }
Michael Kolb376b5412010-12-15 11:52:57 -0800176 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700177 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800178 } else {
179 revertVoiceTitleBar(tab);
180 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700181 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800182 tab.getTopWindow().requestFocus();
183 }
184
185 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800186 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800187 return mTitleBar;
188 }
189
190 // active tabs page
191
192 @Override
193 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700194 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800195 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
196 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800197 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800198 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
199 mActiveTabsPage.requestFocus();
200 }
201
202 /**
203 * Remove the active tabs page.
204 */
205 @Override
206 public void removeActiveTabsPage() {
207 mContentView.removeView(mActiveTabsPage);
208 mTitleBar.setVisibility(View.VISIBLE);
209 mActiveTabsPage = null;
210 }
211
212 @Override
213 public boolean showsWeb() {
214 return super.showsWeb() && mActiveTabsPage == null;
215 }
216
217 // menu handling callbacks
218
219 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800220 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800221 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800222 }
223
224 @Override
225 public void onContextMenuClosed(Menu menu, boolean inLoad) {
226 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800227 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800228 }
229 }
230
231 // action mode callbacks
232
233 @Override
234 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
Michael Kolba4183062011-01-16 10:43:21 -0800238 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700239 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700240 if (inLoad) {
241 if (mUseQuickControls) {
242 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700243 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700244 showTitleBar();
245 }
246 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700247 }
248
249 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700250 protected void setTitleGravity(int gravity) {
251 if (mUseQuickControls) {
252 FrameLayout.LayoutParams lp =
253 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
254 lp.gravity = gravity;
255 getTitleBar().setLayoutParams(lp);
256 } else {
257 super.setTitleGravity(gravity);
258 }
259 }
260
261 private void setUseQuickControls(boolean useQuickControls) {
262 mUseQuickControls = useQuickControls;
263 getTitleBar().setUseQuickControls(mUseQuickControls);
264 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700265 mPieControl = new PieControl(mActivity, mUiController, this);
266 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700267 WebView web = getWebView();
268 if (web != null) {
269 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700270 }
271 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700272 if (mPieControl != null) {
273 mPieControl.removeFromContainer(mContentView);
274 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700275 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700276 if (web != null) {
277 web.setEmbeddedTitleBar(mTitleBar);
278 }
279 setTitleGravity(Gravity.NO_GRAVITY);
280 }
281 }
282
283 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700284 protected void captureTab(final Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700285 if (tab == null) return;
Michael Kolbfdb70242011-03-24 09:41:11 -0700286 if (mUseQuickControls) {
287 super.captureTab(tab);
288 } else {
Michael Kolba4261fd2011-05-05 11:27:37 -0700289 BrowserWebView web = (BrowserWebView) tab.getWebView();
290 if (web != null) {
291 tab.setScreenshot(web.capture());
292 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700293 }
294 }
Michael Kolbf2055602011-04-09 17:20:03 -0700295 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700296 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700297 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700298 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700299 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700300 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700301 mCustomViewContainer.setVisibility(View.VISIBLE);
302 mCustomViewContainer.bringToFront();
303 }
304
Michael Kolb2814a362011-05-19 15:49:41 -0700305 void hideNavScreen(boolean animate) {
306 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700307 mCustomViewContainer.removeView(mNavScreen);
308 mNavScreen = null;
309 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700310 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700311 // Show the content view.
312 mContentView.setVisibility(View.VISIBLE);
313 }
314
Michael Kolb66706532010-12-12 19:50:22 -0800315}