blob: b2435a3b77d1d7ab42c6f54a75d4714ab6fef9a0 [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
287 public boolean onPrepareOptionsMenu(Menu menu) {
288 if (mUseQuickControls) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700289 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolbfdb70242011-03-24 09:41:11 -0700290 mPieControl.onMenuOpened(menu);
291 return false;
Michael Kolbfdb70242011-03-24 09:41:11 -0700292 }
Michael Kolbfedb4922011-04-20 16:45:33 -0700293 return true;
Michael Kolbfdb70242011-03-24 09:41:11 -0700294 }
295
296 @Override
297 protected void captureTab(final Tab tab) {
298 if (mUseQuickControls) {
299 super.captureTab(tab);
300 } else {
301 captureTab(tab,
302 mActivity.getWindowManager().getDefaultDisplay().getWidth(),
Michael Kolbf2055602011-04-09 17:20:03 -0700303 mActivity.getWindowManager().getDefaultDisplay().getHeight());
Michael Kolbfdb70242011-03-24 09:41:11 -0700304 }
305 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700306
Michael Kolbf2055602011-04-09 17:20:03 -0700307 void showNavScreen() {
Michael Kolb08a687a2011-04-22 16:13:02 -0700308 if (mAnimating) return;
309 mAnimating = true;
Michael Kolbf2055602011-04-09 17:20:03 -0700310 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolb08a687a2011-04-22 16:13:02 -0700311 mNavScreen.startTask(new Runnable() {
312 public void run() {
313 BrowserWebView web = (BrowserWebView) getWebView();
314 if (web != null) {
315 mActiveTab.setScreenshot(web.capture());
316 }
317 mNavScreen.finishTask();
318 }
319 });
Michael Kolbf2055602011-04-09 17:20:03 -0700320 WebView web = getWebView();
321 if (web != null) {
322 int w = web.getWidth();
323 int h = web.getHeight();
324 mNavScreen.setTabDimensions((int) (w * NAV_TAB_SCALE),
325 (int) (h * NAV_TAB_SCALE));
326 }
327 // Add the custom view to its container.
328 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_GRAVITY_CENTER);
Michael Kolb08a687a2011-04-22 16:13:02 -0700329 mContentView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700330 ObjectAnimator animx = ObjectAnimator.ofFloat(mContentView,
331 "scaleX", 1.0f, 0.85f);
332 ObjectAnimator animy = ObjectAnimator.ofFloat(mContentView,
333 "scaleY", 1.0f, 0.85f);
334 AnimatorSet anims = new AnimatorSet();
335 anims.setDuration(200);
336 anims.addListener(new AnimatorListener() {
337
338 @Override
339 public void onAnimationCancel(Animator animation) {
Michael Kolbf2055602011-04-09 17:20:03 -0700340 }
341
342 @Override
343 public void onAnimationEnd(Animator animation) {
344 finishShowNavScreen();
345 }
346
347 @Override
348 public void onAnimationRepeat(Animator animation) {
349 }
350
351 @Override
352 public void onAnimationStart(Animator animation) {
353 }
354 });
355 anims.playTogether(animx, animy);
356 anims.start();
357 }
358
359 private void finishShowNavScreen() {
360 // Hide the content view.
Michael Kolb08a687a2011-04-22 16:13:02 -0700361 mContentView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700362 mContentView.setVisibility(View.GONE);
363 mContentView.setScaleX(1.0f);
364 mContentView.setScaleY(1.0f);
Michael Kolb08a687a2011-04-22 16:13:02 -0700365 mNavScreen.waitForTask();
Michael Kolbf2055602011-04-09 17:20:03 -0700366 // Finally show the custom view container.
367 mCustomViewContainer.setVisibility(View.VISIBLE);
368 mCustomViewContainer.bringToFront();
Michael Kolb08a687a2011-04-22 16:13:02 -0700369 mAnimating = false;
Michael Kolbf2055602011-04-09 17:20:03 -0700370 }
371
372 void hideNavScreen(boolean animateToPage) {
Michael Kolb08a687a2011-04-22 16:13:02 -0700373 if (mAnimating || mNavScreen == null) return;
374 mAnimating = true;
375 mNavScreen.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700376 if (animateToPage) {
377 ObjectAnimator animx = ObjectAnimator.ofFloat(mNavScreen, "scaleX",
378 1.0f, 1.2f);
379 ObjectAnimator animy = ObjectAnimator.ofFloat(mNavScreen, "scaleY",
380 1.0f, 1.2f);
381 AnimatorSet anims = new AnimatorSet();
382 anims.setDuration(200);
383 anims.addListener(new AnimatorListener() {
384
385 @Override
386 public void onAnimationCancel(Animator animation) {
Michael Kolbf2055602011-04-09 17:20:03 -0700387 }
388
389 @Override
390 public void onAnimationEnd(Animator animation) {
391 finishHideNavScreen();
392 }
393
394 @Override
395 public void onAnimationRepeat(Animator animation) {
396 }
397
398 @Override
399 public void onAnimationStart(Animator animation) {
400 }
401 });
402 anims.playTogether(animx, animy);
403 anims.start();
404 } else {
405 finishHideNavScreen();
406 }
407
408 }
409
410 private void finishHideNavScreen() {
411 // Hide the custom view.
412 mNavScreen.setVisibility(View.GONE);
Michael Kolb08a687a2011-04-22 16:13:02 -0700413 mNavScreen.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700414 // Remove the custom view from its container.
415 mCustomViewContainer.removeView(mNavScreen);
416 mNavScreen = null;
417 mCustomViewContainer.setVisibility(View.GONE);
418 // Show the content view.
419 mContentView.setVisibility(View.VISIBLE);
Michael Kolb08a687a2011-04-22 16:13:02 -0700420 mAnimating = false;
Michael Kolbf2055602011-04-09 17:20:03 -0700421 }
422
Michael Kolb66706532010-12-12 19:50:22 -0800423}