blob: 78b3cc8127e4821f7e4157a18f7225e3f507ffc6 [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;
Vivek Sekhar60eb9802014-07-21 19:13:33 -070055 private Activity mBrowser;
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
Vivek Sekhar60eb9802014-07-21 19:13:33 -070060
Michael Kolb66706532010-12-12 19:50:22 -080061 /**
62 * @param browser
63 * @param controller
64 */
65 public PhoneUi(Activity browser, UiController controller) {
66 super(browser, controller);
Michael Kolbfdb70242011-03-24 09:41:11 -070067 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070068 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
Vivek Sekhar60eb9802014-07-21 19:13:33 -070069 mBrowser = browser;
John Reck285ef042011-02-11 15:44:17 -080070 }
Michael Kolb66706532010-12-12 19:50:22 -080071
John Reck285ef042011-02-11 15:44:17 -080072 @Override
Michael Kolb66706532010-12-12 19:50:22 -080073 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080074 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080075 }
76
77 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -070078 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -070079 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070080 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070081 }
yijunx.zhu2230a312012-07-27 13:37:00 -040082 //Do nothing while at Nav show screen.
83 if (mShowNav) return;
Michael Kolb1f9b3562012-04-24 14:38:34 -070084 super.editUrl(clearInput, forceIME);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080085 }
86
87 @Override
Michael Kolb66706532010-12-12 19:50:22 -080088 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070089 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070090 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070091 return true;
Michael Kolb66706532010-12-12 19:50:22 -080092 }
93 return super.onBackKey();
94 }
95
John Reckcc0059c2011-10-07 13:53:13 -070096 private boolean showingNavScreen() {
97 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
98 }
99
Michael Kolb66706532010-12-12 19:50:22 -0800100 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700101 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700102 return false;
103 }
104
105 @Override
John Reck30c714c2010-12-16 17:30:34 -0800106 public void onProgressChanged(Tab tab) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700107 super.onProgressChanged(tab);
John Reck9c5004e2011-10-07 16:00:12 -0700108 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
109 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
110 }
111 }
112
113 @Override
114 protected void handleMessage(Message msg) {
115 super.handleMessage(msg);
116 if (msg.what == MSG_INIT_NAVSCREEN) {
117 if (mNavScreen == null) {
118 mNavScreen = new NavScreen(mActivity, mUiController, this);
119 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
120 mNavScreen.setVisibility(View.GONE);
121 }
122 if (mAnimScreen == null) {
123 mAnimScreen = new AnimScreen(mActivity);
124 // initialize bitmaps
125 mAnimScreen.set(getTitleBar(), getWebView());
126 }
127 }
Michael Kolb66706532010-12-12 19:50:22 -0800128 }
129
130 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700131 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700132 mTitleBar.cancelTitleBarAnimation(true);
133 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700134 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400135
136 //if at Nav screen show, detach tab like what showNavScreen() do.
137 if (mShowNav) {
138 detachTab(mActiveTab);
139 }
140
Michael Kolbfdb70242011-03-24 09:41:11 -0700141 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800142 // TabControl.setCurrentTab has been called before this,
143 // so the tab is guaranteed to have a webview
144 if (view == null) {
145 Log.e(LOGTAG, "active tab with no webview detected");
146 return;
147 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700148 // Request focus on the top window.
149 if (mUseQuickControls) {
150 mPieControl.forceToTop(mContentView);
Michael Kolb4923c222012-04-02 16:18:36 -0700151 view.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700152 mTitleBar.setShowProgressOnly(true);
Michael Kolbfdb70242011-03-24 09:41:11 -0700153 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700154 view.setTitleBar(mTitleBar);
Michael Kolbfdb70242011-03-24 09:41:11 -0700155 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700156 // update nav bar state
157 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700158 updateLockIconToLatest(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700159 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800160 }
161
Michael Kolb66706532010-12-12 19:50:22 -0800162 // menu handling callbacks
163
164 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700165 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700166 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700167 return true;
168 }
169
170 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700171 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800172 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
173 if (bm != null) {
174 bm.setVisible(!showingNavScreen());
175 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700176 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
177 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700178 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700179 }
Michael Kolb315d5022011-10-13 12:47:11 -0700180 MenuItem info = menu.findItem(R.id.page_info_menu_id);
181 if (info != null) {
182 info.setVisible(false);
183 }
184 MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
185 if (newtab != null && !mUseQuickControls) {
186 newtab.setVisible(false);
187 }
188 MenuItem incognito = menu.findItem(R.id.incognito_menu_id);
189 if (incognito != null) {
190 incognito.setVisible(showingNavScreen() || mUseQuickControls);
191 }
Afzal Najamd4e33312012-04-26 01:54:01 -0400192 MenuItem closeOthers = menu.findItem(R.id.close_other_tabs_id);
193 if (closeOthers != null) {
194 boolean isLastTab = true;
195 if (tab != null) {
196 isLastTab = (mTabControl.getTabCount() <= 1);
197 }
198 closeOthers.setEnabled(!isLastTab);
199 }
John Reckcc0059c2011-10-07 13:53:13 -0700200 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700201 menu.setGroupVisible(R.id.LIVE_MENU, false);
202 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700203 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb315d5022011-10-13 12:47:11 -0700204 menu.setGroupVisible(R.id.COMBO_MENU, true);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700205 }
206 }
207
208 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700209 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700210 if (showingNavScreen()
211 && (item.getItemId() != R.id.history_menu_id)
212 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700213 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700214 }
215 return false;
216 }
217
218 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800219 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800220 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800221 }
222
223 @Override
224 public void onContextMenuClosed(Menu menu, boolean inLoad) {
225 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800226 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800227 }
228 }
229
230 // action mode callbacks
231
232 @Override
233 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700234 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700235 if (!isEditingUrl()) {
236 hideTitleBar();
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700237 } else {
Vivek Sekhar60eb9802014-07-21 19:13:33 -0700238 TypedValue heightValue = new TypedValue();
239 mBrowser.getTheme().resolveAttribute(
240 android.R.attr.actionBarSize, heightValue, true);
241 int actionBarHeight = TypedValue.complexToDimensionPixelSize(heightValue.data,
242 mBrowser.getResources().getDisplayMetrics());
243 mTitleBar.setTranslationY(actionBarHeight);
Michael Kolb42c0c062011-08-02 12:56:06 -0700244 }
Michael Kolb66706532010-12-12 19:50:22 -0800245 }
246
Michael Kolba4183062011-01-16 10:43:21 -0800247 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700248 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700249 super.onActionModeFinished(inLoad);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700250 mTitleBar.setTranslationY(0);
Michael Kolbc16c5952011-03-29 15:37:03 -0700251 if (inLoad) {
252 if (mUseQuickControls) {
253 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700254 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700255 showTitleBar();
256 }
John Reck6cda7b12011-03-18 15:57:13 -0700257 }
258
259 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700260 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700261 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700262 }
263
264 @Override
265 public void showWeb(boolean animate) {
266 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700267 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700268 }
269
Michael Kolbf2055602011-04-09 17:20:03 -0700270 void showNavScreen() {
yijunx.zhu2230a312012-07-27 13:37:00 -0400271 mShowNav = true;
Vivek Sekhar4ddafb12014-07-22 15:33:16 -0700272 dismissIME();
Michael Kolbc3af0672011-08-09 10:24:41 -0700273 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700274 if (mNavScreen == null) {
275 mNavScreen = new NavScreen(mActivity, mUiController, this);
276 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
277 } else {
278 mNavScreen.setVisibility(View.VISIBLE);
279 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700280 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700281 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700282 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700283 if (mAnimScreen == null) {
284 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700285 } else {
286 mAnimScreen.mMain.setAlpha(1f);
287 mAnimScreen.mTitle.setAlpha(1f);
288 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700289 }
290 mAnimScreen.set(getTitleBar(), getWebView());
Michael Kolba30e4fb2011-11-07 17:32:01 -0800291 if (mAnimScreen.mMain.getParent() == null) {
292 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
293 }
Michael Kolbf2055602011-04-09 17:20:03 -0700294 mCustomViewContainer.setVisibility(View.VISIBLE);
295 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700296 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
297 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700298 int fromLeft = 0;
299 int fromTop = getTitleBar().getHeight();
300 int fromRight = mContentView.getWidth();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800301 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
302 int fromBottom = mContentView.getHeight() + fixedTbarHeight;
Michael Kolba3194d02011-09-07 11:23:51 -0700303 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
304 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700305 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700306 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700307 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700308 int toRight = toLeft + width;
309 int toBottom = toTop + height;
310 float scaleFactor = width / (float) mContentView.getWidth();
Michael Kolbc3af0672011-08-09 10:24:41 -0700311 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700312 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700313 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700314 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700315 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700316 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700317 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700318 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700319 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700320 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700321 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700322 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700323 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700324 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700325 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700326 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
327 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700328 blend1.setDuration(100);
329
330 inanim.playTogether(tx, ty, tr, tb, sx, title);
331 inanim.setDuration(200);
332 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700333 @Override
334 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700335 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700336 finishAnimationIn();
337 mUiController.setBlockEvents(false);
338 }
339 });
Michael Kolba3194d02011-09-07 11:23:51 -0700340 set1.playSequentially(inanim, blend1);
341 set1.start();
Bijan Amirzadafd75eae2014-04-17 17:09:12 -0700342 mUiController.setBlockEvents(false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700343 }
344
345 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700346 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700347 // notify accessibility manager about the screen change
348 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
349 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
350 }
Michael Kolbf2055602011-04-09 17:20:03 -0700351 }
352
Michael Kolba3194d02011-09-07 11:23:51 -0700353 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400354 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700355 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700356 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700357 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700358 if (tab != null) {
359 setActiveTab(tab);
360 } else if (mTabControl.getTabCount() > 0) {
361 // use a fallback tab
362 setActiveTab(mTabControl.getCurrentTab());
363 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700364 mContentView.setVisibility(View.VISIBLE);
365 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700366 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700367 }
Michael Kolba3194d02011-09-07 11:23:51 -0700368 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700369 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700370 if (mTabControl.getTabCount() > 0) {
371 // use a fallback tab
372 setActiveTab(mTabControl.getCurrentTab());
373 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700374 mContentView.setVisibility(View.VISIBLE);
375 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700376 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700377 }
378 mUiController.setBlockEvents(true);
379 mUiController.setActiveTab(tab);
380 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700381 if (mAnimScreen == null) {
382 mAnimScreen = new AnimScreen(mActivity);
383 }
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700384 int width = mContentView.getWidth();
385 int height = mContentView.getHeight();
386 Bitmap bm = tab.getFullScreenshot();
387 if (bm == null)
388 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
389 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800390 if (mAnimScreen.mMain.getParent() == null) {
391 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
392 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800393 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
John Reckcc0059c2011-10-07 13:53:13 -0700394 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800395 mContentView.getHeight() + fixedTbarHeight);
Michael Kolba3194d02011-09-07 11:23:51 -0700396 mNavScreen.mScroller.finishScroller();
397 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700398 int toLeft = 0;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800399 int toTop = 0;
400 if (mTitleBar.isFixed()) {
401 toTop = fixedTbarHeight;
402 } else {
403 toTop = (tab.getWebView() != null) ? tab.getWebView().getVisibleTitleHeight() : 0;
404 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700405 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700406 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
407 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700408 int fromRight = fromLeft + target.getDrawable().getIntrinsicWidth();
409 int fromBottom = fromTop + target.getDrawable().getIntrinsicHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700410 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700411 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700412 mAnimScreen.mContent.setLeft(fromLeft);
413 mAnimScreen.mContent.setTop(fromTop);
414 mAnimScreen.mContent.setRight(fromRight);
415 mAnimScreen.mContent.setBottom(fromBottom);
416 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700417 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700418 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700419 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700420 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700421 set1.setDuration(100);
422 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700423 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700424 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700425 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700426 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700427 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700428 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700429 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700430 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700431 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700432 1f, scaleFactor);
Michael Kolba3194d02011-09-07 11:23:51 -0700433 set2.playTogether(l, t, r, b, scale);
434 set2.setDuration(200);
435 AnimatorSet combo = new AnimatorSet();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400436 combo.playSequentially(set1, set2);
Michael Kolba3194d02011-09-07 11:23:51 -0700437 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700438 @Override
439 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400440 checkTabReady();
441 }
442 });
443 combo.start();
444 }
445
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700446
447 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400448 private void checkTabReady() {
449 boolean isready = true;
450 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700451 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400452 if (tab == null)
453 isready = false;
454 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700455 webview = (BrowserWebView)tab.getWebView();
456 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400457 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700458 }
459 else if (webview.hasCrashed()) {
460 webview.reload();
461 isready = true;
462 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400463 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700464 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400465 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700466 // Post only when not ready and not crashed
467 if (!isready && mNumTries++ < 150) {
468 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400469 public void run() {
470 checkTabReady();
471 }
472 }, 17); //WebView is not ready. check again in for next frame.
473 return;
474 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700475 mNumTries = 0;
476 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
477 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400478 public void run() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700479 fadeOutCustomViewContainer(hasCrashed);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400480 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700481 }, 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 -0400482 }
483
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700484 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400485 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700486 if (hasCrashed)
487 otheralpha.setDuration(300);
488 else
489 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400490 otheralpha.addListener(new AnimatorListenerAdapter() {
491 @Override
492 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700493 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700494 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700495 mUiController.setBlockEvents(false);
496 }
497 });
Matthew Hui1eb7a832014-04-15 12:20:58 -0400498 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700499 }
500
Michael Kolbbf32cd02011-08-16 15:07:04 -0700501 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700502 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700503 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700504 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700505 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700506 }
507
John Reck6b4bd5f2011-07-12 10:14:38 -0700508 @Override
509 public boolean needsRestoreAllTabs() {
510 return false;
511 }
512
Michael Kolb20be26d2011-07-18 16:38:02 -0700513 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700514 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700515 showNavScreen();
516 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700517 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700518 }
519 }
520
John Reck1cf4b792011-07-26 10:22:22 -0700521 @Override
522 public boolean shouldCaptureThumbnails() {
523 return true;
524 }
525
Michael Kolbc3af0672011-08-09 10:24:41 -0700526 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700527
Michael Kolbc3af0672011-08-09 10:24:41 -0700528 private View mMain;
529 private ImageView mTitle;
530 private ImageView mContent;
531 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700532 private Bitmap mTitleBarBitmap;
533 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700534
John Reckcc0059c2011-10-07 13:53:13 -0700535 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700536 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
537 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700538 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700539 mContent = (ImageView) mMain.findViewById(R.id.content);
540 mContent.setScaleType(ImageView.ScaleType.MATRIX);
541 mContent.setImageMatrix(new Matrix());
542 mScale = 1.0f;
543 setScaleFactor(getScaleFactor());
544 }
545
546 public void set(TitleBar tbar, WebView web) {
John Reck62077d82011-10-13 15:17:50 -0700547 if (tbar == null || web == null) {
548 return;
549 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800550 int embTbarHeight = tbar.getEmbeddedHeight();
551 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
552 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700553 if (mTitleBarBitmap == null
554 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800555 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700556 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800557 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700558 }
John Recka98c83b2011-10-31 12:31:58 -0700559 if (mTitleBarBitmap != null) {
560 Canvas c = new Canvas(mTitleBarBitmap);
561 tbar.draw(c);
562 c.setBitmap(null);
563 }
John Reck1adf0302011-10-11 10:58:12 -0700564 } else {
565 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700566 }
John Reckcc0059c2011-10-07 13:53:13 -0700567 mTitle.setImageBitmap(mTitleBarBitmap);
568 mTitle.setVisibility(View.VISIBLE);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800569 // SWE: WebView.draw() wouldn't draw anything if SurfaceView is enabled.
Vivek Sekhar0ab52512014-07-02 14:51:32 -0700570 if (mContentBitmap != null) {
571 mContentBitmap.recycle();
572 mContentBitmap = null;
573 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800574 mContentBitmap = web.getViewportBitmap();
575 if (mContentBitmap == null) {
576 int h = web.getHeight() - embTbarHeight;
577 if (mContentBitmap == null
578 || mContentBitmap.getWidth() != web.getWidth()
579 || mContentBitmap.getHeight() != h) {
580 mContentBitmap = safeCreateBitmap(web.getWidth(), h);
581 }
582 if (mContentBitmap != null) {
583 Canvas c = new Canvas(mContentBitmap);
584 int tx = web.getScrollX();
585 int ty = web.getScrollY();
586 c.translate(-tx, -ty - embTbarHeight);
587 web.draw(c);
588 c.setBitmap(null);
589 }
John Recka98c83b2011-10-31 12:31:58 -0700590 }
John Reckcc0059c2011-10-07 13:53:13 -0700591 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700592 }
593
John Recka98c83b2011-10-31 12:31:58 -0700594 private Bitmap safeCreateBitmap(int width, int height) {
595 if (width <= 0 || height <= 0) {
596 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
597 + ", height: " + height);
598 return null;
599 }
600 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
601 }
602
John Reckcc0059c2011-10-07 13:53:13 -0700603 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700604 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700605 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700606 }
607
John Reckcc0059c2011-10-07 13:53:13 -0700608 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700609 mScale = sf;
610 Matrix m = new Matrix();
611 m.postScale(sf,sf);
612 mContent.setImageMatrix(m);
613 }
614
John Reckcc0059c2011-10-07 13:53:13 -0700615 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700616 return mScale;
617 }
618
John Reck3ba45532011-08-11 16:26:53 -0700619 }
620
Michael Kolb66706532010-12-12 19:50:22 -0800621}