blob: d74e988ccb9141d3044fdfeebcd4904d42474423 [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;
26import android.graphics.Canvas;
27import android.graphics.Matrix;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070028import android.graphics.Rect;
John Reck9c5004e2011-10-07 16:00:12 -070029import android.os.Message;
Michael Kolb376b5412010-12-15 11:52:57 -080030import android.util.Log;
John Reckaae029c2011-09-16 14:36:39 -070031import android.util.TypedValue;
Michael Kolb66706532010-12-12 19:50:22 -080032import android.view.ActionMode;
Michael Kolba4183062011-01-16 10:43:21 -080033import android.view.KeyEvent;
Michael Kolbc3af0672011-08-09 10:24:41 -070034import android.view.LayoutInflater;
Michael Kolb66706532010-12-12 19:50:22 -080035import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070036import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080037import android.view.View;
Michael Kolb30adae62011-07-29 13:37:05 -070038import android.view.accessibility.AccessibilityEvent;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070039import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080040import org.codeaurora.swe.WebView;
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.R;
44import com.android.browser.UrlInputView.StateListener;
Michael Kolb629b22c2011-07-13 16:26:47 -070045
Michael Kolb66706532010-12-12 19:50:22 -080046/**
47 * Ui for regular phone screen sizes
48 */
49public class PhoneUi extends BaseUi {
50
51 private static final String LOGTAG = "PhoneUi";
John Reck9c5004e2011-10-07 16:00:12 -070052 private static final int MSG_INIT_NAVSCREEN = 100;
Michael Kolb66706532010-12-12 19:50:22 -080053
Michael Kolbf2055602011-04-09 17:20:03 -070054 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070055 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070056 private NavigationBarPhone mNavigationBar;
Vivek Sekhar60eb9802014-07-21 19:13:33 -070057 private Activity mBrowser;
Michael Kolb66706532010-12-12 19:50:22 -080058
Michael Kolb08a687a2011-04-22 16:13:02 -070059 boolean mAnimating;
yijunx.zhu2230a312012-07-27 13:37:00 -040060 boolean mShowNav = false;
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();
Vivek Sekhar60eb9802014-07-21 19:13:33 -070070 mBrowser = browser;
John Reck285ef042011-02-11 15:44:17 -080071 }
Michael Kolb66706532010-12-12 19:50:22 -080072
John Reck285ef042011-02-11 15:44:17 -080073 @Override
Michael Kolb66706532010-12-12 19:50:22 -080074 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080075 hideTitleBar();
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 Kolb7bdee0b2011-08-01 11:55:38 -0700169 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
170 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700171 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700172 }
Michael Kolb315d5022011-10-13 12:47:11 -0700173 MenuItem info = menu.findItem(R.id.page_info_menu_id);
174 if (info != null) {
175 info.setVisible(false);
176 }
177 MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700178 if (newtab != null) {
Michael Kolb315d5022011-10-13 12:47:11 -0700179 newtab.setVisible(false);
180 }
181 MenuItem incognito = menu.findItem(R.id.incognito_menu_id);
182 if (incognito != null) {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700183 incognito.setVisible(showingNavScreen());
Michael Kolb315d5022011-10-13 12:47:11 -0700184 }
Afzal Najamd4e33312012-04-26 01:54:01 -0400185 MenuItem closeOthers = menu.findItem(R.id.close_other_tabs_id);
186 if (closeOthers != null) {
187 boolean isLastTab = true;
188 if (tab != null) {
189 isLastTab = (mTabControl.getTabCount() <= 1);
190 }
191 closeOthers.setEnabled(!isLastTab);
192 }
John Reckcc0059c2011-10-07 13:53:13 -0700193 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700194 menu.setGroupVisible(R.id.LIVE_MENU, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700195 menu.setGroupVisible(R.id.OFFLINE_READING, false);
John Recke1a03a32011-09-14 17:04:16 -0700196 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700197 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb315d5022011-10-13 12:47:11 -0700198 menu.setGroupVisible(R.id.COMBO_MENU, true);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700199 }
200 }
201
202 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700203 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700204 if (showingNavScreen()
205 && (item.getItemId() != R.id.history_menu_id)
206 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700207 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700208 }
209 return false;
210 }
211
212 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800213 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800214 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800215 }
216
217 @Override
218 public void onContextMenuClosed(Menu menu, boolean inLoad) {
219 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800220 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800221 }
222 }
223
224 // action mode callbacks
225
226 @Override
227 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700228 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700229 if (!isEditingUrl()) {
230 hideTitleBar();
231 }
Michael Kolb66706532010-12-12 19:50:22 -0800232 }
233
Michael Kolba4183062011-01-16 10:43:21 -0800234 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700235 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700236 super.onActionModeFinished(inLoad);
Michael Kolbc16c5952011-03-29 15:37:03 -0700237 if (inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700238 showTitleBar();
239 }
John Reck6cda7b12011-03-18 15:57:13 -0700240 }
241
242 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700243 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700244 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700245 }
246
247 @Override
248 public void showWeb(boolean animate) {
249 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700250 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700251 }
252
Michael Kolbf2055602011-04-09 17:20:03 -0700253 void showNavScreen() {
yijunx.zhu2230a312012-07-27 13:37:00 -0400254 mShowNav = true;
Vivek Sekhar4ddafb12014-07-22 15:33:16 -0700255 dismissIME();
Michael Kolbc3af0672011-08-09 10:24:41 -0700256 mUiController.setBlockEvents(true);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700257
258 getWebView()
259 .getContentBitmapAsync(1.0f,
260 new Rect(),
261 new ValueCallback<Bitmap>() {
262 @Override
263 public void onReceiveValue(Bitmap bitmap) {
264 onShowNavScreenContinue(bitmap);
265 }});
266 }
267
268 void onShowNavScreenContinue(Bitmap viewportBitmap) {
269
John Reckcc0059c2011-10-07 13:53:13 -0700270 if (mNavScreen == null) {
271 mNavScreen = new NavScreen(mActivity, mUiController, this);
272 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
273 } else {
274 mNavScreen.setVisibility(View.VISIBLE);
275 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700276 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700277 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700278 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700279 if (mAnimScreen == null) {
280 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700281 } else {
282 mAnimScreen.mMain.setAlpha(1f);
283 mAnimScreen.mTitle.setAlpha(1f);
284 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700285 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700286 mAnimScreen.set(getTitleBar(), viewportBitmap);
Michael Kolba30e4fb2011-11-07 17:32:01 -0800287 if (mAnimScreen.mMain.getParent() == null) {
288 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
289 }
Michael Kolbf2055602011-04-09 17:20:03 -0700290 mCustomViewContainer.setVisibility(View.VISIBLE);
291 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700292 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
293 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700294 int fromLeft = 0;
295 int fromTop = getTitleBar().getHeight();
296 int fromRight = mContentView.getWidth();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700297 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700298 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
299 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700300 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700301 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700302 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700303 int toRight = toLeft + width;
304 int toBottom = toTop + height;
305 float scaleFactor = width / (float) mContentView.getWidth();
Michael Kolbc3af0672011-08-09 10:24:41 -0700306 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700307 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700308 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700309 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700310 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700311 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700312 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700313 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700314 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700315 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700316 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700317 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700318 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700319 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700320 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700321 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
322 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700323 blend1.setDuration(100);
324
325 inanim.playTogether(tx, ty, tr, tb, sx, title);
326 inanim.setDuration(200);
327 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700328 @Override
329 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700330 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700331 finishAnimationIn();
332 mUiController.setBlockEvents(false);
333 }
334 });
Michael Kolba3194d02011-09-07 11:23:51 -0700335 set1.playSequentially(inanim, blend1);
336 set1.start();
Bijan Amirzadafd75eae2014-04-17 17:09:12 -0700337 mUiController.setBlockEvents(false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700338 }
339
340 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700341 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700342 // notify accessibility manager about the screen change
343 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
344 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
345 }
Michael Kolbf2055602011-04-09 17:20:03 -0700346 }
347
Michael Kolba3194d02011-09-07 11:23:51 -0700348 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400349 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700350 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700351 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700352 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700353 if (tab != null) {
354 setActiveTab(tab);
355 } else if (mTabControl.getTabCount() > 0) {
356 // use a fallback tab
357 setActiveTab(mTabControl.getCurrentTab());
358 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700359 mContentView.setVisibility(View.VISIBLE);
360 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700361 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700362 }
Michael Kolba3194d02011-09-07 11:23:51 -0700363 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700364 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700365 if (mTabControl.getTabCount() > 0) {
366 // use a fallback tab
367 setActiveTab(mTabControl.getCurrentTab());
368 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700369 mContentView.setVisibility(View.VISIBLE);
370 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700371 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700372 }
373 mUiController.setBlockEvents(true);
374 mUiController.setActiveTab(tab);
375 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700376 if (mAnimScreen == null) {
377 mAnimScreen = new AnimScreen(mActivity);
378 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700379 ImageView target = tabview.mImage;
380 int width = target.getDrawable().getIntrinsicWidth();
381 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700382 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700383 if (bm == null)
384 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
385 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800386 if (mAnimScreen.mMain.getParent() == null) {
387 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
388 }
John Reckcc0059c2011-10-07 13:53:13 -0700389 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700390 mContentView.getHeight());
Michael Kolba3194d02011-09-07 11:23:51 -0700391 mNavScreen.mScroller.finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700392 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700393 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700394 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700395 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
396 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700397 int fromRight = fromLeft + width;
398 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700399 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700400 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700401 mAnimScreen.mContent.setLeft(fromLeft);
402 mAnimScreen.mContent.setTop(fromTop);
403 mAnimScreen.mContent.setRight(fromRight);
404 mAnimScreen.mContent.setBottom(fromBottom);
405 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700406 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700407 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700408 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700409 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700410 set1.setDuration(100);
411 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700412 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700413 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700414 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700415 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700416 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700417 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700418 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700419 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700420 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700421 1f, scaleFactor);
Michael Kolba3194d02011-09-07 11:23:51 -0700422 set2.playTogether(l, t, r, b, scale);
423 set2.setDuration(200);
424 AnimatorSet combo = new AnimatorSet();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400425 combo.playSequentially(set1, set2);
Michael Kolba3194d02011-09-07 11:23:51 -0700426 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700427 @Override
428 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400429 checkTabReady();
430 }
431 });
432 combo.start();
433 }
434
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700435
436 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400437 private void checkTabReady() {
438 boolean isready = true;
439 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700440 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400441 if (tab == null)
442 isready = false;
443 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700444 webview = (BrowserWebView)tab.getWebView();
445 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400446 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700447 }
448 else if (webview.hasCrashed()) {
449 webview.reload();
450 isready = true;
451 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400452 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700453 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400454 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700455 // Post only when not ready and not crashed
456 if (!isready && mNumTries++ < 150) {
457 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400458 public void run() {
459 checkTabReady();
460 }
461 }, 17); //WebView is not ready. check again in for next frame.
462 return;
463 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700464 mNumTries = 0;
465 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
466 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400467 public void run() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700468 fadeOutCustomViewContainer(hasCrashed);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400469 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700470 }, 33); //WebView is ready, but give it extra 2 frame's time to display and finish the swaps
Matthew Hui1eb7a832014-04-15 12:20:58 -0400471 }
472
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700473 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400474 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700475 if (hasCrashed)
476 otheralpha.setDuration(300);
477 else
478 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400479 otheralpha.addListener(new AnimatorListenerAdapter() {
480 @Override
481 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700482 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700483 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700484 mUiController.setBlockEvents(false);
485 }
486 });
Matthew Hui1eb7a832014-04-15 12:20:58 -0400487 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700488 }
489
Michael Kolbbf32cd02011-08-16 15:07:04 -0700490 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700491 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700492 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700493 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700494 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700495 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700496 }
497
John Reck6b4bd5f2011-07-12 10:14:38 -0700498 @Override
499 public boolean needsRestoreAllTabs() {
500 return false;
501 }
502
Michael Kolb20be26d2011-07-18 16:38:02 -0700503 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700504 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700505 showNavScreen();
506 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700507 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700508 }
509 }
510
John Reck1cf4b792011-07-26 10:22:22 -0700511 @Override
512 public boolean shouldCaptureThumbnails() {
513 return true;
514 }
515
Michael Kolbc3af0672011-08-09 10:24:41 -0700516 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700517
Michael Kolbc3af0672011-08-09 10:24:41 -0700518 private View mMain;
519 private ImageView mTitle;
520 private ImageView mContent;
521 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700522 private Bitmap mTitleBarBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700523
John Reckcc0059c2011-10-07 13:53:13 -0700524 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700525 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
526 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700527 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700528 mContent = (ImageView) mMain.findViewById(R.id.content);
529 mContent.setScaleType(ImageView.ScaleType.MATRIX);
530 mContent.setImageMatrix(new Matrix());
531 mScale = 1.0f;
532 setScaleFactor(getScaleFactor());
533 }
534
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700535 public void set(TitleBar tbar, Bitmap viewportBitmap) {
536 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700537 return;
538 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800539 int embTbarHeight = tbar.getEmbeddedHeight();
540 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
541 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700542 if (mTitleBarBitmap == null
543 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800544 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700545 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800546 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700547 }
John Recka98c83b2011-10-31 12:31:58 -0700548 if (mTitleBarBitmap != null) {
549 Canvas c = new Canvas(mTitleBarBitmap);
550 tbar.draw(c);
551 c.setBitmap(null);
552 }
John Reck1adf0302011-10-11 10:58:12 -0700553 } else {
554 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700555 }
John Reckcc0059c2011-10-07 13:53:13 -0700556 mTitle.setImageBitmap(mTitleBarBitmap);
557 mTitle.setVisibility(View.VISIBLE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700558
559 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700560 }
561
John Recka98c83b2011-10-31 12:31:58 -0700562 private Bitmap safeCreateBitmap(int width, int height) {
563 if (width <= 0 || height <= 0) {
564 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
565 + ", height: " + height);
566 return null;
567 }
568 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
569 }
570
John Reckcc0059c2011-10-07 13:53:13 -0700571 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700572 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700573 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700574 }
575
John Reckcc0059c2011-10-07 13:53:13 -0700576 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700577 mScale = sf;
578 Matrix m = new Matrix();
579 m.postScale(sf,sf);
580 mContent.setImageMatrix(m);
581 }
582
John Reckcc0059c2011-10-07 13:53:13 -0700583 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700584 return mScale;
585 }
586
John Reck3ba45532011-08-11 16:26:53 -0700587 }
588
Michael Kolb66706532010-12-12 19:50:22 -0800589}