blob: a2799b39d3a601dc64ea792c992c273c0598a374 [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() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700254 WebView webView = getWebView();
255 if (webView != null) {
256 mShowNav = true;
257 dismissIME();
258 mUiController.setBlockEvents(true);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700259
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700260 webView.getContentBitmapAsync(1.0f,
261 new Rect(),
262 new ValueCallback<Bitmap>() {
263 @Override
264 public void onReceiveValue(Bitmap bitmap) {
265 onShowNavScreenContinue(bitmap);
266 }
267 });
268 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700269 }
270
271 void onShowNavScreenContinue(Bitmap viewportBitmap) {
272
John Reckcc0059c2011-10-07 13:53:13 -0700273 if (mNavScreen == null) {
274 mNavScreen = new NavScreen(mActivity, mUiController, this);
275 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
276 } else {
277 mNavScreen.setVisibility(View.VISIBLE);
278 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700279 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700280 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700281 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700282 if (mAnimScreen == null) {
283 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700284 } else {
285 mAnimScreen.mMain.setAlpha(1f);
286 mAnimScreen.mTitle.setAlpha(1f);
287 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700288 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700289 mAnimScreen.set(getTitleBar(), viewportBitmap);
Michael Kolba30e4fb2011-11-07 17:32:01 -0800290 if (mAnimScreen.mMain.getParent() == null) {
291 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
292 }
Michael Kolbf2055602011-04-09 17:20:03 -0700293 mCustomViewContainer.setVisibility(View.VISIBLE);
294 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700295 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
296 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700297 int fromLeft = 0;
298 int fromTop = getTitleBar().getHeight();
299 int fromRight = mContentView.getWidth();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700300 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700301 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
302 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700303 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700304 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700305 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700306 int toRight = toLeft + width;
307 int toBottom = toTop + height;
308 float scaleFactor = width / (float) mContentView.getWidth();
Michael Kolbc3af0672011-08-09 10:24:41 -0700309 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700310 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700311 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700312 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700313 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700314 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700315 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700316 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700317 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700318 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700319 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700320 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700321 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700322 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700323 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700324 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
325 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700326 blend1.setDuration(100);
327
328 inanim.playTogether(tx, ty, tr, tb, sx, title);
329 inanim.setDuration(200);
330 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700331 @Override
332 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700333 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700334 finishAnimationIn();
335 mUiController.setBlockEvents(false);
336 }
337 });
Michael Kolba3194d02011-09-07 11:23:51 -0700338 set1.playSequentially(inanim, blend1);
339 set1.start();
Bijan Amirzadafd75eae2014-04-17 17:09:12 -0700340 mUiController.setBlockEvents(false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700341 }
342
343 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700344 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700345 // notify accessibility manager about the screen change
346 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
347 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
348 }
Michael Kolbf2055602011-04-09 17:20:03 -0700349 }
350
Michael Kolba3194d02011-09-07 11:23:51 -0700351 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400352 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700353 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700354 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700355 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700356 if (tab != null) {
357 setActiveTab(tab);
358 } else if (mTabControl.getTabCount() > 0) {
359 // use a fallback tab
360 setActiveTab(mTabControl.getCurrentTab());
361 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700362 mContentView.setVisibility(View.VISIBLE);
363 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700364 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700365 }
Michael Kolba3194d02011-09-07 11:23:51 -0700366 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700367 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700368 if (mTabControl.getTabCount() > 0) {
369 // use a fallback tab
370 setActiveTab(mTabControl.getCurrentTab());
371 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700372 mContentView.setVisibility(View.VISIBLE);
373 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700374 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700375 }
376 mUiController.setBlockEvents(true);
377 mUiController.setActiveTab(tab);
378 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700379 if (mAnimScreen == null) {
380 mAnimScreen = new AnimScreen(mActivity);
381 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700382 ImageView target = tabview.mImage;
383 int width = target.getDrawable().getIntrinsicWidth();
384 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700385 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700386 if (bm == null)
387 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
388 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800389 if (mAnimScreen.mMain.getParent() == null) {
390 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
391 }
John Reckcc0059c2011-10-07 13:53:13 -0700392 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700393 mContentView.getHeight());
Michael Kolba3194d02011-09-07 11:23:51 -0700394 mNavScreen.mScroller.finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700395 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700396 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700397 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700398 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
399 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700400 int fromRight = fromLeft + width;
401 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700402 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700403 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700404 mAnimScreen.mContent.setLeft(fromLeft);
405 mAnimScreen.mContent.setTop(fromTop);
406 mAnimScreen.mContent.setRight(fromRight);
407 mAnimScreen.mContent.setBottom(fromBottom);
408 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700409 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700410 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700411 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700412 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700413 set1.setDuration(100);
414 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700415 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700416 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700417 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700418 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700419 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700420 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700421 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700422 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700423 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700424 1f, scaleFactor);
Michael Kolba3194d02011-09-07 11:23:51 -0700425 set2.playTogether(l, t, r, b, scale);
426 set2.setDuration(200);
427 AnimatorSet combo = new AnimatorSet();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400428 combo.playSequentially(set1, set2);
Michael Kolba3194d02011-09-07 11:23:51 -0700429 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700430 @Override
431 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400432 checkTabReady();
433 }
434 });
435 combo.start();
436 }
437
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700438
439 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400440 private void checkTabReady() {
441 boolean isready = true;
442 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700443 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400444 if (tab == null)
445 isready = false;
446 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700447 webview = (BrowserWebView)tab.getWebView();
448 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400449 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700450 }
451 else if (webview.hasCrashed()) {
452 webview.reload();
453 isready = true;
454 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400455 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700456 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400457 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700458 // Post only when not ready and not crashed
459 if (!isready && mNumTries++ < 150) {
460 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400461 public void run() {
462 checkTabReady();
463 }
464 }, 17); //WebView is not ready. check again in for next frame.
465 return;
466 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700467 mNumTries = 0;
468 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
469 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400470 public void run() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700471 fadeOutCustomViewContainer(hasCrashed);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400472 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700473 }, 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 -0400474 }
475
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700476 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400477 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700478 if (hasCrashed)
479 otheralpha.setDuration(300);
480 else
481 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400482 otheralpha.addListener(new AnimatorListenerAdapter() {
483 @Override
484 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700485 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700486 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700487 mUiController.setBlockEvents(false);
488 }
489 });
Matthew Hui1eb7a832014-04-15 12:20:58 -0400490 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700491 }
492
Michael Kolbbf32cd02011-08-16 15:07:04 -0700493 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700494 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700495 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700496 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700497 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700498 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700499 }
500
John Reck6b4bd5f2011-07-12 10:14:38 -0700501 @Override
502 public boolean needsRestoreAllTabs() {
503 return false;
504 }
505
Michael Kolb20be26d2011-07-18 16:38:02 -0700506 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700507 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700508 showNavScreen();
509 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700510 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700511 }
512 }
513
John Reck1cf4b792011-07-26 10:22:22 -0700514 @Override
515 public boolean shouldCaptureThumbnails() {
516 return true;
517 }
518
Michael Kolbc3af0672011-08-09 10:24:41 -0700519 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700520
Michael Kolbc3af0672011-08-09 10:24:41 -0700521 private View mMain;
522 private ImageView mTitle;
523 private ImageView mContent;
524 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700525 private Bitmap mTitleBarBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700526
John Reckcc0059c2011-10-07 13:53:13 -0700527 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700528 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
529 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700530 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700531 mContent = (ImageView) mMain.findViewById(R.id.content);
532 mContent.setScaleType(ImageView.ScaleType.MATRIX);
533 mContent.setImageMatrix(new Matrix());
534 mScale = 1.0f;
535 setScaleFactor(getScaleFactor());
536 }
537
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700538 public void set(TitleBar tbar, Bitmap viewportBitmap) {
539 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700540 return;
541 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800542 int embTbarHeight = tbar.getEmbeddedHeight();
543 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
544 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700545 if (mTitleBarBitmap == null
546 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800547 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700548 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800549 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700550 }
John Recka98c83b2011-10-31 12:31:58 -0700551 if (mTitleBarBitmap != null) {
552 Canvas c = new Canvas(mTitleBarBitmap);
553 tbar.draw(c);
554 c.setBitmap(null);
555 }
John Reck1adf0302011-10-11 10:58:12 -0700556 } else {
557 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700558 }
John Reckcc0059c2011-10-07 13:53:13 -0700559 mTitle.setImageBitmap(mTitleBarBitmap);
560 mTitle.setVisibility(View.VISIBLE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700561
562 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700563 }
564
John Recka98c83b2011-10-31 12:31:58 -0700565 private Bitmap safeCreateBitmap(int width, int height) {
566 if (width <= 0 || height <= 0) {
567 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
568 + ", height: " + height);
569 return null;
570 }
571 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
572 }
573
John Reckcc0059c2011-10-07 13:53:13 -0700574 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700575 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700576 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700577 }
578
John Reckcc0059c2011-10-07 13:53:13 -0700579 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700580 mScale = sf;
581 Matrix m = new Matrix();
582 m.postScale(sf,sf);
583 mContent.setImageMatrix(m);
584 }
585
John Reckcc0059c2011-10-07 13:53:13 -0700586 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700587 return mScale;
588 }
589
John Reck3ba45532011-08-11 16:26:53 -0700590 }
591
Michael Kolb66706532010-12-12 19:50:22 -0800592}