blob: fe131399d3cb20c73858dd671e2ba5da634651fc [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
Michael Kolbf2055602011-04-09 17:20:03 -070019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Michael Kolb66706532010-12-12 19:50:22 -080023import android.app.Activity;
Michael Kolb376b5412010-12-15 11:52:57 -080024import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080025import android.view.ActionMode;
26import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080027import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080028import android.view.Menu;
29import android.view.View;
Michael Kolb66706532010-12-12 19:50:22 -080030import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070031import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080032
33/**
34 * Ui for regular phone screen sizes
35 */
36public class PhoneUi extends BaseUi {
37
38 private static final String LOGTAG = "PhoneUi";
Michael Kolbf2055602011-04-09 17:20:03 -070039 private static final float NAV_TAB_SCALE = 0.75f;
Michael Kolb66706532010-12-12 19:50:22 -080040
Michael Kolb11d19782011-03-20 10:17:40 -070041 private TitleBarPhone mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080042 private ActiveTabsPage mActiveTabsPage;
Michael Kolbfdb70242011-03-24 09:41:11 -070043 private boolean mUseQuickControls;
44 private PieControl mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070045 private NavScreen mNavScreen;
Michael Kolb66706532010-12-12 19:50:22 -080046
47 boolean mExtendedMenuOpen;
48 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070049 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080050
51 /**
52 * @param browser
53 * @param controller
54 */
55 public PhoneUi(Activity browser, UiController controller) {
56 super(browser, controller);
Michael Kolb46f987e2011-04-05 10:39:10 -070057 mTitleBar = new TitleBarPhone(mActivity, mUiController, this,
58 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080059 // mTitleBar will be always be shown in the fully loaded mode on
60 // phone
61 mTitleBar.setProgress(100);
John Reck285ef042011-02-11 15:44:17 -080062 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070063 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck285ef042011-02-11 15:44:17 -080064 }
Michael Kolb66706532010-12-12 19:50:22 -080065
John Reck285ef042011-02-11 15:44:17 -080066 @Override
67 public void hideComboView() {
68 super.hideComboView();
69 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080070 }
71
Michael Kolb66706532010-12-12 19:50:22 -080072 // lifecycle
73
74 @Override
75 public void onPause() {
76 // FIXME: This removes the active tabs page and resets the menu to
77 // MAIN_MENU. A better solution might be to do this work in onNewIntent
78 // but then we would need to save it in onSaveInstanceState and restore
79 // it in onCreate/onRestoreInstanceState
80 if (mActiveTabsPage != null) {
81 mUiController.removeActiveTabsPage(true);
82 }
83 super.onPause();
84 }
85
86 @Override
87 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080088 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080089 }
90
91 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080092 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070093 if (mUseQuickControls) {
94 getTitleBar().setShowProgressOnly(false);
95 }
96 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080097 }
98
99 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800100 public boolean onBackKey() {
101 if (mActiveTabsPage != null) {
102 // if tab page is showing, hide it
103 mUiController.removeActiveTabsPage(true);
104 return true;
Michael Kolbf2055602011-04-09 17:20:03 -0700105 } else if (mNavScreen != null) {
106 mNavScreen.close();
107 return true;
Michael Kolb66706532010-12-12 19:50:22 -0800108 }
109 return super.onBackKey();
110 }
111
112 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700113 public boolean dispatchKey(int code, KeyEvent event) {
114 if (!isComboViewShowing()) {
115 switch (code) {
116 case KeyEvent.KEYCODE_MENU:
117 if (mNavScreen == null) {
118 showNavScreen();
119 return true;
120 } else {
121 mNavScreen.close();
122 return true;
123 }
124 }
125 }
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) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800136 hideTitleBar();
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);
Michael Kolbfdb70242011-03-24 09:41:11 -0700156 super.setActiveTab(tab, true);
157 setActiveTab(tab, true);
158 }
159
160 @Override
161 void setActiveTab(Tab tab, boolean needsAttaching) {
162 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800163 // TabControl.setCurrentTab has been called before this,
164 // so the tab is guaranteed to have a webview
165 if (view == null) {
166 Log.e(LOGTAG, "active tab with no webview detected");
167 return;
168 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700169 // Request focus on the top window.
170 if (mUseQuickControls) {
171 mPieControl.forceToTop(mContentView);
172 view.setScrollListener(null);
173 } else {
174 // check if title bar is already attached by animation
175 if (mTitleBar.getParent() == null) {
176 view.setEmbeddedTitleBar(mTitleBar);
177 }
178 }
Michael Kolb376b5412010-12-15 11:52:57 -0800179 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700180 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800181 } else {
182 revertVoiceTitleBar(tab);
183 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700184 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800185 tab.getTopWindow().requestFocus();
186 }
187
188 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800189 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800190 return mTitleBar;
191 }
192
193 // active tabs page
194
195 @Override
196 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700197 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800198 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
199 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800201 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
202 mActiveTabsPage.requestFocus();
203 }
204
205 /**
206 * Remove the active tabs page.
207 */
208 @Override
209 public void removeActiveTabsPage() {
210 mContentView.removeView(mActiveTabsPage);
211 mTitleBar.setVisibility(View.VISIBLE);
212 mActiveTabsPage = null;
213 }
214
215 @Override
216 public boolean showsWeb() {
217 return super.showsWeb() && mActiveTabsPage == null;
218 }
219
220 // menu handling callbacks
221
222 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800223 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800224 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800225 }
226
227 @Override
228 public void onContextMenuClosed(Menu menu, boolean inLoad) {
229 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800230 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800231 }
232 }
233
234 // action mode callbacks
235
236 @Override
237 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800238 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800239 }
240
Michael Kolba4183062011-01-16 10:43:21 -0800241 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700242 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700243 if (inLoad) {
244 if (mUseQuickControls) {
245 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700246 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700247 showTitleBar();
248 }
249 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700250 }
251
252 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700253 protected void setTitleGravity(int gravity) {
254 if (mUseQuickControls) {
255 FrameLayout.LayoutParams lp =
256 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
257 lp.gravity = gravity;
258 getTitleBar().setLayoutParams(lp);
259 } else {
260 super.setTitleGravity(gravity);
261 }
262 }
263
264 private void setUseQuickControls(boolean useQuickControls) {
265 mUseQuickControls = useQuickControls;
266 getTitleBar().setUseQuickControls(mUseQuickControls);
267 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700268 mPieControl = new PieControl(mActivity, mUiController, this);
269 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700270 WebView web = getWebView();
271 if (web != null) {
272 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700273 }
274 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700275 if (mPieControl != null) {
276 mPieControl.removeFromContainer(mContentView);
277 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700278 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700279 if (web != null) {
280 web.setEmbeddedTitleBar(mTitleBar);
281 }
282 setTitleGravity(Gravity.NO_GRAVITY);
283 }
284 }
285
286 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700287 protected void captureTab(final Tab tab) {
288 if (mUseQuickControls) {
289 super.captureTab(tab);
290 } else {
291 captureTab(tab,
292 mActivity.getWindowManager().getDefaultDisplay().getWidth(),
Michael Kolbf2055602011-04-09 17:20:03 -0700293 mActivity.getWindowManager().getDefaultDisplay().getHeight());
Michael Kolbfdb70242011-03-24 09:41:11 -0700294 }
295 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700296
Michael Kolbf2055602011-04-09 17:20:03 -0700297 void showNavScreen() {
Michael Kolb08a687a2011-04-22 16:13:02 -0700298 if (mAnimating) return;
299 mAnimating = true;
Michael Kolbf2055602011-04-09 17:20:03 -0700300 mNavScreen = new NavScreen(mActivity, mUiController, this);
301 WebView web = getWebView();
302 if (web != null) {
303 int w = web.getWidth();
304 int h = web.getHeight();
305 mNavScreen.setTabDimensions((int) (w * NAV_TAB_SCALE),
306 (int) (h * NAV_TAB_SCALE));
307 }
308 // Add the custom view to its container.
309 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_GRAVITY_CENTER);
Michael Kolb08a687a2011-04-22 16:13:02 -0700310 mContentView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700311 ObjectAnimator animx = ObjectAnimator.ofFloat(mContentView,
312 "scaleX", 1.0f, 0.85f);
313 ObjectAnimator animy = ObjectAnimator.ofFloat(mContentView,
314 "scaleY", 1.0f, 0.85f);
315 AnimatorSet anims = new AnimatorSet();
316 anims.setDuration(200);
317 anims.addListener(new AnimatorListener() {
318
319 @Override
320 public void onAnimationCancel(Animator animation) {
Michael Kolbf2055602011-04-09 17:20:03 -0700321 }
322
323 @Override
324 public void onAnimationEnd(Animator animation) {
325 finishShowNavScreen();
326 }
327
328 @Override
329 public void onAnimationRepeat(Animator animation) {
330 }
331
332 @Override
333 public void onAnimationStart(Animator animation) {
334 }
335 });
336 anims.playTogether(animx, animy);
337 anims.start();
338 }
339
340 private void finishShowNavScreen() {
341 // Hide the content view.
Michael Kolb08a687a2011-04-22 16:13:02 -0700342 mContentView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700343 mContentView.setVisibility(View.GONE);
344 mContentView.setScaleX(1.0f);
345 mContentView.setScaleY(1.0f);
Michael Kolb6cd8f6b2011-04-25 15:16:58 -0700346 BrowserWebView web = (BrowserWebView) getWebView();
347 if (web != null) {
348 mActiveTab.setScreenshot(web.capture());
349 }
Michael Kolbf2055602011-04-09 17:20:03 -0700350 // Finally show the custom view container.
351 mCustomViewContainer.setVisibility(View.VISIBLE);
352 mCustomViewContainer.bringToFront();
Michael Kolb08a687a2011-04-22 16:13:02 -0700353 mAnimating = false;
Michael Kolbf2055602011-04-09 17:20:03 -0700354 }
355
356 void hideNavScreen(boolean animateToPage) {
Michael Kolb08a687a2011-04-22 16:13:02 -0700357 if (mAnimating || mNavScreen == null) return;
358 mAnimating = true;
359 mNavScreen.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700360 if (animateToPage) {
361 ObjectAnimator animx = ObjectAnimator.ofFloat(mNavScreen, "scaleX",
362 1.0f, 1.2f);
363 ObjectAnimator animy = ObjectAnimator.ofFloat(mNavScreen, "scaleY",
364 1.0f, 1.2f);
365 AnimatorSet anims = new AnimatorSet();
366 anims.setDuration(200);
367 anims.addListener(new AnimatorListener() {
368
369 @Override
370 public void onAnimationCancel(Animator animation) {
Michael Kolbf2055602011-04-09 17:20:03 -0700371 }
372
373 @Override
374 public void onAnimationEnd(Animator animation) {
375 finishHideNavScreen();
376 }
377
378 @Override
379 public void onAnimationRepeat(Animator animation) {
380 }
381
382 @Override
383 public void onAnimationStart(Animator animation) {
384 }
385 });
386 anims.playTogether(animx, animy);
387 anims.start();
388 } else {
389 finishHideNavScreen();
390 }
391
392 }
393
394 private void finishHideNavScreen() {
395 // Hide the custom view.
396 mNavScreen.setVisibility(View.GONE);
Michael Kolb08a687a2011-04-22 16:13:02 -0700397 mNavScreen.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700398 // Remove the custom view from its container.
399 mCustomViewContainer.removeView(mNavScreen);
400 mNavScreen = null;
401 mCustomViewContainer.setVisibility(View.GONE);
402 // Show the content view.
403 mContentView.setVisibility(View.VISIBLE);
Michael Kolb08a687a2011-04-22 16:13:02 -0700404 mAnimating = false;
Michael Kolbf2055602011-04-09 17:20:03 -0700405 }
406
Michael Kolb66706532010-12-12 19:50:22 -0800407}