blob: 01488e18f2875f8bc9ec77aefa607ef550fa5b1f [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb66706532010-12-12 19:50:22 -080018
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080038import org.codeaurora.swe.WebView;
Michael Kolbc3af0672011-08-09 10:24:41 -070039import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080040
Bijan Amirzada41242f22014-03-21 12:12:18 -070041import com.android.browser.R;
42import com.android.browser.UrlInputView.StateListener;
Michael Kolb629b22c2011-07-13 16:26:47 -070043
Michael Kolb66706532010-12-12 19:50:22 -080044/**
45 * Ui for regular phone screen sizes
46 */
47public class PhoneUi extends BaseUi {
48
49 private static final String LOGTAG = "PhoneUi";
John Reck9c5004e2011-10-07 16:00:12 -070050 private static final int MSG_INIT_NAVSCREEN = 100;
Michael Kolb66706532010-12-12 19:50:22 -080051
Michael Kolbf2055602011-04-09 17:20:03 -070052 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070053 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070054 private NavigationBarPhone mNavigationBar;
John Reckaae029c2011-09-16 14:36:39 -070055 private int mActionBarHeight;
Michael Kolb66706532010-12-12 19:50:22 -080056
Michael Kolb08a687a2011-04-22 16:13:02 -070057 boolean mAnimating;
yijunx.zhu2230a312012-07-27 13:37:00 -040058 boolean mShowNav = false;
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(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080070 android.R.attr.actionBarSize, heightValue, true);
John Reckaae029c2011-09-16 14:36:39 -070071 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 Kolb1f9b3562012-04-24 14:38:34 -070081 public void editUrl(boolean clearInput, boolean forceIME) {
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 }
yijunx.zhu2230a312012-07-27 13:37:00 -040085 //Do nothing while at Nav show screen.
86 if (mShowNav) return;
Michael Kolb1f9b3562012-04-24 14:38:34 -070087 super.editUrl(clearInput, forceIME);
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 Kolbe8a82332012-04-25 14:14:26 -0700110 super.onProgressChanged(tab);
John Reck9c5004e2011-10-07 16:00:12 -0700111 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
112 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
113 }
114 }
115
116 @Override
117 protected void handleMessage(Message msg) {
118 super.handleMessage(msg);
119 if (msg.what == MSG_INIT_NAVSCREEN) {
120 if (mNavScreen == null) {
121 mNavScreen = new NavScreen(mActivity, mUiController, this);
122 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
123 mNavScreen.setVisibility(View.GONE);
124 }
125 if (mAnimScreen == null) {
126 mAnimScreen = new AnimScreen(mActivity);
127 // initialize bitmaps
128 mAnimScreen.set(getTitleBar(), getWebView());
129 }
130 }
Michael Kolb66706532010-12-12 19:50:22 -0800131 }
132
133 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700134 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700135 mTitleBar.cancelTitleBarAnimation(true);
136 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700137 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400138
139 //if at Nav screen show, detach tab like what showNavScreen() do.
140 if (mShowNav) {
141 detachTab(mActiveTab);
142 }
143
Michael Kolbfdb70242011-03-24 09:41:11 -0700144 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800145 // TabControl.setCurrentTab has been called before this,
146 // so the tab is guaranteed to have a webview
147 if (view == null) {
148 Log.e(LOGTAG, "active tab with no webview detected");
149 return;
150 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700151 // Request focus on the top window.
152 if (mUseQuickControls) {
153 mPieControl.forceToTop(mContentView);
Michael Kolb4923c222012-04-02 16:18:36 -0700154 view.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700155 mTitleBar.setShowProgressOnly(true);
Michael Kolbfdb70242011-03-24 09:41:11 -0700156 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700157 view.setTitleBar(mTitleBar);
Michael Kolbfdb70242011-03-24 09:41:11 -0700158 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700159 // update nav bar state
160 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700161 updateLockIconToLatest(tab);
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 Kolb0d0245f2011-12-05 16:36:05 -0800175 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
176 if (bm != null) {
177 bm.setVisible(!showingNavScreen());
178 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700179 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
180 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700181 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700182 }
Michael Kolb315d5022011-10-13 12:47:11 -0700183 MenuItem info = menu.findItem(R.id.page_info_menu_id);
184 if (info != null) {
185 info.setVisible(false);
186 }
187 MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
188 if (newtab != null && !mUseQuickControls) {
189 newtab.setVisible(false);
190 }
191 MenuItem incognito = menu.findItem(R.id.incognito_menu_id);
192 if (incognito != null) {
193 incognito.setVisible(showingNavScreen() || mUseQuickControls);
194 }
Afzal Najamd4e33312012-04-26 01:54:01 -0400195 MenuItem closeOthers = menu.findItem(R.id.close_other_tabs_id);
196 if (closeOthers != null) {
197 boolean isLastTab = true;
198 if (tab != null) {
199 isLastTab = (mTabControl.getTabCount() <= 1);
200 }
201 closeOthers.setEnabled(!isLastTab);
202 }
John Reckcc0059c2011-10-07 13:53:13 -0700203 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700204 menu.setGroupVisible(R.id.LIVE_MENU, false);
205 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700206 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb315d5022011-10-13 12:47:11 -0700207 menu.setGroupVisible(R.id.COMBO_MENU, true);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700208 }
209 }
210
211 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700212 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700213 if (showingNavScreen()
214 && (item.getItemId() != R.id.history_menu_id)
215 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700216 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700217 }
218 return false;
219 }
220
221 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800222 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800223 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800224 }
225
226 @Override
227 public void onContextMenuClosed(Menu menu, boolean inLoad) {
228 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800229 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231 }
232
233 // action mode callbacks
234
235 @Override
236 public void onActionModeStarted(ActionMode mode) {
Michael Kolb42c0c062011-08-02 12:56:06 -0700237 if (!isEditingUrl()) {
238 hideTitleBar();
John Reckaae029c2011-09-16 14:36:39 -0700239 } else {
240 mTitleBar.animate().translationY(mActionBarHeight);
Michael Kolb42c0c062011-08-02 12:56:06 -0700241 }
Michael Kolb66706532010-12-12 19:50:22 -0800242 }
243
Michael Kolba4183062011-01-16 10:43:21 -0800244 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700245 public void onActionModeFinished(boolean inLoad) {
John Reckaae029c2011-09-16 14:36:39 -0700246 mTitleBar.animate().translationY(0);
Michael Kolbc16c5952011-03-29 15:37:03 -0700247 if (inLoad) {
248 if (mUseQuickControls) {
249 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700250 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700251 showTitleBar();
252 }
John Reck6cda7b12011-03-18 15:57:13 -0700253 }
254
255 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700256 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700257 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700258 }
259
260 @Override
261 public void showWeb(boolean animate) {
262 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700263 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700264 }
265
Michael Kolbf2055602011-04-09 17:20:03 -0700266 void showNavScreen() {
yijunx.zhu2230a312012-07-27 13:37:00 -0400267 mShowNav = true;
Michael Kolbc3af0672011-08-09 10:24:41 -0700268 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700269 if (mNavScreen == null) {
270 mNavScreen = new NavScreen(mActivity, mUiController, this);
271 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
272 } else {
273 mNavScreen.setVisibility(View.VISIBLE);
274 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700275 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700276 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700277 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700278 if (mAnimScreen == null) {
279 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700280 } else {
281 mAnimScreen.mMain.setAlpha(1f);
282 mAnimScreen.mTitle.setAlpha(1f);
283 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700284 }
285 mAnimScreen.set(getTitleBar(), getWebView());
Michael Kolba30e4fb2011-11-07 17:32:01 -0800286 if (mAnimScreen.mMain.getParent() == null) {
287 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
288 }
Michael Kolbf2055602011-04-09 17:20:03 -0700289 mCustomViewContainer.setVisibility(View.VISIBLE);
290 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700291 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
292 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700293 int fromLeft = 0;
294 int fromTop = getTitleBar().getHeight();
295 int fromRight = mContentView.getWidth();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800296 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
297 int fromBottom = mContentView.getHeight() + fixedTbarHeight;
Michael Kolba3194d02011-09-07 11:23:51 -0700298 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
299 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700300 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700301 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700302 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700303 int toRight = toLeft + width;
304 int toBottom = toTop + height;
305 float scaleFactor = width / (float) mContentView.getWidth();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800306 // SWE: Detaching the active tab results flashing screen with SWE.
307 // Not detaching the tab doesn't seem to have any issues.
308 //detachTab(mActiveTab);
Michael Kolbc3af0672011-08-09 10:24:41 -0700309 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700310 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700311 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700312 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700313 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700314 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700315 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700316 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700317 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700318 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700319 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700320 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700321 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700322 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700323 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700324 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
325 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700326 blend1.setDuration(100);
327
328 inanim.playTogether(tx, ty, tr, tb, sx, title);
329 inanim.setDuration(200);
330 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700331 @Override
332 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700333 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700334 finishAnimationIn();
335 mUiController.setBlockEvents(false);
336 }
337 });
Michael Kolba3194d02011-09-07 11:23:51 -0700338 set1.playSequentially(inanim, blend1);
339 set1.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700340 }
341
342 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700343 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700344 // notify accessibility manager about the screen change
345 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
346 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
347 }
Michael Kolbf2055602011-04-09 17:20:03 -0700348 }
349
Michael Kolba3194d02011-09-07 11:23:51 -0700350 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400351 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700352 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700353 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700354 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700355 if (tab != null) {
356 setActiveTab(tab);
357 } else if (mTabControl.getTabCount() > 0) {
358 // use a fallback tab
359 setActiveTab(mTabControl.getCurrentTab());
360 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700361 mContentView.setVisibility(View.VISIBLE);
362 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700363 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700364 }
Michael Kolba3194d02011-09-07 11:23:51 -0700365 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700366 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700367 if (mTabControl.getTabCount() > 0) {
368 // use a fallback tab
369 setActiveTab(mTabControl.getCurrentTab());
370 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700371 mContentView.setVisibility(View.VISIBLE);
372 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700373 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700374 }
375 mUiController.setBlockEvents(true);
376 mUiController.setActiveTab(tab);
377 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700378 if (mAnimScreen == null) {
379 mAnimScreen = new AnimScreen(mActivity);
380 }
381 mAnimScreen.set(tab.getScreenshot());
Michael Kolb0755fcd2012-01-10 10:19:50 -0800382 if (mAnimScreen.mMain.getParent() == null) {
383 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
384 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800385 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
John Reckcc0059c2011-10-07 13:53:13 -0700386 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800387 mContentView.getHeight() + fixedTbarHeight);
Michael Kolba3194d02011-09-07 11:23:51 -0700388 mNavScreen.mScroller.finishScroller();
389 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700390 int toLeft = 0;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800391 int toTop = 0;
392 if (mTitleBar.isFixed()) {
393 toTop = fixedTbarHeight;
394 } else {
395 toTop = (tab.getWebView() != null) ? tab.getWebView().getVisibleTitleHeight() : 0;
396 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700397 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 Reck9c5004e2011-10-07 16:00:12 -0700406 mAnimScreen.mContent.setLeft(fromLeft);
407 mAnimScreen.mContent.setTop(fromTop);
408 mAnimScreen.mContent.setRight(fromRight);
409 mAnimScreen.mContent.setBottom(fromBottom);
410 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700411 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700412 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700413 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700414 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700415 set1.setDuration(100);
416 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700417 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700418 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700419 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700420 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700421 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700422 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700423 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700424 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700425 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700426 1f, scaleFactor);
Michael Kolbc3af0672011-08-09 10:24:41 -0700427 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700428 otheralpha.setDuration(100);
429 set2.playTogether(l, t, r, b, scale);
430 set2.setDuration(200);
431 AnimatorSet combo = new AnimatorSet();
432 combo.playSequentially(set1, set2, otheralpha);
433 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700434 @Override
435 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700436 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700437 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700438 mUiController.setBlockEvents(false);
439 }
440 });
Michael Kolba3194d02011-09-07 11:23:51 -0700441 combo.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700442 }
443
Michael Kolbbf32cd02011-08-16 15:07:04 -0700444 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700445 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700446 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700447 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700448 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700449 }
450
John Reck6b4bd5f2011-07-12 10:14:38 -0700451 @Override
452 public boolean needsRestoreAllTabs() {
453 return false;
454 }
455
Michael Kolb20be26d2011-07-18 16:38:02 -0700456 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700457 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700458 showNavScreen();
459 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700460 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700461 }
462 }
463
John Reck1cf4b792011-07-26 10:22:22 -0700464 @Override
465 public boolean shouldCaptureThumbnails() {
466 return true;
467 }
468
Michael Kolbc3af0672011-08-09 10:24:41 -0700469 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700470
Michael Kolbc3af0672011-08-09 10:24:41 -0700471 private View mMain;
472 private ImageView mTitle;
473 private ImageView mContent;
474 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700475 private Bitmap mTitleBarBitmap;
476 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700477
John Reckcc0059c2011-10-07 13:53:13 -0700478 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700479 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
480 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700481 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700482 mContent = (ImageView) mMain.findViewById(R.id.content);
483 mContent.setScaleType(ImageView.ScaleType.MATRIX);
484 mContent.setImageMatrix(new Matrix());
485 mScale = 1.0f;
486 setScaleFactor(getScaleFactor());
487 }
488
489 public void set(TitleBar tbar, WebView web) {
John Reck62077d82011-10-13 15:17:50 -0700490 if (tbar == null || web == null) {
491 return;
492 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800493 int embTbarHeight = tbar.getEmbeddedHeight();
494 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
495 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700496 if (mTitleBarBitmap == null
497 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800498 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700499 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800500 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700501 }
John Recka98c83b2011-10-31 12:31:58 -0700502 if (mTitleBarBitmap != null) {
503 Canvas c = new Canvas(mTitleBarBitmap);
504 tbar.draw(c);
505 c.setBitmap(null);
506 }
John Reck1adf0302011-10-11 10:58:12 -0700507 } else {
508 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700509 }
John Reckcc0059c2011-10-07 13:53:13 -0700510 mTitle.setImageBitmap(mTitleBarBitmap);
511 mTitle.setVisibility(View.VISIBLE);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800512 // SWE: WebView.draw() wouldn't draw anything if SurfaceView is enabled.
513 mContentBitmap = web.getViewportBitmap();
514 if (mContentBitmap == null) {
515 int h = web.getHeight() - embTbarHeight;
516 if (mContentBitmap == null
517 || mContentBitmap.getWidth() != web.getWidth()
518 || mContentBitmap.getHeight() != h) {
519 mContentBitmap = safeCreateBitmap(web.getWidth(), h);
520 }
521 if (mContentBitmap != null) {
522 Canvas c = new Canvas(mContentBitmap);
523 int tx = web.getScrollX();
524 int ty = web.getScrollY();
525 c.translate(-tx, -ty - embTbarHeight);
526 web.draw(c);
527 c.setBitmap(null);
528 }
John Recka98c83b2011-10-31 12:31:58 -0700529 }
John Reckcc0059c2011-10-07 13:53:13 -0700530 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700531 }
532
John Recka98c83b2011-10-31 12:31:58 -0700533 private Bitmap safeCreateBitmap(int width, int height) {
534 if (width <= 0 || height <= 0) {
535 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
536 + ", height: " + height);
537 return null;
538 }
539 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
540 }
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}