blob: e838cd4664717a66ed0fb46a164414aac2687e94 [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) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700288 if (tab == null) return;
Michael Kolbfdb70242011-03-24 09:41:11 -0700289 if (mUseQuickControls) {
290 super.captureTab(tab);
291 } else {
Michael Kolba4261fd2011-05-05 11:27:37 -0700292 BrowserWebView web = (BrowserWebView) tab.getWebView();
293 if (web != null) {
294 tab.setScreenshot(web.capture());
295 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700296 }
297 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700298
Michael Kolbf2055602011-04-09 17:20:03 -0700299 void showNavScreen() {
Michael Kolb08a687a2011-04-22 16:13:02 -0700300 if (mAnimating) return;
301 mAnimating = true;
Michael Kolbf2055602011-04-09 17:20:03 -0700302 mNavScreen = new NavScreen(mActivity, mUiController, this);
John Reckadc921f2011-04-27 10:11:03 -0700303 float yoffset = 0;
Michael Kolbf2055602011-04-09 17:20:03 -0700304 WebView web = getWebView();
305 if (web != null) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700306 yoffset = mNavScreen.getToolbarHeight() -
307 web.getVisibleTitleHeight();
Michael Kolbf2055602011-04-09 17:20:03 -0700308 }
309 // Add the custom view to its container.
310 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_GRAVITY_CENTER);
Michael Kolb08a687a2011-04-22 16:13:02 -0700311 mContentView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700312 ObjectAnimator animx = ObjectAnimator.ofFloat(mContentView,
John Reckadc921f2011-04-27 10:11:03 -0700313 "scaleX", 1.0f, NAV_TAB_SCALE);
Michael Kolbf2055602011-04-09 17:20:03 -0700314 ObjectAnimator animy = ObjectAnimator.ofFloat(mContentView,
John Reckadc921f2011-04-27 10:11:03 -0700315 "scaleY", 1.0f, NAV_TAB_SCALE);
316 ObjectAnimator translatey = ObjectAnimator.ofFloat(mContentView,
317 "translationY", 0, yoffset * NAV_TAB_SCALE);
Michael Kolbf2055602011-04-09 17:20:03 -0700318 AnimatorSet anims = new AnimatorSet();
319 anims.setDuration(200);
320 anims.addListener(new AnimatorListener() {
321
322 @Override
323 public void onAnimationCancel(Animator animation) {
Michael Kolbf2055602011-04-09 17:20:03 -0700324 }
325
326 @Override
327 public void onAnimationEnd(Animator animation) {
328 finishShowNavScreen();
329 }
330
331 @Override
332 public void onAnimationRepeat(Animator animation) {
333 }
334
335 @Override
336 public void onAnimationStart(Animator animation) {
337 }
338 });
John Reckadc921f2011-04-27 10:11:03 -0700339 anims.playTogether(animx, animy, translatey);
Michael Kolbf2055602011-04-09 17:20:03 -0700340 anims.start();
341 }
342
343 private void finishShowNavScreen() {
344 // Hide the content view.
Michael Kolb08a687a2011-04-22 16:13:02 -0700345 mContentView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700346 mContentView.setVisibility(View.GONE);
347 mContentView.setScaleX(1.0f);
348 mContentView.setScaleY(1.0f);
John Reckadc921f2011-04-27 10:11:03 -0700349 mContentView.setTranslationY(0);
Michael Kolb6cd8f6b2011-04-25 15:16:58 -0700350 BrowserWebView web = (BrowserWebView) getWebView();
351 if (web != null) {
352 mActiveTab.setScreenshot(web.capture());
353 }
Michael Kolbf2055602011-04-09 17:20:03 -0700354 // Finally show the custom view container.
355 mCustomViewContainer.setVisibility(View.VISIBLE);
356 mCustomViewContainer.bringToFront();
Michael Kolb08a687a2011-04-22 16:13:02 -0700357 mAnimating = false;
Michael Kolbf2055602011-04-09 17:20:03 -0700358 }
359
360 void hideNavScreen(boolean animateToPage) {
Michael Kolb08a687a2011-04-22 16:13:02 -0700361 if (mAnimating || mNavScreen == null) return;
362 mAnimating = true;
363 mNavScreen.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700364 if (animateToPage) {
John Reckadc921f2011-04-27 10:11:03 -0700365 float yoffset = 0;
366 WebView web = mNavScreen.getSelectedTab().getWebView();
367 if (web != null) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700368 yoffset = mNavScreen.getToolbarHeight() -
369 web.getVisibleTitleHeight();
John Reckadc921f2011-04-27 10:11:03 -0700370 }
Michael Kolbf2055602011-04-09 17:20:03 -0700371 ObjectAnimator animx = ObjectAnimator.ofFloat(mNavScreen, "scaleX",
John Reckadc921f2011-04-27 10:11:03 -0700372 1.0f, 1 / NAV_TAB_SCALE);
Michael Kolbf2055602011-04-09 17:20:03 -0700373 ObjectAnimator animy = ObjectAnimator.ofFloat(mNavScreen, "scaleY",
John Reckadc921f2011-04-27 10:11:03 -0700374 1.0f, 1 / NAV_TAB_SCALE);
375 ObjectAnimator translatey = ObjectAnimator.ofFloat(mNavScreen,
376 "translationY", 0, -yoffset);
Michael Kolbf2055602011-04-09 17:20:03 -0700377 AnimatorSet anims = new AnimatorSet();
378 anims.setDuration(200);
379 anims.addListener(new AnimatorListener() {
380
381 @Override
382 public void onAnimationCancel(Animator animation) {
Michael Kolbf2055602011-04-09 17:20:03 -0700383 }
384
385 @Override
386 public void onAnimationEnd(Animator animation) {
387 finishHideNavScreen();
388 }
389
390 @Override
391 public void onAnimationRepeat(Animator animation) {
392 }
393
394 @Override
395 public void onAnimationStart(Animator animation) {
396 }
397 });
John Reckadc921f2011-04-27 10:11:03 -0700398 anims.playTogether(animx, animy, translatey);
Michael Kolbf2055602011-04-09 17:20:03 -0700399 anims.start();
400 } else {
401 finishHideNavScreen();
402 }
403
404 }
405
406 private void finishHideNavScreen() {
407 // Hide the custom view.
408 mNavScreen.setVisibility(View.GONE);
Michael Kolb08a687a2011-04-22 16:13:02 -0700409 mNavScreen.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Kolbf2055602011-04-09 17:20:03 -0700410 // Remove the custom view from its container.
411 mCustomViewContainer.removeView(mNavScreen);
412 mNavScreen = null;
413 mCustomViewContainer.setVisibility(View.GONE);
414 // Show the content view.
415 mContentView.setVisibility(View.VISIBLE);
Michael Kolb08a687a2011-04-22 16:13:02 -0700416 mAnimating = false;
Michael Kolbf2055602011-04-09 17:20:03 -0700417 }
418
Michael Kolb66706532010-12-12 19:50:22 -0800419}