blob: 7edaa7f3091f0101fba7511acaeecf1f02d48740 [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;
Michael Kolbc3af0672011-08-09 10:24:41 -070026import android.graphics.Matrix;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070027import android.graphics.Rect;
John Reck9c5004e2011-10-07 16:00:12 -070028import android.os.Message;
Michael Kolb376b5412010-12-15 11:52:57 -080029import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080030import android.view.ActionMode;
Michael Kolba4183062011-01-16 10:43:21 -080031import android.view.KeyEvent;
Michael Kolbc3af0672011-08-09 10:24:41 -070032import android.view.LayoutInflater;
Michael Kolb66706532010-12-12 19:50:22 -080033import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070034import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080035import android.view.View;
Michael Kolb30adae62011-07-29 13:37:05 -070036import android.view.accessibility.AccessibilityEvent;
Enrico Ros1f5a0952014-11-18 20:15:48 -080037import android.view.animation.DecelerateInterpolator;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070038import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080039import org.codeaurora.swe.WebView;
Michael Kolbc3af0672011-08-09 10:24:41 -070040import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080041
Bijan Amirzada41242f22014-03-21 12:12:18 -070042import 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;
Vivek Sekhar60eb9802014-07-21 19:13:33 -070055 private Activity mBrowser;
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -080056 private boolean mNavScreenRequested = false;
Michael Kolb66706532010-12-12 19:50:22 -080057
Michael Kolb08a687a2011-04-22 16:13:02 -070058 boolean mAnimating;
yijunx.zhu2230a312012-07-27 13:37:00 -040059 boolean mShowNav = false;
Michael Kolb66706532010-12-12 19:50:22 -080060
Vivek Sekhar60eb9802014-07-21 19:13:33 -070061
Michael Kolb66706532010-12-12 19:50:22 -080062 /**
63 * @param browser
64 * @param controller
65 */
66 public PhoneUi(Activity browser, UiController controller) {
67 super(browser, controller);
John Reck0f602f32011-07-07 15:38:43 -070068 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
Vivek Sekhar60eb9802014-07-21 19:13:33 -070069 mBrowser = browser;
John Reck285ef042011-02-11 15:44:17 -080070 }
Michael Kolb66706532010-12-12 19:50:22 -080071
John Reck285ef042011-02-11 15:44:17 -080072 @Override
Michael Kolb66706532010-12-12 19:50:22 -080073 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080074 hideTitleBar();
Karthikeyan Periasamy5b8f5752015-01-13 16:00:27 -080075 // Free the allocated memory for GC to clear it from the heap.
76 mAnimScreen = null;
Michael Kolb66706532010-12-12 19:50:22 -080077 }
78
79 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -070080 public void editUrl(boolean clearInput, boolean forceIME) {
yijunx.zhu2230a312012-07-27 13:37:00 -040081 //Do nothing while at Nav show screen.
82 if (mShowNav) return;
Michael Kolb1f9b3562012-04-24 14:38:34 -070083 super.editUrl(clearInput, forceIME);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080084 }
85
86 @Override
Michael Kolb66706532010-12-12 19:50:22 -080087 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070088 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070089 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070090 return true;
Michael Kolb66706532010-12-12 19:50:22 -080091 }
92 return super.onBackKey();
93 }
94
John Reckcc0059c2011-10-07 13:53:13 -070095 private boolean showingNavScreen() {
96 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
97 }
98
Michael Kolb66706532010-12-12 19:50:22 -080099 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700100 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700101 return false;
102 }
103
104 @Override
John Reck30c714c2010-12-16 17:30:34 -0800105 public void onProgressChanged(Tab tab) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700106 super.onProgressChanged(tab);
John Reck9c5004e2011-10-07 16:00:12 -0700107 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
108 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
109 }
110 }
111
112 @Override
113 protected void handleMessage(Message msg) {
114 super.handleMessage(msg);
115 if (msg.what == MSG_INIT_NAVSCREEN) {
116 if (mNavScreen == null) {
117 mNavScreen = new NavScreen(mActivity, mUiController, this);
118 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
119 mNavScreen.setVisibility(View.GONE);
120 }
121 if (mAnimScreen == null) {
122 mAnimScreen = new AnimScreen(mActivity);
123 // initialize bitmaps
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700124 //mAnimScreen.set(getTitleBar(), getWebView());
John Reck9c5004e2011-10-07 16:00:12 -0700125 }
126 }
Michael Kolb66706532010-12-12 19:50:22 -0800127 }
128
129 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700130 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700131 mTitleBar.cancelTitleBarAnimation(true);
132 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700133 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400134
135 //if at Nav screen show, detach tab like what showNavScreen() do.
136 if (mShowNav) {
137 detachTab(mActiveTab);
138 }
139
Michael Kolbfdb70242011-03-24 09:41:11 -0700140 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800141 // TabControl.setCurrentTab has been called before this,
142 // so the tab is guaranteed to have a webview
143 if (view == null) {
144 Log.e(LOGTAG, "active tab with no webview detected");
145 return;
146 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700147 // Request focus on the top window.
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700148 view.setTitleBar(mTitleBar);
149
Michael Kolb629b22c2011-07-13 16:26:47 -0700150 // update nav bar state
151 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700152 updateLockIconToLatest(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700153 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800154 }
155
Michael Kolb66706532010-12-12 19:50:22 -0800156 // menu handling callbacks
157
158 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700159 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700160 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700161 return true;
162 }
163
164 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700165 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800166 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
167 if (bm != null) {
168 bm.setVisible(!showingNavScreen());
169 }
Michael Kolb315d5022011-10-13 12:47:11 -0700170 MenuItem info = menu.findItem(R.id.page_info_menu_id);
171 if (info != null) {
172 info.setVisible(false);
173 }
Pankaj Garg49b79252014-11-07 17:33:41 -0800174
John Reckcc0059c2011-10-07 13:53:13 -0700175 if (showingNavScreen()) {
Pankaj Garg49b79252014-11-07 17:33:41 -0800176 setMenuItemVisibility(menu, R.id.history_menu_id, false);
177 setMenuItemVisibility(menu, R.id.find_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700178 menu.setGroupVisible(R.id.LIVE_MENU, false);
Pankaj Garg49b79252014-11-07 17:33:41 -0800179 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700180 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700181 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700182 }
183 }
184
185 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700186 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700187 if (showingNavScreen()
188 && (item.getItemId() != R.id.history_menu_id)
189 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700190 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700191 }
192 return false;
193 }
194
195 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800196 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800197 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800198 }
199
200 @Override
201 public void onContextMenuClosed(Menu menu, boolean inLoad) {
202 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800203 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800204 }
205 }
206
207 // action mode callbacks
208
209 @Override
210 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700211 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700212 if (!isEditingUrl()) {
213 hideTitleBar();
214 }
Michael Kolb66706532010-12-12 19:50:22 -0800215 }
216
Michael Kolba4183062011-01-16 10:43:21 -0800217 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700218 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700219 super.onActionModeFinished(inLoad);
Michael Kolbc16c5952011-03-29 15:37:03 -0700220 if (inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700221 showTitleBar();
222 }
John Reck6cda7b12011-03-18 15:57:13 -0700223 }
224
225 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700226 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700227 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700228 }
229
230 @Override
231 public void showWeb(boolean animate) {
232 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700233 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700234 }
235
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800236 //Unblock touch events
237 private void unblockEvents() {
238 mUiController.setBlockEvents(false);
239 }
240 //Block touch events
241 private void blockEvents() {
242 mUiController.setBlockEvents(true);
243 }
244
245 @Override
246 public void cancelNavScreenRequest() {
247 mNavScreenRequested = false;
248 }
249
Michael Kolbf2055602011-04-09 17:20:03 -0700250 void showNavScreen() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700251 WebView webView = getWebView();
252 if (webView != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800253 blockEvents();
254 mNavScreenRequested = true;
Sagar Dhawan01493f62015-02-09 11:03:19 -0800255 webView.getContentBitmapAsync(1.0f,
256 new Rect(),
257 new ValueCallback<Bitmap>() {
258 @Override
259 public void onReceiveValue(Bitmap bitmap) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800260
261 // If something interrupted the NavScreen request, discard
262 // the callback
263 if (mNavScreenRequested) {
264 onShowNavScreenContinue(bitmap);
265 } else {
266 unblockEvents();
267 }
268
Sagar Dhawan01493f62015-02-09 11:03:19 -0800269 }
270 });
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700271 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700272 }
273
Sagar Dhawan01493f62015-02-09 11:03:19 -0800274
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800275
276 void onShowNavScreenContinue(Bitmap viewportBitmap) {
277 dismissIME();
278 mShowNav = true;
279 mNavScreenRequested = false;
John Reckcc0059c2011-10-07 13:53:13 -0700280 if (mNavScreen == null) {
281 mNavScreen = new NavScreen(mActivity, mUiController, this);
282 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
283 } else {
284 mNavScreen.setVisibility(View.VISIBLE);
285 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700286 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700287 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700288 mActiveTab.capture();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800289 if (mAnimScreen == null) {
290 mAnimScreen = new AnimScreen(mActivity);
291 } else {
292 mAnimScreen.mMain.setAlpha(1f);
293 mAnimScreen.setScaleFactor(1f);
294 }
295 mAnimScreen.set(getTitleBar(), viewportBitmap);
296 if (mAnimScreen.mMain.getParent() == null) {
297 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
298 }
Michael Kolbf2055602011-04-09 17:20:03 -0700299 mCustomViewContainer.setVisibility(View.VISIBLE);
300 mCustomViewContainer.bringToFront();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800301 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
302 mContentView.getHeight());
303 int fromLeft = 0;
304 int fromTop = getTitleBar().getHeight();
305 int fromRight = mContentView.getWidth();
306 int fromBottom = mContentView.getHeight();
307 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
308 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
309 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
310 int toLeft = (mContentView.getWidth() - width) / 2;
311 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
312 int toRight = toLeft + width;
313 int toBottom = toTop + height;
314 float toScaleFactor = width / (float) mContentView.getWidth();
315 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
316 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
317 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
318 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
319 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, toScaleFactor);
320 ObjectAnimator navTabsIn = mNavScreen.createToolbarInAnimator();
321 mAnimScreen.mContent.layout(fromLeft, fromTop, fromRight, fromBottom);
322 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700323
Sagar Dhawan01493f62015-02-09 11:03:19 -0800324 AnimatorSet inanim = new AnimatorSet();
325 inanim.playTogether(tx, ty, tr, tb, sx, navTabsIn);
326 inanim.setInterpolator(new DecelerateInterpolator());
327 inanim.setDuration(200);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800328
Sagar Dhawan01493f62015-02-09 11:03:19 -0800329 ObjectAnimator disappear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 1f, 0f);
330 disappear.setInterpolator(new DecelerateInterpolator());
331 disappear.setDuration(100);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800332
Sagar Dhawan01493f62015-02-09 11:03:19 -0800333 AnimatorSet set1 = new AnimatorSet();
334 set1.addListener(new AnimatorListenerAdapter() {
335 @Override
336 public void onAnimationStart(Animator animation) {
337 mContentView.setVisibility(View.GONE);
338 }
339 @Override
340 public void onAnimationEnd(Animator anim) {
341 mCustomViewContainer.removeView(mAnimScreen.mMain);
342 finishAnimationIn();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800343 unblockEvents();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800344 }
345 });
346 set1.playSequentially(inanim, disappear);
347 set1.start();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800348 unblockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700349 }
350
351 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700352 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700353 // notify accessibility manager about the screen change
354 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
355 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
356 }
Michael Kolbf2055602011-04-09 17:20:03 -0700357 }
358
Michael Kolba3194d02011-09-07 11:23:51 -0700359 void hideNavScreen(int position, boolean animate) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800360
yijunx.zhu2230a312012-07-27 13:37:00 -0400361 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700362 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700363 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700364 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700365 if (tab != null) {
366 setActiveTab(tab);
367 } else 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 }
Michael Kolba3194d02011-09-07 11:23:51 -0700375 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700376 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700377 if (mTabControl.getTabCount() > 0) {
378 // use a fallback tab
379 setActiveTab(mTabControl.getCurrentTab());
380 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700381 mContentView.setVisibility(View.VISIBLE);
382 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700383 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700384 }
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800385 blockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700386 mUiController.setActiveTab(tab);
387 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700388 if (mAnimScreen == null) {
389 mAnimScreen = new AnimScreen(mActivity);
390 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700391 ImageView target = tabview.mImage;
392 int width = target.getDrawable().getIntrinsicWidth();
393 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700394 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700395 if (bm == null)
396 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
397 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800398 if (mAnimScreen.mMain.getParent() == null) {
399 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
400 }
John Reckcc0059c2011-10-07 13:53:13 -0700401 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700402 mContentView.getHeight());
Enrico Ros80c045a2014-11-24 15:23:12 -0800403 mNavScreen.getScroller().finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700404 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700405 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700406 int toRight = mContentView.getWidth();
Enrico Ros80c045a2014-11-24 15:23:12 -0800407 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.getScroller().getScrollX();
408 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.getScroller().getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700409 int fromRight = fromLeft + width;
410 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700411 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700412 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700413 mAnimScreen.mContent.setLeft(fromLeft);
414 mAnimScreen.mContent.setTop(fromTop);
415 mAnimScreen.mContent.setRight(fromRight);
416 mAnimScreen.mContent.setBottom(fromBottom);
417 mAnimScreen.setScaleFactor(1f);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800418 //ObjectAnimator fade2 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
419 //fade2.setDuration(100);
420 AnimatorSet set = new AnimatorSet();
421 ObjectAnimator animAppear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
422 animAppear.setDuration(100);
423 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
424 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
425 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
426 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
427 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, scaleFactor);
428 set.playTogether(animAppear, l, t, r, b, scale);
429 set.setInterpolator(new DecelerateInterpolator());
430 set.setDuration(200);
431 set.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700432 @Override
433 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400434 checkTabReady();
435 }
436 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800437 set.start();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400438 }
439
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700440
441 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400442 private void checkTabReady() {
443 boolean isready = true;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800444 boolean zeroTries = mNumTries == 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400445 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700446 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400447 if (tab == null)
448 isready = false;
449 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700450 webview = (BrowserWebView)tab.getWebView();
451 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400452 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700453 }
454 else if (webview.hasCrashed()) {
455 webview.reload();
456 isready = true;
457 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400458 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700459 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400460 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700461 // Post only when not ready and not crashed
462 if (!isready && mNumTries++ < 150) {
463 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400464 public void run() {
465 checkTabReady();
466 }
467 }, 17); //WebView is not ready. check again in for next frame.
468 return;
469 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700470 mNumTries = 0;
471 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800472 // fast path: don't wait if we've been ready for a while
473 if (zeroTries) {
474 fadeOutCustomViewContainer(hasCrashed);
475 return;
476 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700477 mCustomViewContainer.postDelayed(new Runnable() {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800478 public void run() {
479 fadeOutCustomViewContainer(hasCrashed);
480 }
481 }, 32); //WebView is ready, but give it extra 2 frame's time to display and finish the swaps
Matthew Hui1eb7a832014-04-15 12:20:58 -0400482 }
483
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700484 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400485 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700486 if (hasCrashed)
487 otheralpha.setDuration(300);
488 else
489 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400490 otheralpha.addListener(new AnimatorListenerAdapter() {
491 @Override
492 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700493 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700494 finishAnimateOut();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800495 unblockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700496 }
497 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800498 otheralpha.setInterpolator(new DecelerateInterpolator());
Matthew Hui1eb7a832014-04-15 12:20:58 -0400499 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700500 }
501
Michael Kolbbf32cd02011-08-16 15:07:04 -0700502 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700503 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700504 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700505 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700506 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700507 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700508 }
509
John Reck6b4bd5f2011-07-12 10:14:38 -0700510 @Override
511 public boolean needsRestoreAllTabs() {
512 return false;
513 }
514
Michael Kolb20be26d2011-07-18 16:38:02 -0700515 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700516 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700517 showNavScreen();
518 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700519 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700520 }
521 }
522
John Reck1cf4b792011-07-26 10:22:22 -0700523 @Override
524 public boolean shouldCaptureThumbnails() {
525 return true;
526 }
527
Michael Kolbc3af0672011-08-09 10:24:41 -0700528 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700529
Michael Kolbc3af0672011-08-09 10:24:41 -0700530 private View mMain;
Michael Kolbc3af0672011-08-09 10:24:41 -0700531 private ImageView mContent;
532 private float mScale;
533
John Reckcc0059c2011-10-07 13:53:13 -0700534 public AnimScreen(Context ctx) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800535 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen, null);
536 mMain.setOnClickListener(new View.OnClickListener() {
537 @Override
538 public void onClick(View v) {
539 // just eat clicks when this view is visible
540 }
541 });
542 mContent = (ImageView) mMain.findViewById(R.id.anim_screen_content);
John Reckcc0059c2011-10-07 13:53:13 -0700543 mContent.setScaleType(ImageView.ScaleType.MATRIX);
544 mContent.setImageMatrix(new Matrix());
545 mScale = 1.0f;
546 setScaleFactor(getScaleFactor());
547 }
548
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700549 public void set(TitleBar tbar, Bitmap viewportBitmap) {
550 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700551 return;
552 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700553 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700554 }
555
Enrico Ros1f5a0952014-11-18 20:15:48 -0800556 /*private Bitmap safeCreateBitmap(int width, int height) {
John Recka98c83b2011-10-31 12:31:58 -0700557 if (width <= 0 || height <= 0) {
558 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
559 + ", height: " + height);
560 return null;
561 }
562 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800563 }*/
John Recka98c83b2011-10-31 12:31:58 -0700564
John Reckcc0059c2011-10-07 13:53:13 -0700565 public void set(Bitmap image) {
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();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800572 m.postScale(sf, sf);
Michael Kolbc3af0672011-08-09 10:24:41 -0700573 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}