blob: 774d2baaa6d6e7c7fe45bab856b66309dcba238a [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 Nainani87a86682015-02-05 11:47:35 -080027import android.os.Bundle;
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;
Tarun Nainani87a86682015-02-05 11:47:35 -080035import android.view.ViewStub;
Michael Kolb30adae62011-07-29 13:37:05 -070036import android.view.accessibility.AccessibilityEvent;
Enrico Ros1f5a0952014-11-18 20:15:48 -080037import android.view.animation.DecelerateInterpolator;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070038
Tarun Nainani87a86682015-02-05 11:47:35 -080039import org.codeaurora.swe.WebView;
40
Michael Kolbc3af0672011-08-09 10:24:41 -070041import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080042
Bijan Amirzada41242f22014-03-21 12:12:18 -070043import com.android.browser.UrlInputView.StateListener;
Michael Kolb629b22c2011-07-13 16:26:47 -070044
Dany Rybnikovbcd37da2015-04-20 11:43:15 +030045import org.codeaurora.net.NetworkServices;
Pankaj Garg79878492015-04-01 14:48:21 -070046
Michael Kolb66706532010-12-12 19:50:22 -080047/**
48 * Ui for regular phone screen sizes
49 */
50public class PhoneUi extends BaseUi {
51
52 private static final String LOGTAG = "PhoneUi";
53
Michael Kolbf2055602011-04-09 17:20:03 -070054 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070055 private AnimScreen mAnimScreen;
Tarun Nainani87a86682015-02-05 11:47:35 -080056 private final NavigationBarPhone mNavigationBar;
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -080057 private boolean mNavScreenRequested = false;
Michael Kolb66706532010-12-12 19:50:22 -080058
yijunx.zhu2230a312012-07-27 13:37:00 -040059 boolean mShowNav = false;
Tarun Nainani87a86682015-02-05 11:47:35 -080060 private ComboView mComboView;
Michael Kolb66706532010-12-12 19:50:22 -080061
Vivek Sekhar60eb9802014-07-21 19:13:33 -070062
Michael Kolb66706532010-12-12 19:50:22 -080063 /**
64 * @param browser
65 * @param controller
66 */
67 public PhoneUi(Activity browser, UiController controller) {
68 super(browser, controller);
John Reck0f602f32011-07-07 15:38:43 -070069 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
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
Tarun Nainani87a86682015-02-05 11:47:35 -080087 public void showComboView(ComboViews startingView, Bundle extras) {
88
89 if (mComboView == null) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -070090 if (mNavScreen != null) {
91 mNavScreen.setVisibility(View.GONE);
92 }
93 ViewStub stub = (ViewStub) mActivity.getWindow().
94 getDecorView().findViewById(R.id.combo_view_stub);
Tarun Nainani87a86682015-02-05 11:47:35 -080095 mComboView = (ComboView) stub.inflate();
96 mComboView.setVisibility(View.GONE);
97 mComboView.setupViews(mActivity);
98 }
99
100 Bundle b = new Bundle();
101 b.putString(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
102 b.putBundle(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
103 Tab t = getActiveTab();
104 if (t != null) {
105 b.putString(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
106 }
107
108 mComboView.showViews(mActivity, b);
109 }
110
111 @Override
112 public void hideComboView() {
113 mComboView.hideViews();
114 }
115
116 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800117 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -0700118 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -0700119 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -0700120 return true;
Michael Kolb66706532010-12-12 19:50:22 -0800121 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700122 if (isComboViewShowing()) {
Tarun Nainani87a86682015-02-05 11:47:35 -0800123 hideComboView();
124 return true;
125 }
Michael Kolb66706532010-12-12 19:50:22 -0800126 return super.onBackKey();
127 }
128
John Reckcc0059c2011-10-07 13:53:13 -0700129 private boolean showingNavScreen() {
130 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
131 }
132
Michael Kolb66706532010-12-12 19:50:22 -0800133 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700134 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700135 return false;
136 }
137
138 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700139 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700140 mTitleBar.cancelTitleBarAnimation(true);
141 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700142 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400143
144 //if at Nav screen show, detach tab like what showNavScreen() do.
145 if (mShowNav) {
146 detachTab(mActiveTab);
147 }
148
Michael Kolbfdb70242011-03-24 09:41:11 -0700149 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800150 // TabControl.setCurrentTab has been called before this,
151 // so the tab is guaranteed to have a webview
152 if (view == null) {
153 Log.e(LOGTAG, "active tab with no webview detected");
154 return;
155 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700156 // Request focus on the top window.
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700157 view.setTitleBar(mTitleBar);
158
Michael Kolb629b22c2011-07-13 16:26:47 -0700159 // update nav bar state
160 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolb376b5412010-12-15 11:52:57 -0800161 }
162
Michael Kolb66706532010-12-12 19:50:22 -0800163 // menu handling callbacks
164
165 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700166 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700167 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700168 return true;
169 }
170
171 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700172 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800173 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
174 if (bm != null) {
175 bm.setVisible(!showingNavScreen());
176 }
Michael Kolb315d5022011-10-13 12:47:11 -0700177 MenuItem info = menu.findItem(R.id.page_info_menu_id);
178 if (info != null) {
179 info.setVisible(false);
180 }
Pankaj Garg49b79252014-11-07 17:33:41 -0800181
John Reckcc0059c2011-10-07 13:53:13 -0700182 if (showingNavScreen()) {
Pankaj Garg49b79252014-11-07 17:33:41 -0800183 setMenuItemVisibility(menu, R.id.history_menu_id, false);
184 setMenuItemVisibility(menu, R.id.find_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700185 menu.setGroupVisible(R.id.LIVE_MENU, false);
Pankaj Garg49b79252014-11-07 17:33:41 -0800186 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700187 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700188 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700189 }
Tarun Nainani87a86682015-02-05 11:47:35 -0800190
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700191 if (isComboViewShowing()) {
Tarun Nainani87a86682015-02-05 11:47:35 -0800192 menu.setGroupVisible(R.id.MAIN_MENU, false);
193 menu.setGroupEnabled(R.id.MAIN_MENU, false);
194 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
195 }
196
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700197 }
198
199 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700200 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700201 if (showingNavScreen()
202 && (item.getItemId() != R.id.history_menu_id)
203 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700204 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700205 }
206 return false;
207 }
208
209 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800210 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800211 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800212 }
213
214 @Override
215 public void onContextMenuClosed(Menu menu, boolean inLoad) {
216 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800217 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800218 }
219 }
220
221 // action mode callbacks
222
223 @Override
224 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700225 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700226 if (!isEditingUrl()) {
227 hideTitleBar();
228 }
Michael Kolb66706532010-12-12 19:50:22 -0800229 }
230
Michael Kolba4183062011-01-16 10:43:21 -0800231 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700232 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700233 super.onActionModeFinished(inLoad);
Michael Kolbc16c5952011-03-29 15:37:03 -0700234 if (inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700235 showTitleBar();
236 }
John Reck6cda7b12011-03-18 15:57:13 -0700237 }
238
239 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700240 public boolean isWebShowing() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700241 return super.isWebShowing() && !showingNavScreen() && !isComboViewShowing();
Tarun Nainani87a86682015-02-05 11:47:35 -0800242 }
243
244 @Override
245 public boolean isComboViewShowing() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700246 return mComboView != null && mComboView.getVisibility() == View.VISIBLE;
Michael Kolbc3af0672011-08-09 10:24:41 -0700247 }
248
249 @Override
250 public void showWeb(boolean animate) {
251 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700252 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700253 }
254
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800255 //Unblock touch events
256 private void unblockEvents() {
257 mUiController.setBlockEvents(false);
258 }
259 //Block touch events
260 private void blockEvents() {
261 mUiController.setBlockEvents(true);
262 }
263
264 @Override
265 public void cancelNavScreenRequest() {
266 mNavScreenRequested = false;
267 }
268
Michael Kolbf2055602011-04-09 17:20:03 -0700269 void showNavScreen() {
Pankaj Garg79878492015-04-01 14:48:21 -0700270 blockEvents();
271 mNavScreenRequested = true;
272 mTabControl.setOnThumbnailUpdatedListener(
273 new TabControl.OnThumbnailUpdatedListener() {
274 @Override
275 public void onThumbnailUpdated(Tab t) {
276 mTabControl.setOnThumbnailUpdatedListener(null);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800277
Pankaj Garg79878492015-04-01 14:48:21 -0700278 // Discard the callback if the req is interrupted
279 if (!mNavScreenRequested) {
280 unblockEvents();
281 return;
Sudheer Koganti06cd42f2015-01-30 15:35:12 -0800282 }
Pankaj Garg79878492015-04-01 14:48:21 -0700283
284 Bitmap bm = t.getScreenshot();
285 Bitmap sbm;
286 WebView webView = getWebView();
287 if (webView != null) {
288 int view_width = webView.getWidth();
289 int capture_width = mActivity.getResources().getDimensionPixelSize(
290 R.dimen.tab_thumbnail_width);
291
292 float scale = (float) view_width / capture_width;
293
294 //Upscale the low-res bitmap to the needed size
295 Matrix m = new Matrix();
296 m.postScale(scale, scale);
297 sbm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
298 bm.getHeight(), m, false);
299 } else {
300 sbm = bm;
301 }
302
303 onShowNavScreenContinue(sbm);
Sudheer Koganti06cd42f2015-01-30 15:35:12 -0800304 }
Pankaj Garg79878492015-04-01 14:48:21 -0700305 });
Dany Rybnikovbcd37da2015-04-20 11:43:15 +0300306 if (!BrowserSettings.getInstance().isPowerSaveModeEnabled()) {
307 //Notify about anticipated network activity
308 NetworkServices.hintUpcomingUserActivity();
309 }
Pankaj Garg79878492015-04-01 14:48:21 -0700310 mActiveTab.capture();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700311 }
312
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800313 void onShowNavScreenContinue(Bitmap viewportBitmap) {
314 dismissIME();
315 mShowNav = true;
316 mNavScreenRequested = false;
John Reckcc0059c2011-10-07 13:53:13 -0700317 if (mNavScreen == null) {
318 mNavScreen = new NavScreen(mActivity, mUiController, this);
319 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
320 } else {
321 mNavScreen.setVisibility(View.VISIBLE);
322 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700323 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700324 }
Sagar Dhawan01493f62015-02-09 11:03:19 -0800325 if (mAnimScreen == null) {
326 mAnimScreen = new AnimScreen(mActivity);
327 } else {
328 mAnimScreen.mMain.setAlpha(1f);
329 mAnimScreen.setScaleFactor(1f);
330 }
331 mAnimScreen.set(getTitleBar(), viewportBitmap);
332 if (mAnimScreen.mMain.getParent() == null) {
333 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
334 }
Michael Kolbf2055602011-04-09 17:20:03 -0700335 mCustomViewContainer.setVisibility(View.VISIBLE);
336 mCustomViewContainer.bringToFront();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800337 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
338 mContentView.getHeight());
339 int fromLeft = 0;
340 int fromTop = getTitleBar().getHeight();
341 int fromRight = mContentView.getWidth();
342 int fromBottom = mContentView.getHeight();
343 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
344 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
345 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
346 int toLeft = (mContentView.getWidth() - width) / 2;
347 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
348 int toRight = toLeft + width;
349 int toBottom = toTop + height;
350 float toScaleFactor = width / (float) mContentView.getWidth();
351 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
352 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
353 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700354 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
355 fromBottom, toBottom);
Sagar Dhawan01493f62015-02-09 11:03:19 -0800356 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, toScaleFactor);
357 ObjectAnimator navTabsIn = mNavScreen.createToolbarInAnimator();
358 mAnimScreen.mContent.layout(fromLeft, fromTop, fromRight, fromBottom);
359 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700360
Sagar Dhawan01493f62015-02-09 11:03:19 -0800361 AnimatorSet inanim = new AnimatorSet();
362 inanim.playTogether(tx, ty, tr, tb, sx, navTabsIn);
363 inanim.setInterpolator(new DecelerateInterpolator());
364 inanim.setDuration(200);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800365
Sagar Dhawan01493f62015-02-09 11:03:19 -0800366 ObjectAnimator disappear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 1f, 0f);
367 disappear.setInterpolator(new DecelerateInterpolator());
368 disappear.setDuration(100);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800369
Sagar Dhawan01493f62015-02-09 11:03:19 -0800370 AnimatorSet set1 = new AnimatorSet();
371 set1.addListener(new AnimatorListenerAdapter() {
372 @Override
373 public void onAnimationStart(Animator animation) {
374 mContentView.setVisibility(View.GONE);
375 }
376 @Override
377 public void onAnimationEnd(Animator anim) {
378 mCustomViewContainer.removeView(mAnimScreen.mMain);
379 finishAnimationIn();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800380 unblockEvents();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800381 }
382 });
383 set1.playSequentially(inanim, disappear);
384 set1.start();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800385 unblockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700386 }
387
388 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700389 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700390 // notify accessibility manager about the screen change
391 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Kolbb43f2f62011-08-17 10:39:31 -0700392 }
Michael Kolbf2055602011-04-09 17:20:03 -0700393 }
394
Michael Kolba3194d02011-09-07 11:23:51 -0700395 void hideNavScreen(int position, boolean animate) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800396
yijunx.zhu2230a312012-07-27 13:37:00 -0400397 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700398 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700399 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700400 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700401 if (tab != null) {
402 setActiveTab(tab);
403 } else if (mTabControl.getTabCount() > 0) {
404 // use a fallback tab
405 setActiveTab(mTabControl.getCurrentTab());
406 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700407 mContentView.setVisibility(View.VISIBLE);
408 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700409 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700410 }
Michael Kolba3194d02011-09-07 11:23:51 -0700411 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700412 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700413 if (mTabControl.getTabCount() > 0) {
414 // use a fallback tab
415 setActiveTab(mTabControl.getCurrentTab());
416 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700417 mContentView.setVisibility(View.VISIBLE);
418 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700419 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700420 }
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800421 blockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700422 mUiController.setActiveTab(tab);
423 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700424 if (mAnimScreen == null) {
425 mAnimScreen = new AnimScreen(mActivity);
426 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700427 ImageView target = tabview.mImage;
428 int width = target.getDrawable().getIntrinsicWidth();
429 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700430 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700431 if (bm == null)
432 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
433 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800434 if (mAnimScreen.mMain.getParent() == null) {
435 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
436 }
John Reckcc0059c2011-10-07 13:53:13 -0700437 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700438 mContentView.getHeight());
Enrico Ros80c045a2014-11-24 15:23:12 -0800439 mNavScreen.getScroller().finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700440 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700441 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700442 int toRight = mContentView.getWidth();
Enrico Ros80c045a2014-11-24 15:23:12 -0800443 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.getScroller().getScrollX();
444 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.getScroller().getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700445 int fromRight = fromLeft + width;
446 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700447 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700448 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700449 mAnimScreen.mContent.setLeft(fromLeft);
450 mAnimScreen.mContent.setTop(fromTop);
451 mAnimScreen.mContent.setRight(fromRight);
452 mAnimScreen.mContent.setBottom(fromBottom);
453 mAnimScreen.setScaleFactor(1f);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800454 //ObjectAnimator fade2 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
455 //fade2.setDuration(100);
456 AnimatorSet set = new AnimatorSet();
457 ObjectAnimator animAppear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
458 animAppear.setDuration(100);
459 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
460 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
461 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700462 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
463 fromBottom, toBottom);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800464 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, scaleFactor);
465 set.playTogether(animAppear, l, t, r, b, scale);
466 set.setInterpolator(new DecelerateInterpolator());
467 set.setDuration(200);
468 set.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700469 @Override
470 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400471 checkTabReady();
472 }
473 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800474 set.start();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400475 }
476
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700477
478 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400479 private void checkTabReady() {
480 boolean isready = true;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800481 boolean zeroTries = mNumTries == 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400482 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700483 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400484 if (tab == null)
485 isready = false;
486 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700487 webview = (BrowserWebView)tab.getWebView();
488 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400489 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700490 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400491 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700492 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400493 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700494 // Post only when not ready and not crashed
495 if (!isready && mNumTries++ < 150) {
496 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400497 public void run() {
498 checkTabReady();
499 }
500 }, 17); //WebView is not ready. check again in for next frame.
501 return;
502 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700503 mNumTries = 0;
Vivek Sekharc70ae632015-04-08 17:54:05 -0700504 final boolean hasCrashed = (webview == null) ? false :false;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800505 // fast path: don't wait if we've been ready for a while
506 if (zeroTries) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700507 fadeOutCustomViewContainer();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800508 return;
509 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700510 mCustomViewContainer.postDelayed(new Runnable() {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800511 public void run() {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700512 fadeOutCustomViewContainer();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800513 }
514 }, 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 -0400515 }
516
Vivek Sekharc70ae632015-04-08 17:54:05 -0700517 private void fadeOutCustomViewContainer() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400518 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700519 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400520 otheralpha.addListener(new AnimatorListenerAdapter() {
521 @Override
522 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700523 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700524 finishAnimateOut();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800525 unblockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700526 }
527 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800528 otheralpha.setInterpolator(new DecelerateInterpolator());
Matthew Hui1eb7a832014-04-15 12:20:58 -0400529 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700530 }
531
Michael Kolbbf32cd02011-08-16 15:07:04 -0700532 private void finishAnimateOut() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700533 if (mNavScreen != null) {
534 mNavScreen.setVisibility(View.GONE);
535 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700536 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700537 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700538 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700539 }
540
John Reck6b4bd5f2011-07-12 10:14:38 -0700541 @Override
542 public boolean needsRestoreAllTabs() {
543 return false;
544 }
545
Michael Kolb20be26d2011-07-18 16:38:02 -0700546 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700547 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700548 showNavScreen();
549 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700550 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700551 }
552 }
553
Michael Kolbc3af0672011-08-09 10:24:41 -0700554 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700555
Michael Kolbc3af0672011-08-09 10:24:41 -0700556 private View mMain;
Michael Kolbc3af0672011-08-09 10:24:41 -0700557 private ImageView mContent;
558 private float mScale;
559
John Reckcc0059c2011-10-07 13:53:13 -0700560 public AnimScreen(Context ctx) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800561 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen, null);
562 mMain.setOnClickListener(new View.OnClickListener() {
563 @Override
564 public void onClick(View v) {
565 // just eat clicks when this view is visible
566 }
567 });
568 mContent = (ImageView) mMain.findViewById(R.id.anim_screen_content);
John Reckcc0059c2011-10-07 13:53:13 -0700569 mContent.setScaleType(ImageView.ScaleType.MATRIX);
570 mContent.setImageMatrix(new Matrix());
571 mScale = 1.0f;
572 setScaleFactor(getScaleFactor());
573 }
574
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700575 public void set(TitleBar tbar, Bitmap viewportBitmap) {
576 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700577 return;
578 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700579 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700580 }
581
John Reckcc0059c2011-10-07 13:53:13 -0700582 public void set(Bitmap image) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700583 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700584 }
585
John Reckcc0059c2011-10-07 13:53:13 -0700586 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700587 mScale = sf;
588 Matrix m = new Matrix();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800589 m.postScale(sf, sf);
Michael Kolbc3af0672011-08-09 10:24:41 -0700590 mContent.setImageMatrix(m);
591 }
592
John Reckcc0059c2011-10-07 13:53:13 -0700593 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700594 return mScale;
595 }
596
John Reck3ba45532011-08-11 16:26:53 -0700597 }
598
Michael Kolb66706532010-12-12 19:50:22 -0800599}