blob: d02a3d29924fb6dbacd135b42afaaffd6a2e97ac [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
17package com.android.browser;
18
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;
32import android.view.Gravity;
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 Kolb199e43c2011-08-17 10:45:45 -070038import android.view.ViewGroup;
Michael Kolb30adae62011-07-29 13:37:05 -070039import android.view.accessibility.AccessibilityEvent;
Michael Kolb66706532010-12-12 19:50:22 -080040import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000041import android.webkit.WebViewClassic;
Michael Kolbfdb70242011-03-24 09:41:11 -070042import android.widget.FrameLayout;
Michael Kolbc3af0672011-08-09 10:24:41 -070043import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080044
Michael Kolb629b22c2011-07-13 16:26:47 -070045import com.android.browser.UrlInputView.StateListener;
46
Michael Kolb66706532010-12-12 19:50:22 -080047/**
48 * Ui for regular phone screen sizes
49 */
50public class PhoneUi extends BaseUi {
51
52 private static final String LOGTAG = "PhoneUi";
John Reck9c5004e2011-10-07 16:00:12 -070053 private static final int MSG_INIT_NAVSCREEN = 100;
Michael Kolb66706532010-12-12 19:50:22 -080054
Michael Kolb0241e752011-07-07 14:58:50 -070055 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070056 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070057 private AnimScreen mAnimScreen;
John Reck0f602f32011-07-07 15:38:43 -070058 private NavigationBarPhone mNavigationBar;
John Reckaae029c2011-09-16 14:36:39 -070059 private int mActionBarHeight;
Michael Kolb66706532010-12-12 19:50:22 -080060
61 boolean mExtendedMenuOpen;
62 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070063 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080064
65 /**
66 * @param browser
67 * @param controller
68 */
69 public PhoneUi(Activity browser, UiController controller) {
70 super(browser, controller);
Michael Kolbfdb70242011-03-24 09:41:11 -070071 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070072 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reckaae029c2011-09-16 14:36:39 -070073 TypedValue heightValue = new TypedValue();
74 browser.getTheme().resolveAttribute(
75 com.android.internal.R.attr.actionBarSize, heightValue, true);
76 mActionBarHeight = TypedValue.complexToDimensionPixelSize(heightValue.data,
77 browser.getResources().getDisplayMetrics());
John Reck285ef042011-02-11 15:44:17 -080078 }
Michael Kolb66706532010-12-12 19:50:22 -080079
John Reck285ef042011-02-11 15:44:17 -080080 @Override
Michael Kolb66706532010-12-12 19:50:22 -080081 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080082 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080083 }
84
85 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080086 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070087 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070088 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070089 }
90 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080091 }
92
93 @Override
Michael Kolb66706532010-12-12 19:50:22 -080094 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -070095 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -070096 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -070097 return true;
Michael Kolb66706532010-12-12 19:50:22 -080098 }
99 return super.onBackKey();
100 }
101
John Reckcc0059c2011-10-07 13:53:13 -0700102 private boolean showingNavScreen() {
103 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
104 }
105
Michael Kolb66706532010-12-12 19:50:22 -0800106 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700107 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700108 return false;
109 }
110
111 @Override
John Reck30c714c2010-12-16 17:30:34 -0800112 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800113 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800114 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800115 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800116 if (progress == 100) {
117 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700118 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700119 if (mUseQuickControls) {
120 mTitleBar.setShowProgressOnly(false);
121 }
Michael Kolb66706532010-12-12 19:50:22 -0800122 }
123 } else {
124 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700125 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
126 mTitleBar.setShowProgressOnly(true);
Michael Kolbc16c5952011-03-29 15:37:03 -0700127 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800128 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800129 }
130 }
131 }
John Reck9c5004e2011-10-07 16:00:12 -0700132 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
133 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
134 }
135 }
136
137 @Override
138 protected void handleMessage(Message msg) {
139 super.handleMessage(msg);
140 if (msg.what == MSG_INIT_NAVSCREEN) {
141 if (mNavScreen == null) {
142 mNavScreen = new NavScreen(mActivity, mUiController, this);
143 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
144 mNavScreen.setVisibility(View.GONE);
145 }
146 if (mAnimScreen == null) {
147 mAnimScreen = new AnimScreen(mActivity);
148 // initialize bitmaps
149 mAnimScreen.set(getTitleBar(), getWebView());
150 }
151 }
Michael Kolb66706532010-12-12 19:50:22 -0800152 }
153
154 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700155 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700156 mTitleBar.cancelTitleBarAnimation(true);
157 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700158 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700159 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800160 // TabControl.setCurrentTab has been called before this,
161 // so the tab is guaranteed to have a webview
162 if (view == null) {
163 Log.e(LOGTAG, "active tab with no webview detected");
164 return;
165 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700166 // Request focus on the top window.
167 if (mUseQuickControls) {
168 mPieControl.forceToTop(mContentView);
Michael Kolb4923c222012-04-02 16:18:36 -0700169 view.setTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700170 } else {
Michael Kolb4923c222012-04-02 16:18:36 -0700171 view.setTitleBar(mTitleBar);
Michael Kolbfdb70242011-03-24 09:41:11 -0700172 }
Michael Kolb376b5412010-12-15 11:52:57 -0800173 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700174 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800175 } else {
176 revertVoiceTitleBar(tab);
177 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700178 // update nav bar state
179 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700180 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800181 tab.getTopWindow().requestFocus();
John Reck8ee633f2011-08-09 16:00:35 -0700182 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800183 }
184
Michael Kolb66706532010-12-12 19:50:22 -0800185 // menu handling callbacks
186
187 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700188 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700189 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700190 return true;
191 }
192
193 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700194 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800195 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
196 if (bm != null) {
197 bm.setVisible(!showingNavScreen());
198 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700199 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
200 if (abm != null) {
John Reckcc0059c2011-10-07 13:53:13 -0700201 abm.setVisible((tab != null) && !tab.isSnapshot() && !showingNavScreen());
John Recke1a03a32011-09-14 17:04:16 -0700202 }
Michael Kolb315d5022011-10-13 12:47:11 -0700203 MenuItem info = menu.findItem(R.id.page_info_menu_id);
204 if (info != null) {
205 info.setVisible(false);
206 }
207 MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
208 if (newtab != null && !mUseQuickControls) {
209 newtab.setVisible(false);
210 }
211 MenuItem incognito = menu.findItem(R.id.incognito_menu_id);
212 if (incognito != null) {
213 incognito.setVisible(showingNavScreen() || mUseQuickControls);
214 }
John Reckcc0059c2011-10-07 13:53:13 -0700215 if (showingNavScreen()) {
John Recke1a03a32011-09-14 17:04:16 -0700216 menu.setGroupVisible(R.id.LIVE_MENU, false);
217 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700218 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb315d5022011-10-13 12:47:11 -0700219 menu.setGroupVisible(R.id.COMBO_MENU, true);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700220 }
221 }
222
223 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700224 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700225 if (showingNavScreen()
226 && (item.getItemId() != R.id.history_menu_id)
227 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700228 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700229 }
230 return false;
231 }
232
233 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800234 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
238 @Override
239 public void onContextMenuClosed(Menu menu, boolean inLoad) {
240 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800241 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800242 }
243 }
244
245 // action mode callbacks
246
247 @Override
248 public void onActionModeStarted(ActionMode mode) {
Michael Kolb42c0c062011-08-02 12:56:06 -0700249 if (!isEditingUrl()) {
250 hideTitleBar();
John Reckaae029c2011-09-16 14:36:39 -0700251 } else {
252 mTitleBar.animate().translationY(mActionBarHeight);
Michael Kolb42c0c062011-08-02 12:56:06 -0700253 }
Michael Kolb66706532010-12-12 19:50:22 -0800254 }
255
Michael Kolba4183062011-01-16 10:43:21 -0800256 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700257 public void onActionModeFinished(boolean inLoad) {
John Reckaae029c2011-09-16 14:36:39 -0700258 mTitleBar.animate().translationY(0);
Michael Kolbc16c5952011-03-29 15:37:03 -0700259 if (inLoad) {
260 if (mUseQuickControls) {
261 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700262 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700263 showTitleBar();
264 }
John Reck6cda7b12011-03-18 15:57:13 -0700265 }
266
267 @Override
Michael Kolb0241e752011-07-07 14:58:50 -0700268 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700269 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700270 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700271 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700272 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700273 mPieControl.attachToContainer(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700274 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700275 if (mPieControl != null) {
276 mPieControl.removeFromContainer(mContentView);
277 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700278 }
John Reck718a24d2011-08-12 11:08:30 -0700279 updateUrlBarAutoShowManagerTarget();
Michael Kolbfdb70242011-03-24 09:41:11 -0700280 }
281
Michael Kolbc3af0672011-08-09 10:24:41 -0700282 @Override
283 public boolean isWebShowing() {
John Reckcc0059c2011-10-07 13:53:13 -0700284 return super.isWebShowing() && !showingNavScreen();
Michael Kolbc3af0672011-08-09 10:24:41 -0700285 }
286
287 @Override
288 public void showWeb(boolean animate) {
289 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700290 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700291 }
292
Michael Kolbf2055602011-04-09 17:20:03 -0700293 void showNavScreen() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700294 mUiController.setBlockEvents(true);
John Reckcc0059c2011-10-07 13:53:13 -0700295 if (mNavScreen == null) {
296 mNavScreen = new NavScreen(mActivity, mUiController, this);
297 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
298 } else {
299 mNavScreen.setVisibility(View.VISIBLE);
300 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700301 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700302 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700303 mActiveTab.capture();
John Reckcc0059c2011-10-07 13:53:13 -0700304 if (mAnimScreen == null) {
305 mAnimScreen = new AnimScreen(mActivity);
John Reckce8bcb02011-10-11 13:45:29 -0700306 } else {
307 mAnimScreen.mMain.setAlpha(1f);
308 mAnimScreen.mTitle.setAlpha(1f);
309 mAnimScreen.setScaleFactor(1f);
John Reckcc0059c2011-10-07 13:53:13 -0700310 }
311 mAnimScreen.set(getTitleBar(), getWebView());
Michael Kolba30e4fb2011-11-07 17:32:01 -0800312 if (mAnimScreen.mMain.getParent() == null) {
313 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
314 }
Michael Kolbf2055602011-04-09 17:20:03 -0700315 mCustomViewContainer.setVisibility(View.VISIBLE);
316 mCustomViewContainer.bringToFront();
John Reckce8bcb02011-10-11 13:45:29 -0700317 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
318 mContentView.getHeight());
Michael Kolbc3af0672011-08-09 10:24:41 -0700319 int fromLeft = 0;
320 int fromTop = getTitleBar().getHeight();
321 int fromRight = mContentView.getWidth();
322 int fromBottom = mContentView.getHeight();
Michael Kolba3194d02011-09-07 11:23:51 -0700323 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
324 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
Michael Kolb7a321d62011-09-23 15:54:22 -0700325 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
Michael Kolbc3af0672011-08-09 10:24:41 -0700326 int toLeft = (mContentView.getWidth() - width) / 2;
Michael Kolb7a321d62011-09-23 15:54:22 -0700327 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
Michael Kolbc3af0672011-08-09 10:24:41 -0700328 int toRight = toLeft + width;
329 int toBottom = toTop + height;
330 float scaleFactor = width / (float) mContentView.getWidth();
331 detachTab(mActiveTab);
332 mContentView.setVisibility(View.GONE);
Michael Kolba3194d02011-09-07 11:23:51 -0700333 AnimatorSet set1 = new AnimatorSet();
Michael Kolbc3af0672011-08-09 10:24:41 -0700334 AnimatorSet inanim = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700335 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700336 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700337 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700338 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700339 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700340 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700341 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700342 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700343 ObjectAnimator title = ObjectAnimator.ofFloat(mAnimScreen.mTitle, "alpha",
Michael Kolbc3af0672011-08-09 10:24:41 -0700344 1f, 0f);
John Reckcc0059c2011-10-07 13:53:13 -0700345 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700346 1f, scaleFactor);
John Reck1adf0302011-10-11 10:58:12 -0700347 ObjectAnimator blend1 = ObjectAnimator.ofFloat(mAnimScreen.mMain,
348 "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700349 blend1.setDuration(100);
350
351 inanim.playTogether(tx, ty, tr, tb, sx, title);
352 inanim.setDuration(200);
353 set1.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700354 @Override
355 public void onAnimationEnd(Animator anim) {
John Reck1adf0302011-10-11 10:58:12 -0700356 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbc3af0672011-08-09 10:24:41 -0700357 finishAnimationIn();
358 mUiController.setBlockEvents(false);
359 }
360 });
Michael Kolba3194d02011-09-07 11:23:51 -0700361 set1.playSequentially(inanim, blend1);
362 set1.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700363 }
364
365 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700366 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700367 // notify accessibility manager about the screen change
368 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
369 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
370 }
Michael Kolbf2055602011-04-09 17:20:03 -0700371 }
372
Michael Kolba3194d02011-09-07 11:23:51 -0700373 void hideNavScreen(int position, boolean animate) {
John Reckcc0059c2011-10-07 13:53:13 -0700374 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700375 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700376 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700377 if (tab != null) {
378 setActiveTab(tab);
379 } else if (mTabControl.getTabCount() > 0) {
380 // use a fallback tab
381 setActiveTab(mTabControl.getCurrentTab());
382 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700383 mContentView.setVisibility(View.VISIBLE);
384 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700385 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700386 }
Michael Kolba3194d02011-09-07 11:23:51 -0700387 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700388 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700389 if (mTabControl.getTabCount() > 0) {
390 // use a fallback tab
391 setActiveTab(mTabControl.getCurrentTab());
392 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700393 mContentView.setVisibility(View.VISIBLE);
394 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700395 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700396 }
397 mUiController.setBlockEvents(true);
398 mUiController.setActiveTab(tab);
399 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700400 if (mAnimScreen == null) {
401 mAnimScreen = new AnimScreen(mActivity);
402 }
403 mAnimScreen.set(tab.getScreenshot());
Michael Kolb0755fcd2012-01-10 10:19:50 -0800404 if (mAnimScreen.mMain.getParent() == null) {
405 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
406 }
John Reckcc0059c2011-10-07 13:53:13 -0700407 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Michael Kolba3194d02011-09-07 11:23:51 -0700408 mContentView.getHeight());
409 mNavScreen.mScroller.finishScroller();
410 ImageView target = tabview.mImage;
Michael Kolbc3af0672011-08-09 10:24:41 -0700411 int toLeft = 0;
Michael Kolbb7e16702011-12-15 11:59:40 -0800412 int toTop = (tab.getWebView() != null) ? tab.getWebView().getVisibleTitleHeight() : 0;
Michael Kolbc3af0672011-08-09 10:24:41 -0700413 int toRight = mContentView.getWidth();
Michael Kolba3194d02011-09-07 11:23:51 -0700414 int width = target.getDrawable().getIntrinsicWidth();
415 int height = target.getDrawable().getIntrinsicHeight();
416 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
417 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
Michael Kolbc3af0672011-08-09 10:24:41 -0700418 int fromRight = fromLeft + width;
419 int fromBottom = fromTop + height;
420 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700421 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700422 mAnimScreen.mContent.setLeft(fromLeft);
423 mAnimScreen.mContent.setTop(fromTop);
424 mAnimScreen.mContent.setRight(fromRight);
425 mAnimScreen.mContent.setBottom(fromBottom);
426 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700427 AnimatorSet set1 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700428 ObjectAnimator fade2 = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700429 ObjectAnimator fade1 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
John Reck9c5004e2011-10-07 16:00:12 -0700430 set1.playTogether(fade1, fade2);
Michael Kolba3194d02011-09-07 11:23:51 -0700431 set1.setDuration(100);
432 AnimatorSet set2 = new AnimatorSet();
John Reckcc0059c2011-10-07 13:53:13 -0700433 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left",
Michael Kolbc3af0672011-08-09 10:24:41 -0700434 fromLeft, toLeft);
John Reckcc0059c2011-10-07 13:53:13 -0700435 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top",
Michael Kolbc3af0672011-08-09 10:24:41 -0700436 fromTop, toTop);
John Reckcc0059c2011-10-07 13:53:13 -0700437 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right",
Michael Kolbc3af0672011-08-09 10:24:41 -0700438 fromRight, toRight);
John Reckcc0059c2011-10-07 13:53:13 -0700439 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom",
Michael Kolbc3af0672011-08-09 10:24:41 -0700440 fromBottom, toBottom);
John Reckcc0059c2011-10-07 13:53:13 -0700441 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor",
Michael Kolbc3af0672011-08-09 10:24:41 -0700442 1f, scaleFactor);
Michael Kolbc3af0672011-08-09 10:24:41 -0700443 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Michael Kolba3194d02011-09-07 11:23:51 -0700444 otheralpha.setDuration(100);
445 set2.playTogether(l, t, r, b, scale);
446 set2.setDuration(200);
447 AnimatorSet combo = new AnimatorSet();
448 combo.playSequentially(set1, set2, otheralpha);
449 combo.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700450 @Override
451 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700452 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700453 finishAnimateOut();
Michael Kolbc3af0672011-08-09 10:24:41 -0700454 mUiController.setBlockEvents(false);
455 }
456 });
Michael Kolba3194d02011-09-07 11:23:51 -0700457 combo.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700458 }
459
Michael Kolbbf32cd02011-08-16 15:07:04 -0700460 private void finishAnimateOut() {
John Reck8ee633f2011-08-09 16:00:35 -0700461 mTabControl.setOnThumbnailUpdatedListener(null);
John Reckcc0059c2011-10-07 13:53:13 -0700462 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700463 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700464 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700465 }
466
John Reck6b4bd5f2011-07-12 10:14:38 -0700467 @Override
468 public boolean needsRestoreAllTabs() {
469 return false;
470 }
471
Michael Kolb20be26d2011-07-18 16:38:02 -0700472 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700473 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700474 showNavScreen();
475 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700476 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700477 }
478 }
479
John Reck1cf4b792011-07-26 10:22:22 -0700480 @Override
481 public boolean shouldCaptureThumbnails() {
482 return true;
483 }
484
Michael Kolbc3af0672011-08-09 10:24:41 -0700485 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700486
Michael Kolbc3af0672011-08-09 10:24:41 -0700487 private View mMain;
488 private ImageView mTitle;
489 private ImageView mContent;
490 private float mScale;
John Reckcc0059c2011-10-07 13:53:13 -0700491 private Bitmap mTitleBarBitmap;
492 private Bitmap mContentBitmap;
Michael Kolbc3af0672011-08-09 10:24:41 -0700493
John Reckcc0059c2011-10-07 13:53:13 -0700494 public AnimScreen(Context ctx) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700495 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen,
496 null);
Michael Kolbc3af0672011-08-09 10:24:41 -0700497 mTitle = (ImageView) mMain.findViewById(R.id.title);
John Reckcc0059c2011-10-07 13:53:13 -0700498 mContent = (ImageView) mMain.findViewById(R.id.content);
499 mContent.setScaleType(ImageView.ScaleType.MATRIX);
500 mContent.setImageMatrix(new Matrix());
501 mScale = 1.0f;
502 setScaleFactor(getScaleFactor());
503 }
504
505 public void set(TitleBar tbar, WebView web) {
John Reck62077d82011-10-13 15:17:50 -0700506 if (tbar == null || web == null) {
507 return;
508 }
John Reck1adf0302011-10-11 10:58:12 -0700509 if (tbar.getWidth() > 0 && tbar.getEmbeddedHeight() > 0) {
510 if (mTitleBarBitmap == null
511 || mTitleBarBitmap.getWidth() != tbar.getWidth()
512 || mTitleBarBitmap.getHeight() != tbar.getEmbeddedHeight()) {
John Recka98c83b2011-10-31 12:31:58 -0700513 mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
514 tbar.getEmbeddedHeight());
John Reck1adf0302011-10-11 10:58:12 -0700515 }
John Recka98c83b2011-10-31 12:31:58 -0700516 if (mTitleBarBitmap != null) {
517 Canvas c = new Canvas(mTitleBarBitmap);
518 tbar.draw(c);
519 c.setBitmap(null);
520 }
John Reck1adf0302011-10-11 10:58:12 -0700521 } else {
522 mTitleBarBitmap = null;
John Reckcc0059c2011-10-07 13:53:13 -0700523 }
John Reckcc0059c2011-10-07 13:53:13 -0700524 mTitle.setImageBitmap(mTitleBarBitmap);
525 mTitle.setVisibility(View.VISIBLE);
John Reck9c5004e2011-10-07 16:00:12 -0700526 int h = web.getHeight() - tbar.getEmbeddedHeight();
John Reckcc0059c2011-10-07 13:53:13 -0700527 if (mContentBitmap == null
528 || mContentBitmap.getWidth() != web.getWidth()
529 || mContentBitmap.getHeight() != h) {
John Recka98c83b2011-10-31 12:31:58 -0700530 mContentBitmap = safeCreateBitmap(web.getWidth(), h);
John Reckcc0059c2011-10-07 13:53:13 -0700531 }
John Recka98c83b2011-10-31 12:31:58 -0700532 if (mContentBitmap != null) {
533 Canvas c = new Canvas(mContentBitmap);
534 int tx = web.getScrollX();
535 int ty = web.getScrollY();
536 c.translate(-tx, -ty - tbar.getEmbeddedHeight());
537 web.draw(c);
538 c.setBitmap(null);
539 }
John Reckcc0059c2011-10-07 13:53:13 -0700540 mContent.setImageBitmap(mContentBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700541 }
542
John Recka98c83b2011-10-31 12:31:58 -0700543 private Bitmap safeCreateBitmap(int width, int height) {
544 if (width <= 0 || height <= 0) {
545 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
546 + ", height: " + height);
547 return null;
548 }
549 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
550 }
551
John Reckcc0059c2011-10-07 13:53:13 -0700552 public void set(Bitmap image) {
Michael Kolba3194d02011-09-07 11:23:51 -0700553 mTitle.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700554 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700555 }
556
John Reckcc0059c2011-10-07 13:53:13 -0700557 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700558 mScale = sf;
559 Matrix m = new Matrix();
560 m.postScale(sf,sf);
561 mContent.setImageMatrix(m);
562 }
563
John Reckcc0059c2011-10-07 13:53:13 -0700564 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700565 return mScale;
566 }
567
John Reck3ba45532011-08-11 16:26:53 -0700568 }
569
Michael Kolb66706532010-12-12 19:50:22 -0800570}