blob: 906a246bd92c2301b6d45237f4ae0761973da67a [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 Kolbc3af0672011-08-09 10:24:41 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Michael Kolb66706532010-12-12 19:50:22 -080023import android.app.Activity;
Michael Kolbc3af0672011-08-09 10:24:41 -070024import android.content.Context;
25import android.graphics.Bitmap;
26import android.graphics.Canvas;
27import android.graphics.Matrix;
Michael Kolb376b5412010-12-15 11:52:57 -080028import android.util.Log;
John Reckaae029c2011-09-16 14:36:39 -070029import android.util.TypedValue;
Michael Kolb66706532010-12-12 19:50:22 -080030import android.view.ActionMode;
31import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080032import android.view.KeyEvent;
Michael Kolbc3af0672011-08-09 10:24:41 -070033import android.view.LayoutInflater;
Michael Kolb66706532010-12-12 19:50:22 -080034import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070035import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080036import android.view.View;
Michael Kolb199e43c2011-08-17 10:45:45 -070037import android.view.ViewGroup;
Michael Kolb30adae62011-07-29 13:37:05 -070038import android.view.accessibility.AccessibilityEvent;
Michael Kolb66706532010-12-12 19:50:22 -080039import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070040import android.widget.FrameLayout;
Michael Kolbc3af0672011-08-09 10:24:41 -070041import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080042
Michael Kolb629b22c2011-07-13 16:26:47 -070043import com.android.browser.UrlInputView.StateListener;
44
Michael Kolb66706532010-12-12 19:50:22 -080045/**
46 * Ui for regular phone screen sizes
47 */
48public class PhoneUi extends BaseUi {
49
50 private static final String LOGTAG = "PhoneUi";
51
Michael Kolb0241e752011-07-07 14:58:50 -070052 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070053 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070054 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070055 private NavigationBarPhone mNavigationBar;
John Reckaae029c2011-09-16 14:36:39 -070056 private int mActionBarHeight;
Michael Kolb66706532010-12-12 19:50:22 -080057
58 boolean mExtendedMenuOpen;
59 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070060 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080061
62 /**
63 * @param browser
64 * @param controller
65 */
66 public PhoneUi(Activity browser, UiController controller) {
67 super(browser, controller);
Michael Kolbfdb70242011-03-24 09:41:11 -070068 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070069 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reckaae029c2011-09-16 14:36:39 -070070 TypedValue heightValue = new TypedValue();
71 browser.getTheme().resolveAttribute(
72 com.android.internal.R.attr.actionBarSize, heightValue, true);
73 mActionBarHeight = TypedValue.complexToDimensionPixelSize(heightValue.data,
74 browser.getResources().getDisplayMetrics());
John Reck285ef042011-02-11 15:44:17 -080075 }
Michael Kolb66706532010-12-12 19:50:22 -080076
John Reck285ef042011-02-11 15:44:17 -080077 @Override
Michael Kolb66706532010-12-12 19:50:22 -080078 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080079 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080080 }
81
82 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080083 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070084 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070085 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070086 }
87 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080088 }
89
90 @Override
Michael Kolb66706532010-12-12 19:50:22 -080091 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070092 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070093 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070094 return true;
Michael Kolb66706532010-12-12 19:50:22 -080095 }
96 return super.onBackKey();
97 }
98
John Reckcc0059c2011-10-07 13:53:13 -070099 private boolean showingNavScreen() {
100 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
101 }
102
Michael Kolb66706532010-12-12 19:50:22 -0800103 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700104 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700105 return false;
106 }
107
108 @Override
John Reck30c714c2010-12-16 17:30:34 -0800109 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800110 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800111 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800112 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800113 if (progress == 100) {
114 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700115 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700116 if (mUseQuickControls) {
117 mTitleBar.setShowProgressOnly(false);
118 }
Michael Kolb66706532010-12-12 19:50:22 -0800119 }
120 } else {
121 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700122 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
123 mTitleBar.setShowProgressOnly(true);
124 setTitleGravity(Gravity.TOP);
125 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800126 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800127 }
128 }
129 }
130 }
131
132 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700133 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700134 mTitleBar.cancelTitleBarAnimation(true);
135 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700136 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700137 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800138 // TabControl.setCurrentTab has been called before this,
139 // so the tab is guaranteed to have a webview
140 if (view == null) {
141 Log.e(LOGTAG, "active tab with no webview detected");
142 return;
143 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700144 // Request focus on the top window.
145 if (mUseQuickControls) {
146 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700147 } else {
148 // check if title bar is already attached by animation
149 if (mTitleBar.getParent() == null) {
150 view.setEmbeddedTitleBar(mTitleBar);
151 }
152 }
Michael Kolb376b5412010-12-15 11:52:57 -0800153 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700154 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800155 } else {
156 revertVoiceTitleBar(tab);
157 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700158 // update nav bar state
159 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700160 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800161 tab.getTopWindow().requestFocus();
John Reck8ee633f2011-08-09 16:00:35 -0700162 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800163 }
164
Michael Kolb66706532010-12-12 19:50:22 -0800165 // menu handling callbacks
166
167 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700168 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700169 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700170 return true;
171 }
172
173 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700174 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700175 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
176 if (bm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700177 bm.setVisible(!showingNavScreen());
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700178 }
179 MenuItem nt = menu.findItem(R.id.new_tab_menu_id);
180 if (nt != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700181 nt.setVisible(!showingNavScreen());
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700182 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700183 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
184 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700185 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700186 }
John Reckcc0059c2011-10-07 13:53:13 -0700187 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700188 menu.setGroupVisible(R.id.LIVE_MENU, false);
189 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
190 menu.findItem(R.id.page_info_menu_id).setVisible(false);
191 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700192 }
193 }
194
195 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700196 public boolean onOptionsItemSelected(MenuItem item) {
John Reckcc0059c2011-10-07 13:53:13 -0700197 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -0700198 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700199 }
200 return false;
201 }
202
203 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800204 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800205 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800206 }
207
208 @Override
209 public void onContextMenuClosed(Menu menu, boolean inLoad) {
210 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800211 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800212 }
213 }
214
215 // action mode callbacks
216
217 @Override
218 public void onActionModeStarted(ActionMode mode) {
Michael Kolb42c0c062011-08-02 12:56:06 -0700219 if (!isEditingUrl()) {
220 hideTitleBar();
John Reckaae029c2011-09-16 14:36:39 -0700221 } else {
222 mTitleBar.animate().translationY(mActionBarHeight);
Michael Kolb42c0c062011-08-02 12:56:06 -0700223 }
Michael Kolb66706532010-12-12 19:50:22 -0800224 }
225
Michael Kolba4183062011-01-16 10:43:21 -0800226 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700227 public void onActionModeFinished(boolean inLoad) {
John Reckaae029c2011-09-16 14:36:39 -0700228 mTitleBar.animate().translationY(0);
Michael Kolbc16c5952011-03-29 15:37:03 -0700229 if (inLoad) {
230 if (mUseQuickControls) {
231 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700232 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700233 showTitleBar();
234 }
John Reck6cda7b12011-03-18 15:57:13 -0700235 }
236
237 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700238 protected void setTitleGravity(int gravity) {
239 if (mUseQuickControls) {
240 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700241 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700242 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700243 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700244 } else {
245 super.setTitleGravity(gravity);
246 }
247 }
248
Michael Kolb0241e752011-07-07 14:58:50 -0700249 @Override
250 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700251 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700252 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700253 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700254 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700255 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700256 WebView web = getWebView();
257 if (web != null) {
258 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700259 }
260 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700261 if (mPieControl != null) {
262 mPieControl.removeFromContainer(mContentView);
263 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700264 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700265 if (web != null) {
Michael Kolb199e43c2011-08-17 10:45:45 -0700266 // make sure we can re-parent titlebar
267 if ((mTitleBar != null) && (mTitleBar.getParent() != null)) {
268 ((ViewGroup) mTitleBar.getParent()).removeView(mTitleBar);
269 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700270 web.setEmbeddedTitleBar(mTitleBar);
271 }
272 setTitleGravity(Gravity.NO_GRAVITY);
273 }
John Reck718a24d2011-08-12 11:08:30 -0700274 updateUrlBarAutoShowManagerTarget();
Michael Kolbfdb70242011-03-24 09:41:11 -0700275 }
276
Michael Kolbc3af0672011-08-09 10:24:41 -0700277 @Override
278 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700279 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700280 }
281
282 @Override
283 public void showWeb(boolean animate) {
284 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700285 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700286 }
287
Michael Kolbf2055602011-04-09 17:20:03 -0700288 void showNavScreen() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700289 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700290 if (mNavScreen == null) {
291 mNavScreen = new NavScreen(mActivity, mUiController, this);
292 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
293 } else {
294 mNavScreen.setVisibility(View.VISIBLE);
295 mNavScreen.setAlpha(1f);
296 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700297 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700298 if (mAnimScreen == null) {
299 mAnimScreen = new AnimScreen(mActivity);
300 }
301 mAnimScreen.set(getTitleBar(), getWebView());
302 final View animView = mAnimScreen.mMain;
Michael Kolbc3af0672011-08-09 10:24:41 -0700303 mCustomViewContainer.addView(animView, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700304 mCustomViewContainer.setVisibility(View.VISIBLE);
305 mCustomViewContainer.bringToFront();
Michael Kolbc3af0672011-08-09 10:24:41 -0700306 int fromLeft = 0;
307 int fromTop = getTitleBar().getHeight();
308 int fromRight = mContentView.getWidth();
309 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700310 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
311 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700312 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700313 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700314 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700315 int toRight = toLeft + width;
316 int toBottom = toTop + height;
317 float scaleFactor = width / (float) mContentView.getWidth();
318 detachTab(mActiveTab);
319 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700320 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700321 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700322 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700323 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700324 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700325 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700326 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700327 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700328 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700329 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700330 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700331 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700332 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700333 1f, scaleFactor);
John Reckcc0059c2011-10-07 13:53:13 -0700334 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 1, 0);
Michael Kolba3194d02011-09-07 11:23:51 -0700335 blend1.setDuration(100);
336
337 inanim.playTogether(tx, ty, tr, tb, sx, title);
338 inanim.setDuration(200);
339 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700340 @Override
341 public void onAnimationEnd(Animator anim) {
342 mCustomViewContainer.removeView(animView);
343 finishAnimationIn();
344 mUiController.setBlockEvents(false);
345 }
346 });
Michael Kolba3194d02011-09-07 11:23:51 -0700347 set1.playSequentially(inanim, blend1);
348 set1.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700349 }
350
351 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700352 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700353 // notify accessibility manager about the screen change
354 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
355 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
356 }
Michael Kolbf2055602011-04-09 17:20:03 -0700357 }
358
Michael Kolba3194d02011-09-07 11:23:51 -0700359 void hideNavScreen(int position, boolean animate) {
John Reckcc0059c2011-10-07 13:53:13 -0700360 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700361 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700362 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700363 if (tab != null) {
364 setActiveTab(tab);
365 } else if (mTabControl.getTabCount() > 0) {
366 // use a fallback tab
367 setActiveTab(mTabControl.getCurrentTab());
368 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700369 mContentView.setVisibility(View.VISIBLE);
370 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700371 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700372 }
Michael Kolba3194d02011-09-07 11:23:51 -0700373 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700374 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700375 if (mTabControl.getTabCount() > 0) {
376 // use a fallback tab
377 setActiveTab(mTabControl.getCurrentTab());
378 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700379 mContentView.setVisibility(View.VISIBLE);
380 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700381 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700382 }
383 mUiController.setBlockEvents(true);
384 mUiController.setActiveTab(tab);
385 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700386 if (mAnimScreen == null) {
387 mAnimScreen = new AnimScreen(mActivity);
388 }
389 mAnimScreen.set(tab.getScreenshot());
390 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
391 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Michael Kolba3194d02011-09-07 11:23:51 -0700392 mContentView.getHeight());
393 mNavScreen.mScroller.finishScroller();
394 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700395 int toLeft = 0;
396 int toTop = getTitleBar().getHeight();
397 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700398 int width = target.getDrawable().getIntrinsicWidth();
399 int height = target.getDrawable().getIntrinsicHeight();
400 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
401 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Michael Kolbc3af0672011-08-09 10:24:41 -0700402 int fromRight = fromLeft + width;
403 int fromBottom = fromTop + height;
404 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700405 int toBottom = toTop + (int) (height * scaleFactor);
John Reckcc0059c2011-10-07 13:53:13 -0700406 ObjectAnimator l1 = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolba3194d02011-09-07 11:23:51 -0700407 fromLeft, fromLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700408 ObjectAnimator t1 = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolba3194d02011-09-07 11:23:51 -0700409 fromTop, fromTop);
John Reckcc0059c2011-10-07 13:53:13 -0700410 ObjectAnimator r1 = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolba3194d02011-09-07 11:23:51 -0700411 fromRight, fromRight);
John Reckcc0059c2011-10-07 13:53:13 -0700412 ObjectAnimator b1 = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolba3194d02011-09-07 11:23:51 -0700413 fromBottom, fromBottom);
414 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700415 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700416 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
417 set1.playTogether(l1, t1, r1, b1, fade1, fade2);
418 set1.setDuration(100);
419 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700420 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700421 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700422 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700423 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700424 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700425 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700426 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700427 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700428 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700429 1f, scaleFactor);
Michael Kolbc3af0672011-08-09 10:24:41 -0700430 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700431 otheralpha.setDuration(100);
432 set2.playTogether(l, t, r, b, scale);
433 set2.setDuration(200);
434 AnimatorSet combo = new AnimatorSet();
435 combo.playSequentially(set1, set2, otheralpha);
436 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700437 @Override
438 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700439 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700440 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700441 mUiController.setBlockEvents(false);
442 }
443 });
Michael Kolba3194d02011-09-07 11:23:51 -0700444 combo.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700445 }
446
Michael Kolbbf32cd02011-08-16 15:07:04 -0700447 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700448 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700449 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700450 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700451 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700452 }
453
John Reck6b4bd5f2011-07-12 10:14:38 -0700454 @Override
455 public boolean needsRestoreAllTabs() {
456 return false;
457 }
458
Michael Kolb20be26d2011-07-18 16:38:02 -0700459 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700460 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700461 showNavScreen();
462 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700463 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700464 }
465 }
466
John Reck1cf4b792011-07-26 10:22:22 -0700467 @Override
468 public boolean shouldCaptureThumbnails() {
469 return true;
470 }
471
Michael Kolbc3af0672011-08-09 10:24:41 -0700472 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700473
Michael Kolbc3af0672011-08-09 10:24:41 -0700474 private View mMain;
475 private ImageView mTitle;
476 private ImageView mContent;
477 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700478 private Bitmap mTitleBarBitmap;
479 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700480
John Reckcc0059c2011-10-07 13:53:13 -0700481 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700482 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
483 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700484 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700485 mContent = (ImageView) mMain.findViewById(R.id.content);
486 mContent.setScaleType(ImageView.ScaleType.MATRIX);
487 mContent.setImageMatrix(new Matrix());
488 mScale = 1.0f;
489 setScaleFactor(getScaleFactor());
490 }
491
492 public void set(TitleBar tbar, WebView web) {
493 if (mTitleBarBitmap == null
494 || mTitleBarBitmap.getWidth() != tbar.getWidth()
495 || mTitleBarBitmap.getHeight() != tbar.getHeight()) {
496 mTitleBarBitmap = Bitmap.createBitmap(tbar.getWidth(),
497 tbar.getHeight(), Bitmap.Config.RGB_565);
498 }
499 Canvas c = new Canvas(mTitleBarBitmap);
500 tbar.draw(c);
501 c.setBitmap(null);
502 mTitle.setImageBitmap(mTitleBarBitmap);
503 mTitle.setVisibility(View.VISIBLE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700504 int h = web.getHeight() - tbar.getHeight();
John Reckcc0059c2011-10-07 13:53:13 -0700505 if (mContentBitmap == null
506 || mContentBitmap.getWidth() != web.getWidth()
507 || mContentBitmap.getHeight() != h) {
508 mContentBitmap = Bitmap.createBitmap(web.getWidth(), h,
509 Bitmap.Config.RGB_565);
510 }
511 c.setBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700512 int tx = web.getScrollX();
513 int ty = web.getScrollY();
John Reckcc0059c2011-10-07 13:53:13 -0700514 c.translate(-tx, -ty - tbar.getHeight());
515 web.draw(c);
516 c.setBitmap(null);
517 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700518 }
519
John Reckcc0059c2011-10-07 13:53:13 -0700520 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700521 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700522 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700523 }
524
John Reckcc0059c2011-10-07 13:53:13 -0700525 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700526 mScale = sf;
527 Matrix m = new Matrix();
528 m.postScale(sf,sf);
529 mContent.setImageMatrix(m);
530 }
531
John Reckcc0059c2011-10-07 13:53:13 -0700532 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700533 return mScale;
534 }
535
John Reck3ba45532011-08-11 16:26:53 -0700536 }
537
Michael Kolb66706532010-12-12 19:50:22 -0800538}