blob: c789eba1802b53fa28037cc102095d74608d8aaf [file] [log] [blame]
Michael Kolb66706532010-12-12 19:50:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb66706532010-12-12 19:50:22 -080018
Michael Kolbc3af0672011-08-09 10:24:41 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Michael Kolb66706532010-12-12 19:50:22 -080023import android.app.Activity;
Michael Kolbc3af0672011-08-09 10:24:41 -070024import android.content.Context;
25import android.graphics.Bitmap;
26import android.graphics.Canvas;
27import android.graphics.Matrix;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070028import android.graphics.Rect;
John Reck9c5004e2011-10-07 16:00:12 -070029import android.os.Message;
Michael Kolb376b5412010-12-15 11:52:57 -080030import android.util.Log;
John Reckaae029c2011-09-16 14:36:39 -070031import android.util.TypedValue;
Michael Kolb66706532010-12-12 19:50:22 -080032import android.view.ActionMode;
Michael Kolba4183062011-01-16 10:43:21 -080033import android.view.KeyEvent;
Michael Kolbc3af0672011-08-09 10:24:41 -070034import android.view.LayoutInflater;
Michael Kolb66706532010-12-12 19:50:22 -080035import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070036import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080037import android.view.View;
Michael Kolb30adae62011-07-29 13:37:05 -070038import android.view.accessibility.AccessibilityEvent;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070039import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080040import org.codeaurora.swe.WebView;
Michael Kolbc3af0672011-08-09 10:24:41 -070041import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080042
Bijan Amirzada41242f22014-03-21 12:12:18 -070043import com.android.browser.R;
44import com.android.browser.UrlInputView.StateListener;
Michael Kolb629b22c2011-07-13 16:26:47 -070045
Michael Kolb66706532010-12-12 19:50:22 -080046/**
47 * Ui for regular phone screen sizes
48 */
49public class PhoneUi extends BaseUi {
50
51 private static final String LOGTAG = "PhoneUi";
John Reck9c5004e2011-10-07 16:00:12 -070052 private static final int MSG_INIT_NAVSCREEN = 100;
Michael Kolb66706532010-12-12 19:50:22 -080053
Michael Kolbf2055602011-04-09 17:20:03 -070054 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070055 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070056 private NavigationBarPhone mNavigationBar;
Vivek Sekhar60eb9802014-07-21 19:13:33 -070057 private Activity mBrowser;
Michael Kolb66706532010-12-12 19:50:22 -080058
Michael Kolb08a687a2011-04-22 16:13:02 -070059 boolean mAnimating;
yijunx.zhu2230a312012-07-27 13:37:00 -040060 boolean mShowNav = false;
Michael Kolb66706532010-12-12 19:50:22 -080061
Vivek Sekhar60eb9802014-07-21 19:13:33 -070062
Michael Kolb66706532010-12-12 19:50:22 -080063 /**
64 * @param browser
65 * @param controller
66 */
67 public PhoneUi(Activity browser, UiController controller) {
68 super(browser, controller);
Michael Kolbfdb70242011-03-24 09:41:11 -070069 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070070 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
Vivek Sekhar60eb9802014-07-21 19:13:33 -070071 mBrowser = browser;
John Reck285ef042011-02-11 15:44:17 -080072 }
Michael Kolb66706532010-12-12 19:50:22 -080073
John Reck285ef042011-02-11 15:44:17 -080074 @Override
Michael Kolb66706532010-12-12 19:50:22 -080075 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080076 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080077 }
78
79 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -070080 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -070081 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070082 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070083 }
yijunx.zhu2230a312012-07-27 13:37:00 -040084 //Do nothing while at Nav show screen.
85 if (mShowNav) return;
Michael Kolb1f9b3562012-04-24 14:38:34 -070086 super.editUrl(clearInput, forceIME);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080087 }
88
89 @Override
Michael Kolb66706532010-12-12 19:50:22 -080090 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070091 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070092 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070093 return true;
Michael Kolb66706532010-12-12 19:50:22 -080094 }
95 return super.onBackKey();
96 }
97
John Reckcc0059c2011-10-07 13:53:13 -070098 private boolean showingNavScreen() {
99 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
100 }
101
Michael Kolb66706532010-12-12 19:50:22 -0800102 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700103 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700104 return false;
105 }
106
107 @Override
John Reck30c714c2010-12-16 17:30:34 -0800108 public void onProgressChanged(Tab tab) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700109 super.onProgressChanged(tab);
John Reck9c5004e2011-10-07 16:00:12 -0700110 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
111 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
112 }
113 }
114
115 @Override
116 protected void handleMessage(Message msg) {
117 super.handleMessage(msg);
118 if (msg.what == MSG_INIT_NAVSCREEN) {
119 if (mNavScreen == null) {
120 mNavScreen = new NavScreen(mActivity, mUiController, this);
121 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
122 mNavScreen.setVisibility(View.GONE);
123 }
124 if (mAnimScreen == null) {
125 mAnimScreen = new AnimScreen(mActivity);
126 // initialize bitmaps
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700127 //mAnimScreen.set(getTitleBar(), getWebView());
John Reck9c5004e2011-10-07 16:00:12 -0700128 }
129 }
Michael Kolb66706532010-12-12 19:50:22 -0800130 }
131
132 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700133 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700134 mTitleBar.cancelTitleBarAnimation(true);
135 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700136 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400137
138 //if at Nav screen show, detach tab like what showNavScreen() do.
139 if (mShowNav) {
140 detachTab(mActiveTab);
141 }
142
Michael Kolbfdb70242011-03-24 09:41:11 -0700143 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800144 // TabControl.setCurrentTab has been called before this,
145 // so the tab is guaranteed to have a webview
146 if (view == null) {
147 Log.e(LOGTAG, "active tab with no webview detected");
148 return;
149 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700150 // Request focus on the top window.
151 if (mUseQuickControls) {
152 mPieControl.forceToTop(mContentView);
Michael Kolb4923c222012-04-02 16:18:36 -0700153 view.setTitleBar(null);
Michael Kolbe8a82332012-04-25 14:14:26 -0700154 mTitleBar.setShowProgressOnly(true);
Michael Kolbfdb70242011-03-24 09:41:11 -0700155 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700156 view.setTitleBar(mTitleBar);
Michael Kolbfdb70242011-03-24 09:41:11 -0700157 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700158 // update nav bar state
159 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700160 updateLockIconToLatest(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700161 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800162 }
163
Michael Kolb66706532010-12-12 19:50:22 -0800164 // menu handling callbacks
165
166 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700167 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700168 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700169 return true;
170 }
171
172 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700173 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800174 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
175 if (bm != null) {
176 bm.setVisible(!showingNavScreen());
177 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700178 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
179 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700180 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700181 }
Michael Kolb315d5022011-10-13 12:47:11 -0700182 MenuItem info = menu.findItem(R.id.page_info_menu_id);
183 if (info != null) {
184 info.setVisible(false);
185 }
186 MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
187 if (newtab != null && !mUseQuickControls) {
188 newtab.setVisible(false);
189 }
190 MenuItem incognito = menu.findItem(R.id.incognito_menu_id);
191 if (incognito != null) {
192 incognito.setVisible(showingNavScreen() || mUseQuickControls);
193 }
Afzal Najamd4e33312012-04-26 01:54:01 -0400194 MenuItem closeOthers = menu.findItem(R.id.close_other_tabs_id);
195 if (closeOthers != null) {
196 boolean isLastTab = true;
197 if (tab != null) {
198 isLastTab = (mTabControl.getTabCount() <= 1);
199 }
200 closeOthers.setEnabled(!isLastTab);
201 }
John Reckcc0059c2011-10-07 13:53:13 -0700202 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700203 menu.setGroupVisible(R.id.LIVE_MENU, false);
204 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700205 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb315d5022011-10-13 12:47:11 -0700206 menu.setGroupVisible(R.id.COMBO_MENU, true);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700207 }
208 }
209
210 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700211 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700212 if (showingNavScreen()
213 && (item.getItemId() != R.id.history_menu_id)
214 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700215 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700216 }
217 return false;
218 }
219
220 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800221 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800222 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800223 }
224
225 @Override
226 public void onContextMenuClosed(Menu menu, boolean inLoad) {
227 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800228 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800229 }
230 }
231
232 // action mode callbacks
233
234 @Override
235 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700236 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700237 if (!isEditingUrl()) {
238 hideTitleBar();
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700239 } else {
Vivek Sekhar60eb9802014-07-21 19:13:33 -0700240 TypedValue heightValue = new TypedValue();
241 mBrowser.getTheme().resolveAttribute(
242 android.R.attr.actionBarSize, heightValue, true);
243 int actionBarHeight = TypedValue.complexToDimensionPixelSize(heightValue.data,
244 mBrowser.getResources().getDisplayMetrics());
245 mTitleBar.setTranslationY(actionBarHeight);
Michael Kolb42c0c062011-08-02 12:56:06 -0700246 }
Michael Kolb66706532010-12-12 19:50:22 -0800247 }
248
Michael Kolba4183062011-01-16 10:43:21 -0800249 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700250 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700251 super.onActionModeFinished(inLoad);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700252 mTitleBar.setTranslationY(0);
Michael Kolbc16c5952011-03-29 15:37:03 -0700253 if (inLoad) {
254 if (mUseQuickControls) {
255 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700256 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700257 showTitleBar();
258 }
John Reck6cda7b12011-03-18 15:57:13 -0700259 }
260
261 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700262 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700263 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700264 }
265
266 @Override
267 public void showWeb(boolean animate) {
268 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700269 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700270 }
271
Michael Kolbf2055602011-04-09 17:20:03 -0700272 void showNavScreen() {
yijunx.zhu2230a312012-07-27 13:37:00 -0400273 mShowNav = true;
Vivek Sekhar4ddafb12014-07-22 15:33:16 -0700274 dismissIME();
Michael Kolbc3af0672011-08-09 10:24:41 -0700275 mUiController.setBlockEvents(true);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700276
277 getWebView()
278 .getContentBitmapAsync(1.0f,
279 new Rect(),
280 new ValueCallback<Bitmap>() {
281 @Override
282 public void onReceiveValue(Bitmap bitmap) {
283 onShowNavScreenContinue(bitmap);
284 }});
285 }
286
287 void onShowNavScreenContinue(Bitmap viewportBitmap) {
288
John Reckcc0059c2011-10-07 13:53:13 -0700289 if (mNavScreen == null) {
290 mNavScreen = new NavScreen(mActivity, mUiController, this);
291 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
292 } else {
293 mNavScreen.setVisibility(View.VISIBLE);
294 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700295 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700296 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700297 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700298 if (mAnimScreen == null) {
299 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700300 } else {
301 mAnimScreen.mMain.setAlpha(1f);
302 mAnimScreen.mTitle.setAlpha(1f);
303 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700304 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700305 mAnimScreen.set(getTitleBar(), viewportBitmap);
Michael Kolba30e4fb2011-11-07 17:32:01 -0800306 if (mAnimScreen.mMain.getParent() == null) {
307 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
308 }
Michael Kolbf2055602011-04-09 17:20:03 -0700309 mCustomViewContainer.setVisibility(View.VISIBLE);
310 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700311 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
312 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700313 int fromLeft = 0;
314 int fromTop = getTitleBar().getHeight();
315 int fromRight = mContentView.getWidth();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800316 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
317 int fromBottom = mContentView.getHeight() + fixedTbarHeight;
Michael Kolba3194d02011-09-07 11:23:51 -0700318 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
319 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700320 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700321 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700322 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700323 int toRight = toLeft + width;
324 int toBottom = toTop + height;
325 float scaleFactor = width / (float) mContentView.getWidth();
Michael Kolbc3af0672011-08-09 10:24:41 -0700326 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700327 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700328 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700329 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700330 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700331 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700332 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700333 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700334 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700335 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700336 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700337 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700338 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700339 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700340 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700341 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
342 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700343 blend1.setDuration(100);
344
345 inanim.playTogether(tx, ty, tr, tb, sx, title);
346 inanim.setDuration(200);
347 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700348 @Override
349 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700350 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700351 finishAnimationIn();
352 mUiController.setBlockEvents(false);
353 }
354 });
Michael Kolba3194d02011-09-07 11:23:51 -0700355 set1.playSequentially(inanim, blend1);
356 set1.start();
Bijan Amirzadafd75eae2014-04-17 17:09:12 -0700357 mUiController.setBlockEvents(false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700358 }
359
360 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700361 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700362 // notify accessibility manager about the screen change
363 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
364 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
365 }
Michael Kolbf2055602011-04-09 17:20:03 -0700366 }
367
Michael Kolba3194d02011-09-07 11:23:51 -0700368 void hideNavScreen(int position, boolean animate) {
yijunx.zhu2230a312012-07-27 13:37:00 -0400369 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700370 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700371 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700372 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700373 if (tab != null) {
374 setActiveTab(tab);
375 } else if (mTabControl.getTabCount() > 0) {
376 // use a fallback tab
377 setActiveTab(mTabControl.getCurrentTab());
378 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700379 mContentView.setVisibility(View.VISIBLE);
380 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700381 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700382 }
Michael Kolba3194d02011-09-07 11:23:51 -0700383 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700384 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700385 if (mTabControl.getTabCount() > 0) {
386 // use a fallback tab
387 setActiveTab(mTabControl.getCurrentTab());
388 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700389 mContentView.setVisibility(View.VISIBLE);
390 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700391 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700392 }
393 mUiController.setBlockEvents(true);
394 mUiController.setActiveTab(tab);
395 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700396 if (mAnimScreen == null) {
397 mAnimScreen = new AnimScreen(mActivity);
398 }
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700399 int width = mContentView.getWidth();
400 int height = mContentView.getHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700401 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700402 if (bm == null)
403 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
404 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800405 if (mAnimScreen.mMain.getParent() == null) {
406 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
407 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800408 int fixedTbarHeight = mTitleBar.isFixed() ? mTitleBar.calculateEmbeddedHeight() : 0;
John Reckcc0059c2011-10-07 13:53:13 -0700409 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800410 mContentView.getHeight() + fixedTbarHeight);
Michael Kolba3194d02011-09-07 11:23:51 -0700411 mNavScreen.mScroller.finishScroller();
412 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700413 int toLeft = 0;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800414 int toTop = 0;
415 if (mTitleBar.isFixed()) {
416 toTop = fixedTbarHeight;
417 } else {
418 toTop = (tab.getWebView() != null) ? tab.getWebView().getVisibleTitleHeight() : 0;
419 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700420 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700421 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
422 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700423 int fromRight = fromLeft + target.getDrawable().getIntrinsicWidth();
424 int fromBottom = fromTop + target.getDrawable().getIntrinsicHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700425 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700426 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700427 mAnimScreen.mContent.setLeft(fromLeft);
428 mAnimScreen.mContent.setTop(fromTop);
429 mAnimScreen.mContent.setRight(fromRight);
430 mAnimScreen.mContent.setBottom(fromBottom);
431 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700432 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700433 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700434 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700435 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700436 set1.setDuration(100);
437 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700438 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700439 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700440 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700441 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700442 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700443 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700444 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700445 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700446 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700447 1f, scaleFactor);
Michael Kolba3194d02011-09-07 11:23:51 -0700448 set2.playTogether(l, t, r, b, scale);
449 set2.setDuration(200);
450 AnimatorSet combo = new AnimatorSet();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400451 combo.playSequentially(set1, set2);
Michael Kolba3194d02011-09-07 11:23:51 -0700452 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700453 @Override
454 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400455 checkTabReady();
456 }
457 });
458 combo.start();
459 }
460
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700461
462 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400463 private void checkTabReady() {
464 boolean isready = true;
465 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700466 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400467 if (tab == null)
468 isready = false;
469 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700470 webview = (BrowserWebView)tab.getWebView();
471 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400472 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700473 }
474 else if (webview.hasCrashed()) {
475 webview.reload();
476 isready = true;
477 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400478 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700479 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400480 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700481 // Post only when not ready and not crashed
482 if (!isready && mNumTries++ < 150) {
483 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400484 public void run() {
485 checkTabReady();
486 }
487 }, 17); //WebView is not ready. check again in for next frame.
488 return;
489 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700490 mNumTries = 0;
491 final boolean hasCrashed = (webview == null) ? false : webview.hasCrashed();
492 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400493 public void run() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700494 fadeOutCustomViewContainer(hasCrashed);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400495 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700496 }, 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 -0400497 }
498
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700499 private void fadeOutCustomViewContainer(boolean hasCrashed) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400500 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700501 if (hasCrashed)
502 otheralpha.setDuration(300);
503 else
504 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400505 otheralpha.addListener(new AnimatorListenerAdapter() {
506 @Override
507 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700508 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700509 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700510 mUiController.setBlockEvents(false);
511 }
512 });
Matthew Hui1eb7a832014-04-15 12:20:58 -0400513 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700514 }
515
Michael Kolbbf32cd02011-08-16 15:07:04 -0700516 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700517 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700518 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700519 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700520 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700521 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700522 }
523
John Reck6b4bd5f2011-07-12 10:14:38 -0700524 @Override
525 public boolean needsRestoreAllTabs() {
526 return false;
527 }
528
Michael Kolb20be26d2011-07-18 16:38:02 -0700529 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700530 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700531 showNavScreen();
532 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700533 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700534 }
535 }
536
John Reck1cf4b792011-07-26 10:22:22 -0700537 @Override
538 public boolean shouldCaptureThumbnails() {
539 return true;
540 }
541
Michael Kolbc3af0672011-08-09 10:24:41 -0700542 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700543
Michael Kolbc3af0672011-08-09 10:24:41 -0700544 private View mMain;
545 private ImageView mTitle;
546 private ImageView mContent;
547 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700548 private Bitmap mTitleBarBitmap;
549 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700550
John Reckcc0059c2011-10-07 13:53:13 -0700551 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700552 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
553 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700554 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700555 mContent = (ImageView) mMain.findViewById(R.id.content);
556 mContent.setScaleType(ImageView.ScaleType.MATRIX);
557 mContent.setImageMatrix(new Matrix());
558 mScale = 1.0f;
559 setScaleFactor(getScaleFactor());
560 }
561
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700562 public void set(TitleBar tbar, Bitmap viewportBitmap) {
563 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700564 return;
565 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800566 int embTbarHeight = tbar.getEmbeddedHeight();
567 int tbarHeight = tbar.isFixed() ? tbar.calculateEmbeddedHeight() : embTbarHeight;
568 if (tbar.getWidth() > 0 && tbarHeight > 0) {
John Reck1adf0302011-10-11 10:58:12 -0700569 if (mTitleBarBitmap == null
570 || mTitleBarBitmap.getWidth() != tbar.getWidth()
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800571 || mTitleBarBitmap.getHeight() != tbarHeight) {
John Recka98c83b2011-10-31 12:31:58 -0700572 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800573 tbarHeight);
John Reck1adf0302011-10-11 10:58:12 -0700574 }
John Recka98c83b2011-10-31 12:31:58 -0700575 if (mTitleBarBitmap != null) {
576 Canvas c = new Canvas(mTitleBarBitmap);
577 tbar.draw(c);
578 c.setBitmap(null);
579 }
John Reck1adf0302011-10-11 10:58:12 -0700580 } else {
581 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700582 }
John Reckcc0059c2011-10-07 13:53:13 -0700583 mTitle.setImageBitmap(mTitleBarBitmap);
584 mTitle.setVisibility(View.VISIBLE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700585
586 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700587 }
588
John Recka98c83b2011-10-31 12:31:58 -0700589 private Bitmap safeCreateBitmap(int width, int height) {
590 if (width <= 0 || height <= 0) {
591 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
592 + ", height: " + height);
593 return null;
594 }
595 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
596 }
597
John Reckcc0059c2011-10-07 13:53:13 -0700598 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700599 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700600 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700601 }
602
John Reckcc0059c2011-10-07 13:53:13 -0700603 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700604 mScale = sf;
605 Matrix m = new Matrix();
606 m.postScale(sf,sf);
607 mContent.setImageMatrix(m);
608 }
609
John Reckcc0059c2011-10-07 13:53:13 -0700610 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700611 return mScale;
612 }
613
John Reck3ba45532011-08-11 16:26:53 -0700614 }
615
Michael Kolb66706532010-12-12 19:50:22 -0800616}