blob: 7db55633ccce52f7d30703cba686f6f9fdb43e8c [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;
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 Kolb30adae62011-07-29 13:37:05 -070037import android.view.accessibility.AccessibilityEvent;
Michael Kolb66706532010-12-12 19:50:22 -080038import android.webkit.WebView;
Michael Kolbc3af0672011-08-09 10:24:41 -070039import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080040
Michael Kolb629b22c2011-07-13 16:26:47 -070041import com.android.browser.UrlInputView.StateListener;
42
Michael Kolb66706532010-12-12 19:50:22 -080043/**
44 * Ui for regular phone screen sizes
45 */
46public class PhoneUi extends BaseUi {
47
48 private static final String LOGTAG = "PhoneUi";
John Reck9c5004e2011-10-07 16:00:12 -070049 private static final int MSG_INIT_NAVSCREEN = 100;
Michael Kolb66706532010-12-12 19:50:22 -080050
Michael Kolbf2055602011-04-09 17:20:03 -070051 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070052 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070053 private NavigationBarPhone mNavigationBar;
John Reckaae029c2011-09-16 14:36:39 -070054 private int mActionBarHeight;
Michael Kolb66706532010-12-12 19:50:22 -080055
56 boolean mExtendedMenuOpen;
57 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070058 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080059
60 /**
61 * @param browser
62 * @param controller
63 */
64 public PhoneUi(Activity browser, UiController controller) {
65 super(browser, controller);
Michael Kolbfdb70242011-03-24 09:41:11 -070066 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070067 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reckaae029c2011-09-16 14:36:39 -070068 TypedValue heightValue = new TypedValue();
69 browser.getTheme().resolveAttribute(
70 com.android.internal.R.attr.actionBarSize, heightValue, true);
71 mActionBarHeight = TypedValue.complexToDimensionPixelSize(heightValue.data,
72 browser.getResources().getDisplayMetrics());
John Reck285ef042011-02-11 15:44:17 -080073 }
Michael Kolb66706532010-12-12 19:50:22 -080074
John Reck285ef042011-02-11 15:44:17 -080075 @Override
Michael Kolb66706532010-12-12 19:50:22 -080076 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080077 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080078 }
79
80 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080081 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070082 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070083 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070084 }
85 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080086 }
87
88 @Override
Michael Kolb66706532010-12-12 19:50:22 -080089 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070090 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070091 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070092 return true;
Michael Kolb66706532010-12-12 19:50:22 -080093 }
94 return super.onBackKey();
95 }
96
John Reckcc0059c2011-10-07 13:53:13 -070097 private boolean showingNavScreen() {
98 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
99 }
100
Michael Kolb66706532010-12-12 19:50:22 -0800101 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700102 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700103 return false;
104 }
105
106 @Override
John Reck30c714c2010-12-16 17:30:34 -0800107 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800108 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800109 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800110 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800111 if (progress == 100) {
112 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700113 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700114 if (mUseQuickControls) {
115 mTitleBar.setShowProgressOnly(false);
116 }
Michael Kolb66706532010-12-12 19:50:22 -0800117 }
118 } else {
119 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700120 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
121 mTitleBar.setShowProgressOnly(true);
Michael Kolbc16c5952011-03-29 15:37:03 -0700122 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800123 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800124 }
125 }
126 }
John Reck9c5004e2011-10-07 16:00:12 -0700127 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
128 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
129 }
130 }
131
132 @Override
133 protected void handleMessage(Message msg) {
134 super.handleMessage(msg);
135 if (msg.what == MSG_INIT_NAVSCREEN) {
136 if (mNavScreen == null) {
137 mNavScreen = new NavScreen(mActivity, mUiController, this);
138 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
139 mNavScreen.setVisibility(View.GONE);
140 }
141 if (mAnimScreen == null) {
142 mAnimScreen = new AnimScreen(mActivity);
143 // initialize bitmaps
144 mAnimScreen.set(getTitleBar(), getWebView());
145 }
146 }
Michael Kolb66706532010-12-12 19:50:22 -0800147 }
148
149 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700150 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700151 mTitleBar.cancelTitleBarAnimation(true);
152 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700153 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700154 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800155 // TabControl.setCurrentTab has been called before this,
156 // so the tab is guaranteed to have a webview
157 if (view == null) {
158 Log.e(LOGTAG, "active tab with no webview detected");
159 return;
160 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700161 // Request focus on the top window.
162 if (mUseQuickControls) {
163 mPieControl.forceToTop(mContentView);
Michael Kolb4923c222012-04-02 16:18:36 -0700164 view.setTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700165 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700166 view.setTitleBar(mTitleBar);
Michael Kolbfdb70242011-03-24 09:41:11 -0700167 }
Michael Kolb376b5412010-12-15 11:52:57 -0800168 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700169 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800170 } else {
171 revertVoiceTitleBar(tab);
172 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700173 // update nav bar state
174 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700175 updateLockIconToLatest(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700176 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800177 }
178
Michael Kolb66706532010-12-12 19:50:22 -0800179 // menu handling callbacks
180
181 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700182 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700183 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700184 return true;
185 }
186
187 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700188 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800189 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
190 if (bm != null) {
191 bm.setVisible(!showingNavScreen());
192 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700193 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
194 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700195 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700196 }
Michael Kolb315d5022011-10-13 12:47:11 -0700197 MenuItem info = menu.findItem(R.id.page_info_menu_id);
198 if (info != null) {
199 info.setVisible(false);
200 }
201 MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
202 if (newtab != null && !mUseQuickControls) {
203 newtab.setVisible(false);
204 }
205 MenuItem incognito = menu.findItem(R.id.incognito_menu_id);
206 if (incognito != null) {
207 incognito.setVisible(showingNavScreen() || mUseQuickControls);
208 }
John Reckcc0059c2011-10-07 13:53:13 -0700209 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700210 menu.setGroupVisible(R.id.LIVE_MENU, false);
211 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700212 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb315d5022011-10-13 12:47:11 -0700213 menu.setGroupVisible(R.id.COMBO_MENU, true);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700214 }
215 }
216
217 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700218 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700219 if (showingNavScreen()
220 && (item.getItemId() != R.id.history_menu_id)
221 && (item.getItemId() != R.id.snapshots_menu_id)) {
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 Kolbc3af0672011-08-09 10:24:41 -0700262 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700263 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700264 }
265
266 @Override
267 public void showWeb(boolean animate) {
268 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700269 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700270 }
271
Michael Kolbf2055602011-04-09 17:20:03 -0700272 void showNavScreen() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700273 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700274 if (mNavScreen == null) {
275 mNavScreen = new NavScreen(mActivity, mUiController, this);
276 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
277 } else {
278 mNavScreen.setVisibility(View.VISIBLE);
279 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700280 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700281 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700282 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700283 if (mAnimScreen == null) {
284 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700285 } else {
286 mAnimScreen.mMain.setAlpha(1f);
287 mAnimScreen.mTitle.setAlpha(1f);
288 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700289 }
290 mAnimScreen.set(getTitleBar(), getWebView());
Michael Kolba30e4fb2011-11-07 17:32:01 -0800291 if (mAnimScreen.mMain.getParent() == null) {
292 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
293 }
Michael Kolbf2055602011-04-09 17:20:03 -0700294 mCustomViewContainer.setVisibility(View.VISIBLE);
295 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700296 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
297 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700298 int fromLeft = 0;
299 int fromTop = getTitleBar().getHeight();
300 int fromRight = mContentView.getWidth();
301 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700302 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
303 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700304 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700305 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700306 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700307 int toRight = toLeft + width;
308 int toBottom = toTop + height;
309 float scaleFactor = width / (float) mContentView.getWidth();
310 detachTab(mActiveTab);
311 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700312 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700313 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700314 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700315 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700316 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700317 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700318 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700319 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700320 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700321 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700322 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700323 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700324 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700325 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700326 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
327 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700328 blend1.setDuration(100);
329
330 inanim.playTogether(tx, ty, tr, tb, sx, title);
331 inanim.setDuration(200);
332 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700333 @Override
334 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700335 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700336 finishAnimationIn();
337 mUiController.setBlockEvents(false);
338 }
339 });
Michael Kolba3194d02011-09-07 11:23:51 -0700340 set1.playSequentially(inanim, blend1);
341 set1.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700342 }
343
344 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700345 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700346 // notify accessibility manager about the screen change
347 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
348 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
349 }
Michael Kolbf2055602011-04-09 17:20:03 -0700350 }
351
Michael Kolba3194d02011-09-07 11:23:51 -0700352 void hideNavScreen(int position, boolean animate) {
John Reckcc0059c2011-10-07 13:53:13 -0700353 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700354 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700355 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700356 if (tab != null) {
357 setActiveTab(tab);
358 } else if (mTabControl.getTabCount() > 0) {
359 // use a fallback tab
360 setActiveTab(mTabControl.getCurrentTab());
361 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700362 mContentView.setVisibility(View.VISIBLE);
363 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700364 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700365 }
Michael Kolba3194d02011-09-07 11:23:51 -0700366 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700367 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700368 if (mTabControl.getTabCount() > 0) {
369 // use a fallback tab
370 setActiveTab(mTabControl.getCurrentTab());
371 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700372 mContentView.setVisibility(View.VISIBLE);
373 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700374 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700375 }
376 mUiController.setBlockEvents(true);
377 mUiController.setActiveTab(tab);
378 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700379 if (mAnimScreen == null) {
380 mAnimScreen = new AnimScreen(mActivity);
381 }
382 mAnimScreen.set(tab.getScreenshot());
Michael Kolb0755fcd2012-01-10 10:19:50 -0800383 if (mAnimScreen.mMain.getParent() == null) {
384 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
385 }
John Reckcc0059c2011-10-07 13:53:13 -0700386 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Michael Kolba3194d02011-09-07 11:23:51 -0700387 mContentView.getHeight());
388 mNavScreen.mScroller.finishScroller();
389 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700390 int toLeft = 0;
Michael Kolbb7e16702011-12-15 11:59:40 -0800391 int toTop = (tab.getWebView() != null) ? tab.getWebView().getVisibleTitleHeight() : 0;
Michael Kolbc3af0672011-08-09 10:24:41 -0700392 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700393 int width = target.getDrawable().getIntrinsicWidth();
394 int height = target.getDrawable().getIntrinsicHeight();
395 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
396 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Michael Kolbc3af0672011-08-09 10:24:41 -0700397 int fromRight = fromLeft + width;
398 int fromBottom = fromTop + height;
399 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700400 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700401 mAnimScreen.mContent.setLeft(fromLeft);
402 mAnimScreen.mContent.setTop(fromTop);
403 mAnimScreen.mContent.setRight(fromRight);
404 mAnimScreen.mContent.setBottom(fromBottom);
405 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700406 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700407 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700408 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700409 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700410 set1.setDuration(100);
411 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700412 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700413 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700414 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700415 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700416 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700417 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700418 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700419 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700420 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700421 1f, scaleFactor);
Michael Kolbc3af0672011-08-09 10:24:41 -0700422 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700423 otheralpha.setDuration(100);
424 set2.playTogether(l, t, r, b, scale);
425 set2.setDuration(200);
426 AnimatorSet combo = new AnimatorSet();
427 combo.playSequentially(set1, set2, otheralpha);
428 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700429 @Override
430 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700431 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700432 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700433 mUiController.setBlockEvents(false);
434 }
435 });
Michael Kolba3194d02011-09-07 11:23:51 -0700436 combo.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700437 }
438
Michael Kolbbf32cd02011-08-16 15:07:04 -0700439 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700440 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700441 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700442 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700443 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700444 }
445
John Reck6b4bd5f2011-07-12 10:14:38 -0700446 @Override
447 public boolean needsRestoreAllTabs() {
448 return false;
449 }
450
Michael Kolb20be26d2011-07-18 16:38:02 -0700451 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700452 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700453 showNavScreen();
454 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700455 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700456 }
457 }
458
John Reck1cf4b792011-07-26 10:22:22 -0700459 @Override
460 public boolean shouldCaptureThumbnails() {
461 return true;
462 }
463
Michael Kolbc3af0672011-08-09 10:24:41 -0700464 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700465
Michael Kolbc3af0672011-08-09 10:24:41 -0700466 private View mMain;
467 private ImageView mTitle;
468 private ImageView mContent;
469 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700470 private Bitmap mTitleBarBitmap;
471 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700472
John Reckcc0059c2011-10-07 13:53:13 -0700473 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700474 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
475 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700476 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700477 mContent = (ImageView) mMain.findViewById(R.id.content);
478 mContent.setScaleType(ImageView.ScaleType.MATRIX);
479 mContent.setImageMatrix(new Matrix());
480 mScale = 1.0f;
481 setScaleFactor(getScaleFactor());
482 }
483
484 public void set(TitleBar tbar, WebView web) {
John Reck62077d82011-10-13 15:17:50 -0700485 if (tbar == null || web == null) {
486 return;
487 }
John Reck1adf0302011-10-11 10:58:12 -0700488 if (tbar.getWidth() > 0 && tbar.getEmbeddedHeight() > 0) {
489 if (mTitleBarBitmap == null
490 || mTitleBarBitmap.getWidth() != tbar.getWidth()
491 || mTitleBarBitmap.getHeight() != tbar.getEmbeddedHeight()) {
John Recka98c83b2011-10-31 12:31:58 -0700492 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
493 tbar.getEmbeddedHeight());
John Reck1adf0302011-10-11 10:58:12 -0700494 }
John Recka98c83b2011-10-31 12:31:58 -0700495 if (mTitleBarBitmap != null) {
496 Canvas c = new Canvas(mTitleBarBitmap);
497 tbar.draw(c);
498 c.setBitmap(null);
499 }
John Reck1adf0302011-10-11 10:58:12 -0700500 } else {
501 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700502 }
John Reckcc0059c2011-10-07 13:53:13 -0700503 mTitle.setImageBitmap(mTitleBarBitmap);
504 mTitle.setVisibility(View.VISIBLE);
John Reck9c5004e2011-10-07 16:00:12 -0700505 int h = web.getHeight() - tbar.getEmbeddedHeight();
John Reckcc0059c2011-10-07 13:53:13 -0700506 if (mContentBitmap == null
507 || mContentBitmap.getWidth() != web.getWidth()
508 || mContentBitmap.getHeight() != h) {
John Recka98c83b2011-10-31 12:31:58 -0700509 mContentBitmap = safeCreateBitmap(web.getWidth(), h);
John Reckcc0059c2011-10-07 13:53:13 -0700510 }
John Recka98c83b2011-10-31 12:31:58 -0700511 if (mContentBitmap != null) {
512 Canvas c = new Canvas(mContentBitmap);
513 int tx = web.getScrollX();
514 int ty = web.getScrollY();
515 c.translate(-tx, -ty - tbar.getEmbeddedHeight());
516 web.draw(c);
517 c.setBitmap(null);
518 }
John Reckcc0059c2011-10-07 13:53:13 -0700519 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700520 }
521
John Recka98c83b2011-10-31 12:31:58 -0700522 private Bitmap safeCreateBitmap(int width, int height) {
523 if (width <= 0 || height <= 0) {
524 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
525 + ", height: " + height);
526 return null;
527 }
528 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
529 }
530
John Reckcc0059c2011-10-07 13:53:13 -0700531 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700532 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700533 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700534 }
535
John Reckcc0059c2011-10-07 13:53:13 -0700536 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700537 mScale = sf;
538 Matrix m = new Matrix();
539 m.postScale(sf,sf);
540 mContent.setImageMatrix(m);
541 }
542
John Reckcc0059c2011-10-07 13:53:13 -0700543 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700544 return mScale;
545 }
546
John Reck3ba45532011-08-11 16:26:53 -0700547 }
548
Michael Kolb66706532010-12-12 19:50:22 -0800549}