blob: ca478c2084be7bd732968ed9275cd7879cfcc9ff [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;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070028import android.graphics.Rect;
John Reck9c5004e2011-10-07 16:00:12 -070029import android.os.Message;
Michael Kolb376b5412010-12-15 11:52:57 -080030import android.util.Log;
John Reckaae029c2011-09-16 14:36:39 -070031import android.util.TypedValue;
Michael Kolb66706532010-12-12 19:50:22 -080032import android.view.ActionMode;
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 Kolb30adae62011-07-29 13:37:05 -070038import android.view.accessibility.AccessibilityEvent;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070039import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080040import org.codeaurora.swe.WebView;
Michael Kolbc3af0672011-08-09 10:24:41 -070041import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080042
Bijan Amirzada41242f22014-03-21 12:12:18 -070043import com.android.browser.R;
44import com.android.browser.UrlInputView.StateListener;
Michael Kolb629b22c2011-07-13 16:26:47 -070045
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 Kolbf2055602011-04-09 17:20:03 -070054 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070055 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070056 private NavigationBarPhone mNavigationBar;
Vivek Sekhar60eb9802014-07-21 19:13:33 -070057 private Activity mBrowser;
Michael Kolb66706532010-12-12 19:50:22 -080058
Michael Kolb08a687a2011-04-22 16:13:02 -070059 boolean mAnimating;
yijunx.zhu2230a312012-07-27 13:37:00 -040060 boolean mShowNav = false;
Michael Kolb66706532010-12-12 19:50:22 -080061
Vivek Sekhar60eb9802014-07-21 19:13:33 -070062
Michael Kolb66706532010-12-12 19:50:22 -080063 /**
64 * @param browser
65 * @param controller
66 */
67 public PhoneUi(Activity browser, UiController controller) {
68 super(browser, controller);
John Reck0f602f32011-07-07 15:38:43 -070069 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
Vivek Sekhar60eb9802014-07-21 19:13:33 -070070 mBrowser = browser;
John Reck285ef042011-02-11 15:44:17 -080071 }
Michael Kolb66706532010-12-12 19:50:22 -080072
John Reck285ef042011-02-11 15:44:17 -080073 @Override
Michael Kolb66706532010-12-12 19:50:22 -080074 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080075 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080076 }
77
78 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -070079 public void editUrl(boolean clearInput, boolean forceIME) {
yijunx.zhu2230a312012-07-27 13:37:00 -040080 //Do nothing while at Nav show screen.
81 if (mShowNav) return;
Michael Kolb1f9b3562012-04-24 14:38:34 -070082 super.editUrl(clearInput, forceIME);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080083 }
84
85 @Override
Michael Kolb66706532010-12-12 19:50:22 -080086 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070087 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070088 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070089 return true;
Michael Kolb66706532010-12-12 19:50:22 -080090 }
91 return super.onBackKey();
92 }
93
John Reckcc0059c2011-10-07 13:53:13 -070094 private boolean showingNavScreen() {
95 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
96 }
97
Michael Kolb66706532010-12-12 19:50:22 -080098 @Override
Michael Kolbf2055602011-04-09 17:20:03 -070099 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700100 return false;
101 }
102
103 @Override
John Reck30c714c2010-12-16 17:30:34 -0800104 public void onProgressChanged(Tab tab) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700105 super.onProgressChanged(tab);
John Reck9c5004e2011-10-07 16:00:12 -0700106 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
107 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
108 }
109 }
110
111 @Override
112 protected void handleMessage(Message msg) {
113 super.handleMessage(msg);
114 if (msg.what == MSG_INIT_NAVSCREEN) {
115 if (mNavScreen == null) {
116 mNavScreen = new NavScreen(mActivity, mUiController, this);
117 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
118 mNavScreen.setVisibility(View.GONE);
119 }
120 if (mAnimScreen == null) {
121 mAnimScreen = new AnimScreen(mActivity);
122 // initialize bitmaps
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700123 //mAnimScreen.set(getTitleBar(), getWebView());
John Reck9c5004e2011-10-07 16:00:12 -0700124 }
125 }
Michael Kolb66706532010-12-12 19:50:22 -0800126 }
127
128 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700129 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700130 mTitleBar.cancelTitleBarAnimation(true);
131 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700132 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400133
134 //if at Nav screen show, detach tab like what showNavScreen() do.
135 if (mShowNav) {
136 detachTab(mActiveTab);
137 }
138
Michael Kolbfdb70242011-03-24 09:41:11 -0700139 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800140 // TabControl.setCurrentTab has been called before this,
141 // so the tab is guaranteed to have a webview
142 if (view == null) {
143 Log.e(LOGTAG, "active tab with no webview detected");
144 return;
145 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700146 // Request focus on the top window.
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700147 view.setTitleBar(mTitleBar);
148
Michael Kolb629b22c2011-07-13 16:26:47 -0700149 // update nav bar state
150 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700151 updateLockIconToLatest(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700152 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800153 }
154
Michael Kolb66706532010-12-12 19:50:22 -0800155 // menu handling callbacks
156
157 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700158 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700159 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700160 return true;
161 }
162
Pankaj Garg49b79252014-11-07 17:33:41 -0800163 private void setMenuItemVisibility(Menu menu, int id,
164 boolean visibility) {
165 MenuItem item = menu.findItem(id);
166 if (item != null) {
167 item.setVisible(visibility);
168 }
169 }
170
Michael Kolb3ca12752011-07-20 13:52:25 -0700171 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700172 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800173 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
174 if (bm != null) {
175 bm.setVisible(!showingNavScreen());
176 }
Michael Kolb315d5022011-10-13 12:47:11 -0700177 MenuItem info = menu.findItem(R.id.page_info_menu_id);
178 if (info != null) {
179 info.setVisible(false);
180 }
Pankaj Garg49b79252014-11-07 17:33:41 -0800181
John Reckcc0059c2011-10-07 13:53:13 -0700182 if (showingNavScreen()) {
Pankaj Garg49b79252014-11-07 17:33:41 -0800183 setMenuItemVisibility(menu, R.id.history_menu_id, false);
184 setMenuItemVisibility(menu, R.id.find_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700185 menu.setGroupVisible(R.id.LIVE_MENU, false);
Pankaj Garg49b79252014-11-07 17:33:41 -0800186 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700187 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700188 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700189 }
190 }
191
192 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700193 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700194 if (showingNavScreen()
195 && (item.getItemId() != R.id.history_menu_id)
196 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700197 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700198 }
199 return false;
200 }
201
202 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800203 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800204 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800205 }
206
207 @Override
208 public void onContextMenuClosed(Menu menu, boolean inLoad) {
209 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800210 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800211 }
212 }
213
214 // action mode callbacks
215
216 @Override
217 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700218 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700219 if (!isEditingUrl()) {
220 hideTitleBar();
221 }
Michael Kolb66706532010-12-12 19:50:22 -0800222 }
223
Michael Kolba4183062011-01-16 10:43:21 -0800224 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700225 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700226 super.onActionModeFinished(inLoad);
Michael Kolbc16c5952011-03-29 15:37:03 -0700227 if (inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700228 showTitleBar();
229 }
John Reck6cda7b12011-03-18 15:57:13 -0700230 }
231
232 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700233 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700234 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700235 }
236
237 @Override
238 public void showWeb(boolean animate) {
239 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700240 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700241 }
242
Michael Kolbf2055602011-04-09 17:20:03 -0700243 void showNavScreen() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700244 WebView webView = getWebView();
245 if (webView != null) {
246 mShowNav = true;
247 dismissIME();
248 mUiController.setBlockEvents(true);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700249
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700250 webView.getContentBitmapAsync(1.0f,
251 new Rect(),
252 new ValueCallback<Bitmap>() {
253 @Override
254 public void onReceiveValue(Bitmap bitmap) {
255 onShowNavScreenContinue(bitmap);
256 }
257 });
258 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700259 }
260
261 void onShowNavScreenContinue(Bitmap viewportBitmap) {
262
John Reckcc0059c2011-10-07 13:53:13 -0700263 if (mNavScreen == null) {
264 mNavScreen = new NavScreen(mActivity, mUiController, this);
265 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
266 } else {
267 mNavScreen.setVisibility(View.VISIBLE);
268 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700269 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700270 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700271 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700272 if (mAnimScreen == null) {
273 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700274 } else {
275 mAnimScreen.mMain.setAlpha(1f);
276 mAnimScreen.mTitle.setAlpha(1f);
277 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700278 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700279 mAnimScreen.set(getTitleBar(), viewportBitmap);
Michael Kolba30e4fb2011-11-07 17:32:01 -0800280 if (mAnimScreen.mMain.getParent() == null) {
281 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
282 }
Michael Kolbf2055602011-04-09 17:20:03 -0700283 mCustomViewContainer.setVisibility(View.VISIBLE);
284 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700285 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
286 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700287 int fromLeft = 0;
288 int fromTop = getTitleBar().getHeight();
289 int fromRight = mContentView.getWidth();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700290 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700291 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
292 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700293 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700294 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700295 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700296 int toRight = toLeft + width;
297 int toBottom = toTop + height;
298 float scaleFactor = width / (float) mContentView.getWidth();
Michael Kolbc3af0672011-08-09 10:24:41 -0700299 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700300 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700301 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700302 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700303 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700304 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700305 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700306 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700307 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700308 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700309 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700310 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700311 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700312 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700313 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700314 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
315 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700316 blend1.setDuration(100);
317
318 inanim.playTogether(tx, ty, tr, tb, sx, title);
319 inanim.setDuration(200);
320 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700321 @Override
322 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700323 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700324 finishAnimationIn();
325 mUiController.setBlockEvents(false);
326 }
327 });
Michael Kolba3194d02011-09-07 11:23:51 -0700328 set1.playSequentially(inanim, blend1);
329 set1.start();
Bijan Amirzadafd75eae2014-04-17 17:09:12 -0700330 mUiController.setBlockEvents(false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700331 }
332
333 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700334 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700335 // notify accessibility manager about the screen change
336 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
337 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
338 }
Michael Kolbf2055602011-04-09 17:20:03 -0700339 }
340
Michael Kolba3194d02011-09-07 11:23:51 -0700341 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400342 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700343 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700344 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700345 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700346 if (tab != null) {
347 setActiveTab(tab);
348 } else if (mTabControl.getTabCount() > 0) {
349 // use a fallback tab
350 setActiveTab(mTabControl.getCurrentTab());
351 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700352 mContentView.setVisibility(View.VISIBLE);
353 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700354 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700355 }
Michael Kolba3194d02011-09-07 11:23:51 -0700356 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700357 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700358 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 }
366 mUiController.setBlockEvents(true);
367 mUiController.setActiveTab(tab);
368 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700369 if (mAnimScreen == null) {
370 mAnimScreen = new AnimScreen(mActivity);
371 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700372 ImageView target = tabview.mImage;
373 int width = target.getDrawable().getIntrinsicWidth();
374 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700375 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700376 if (bm == null)
377 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
378 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800379 if (mAnimScreen.mMain.getParent() == null) {
380 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
381 }
John Reckcc0059c2011-10-07 13:53:13 -0700382 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700383 mContentView.getHeight());
Michael Kolba3194d02011-09-07 11:23:51 -0700384 mNavScreen.mScroller.finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700385 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700386 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700387 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700388 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
389 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700390 int fromRight = fromLeft + width;
391 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700392 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700393 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700394 mAnimScreen.mContent.setLeft(fromLeft);
395 mAnimScreen.mContent.setTop(fromTop);
396 mAnimScreen.mContent.setRight(fromRight);
397 mAnimScreen.mContent.setBottom(fromBottom);
398 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700399 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700400 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700401 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700402 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700403 set1.setDuration(100);
404 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700405 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700406 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700407 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700408 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700409 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700410 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700411 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700412 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700413 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700414 1f, scaleFactor);
Michael Kolba3194d02011-09-07 11:23:51 -0700415 set2.playTogether(l, t, r, b, scale);
416 set2.setDuration(200);
417 AnimatorSet combo = new AnimatorSet();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400418 combo.playSequentially(set1, set2);
Michael Kolba3194d02011-09-07 11:23:51 -0700419 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700420 @Override
421 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400422 checkTabReady();
423 }
424 });
425 combo.start();
426 }
427
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700428
429 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400430 private void checkTabReady() {
431 boolean isready = true;
432 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700433 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400434 if (tab == null)
435 isready = false;
436 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700437 webview = (BrowserWebView)tab.getWebView();
438 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400439 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700440 }
441 else if (webview.hasCrashed()) {
442 webview.reload();
443 isready = true;
444 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400445 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700446 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400447 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700448 // Post only when not ready and not crashed
449 if (!isready && mNumTries++ < 150) {
450 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400451 public void run() {
452 checkTabReady();
453 }
454 }, 17); //WebView is not ready. check again in for next frame.
455 return;
456 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700457 mNumTries = 0;
458 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
459 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400460 public void run() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700461 fadeOutCustomViewContainer(hasCrashed);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400462 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700463 }, 33); //WebView is ready, but give it extra 2 frame's time to display and finish the swaps
Matthew Hui1eb7a832014-04-15 12:20:58 -0400464 }
465
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700466 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400467 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700468 if (hasCrashed)
469 otheralpha.setDuration(300);
470 else
471 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400472 otheralpha.addListener(new AnimatorListenerAdapter() {
473 @Override
474 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700475 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700476 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700477 mUiController.setBlockEvents(false);
478 }
479 });
Matthew Hui1eb7a832014-04-15 12:20:58 -0400480 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700481 }
482
Michael Kolbbf32cd02011-08-16 15:07:04 -0700483 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700484 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700485 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700486 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700487 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700488 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700489 }
490
John Reck6b4bd5f2011-07-12 10:14:38 -0700491 @Override
492 public boolean needsRestoreAllTabs() {
493 return false;
494 }
495
Michael Kolb20be26d2011-07-18 16:38:02 -0700496 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700497 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700498 showNavScreen();
499 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700500 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700501 }
502 }
503
John Reck1cf4b792011-07-26 10:22:22 -0700504 @Override
505 public boolean shouldCaptureThumbnails() {
506 return true;
507 }
508
Michael Kolbc3af0672011-08-09 10:24:41 -0700509 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700510
Michael Kolbc3af0672011-08-09 10:24:41 -0700511 private View mMain;
512 private ImageView mTitle;
513 private ImageView mContent;
514 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700515 private Bitmap mTitleBarBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700516
John Reckcc0059c2011-10-07 13:53:13 -0700517 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700518 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
519 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700520 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700521 mContent = (ImageView) mMain.findViewById(R.id.content);
522 mContent.setScaleType(ImageView.ScaleType.MATRIX);
523 mContent.setImageMatrix(new Matrix());
524 mScale = 1.0f;
525 setScaleFactor(getScaleFactor());
526 }
527
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700528 public void set(TitleBar tbar, Bitmap viewportBitmap) {
529 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700530 return;
531 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800532 int embTbarHeight = tbar.getEmbeddedHeight();
533 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
534 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700535 if (mTitleBarBitmap == null
536 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800537 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700538 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800539 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700540 }
John Recka98c83b2011-10-31 12:31:58 -0700541 if (mTitleBarBitmap != null) {
542 Canvas c = new Canvas(mTitleBarBitmap);
543 tbar.draw(c);
544 c.setBitmap(null);
545 }
John Reck1adf0302011-10-11 10:58:12 -0700546 } else {
547 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700548 }
John Reckcc0059c2011-10-07 13:53:13 -0700549 mTitle.setImageBitmap(mTitleBarBitmap);
550 mTitle.setVisibility(View.VISIBLE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700551
552 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700553 }
554
John Recka98c83b2011-10-31 12:31:58 -0700555 private Bitmap safeCreateBitmap(int width, int height) {
556 if (width <= 0 || height <= 0) {
557 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
558 + ", height: " + height);
559 return null;
560 }
561 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
562 }
563
John Reckcc0059c2011-10-07 13:53:13 -0700564 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700565 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700566 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700567 }
568
John Reckcc0059c2011-10-07 13:53:13 -0700569 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700570 mScale = sf;
571 Matrix m = new Matrix();
572 m.postScale(sf,sf);
573 mContent.setImageMatrix(m);
574 }
575
John Reckcc0059c2011-10-07 13:53:13 -0700576 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700577 return mScale;
578 }
579
John Reck3ba45532011-08-11 16:26:53 -0700580 }
581
Michael Kolb66706532010-12-12 19:50:22 -0800582}