blob: ee5ca4080e4af2545fd3c00ca8e4ea4b272e29cb [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 Kolb0d0245f2011-12-05 16:36:05 -0800197 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
198 if (bm != null) {
199 bm.setVisible(!showingNavScreen());
200 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700201 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
202 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700203 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700204 }
Michael Kolb315d5022011-10-13 12:47:11 -0700205 MenuItem info = menu.findItem(R.id.page_info_menu_id);
206 if (info != null) {
207 info.setVisible(false);
208 }
209 MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
210 if (newtab != null && !mUseQuickControls) {
211 newtab.setVisible(false);
212 }
213 MenuItem incognito = menu.findItem(R.id.incognito_menu_id);
214 if (incognito != null) {
215 incognito.setVisible(showingNavScreen() || mUseQuickControls);
216 }
John Reckcc0059c2011-10-07 13:53:13 -0700217 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700218 menu.setGroupVisible(R.id.LIVE_MENU, false);
219 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700220 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb315d5022011-10-13 12:47:11 -0700221 menu.setGroupVisible(R.id.COMBO_MENU, true);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700222 }
223 }
224
225 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700226 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700227 if (showingNavScreen()
228 && (item.getItemId() != R.id.history_menu_id)
229 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700230 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700231 }
232 return false;
233 }
234
235 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800236 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800237 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800238 }
239
240 @Override
241 public void onContextMenuClosed(Menu menu, boolean inLoad) {
242 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800243 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800244 }
245 }
246
247 // action mode callbacks
248
249 @Override
250 public void onActionModeStarted(ActionMode mode) {
Michael Kolb42c0c062011-08-02 12:56:06 -0700251 if (!isEditingUrl()) {
252 hideTitleBar();
John Reckaae029c2011-09-16 14:36:39 -0700253 } else {
254 mTitleBar.animate().translationY(mActionBarHeight);
Michael Kolb42c0c062011-08-02 12:56:06 -0700255 }
Michael Kolb66706532010-12-12 19:50:22 -0800256 }
257
Michael Kolba4183062011-01-16 10:43:21 -0800258 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700259 public void onActionModeFinished(boolean inLoad) {
John Reckaae029c2011-09-16 14:36:39 -0700260 mTitleBar.animate().translationY(0);
Michael Kolbc16c5952011-03-29 15:37:03 -0700261 if (inLoad) {
262 if (mUseQuickControls) {
263 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700264 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700265 showTitleBar();
266 }
John Reck6cda7b12011-03-18 15:57:13 -0700267 }
268
269 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700270 protected void setTitleGravity(int gravity) {
271 if (mUseQuickControls) {
272 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700273 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700274 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700275 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700276 } else {
277 super.setTitleGravity(gravity);
278 }
279 }
280
Michael Kolb0241e752011-07-07 14:58:50 -0700281 @Override
282 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700283 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700284 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700285 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700286 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700287 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700288 WebView web = getWebView();
289 if (web != null) {
290 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700291 }
292 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700293 if (mPieControl != null) {
294 mPieControl.removeFromContainer(mContentView);
295 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700296 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700297 if (web != null) {
Michael Kolb199e43c2011-08-17 10:45:45 -0700298 // make sure we can re-parent titlebar
299 if ((mTitleBar != null) && (mTitleBar.getParent() != null)) {
300 ((ViewGroup) mTitleBar.getParent()).removeView(mTitleBar);
301 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700302 web.setEmbeddedTitleBar(mTitleBar);
303 }
304 setTitleGravity(Gravity.NO_GRAVITY);
305 }
John Reck718a24d2011-08-12 11:08:30 -0700306 updateUrlBarAutoShowManagerTarget();
Michael Kolbfdb70242011-03-24 09:41:11 -0700307 }
308
Michael Kolbc3af0672011-08-09 10:24:41 -0700309 @Override
310 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700311 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700312 }
313
314 @Override
315 public void showWeb(boolean animate) {
316 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700317 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700318 }
319
Michael Kolbf2055602011-04-09 17:20:03 -0700320 void showNavScreen() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700321 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700322 if (mNavScreen == null) {
323 mNavScreen = new NavScreen(mActivity, mUiController, this);
324 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
325 } else {
326 mNavScreen.setVisibility(View.VISIBLE);
327 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700328 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700329 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700330 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700331 if (mAnimScreen == null) {
332 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700333 } else {
334 mAnimScreen.mMain.setAlpha(1f);
335 mAnimScreen.mTitle.setAlpha(1f);
336 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700337 }
338 mAnimScreen.set(getTitleBar(), getWebView());
Michael Kolba30e4fb2011-11-07 17:32:01 -0800339 if (mAnimScreen.mMain.getParent() == null) {
340 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
341 }
Michael Kolbf2055602011-04-09 17:20:03 -0700342 mCustomViewContainer.setVisibility(View.VISIBLE);
343 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700344 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
345 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700346 int fromLeft = 0;
347 int fromTop = getTitleBar().getHeight();
348 int fromRight = mContentView.getWidth();
349 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700350 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
351 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700352 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700353 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700354 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700355 int toRight = toLeft + width;
356 int toBottom = toTop + height;
357 float scaleFactor = width / (float) mContentView.getWidth();
358 detachTab(mActiveTab);
359 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700360 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700361 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700362 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700363 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700364 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700365 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700366 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700367 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700368 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700369 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700370 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700371 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700372 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700373 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700374 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
375 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700376 blend1.setDuration(100);
377
378 inanim.playTogether(tx, ty, tr, tb, sx, title);
379 inanim.setDuration(200);
380 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700381 @Override
382 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700383 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700384 finishAnimationIn();
385 mUiController.setBlockEvents(false);
386 }
387 });
Michael Kolba3194d02011-09-07 11:23:51 -0700388 set1.playSequentially(inanim, blend1);
389 set1.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700390 }
391
392 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700393 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700394 // notify accessibility manager about the screen change
395 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
396 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
397 }
Michael Kolbf2055602011-04-09 17:20:03 -0700398 }
399
Michael Kolba3194d02011-09-07 11:23:51 -0700400 void hideNavScreen(int position, boolean animate) {
John Reckcc0059c2011-10-07 13:53:13 -0700401 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700402 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700403 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700404 if (tab != null) {
405 setActiveTab(tab);
406 } else if (mTabControl.getTabCount() > 0) {
407 // use a fallback tab
408 setActiveTab(mTabControl.getCurrentTab());
409 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700410 mContentView.setVisibility(View.VISIBLE);
411 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700412 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700413 }
Michael Kolba3194d02011-09-07 11:23:51 -0700414 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700415 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700416 if (mTabControl.getTabCount() > 0) {
417 // use a fallback tab
418 setActiveTab(mTabControl.getCurrentTab());
419 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700420 mContentView.setVisibility(View.VISIBLE);
421 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700422 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700423 }
424 mUiController.setBlockEvents(true);
425 mUiController.setActiveTab(tab);
426 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700427 if (mAnimScreen == null) {
428 mAnimScreen = new AnimScreen(mActivity);
429 }
430 mAnimScreen.set(tab.getScreenshot());
Michael Kolb0755fcd2012-01-10 10:19:50 -0800431 if (mAnimScreen.mMain.getParent() == null) {
432 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
433 }
John Reckcc0059c2011-10-07 13:53:13 -0700434 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Michael Kolba3194d02011-09-07 11:23:51 -0700435 mContentView.getHeight());
436 mNavScreen.mScroller.finishScroller();
437 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700438 int toLeft = 0;
Michael Kolbb7e16702011-12-15 11:59:40 -0800439 int toTop = (tab.getWebView() != null) ? tab.getWebView().getVisibleTitleHeight() : 0;
Michael Kolbc3af0672011-08-09 10:24:41 -0700440 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700441 int width = target.getDrawable().getIntrinsicWidth();
442 int height = target.getDrawable().getIntrinsicHeight();
443 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
444 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Michael Kolbc3af0672011-08-09 10:24:41 -0700445 int fromRight = fromLeft + width;
446 int fromBottom = fromTop + height;
447 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700448 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700449 mAnimScreen.mContent.setLeft(fromLeft);
450 mAnimScreen.mContent.setTop(fromTop);
451 mAnimScreen.mContent.setRight(fromRight);
452 mAnimScreen.mContent.setBottom(fromBottom);
453 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700454 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700455 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700456 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700457 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700458 set1.setDuration(100);
459 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700460 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700461 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700462 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700463 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700464 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700465 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700466 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700467 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700468 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700469 1f, scaleFactor);
Michael Kolbc3af0672011-08-09 10:24:41 -0700470 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700471 otheralpha.setDuration(100);
472 set2.playTogether(l, t, r, b, scale);
473 set2.setDuration(200);
474 AnimatorSet combo = new AnimatorSet();
475 combo.playSequentially(set1, set2, otheralpha);
476 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700477 @Override
478 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700479 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700480 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700481 mUiController.setBlockEvents(false);
482 }
483 });
Michael Kolba3194d02011-09-07 11:23:51 -0700484 combo.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700485 }
486
Michael Kolbbf32cd02011-08-16 15:07:04 -0700487 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700488 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700489 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700490 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700491 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700492 }
493
John Reck6b4bd5f2011-07-12 10:14:38 -0700494 @Override
495 public boolean needsRestoreAllTabs() {
496 return false;
497 }
498
Michael Kolb20be26d2011-07-18 16:38:02 -0700499 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700500 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700501 showNavScreen();
502 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700503 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700504 }
505 }
506
John Reck1cf4b792011-07-26 10:22:22 -0700507 @Override
508 public boolean shouldCaptureThumbnails() {
509 return true;
510 }
511
Michael Kolbc3af0672011-08-09 10:24:41 -0700512 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700513
Michael Kolbc3af0672011-08-09 10:24:41 -0700514 private View mMain;
515 private ImageView mTitle;
516 private ImageView mContent;
517 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700518 private Bitmap mTitleBarBitmap;
519 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700520
John Reckcc0059c2011-10-07 13:53:13 -0700521 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700522 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
523 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700524 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700525 mContent = (ImageView) mMain.findViewById(R.id.content);
526 mContent.setScaleType(ImageView.ScaleType.MATRIX);
527 mContent.setImageMatrix(new Matrix());
528 mScale = 1.0f;
529 setScaleFactor(getScaleFactor());
530 }
531
532 public void set(TitleBar tbar, WebView web) {
John Reck62077d82011-10-13 15:17:50 -0700533 if (tbar == null || web == null) {
534 return;
535 }
John Reck1adf0302011-10-11 10:58:12 -0700536 if (tbar.getWidth() > 0 && tbar.getEmbeddedHeight() > 0) {
537 if (mTitleBarBitmap == null
538 || mTitleBarBitmap.getWidth() != tbar.getWidth()
539 || mTitleBarBitmap.getHeight() != tbar.getEmbeddedHeight()) {
John Recka98c83b2011-10-31 12:31:58 -0700540 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
541 tbar.getEmbeddedHeight());
John Reck1adf0302011-10-11 10:58:12 -0700542 }
John Recka98c83b2011-10-31 12:31:58 -0700543 if (mTitleBarBitmap != null) {
544 Canvas c = new Canvas(mTitleBarBitmap);
545 tbar.draw(c);
546 c.setBitmap(null);
547 }
John Reck1adf0302011-10-11 10:58:12 -0700548 } else {
549 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700550 }
John Reckcc0059c2011-10-07 13:53:13 -0700551 mTitle.setImageBitmap(mTitleBarBitmap);
552 mTitle.setVisibility(View.VISIBLE);
John Reck9c5004e2011-10-07 16:00:12 -0700553 int h = web.getHeight() - tbar.getEmbeddedHeight();
John Reckcc0059c2011-10-07 13:53:13 -0700554 if (mContentBitmap == null
555 || mContentBitmap.getWidth() != web.getWidth()
556 || mContentBitmap.getHeight() != h) {
John Recka98c83b2011-10-31 12:31:58 -0700557 mContentBitmap = safeCreateBitmap(web.getWidth(), h);
John Reckcc0059c2011-10-07 13:53:13 -0700558 }
John Recka98c83b2011-10-31 12:31:58 -0700559 if (mContentBitmap != null) {
560 Canvas c = new Canvas(mContentBitmap);
561 int tx = web.getScrollX();
562 int ty = web.getScrollY();
563 c.translate(-tx, -ty - tbar.getEmbeddedHeight());
564 web.draw(c);
565 c.setBitmap(null);
566 }
John Reckcc0059c2011-10-07 13:53:13 -0700567 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700568 }
569
John Recka98c83b2011-10-31 12:31:58 -0700570 private Bitmap safeCreateBitmap(int width, int height) {
571 if (width <= 0 || height <= 0) {
572 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
573 + ", height: " + height);
574 return null;
575 }
576 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
577 }
578
John Reckcc0059c2011-10-07 13:53:13 -0700579 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700580 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700581 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700582 }
583
John Reckcc0059c2011-10-07 13:53:13 -0700584 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700585 mScale = sf;
586 Matrix m = new Matrix();
587 m.postScale(sf,sf);
588 mContent.setImageMatrix(m);
589 }
590
John Reckcc0059c2011-10-07 13:53:13 -0700591 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700592 return mScale;
593 }
594
John Reck3ba45532011-08-11 16:26:53 -0700595 }
596
Michael Kolb66706532010-12-12 19:50:22 -0800597}