blob: c051ec0fffc610af3fa0644fc165612a10ff517d [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;
Michael Kolbc3af0672011-08-09 10:24:41 -070026import android.graphics.Matrix;
Tarun Nainani87a86682015-02-05 11:47:35 -080027import android.graphics.Paint;
28import android.graphics.Rect;
29import android.os.Bundle;
John Reck9c5004e2011-10-07 16:00:12 -070030import android.os.Message;
Michael Kolb376b5412010-12-15 11:52:57 -080031import android.util.Log;
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;
Tarun Nainani87a86682015-02-05 11:47:35 -080038import android.view.ViewStub;
Michael Kolb30adae62011-07-29 13:37:05 -070039import android.view.accessibility.AccessibilityEvent;
Enrico Ros1f5a0952014-11-18 20:15:48 -080040import android.view.animation.DecelerateInterpolator;
Tarun Nainani87a86682015-02-05 11:47:35 -080041import android.webkit.ValueCallback;
42import org.codeaurora.swe.WebView;
43
44import android.widget.FrameLayout;
Michael Kolbc3af0672011-08-09 10:24:41 -070045import android.widget.ImageView;
Michael Kolb66706532010-12-12 19:50:22 -080046
Bijan Amirzada41242f22014-03-21 12:12:18 -070047import com.android.browser.UrlInputView.StateListener;
Michael Kolb629b22c2011-07-13 16:26:47 -070048
Pankaj Garg79878492015-04-01 14:48:21 -070049import org.codeaurora.swe.WebView;
50
Michael Kolb66706532010-12-12 19:50:22 -080051/**
52 * Ui for regular phone screen sizes
53 */
54public class PhoneUi extends BaseUi {
55
56 private static final String LOGTAG = "PhoneUi";
John Reck9c5004e2011-10-07 16:00:12 -070057 private static final int MSG_INIT_NAVSCREEN = 100;
Michael Kolb66706532010-12-12 19:50:22 -080058
Michael Kolbf2055602011-04-09 17:20:03 -070059 private NavScreen mNavScreen;
John Reckcc0059c2011-10-07 13:53:13 -070060 private AnimScreen mAnimScreen;
Tarun Nainani87a86682015-02-05 11:47:35 -080061 private final NavigationBarPhone mNavigationBar;
62 private final Activity mBrowser;
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -080063 private boolean mNavScreenRequested = false;
Michael Kolb66706532010-12-12 19:50:22 -080064
Michael Kolb08a687a2011-04-22 16:13:02 -070065 boolean mAnimating;
yijunx.zhu2230a312012-07-27 13:37:00 -040066 boolean mShowNav = false;
Tarun Nainani87a86682015-02-05 11:47:35 -080067 private ComboView mComboView;
Michael Kolb66706532010-12-12 19:50:22 -080068
Vivek Sekhar60eb9802014-07-21 19:13:33 -070069
Michael Kolb66706532010-12-12 19:50:22 -080070 /**
71 * @param browser
72 * @param controller
73 */
74 public PhoneUi(Activity browser, UiController controller) {
75 super(browser, controller);
John Reck0f602f32011-07-07 15:38:43 -070076 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
Vivek Sekhar60eb9802014-07-21 19:13:33 -070077 mBrowser = browser;
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();
Karthikeyan Periasamy5b8f5752015-01-13 16:00:27 -080083 // Free the allocated memory for GC to clear it from the heap.
84 mAnimScreen = null;
Michael Kolb66706532010-12-12 19:50:22 -080085 }
86
87 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -070088 public void editUrl(boolean clearInput, boolean forceIME) {
yijunx.zhu2230a312012-07-27 13:37:00 -040089 //Do nothing while at Nav show screen.
90 if (mShowNav) return;
Michael Kolb1f9b3562012-04-24 14:38:34 -070091 super.editUrl(clearInput, forceIME);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080092 }
93
94 @Override
Tarun Nainani87a86682015-02-05 11:47:35 -080095 public void showComboView(ComboViews startingView, Bundle extras) {
96
97 if (mComboView == null) {
98 mNavScreen.setVisibility(View.GONE);
99 ViewStub stub = (ViewStub) mActivity.getWindow().getDecorView().findViewById(R.id.combo_view_stub);
100 mComboView = (ComboView) stub.inflate();
101 mComboView.setVisibility(View.GONE);
102 mComboView.setupViews(mActivity);
103 }
104
105 Bundle b = new Bundle();
106 b.putString(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
107 b.putBundle(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
108 Tab t = getActiveTab();
109 if (t != null) {
110 b.putString(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
111 }
112
113 mComboView.showViews(mActivity, b);
114 }
115
116 @Override
117 public void hideComboView() {
118 mComboView.hideViews();
119 }
120
121 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800122 public boolean onBackKey() {
John Reckcc0059c2011-10-07 13:53:13 -0700123 if (showingNavScreen()) {
Michael Kolba3194d02011-09-07 11:23:51 -0700124 mNavScreen.close(mUiController.getTabControl().getCurrentPosition());
Michael Kolbf2055602011-04-09 17:20:03 -0700125 return true;
Michael Kolb66706532010-12-12 19:50:22 -0800126 }
Tarun Nainani87a86682015-02-05 11:47:35 -0800127 if (showingComboView()) {
128 hideComboView();
129 return true;
130 }
Michael Kolb66706532010-12-12 19:50:22 -0800131 return super.onBackKey();
132 }
133
John Reckcc0059c2011-10-07 13:53:13 -0700134 private boolean showingNavScreen() {
135 return mNavScreen != null && mNavScreen.getVisibility() == View.VISIBLE;
136 }
137
Tarun Nainani87a86682015-02-05 11:47:35 -0800138 private boolean showingComboView() {
139 return mComboView != null && mComboView.getVisibility() == View.VISIBLE;
140 }
141
Michael Kolb66706532010-12-12 19:50:22 -0800142 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700143 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -0700144 return false;
145 }
146
147 @Override
John Reck30c714c2010-12-16 17:30:34 -0800148 public void onProgressChanged(Tab tab) {
Michael Kolbe8a82332012-04-25 14:14:26 -0700149 super.onProgressChanged(tab);
John Reck9c5004e2011-10-07 16:00:12 -0700150 if (mNavScreen == null && getTitleBar().getHeight() > 0) {
151 mHandler.sendEmptyMessage(MSG_INIT_NAVSCREEN);
152 }
153 }
154
155 @Override
156 protected void handleMessage(Message msg) {
157 super.handleMessage(msg);
158 if (msg.what == MSG_INIT_NAVSCREEN) {
159 if (mNavScreen == null) {
160 mNavScreen = new NavScreen(mActivity, mUiController, this);
161 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
162 mNavScreen.setVisibility(View.GONE);
163 }
164 if (mAnimScreen == null) {
165 mAnimScreen = new AnimScreen(mActivity);
John Reck9c5004e2011-10-07 16:00:12 -0700166 }
167 }
Michael Kolb66706532010-12-12 19:50:22 -0800168 }
169
170 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700171 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700172 mTitleBar.cancelTitleBarAnimation(true);
173 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700174 super.setActiveTab(tab);
yijunx.zhu2230a312012-07-27 13:37:00 -0400175
176 //if at Nav screen show, detach tab like what showNavScreen() do.
177 if (mShowNav) {
178 detachTab(mActiveTab);
179 }
180
Michael Kolbfdb70242011-03-24 09:41:11 -0700181 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800182 // TabControl.setCurrentTab has been called before this,
183 // so the tab is guaranteed to have a webview
184 if (view == null) {
185 Log.e(LOGTAG, "active tab with no webview detected");
186 return;
187 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700188 // Request focus on the top window.
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700189 view.setTitleBar(mTitleBar);
190
Michael Kolb629b22c2011-07-13 16:26:47 -0700191 // update nav bar state
192 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700193 updateLockIconToLatest(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700194 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800195 }
196
Michael Kolb66706532010-12-12 19:50:22 -0800197 // menu handling callbacks
198
199 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700200 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700201 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700202 return true;
203 }
204
205 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700206 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb0d0245f2011-12-05 16:36:05 -0800207 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
208 if (bm != null) {
209 bm.setVisible(!showingNavScreen());
210 }
Michael Kolb315d5022011-10-13 12:47:11 -0700211 MenuItem info = menu.findItem(R.id.page_info_menu_id);
212 if (info != null) {
213 info.setVisible(false);
214 }
Pankaj Garg49b79252014-11-07 17:33:41 -0800215
John Reckcc0059c2011-10-07 13:53:13 -0700216 if (showingNavScreen()) {
Pankaj Garg49b79252014-11-07 17:33:41 -0800217 setMenuItemVisibility(menu, R.id.history_menu_id, false);
218 setMenuItemVisibility(menu, R.id.find_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700219 menu.setGroupVisible(R.id.LIVE_MENU, false);
Pankaj Garg49b79252014-11-07 17:33:41 -0800220 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id, false);
John Recke1a03a32011-09-14 17:04:16 -0700221 menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
John Recke1a03a32011-09-14 17:04:16 -0700222 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700223 }
Tarun Nainani87a86682015-02-05 11:47:35 -0800224
225 if (showingComboView()) {
226 menu.setGroupVisible(R.id.MAIN_MENU, false);
227 menu.setGroupEnabled(R.id.MAIN_MENU, false);
228 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
229 }
230
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700231 }
232
233 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700234 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb315d5022011-10-13 12:47:11 -0700235 if (showingNavScreen()
236 && (item.getItemId() != R.id.history_menu_id)
237 && (item.getItemId() != R.id.snapshots_menu_id)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700238 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb3ca12752011-07-20 13:52:25 -0700239 }
240 return false;
241 }
242
243 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800244 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800245 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800246 }
247
248 @Override
249 public void onContextMenuClosed(Menu menu, boolean inLoad) {
250 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800251 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800252 }
253 }
254
255 // action mode callbacks
256
257 @Override
258 public void onActionModeStarted(ActionMode mode) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700259 super.onActionModeStarted(mode);
Michael Kolb42c0c062011-08-02 12:56:06 -0700260 if (!isEditingUrl()) {
261 hideTitleBar();
262 }
Michael Kolb66706532010-12-12 19:50:22 -0800263 }
264
Michael Kolba4183062011-01-16 10:43:21 -0800265 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700266 public void onActionModeFinished(boolean inLoad) {
Bijan Amirzada357ec8a2014-04-08 14:19:10 -0700267 super.onActionModeFinished(inLoad);
Michael Kolbc16c5952011-03-29 15:37:03 -0700268 if (inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700269 showTitleBar();
270 }
John Reck6cda7b12011-03-18 15:57:13 -0700271 }
272
273 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700274 public boolean isWebShowing() {
Tarun Nainani87a86682015-02-05 11:47:35 -0800275 return super.isWebShowing() && !showingNavScreen() && !showingComboView();
276 }
277
278 @Override
279 public boolean isComboViewShowing() {
280 return showingComboView();
Michael Kolbc3af0672011-08-09 10:24:41 -0700281 }
282
283 @Override
284 public void showWeb(boolean animate) {
285 super.showWeb(animate);
Michael Kolba3194d02011-09-07 11:23:51 -0700286 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
Michael Kolbc3af0672011-08-09 10:24:41 -0700287 }
288
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800289 //Unblock touch events
290 private void unblockEvents() {
291 mUiController.setBlockEvents(false);
292 }
293 //Block touch events
294 private void blockEvents() {
295 mUiController.setBlockEvents(true);
296 }
297
298 @Override
299 public void cancelNavScreenRequest() {
300 mNavScreenRequested = false;
301 }
302
Michael Kolbf2055602011-04-09 17:20:03 -0700303 void showNavScreen() {
Pankaj Garg79878492015-04-01 14:48:21 -0700304 blockEvents();
305 mNavScreenRequested = true;
306 mTabControl.setOnThumbnailUpdatedListener(
307 new TabControl.OnThumbnailUpdatedListener() {
308 @Override
309 public void onThumbnailUpdated(Tab t) {
310 mTabControl.setOnThumbnailUpdatedListener(null);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800311
Pankaj Garg79878492015-04-01 14:48:21 -0700312 // Discard the callback if the req is interrupted
313 if (!mNavScreenRequested) {
314 unblockEvents();
315 return;
Sudheer Koganti06cd42f2015-01-30 15:35:12 -0800316 }
Pankaj Garg79878492015-04-01 14:48:21 -0700317
318 Bitmap bm = t.getScreenshot();
319 Bitmap sbm;
320 WebView webView = getWebView();
321 if (webView != null) {
322 int view_width = webView.getWidth();
323 int capture_width = mActivity.getResources().getDimensionPixelSize(
324 R.dimen.tab_thumbnail_width);
325
326 float scale = (float) view_width / capture_width;
327
328 //Upscale the low-res bitmap to the needed size
329 Matrix m = new Matrix();
330 m.postScale(scale, scale);
331 sbm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
332 bm.getHeight(), m, false);
333 } else {
334 sbm = bm;
335 }
336
337 onShowNavScreenContinue(sbm);
Sudheer Koganti06cd42f2015-01-30 15:35:12 -0800338 }
Pankaj Garg79878492015-04-01 14:48:21 -0700339 });
340 mActiveTab.capture();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700341 }
342
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800343 void onShowNavScreenContinue(Bitmap viewportBitmap) {
344 dismissIME();
345 mShowNav = true;
346 mNavScreenRequested = false;
John Reckcc0059c2011-10-07 13:53:13 -0700347 if (mNavScreen == null) {
348 mNavScreen = new NavScreen(mActivity, mUiController, this);
349 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
350 } else {
351 mNavScreen.setVisibility(View.VISIBLE);
352 mNavScreen.setAlpha(1f);
John Reck9c5004e2011-10-07 16:00:12 -0700353 mNavScreen.refreshAdapter();
John Reckcc0059c2011-10-07 13:53:13 -0700354 }
Sagar Dhawan01493f62015-02-09 11:03:19 -0800355 if (mAnimScreen == null) {
356 mAnimScreen = new AnimScreen(mActivity);
357 } else {
358 mAnimScreen.mMain.setAlpha(1f);
359 mAnimScreen.setScaleFactor(1f);
360 }
361 mAnimScreen.set(getTitleBar(), viewportBitmap);
362 if (mAnimScreen.mMain.getParent() == null) {
363 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
364 }
Michael Kolbf2055602011-04-09 17:20:03 -0700365 mCustomViewContainer.setVisibility(View.VISIBLE);
366 mCustomViewContainer.bringToFront();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800367 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
368 mContentView.getHeight());
369 int fromLeft = 0;
370 int fromTop = getTitleBar().getHeight();
371 int fromRight = mContentView.getWidth();
372 int fromBottom = mContentView.getHeight();
373 int width = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
374 int height = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
375 int ntth = mActivity.getResources().getDimensionPixelSize(R.dimen.nav_tab_titleheight);
376 int toLeft = (mContentView.getWidth() - width) / 2;
377 int toTop = ((fromBottom - (ntth + height)) / 2 + ntth);
378 int toRight = toLeft + width;
379 int toBottom = toTop + height;
380 float toScaleFactor = width / (float) mContentView.getWidth();
381 ObjectAnimator tx = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
382 ObjectAnimator ty = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
383 ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
384 ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
385 ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, toScaleFactor);
386 ObjectAnimator navTabsIn = mNavScreen.createToolbarInAnimator();
387 mAnimScreen.mContent.layout(fromLeft, fromTop, fromRight, fromBottom);
388 mAnimScreen.setScaleFactor(1f);
Michael Kolba3194d02011-09-07 11:23:51 -0700389
Sagar Dhawan01493f62015-02-09 11:03:19 -0800390 AnimatorSet inanim = new AnimatorSet();
391 inanim.playTogether(tx, ty, tr, tb, sx, navTabsIn);
392 inanim.setInterpolator(new DecelerateInterpolator());
393 inanim.setDuration(200);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800394
Sagar Dhawan01493f62015-02-09 11:03:19 -0800395 ObjectAnimator disappear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 1f, 0f);
396 disappear.setInterpolator(new DecelerateInterpolator());
397 disappear.setDuration(100);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800398
Sagar Dhawan01493f62015-02-09 11:03:19 -0800399 AnimatorSet set1 = new AnimatorSet();
400 set1.addListener(new AnimatorListenerAdapter() {
401 @Override
402 public void onAnimationStart(Animator animation) {
403 mContentView.setVisibility(View.GONE);
404 }
405 @Override
406 public void onAnimationEnd(Animator anim) {
407 mCustomViewContainer.removeView(mAnimScreen.mMain);
408 finishAnimationIn();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800409 unblockEvents();
Sagar Dhawan01493f62015-02-09 11:03:19 -0800410 }
411 });
412 set1.playSequentially(inanim, disappear);
413 set1.start();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800414 unblockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700415 }
416
417 private void finishAnimationIn() {
John Reckcc0059c2011-10-07 13:53:13 -0700418 if (showingNavScreen()) {
Michael Kolbb43f2f62011-08-17 10:39:31 -0700419 // notify accessibility manager about the screen change
420 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Kolbb43f2f62011-08-17 10:39:31 -0700421 }
Michael Kolbf2055602011-04-09 17:20:03 -0700422 }
423
Michael Kolba3194d02011-09-07 11:23:51 -0700424 void hideNavScreen(int position, boolean animate) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800425
yijunx.zhu2230a312012-07-27 13:37:00 -0400426 mShowNav = false;
John Reckcc0059c2011-10-07 13:53:13 -0700427 if (!showingNavScreen()) return;
Michael Kolba3194d02011-09-07 11:23:51 -0700428 final Tab tab = mUiController.getTabControl().getTab(position);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700429 if ((tab == null) || !animate) {
Michael Kolb365561d2011-08-18 09:56:25 -0700430 if (tab != null) {
431 setActiveTab(tab);
432 } else if (mTabControl.getTabCount() > 0) {
433 // use a fallback tab
434 setActiveTab(mTabControl.getCurrentTab());
435 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700436 mContentView.setVisibility(View.VISIBLE);
437 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700438 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700439 }
Michael Kolba3194d02011-09-07 11:23:51 -0700440 NavTabView tabview = (NavTabView) mNavScreen.getTabView(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700441 if (tabview == null) {
Michael Kolb365561d2011-08-18 09:56:25 -0700442 if (mTabControl.getTabCount() > 0) {
443 // use a fallback tab
444 setActiveTab(mTabControl.getCurrentTab());
445 }
Michael Kolbbf32cd02011-08-16 15:07:04 -0700446 mContentView.setVisibility(View.VISIBLE);
447 finishAnimateOut();
Michael Kolb8a009492011-08-15 15:37:30 -0700448 return;
Michael Kolbc3af0672011-08-09 10:24:41 -0700449 }
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800450 blockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700451 mUiController.setActiveTab(tab);
452 mContentView.setVisibility(View.VISIBLE);
John Reckcc0059c2011-10-07 13:53:13 -0700453 if (mAnimScreen == null) {
454 mAnimScreen = new AnimScreen(mActivity);
455 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700456 ImageView target = tabview.mImage;
457 int width = target.getDrawable().getIntrinsicWidth();
458 int height = target.getDrawable().getIntrinsicHeight();
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700459 Bitmap bm = tab.getScreenshot();
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700460 if (bm == null)
461 bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
462 mAnimScreen.set(bm);
Michael Kolb0755fcd2012-01-10 10:19:50 -0800463 if (mAnimScreen.mMain.getParent() == null) {
464 mCustomViewContainer.addView(mAnimScreen.mMain, COVER_SCREEN_PARAMS);
465 }
John Reckcc0059c2011-10-07 13:53:13 -0700466 mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
Tarun Nainani8eb00912014-07-17 12:28:32 -0700467 mContentView.getHeight());
Enrico Ros80c045a2014-11-24 15:23:12 -0800468 mNavScreen.getScroller().finishScroller();
Michael Kolbc3af0672011-08-09 10:24:41 -0700469 int toLeft = 0;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700470 int toTop = mTitleBar.calculateEmbeddedHeight();
Michael Kolbc3af0672011-08-09 10:24:41 -0700471 int toRight = mContentView.getWidth();
Enrico Ros80c045a2014-11-24 15:23:12 -0800472 int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.getScroller().getScrollX();
473 int fromTop = tabview.getTop() + target.getTop() - mNavScreen.getScroller().getScrollY();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700474 int fromRight = fromLeft + width;
475 int fromBottom = fromTop + height;
Michael Kolbc3af0672011-08-09 10:24:41 -0700476 float scaleFactor = mContentView.getWidth() / (float) width;
Michael Kolba3194d02011-09-07 11:23:51 -0700477 int toBottom = toTop + (int) (height * scaleFactor);
John Reck9c5004e2011-10-07 16:00:12 -0700478 mAnimScreen.mContent.setLeft(fromLeft);
479 mAnimScreen.mContent.setTop(fromTop);
480 mAnimScreen.mContent.setRight(fromRight);
481 mAnimScreen.mContent.setBottom(fromBottom);
482 mAnimScreen.setScaleFactor(1f);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800483 //ObjectAnimator fade2 = ObjectAnimator.ofFloat(mNavScreen, "alpha", 1f, 0f);
484 //fade2.setDuration(100);
485 AnimatorSet set = new AnimatorSet();
486 ObjectAnimator animAppear = ObjectAnimator.ofFloat(mAnimScreen.mMain, "alpha", 0f, 1f);
487 animAppear.setDuration(100);
488 ObjectAnimator l = ObjectAnimator.ofInt(mAnimScreen.mContent, "left", fromLeft, toLeft);
489 ObjectAnimator t = ObjectAnimator.ofInt(mAnimScreen.mContent, "top", fromTop, toTop);
490 ObjectAnimator r = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
491 ObjectAnimator b = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
492 ObjectAnimator scale = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, scaleFactor);
493 set.playTogether(animAppear, l, t, r, b, scale);
494 set.setInterpolator(new DecelerateInterpolator());
495 set.setDuration(200);
496 set.addListener(new AnimatorListenerAdapter() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700497 @Override
498 public void onAnimationEnd(Animator anim) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400499 checkTabReady();
500 }
501 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800502 set.start();
Matthew Hui1eb7a832014-04-15 12:20:58 -0400503 }
504
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700505
506 private int mNumTries = 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400507 private void checkTabReady() {
508 boolean isready = true;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800509 boolean zeroTries = mNumTries == 0;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400510 Tab tab = mUiController.getTabControl().getCurrentTab();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700511 BrowserWebView webview = null;
Matthew Hui1eb7a832014-04-15 12:20:58 -0400512 if (tab == null)
513 isready = false;
514 else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700515 webview = (BrowserWebView)tab.getWebView();
516 if (webview == null) {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400517 isready = false;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700518 } else {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400519 isready = webview.isReady();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700520 }
Matthew Hui1eb7a832014-04-15 12:20:58 -0400521 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700522 // Post only when not ready and not crashed
523 if (!isready && mNumTries++ < 150) {
524 mCustomViewContainer.postDelayed(new Runnable() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400525 public void run() {
526 checkTabReady();
527 }
528 }, 17); //WebView is not ready. check again in for next frame.
529 return;
530 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700531 mNumTries = 0;
Vivek Sekharc70ae632015-04-08 17:54:05 -0700532 final boolean hasCrashed = (webview == null) ? false :false;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800533 // fast path: don't wait if we've been ready for a while
534 if (zeroTries) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700535 fadeOutCustomViewContainer();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800536 return;
537 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700538 mCustomViewContainer.postDelayed(new Runnable() {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800539 public void run() {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700540 fadeOutCustomViewContainer();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800541 }
542 }, 32); //WebView is ready, but give it extra 2 frame's time to display and finish the swaps
Matthew Hui1eb7a832014-04-15 12:20:58 -0400543 }
544
Vivek Sekharc70ae632015-04-08 17:54:05 -0700545 private void fadeOutCustomViewContainer() {
Matthew Hui1eb7a832014-04-15 12:20:58 -0400546 ObjectAnimator otheralpha = ObjectAnimator.ofFloat(mCustomViewContainer, "alpha", 1f, 0f);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700547 otheralpha.setDuration(100);
Matthew Hui1eb7a832014-04-15 12:20:58 -0400548 otheralpha.addListener(new AnimatorListenerAdapter() {
549 @Override
550 public void onAnimationEnd(Animator anim) {
John Reckcc0059c2011-10-07 13:53:13 -0700551 mCustomViewContainer.removeView(mAnimScreen.mMain);
Michael Kolbbf32cd02011-08-16 15:07:04 -0700552 finishAnimateOut();
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -0800553 unblockEvents();
Michael Kolbc3af0672011-08-09 10:24:41 -0700554 }
555 });
Enrico Ros1f5a0952014-11-18 20:15:48 -0800556 otheralpha.setInterpolator(new DecelerateInterpolator());
Matthew Hui1eb7a832014-04-15 12:20:58 -0400557 otheralpha.start();
Michael Kolbc3af0672011-08-09 10:24:41 -0700558 }
559
Michael Kolbbf32cd02011-08-16 15:07:04 -0700560 private void finishAnimateOut() {
John Reckcc0059c2011-10-07 13:53:13 -0700561 mNavScreen.setVisibility(View.GONE);
Michael Kolbc3af0672011-08-09 10:24:41 -0700562 mCustomViewContainer.setAlpha(1f);
Michael Kolbf2055602011-04-09 17:20:03 -0700563 mCustomViewContainer.setVisibility(View.GONE);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700564 mAnimScreen.set(null);
Michael Kolbf2055602011-04-09 17:20:03 -0700565 }
566
John Reck6b4bd5f2011-07-12 10:14:38 -0700567 @Override
568 public boolean needsRestoreAllTabs() {
569 return false;
570 }
571
Michael Kolb20be26d2011-07-18 16:38:02 -0700572 public void toggleNavScreen() {
John Reckcc0059c2011-10-07 13:53:13 -0700573 if (!showingNavScreen()) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700574 showNavScreen();
575 } else {
Michael Kolba3194d02011-09-07 11:23:51 -0700576 hideNavScreen(mUiController.getTabControl().getCurrentPosition(), false);
Michael Kolb20be26d2011-07-18 16:38:02 -0700577 }
578 }
579
John Reck1cf4b792011-07-26 10:22:22 -0700580 @Override
581 public boolean shouldCaptureThumbnails() {
582 return true;
583 }
584
Michael Kolbc3af0672011-08-09 10:24:41 -0700585 static class AnimScreen {
John Reck3ba45532011-08-11 16:26:53 -0700586
Michael Kolbc3af0672011-08-09 10:24:41 -0700587 private View mMain;
Michael Kolbc3af0672011-08-09 10:24:41 -0700588 private ImageView mContent;
589 private float mScale;
590
John Reckcc0059c2011-10-07 13:53:13 -0700591 public AnimScreen(Context ctx) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800592 mMain = LayoutInflater.from(ctx).inflate(R.layout.anim_screen, null);
593 mMain.setOnClickListener(new View.OnClickListener() {
594 @Override
595 public void onClick(View v) {
596 // just eat clicks when this view is visible
597 }
598 });
599 mContent = (ImageView) mMain.findViewById(R.id.anim_screen_content);
John Reckcc0059c2011-10-07 13:53:13 -0700600 mContent.setScaleType(ImageView.ScaleType.MATRIX);
601 mContent.setImageMatrix(new Matrix());
602 mScale = 1.0f;
603 setScaleFactor(getScaleFactor());
604 }
605
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700606 public void set(TitleBar tbar, Bitmap viewportBitmap) {
607 if (tbar == null) {
John Reck62077d82011-10-13 15:17:50 -0700608 return;
609 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700610 mContent.setImageBitmap(viewportBitmap);
Michael Kolbc3af0672011-08-09 10:24:41 -0700611 }
612
Enrico Ros1f5a0952014-11-18 20:15:48 -0800613 /*private Bitmap safeCreateBitmap(int width, int height) {
John Recka98c83b2011-10-31 12:31:58 -0700614 if (width <= 0 || height <= 0) {
615 Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
616 + ", height: " + height);
617 return null;
618 }
619 return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800620 }*/
John Recka98c83b2011-10-31 12:31:58 -0700621
John Reckcc0059c2011-10-07 13:53:13 -0700622 public void set(Bitmap image) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700623 mContent.setImageBitmap(image);
Michael Kolbc3af0672011-08-09 10:24:41 -0700624 }
625
John Reckcc0059c2011-10-07 13:53:13 -0700626 private void setScaleFactor(float sf) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700627 mScale = sf;
628 Matrix m = new Matrix();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800629 m.postScale(sf, sf);
Michael Kolbc3af0672011-08-09 10:24:41 -0700630 mContent.setImageMatrix(m);
631 }
632
John Reckcc0059c2011-10-07 13:53:13 -0700633 private float getScaleFactor() {
Michael Kolbc3af0672011-08-09 10:24:41 -0700634 return mScale;
635 }
636
John Reck3ba45532011-08-11 16:26:53 -0700637 }
638
Michael Kolb66706532010-12-12 19:50:22 -0800639}