blob: 898acf5d8beea356ba17572b5fed6cde73ee967e [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;
Michael Kolbc3af0672011-08-09 10:24:41 -0700270 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700271 if (mNavScreen == null) {
272 mNavScreen = new NavScreen(mActivity, mUiController, this);
273 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
274 } else {
275 mNavScreen.setVisibility(View.VISIBLE);
276 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700277 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700278 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700279 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700280 if (mAnimScreen == null) {
281 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700282 } else {
283 mAnimScreen.mMain.setAlpha(1f);
284 mAnimScreen.mTitle.setAlpha(1f);
285 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700286 }
287 mAnimScreen.set(getTitleBar(), getWebView());
Michael Kolba30e4fb2011-11-07 17:32:01 -0800288 if (mAnimScreen.mMain.getParent() == null) {
289 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
290 }
Michael Kolbf2055602011-04-09 17:20:03 -0700291 mCustomViewContainer.setVisibility(View.VISIBLE);
292 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700293 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
294 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700295 int fromLeft = 0;
296 int fromTop = getTitleBar().getHeight();
297 int fromRight = mContentView.getWidth();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800298 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
299 int fromBottom = mContentView.getHeight() + fixedTbarHeight;
Michael Kolba3194d02011-09-07 11:23:51 -0700300 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
301 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700302 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700303 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700304 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700305 int toRight = toLeft + width;
306 int toBottom = toTop + height;
307 float scaleFactor = width / (float) mContentView.getWidth();
Michael Kolbc3af0672011-08-09 10:24:41 -0700308 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700309 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700310 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700311 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700312 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700313 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700314 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700315 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700316 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700317 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700318 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700319 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700320 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700321 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700322 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700323 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
324 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700325 blend1.setDuration(100);
326
327 inanim.playTogether(tx, ty, tr, tb, sx, title);
328 inanim.setDuration(200);
329 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700330 @Override
331 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700332 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700333 finishAnimationIn();
334 mUiController.setBlockEvents(false);
335 }
336 });
Michael Kolba3194d02011-09-07 11:23:51 -0700337 set1.playSequentially(inanim, blend1);
338 set1.start();
Bijan Amirzadafd75eae2014-04-17 17:09:12 -0700339 mUiController.setBlockEvents(false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700340 }
341
342 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700343 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700344 // notify accessibility manager about the screen change
345 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
346 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
347 }
Michael Kolbf2055602011-04-09 17:20:03 -0700348 }
349
Michael Kolba3194d02011-09-07 11:23:51 -0700350 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400351 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700352 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700353 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700354 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700355 if (tab != null) {
356 setActiveTab(tab);
357 } else if (mTabControl.getTabCount() > 0) {
358 // use a fallback tab
359 setActiveTab(mTabControl.getCurrentTab());
360 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700361 mContentView.setVisibility(View.VISIBLE);
362 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700363 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700364 }
Michael Kolba3194d02011-09-07 11:23:51 -0700365 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700366 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700367 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 }
375 mUiController.setBlockEvents(true);
376 mUiController.setActiveTab(tab);
377 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700378 if (mAnimScreen == null) {
379 mAnimScreen = new AnimScreen(mActivity);
380 }
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700381 int width = mContentView.getWidth();
382 int height = mContentView.getHeight();
383 Bitmap bm = tab.getFullScreenshot();
384 if (bm == null)
385 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
386 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800387 if (mAnimScreen.mMain.getParent() == null) {
388 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
389 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800390 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
John Reckcc0059c2011-10-07 13:53:13 -0700391 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800392 mContentView.getHeight() + fixedTbarHeight);
Michael Kolba3194d02011-09-07 11:23:51 -0700393 mNavScreen.mScroller.finishScroller();
394 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700395 int toLeft = 0;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800396 int toTop = 0;
397 if (mTitleBar.isFixed()) {
398 toTop = fixedTbarHeight;
399 } else {
400 toTop = (tab.getWebView() != null) ? tab.getWebView().getVisibleTitleHeight() : 0;
401 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700402 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700403 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
404 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700405 int fromRight = fromLeft + target.getDrawable().getIntrinsicWidth();
406 int fromBottom = fromTop + target.getDrawable().getIntrinsicHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700407 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700408 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700409 mAnimScreen.mContent.setLeft(fromLeft);
410 mAnimScreen.mContent.setTop(fromTop);
411 mAnimScreen.mContent.setRight(fromRight);
412 mAnimScreen.mContent.setBottom(fromBottom);
413 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700414 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700415 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700416 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700417 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700418 set1.setDuration(100);
419 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700420 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700421 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700422 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700423 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700424 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700425 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700426 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700427 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700428 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700429 1f, scaleFactor);
Michael Kolba3194d02011-09-07 11:23:51 -0700430 set2.playTogether(l, t, r, b, scale);
431 set2.setDuration(200);
432 AnimatorSet combo = new AnimatorSet();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400433 combo.playSequentially(set1, set2);
Michael Kolba3194d02011-09-07 11:23:51 -0700434 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700435 @Override
436 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400437 checkTabReady();
438 }
439 });
440 combo.start();
441 }
442
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700443
444 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400445 private void checkTabReady() {
446 boolean isready = true;
447 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700448 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400449 if (tab == null)
450 isready = false;
451 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700452 webview = (BrowserWebView)tab.getWebView();
453 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400454 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700455 }
456 else if (webview.hasCrashed()) {
457 webview.reload();
458 isready = true;
459 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400460 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700461 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400462 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700463 // Post only when not ready and not crashed
464 if (!isready && mNumTries++ < 150) {
465 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400466 public void run() {
467 checkTabReady();
468 }
469 }, 17); //WebView is not ready. check again in for next frame.
470 return;
471 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700472 mNumTries = 0;
473 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
474 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400475 public void run() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700476 fadeOutCustomViewContainer(hasCrashed);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400477 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700478 }, 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 -0400479 }
480
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700481 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400482 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700483 if (hasCrashed)
484 otheralpha.setDuration(300);
485 else
486 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400487 otheralpha.addListener(new AnimatorListenerAdapter() {
488 @Override
489 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700490 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700491 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700492 mUiController.setBlockEvents(false);
493 }
494 });
Matthew Hui1eb7a832014-04-15 12:20:58 -0400495 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700496 }
497
Michael Kolbbf32cd02011-08-16 15:07:04 -0700498 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700499 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700500 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700501 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700502 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700503 }
504
John Reck6b4bd5f2011-07-12 10:14:38 -0700505 @Override
506 public boolean needsRestoreAllTabs() {
507 return false;
508 }
509
Michael Kolb20be26d2011-07-18 16:38:02 -0700510 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700511 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700512 showNavScreen();
513 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700514 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700515 }
516 }
517
John Reck1cf4b792011-07-26 10:22:22 -0700518 @Override
519 public boolean shouldCaptureThumbnails() {
520 return true;
521 }
522
Michael Kolbc3af0672011-08-09 10:24:41 -0700523 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700524
Michael Kolbc3af0672011-08-09 10:24:41 -0700525 private View mMain;
526 private ImageView mTitle;
527 private ImageView mContent;
528 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700529 private Bitmap mTitleBarBitmap;
530 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700531
John Reckcc0059c2011-10-07 13:53:13 -0700532 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700533 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
534 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700535 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700536 mContent = (ImageView) mMain.findViewById(R.id.content);
537 mContent.setScaleType(ImageView.ScaleType.MATRIX);
538 mContent.setImageMatrix(new Matrix());
539 mScale = 1.0f;
540 setScaleFactor(getScaleFactor());
541 }
542
543 public void set(TitleBar tbar, WebView web) {
John Reck62077d82011-10-13 15:17:50 -0700544 if (tbar == null || web == null) {
545 return;
546 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800547 int embTbarHeight = tbar.getEmbeddedHeight();
548 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
549 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700550 if (mTitleBarBitmap == null
551 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800552 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700553 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800554 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700555 }
John Recka98c83b2011-10-31 12:31:58 -0700556 if (mTitleBarBitmap != null) {
557 Canvas c = new Canvas(mTitleBarBitmap);
558 tbar.draw(c);
559 c.setBitmap(null);
560 }
John Reck1adf0302011-10-11 10:58:12 -0700561 } else {
562 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700563 }
John Reckcc0059c2011-10-07 13:53:13 -0700564 mTitle.setImageBitmap(mTitleBarBitmap);
565 mTitle.setVisibility(View.VISIBLE);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800566 // SWE: WebView.draw() wouldn't draw anything if SurfaceView is enabled.
Vivek Sekhar0ab52512014-07-02 14:51:32 -0700567 if (mContentBitmap != null) {
568 mContentBitmap.recycle();
569 mContentBitmap = null;
570 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800571 mContentBitmap = web.getViewportBitmap();
572 if (mContentBitmap == null) {
573 int h = web.getHeight() - embTbarHeight;
574 if (mContentBitmap == null
575 || mContentBitmap.getWidth() != web.getWidth()
576 || mContentBitmap.getHeight() != h) {
577 mContentBitmap = safeCreateBitmap(web.getWidth(), h);
578 }
579 if (mContentBitmap != null) {
580 Canvas c = new Canvas(mContentBitmap);
581 int tx = web.getScrollX();
582 int ty = web.getScrollY();
583 c.translate(-tx, -ty - embTbarHeight);
584 web.draw(c);
585 c.setBitmap(null);
586 }
John Recka98c83b2011-10-31 12:31:58 -0700587 }
John Reckcc0059c2011-10-07 13:53:13 -0700588 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700589 }
590
John Recka98c83b2011-10-31 12:31:58 -0700591 private Bitmap safeCreateBitmap(int width, int height) {
592 if (width <= 0 || height <= 0) {
593 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
594 + ", height: " + height);
595 return null;
596 }
597 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
598 }
599
John Reckcc0059c2011-10-07 13:53:13 -0700600 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700601 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700602 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700603 }
604
John Reckcc0059c2011-10-07 13:53:13 -0700605 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700606 mScale = sf;
607 Matrix m = new Matrix();
608 m.postScale(sf,sf);
609 mContent.setImageMatrix(m);
610 }
611
John Reckcc0059c2011-10-07 13:53:13 -0700612 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700613 return mScale;
614 }
615
John Reck3ba45532011-08-11 16:26:53 -0700616 }
617
Michael Kolb66706532010-12-12 19:50:22 -0800618}