blob: 5dc87f31b9e76b830f0a9409e8283b675ce7fd1f [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;
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();
Michael Kolb66706532010-12-12 19:50:22 -080074 }
75
76 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -070077 public void editUrl(boolean clearInput, boolean forceIME) {
yijunx.zhu2230a312012-07-27 13:37:00 -040078 //Do nothing while at Nav show screen.
79 if (mShowNav) return;
Michael Kolb1f9b3562012-04-24 14:38:34 -070080 super.editUrl(clearInput, forceIME);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080081 }
82
83 @Override
Michael Kolb66706532010-12-12 19:50:22 -080084 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070085 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070086 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070087 return true;
Michael Kolb66706532010-12-12 19:50:22 -080088 }
89 return super.onBackKey();
90 }
91
John Reckcc0059c2011-10-07 13:53:13 -070092 private boolean showingNavScreen() {
93 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
94 }
95
Michael Kolb66706532010-12-12 19:50:22 -080096 @Override
Michael Kolbf2055602011-04-09 17:20:03 -070097 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -070098 return false;
99 }
100
101 @Override
John Reck30c714c2010-12-16 17:30:34 -0800102 public void onProgressChanged(Tab tab) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700103 super.onProgressChanged(tab);
John Reck9c5004e2011-10-07 16:00:12 -0700104 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
105 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
106 }
107 }
108
109 @Override
110 protected void handleMessage(Message msg) {
111 super.handleMessage(msg);
112 if (msg.what == MSG_INIT_NAVSCREEN) {
113 if (mNavScreen == null) {
114 mNavScreen = new NavScreen(mActivity, mUiController, this);
115 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
116 mNavScreen.setVisibility(View.GONE);
117 }
118 if (mAnimScreen == null) {
119 mAnimScreen = new AnimScreen(mActivity);
120 // initialize bitmaps
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700121 //mAnimScreen.set(getTitleBar(), getWebView());
John Reck9c5004e2011-10-07 16:00:12 -0700122 }
123 }
Michael Kolb66706532010-12-12 19:50:22 -0800124 }
125
126 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700127 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700128 mTitleBar.cancelTitleBarAnimation(true);
129 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700130 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400131
132 //if at Nav screen show, detach tab like what showNavScreen() do.
133 if (mShowNav) {
134 detachTab(mActiveTab);
135 }
136
Michael Kolbfdb70242011-03-24 09:41:11 -0700137 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800138 // TabControl.setCurrentTab has been called before this,
139 // so the tab is guaranteed to have a webview
140 if (view == null) {
141 Log.e(LOGTAG, "active tab with no webview detected");
142 return;
143 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700144 // Request focus on the top window.
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700145 view.setTitleBar(mTitleBar);
146
Michael Kolb629b22c2011-07-13 16:26:47 -0700147 // update nav bar state
148 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700149 updateLockIconToLatest(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700150 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800151 }
152
Michael Kolb66706532010-12-12 19:50:22 -0800153 // menu handling callbacks
154
155 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700156 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700157 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700158 return true;
159 }
160
161 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700162 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800163 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
164 if (bm != null) {
165 bm.setVisible(!showingNavScreen());
166 }
Michael Kolb315d5022011-10-13 12:47:11 -0700167 MenuItem info = menu.findItem(R.id.page_info_menu_id);
168 if (info != null) {
169 info.setVisible(false);
170 }
Pankaj Garg49b79252014-11-07 17:33:41 -0800171
John Reckcc0059c2011-10-07 13:53:13 -0700172 if (showingNavScreen()) {
Pankaj Garg49b79252014-11-07 17:33:41 -0800173 setMenuItemVisibility(menu, R.id.history_menu_id, false);
174 setMenuItemVisibility(menu, R.id.find_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700175 menu.setGroupVisible(R.id.LIVE_MENU, false);
Pankaj Garg49b79252014-11-07 17:33:41 -0800176 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700177 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700178 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700179 }
180 }
181
182 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700183 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700184 if (showingNavScreen()
185 && (item.getItemId() != R.id.history_menu_id)
186 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700187 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700188 }
189 return false;
190 }
191
192 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800193 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800194 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800195 }
196
197 @Override
198 public void onContextMenuClosed(Menu menu, boolean inLoad) {
199 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800201 }
202 }
203
204 // action mode callbacks
205
206 @Override
207 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700208 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700209 if (!isEditingUrl()) {
210 hideTitleBar();
211 }
Michael Kolb66706532010-12-12 19:50:22 -0800212 }
213
Michael Kolba4183062011-01-16 10:43:21 -0800214 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700215 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700216 super.onActionModeFinished(inLoad);
Michael Kolbc16c5952011-03-29 15:37:03 -0700217 if (inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700218 showTitleBar();
219 }
John Reck6cda7b12011-03-18 15:57:13 -0700220 }
221
222 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700223 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700224 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700225 }
226
227 @Override
228 public void showWeb(boolean animate) {
229 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700230 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700231 }
232
Michael Kolbf2055602011-04-09 17:20:03 -0700233 void showNavScreen() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700234 WebView webView = getWebView();
235 if (webView != null) {
236 mShowNav = true;
237 dismissIME();
238 mUiController.setBlockEvents(true);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700239
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700240 webView.getContentBitmapAsync(1.0f,
241 new Rect(),
242 new ValueCallback<Bitmap>() {
243 @Override
244 public void onReceiveValue(Bitmap bitmap) {
245 onShowNavScreenContinue(bitmap);
246 }
247 });
248 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700249 }
250
251 void onShowNavScreenContinue(Bitmap viewportBitmap) {
252
John Reckcc0059c2011-10-07 13:53:13 -0700253 if (mNavScreen == null) {
254 mNavScreen = new NavScreen(mActivity, mUiController, this);
255 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
256 } else {
257 mNavScreen.setVisibility(View.VISIBLE);
258 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700259 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700260 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700261 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700262 if (mAnimScreen == null) {
263 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700264 } else {
265 mAnimScreen.mMain.setAlpha(1f);
John Reckce8bcb02011-10-11 13:45:29 -0700266 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700267 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700268 mAnimScreen.set(getTitleBar(), viewportBitmap);
Michael Kolba30e4fb2011-11-07 17:32:01 -0800269 if (mAnimScreen.mMain.getParent() == null) {
270 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
271 }
Michael Kolbf2055602011-04-09 17:20:03 -0700272 mCustomViewContainer.setVisibility(View.VISIBLE);
273 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700274 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
275 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700276 int fromLeft = 0;
277 int fromTop = getTitleBar().getHeight();
278 int fromRight = mContentView.getWidth();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700279 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700280 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
281 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700282 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700283 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700284 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700285 int toRight = toLeft + width;
286 int toBottom = toTop + height;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800287 float toScaleFactor = width / (float) mContentView.getWidth();
288 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
289 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
290 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
291 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
292 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, toScaleFactor);
293 ObjectAnimator navTabsIn = ObjectAnimator.ofFloat(mNavScreen.mToolbarLayout, "translationY",
294 -mNavScreen.getResources().getDimensionPixelSize(R.dimen.toolbar_height), 0f);
295 mAnimScreen.mContent.layout(fromLeft, fromTop, fromRight, fromBottom);
296 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700297
Enrico Ros1f5a0952014-11-18 20:15:48 -0800298 AnimatorSet inanim = new AnimatorSet();
299 inanim.playTogether(tx, ty, tr, tb, sx, navTabsIn);
300 inanim.setInterpolator(new DecelerateInterpolator());
Michael Kolba3194d02011-09-07 11:23:51 -0700301 inanim.setDuration(200);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800302
303 ObjectAnimator disappear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 1f, 0f);
304 disappear.setInterpolator(new DecelerateInterpolator());
305 disappear.setDuration(100);
306
307 AnimatorSet set1 = new AnimatorSet();
Michael Kolba3194d02011-09-07 11:23:51 -0700308 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700309 @Override
Enrico Ros1f5a0952014-11-18 20:15:48 -0800310 public void onAnimationStart(Animator animation) {
311 mContentView.setVisibility(View.GONE);
312 }
313 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700314 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700315 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700316 finishAnimationIn();
317 mUiController.setBlockEvents(false);
318 }
319 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800320 set1.playSequentially(inanim, disappear);
Michael Kolba3194d02011-09-07 11:23:51 -0700321 set1.start();
Bijan Amirzadafd75eae2014-04-17 17:09:12 -0700322 mUiController.setBlockEvents(false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700323 }
324
325 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700326 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700327 // notify accessibility manager about the screen change
328 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
329 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
330 }
Michael Kolbf2055602011-04-09 17:20:03 -0700331 }
332
Michael Kolba3194d02011-09-07 11:23:51 -0700333 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400334 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700335 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700336 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700337 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700338 if (tab != null) {
339 setActiveTab(tab);
340 } else if (mTabControl.getTabCount() > 0) {
341 // use a fallback tab
342 setActiveTab(mTabControl.getCurrentTab());
343 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700344 mContentView.setVisibility(View.VISIBLE);
345 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700346 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700347 }
Michael Kolba3194d02011-09-07 11:23:51 -0700348 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700349 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700350 if (mTabControl.getTabCount() > 0) {
351 // use a fallback tab
352 setActiveTab(mTabControl.getCurrentTab());
353 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700354 mContentView.setVisibility(View.VISIBLE);
355 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700356 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700357 }
358 mUiController.setBlockEvents(true);
359 mUiController.setActiveTab(tab);
360 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700361 if (mAnimScreen == null) {
362 mAnimScreen = new AnimScreen(mActivity);
363 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700364 ImageView target = tabview.mImage;
365 int width = target.getDrawable().getIntrinsicWidth();
366 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700367 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700368 if (bm == null)
369 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
370 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800371 if (mAnimScreen.mMain.getParent() == null) {
372 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
373 }
John Reckcc0059c2011-10-07 13:53:13 -0700374 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700375 mContentView.getHeight());
Michael Kolba3194d02011-09-07 11:23:51 -0700376 mNavScreen.mScroller.finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700377 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700378 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700379 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700380 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
381 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700382 int fromRight = fromLeft + width;
383 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700384 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700385 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700386 mAnimScreen.mContent.setLeft(fromLeft);
387 mAnimScreen.mContent.setTop(fromTop);
388 mAnimScreen.mContent.setRight(fromRight);
389 mAnimScreen.mContent.setBottom(fromBottom);
390 mAnimScreen.setScaleFactor(1f);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800391 //ObjectAnimator fade2 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
392 //fade2.setDuration(100);
393 AnimatorSet set = new AnimatorSet();
394 ObjectAnimator animAppear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
395 animAppear.setDuration(100);
396 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
397 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
398 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
399 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
400 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, scaleFactor);
401 set.playTogether(animAppear, l, t, r, b, scale);
402 set.setInterpolator(new DecelerateInterpolator());
403 set.setDuration(200);
404 set.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700405 @Override
406 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400407 checkTabReady();
408 }
409 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800410 set.start();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400411 }
412
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700413
414 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400415 private void checkTabReady() {
416 boolean isready = true;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800417 boolean zeroTries = mNumTries == 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400418 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700419 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400420 if (tab == null)
421 isready = false;
422 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700423 webview = (BrowserWebView)tab.getWebView();
424 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400425 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700426 }
427 else if (webview.hasCrashed()) {
428 webview.reload();
429 isready = true;
430 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400431 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700432 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400433 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700434 // Post only when not ready and not crashed
435 if (!isready && mNumTries++ < 150) {
436 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400437 public void run() {
438 checkTabReady();
439 }
440 }, 17); //WebView is not ready. check again in for next frame.
441 return;
442 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700443 mNumTries = 0;
444 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800445 // fast path: don't wait if we've been ready for a while
446 if (zeroTries) {
447 fadeOutCustomViewContainer(hasCrashed);
448 return;
449 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700450 mCustomViewContainer.postDelayed(new Runnable() {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800451 public void run() {
452 fadeOutCustomViewContainer(hasCrashed);
453 }
454 }, 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 -0400455 }
456
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700457 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400458 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700459 if (hasCrashed)
460 otheralpha.setDuration(300);
461 else
462 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400463 otheralpha.addListener(new AnimatorListenerAdapter() {
464 @Override
465 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700466 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700467 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700468 mUiController.setBlockEvents(false);
469 }
470 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800471 otheralpha.setInterpolator(new DecelerateInterpolator());
Matthew Hui1eb7a832014-04-15 12:20:58 -0400472 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700473 }
474
Michael Kolbbf32cd02011-08-16 15:07:04 -0700475 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700476 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700477 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700478 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700479 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700480 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700481 }
482
John Reck6b4bd5f2011-07-12 10:14:38 -0700483 @Override
484 public boolean needsRestoreAllTabs() {
485 return false;
486 }
487
Michael Kolb20be26d2011-07-18 16:38:02 -0700488 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700489 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700490 showNavScreen();
491 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700492 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700493 }
494 }
495
John Reck1cf4b792011-07-26 10:22:22 -0700496 @Override
497 public boolean shouldCaptureThumbnails() {
498 return true;
499 }
500
Michael Kolbc3af0672011-08-09 10:24:41 -0700501 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700502
Michael Kolbc3af0672011-08-09 10:24:41 -0700503 private View mMain;
Michael Kolbc3af0672011-08-09 10:24:41 -0700504 private ImageView mContent;
505 private float mScale;
506
John Reckcc0059c2011-10-07 13:53:13 -0700507 public AnimScreen(Context ctx) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800508 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen, null);
509 mMain.setOnClickListener(new View.OnClickListener() {
510 @Override
511 public void onClick(View v) {
512 // just eat clicks when this view is visible
513 }
514 });
515 mContent = (ImageView) mMain.findViewById(R.id.anim_screen_content);
John Reckcc0059c2011-10-07 13:53:13 -0700516 mContent.setScaleType(ImageView.ScaleType.MATRIX);
517 mContent.setImageMatrix(new Matrix());
518 mScale = 1.0f;
519 setScaleFactor(getScaleFactor());
520 }
521
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700522 public void set(TitleBar tbar, Bitmap viewportBitmap) {
523 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700524 return;
525 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700526 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700527 }
528
Enrico Ros1f5a0952014-11-18 20:15:48 -0800529 /*private Bitmap safeCreateBitmap(int width, int height) {
John Recka98c83b2011-10-31 12:31:58 -0700530 if (width <= 0 || height <= 0) {
531 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
532 + ", height: " + height);
533 return null;
534 }
535 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800536 }*/
John Recka98c83b2011-10-31 12:31:58 -0700537
John Reckcc0059c2011-10-07 13:53:13 -0700538 public void set(Bitmap image) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700539 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700540 }
541
John Reckcc0059c2011-10-07 13:53:13 -0700542 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700543 mScale = sf;
544 Matrix m = new Matrix();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800545 m.postScale(sf, sf);
Michael Kolbc3af0672011-08-09 10:24:41 -0700546 mContent.setImageMatrix(m);
547 }
548
John Reckcc0059c2011-10-07 13:53:13 -0700549 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700550 return mScale;
551 }
552
John Reck3ba45532011-08-11 16:26:53 -0700553 }
554
Michael Kolb66706532010-12-12 19:50:22 -0800555}