blob: f2ace49edb5caeba433cde13200e157ee8bb8120 [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;
49
50 /**
51 * @param browser
52 * @param controller
53 */
54 public PhoneUi(Activity browser, UiController controller) {
55 super(browser, controller);
Michael Kolb46f987e2011-04-05 10:39:10 -070056 mTitleBar = new TitleBarPhone(mActivity, mUiController, this,
57 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080058 // mTitleBar will be always be shown in the fully loaded mode on
59 // phone
60 mTitleBar.setProgress(100);
John Reck285ef042011-02-11 15:44:17 -080061 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070062 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck285ef042011-02-11 15:44:17 -080063 }
Michael Kolb66706532010-12-12 19:50:22 -080064
John Reck285ef042011-02-11 15:44:17 -080065 @Override
66 public void hideComboView() {
67 super.hideComboView();
68 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080069 }
70
Michael Kolb66706532010-12-12 19:50:22 -080071 // lifecycle
72
73 @Override
74 public void onPause() {
75 // FIXME: This removes the active tabs page and resets the menu to
76 // MAIN_MENU. A better solution might be to do this work in onNewIntent
77 // but then we would need to save it in onSaveInstanceState and restore
78 // it in onCreate/onRestoreInstanceState
79 if (mActiveTabsPage != null) {
80 mUiController.removeActiveTabsPage(true);
81 }
82 super.onPause();
83 }
84
85 @Override
86 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080087 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080088 }
89
90 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080091 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070092 if (mUseQuickControls) {
93 getTitleBar().setShowProgressOnly(false);
94 }
95 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080096 }
97
98 @Override
Michael Kolb66706532010-12-12 19:50:22 -080099 public boolean onBackKey() {
100 if (mActiveTabsPage != null) {
101 // if tab page is showing, hide it
102 mUiController.removeActiveTabsPage(true);
103 return true;
Michael Kolbf2055602011-04-09 17:20:03 -0700104 } else if (mNavScreen != null) {
105 mNavScreen.close();
106 return true;
Michael Kolb66706532010-12-12 19:50:22 -0800107 }
108 return super.onBackKey();
109 }
110
111 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700112 public boolean dispatchKey(int code, KeyEvent event) {
113 if (!isComboViewShowing()) {
114 switch (code) {
115 case KeyEvent.KEYCODE_MENU:
116 if (mNavScreen == null) {
117 showNavScreen();
118 return true;
119 } else {
120 mNavScreen.close();
121 return true;
122 }
123 }
124 }
125 return false;
126 }
127
128 @Override
John Reck30c714c2010-12-16 17:30:34 -0800129 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800130 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800131 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800132 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800133 if (progress == 100) {
134 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800135 hideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700136 if (mUseQuickControls) {
137 mTitleBar.setShowProgressOnly(false);
138 }
Michael Kolb66706532010-12-12 19:50:22 -0800139 }
140 } else {
141 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700142 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
143 mTitleBar.setShowProgressOnly(true);
144 setTitleGravity(Gravity.TOP);
145 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800146 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800147 }
148 }
149 }
150 }
151
152 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700153 public void setActiveTab(final Tab tab) {
John Reck88a42b72011-03-21 16:30:12 -0700154 captureTab(mActiveTab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700155 super.setActiveTab(tab, true);
156 setActiveTab(tab, true);
157 }
158
159 @Override
160 void setActiveTab(Tab tab, boolean needsAttaching) {
161 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800162 // TabControl.setCurrentTab has been called before this,
163 // so the tab is guaranteed to have a webview
164 if (view == null) {
165 Log.e(LOGTAG, "active tab with no webview detected");
166 return;
167 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700168 // Request focus on the top window.
169 if (mUseQuickControls) {
170 mPieControl.forceToTop(mContentView);
171 view.setScrollListener(null);
172 } else {
173 // check if title bar is already attached by animation
174 if (mTitleBar.getParent() == null) {
175 view.setEmbeddedTitleBar(mTitleBar);
176 }
177 }
Michael Kolb376b5412010-12-15 11:52:57 -0800178 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700179 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800180 } else {
181 revertVoiceTitleBar(tab);
182 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700183 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800184 tab.getTopWindow().requestFocus();
185 }
186
187 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800188 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800189 return mTitleBar;
190 }
191
192 // active tabs page
193
194 @Override
195 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700196 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800197 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
198 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800199 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800200 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
201 mActiveTabsPage.requestFocus();
202 }
203
204 /**
205 * Remove the active tabs page.
206 */
207 @Override
208 public void removeActiveTabsPage() {
209 mContentView.removeView(mActiveTabsPage);
210 mTitleBar.setVisibility(View.VISIBLE);
211 mActiveTabsPage = null;
212 }
213
214 @Override
215 public boolean showsWeb() {
216 return super.showsWeb() && mActiveTabsPage == null;
217 }
218
219 // menu handling callbacks
220
221 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800222 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800223 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800224 }
225
226 @Override
227 public void onContextMenuClosed(Menu menu, boolean inLoad) {
228 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800229 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231 }
232
233 // action mode callbacks
234
235 @Override
236 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800237 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800238 }
239
Michael Kolba4183062011-01-16 10:43:21 -0800240 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700241 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700242 if (inLoad) {
243 if (mUseQuickControls) {
244 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700245 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700246 showTitleBar();
247 }
248 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700249 }
250
251 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700252 protected void setTitleGravity(int gravity) {
253 if (mUseQuickControls) {
254 FrameLayout.LayoutParams lp =
255 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
256 lp.gravity = gravity;
257 getTitleBar().setLayoutParams(lp);
258 } else {
259 super.setTitleGravity(gravity);
260 }
261 }
262
263 private void setUseQuickControls(boolean useQuickControls) {
264 mUseQuickControls = useQuickControls;
265 getTitleBar().setUseQuickControls(mUseQuickControls);
266 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700267 mPieControl = new PieControl(mActivity, mUiController, this);
268 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700269 WebView web = getWebView();
270 if (web != null) {
271 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700272 }
273 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700274 if (mPieControl != null) {
275 mPieControl.removeFromContainer(mContentView);
276 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700277 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700278 if (web != null) {
279 web.setEmbeddedTitleBar(mTitleBar);
280 }
281 setTitleGravity(Gravity.NO_GRAVITY);
282 }
283 }
284
285 @Override
286 public boolean onPrepareOptionsMenu(Menu menu) {
287 if (mUseQuickControls) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700288 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolbfdb70242011-03-24 09:41:11 -0700289 mPieControl.onMenuOpened(menu);
290 return false;
Michael Kolbfdb70242011-03-24 09:41:11 -0700291 }
Michael Kolbfedb4922011-04-20 16:45:33 -0700292 return true;
Michael Kolbfdb70242011-03-24 09:41:11 -0700293 }
294
295 @Override
296 protected void captureTab(final Tab tab) {
297 if (mUseQuickControls) {
298 super.captureTab(tab);
299 } else {
300 captureTab(tab,
301 mActivity.getWindowManager().getDefaultDisplay().getWidth(),
Michael Kolbf2055602011-04-09 17:20:03 -0700302 mActivity.getWindowManager().getDefaultDisplay().getHeight());
Michael Kolbfdb70242011-03-24 09:41:11 -0700303 }
304 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700305
Michael Kolbf2055602011-04-09 17:20:03 -0700306 void showNavScreen() {
307 captureTab(mActiveTab);
308 mNavScreen = new NavScreen(mActivity, mUiController, this);
309 WebView web = getWebView();
310 if (web != null) {
311 int w = web.getWidth();
312 int h = web.getHeight();
313 mNavScreen.setTabDimensions((int) (w * NAV_TAB_SCALE),
314 (int) (h * NAV_TAB_SCALE));
315 }
316 // Add the custom view to its container.
317 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_GRAVITY_CENTER);
318 ObjectAnimator animx = ObjectAnimator.ofFloat(mContentView,
319 "scaleX", 1.0f, 0.85f);
320 ObjectAnimator animy = ObjectAnimator.ofFloat(mContentView,
321 "scaleY", 1.0f, 0.85f);
322 AnimatorSet anims = new AnimatorSet();
323 anims.setDuration(200);
324 anims.addListener(new AnimatorListener() {
325
326 @Override
327 public void onAnimationCancel(Animator animation) {
328 finishShowNavScreen();
329 }
330
331 @Override
332 public void onAnimationEnd(Animator animation) {
333 finishShowNavScreen();
334 }
335
336 @Override
337 public void onAnimationRepeat(Animator animation) {
338 }
339
340 @Override
341 public void onAnimationStart(Animator animation) {
342 }
343 });
344 anims.playTogether(animx, animy);
345 anims.start();
346 }
347
348 private void finishShowNavScreen() {
349 // Hide the content view.
350 mContentView.setVisibility(View.GONE);
351 mContentView.setScaleX(1.0f);
352 mContentView.setScaleY(1.0f);
353 // Finally show the custom view container.
354 mCustomViewContainer.setVisibility(View.VISIBLE);
355 mCustomViewContainer.bringToFront();
356 }
357
358 void hideNavScreen(boolean animateToPage) {
359 if (mNavScreen == null) return;
360 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) {
371 finishHideNavScreen();
372 }
373
374 @Override
375 public void onAnimationEnd(Animator animation) {
376 finishHideNavScreen();
377 }
378
379 @Override
380 public void onAnimationRepeat(Animator animation) {
381 }
382
383 @Override
384 public void onAnimationStart(Animator animation) {
385 }
386 });
387 anims.playTogether(animx, animy);
388 anims.start();
389 } else {
390 finishHideNavScreen();
391 }
392
393 }
394
395 private void finishHideNavScreen() {
396 // Hide the custom view.
397 mNavScreen.setVisibility(View.GONE);
398 // 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);
404 }
405
Michael Kolb66706532010-12-12 19:50:22 -0800406}