blob: 5aefc9214c11043d57b3ea2b1c8111439e214981 [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) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700237 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700238 if (!isEditingUrl()) {
239 hideTitleBar();
240 }
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242
Michael Kolba4183062011-01-16 10:43:21 -0800243 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700244 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700245 super.onActionModeFinished(inLoad);
Michael Kolbc16c5952011-03-29 15:37:03 -0700246 if (inLoad) {
247 if (mUseQuickControls) {
248 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700249 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700250 showTitleBar();
251 }
John Reck6cda7b12011-03-18 15:57:13 -0700252 }
253
254 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700255 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700256 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700257 }
258
259 @Override
260 public void showWeb(boolean animate) {
261 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700262 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700263 }
264
Michael Kolbf2055602011-04-09 17:20:03 -0700265 void showNavScreen() {
yijunx.zhu2230a312012-07-27 13:37:00 -0400266 mShowNav = true;
Michael Kolbc3af0672011-08-09 10:24:41 -0700267 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700268 if (mNavScreen == null) {
269 mNavScreen = new NavScreen(mActivity, mUiController, this);
270 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
271 } else {
272 mNavScreen.setVisibility(View.VISIBLE);
273 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700274 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700275 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700276 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700277 if (mAnimScreen == null) {
278 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700279 } else {
280 mAnimScreen.mMain.setAlpha(1f);
281 mAnimScreen.mTitle.setAlpha(1f);
282 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700283 }
284 mAnimScreen.set(getTitleBar(), getWebView());
Michael Kolba30e4fb2011-11-07 17:32:01 -0800285 if (mAnimScreen.mMain.getParent() == null) {
286 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
287 }
Michael Kolbf2055602011-04-09 17:20:03 -0700288 mCustomViewContainer.setVisibility(View.VISIBLE);
289 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700290 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
291 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700292 int fromLeft = 0;
293 int fromTop = getTitleBar().getHeight();
294 int fromRight = mContentView.getWidth();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800295 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
296 int fromBottom = mContentView.getHeight() + fixedTbarHeight;
Michael Kolba3194d02011-09-07 11:23:51 -0700297 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
298 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700299 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700300 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700301 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700302 int toRight = toLeft + width;
303 int toBottom = toTop + height;
304 float scaleFactor = width / (float) mContentView.getWidth();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800305 // SWE: Detaching the active tab results flashing screen with SWE.
306 // Not detaching the tab doesn't seem to have any issues.
307 //detachTab(mActiveTab);
Michael Kolbc3af0672011-08-09 10:24:41 -0700308 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700309 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700310 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700311 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700312 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700313 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700314 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700315 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700316 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700317 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700318 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700319 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700320 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700321 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700322 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700323 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
324 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700325 blend1.setDuration(100);
326
327 inanim.playTogether(tx, ty, tr, tb, sx, title);
328 inanim.setDuration(200);
329 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700330 @Override
331 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700332 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700333 finishAnimationIn();
334 mUiController.setBlockEvents(false);
335 }
336 });
Michael Kolba3194d02011-09-07 11:23:51 -0700337 set1.playSequentially(inanim, blend1);
338 set1.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700339 }
340
341 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700342 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700343 // notify accessibility manager about the screen change
344 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
345 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
346 }
Michael Kolbf2055602011-04-09 17:20:03 -0700347 }
348
Michael Kolba3194d02011-09-07 11:23:51 -0700349 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400350 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700351 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700352 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700353 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700354 if (tab != null) {
355 setActiveTab(tab);
356 } else if (mTabControl.getTabCount() > 0) {
357 // use a fallback tab
358 setActiveTab(mTabControl.getCurrentTab());
359 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700360 mContentView.setVisibility(View.VISIBLE);
361 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700362 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700363 }
Michael Kolba3194d02011-09-07 11:23:51 -0700364 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700365 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700366 if (mTabControl.getTabCount() > 0) {
367 // use a fallback tab
368 setActiveTab(mTabControl.getCurrentTab());
369 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700370 mContentView.setVisibility(View.VISIBLE);
371 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700372 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700373 }
374 mUiController.setBlockEvents(true);
375 mUiController.setActiveTab(tab);
376 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700377 if (mAnimScreen == null) {
378 mAnimScreen = new AnimScreen(mActivity);
379 }
380 mAnimScreen.set(tab.getScreenshot());
Michael Kolb0755fcd2012-01-10 10:19:50 -0800381 if (mAnimScreen.mMain.getParent() == null) {
382 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
383 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800384 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
John Reckcc0059c2011-10-07 13:53:13 -0700385 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800386 mContentView.getHeight() + fixedTbarHeight);
Michael Kolba3194d02011-09-07 11:23:51 -0700387 mNavScreen.mScroller.finishScroller();
388 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700389 int toLeft = 0;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800390 int toTop = 0;
391 if (mTitleBar.isFixed()) {
392 toTop = fixedTbarHeight;
393 } else {
394 toTop = (tab.getWebView() != null) ? tab.getWebView().getVisibleTitleHeight() : 0;
395 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700396 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700397 int width = target.getDrawable().getIntrinsicWidth();
398 int height = target.getDrawable().getIntrinsicHeight();
399 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
400 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Michael Kolbc3af0672011-08-09 10:24:41 -0700401 int fromRight = fromLeft + width;
402 int fromBottom = fromTop + height;
403 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700404 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700405 mAnimScreen.mContent.setLeft(fromLeft);
406 mAnimScreen.mContent.setTop(fromTop);
407 mAnimScreen.mContent.setRight(fromRight);
408 mAnimScreen.mContent.setBottom(fromBottom);
409 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700410 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700411 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700412 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700413 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700414 set1.setDuration(100);
415 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700416 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700417 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700418 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700419 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700420 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700421 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700422 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700423 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700424 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700425 1f, scaleFactor);
Michael Kolbc3af0672011-08-09 10:24:41 -0700426 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700427 otheralpha.setDuration(100);
428 set2.playTogether(l, t, r, b, scale);
429 set2.setDuration(200);
430 AnimatorSet combo = new AnimatorSet();
431 combo.playSequentially(set1, set2, otheralpha);
432 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700433 @Override
434 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700435 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700436 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700437 mUiController.setBlockEvents(false);
438 }
439 });
Michael Kolba3194d02011-09-07 11:23:51 -0700440 combo.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700441 }
442
Michael Kolbbf32cd02011-08-16 15:07:04 -0700443 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700444 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700445 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700446 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700447 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700448 }
449
John Reck6b4bd5f2011-07-12 10:14:38 -0700450 @Override
451 public boolean needsRestoreAllTabs() {
452 return false;
453 }
454
Michael Kolb20be26d2011-07-18 16:38:02 -0700455 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700456 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700457 showNavScreen();
458 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700459 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700460 }
461 }
462
John Reck1cf4b792011-07-26 10:22:22 -0700463 @Override
464 public boolean shouldCaptureThumbnails() {
465 return true;
466 }
467
Michael Kolbc3af0672011-08-09 10:24:41 -0700468 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700469
Michael Kolbc3af0672011-08-09 10:24:41 -0700470 private View mMain;
471 private ImageView mTitle;
472 private ImageView mContent;
473 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700474 private Bitmap mTitleBarBitmap;
475 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700476
John Reckcc0059c2011-10-07 13:53:13 -0700477 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700478 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
479 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700480 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700481 mContent = (ImageView) mMain.findViewById(R.id.content);
482 mContent.setScaleType(ImageView.ScaleType.MATRIX);
483 mContent.setImageMatrix(new Matrix());
484 mScale = 1.0f;
485 setScaleFactor(getScaleFactor());
486 }
487
488 public void set(TitleBar tbar, WebView web) {
John Reck62077d82011-10-13 15:17:50 -0700489 if (tbar == null || web == null) {
490 return;
491 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800492 int embTbarHeight = tbar.getEmbeddedHeight();
493 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
494 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700495 if (mTitleBarBitmap == null
496 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800497 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700498 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800499 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700500 }
John Recka98c83b2011-10-31 12:31:58 -0700501 if (mTitleBarBitmap != null) {
502 Canvas c = new Canvas(mTitleBarBitmap);
503 tbar.draw(c);
504 c.setBitmap(null);
505 }
John Reck1adf0302011-10-11 10:58:12 -0700506 } else {
507 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700508 }
John Reckcc0059c2011-10-07 13:53:13 -0700509 mTitle.setImageBitmap(mTitleBarBitmap);
510 mTitle.setVisibility(View.VISIBLE);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800511 // SWE: WebView.draw() wouldn't draw anything if SurfaceView is enabled.
512 mContentBitmap = web.getViewportBitmap();
513 if (mContentBitmap == null) {
514 int h = web.getHeight() - embTbarHeight;
515 if (mContentBitmap == null
516 || mContentBitmap.getWidth() != web.getWidth()
517 || mContentBitmap.getHeight() != h) {
518 mContentBitmap = safeCreateBitmap(web.getWidth(), h);
519 }
520 if (mContentBitmap != null) {
521 Canvas c = new Canvas(mContentBitmap);
522 int tx = web.getScrollX();
523 int ty = web.getScrollY();
524 c.translate(-tx, -ty - embTbarHeight);
525 web.draw(c);
526 c.setBitmap(null);
527 }
John Recka98c83b2011-10-31 12:31:58 -0700528 }
John Reckcc0059c2011-10-07 13:53:13 -0700529 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700530 }
531
John Recka98c83b2011-10-31 12:31:58 -0700532 private Bitmap safeCreateBitmap(int width, int height) {
533 if (width <= 0 || height <= 0) {
534 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
535 + ", height: " + height);
536 return null;
537 }
538 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
539 }
540
John Reckcc0059c2011-10-07 13:53:13 -0700541 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700542 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700543 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700544 }
545
John Reckcc0059c2011-10-07 13:53:13 -0700546 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700547 mScale = sf;
548 Matrix m = new Matrix();
549 m.postScale(sf,sf);
550 mContent.setImageMatrix(m);
551 }
552
John Reckcc0059c2011-10-07 13:53:13 -0700553 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700554 return mScale;
555 }
556
John Reck3ba45532011-08-11 16:26:53 -0700557 }
558
Michael Kolb66706532010-12-12 19:50:22 -0800559}