blob: 5a656a2129fe211422992185bd4391f8114640a7 [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();
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
Michael Kolbf2055602011-04-09 17:20:03 -0700235 void showNavScreen() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700236 WebView webView = getWebView();
237 if (webView != null) {
Sagar Dhawan01493f62015-02-09 11:03:19 -0800238 mShowNav = true;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700239 dismissIME();
240 mUiController.setBlockEvents(true);
Sagar Dhawan01493f62015-02-09 11:03:19 -0800241
242 webView.getContentBitmapAsync(1.0f,
243 new Rect(),
244 new ValueCallback<Bitmap>() {
245 @Override
246 public void onReceiveValue(Bitmap bitmap) {
247 onShowNavScreenContinue(bitmap);
248 }
249 });
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700250 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700251 }
252
253 void onShowNavScreenContinue(Bitmap viewportBitmap) {
Sagar Dhawan01493f62015-02-09 11:03:19 -0800254
John Reckcc0059c2011-10-07 13:53:13 -0700255 if (mNavScreen == null) {
256 mNavScreen = new NavScreen(mActivity, mUiController, this);
257 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
258 } else {
259 mNavScreen.setVisibility(View.VISIBLE);
260 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700261 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700262 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700263 mActiveTab.capture();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800264 if (mAnimScreen == null) {
265 mAnimScreen = new AnimScreen(mActivity);
266 } else {
267 mAnimScreen.mMain.setAlpha(1f);
268 mAnimScreen.setScaleFactor(1f);
269 }
270 mAnimScreen.set(getTitleBar(), viewportBitmap);
271 if (mAnimScreen.mMain.getParent() == null) {
272 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
273 }
Michael Kolbf2055602011-04-09 17:20:03 -0700274 mCustomViewContainer.setVisibility(View.VISIBLE);
275 mCustomViewContainer.bringToFront();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800276 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
277 mContentView.getHeight());
278 int fromLeft = 0;
279 int fromTop = getTitleBar().getHeight();
280 int fromRight = mContentView.getWidth();
281 int fromBottom = mContentView.getHeight();
282 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
283 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
284 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
285 int toLeft = (mContentView.getWidth() - width) / 2;
286 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
287 int toRight = toLeft + width;
288 int toBottom = toTop + height;
289 float toScaleFactor = width / (float) mContentView.getWidth();
290 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
291 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
292 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
293 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
294 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, toScaleFactor);
295 ObjectAnimator navTabsIn = mNavScreen.createToolbarInAnimator();
296 mAnimScreen.mContent.layout(fromLeft, fromTop, fromRight, fromBottom);
297 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700298
Sagar Dhawan01493f62015-02-09 11:03:19 -0800299 AnimatorSet inanim = new AnimatorSet();
300 inanim.playTogether(tx, ty, tr, tb, sx, navTabsIn);
301 inanim.setInterpolator(new DecelerateInterpolator());
302 inanim.setDuration(200);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800303
Sagar Dhawan01493f62015-02-09 11:03:19 -0800304 ObjectAnimator disappear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 1f, 0f);
305 disappear.setInterpolator(new DecelerateInterpolator());
306 disappear.setDuration(100);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800307
Sagar Dhawan01493f62015-02-09 11:03:19 -0800308 AnimatorSet set1 = new AnimatorSet();
309 set1.addListener(new AnimatorListenerAdapter() {
310 @Override
311 public void onAnimationStart(Animator animation) {
312 mContentView.setVisibility(View.GONE);
313 }
314 @Override
315 public void onAnimationEnd(Animator anim) {
316 mCustomViewContainer.removeView(mAnimScreen.mMain);
317 finishAnimationIn();
318 mUiController.setBlockEvents(false);
319 }
320 });
321 set1.playSequentially(inanim, disappear);
322 set1.start();
Bijan Amirzadafd75eae2014-04-17 17:09:12 -0700323 mUiController.setBlockEvents(false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700324 }
325
326 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700327 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700328 // notify accessibility manager about the screen change
329 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
330 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
331 }
Michael Kolbf2055602011-04-09 17:20:03 -0700332 }
333
Michael Kolba3194d02011-09-07 11:23:51 -0700334 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400335 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700336 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700337 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700338 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700339 if (tab != null) {
340 setActiveTab(tab);
341 } else if (mTabControl.getTabCount() > 0) {
342 // use a fallback tab
343 setActiveTab(mTabControl.getCurrentTab());
344 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700345 mContentView.setVisibility(View.VISIBLE);
346 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700347 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700348 }
Michael Kolba3194d02011-09-07 11:23:51 -0700349 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700350 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700351 if (mTabControl.getTabCount() > 0) {
352 // use a fallback tab
353 setActiveTab(mTabControl.getCurrentTab());
354 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700355 mContentView.setVisibility(View.VISIBLE);
356 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700357 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700358 }
359 mUiController.setBlockEvents(true);
360 mUiController.setActiveTab(tab);
361 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700362 if (mAnimScreen == null) {
363 mAnimScreen = new AnimScreen(mActivity);
364 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700365 ImageView target = tabview.mImage;
366 int width = target.getDrawable().getIntrinsicWidth();
367 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700368 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700369 if (bm == null)
370 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
371 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800372 if (mAnimScreen.mMain.getParent() == null) {
373 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
374 }
John Reckcc0059c2011-10-07 13:53:13 -0700375 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700376 mContentView.getHeight());
Enrico Ros80c045a2014-11-24 15:23:12 -0800377 mNavScreen.getScroller().finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700378 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700379 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700380 int toRight = mContentView.getWidth();
Enrico Ros80c045a2014-11-24 15:23:12 -0800381 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.getScroller().getScrollX();
382 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.getScroller().getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700383 int fromRight = fromLeft + width;
384 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700385 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700386 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700387 mAnimScreen.mContent.setLeft(fromLeft);
388 mAnimScreen.mContent.setTop(fromTop);
389 mAnimScreen.mContent.setRight(fromRight);
390 mAnimScreen.mContent.setBottom(fromBottom);
391 mAnimScreen.setScaleFactor(1f);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800392 //ObjectAnimator fade2 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
393 //fade2.setDuration(100);
394 AnimatorSet set = new AnimatorSet();
395 ObjectAnimator animAppear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
396 animAppear.setDuration(100);
397 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
398 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
399 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
400 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
401 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, scaleFactor);
402 set.playTogether(animAppear, l, t, r, b, scale);
403 set.setInterpolator(new DecelerateInterpolator());
404 set.setDuration(200);
405 set.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700406 @Override
407 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400408 checkTabReady();
409 }
410 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800411 set.start();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400412 }
413
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700414
415 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400416 private void checkTabReady() {
417 boolean isready = true;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800418 boolean zeroTries = mNumTries == 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400419 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700420 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400421 if (tab == null)
422 isready = false;
423 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700424 webview = (BrowserWebView)tab.getWebView();
425 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400426 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700427 }
428 else if (webview.hasCrashed()) {
429 webview.reload();
430 isready = true;
431 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400432 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700433 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400434 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700435 // Post only when not ready and not crashed
436 if (!isready && mNumTries++ < 150) {
437 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400438 public void run() {
439 checkTabReady();
440 }
441 }, 17); //WebView is not ready. check again in for next frame.
442 return;
443 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700444 mNumTries = 0;
445 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800446 // fast path: don't wait if we've been ready for a while
447 if (zeroTries) {
448 fadeOutCustomViewContainer(hasCrashed);
449 return;
450 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700451 mCustomViewContainer.postDelayed(new Runnable() {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800452 public void run() {
453 fadeOutCustomViewContainer(hasCrashed);
454 }
455 }, 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 -0400456 }
457
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700458 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400459 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700460 if (hasCrashed)
461 otheralpha.setDuration(300);
462 else
463 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400464 otheralpha.addListener(new AnimatorListenerAdapter() {
465 @Override
466 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700467 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700468 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700469 mUiController.setBlockEvents(false);
470 }
471 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800472 otheralpha.setInterpolator(new DecelerateInterpolator());
Matthew Hui1eb7a832014-04-15 12:20:58 -0400473 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700474 }
475
Michael Kolbbf32cd02011-08-16 15:07:04 -0700476 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700477 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700478 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700479 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700480 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700481 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700482 }
483
John Reck6b4bd5f2011-07-12 10:14:38 -0700484 @Override
485 public boolean needsRestoreAllTabs() {
486 return false;
487 }
488
Michael Kolb20be26d2011-07-18 16:38:02 -0700489 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700490 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700491 showNavScreen();
492 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700493 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700494 }
495 }
496
John Reck1cf4b792011-07-26 10:22:22 -0700497 @Override
498 public boolean shouldCaptureThumbnails() {
499 return true;
500 }
501
Michael Kolbc3af0672011-08-09 10:24:41 -0700502 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700503
Michael Kolbc3af0672011-08-09 10:24:41 -0700504 private View mMain;
Michael Kolbc3af0672011-08-09 10:24:41 -0700505 private ImageView mContent;
506 private float mScale;
507
John Reckcc0059c2011-10-07 13:53:13 -0700508 public AnimScreen(Context ctx) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800509 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen, null);
510 mMain.setOnClickListener(new View.OnClickListener() {
511 @Override
512 public void onClick(View v) {
513 // just eat clicks when this view is visible
514 }
515 });
516 mContent = (ImageView) mMain.findViewById(R.id.anim_screen_content);
John Reckcc0059c2011-10-07 13:53:13 -0700517 mContent.setScaleType(ImageView.ScaleType.MATRIX);
518 mContent.setImageMatrix(new Matrix());
519 mScale = 1.0f;
520 setScaleFactor(getScaleFactor());
521 }
522
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700523 public void set(TitleBar tbar, Bitmap viewportBitmap) {
524 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700525 return;
526 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700527 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700528 }
529
Enrico Ros1f5a0952014-11-18 20:15:48 -0800530 /*private Bitmap safeCreateBitmap(int width, int height) {
John Recka98c83b2011-10-31 12:31:58 -0700531 if (width <= 0 || height <= 0) {
532 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
533 + ", height: " + height);
534 return null;
535 }
536 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800537 }*/
John Recka98c83b2011-10-31 12:31:58 -0700538
John Reckcc0059c2011-10-07 13:53:13 -0700539 public void set(Bitmap image) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700540 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700541 }
542
John Reckcc0059c2011-10-07 13:53:13 -0700543 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700544 mScale = sf;
545 Matrix m = new Matrix();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800546 m.postScale(sf, sf);
Michael Kolbc3af0672011-08-09 10:24:41 -0700547 mContent.setImageMatrix(m);
548 }
549
John Reckcc0059c2011-10-07 13:53:13 -0700550 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700551 return mScale;
552 }
553
John Reck3ba45532011-08-11 16:26:53 -0700554 }
555
Michael Kolb66706532010-12-12 19:50:22 -0800556}