blob: ddb4e0e0c7fe2d6bb7e42eced909b8858021a2e8 [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;
John Reck9c5004e2011-10-07 16:00:12 -070028import android.os.Message;
Michael Kolb376b5412010-12-15 11:52:57 -080029import android.util.Log;
John Reckaae029c2011-09-16 14:36:39 -070030import android.util.TypedValue;
Michael Kolb66706532010-12-12 19:50:22 -080031import android.view.ActionMode;
32import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080033import android.view.KeyEvent;
Michael Kolbc3af0672011-08-09 10:24:41 -070034import android.view.LayoutInflater;
Michael Kolb66706532010-12-12 19:50:22 -080035import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070036import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080037import android.view.View;
Michael Kolb199e43c2011-08-17 10:45:45 -070038import android.view.ViewGroup;
Michael Kolb30adae62011-07-29 13:37:05 -070039import android.view.accessibility.AccessibilityEvent;
Michael Kolb66706532010-12-12 19:50:22 -080040import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070041import android.widget.FrameLayout;
Michael Kolbc3af0672011-08-09 10:24:41 -070042import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080043
Michael Kolb629b22c2011-07-13 16:26:47 -070044import com.android.browser.UrlInputView.StateListener;
45
Michael Kolb66706532010-12-12 19:50:22 -080046/**
47 * Ui for regular phone screen sizes
48 */
49public class PhoneUi extends BaseUi {
50
51 private static final String LOGTAG = "PhoneUi";
John Reck9c5004e2011-10-07 16:00:12 -070052 private static final int MSG_INIT_NAVSCREEN = 100;
Michael Kolb66706532010-12-12 19:50:22 -080053
Michael Kolb0241e752011-07-07 14:58:50 -070054 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070055 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070056 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070057 private NavigationBarPhone mNavigationBar;
John Reckaae029c2011-09-16 14:36:39 -070058 private int mActionBarHeight;
Michael Kolb66706532010-12-12 19:50:22 -080059
60 boolean mExtendedMenuOpen;
61 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070062 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080063
64 /**
65 * @param browser
66 * @param controller
67 */
68 public PhoneUi(Activity browser, UiController controller) {
69 super(browser, controller);
Michael Kolbfdb70242011-03-24 09:41:11 -070070 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070071 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reckaae029c2011-09-16 14:36:39 -070072 TypedValue heightValue = new TypedValue();
73 browser.getTheme().resolveAttribute(
74 com.android.internal.R.attr.actionBarSize, heightValue, true);
75 mActionBarHeight = TypedValue.complexToDimensionPixelSize(heightValue.data,
76 browser.getResources().getDisplayMetrics());
John Reck285ef042011-02-11 15:44:17 -080077 }
Michael Kolb66706532010-12-12 19:50:22 -080078
John Reck285ef042011-02-11 15:44:17 -080079 @Override
Michael Kolb66706532010-12-12 19:50:22 -080080 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080081 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080082 }
83
84 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080085 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070086 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070087 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070088 }
89 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080090 }
91
92 @Override
Michael Kolb66706532010-12-12 19:50:22 -080093 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070094 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070095 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070096 return true;
Michael Kolb66706532010-12-12 19:50:22 -080097 }
98 return super.onBackKey();
99 }
100
John Reckcc0059c2011-10-07 13:53:13 -0700101 private boolean showingNavScreen() {
102 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
103 }
104
Michael Kolb66706532010-12-12 19:50:22 -0800105 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700106 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700107 return false;
108 }
109
110 @Override
John Reck30c714c2010-12-16 17:30:34 -0800111 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800112 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800113 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800114 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800115 if (progress == 100) {
116 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700117 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700118 if (mUseQuickControls) {
119 mTitleBar.setShowProgressOnly(false);
120 }
Michael Kolb66706532010-12-12 19:50:22 -0800121 }
122 } else {
123 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700124 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
125 mTitleBar.setShowProgressOnly(true);
126 setTitleGravity(Gravity.TOP);
127 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800128 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800129 }
130 }
131 }
John Reck9c5004e2011-10-07 16:00:12 -0700132 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
133 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
134 }
135 }
136
137 @Override
138 protected void handleMessage(Message msg) {
139 super.handleMessage(msg);
140 if (msg.what == MSG_INIT_NAVSCREEN) {
141 if (mNavScreen == null) {
142 mNavScreen = new NavScreen(mActivity, mUiController, this);
143 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
144 mNavScreen.setVisibility(View.GONE);
145 }
146 if (mAnimScreen == null) {
147 mAnimScreen = new AnimScreen(mActivity);
148 // initialize bitmaps
149 mAnimScreen.set(getTitleBar(), getWebView());
150 }
151 }
Michael Kolb66706532010-12-12 19:50:22 -0800152 }
153
154 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700155 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700156 mTitleBar.cancelTitleBarAnimation(true);
157 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700158 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700159 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800160 // TabControl.setCurrentTab has been called before this,
161 // so the tab is guaranteed to have a webview
162 if (view == null) {
163 Log.e(LOGTAG, "active tab with no webview detected");
164 return;
165 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700166 // Request focus on the top window.
167 if (mUseQuickControls) {
168 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700169 } else {
170 // check if title bar is already attached by animation
171 if (mTitleBar.getParent() == null) {
172 view.setEmbeddedTitleBar(mTitleBar);
173 }
174 }
Michael Kolb376b5412010-12-15 11:52:57 -0800175 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700176 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800177 } else {
178 revertVoiceTitleBar(tab);
179 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700180 // update nav bar state
181 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700182 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800183 tab.getTopWindow().requestFocus();
John Reck8ee633f2011-08-09 16:00:35 -0700184 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800185 }
186
Michael Kolb66706532010-12-12 19:50:22 -0800187 // menu handling callbacks
188
189 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700190 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700191 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700192 return true;
193 }
194
195 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700196 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700197 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
198 if (bm != null) {
George Mount387d45d2011-10-07 15:57:53 -0700199 String url = tab.getUrl();
200 boolean isDataUrl = DataUri.isDataUri(url);
201 bm.setVisible(!showingNavScreen() && !isDataUrl);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700202 }
203 MenuItem nt = menu.findItem(R.id.new_tab_menu_id);
204 if (nt != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700205 nt.setVisible(!showingNavScreen());
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700206 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700207 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
208 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700209 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700210 }
John Reckcc0059c2011-10-07 13:53:13 -0700211 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700212 menu.setGroupVisible(R.id.LIVE_MENU, false);
213 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
214 menu.findItem(R.id.page_info_menu_id).setVisible(false);
215 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700216 }
217 }
218
219 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700220 public boolean onOptionsItemSelected(MenuItem item) {
John Reckcc0059c2011-10-07 13:53:13 -0700221 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -0700222 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700223 }
224 return false;
225 }
226
227 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800228 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800229 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231
232 @Override
233 public void onContextMenuClosed(Menu menu, boolean inLoad) {
234 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237 }
238
239 // action mode callbacks
240
241 @Override
242 public void onActionModeStarted(ActionMode mode) {
Michael Kolb42c0c062011-08-02 12:56:06 -0700243 if (!isEditingUrl()) {
244 hideTitleBar();
John Reckaae029c2011-09-16 14:36:39 -0700245 } else {
246 mTitleBar.animate().translationY(mActionBarHeight);
Michael Kolb42c0c062011-08-02 12:56:06 -0700247 }
Michael Kolb66706532010-12-12 19:50:22 -0800248 }
249
Michael Kolba4183062011-01-16 10:43:21 -0800250 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700251 public void onActionModeFinished(boolean inLoad) {
John Reckaae029c2011-09-16 14:36:39 -0700252 mTitleBar.animate().translationY(0);
Michael Kolbc16c5952011-03-29 15:37:03 -0700253 if (inLoad) {
254 if (mUseQuickControls) {
255 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700256 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700257 showTitleBar();
258 }
John Reck6cda7b12011-03-18 15:57:13 -0700259 }
260
261 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700262 protected void setTitleGravity(int gravity) {
263 if (mUseQuickControls) {
264 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700265 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700266 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700267 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700268 } else {
269 super.setTitleGravity(gravity);
270 }
271 }
272
Michael Kolb0241e752011-07-07 14:58:50 -0700273 @Override
274 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700275 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700276 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700277 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700278 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700279 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700280 WebView web = getWebView();
281 if (web != null) {
282 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700283 }
284 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700285 if (mPieControl != null) {
286 mPieControl.removeFromContainer(mContentView);
287 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700288 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700289 if (web != null) {
Michael Kolb199e43c2011-08-17 10:45:45 -0700290 // make sure we can re-parent titlebar
291 if ((mTitleBar != null) && (mTitleBar.getParent() != null)) {
292 ((ViewGroup) mTitleBar.getParent()).removeView(mTitleBar);
293 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700294 web.setEmbeddedTitleBar(mTitleBar);
295 }
296 setTitleGravity(Gravity.NO_GRAVITY);
297 }
John Reck718a24d2011-08-12 11:08:30 -0700298 updateUrlBarAutoShowManagerTarget();
Michael Kolbfdb70242011-03-24 09:41:11 -0700299 }
300
Michael Kolbc3af0672011-08-09 10:24:41 -0700301 @Override
302 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700303 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700304 }
305
306 @Override
307 public void showWeb(boolean animate) {
308 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700309 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700310 }
311
Michael Kolbf2055602011-04-09 17:20:03 -0700312 void showNavScreen() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700313 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700314 if (mNavScreen == null) {
315 mNavScreen = new NavScreen(mActivity, mUiController, this);
316 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
317 } else {
318 mNavScreen.setVisibility(View.VISIBLE);
319 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700320 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700321 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700322 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700323 if (mAnimScreen == null) {
324 mAnimScreen = new AnimScreen(mActivity);
325 }
326 mAnimScreen.set(getTitleBar(), getWebView());
327 final View animView = mAnimScreen.mMain;
Michael Kolbc3af0672011-08-09 10:24:41 -0700328 mCustomViewContainer.addView(animView, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700329 mCustomViewContainer.setVisibility(View.VISIBLE);
330 mCustomViewContainer.bringToFront();
Michael Kolbc3af0672011-08-09 10:24:41 -0700331 int fromLeft = 0;
332 int fromTop = getTitleBar().getHeight();
333 int fromRight = mContentView.getWidth();
334 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700335 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
336 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700337 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700338 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700339 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700340 int toRight = toLeft + width;
341 int toBottom = toTop + height;
342 float scaleFactor = width / (float) mContentView.getWidth();
343 detachTab(mActiveTab);
344 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700345 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700346 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700347 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700348 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700349 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700350 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700351 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700352 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700353 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700354 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700355 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700356 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700357 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700358 1f, scaleFactor);
John Reckcc0059c2011-10-07 13:53:13 -0700359 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 1, 0);
Michael Kolba3194d02011-09-07 11:23:51 -0700360 blend1.setDuration(100);
361
362 inanim.playTogether(tx, ty, tr, tb, sx, title);
363 inanim.setDuration(200);
364 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700365 @Override
366 public void onAnimationEnd(Animator anim) {
367 mCustomViewContainer.removeView(animView);
368 finishAnimationIn();
369 mUiController.setBlockEvents(false);
370 }
371 });
Michael Kolba3194d02011-09-07 11:23:51 -0700372 set1.playSequentially(inanim, blend1);
373 set1.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700374 }
375
376 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700377 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700378 // notify accessibility manager about the screen change
379 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
380 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
381 }
Michael Kolbf2055602011-04-09 17:20:03 -0700382 }
383
Michael Kolba3194d02011-09-07 11:23:51 -0700384 void hideNavScreen(int position, boolean animate) {
John Reckcc0059c2011-10-07 13:53:13 -0700385 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700386 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700387 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700388 if (tab != null) {
389 setActiveTab(tab);
390 } else if (mTabControl.getTabCount() > 0) {
391 // use a fallback tab
392 setActiveTab(mTabControl.getCurrentTab());
393 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700394 mContentView.setVisibility(View.VISIBLE);
395 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700396 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700397 }
Michael Kolba3194d02011-09-07 11:23:51 -0700398 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700399 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700400 if (mTabControl.getTabCount() > 0) {
401 // use a fallback tab
402 setActiveTab(mTabControl.getCurrentTab());
403 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700404 mContentView.setVisibility(View.VISIBLE);
405 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700406 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700407 }
408 mUiController.setBlockEvents(true);
409 mUiController.setActiveTab(tab);
410 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700411 if (mAnimScreen == null) {
412 mAnimScreen = new AnimScreen(mActivity);
413 }
414 mAnimScreen.set(tab.getScreenshot());
415 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
416 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Michael Kolba3194d02011-09-07 11:23:51 -0700417 mContentView.getHeight());
418 mNavScreen.mScroller.finishScroller();
419 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700420 int toLeft = 0;
421 int toTop = getTitleBar().getHeight();
422 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700423 int width = target.getDrawable().getIntrinsicWidth();
424 int height = target.getDrawable().getIntrinsicHeight();
425 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
426 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Michael Kolbc3af0672011-08-09 10:24:41 -0700427 int fromRight = fromLeft + width;
428 int fromBottom = fromTop + height;
429 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700430 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700431 mAnimScreen.mContent.setLeft(fromLeft);
432 mAnimScreen.mContent.setTop(fromTop);
433 mAnimScreen.mContent.setRight(fromRight);
434 mAnimScreen.mContent.setBottom(fromBottom);
435 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700436 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700437 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700438 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700439 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700440 set1.setDuration(100);
441 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700442 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700443 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700444 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700445 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700446 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700447 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700448 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700449 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700450 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700451 1f, scaleFactor);
Michael Kolbc3af0672011-08-09 10:24:41 -0700452 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700453 otheralpha.setDuration(100);
454 set2.playTogether(l, t, r, b, scale);
455 set2.setDuration(200);
456 AnimatorSet combo = new AnimatorSet();
457 combo.playSequentially(set1, set2, otheralpha);
458 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700459 @Override
460 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700461 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700462 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700463 mUiController.setBlockEvents(false);
464 }
465 });
Michael Kolba3194d02011-09-07 11:23:51 -0700466 combo.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700467 }
468
Michael Kolbbf32cd02011-08-16 15:07:04 -0700469 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700470 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700471 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700472 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700473 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700474 }
475
John Reck6b4bd5f2011-07-12 10:14:38 -0700476 @Override
477 public boolean needsRestoreAllTabs() {
478 return false;
479 }
480
Michael Kolb20be26d2011-07-18 16:38:02 -0700481 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700482 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700483 showNavScreen();
484 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700485 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700486 }
487 }
488
John Reck1cf4b792011-07-26 10:22:22 -0700489 @Override
490 public boolean shouldCaptureThumbnails() {
491 return true;
492 }
493
Michael Kolbc3af0672011-08-09 10:24:41 -0700494 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700495
Michael Kolbc3af0672011-08-09 10:24:41 -0700496 private View mMain;
497 private ImageView mTitle;
498 private ImageView mContent;
499 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700500 private Bitmap mTitleBarBitmap;
501 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700502
John Reckcc0059c2011-10-07 13:53:13 -0700503 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700504 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
505 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700506 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700507 mContent = (ImageView) mMain.findViewById(R.id.content);
508 mContent.setScaleType(ImageView.ScaleType.MATRIX);
509 mContent.setImageMatrix(new Matrix());
510 mScale = 1.0f;
511 setScaleFactor(getScaleFactor());
512 }
513
514 public void set(TitleBar tbar, WebView web) {
515 if (mTitleBarBitmap == null
516 || mTitleBarBitmap.getWidth() != tbar.getWidth()
John Reck9c5004e2011-10-07 16:00:12 -0700517 || mTitleBarBitmap.getHeight() != tbar.getEmbeddedHeight()) {
John Reckcc0059c2011-10-07 13:53:13 -0700518 mTitleBarBitmap = Bitmap.createBitmap(tbar.getWidth(),
John Reck9c5004e2011-10-07 16:00:12 -0700519 tbar.getEmbeddedHeight(), Bitmap.Config.RGB_565);
John Reckcc0059c2011-10-07 13:53:13 -0700520 }
521 Canvas c = new Canvas(mTitleBarBitmap);
522 tbar.draw(c);
523 c.setBitmap(null);
524 mTitle.setImageBitmap(mTitleBarBitmap);
525 mTitle.setVisibility(View.VISIBLE);
John Reck9c5004e2011-10-07 16:00:12 -0700526 int h = web.getHeight() - tbar.getEmbeddedHeight();
John Reckcc0059c2011-10-07 13:53:13 -0700527 if (mContentBitmap == null
528 || mContentBitmap.getWidth() != web.getWidth()
529 || mContentBitmap.getHeight() != h) {
530 mContentBitmap = Bitmap.createBitmap(web.getWidth(), h,
531 Bitmap.Config.RGB_565);
532 }
533 c.setBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700534 int tx = web.getScrollX();
535 int ty = web.getScrollY();
John Reck9c5004e2011-10-07 16:00:12 -0700536 c.translate(-tx, -ty - tbar.getEmbeddedHeight());
John Reckcc0059c2011-10-07 13:53:13 -0700537 web.draw(c);
538 c.setBitmap(null);
539 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700540 }
541
John Reckcc0059c2011-10-07 13:53:13 -0700542 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700543 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700544 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700545 }
546
John Reckcc0059c2011-10-07 13:53:13 -0700547 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700548 mScale = sf;
549 Matrix m = new Matrix();
550 m.postScale(sf,sf);
551 mContent.setImageMatrix(m);
552 }
553
John Reckcc0059c2011-10-07 13:53:13 -0700554 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700555 return mScale;
556 }
557
John Reck3ba45532011-08-11 16:26:53 -0700558 }
559
Michael Kolb66706532010-12-12 19:50:22 -0800560}