blob: 900abffed580dd17e501a5c5a94931aeee3e7a35 [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;
John Reck9c5004e2011-10-07 16:00:12 -070028import android.os.Message;
Michael Kolb376b5412010-12-15 11:52:57 -080029import android.util.Log;
John Reckaae029c2011-09-16 14:36:39 -070030import android.util.TypedValue;
Michael Kolb66706532010-12-12 19:50:22 -080031import android.view.ActionMode;
Michael Kolba4183062011-01-16 10:43:21 -080032import android.view.KeyEvent;
Michael Kolbc3af0672011-08-09 10:24:41 -070033import android.view.LayoutInflater;
Michael Kolb66706532010-12-12 19:50:22 -080034import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070035import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080036import android.view.View;
Michael Kolb30adae62011-07-29 13:37:05 -070037import android.view.accessibility.AccessibilityEvent;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080038import org.codeaurora.swe.WebView;
Michael Kolbc3af0672011-08-09 10:24:41 -070039import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080040
Bijan Amirzada41242f22014-03-21 12:12:18 -070041import com.android.browser.R;
42import com.android.browser.UrlInputView.StateListener;
Michael Kolb629b22c2011-07-13 16:26:47 -070043
Michael Kolb66706532010-12-12 19:50:22 -080044/**
45 * Ui for regular phone screen sizes
46 */
47public class PhoneUi extends BaseUi {
48
49 private static final String LOGTAG = "PhoneUi";
John Reck9c5004e2011-10-07 16:00:12 -070050 private static final int MSG_INIT_NAVSCREEN = 100;
Michael Kolb66706532010-12-12 19:50:22 -080051
Michael Kolbf2055602011-04-09 17:20:03 -070052 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070053 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070054 private NavigationBarPhone mNavigationBar;
John Reckaae029c2011-09-16 14:36:39 -070055 private int mActionBarHeight;
Michael Kolb66706532010-12-12 19:50:22 -080056
Michael Kolb08a687a2011-04-22 16:13:02 -070057 boolean mAnimating;
yijunx.zhu2230a312012-07-27 13:37:00 -040058 boolean mShowNav = false;
Michael Kolb66706532010-12-12 19:50:22 -080059
60 /**
61 * @param browser
62 * @param controller
63 */
64 public PhoneUi(Activity browser, UiController controller) {
65 super(browser, controller);
Michael Kolbfdb70242011-03-24 09:41:11 -070066 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070067 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reckaae029c2011-09-16 14:36:39 -070068 TypedValue heightValue = new TypedValue();
69 browser.getTheme().resolveAttribute(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080070 android.R.attr.actionBarSize, heightValue, true);
John Reckaae029c2011-09-16 14:36:39 -070071 mActionBarHeight = TypedValue.complexToDimensionPixelSize(heightValue.data,
72 browser.getResources().getDisplayMetrics());
John Reck285ef042011-02-11 15:44:17 -080073 }
Michael Kolb66706532010-12-12 19:50:22 -080074
John Reck285ef042011-02-11 15:44:17 -080075 @Override
Michael Kolb66706532010-12-12 19:50:22 -080076 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080077 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080078 }
79
80 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -070081 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -070082 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070083 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070084 }
yijunx.zhu2230a312012-07-27 13:37:00 -040085 //Do nothing while at Nav show screen.
86 if (mShowNav) return;
Michael Kolb1f9b3562012-04-24 14:38:34 -070087 super.editUrl(clearInput, forceIME);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080088 }
89
90 @Override
Michael Kolb66706532010-12-12 19:50:22 -080091 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070092 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070093 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070094 return true;
Michael Kolb66706532010-12-12 19:50:22 -080095 }
96 return super.onBackKey();
97 }
98
John Reckcc0059c2011-10-07 13:53:13 -070099 private boolean showingNavScreen() {
100 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
101 }
102
Michael Kolb66706532010-12-12 19:50:22 -0800103 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700104 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700105 return false;
106 }
107
108 @Override
John Reck30c714c2010-12-16 17:30:34 -0800109 public void onProgressChanged(Tab tab) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700110 super.onProgressChanged(tab);
John Reck9c5004e2011-10-07 16:00:12 -0700111 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
112 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
113 }
114 }
115
116 @Override
117 protected void handleMessage(Message msg) {
118 super.handleMessage(msg);
119 if (msg.what == MSG_INIT_NAVSCREEN) {
120 if (mNavScreen == null) {
121 mNavScreen = new NavScreen(mActivity, mUiController, this);
122 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
123 mNavScreen.setVisibility(View.GONE);
124 }
125 if (mAnimScreen == null) {
126 mAnimScreen = new AnimScreen(mActivity);
127 // initialize bitmaps
128 mAnimScreen.set(getTitleBar(), getWebView());
129 }
130 }
Michael Kolb66706532010-12-12 19:50:22 -0800131 }
132
133 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700134 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700135 mTitleBar.cancelTitleBarAnimation(true);
136 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700137 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400138
139 //if at Nav screen show, detach tab like what showNavScreen() do.
140 if (mShowNav) {
141 detachTab(mActiveTab);
142 }
143
Michael Kolbfdb70242011-03-24 09:41:11 -0700144 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800145 // TabControl.setCurrentTab has been called before this,
146 // so the tab is guaranteed to have a webview
147 if (view == null) {
148 Log.e(LOGTAG, "active tab with no webview detected");
149 return;
150 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700151 // Request focus on the top window.
152 if (mUseQuickControls) {
153 mPieControl.forceToTop(mContentView);
Michael Kolb4923c222012-04-02 16:18:36 -0700154 view.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700155 mTitleBar.setShowProgressOnly(true);
Michael Kolbfdb70242011-03-24 09:41:11 -0700156 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700157 view.setTitleBar(mTitleBar);
Michael Kolbfdb70242011-03-24 09:41:11 -0700158 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700159 // update nav bar state
160 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700161 updateLockIconToLatest(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700162 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800163 }
164
Michael Kolb66706532010-12-12 19:50:22 -0800165 // menu handling callbacks
166
167 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700168 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700169 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700170 return true;
171 }
172
173 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700174 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800175 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
176 if (bm != null) {
177 bm.setVisible(!showingNavScreen());
178 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700179 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
180 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700181 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700182 }
Michael Kolb315d5022011-10-13 12:47:11 -0700183 MenuItem info = menu.findItem(R.id.page_info_menu_id);
184 if (info != null) {
185 info.setVisible(false);
186 }
187 MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
188 if (newtab != null && !mUseQuickControls) {
189 newtab.setVisible(false);
190 }
191 MenuItem incognito = menu.findItem(R.id.incognito_menu_id);
192 if (incognito != null) {
193 incognito.setVisible(showingNavScreen() || mUseQuickControls);
194 }
Afzal Najamd4e33312012-04-26 01:54:01 -0400195 MenuItem closeOthers = menu.findItem(R.id.close_other_tabs_id);
196 if (closeOthers != null) {
197 boolean isLastTab = true;
198 if (tab != null) {
199 isLastTab = (mTabControl.getTabCount() <= 1);
200 }
201 closeOthers.setEnabled(!isLastTab);
202 }
John Reckcc0059c2011-10-07 13:53:13 -0700203 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700204 menu.setGroupVisible(R.id.LIVE_MENU, false);
205 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();
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700240 } else {
241 mTitleBar.setTranslationY(mActionBarHeight);
Michael Kolb42c0c062011-08-02 12:56:06 -0700242 }
Michael Kolb66706532010-12-12 19:50:22 -0800243 }
244
Michael Kolba4183062011-01-16 10:43:21 -0800245 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700246 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700247 super.onActionModeFinished(inLoad);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700248 mTitleBar.setTranslationY(0);
Michael Kolbc16c5952011-03-29 15:37:03 -0700249 if (inLoad) {
250 if (mUseQuickControls) {
251 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700252 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700253 showTitleBar();
254 }
John Reck6cda7b12011-03-18 15:57:13 -0700255 }
256
257 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700258 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700259 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700260 }
261
262 @Override
263 public void showWeb(boolean animate) {
264 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700265 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700266 }
267
Michael Kolbf2055602011-04-09 17:20:03 -0700268 void showNavScreen() {
yijunx.zhu2230a312012-07-27 13:37:00 -0400269 mShowNav = true;
Vivek Sekhar4ddafb12014-07-22 15:33:16 -0700270 dismissIME();
Michael Kolbc3af0672011-08-09 10:24:41 -0700271 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700272 if (mNavScreen == null) {
273 mNavScreen = new NavScreen(mActivity, mUiController, this);
274 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
275 } else {
276 mNavScreen.setVisibility(View.VISIBLE);
277 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700278 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700279 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700280 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700281 if (mAnimScreen == null) {
282 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700283 } else {
284 mAnimScreen.mMain.setAlpha(1f);
285 mAnimScreen.mTitle.setAlpha(1f);
286 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700287 }
288 mAnimScreen.set(getTitleBar(), getWebView());
Michael Kolba30e4fb2011-11-07 17:32:01 -0800289 if (mAnimScreen.mMain.getParent() == null) {
290 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
291 }
Michael Kolbf2055602011-04-09 17:20:03 -0700292 mCustomViewContainer.setVisibility(View.VISIBLE);
293 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700294 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
295 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700296 int fromLeft = 0;
297 int fromTop = getTitleBar().getHeight();
298 int fromRight = mContentView.getWidth();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800299 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
300 int fromBottom = mContentView.getHeight() + fixedTbarHeight;
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 }
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700382 int width = mContentView.getWidth();
383 int height = mContentView.getHeight();
384 Bitmap bm = tab.getFullScreenshot();
385 if (bm == null)
386 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
387 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800388 if (mAnimScreen.mMain.getParent() == null) {
389 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
390 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800391 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
John Reckcc0059c2011-10-07 13:53:13 -0700392 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800393 mContentView.getHeight() + fixedTbarHeight);
Michael Kolba3194d02011-09-07 11:23:51 -0700394 mNavScreen.mScroller.finishScroller();
395 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700396 int toLeft = 0;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800397 int toTop = 0;
398 if (mTitleBar.isFixed()) {
399 toTop = fixedTbarHeight;
400 } else {
401 toTop = (tab.getWebView() != null) ? tab.getWebView().getVisibleTitleHeight() : 0;
402 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700403 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700404 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
405 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700406 int fromRight = fromLeft + target.getDrawable().getIntrinsicWidth();
407 int fromBottom = fromTop + target.getDrawable().getIntrinsicHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700408 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700409 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700410 mAnimScreen.mContent.setLeft(fromLeft);
411 mAnimScreen.mContent.setTop(fromTop);
412 mAnimScreen.mContent.setRight(fromRight);
413 mAnimScreen.mContent.setBottom(fromBottom);
414 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700415 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700416 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700417 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700418 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700419 set1.setDuration(100);
420 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700421 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700422 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700423 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700424 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700425 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700426 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700427 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700428 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700429 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700430 1f, scaleFactor);
Michael Kolba3194d02011-09-07 11:23:51 -0700431 set2.playTogether(l, t, r, b, scale);
432 set2.setDuration(200);
433 AnimatorSet combo = new AnimatorSet();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400434 combo.playSequentially(set1, set2);
Michael Kolba3194d02011-09-07 11:23:51 -0700435 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700436 @Override
437 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400438 checkTabReady();
439 }
440 });
441 combo.start();
442 }
443
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700444
445 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400446 private void checkTabReady() {
447 boolean isready = true;
448 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700449 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400450 if (tab == null)
451 isready = false;
452 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700453 webview = (BrowserWebView)tab.getWebView();
454 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400455 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700456 }
457 else if (webview.hasCrashed()) {
458 webview.reload();
459 isready = true;
460 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400461 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700462 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400463 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700464 // Post only when not ready and not crashed
465 if (!isready && mNumTries++ < 150) {
466 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400467 public void run() {
468 checkTabReady();
469 }
470 }, 17); //WebView is not ready. check again in for next frame.
471 return;
472 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700473 mNumTries = 0;
474 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
475 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400476 public void run() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700477 fadeOutCustomViewContainer(hasCrashed);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400478 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700479 }, 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 -0400480 }
481
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700482 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400483 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700484 if (hasCrashed)
485 otheralpha.setDuration(300);
486 else
487 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400488 otheralpha.addListener(new AnimatorListenerAdapter() {
489 @Override
490 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700491 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700492 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700493 mUiController.setBlockEvents(false);
494 }
495 });
Matthew Hui1eb7a832014-04-15 12:20:58 -0400496 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700497 }
498
Michael Kolbbf32cd02011-08-16 15:07:04 -0700499 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700500 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700501 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700502 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700503 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700504 }
505
John Reck6b4bd5f2011-07-12 10:14:38 -0700506 @Override
507 public boolean needsRestoreAllTabs() {
508 return false;
509 }
510
Michael Kolb20be26d2011-07-18 16:38:02 -0700511 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700512 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700513 showNavScreen();
514 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700515 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700516 }
517 }
518
John Reck1cf4b792011-07-26 10:22:22 -0700519 @Override
520 public boolean shouldCaptureThumbnails() {
521 return true;
522 }
523
Michael Kolbc3af0672011-08-09 10:24:41 -0700524 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700525
Michael Kolbc3af0672011-08-09 10:24:41 -0700526 private View mMain;
527 private ImageView mTitle;
528 private ImageView mContent;
529 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700530 private Bitmap mTitleBarBitmap;
531 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700532
John Reckcc0059c2011-10-07 13:53:13 -0700533 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700534 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
535 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700536 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700537 mContent = (ImageView) mMain.findViewById(R.id.content);
538 mContent.setScaleType(ImageView.ScaleType.MATRIX);
539 mContent.setImageMatrix(new Matrix());
540 mScale = 1.0f;
541 setScaleFactor(getScaleFactor());
542 }
543
544 public void set(TitleBar tbar, WebView web) {
John Reck62077d82011-10-13 15:17:50 -0700545 if (tbar == null || web == null) {
546 return;
547 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800548 int embTbarHeight = tbar.getEmbeddedHeight();
549 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
550 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700551 if (mTitleBarBitmap == null
552 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800553 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700554 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800555 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700556 }
John Recka98c83b2011-10-31 12:31:58 -0700557 if (mTitleBarBitmap != null) {
558 Canvas c = new Canvas(mTitleBarBitmap);
559 tbar.draw(c);
560 c.setBitmap(null);
561 }
John Reck1adf0302011-10-11 10:58:12 -0700562 } else {
563 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700564 }
John Reckcc0059c2011-10-07 13:53:13 -0700565 mTitle.setImageBitmap(mTitleBarBitmap);
566 mTitle.setVisibility(View.VISIBLE);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800567 // SWE: WebView.draw() wouldn't draw anything if SurfaceView is enabled.
Vivek Sekhar0ab52512014-07-02 14:51:32 -0700568 if (mContentBitmap != null) {
569 mContentBitmap.recycle();
570 mContentBitmap = null;
571 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800572 mContentBitmap = web.getViewportBitmap();
573 if (mContentBitmap == null) {
574 int h = web.getHeight() - embTbarHeight;
575 if (mContentBitmap == null
576 || mContentBitmap.getWidth() != web.getWidth()
577 || mContentBitmap.getHeight() != h) {
578 mContentBitmap = safeCreateBitmap(web.getWidth(), h);
579 }
580 if (mContentBitmap != null) {
581 Canvas c = new Canvas(mContentBitmap);
582 int tx = web.getScrollX();
583 int ty = web.getScrollY();
584 c.translate(-tx, -ty - embTbarHeight);
585 web.draw(c);
586 c.setBitmap(null);
587 }
John Recka98c83b2011-10-31 12:31:58 -0700588 }
John Reckcc0059c2011-10-07 13:53:13 -0700589 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700590 }
591
John Recka98c83b2011-10-31 12:31:58 -0700592 private Bitmap safeCreateBitmap(int width, int height) {
593 if (width <= 0 || height <= 0) {
594 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
595 + ", height: " + height);
596 return null;
597 }
598 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
599 }
600
John Reckcc0059c2011-10-07 13:53:13 -0700601 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700602 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700603 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700604 }
605
John Reckcc0059c2011-10-07 13:53:13 -0700606 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700607 mScale = sf;
608 Matrix m = new Matrix();
609 m.postScale(sf,sf);
610 mContent.setImageMatrix(m);
611 }
612
John Reckcc0059c2011-10-07 13:53:13 -0700613 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700614 return mScale;
615 }
616
John Reck3ba45532011-08-11 16:26:53 -0700617 }
618
Michael Kolb66706532010-12-12 19:50:22 -0800619}