blob: 35e6ad300fea5140f11da3e957a40ba3aeeb4326 [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);
Michael Kolbfdb70242011-03-24 09:41:11 -070069 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070070 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
Vivek Sekhar60eb9802014-07-21 19:13:33 -070071 mBrowser = browser;
John Reck285ef042011-02-11 15:44:17 -080072 }
Michael Kolb66706532010-12-12 19:50:22 -080073
John Reck285ef042011-02-11 15:44:17 -080074 @Override
Michael Kolb66706532010-12-12 19:50:22 -080075 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080076 hideTitleBar();
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) {
Michael Kolb46f987e2011-04-05 10:39:10 -070081 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070082 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070083 }
yijunx.zhu2230a312012-07-27 13:37:00 -040084 //Do nothing while at Nav show screen.
85 if (mShowNav) return;
Michael Kolb1f9b3562012-04-24 14:38:34 -070086 super.editUrl(clearInput, forceIME);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080087 }
88
89 @Override
Michael Kolb66706532010-12-12 19:50:22 -080090 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070091 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070092 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070093 return true;
Michael Kolb66706532010-12-12 19:50:22 -080094 }
95 return super.onBackKey();
96 }
97
John Reckcc0059c2011-10-07 13:53:13 -070098 private boolean showingNavScreen() {
99 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
100 }
101
Michael Kolb66706532010-12-12 19:50:22 -0800102 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700103 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700104 return false;
105 }
106
107 @Override
John Reck30c714c2010-12-16 17:30:34 -0800108 public void onProgressChanged(Tab tab) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700109 super.onProgressChanged(tab);
John Reck9c5004e2011-10-07 16:00:12 -0700110 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
111 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
112 }
113 }
114
115 @Override
116 protected void handleMessage(Message msg) {
117 super.handleMessage(msg);
118 if (msg.what == MSG_INIT_NAVSCREEN) {
119 if (mNavScreen == null) {
120 mNavScreen = new NavScreen(mActivity, mUiController, this);
121 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
122 mNavScreen.setVisibility(View.GONE);
123 }
124 if (mAnimScreen == null) {
125 mAnimScreen = new AnimScreen(mActivity);
126 // initialize bitmaps
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700127 //mAnimScreen.set(getTitleBar(), getWebView());
John Reck9c5004e2011-10-07 16:00:12 -0700128 }
129 }
Michael Kolb66706532010-12-12 19:50:22 -0800130 }
131
132 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700133 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700134 mTitleBar.cancelTitleBarAnimation(true);
135 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700136 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400137
138 //if at Nav screen show, detach tab like what showNavScreen() do.
139 if (mShowNav) {
140 detachTab(mActiveTab);
141 }
142
Michael Kolbfdb70242011-03-24 09:41:11 -0700143 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800144 // TabControl.setCurrentTab has been called before this,
145 // so the tab is guaranteed to have a webview
146 if (view == null) {
147 Log.e(LOGTAG, "active tab with no webview detected");
148 return;
149 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700150 // Request focus on the top window.
151 if (mUseQuickControls) {
152 mPieControl.forceToTop(mContentView);
Michael Kolb4923c222012-04-02 16:18:36 -0700153 view.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700154 mTitleBar.setShowProgressOnly(true);
Michael Kolbfdb70242011-03-24 09:41:11 -0700155 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700156 view.setTitleBar(mTitleBar);
Michael Kolbfdb70242011-03-24 09:41:11 -0700157 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700158 // update nav bar state
159 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700160 updateLockIconToLatest(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700161 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800162 }
163
Michael Kolb66706532010-12-12 19:50:22 -0800164 // menu handling callbacks
165
166 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700167 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700168 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700169 return true;
170 }
171
172 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700173 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800174 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
175 if (bm != null) {
176 bm.setVisible(!showingNavScreen());
177 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700178 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
179 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700180 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700181 }
Michael Kolb315d5022011-10-13 12:47:11 -0700182 MenuItem info = menu.findItem(R.id.page_info_menu_id);
183 if (info != null) {
184 info.setVisible(false);
185 }
186 MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
187 if (newtab != null && !mUseQuickControls) {
188 newtab.setVisible(false);
189 }
190 MenuItem incognito = menu.findItem(R.id.incognito_menu_id);
191 if (incognito != null) {
192 incognito.setVisible(showingNavScreen() || mUseQuickControls);
193 }
Afzal Najamd4e33312012-04-26 01:54:01 -0400194 MenuItem closeOthers = menu.findItem(R.id.close_other_tabs_id);
195 if (closeOthers != null) {
196 boolean isLastTab = true;
197 if (tab != null) {
198 isLastTab = (mTabControl.getTabCount() <= 1);
199 }
200 closeOthers.setEnabled(!isLastTab);
201 }
John Reckcc0059c2011-10-07 13:53:13 -0700202 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700203 menu.setGroupVisible(R.id.LIVE_MENU, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700204 menu.setGroupVisible(R.id.OFFLINE_READING, false);
John Recke1a03a32011-09-14 17:04:16 -0700205 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700206 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb315d5022011-10-13 12:47:11 -0700207 menu.setGroupVisible(R.id.COMBO_MENU, true);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700208 }
209 }
210
211 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700212 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700213 if (showingNavScreen()
214 && (item.getItemId() != R.id.history_menu_id)
215 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700216 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700217 }
218 return false;
219 }
220
221 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800222 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800223 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800224 }
225
226 @Override
227 public void onContextMenuClosed(Menu menu, boolean inLoad) {
228 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800229 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231 }
232
233 // action mode callbacks
234
235 @Override
236 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700237 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700238 if (!isEditingUrl()) {
239 hideTitleBar();
240 }
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242
Michael Kolba4183062011-01-16 10:43:21 -0800243 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700244 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700245 super.onActionModeFinished(inLoad);
Michael Kolbc16c5952011-03-29 15:37:03 -0700246 if (inLoad) {
247 if (mUseQuickControls) {
248 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700249 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700250 showTitleBar();
251 }
John Reck6cda7b12011-03-18 15:57:13 -0700252 }
253
254 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700255 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700256 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700257 }
258
259 @Override
260 public void showWeb(boolean animate) {
261 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700262 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700263 }
264
Michael Kolbf2055602011-04-09 17:20:03 -0700265 void showNavScreen() {
yijunx.zhu2230a312012-07-27 13:37:00 -0400266 mShowNav = true;
Vivek Sekhar4ddafb12014-07-22 15:33:16 -0700267 dismissIME();
Michael Kolbc3af0672011-08-09 10:24:41 -0700268 mUiController.setBlockEvents(true);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700269
270 getWebView()
271 .getContentBitmapAsync(1.0f,
272 new Rect(),
273 new ValueCallback<Bitmap>() {
274 @Override
275 public void onReceiveValue(Bitmap bitmap) {
276 onShowNavScreenContinue(bitmap);
277 }});
278 }
279
280 void onShowNavScreenContinue(Bitmap viewportBitmap) {
281
John Reckcc0059c2011-10-07 13:53:13 -0700282 if (mNavScreen == null) {
283 mNavScreen = new NavScreen(mActivity, mUiController, this);
284 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
285 } else {
286 mNavScreen.setVisibility(View.VISIBLE);
287 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700288 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700289 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700290 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700291 if (mAnimScreen == null) {
292 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700293 } else {
294 mAnimScreen.mMain.setAlpha(1f);
295 mAnimScreen.mTitle.setAlpha(1f);
296 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700297 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700298 mAnimScreen.set(getTitleBar(), viewportBitmap);
Michael Kolba30e4fb2011-11-07 17:32:01 -0800299 if (mAnimScreen.mMain.getParent() == null) {
300 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
301 }
Michael Kolbf2055602011-04-09 17:20:03 -0700302 mCustomViewContainer.setVisibility(View.VISIBLE);
303 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700304 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
305 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700306 int fromLeft = 0;
307 int fromTop = getTitleBar().getHeight();
308 int fromRight = mContentView.getWidth();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700309 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700310 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
311 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700312 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700313 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700314 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700315 int toRight = toLeft + width;
316 int toBottom = toTop + height;
317 float scaleFactor = width / (float) mContentView.getWidth();
Michael Kolbc3af0672011-08-09 10:24:41 -0700318 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700319 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700320 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700321 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700322 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700323 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700324 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700325 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700326 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700327 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700328 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700329 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700330 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700331 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700332 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700333 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
334 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700335 blend1.setDuration(100);
336
337 inanim.playTogether(tx, ty, tr, tb, sx, title);
338 inanim.setDuration(200);
339 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700340 @Override
341 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700342 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700343 finishAnimationIn();
344 mUiController.setBlockEvents(false);
345 }
346 });
Michael Kolba3194d02011-09-07 11:23:51 -0700347 set1.playSequentially(inanim, blend1);
348 set1.start();
Bijan Amirzadafd75eae2014-04-17 17:09:12 -0700349 mUiController.setBlockEvents(false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700350 }
351
352 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700353 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700354 // notify accessibility manager about the screen change
355 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
356 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
357 }
Michael Kolbf2055602011-04-09 17:20:03 -0700358 }
359
Michael Kolba3194d02011-09-07 11:23:51 -0700360 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400361 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700362 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700363 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700364 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700365 if (tab != null) {
366 setActiveTab(tab);
367 } else if (mTabControl.getTabCount() > 0) {
368 // use a fallback tab
369 setActiveTab(mTabControl.getCurrentTab());
370 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700371 mContentView.setVisibility(View.VISIBLE);
372 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700373 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700374 }
Michael Kolba3194d02011-09-07 11:23:51 -0700375 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700376 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700377 if (mTabControl.getTabCount() > 0) {
378 // use a fallback tab
379 setActiveTab(mTabControl.getCurrentTab());
380 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700381 mContentView.setVisibility(View.VISIBLE);
382 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700383 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700384 }
385 mUiController.setBlockEvents(true);
386 mUiController.setActiveTab(tab);
387 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700388 if (mAnimScreen == null) {
389 mAnimScreen = new AnimScreen(mActivity);
390 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700391 ImageView target = tabview.mImage;
392 int width = target.getDrawable().getIntrinsicWidth();
393 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700394 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700395 if (bm == null)
396 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
397 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800398 if (mAnimScreen.mMain.getParent() == null) {
399 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
400 }
John Reckcc0059c2011-10-07 13:53:13 -0700401 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700402 mContentView.getHeight());
Michael Kolba3194d02011-09-07 11:23:51 -0700403 mNavScreen.mScroller.finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700404 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700405 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700406 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700407 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
408 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700409 int fromRight = fromLeft + width;
410 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700411 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700412 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700413 mAnimScreen.mContent.setLeft(fromLeft);
414 mAnimScreen.mContent.setTop(fromTop);
415 mAnimScreen.mContent.setRight(fromRight);
416 mAnimScreen.mContent.setBottom(fromBottom);
417 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700418 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700419 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700420 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700421 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700422 set1.setDuration(100);
423 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700424 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700425 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700426 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700427 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700428 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700429 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700430 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700431 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700432 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700433 1f, scaleFactor);
Michael Kolba3194d02011-09-07 11:23:51 -0700434 set2.playTogether(l, t, r, b, scale);
435 set2.setDuration(200);
436 AnimatorSet combo = new AnimatorSet();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400437 combo.playSequentially(set1, set2);
Michael Kolba3194d02011-09-07 11:23:51 -0700438 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700439 @Override
440 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400441 checkTabReady();
442 }
443 });
444 combo.start();
445 }
446
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700447
448 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400449 private void checkTabReady() {
450 boolean isready = true;
451 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700452 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400453 if (tab == null)
454 isready = false;
455 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700456 webview = (BrowserWebView)tab.getWebView();
457 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400458 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700459 }
460 else if (webview.hasCrashed()) {
461 webview.reload();
462 isready = true;
463 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400464 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700465 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400466 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700467 // Post only when not ready and not crashed
468 if (!isready && mNumTries++ < 150) {
469 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400470 public void run() {
471 checkTabReady();
472 }
473 }, 17); //WebView is not ready. check again in for next frame.
474 return;
475 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700476 mNumTries = 0;
477 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
478 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400479 public void run() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700480 fadeOutCustomViewContainer(hasCrashed);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400481 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700482 }, 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 -0400483 }
484
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700485 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400486 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700487 if (hasCrashed)
488 otheralpha.setDuration(300);
489 else
490 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400491 otheralpha.addListener(new AnimatorListenerAdapter() {
492 @Override
493 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700494 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700495 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700496 mUiController.setBlockEvents(false);
497 }
498 });
Matthew Hui1eb7a832014-04-15 12:20:58 -0400499 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700500 }
501
Michael Kolbbf32cd02011-08-16 15:07:04 -0700502 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700503 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700504 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700505 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700506 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700507 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700508 }
509
John Reck6b4bd5f2011-07-12 10:14:38 -0700510 @Override
511 public boolean needsRestoreAllTabs() {
512 return false;
513 }
514
Michael Kolb20be26d2011-07-18 16:38:02 -0700515 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700516 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700517 showNavScreen();
518 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700519 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700520 }
521 }
522
John Reck1cf4b792011-07-26 10:22:22 -0700523 @Override
524 public boolean shouldCaptureThumbnails() {
525 return true;
526 }
527
Michael Kolbc3af0672011-08-09 10:24:41 -0700528 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700529
Michael Kolbc3af0672011-08-09 10:24:41 -0700530 private View mMain;
531 private ImageView mTitle;
532 private ImageView mContent;
533 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700534 private Bitmap mTitleBarBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700535
John Reckcc0059c2011-10-07 13:53:13 -0700536 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700537 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
538 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700539 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700540 mContent = (ImageView) mMain.findViewById(R.id.content);
541 mContent.setScaleType(ImageView.ScaleType.MATRIX);
542 mContent.setImageMatrix(new Matrix());
543 mScale = 1.0f;
544 setScaleFactor(getScaleFactor());
545 }
546
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700547 public void set(TitleBar tbar, Bitmap viewportBitmap) {
548 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700549 return;
550 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800551 int embTbarHeight = tbar.getEmbeddedHeight();
552 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
553 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700554 if (mTitleBarBitmap == null
555 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800556 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700557 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800558 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700559 }
John Recka98c83b2011-10-31 12:31:58 -0700560 if (mTitleBarBitmap != null) {
561 Canvas c = new Canvas(mTitleBarBitmap);
562 tbar.draw(c);
563 c.setBitmap(null);
564 }
John Reck1adf0302011-10-11 10:58:12 -0700565 } else {
566 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700567 }
John Reckcc0059c2011-10-07 13:53:13 -0700568 mTitle.setImageBitmap(mTitleBarBitmap);
569 mTitle.setVisibility(View.VISIBLE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700570
571 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700572 }
573
John Recka98c83b2011-10-31 12:31:58 -0700574 private Bitmap safeCreateBitmap(int width, int height) {
575 if (width <= 0 || height <= 0) {
576 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
577 + ", height: " + height);
578 return null;
579 }
580 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
581 }
582
John Reckcc0059c2011-10-07 13:53:13 -0700583 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700584 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700585 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700586 }
587
John Reckcc0059c2011-10-07 13:53:13 -0700588 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700589 mScale = sf;
590 Matrix m = new Matrix();
591 m.postScale(sf,sf);
592 mContent.setImageMatrix(m);
593 }
594
John Reckcc0059c2011-10-07 13:53:13 -0700595 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700596 return mScale;
597 }
598
John Reck3ba45532011-08-11 16:26:53 -0700599 }
600
Michael Kolb66706532010-12-12 19:50:22 -0800601}