blob: d606b794d089d401cfc091da8adb232843077047 [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;
John Reck9c5004e2011-10-07 16:00:12 -070027import android.os.Message;
Michael Kolb376b5412010-12-15 11:52:57 -080028import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080029import android.view.ActionMode;
Michael Kolba4183062011-01-16 10:43:21 -080030import android.view.KeyEvent;
Michael Kolbc3af0672011-08-09 10:24:41 -070031import android.view.LayoutInflater;
Michael Kolb66706532010-12-12 19:50:22 -080032import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070033import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080034import android.view.View;
Michael Kolb30adae62011-07-29 13:37:05 -070035import android.view.accessibility.AccessibilityEvent;
Enrico Ros1f5a0952014-11-18 20:15:48 -080036import android.view.animation.DecelerateInterpolator;
Michael Kolbc3af0672011-08-09 10:24:41 -070037import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080038
Bijan Amirzada41242f22014-03-21 12:12:18 -070039import com.android.browser.UrlInputView.StateListener;
Michael Kolb629b22c2011-07-13 16:26:47 -070040
Pankaj Garg79878492015-04-01 14:48:21 -070041import org.codeaurora.swe.WebView;
42
Michael Kolb66706532010-12-12 19:50:22 -080043/**
44 * Ui for regular phone screen sizes
45 */
46public class PhoneUi extends BaseUi {
47
48 private static final String LOGTAG = "PhoneUi";
John Reck9c5004e2011-10-07 16:00:12 -070049 private static final int MSG_INIT_NAVSCREEN = 100;
Michael Kolb66706532010-12-12 19:50:22 -080050
Michael Kolbf2055602011-04-09 17:20:03 -070051 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070052 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070053 private NavigationBarPhone mNavigationBar;
Vivek Sekhar60eb9802014-07-21 19:13:33 -070054 private Activity mBrowser;
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -080055 private boolean mNavScreenRequested = false;
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
Vivek Sekhar60eb9802014-07-21 19:13:33 -070060
Michael Kolb66706532010-12-12 19:50:22 -080061 /**
62 * @param browser
63 * @param controller
64 */
65 public PhoneUi(Activity browser, UiController controller) {
66 super(browser, controller);
John Reck0f602f32011-07-07 15:38:43 -070067 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
Vivek Sekhar60eb9802014-07-21 19:13:33 -070068 mBrowser = browser;
John Reck285ef042011-02-11 15:44:17 -080069 }
Michael Kolb66706532010-12-12 19:50:22 -080070
John Reck285ef042011-02-11 15:44:17 -080071 @Override
Michael Kolb66706532010-12-12 19:50:22 -080072 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080073 hideTitleBar();
Karthikeyan Periasamy5b8f5752015-01-13 16:00:27 -080074 // Free the allocated memory for GC to clear it from the heap.
75 mAnimScreen = null;
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
163 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700164 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800165 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
166 if (bm != null) {
167 bm.setVisible(!showingNavScreen());
168 }
Michael Kolb315d5022011-10-13 12:47:11 -0700169 MenuItem info = menu.findItem(R.id.page_info_menu_id);
170 if (info != null) {
171 info.setVisible(false);
172 }
Pankaj Garg49b79252014-11-07 17:33:41 -0800173
John Reckcc0059c2011-10-07 13:53:13 -0700174 if (showingNavScreen()) {
Pankaj Garg49b79252014-11-07 17:33:41 -0800175 setMenuItemVisibility(menu, R.id.history_menu_id, false);
176 setMenuItemVisibility(menu, R.id.find_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700177 menu.setGroupVisible(R.id.LIVE_MENU, false);
Pankaj Garg49b79252014-11-07 17:33:41 -0800178 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700179 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700180 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700181 }
182 }
183
184 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700185 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700186 if (showingNavScreen()
187 && (item.getItemId() != R.id.history_menu_id)
188 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700189 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700190 }
191 return false;
192 }
193
194 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800195 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800196 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800197 }
198
199 @Override
200 public void onContextMenuClosed(Menu menu, boolean inLoad) {
201 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800202 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800203 }
204 }
205
206 // action mode callbacks
207
208 @Override
209 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700210 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700211 if (!isEditingUrl()) {
212 hideTitleBar();
213 }
Michael Kolb66706532010-12-12 19:50:22 -0800214 }
215
Michael Kolba4183062011-01-16 10:43:21 -0800216 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700217 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700218 super.onActionModeFinished(inLoad);
Michael Kolbc16c5952011-03-29 15:37:03 -0700219 if (inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700220 showTitleBar();
221 }
John Reck6cda7b12011-03-18 15:57:13 -0700222 }
223
224 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700225 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700226 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700227 }
228
229 @Override
230 public void showWeb(boolean animate) {
231 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700232 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700233 }
234
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800235 //Unblock touch events
236 private void unblockEvents() {
237 mUiController.setBlockEvents(false);
238 }
239 //Block touch events
240 private void blockEvents() {
241 mUiController.setBlockEvents(true);
242 }
243
244 @Override
245 public void cancelNavScreenRequest() {
246 mNavScreenRequested = false;
247 }
248
Michael Kolbf2055602011-04-09 17:20:03 -0700249 void showNavScreen() {
Pankaj Garg79878492015-04-01 14:48:21 -0700250 blockEvents();
251 mNavScreenRequested = true;
252 mTabControl.setOnThumbnailUpdatedListener(
253 new TabControl.OnThumbnailUpdatedListener() {
254 @Override
255 public void onThumbnailUpdated(Tab t) {
256 mTabControl.setOnThumbnailUpdatedListener(null);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800257
Pankaj Garg79878492015-04-01 14:48:21 -0700258 // Discard the callback if the req is interrupted
259 if (!mNavScreenRequested) {
260 unblockEvents();
261 return;
Sudheer Koganti06cd42f2015-01-30 15:35:12 -0800262 }
Pankaj Garg79878492015-04-01 14:48:21 -0700263
264 Bitmap bm = t.getScreenshot();
265 Bitmap sbm;
266 WebView webView = getWebView();
267 if (webView != null) {
268 int view_width = webView.getWidth();
269 int capture_width = mActivity.getResources().getDimensionPixelSize(
270 R.dimen.tab_thumbnail_width);
271
272 float scale = (float) view_width / capture_width;
273
274 //Upscale the low-res bitmap to the needed size
275 Matrix m = new Matrix();
276 m.postScale(scale, scale);
277 sbm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
278 bm.getHeight(), m, false);
279 } else {
280 sbm = bm;
281 }
282
283 onShowNavScreenContinue(sbm);
Sudheer Koganti06cd42f2015-01-30 15:35:12 -0800284 }
Pankaj Garg79878492015-04-01 14:48:21 -0700285 });
286 mActiveTab.capture();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700287 }
288
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800289 void onShowNavScreenContinue(Bitmap viewportBitmap) {
290 dismissIME();
291 mShowNav = true;
292 mNavScreenRequested = false;
John Reckcc0059c2011-10-07 13:53:13 -0700293 if (mNavScreen == null) {
294 mNavScreen = new NavScreen(mActivity, mUiController, this);
295 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
296 } else {
297 mNavScreen.setVisibility(View.VISIBLE);
298 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700299 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700300 }
Sagar Dhawan01493f62015-02-09 11:03:19 -0800301 if (mAnimScreen == null) {
302 mAnimScreen = new AnimScreen(mActivity);
303 } else {
304 mAnimScreen.mMain.setAlpha(1f);
305 mAnimScreen.setScaleFactor(1f);
306 }
307 mAnimScreen.set(getTitleBar(), viewportBitmap);
308 if (mAnimScreen.mMain.getParent() == null) {
309 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
310 }
Michael Kolbf2055602011-04-09 17:20:03 -0700311 mCustomViewContainer.setVisibility(View.VISIBLE);
312 mCustomViewContainer.bringToFront();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800313 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
314 mContentView.getHeight());
315 int fromLeft = 0;
316 int fromTop = getTitleBar().getHeight();
317 int fromRight = mContentView.getWidth();
318 int fromBottom = mContentView.getHeight();
319 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
320 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
321 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
322 int toLeft = (mContentView.getWidth() - width) / 2;
323 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
324 int toRight = toLeft + width;
325 int toBottom = toTop + height;
326 float toScaleFactor = width / (float) mContentView.getWidth();
327 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
328 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
329 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
330 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
331 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, toScaleFactor);
332 ObjectAnimator navTabsIn = mNavScreen.createToolbarInAnimator();
333 mAnimScreen.mContent.layout(fromLeft, fromTop, fromRight, fromBottom);
334 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700335
Sagar Dhawan01493f62015-02-09 11:03:19 -0800336 AnimatorSet inanim = new AnimatorSet();
337 inanim.playTogether(tx, ty, tr, tb, sx, navTabsIn);
338 inanim.setInterpolator(new DecelerateInterpolator());
339 inanim.setDuration(200);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800340
Sagar Dhawan01493f62015-02-09 11:03:19 -0800341 ObjectAnimator disappear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 1f, 0f);
342 disappear.setInterpolator(new DecelerateInterpolator());
343 disappear.setDuration(100);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800344
Sagar Dhawan01493f62015-02-09 11:03:19 -0800345 AnimatorSet set1 = new AnimatorSet();
346 set1.addListener(new AnimatorListenerAdapter() {
347 @Override
348 public void onAnimationStart(Animator animation) {
349 mContentView.setVisibility(View.GONE);
350 }
351 @Override
352 public void onAnimationEnd(Animator anim) {
353 mCustomViewContainer.removeView(mAnimScreen.mMain);
354 finishAnimationIn();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800355 unblockEvents();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800356 }
357 });
358 set1.playSequentially(inanim, disappear);
359 set1.start();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800360 unblockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700361 }
362
363 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700364 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700365 // notify accessibility manager about the screen change
366 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Kolbb43f2f62011-08-17 10:39:31 -0700367 }
Michael Kolbf2055602011-04-09 17:20:03 -0700368 }
369
Michael Kolba3194d02011-09-07 11:23:51 -0700370 void hideNavScreen(int position, boolean animate) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800371
yijunx.zhu2230a312012-07-27 13:37:00 -0400372 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700373 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700374 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700375 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700376 if (tab != null) {
377 setActiveTab(tab);
378 } else if (mTabControl.getTabCount() > 0) {
379 // use a fallback tab
380 setActiveTab(mTabControl.getCurrentTab());
381 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700382 mContentView.setVisibility(View.VISIBLE);
383 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700384 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700385 }
Michael Kolba3194d02011-09-07 11:23:51 -0700386 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700387 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700388 if (mTabControl.getTabCount() > 0) {
389 // use a fallback tab
390 setActiveTab(mTabControl.getCurrentTab());
391 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700392 mContentView.setVisibility(View.VISIBLE);
393 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700394 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700395 }
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800396 blockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700397 mUiController.setActiveTab(tab);
398 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700399 if (mAnimScreen == null) {
400 mAnimScreen = new AnimScreen(mActivity);
401 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700402 ImageView target = tabview.mImage;
403 int width = target.getDrawable().getIntrinsicWidth();
404 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700405 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700406 if (bm == null)
407 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
408 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800409 if (mAnimScreen.mMain.getParent() == null) {
410 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
411 }
John Reckcc0059c2011-10-07 13:53:13 -0700412 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700413 mContentView.getHeight());
Enrico Ros80c045a2014-11-24 15:23:12 -0800414 mNavScreen.getScroller().finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700415 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700416 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700417 int toRight = mContentView.getWidth();
Enrico Ros80c045a2014-11-24 15:23:12 -0800418 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.getScroller().getScrollX();
419 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.getScroller().getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700420 int fromRight = fromLeft + width;
421 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700422 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700423 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700424 mAnimScreen.mContent.setLeft(fromLeft);
425 mAnimScreen.mContent.setTop(fromTop);
426 mAnimScreen.mContent.setRight(fromRight);
427 mAnimScreen.mContent.setBottom(fromBottom);
428 mAnimScreen.setScaleFactor(1f);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800429 //ObjectAnimator fade2 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
430 //fade2.setDuration(100);
431 AnimatorSet set = new AnimatorSet();
432 ObjectAnimator animAppear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
433 animAppear.setDuration(100);
434 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
435 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
436 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
437 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
438 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, scaleFactor);
439 set.playTogether(animAppear, l, t, r, b, scale);
440 set.setInterpolator(new DecelerateInterpolator());
441 set.setDuration(200);
442 set.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700443 @Override
444 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400445 checkTabReady();
446 }
447 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800448 set.start();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400449 }
450
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700451
452 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400453 private void checkTabReady() {
454 boolean isready = true;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800455 boolean zeroTries = mNumTries == 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400456 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700457 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400458 if (tab == null)
459 isready = false;
460 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700461 webview = (BrowserWebView)tab.getWebView();
462 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400463 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700464 }
465 else if (webview.hasCrashed()) {
466 webview.reload();
467 isready = true;
468 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400469 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700470 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400471 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700472 // Post only when not ready and not crashed
473 if (!isready && mNumTries++ < 150) {
474 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400475 public void run() {
476 checkTabReady();
477 }
478 }, 17); //WebView is not ready. check again in for next frame.
479 return;
480 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700481 mNumTries = 0;
482 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800483 // fast path: don't wait if we've been ready for a while
484 if (zeroTries) {
485 fadeOutCustomViewContainer(hasCrashed);
486 return;
487 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700488 mCustomViewContainer.postDelayed(new Runnable() {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800489 public void run() {
490 fadeOutCustomViewContainer(hasCrashed);
491 }
492 }, 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 -0400493 }
494
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700495 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400496 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700497 if (hasCrashed)
498 otheralpha.setDuration(300);
499 else
500 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400501 otheralpha.addListener(new AnimatorListenerAdapter() {
502 @Override
503 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700504 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700505 finishAnimateOut();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800506 unblockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700507 }
508 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800509 otheralpha.setInterpolator(new DecelerateInterpolator());
Matthew Hui1eb7a832014-04-15 12:20:58 -0400510 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700511 }
512
Michael Kolbbf32cd02011-08-16 15:07:04 -0700513 private void finishAnimateOut() {
John Reckcc0059c2011-10-07 13:53:13 -0700514 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700515 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700516 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700517 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700518 }
519
John Reck6b4bd5f2011-07-12 10:14:38 -0700520 @Override
521 public boolean needsRestoreAllTabs() {
522 return false;
523 }
524
Michael Kolb20be26d2011-07-18 16:38:02 -0700525 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700526 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700527 showNavScreen();
528 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700529 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700530 }
531 }
532
John Reck1cf4b792011-07-26 10:22:22 -0700533 @Override
534 public boolean shouldCaptureThumbnails() {
535 return true;
536 }
537
Michael Kolbc3af0672011-08-09 10:24:41 -0700538 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700539
Michael Kolbc3af0672011-08-09 10:24:41 -0700540 private View mMain;
Michael Kolbc3af0672011-08-09 10:24:41 -0700541 private ImageView mContent;
542 private float mScale;
543
John Reckcc0059c2011-10-07 13:53:13 -0700544 public AnimScreen(Context ctx) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800545 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen, null);
546 mMain.setOnClickListener(new View.OnClickListener() {
547 @Override
548 public void onClick(View v) {
549 // just eat clicks when this view is visible
550 }
551 });
552 mContent = (ImageView) mMain.findViewById(R.id.anim_screen_content);
John Reckcc0059c2011-10-07 13:53:13 -0700553 mContent.setScaleType(ImageView.ScaleType.MATRIX);
554 mContent.setImageMatrix(new Matrix());
555 mScale = 1.0f;
556 setScaleFactor(getScaleFactor());
557 }
558
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700559 public void set(TitleBar tbar, Bitmap viewportBitmap) {
560 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700561 return;
562 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700563 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700564 }
565
Enrico Ros1f5a0952014-11-18 20:15:48 -0800566 /*private Bitmap safeCreateBitmap(int width, int height) {
John Recka98c83b2011-10-31 12:31:58 -0700567 if (width <= 0 || height <= 0) {
568 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
569 + ", height: " + height);
570 return null;
571 }
572 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800573 }*/
John Recka98c83b2011-10-31 12:31:58 -0700574
John Reckcc0059c2011-10-07 13:53:13 -0700575 public void set(Bitmap image) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700576 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700577 }
578
John Reckcc0059c2011-10-07 13:53:13 -0700579 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700580 mScale = sf;
581 Matrix m = new Matrix();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800582 m.postScale(sf, sf);
Michael Kolbc3af0672011-08-09 10:24:41 -0700583 mContent.setImageMatrix(m);
584 }
585
John Reckcc0059c2011-10-07 13:53:13 -0700586 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700587 return mScale;
588 }
589
John Reck3ba45532011-08-11 16:26:53 -0700590 }
591
Michael Kolb66706532010-12-12 19:50:22 -0800592}