Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.AnimatorSet; |
| 22 | import android.animation.ObjectAnimator; |
| 23 | import android.animation.PropertyValuesHolder; |
| 24 | import android.animation.TimeInterpolator; |
| 25 | import android.content.res.Resources; |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame^] | 26 | import android.support.v7.widget.RecyclerView; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewAnimationUtils; |
| 30 | import android.view.animation.AccelerateInterpolator; |
| 31 | import android.view.animation.DecelerateInterpolator; |
| 32 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.Thunk; |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame^] | 34 | import com.android.launcher3.widget.WidgetsContainerView; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 35 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 36 | import java.util.HashMap; |
| 37 | |
| 38 | /** |
| 39 | * TODO: figure out what kind of tests we can write for this |
| 40 | * |
| 41 | * Things to test when changing the following class. |
| 42 | * - Home from workspace |
| 43 | * - from center screen |
| 44 | * - from other screens |
| 45 | * - Home from all apps |
| 46 | * - from center screen |
| 47 | * - from other screens |
| 48 | * - Back from all apps |
| 49 | * - from center screen |
| 50 | * - from other screens |
| 51 | * - Launch app from workspace and quit |
| 52 | * - with back |
| 53 | * - with home |
| 54 | * - Launch app from all apps and quit |
| 55 | * - with back |
| 56 | * - with home |
| 57 | * - Go to a screen that's not the default, then all |
| 58 | * apps, and launch and app, and go back |
| 59 | * - with back |
| 60 | * -with home |
| 61 | * - On workspace, long press power and go back |
| 62 | * - with back |
| 63 | * - with home |
| 64 | * - On all apps, long press power and go back |
| 65 | * - with back |
| 66 | * - with home |
| 67 | * - On workspace, power off |
| 68 | * - On all apps, power off |
| 69 | * - Launch an app and turn off the screen while in that app |
| 70 | * - Go back with home key |
| 71 | * - Go back with back key TODO: make this not go to workspace |
| 72 | * - From all apps |
| 73 | * - From workspace |
| 74 | * - Enter and exit car mode (becuase it causes an extra configuration changed) |
| 75 | * - From all apps |
| 76 | * - From the center workspace |
| 77 | * - From another workspace |
| 78 | */ |
| 79 | public class LauncherStateTransitionAnimation { |
| 80 | |
| 81 | /** |
| 82 | * Callbacks made during the state transition |
| 83 | */ |
| 84 | interface Callbacks { |
| 85 | public void onStateTransitionHideSearchBar(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Private callbacks made during transition setup. |
| 90 | */ |
| 91 | static abstract class PrivateTransitionCallbacks { |
| 92 | void onRevealViewVisible(View revealView, View contentView, View allAppsButtonView) {} |
| 93 | void onAnimationComplete(View revealView, View contentView, View allAppsButtonView) {} |
| 94 | float getMaterialRevealViewFinalAlpha(View revealView) { |
| 95 | return 0; |
| 96 | } |
| 97 | float getMaterialRevealViewFinalXDrift(View revealView) { |
| 98 | return 0; |
| 99 | } |
| 100 | float getMaterialRevealViewFinalYDrift(View revealView) { |
| 101 | return 0; |
| 102 | } |
| 103 | float getMaterialRevealViewStartFinalRadius() { |
| 104 | return 0; |
| 105 | } |
| 106 | AnimatorListenerAdapter getMaterialRevealViewAnimatorListener(View revealView, |
| 107 | View allAppsButtonView) { |
| 108 | return null; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | public static final String TAG = "LauncherStateTransitionAnimation"; |
| 113 | |
| 114 | // Flags to determine how to set the layers on views before the transition animation |
| 115 | public static final int BUILD_LAYER = 0; |
| 116 | public static final int BUILD_AND_SET_LAYER = 1; |
| 117 | public static final int SINGLE_FRAME_DELAY = 16; |
| 118 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 119 | @Thunk Launcher mLauncher; |
| 120 | @Thunk Callbacks mCb; |
| 121 | @Thunk AnimatorSet mStateAnimation; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 122 | |
| 123 | public LauncherStateTransitionAnimation(Launcher l, Callbacks cb) { |
| 124 | mLauncher = l; |
| 125 | mCb = cb; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Starts an animation to the apps view. |
| 130 | */ |
| 131 | public void startAnimationToAllApps(final boolean animated) { |
| 132 | final AppsContainerView toView = mLauncher.getAppsView(); |
| 133 | PrivateTransitionCallbacks cb = new PrivateTransitionCallbacks() { |
| 134 | private int[] mAllAppsToPanelDelta; |
| 135 | |
| 136 | @Override |
| 137 | public void onRevealViewVisible(View revealView, View contentView, |
| 138 | View allAppsButtonView) { |
| 139 | toView.setBackground(null); |
| 140 | // Get the y delta between the center of the page and the center of the all apps |
| 141 | // button |
| 142 | mAllAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView, |
| 143 | allAppsButtonView, null); |
| 144 | } |
| 145 | @Override |
| 146 | public float getMaterialRevealViewFinalAlpha(View revealView) { |
| 147 | return 1f; |
| 148 | } |
| 149 | @Override |
| 150 | public float getMaterialRevealViewFinalXDrift(View revealView) { |
| 151 | return mAllAppsToPanelDelta[0]; |
| 152 | } |
| 153 | @Override |
| 154 | public float getMaterialRevealViewFinalYDrift(View revealView) { |
| 155 | return mAllAppsToPanelDelta[1]; |
| 156 | } |
| 157 | @Override |
| 158 | public float getMaterialRevealViewStartFinalRadius() { |
| 159 | int allAppsButtonSize = LauncherAppState.getInstance(). |
| 160 | getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize; |
| 161 | return allAppsButtonSize / 2; |
| 162 | } |
| 163 | @Override |
| 164 | public AnimatorListenerAdapter getMaterialRevealViewAnimatorListener( |
| 165 | final View revealView, final View allAppsButtonView) { |
| 166 | return new AnimatorListenerAdapter() { |
| 167 | public void onAnimationStart(Animator animation) { |
| 168 | allAppsButtonView.setVisibility(View.INVISIBLE); |
| 169 | } |
| 170 | public void onAnimationEnd(Animator animation) { |
| 171 | allAppsButtonView.setVisibility(View.VISIBLE); |
| 172 | } |
| 173 | }; |
| 174 | } |
| 175 | }; |
| 176 | startAnimationToOverlay(Workspace.State.NORMAL_HIDDEN, toView, toView.getContentView(), |
| 177 | toView.getRevealView(), null, animated, cb); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Starts an animation to the widgets view. |
| 182 | */ |
| 183 | public void startAnimationToWidgets(final boolean animated) { |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame^] | 184 | final WidgetsContainerView toView = mLauncher.getWidgetsView(); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 185 | PrivateTransitionCallbacks cb = new PrivateTransitionCallbacks() { |
| 186 | @Override |
| 187 | public void onRevealViewVisible(View revealView, View contentView, |
| 188 | View allAppsButtonView) { |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame^] | 189 | revealView.setBackground(mLauncher.getDrawable(R.drawable.quantum_panel_dark)); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 190 | } |
| 191 | @Override |
| 192 | public float getMaterialRevealViewFinalAlpha(View revealView) { |
| 193 | return 0.3f; |
| 194 | } |
| 195 | @Override |
| 196 | public float getMaterialRevealViewFinalYDrift(View revealView) { |
| 197 | return revealView.getMeasuredHeight() / 2; |
| 198 | } |
| 199 | }; |
| 200 | startAnimationToOverlay(Workspace.State.OVERVIEW_HIDDEN, toView, toView.getContentView(), |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame^] | 201 | toView.getRevealView(), null, animated, cb); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Starts and animation to the workspace from the current overlay view. |
| 206 | */ |
| 207 | public void startAnimationToWorkspace(final Launcher.State fromState, |
| 208 | final Workspace.State toWorkspaceState, final boolean animated, |
| 209 | final Runnable onCompleteRunnable) { |
| 210 | if (toWorkspaceState != Workspace.State.NORMAL && |
| 211 | toWorkspaceState != Workspace.State.SPRING_LOADED && |
| 212 | toWorkspaceState != Workspace.State.OVERVIEW) { |
| 213 | Log.e(TAG, "Unexpected call to startAnimationToWorkspace"); |
| 214 | } |
| 215 | |
| 216 | if (fromState == Launcher.State.APPS || fromState == Launcher.State.APPS_SPRING_LOADED) { |
| 217 | startAnimationToWorkspaceFromAllApps(fromState, toWorkspaceState, animated, |
| 218 | onCompleteRunnable); |
| 219 | } else { |
| 220 | startAnimationToWorkspaceFromWidgets(fromState, toWorkspaceState, animated, |
| 221 | onCompleteRunnable); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Creates and starts a new animation to a particular overlay view. |
| 227 | */ |
| 228 | private void startAnimationToOverlay(final Workspace.State toWorkspaceState, final View toView, |
| 229 | final View contentView, final View revealView, final View pageIndicatorsView, |
| 230 | final boolean animated, final PrivateTransitionCallbacks pCb) { |
| 231 | final Resources res = mLauncher.getResources(); |
| 232 | final boolean material = Utilities.isLmpOrAbove(); |
| 233 | final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime); |
| 234 | final int itemsAlphaStagger = |
| 235 | res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger); |
| 236 | |
| 237 | final View allAppsButtonView = mLauncher.getAllAppsButton(); |
| 238 | final View fromView = mLauncher.getWorkspace(); |
| 239 | |
| 240 | final HashMap<View, Integer> layerViews = new HashMap<>(); |
| 241 | |
| 242 | // If for some reason our views aren't initialized, don't animate |
| 243 | boolean initialized = allAppsButtonView != null; |
| 244 | |
| 245 | // Cancel the current animation |
| 246 | cancelAnimation(); |
| 247 | |
| 248 | // Create the workspace animation. |
| 249 | // NOTE: this call apparently also sets the state for the workspace if !animated |
| 250 | Animator workspaceAnim = mLauncher.getWorkspace().getChangeStateAnimation( |
| 251 | toWorkspaceState, animated, layerViews); |
| 252 | |
| 253 | if (animated && initialized) { |
| 254 | mStateAnimation = LauncherAnimUtils.createAnimatorSet(); |
| 255 | |
| 256 | // Setup the reveal view animation |
| 257 | int width = revealView.getMeasuredWidth(); |
| 258 | int height = revealView.getMeasuredHeight(); |
| 259 | float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4); |
| 260 | revealView.setVisibility(View.VISIBLE); |
| 261 | revealView.setAlpha(0f); |
| 262 | revealView.setTranslationY(0f); |
| 263 | revealView.setTranslationX(0f); |
| 264 | pCb.onRevealViewVisible(revealView, contentView, allAppsButtonView); |
| 265 | |
| 266 | // Calculate the final animation values |
| 267 | final float revealViewToAlpha; |
| 268 | final float revealViewToXDrift; |
| 269 | final float revealViewToYDrift; |
| 270 | if (material) { |
| 271 | revealViewToAlpha = pCb.getMaterialRevealViewFinalAlpha(revealView); |
| 272 | revealViewToYDrift = pCb.getMaterialRevealViewFinalYDrift(revealView); |
| 273 | revealViewToXDrift = pCb.getMaterialRevealViewFinalXDrift(revealView); |
| 274 | } else { |
| 275 | revealViewToAlpha = 0f; |
| 276 | revealViewToYDrift = 2 * height / 3; |
| 277 | revealViewToXDrift = 0; |
| 278 | } |
| 279 | |
| 280 | // Create the animators |
| 281 | PropertyValuesHolder panelAlpha = |
| 282 | PropertyValuesHolder.ofFloat("alpha", revealViewToAlpha, 1f); |
| 283 | PropertyValuesHolder panelDriftY = |
| 284 | PropertyValuesHolder.ofFloat("translationY", revealViewToYDrift, 0); |
| 285 | PropertyValuesHolder panelDriftX = |
| 286 | PropertyValuesHolder.ofFloat("translationX", revealViewToXDrift, 0); |
| 287 | ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView, |
| 288 | panelAlpha, panelDriftY, panelDriftX); |
| 289 | panelAlphaAndDrift.setDuration(revealDuration); |
| 290 | panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0)); |
| 291 | |
| 292 | // Play the animation |
| 293 | layerViews.put(revealView, BUILD_AND_SET_LAYER); |
| 294 | mStateAnimation.play(panelAlphaAndDrift); |
| 295 | |
| 296 | // Setup the animation for the page indicators |
| 297 | if (pageIndicatorsView != null) { |
| 298 | pageIndicatorsView.setAlpha(0.01f); |
| 299 | ObjectAnimator indicatorsAlpha = |
| 300 | ObjectAnimator.ofFloat(pageIndicatorsView, "alpha", 1f); |
| 301 | indicatorsAlpha.setDuration(revealDuration); |
| 302 | mStateAnimation.play(indicatorsAlpha); |
| 303 | } |
| 304 | |
| 305 | // Setup the animation for the content view |
| 306 | contentView.setVisibility(View.VISIBLE); |
| 307 | contentView.setAlpha(0f); |
| 308 | contentView.setTranslationY(revealViewToYDrift); |
| 309 | layerViews.put(contentView, BUILD_AND_SET_LAYER); |
| 310 | |
| 311 | // Create the individual animators |
| 312 | ObjectAnimator pageDrift = ObjectAnimator.ofFloat(contentView, "translationY", |
| 313 | revealViewToYDrift, 0); |
| 314 | pageDrift.setDuration(revealDuration); |
| 315 | pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0)); |
| 316 | pageDrift.setStartDelay(itemsAlphaStagger); |
| 317 | mStateAnimation.play(pageDrift); |
| 318 | |
| 319 | ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(contentView, "alpha", 0f, 1f); |
| 320 | itemsAlpha.setDuration(revealDuration); |
| 321 | itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f)); |
| 322 | itemsAlpha.setStartDelay(itemsAlphaStagger); |
| 323 | mStateAnimation.play(itemsAlpha); |
| 324 | |
| 325 | if (material) { |
| 326 | // Animate the all apps button |
| 327 | float startRadius = pCb.getMaterialRevealViewStartFinalRadius(); |
| 328 | AnimatorListenerAdapter listener = pCb.getMaterialRevealViewAnimatorListener( |
| 329 | revealView, allAppsButtonView); |
| 330 | Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2, |
| 331 | height / 2, startRadius, revealRadius); |
| 332 | reveal.setDuration(revealDuration); |
| 333 | reveal.setInterpolator(new LogDecelerateInterpolator(100, 0)); |
| 334 | if (listener != null) { |
| 335 | reveal.addListener(listener); |
| 336 | } |
| 337 | mStateAnimation.play(reveal); |
| 338 | } |
| 339 | |
| 340 | mStateAnimation.addListener(new AnimatorListenerAdapter() { |
| 341 | @Override |
| 342 | public void onAnimationEnd(Animator animation) { |
| 343 | dispatchOnLauncherTransitionEnd(fromView, animated, false); |
| 344 | dispatchOnLauncherTransitionEnd(toView, animated, false); |
| 345 | |
| 346 | // Hide the reveal view |
| 347 | revealView.setVisibility(View.INVISIBLE); |
| 348 | pCb.onAnimationComplete(revealView, contentView, allAppsButtonView); |
| 349 | |
| 350 | // Disable all necessary layers |
| 351 | for (View v : layerViews.keySet()) { |
| 352 | if (layerViews.get(v) == BUILD_AND_SET_LAYER) { |
| 353 | v.setLayerType(View.LAYER_TYPE_NONE, null); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Hide the search bar |
| 358 | mCb.onStateTransitionHideSearchBar(); |
| 359 | |
| 360 | // This can hold unnecessary references to views. |
| 361 | mStateAnimation = null; |
| 362 | } |
| 363 | |
| 364 | }); |
| 365 | |
| 366 | // Play the workspace animation |
| 367 | if (workspaceAnim != null) { |
| 368 | mStateAnimation.play(workspaceAnim); |
| 369 | } |
| 370 | |
| 371 | // Dispatch the prepare transition signal |
| 372 | dispatchOnLauncherTransitionPrepare(fromView, animated, false); |
| 373 | dispatchOnLauncherTransitionPrepare(toView, animated, false); |
| 374 | |
| 375 | |
| 376 | final AnimatorSet stateAnimation = mStateAnimation; |
| 377 | final Runnable startAnimRunnable = new Runnable() { |
| 378 | public void run() { |
| 379 | // Check that mStateAnimation hasn't changed while |
| 380 | // we waited for a layout/draw pass |
| 381 | if (mStateAnimation != stateAnimation) |
| 382 | return; |
| 383 | dispatchOnLauncherTransitionStart(fromView, animated, false); |
| 384 | dispatchOnLauncherTransitionStart(toView, animated, false); |
| 385 | |
| 386 | // Enable all necessary layers |
| 387 | for (View v : layerViews.keySet()) { |
| 388 | if (layerViews.get(v) == BUILD_AND_SET_LAYER) { |
| 389 | v.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 390 | } |
| 391 | if (Utilities.isViewAttachedToWindow(v)) { |
| 392 | v.buildLayer(); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // Focus the new view |
| 397 | toView.requestFocus(); |
| 398 | |
| 399 | mStateAnimation.start(); |
| 400 | } |
| 401 | }; |
| 402 | |
| 403 | toView.bringToFront(); |
| 404 | toView.setVisibility(View.VISIBLE); |
| 405 | toView.post(startAnimRunnable); |
| 406 | } else { |
| 407 | toView.setTranslationX(0.0f); |
| 408 | toView.setTranslationY(0.0f); |
| 409 | toView.setScaleX(1.0f); |
| 410 | toView.setScaleY(1.0f); |
| 411 | toView.setVisibility(View.VISIBLE); |
| 412 | toView.bringToFront(); |
| 413 | |
| 414 | // Show the content view |
| 415 | contentView.setVisibility(View.VISIBLE); |
| 416 | |
| 417 | // Hide the search bar |
| 418 | mCb.onStateTransitionHideSearchBar(); |
| 419 | |
| 420 | dispatchOnLauncherTransitionPrepare(fromView, animated, false); |
| 421 | dispatchOnLauncherTransitionStart(fromView, animated, false); |
| 422 | dispatchOnLauncherTransitionEnd(fromView, animated, false); |
| 423 | dispatchOnLauncherTransitionPrepare(toView, animated, false); |
| 424 | dispatchOnLauncherTransitionStart(toView, animated, false); |
| 425 | dispatchOnLauncherTransitionEnd(toView, animated, false); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Starts and animation to the workspace from the apps view. |
| 431 | */ |
| 432 | private void startAnimationToWorkspaceFromAllApps(final Launcher.State fromState, |
| 433 | final Workspace.State toWorkspaceState, final boolean animated, |
| 434 | final Runnable onCompleteRunnable) { |
| 435 | AppsContainerView appsView = mLauncher.getAppsView(); |
| 436 | PrivateTransitionCallbacks cb = new PrivateTransitionCallbacks() { |
| 437 | int[] mAllAppsToPanelDelta; |
| 438 | |
| 439 | @Override |
| 440 | public void onRevealViewVisible(View revealView, View contentView, |
| 441 | View allAppsButtonView) { |
| 442 | // Get the y delta between the center of the page and the center of the all apps |
| 443 | // button |
| 444 | mAllAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView, |
| 445 | allAppsButtonView, null); |
| 446 | } |
| 447 | @Override |
| 448 | public float getMaterialRevealViewFinalXDrift(View revealView) { |
| 449 | return mAllAppsToPanelDelta[0]; |
| 450 | } |
| 451 | @Override |
| 452 | public float getMaterialRevealViewFinalYDrift(View revealView) { |
| 453 | return mAllAppsToPanelDelta[1]; |
| 454 | } |
| 455 | @Override |
| 456 | float getMaterialRevealViewFinalAlpha(View revealView) { |
| 457 | // No alpha anim from all apps |
| 458 | return 1f; |
| 459 | } |
| 460 | @Override |
| 461 | float getMaterialRevealViewStartFinalRadius() { |
| 462 | int allAppsButtonSize = LauncherAppState.getInstance(). |
| 463 | getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize; |
| 464 | return allAppsButtonSize / 2; |
| 465 | } |
| 466 | @Override |
| 467 | public AnimatorListenerAdapter getMaterialRevealViewAnimatorListener( |
| 468 | final View revealView, final View allAppsButtonView) { |
| 469 | return new AnimatorListenerAdapter() { |
| 470 | public void onAnimationStart(Animator animation) { |
| 471 | // We set the alpha instead of visibility to ensure that the focus does not |
| 472 | // get taken from the all apps view |
| 473 | allAppsButtonView.setVisibility(View.VISIBLE); |
| 474 | allAppsButtonView.setAlpha(0f); |
| 475 | } |
| 476 | public void onAnimationEnd(Animator animation) { |
| 477 | // Hide the reveal view |
| 478 | revealView.setVisibility(View.INVISIBLE); |
| 479 | |
| 480 | // Show the all apps button, and focus it |
| 481 | allAppsButtonView.setAlpha(1f); |
| 482 | } |
| 483 | }; |
| 484 | } |
| 485 | }; |
| 486 | startAnimationToWorkspaceFromOverlay(toWorkspaceState, appsView, appsView.getContentView(), |
| 487 | appsView.getRevealView(), null /* pageIndicatorsView */, animated, |
| 488 | onCompleteRunnable, cb); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Starts and animation to the workspace from the widgets view. |
| 493 | */ |
| 494 | private void startAnimationToWorkspaceFromWidgets(final Launcher.State fromState, |
| 495 | final Workspace.State toWorkspaceState, final boolean animated, |
| 496 | final Runnable onCompleteRunnable) { |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame^] | 497 | WidgetsContainerView widgetsView = mLauncher.getWidgetsView(); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 498 | PrivateTransitionCallbacks cb = new PrivateTransitionCallbacks() { |
| 499 | @Override |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 500 | public float getMaterialRevealViewFinalYDrift(View revealView) { |
| 501 | return revealView.getMeasuredHeight() / 2; |
| 502 | } |
| 503 | @Override |
| 504 | float getMaterialRevealViewFinalAlpha(View revealView) { |
| 505 | return 0.4f; |
| 506 | } |
| 507 | @Override |
| 508 | public AnimatorListenerAdapter getMaterialRevealViewAnimatorListener( |
| 509 | final View revealView, final View allAppsButtonView) { |
| 510 | return new AnimatorListenerAdapter() { |
| 511 | public void onAnimationEnd(Animator animation) { |
| 512 | // Hide the reveal view |
| 513 | revealView.setVisibility(View.INVISIBLE); |
| 514 | } |
| 515 | }; |
| 516 | } |
| 517 | }; |
| 518 | startAnimationToWorkspaceFromOverlay(toWorkspaceState, widgetsView, |
| 519 | widgetsView.getContentView(), widgetsView.getRevealView(), |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame^] | 520 | null, animated, onCompleteRunnable, cb); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Creates and starts a new animation to the workspace. |
| 525 | */ |
| 526 | private void startAnimationToWorkspaceFromOverlay(final Workspace.State toWorkspaceState, |
| 527 | final View fromView, final View contentView, final View revealView, |
| 528 | final View pageIndicatorsView, final boolean animated, |
| 529 | final Runnable onCompleteRunnable, final PrivateTransitionCallbacks pCb) { |
| 530 | final Resources res = mLauncher.getResources(); |
| 531 | final boolean material = Utilities.isLmpOrAbove(); |
| 532 | final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime); |
| 533 | final int itemsAlphaStagger = |
| 534 | res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger); |
| 535 | |
| 536 | final View allAppsButtonView = mLauncher.getAllAppsButton(); |
| 537 | final View toView = mLauncher.getWorkspace(); |
| 538 | |
| 539 | final HashMap<View, Integer> layerViews = new HashMap<>(); |
| 540 | |
| 541 | // If for some reason our views aren't initialized, don't animate |
| 542 | boolean initialized = allAppsButtonView != null; |
| 543 | |
| 544 | // Cancel the current animation |
| 545 | cancelAnimation(); |
| 546 | |
| 547 | // Create the workspace animation. |
| 548 | // NOTE: this call apparently also sets the state for the workspace if !animated |
| 549 | Animator workspaceAnim = mLauncher.getWorkspace().getChangeStateAnimation( |
| 550 | toWorkspaceState, animated, layerViews); |
| 551 | |
| 552 | if (animated && initialized) { |
| 553 | mStateAnimation = LauncherAnimUtils.createAnimatorSet(); |
| 554 | |
| 555 | // Play the workspace animation |
| 556 | if (workspaceAnim != null) { |
| 557 | mStateAnimation.play(workspaceAnim); |
| 558 | } |
| 559 | |
| 560 | // hideAppsCustomizeHelper is called in some cases when it is already hidden |
| 561 | // don't perform all these no-op animations. In particularly, this was causing |
| 562 | // the all-apps button to pop in and out. |
| 563 | if (fromView.getVisibility() == View.VISIBLE) { |
| 564 | int width = revealView.getMeasuredWidth(); |
| 565 | int height = revealView.getMeasuredHeight(); |
| 566 | float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4); |
| 567 | revealView.setVisibility(View.VISIBLE); |
| 568 | revealView.setAlpha(1f); |
| 569 | revealView.setTranslationY(0); |
| 570 | layerViews.put(revealView, BUILD_AND_SET_LAYER); |
| 571 | pCb.onRevealViewVisible(revealView, contentView, allAppsButtonView); |
| 572 | |
| 573 | // Calculate the final animation values |
| 574 | final float revealViewToXDrift; |
| 575 | final float revealViewToYDrift; |
| 576 | if (material) { |
| 577 | revealViewToYDrift = pCb.getMaterialRevealViewFinalYDrift(revealView); |
| 578 | revealViewToXDrift = pCb.getMaterialRevealViewFinalXDrift(revealView); |
| 579 | } else { |
| 580 | revealViewToYDrift = 2 * height / 3; |
| 581 | revealViewToXDrift = 0; |
| 582 | } |
| 583 | |
| 584 | // The vertical motion of the apps panel should be delayed by one frame |
| 585 | // from the conceal animation in order to give the right feel. We correspondingly |
| 586 | // shorten the duration so that the slide and conceal end at the same time. |
| 587 | TimeInterpolator decelerateInterpolator = material ? |
| 588 | new LogDecelerateInterpolator(100, 0) : |
| 589 | new DecelerateInterpolator(1f); |
| 590 | ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY", |
| 591 | 0, revealViewToYDrift); |
| 592 | panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY); |
| 593 | panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY); |
| 594 | panelDriftY.setInterpolator(decelerateInterpolator); |
| 595 | mStateAnimation.play(panelDriftY); |
| 596 | |
| 597 | ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX", |
| 598 | 0, revealViewToXDrift); |
| 599 | panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY); |
| 600 | panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY); |
| 601 | panelDriftX.setInterpolator(decelerateInterpolator); |
| 602 | mStateAnimation.play(panelDriftX); |
| 603 | |
| 604 | // Setup animation for the reveal panel alpha |
| 605 | final float revealViewToAlpha = !material ? 0f : |
| 606 | pCb.getMaterialRevealViewFinalAlpha(revealView); |
| 607 | if (revealViewToAlpha != 1f) { |
| 608 | ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha", |
| 609 | 1f, revealViewToAlpha); |
| 610 | panelAlpha.setDuration(material ? revealDuration : 150); |
| 611 | panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY); |
| 612 | panelAlpha.setInterpolator(decelerateInterpolator); |
| 613 | mStateAnimation.play(panelAlpha); |
| 614 | } |
| 615 | |
| 616 | // Setup the animation for the content view |
| 617 | layerViews.put(contentView, BUILD_AND_SET_LAYER); |
| 618 | |
| 619 | // Create the individual animators |
| 620 | ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(contentView, "translationY", |
| 621 | 0, revealViewToYDrift); |
| 622 | contentView.setTranslationY(0); |
| 623 | pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY); |
| 624 | pageDrift.setInterpolator(decelerateInterpolator); |
| 625 | pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY); |
| 626 | mStateAnimation.play(pageDrift); |
| 627 | |
| 628 | contentView.setAlpha(1f); |
| 629 | ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(contentView, "alpha", 1f, 0f); |
| 630 | itemsAlpha.setDuration(100); |
| 631 | itemsAlpha.setInterpolator(decelerateInterpolator); |
| 632 | mStateAnimation.play(itemsAlpha); |
| 633 | |
| 634 | // Setup the page indicators animation |
| 635 | if (pageIndicatorsView != null) { |
| 636 | pageIndicatorsView.setAlpha(1f); |
| 637 | ObjectAnimator indicatorsAlpha = |
| 638 | LauncherAnimUtils.ofFloat(pageIndicatorsView, "alpha", 0f); |
| 639 | indicatorsAlpha.setDuration(revealDuration); |
| 640 | indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f)); |
| 641 | mStateAnimation.play(indicatorsAlpha); |
| 642 | } |
| 643 | |
| 644 | if (material) { |
| 645 | // Animate the all apps button |
| 646 | float finalRadius = pCb.getMaterialRevealViewStartFinalRadius(); |
| 647 | AnimatorListenerAdapter listener = |
| 648 | pCb.getMaterialRevealViewAnimatorListener(revealView, allAppsButtonView); |
| 649 | Animator reveal = |
| 650 | LauncherAnimUtils.createCircularReveal(revealView, width / 2, |
| 651 | height / 2, revealRadius, finalRadius); |
| 652 | reveal.setInterpolator(new LogDecelerateInterpolator(100, 0)); |
| 653 | reveal.setDuration(revealDuration); |
| 654 | reveal.setStartDelay(itemsAlphaStagger); |
| 655 | if (listener != null) { |
| 656 | reveal.addListener(listener); |
| 657 | } |
| 658 | mStateAnimation.play(reveal); |
| 659 | } |
| 660 | |
| 661 | dispatchOnLauncherTransitionPrepare(fromView, animated, true); |
| 662 | dispatchOnLauncherTransitionPrepare(toView, animated, true); |
| 663 | } |
| 664 | |
| 665 | mStateAnimation.addListener(new AnimatorListenerAdapter() { |
| 666 | @Override |
| 667 | public void onAnimationEnd(Animator animation) { |
| 668 | fromView.setVisibility(View.GONE); |
| 669 | dispatchOnLauncherTransitionEnd(fromView, animated, true); |
| 670 | dispatchOnLauncherTransitionEnd(toView, animated, true); |
| 671 | |
| 672 | // Run any queued runnables |
| 673 | if (onCompleteRunnable != null) { |
| 674 | onCompleteRunnable.run(); |
| 675 | } |
| 676 | |
| 677 | // Animation complete callback |
| 678 | pCb.onAnimationComplete(revealView, contentView, allAppsButtonView); |
| 679 | |
| 680 | // Disable all necessary layers |
| 681 | for (View v : layerViews.keySet()) { |
| 682 | if (layerViews.get(v) == BUILD_AND_SET_LAYER) { |
| 683 | v.setLayerType(View.LAYER_TYPE_NONE, null); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // Reset page transforms |
| 688 | if (contentView != null) { |
| 689 | contentView.setTranslationX(0); |
| 690 | contentView.setTranslationY(0); |
| 691 | contentView.setAlpha(1); |
| 692 | } |
| 693 | |
| 694 | // This can hold unnecessary references to views. |
| 695 | mStateAnimation = null; |
| 696 | } |
| 697 | }); |
| 698 | |
| 699 | final AnimatorSet stateAnimation = mStateAnimation; |
| 700 | final Runnable startAnimRunnable = new Runnable() { |
| 701 | public void run() { |
| 702 | // Check that mStateAnimation hasn't changed while |
| 703 | // we waited for a layout/draw pass |
| 704 | if (mStateAnimation != stateAnimation) |
| 705 | return; |
| 706 | dispatchOnLauncherTransitionStart(fromView, animated, false); |
| 707 | dispatchOnLauncherTransitionStart(toView, animated, false); |
| 708 | |
| 709 | // Enable all necessary layers |
| 710 | for (View v : layerViews.keySet()) { |
| 711 | if (layerViews.get(v) == BUILD_AND_SET_LAYER) { |
| 712 | v.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 713 | } |
| 714 | if (Utilities.isLmpOrAbove()) { |
| 715 | v.buildLayer(); |
| 716 | } |
| 717 | } |
| 718 | mStateAnimation.start(); |
| 719 | } |
| 720 | }; |
| 721 | fromView.post(startAnimRunnable); |
| 722 | } else { |
| 723 | fromView.setVisibility(View.GONE); |
| 724 | dispatchOnLauncherTransitionPrepare(fromView, animated, true); |
| 725 | dispatchOnLauncherTransitionStart(fromView, animated, true); |
| 726 | dispatchOnLauncherTransitionEnd(fromView, animated, true); |
| 727 | dispatchOnLauncherTransitionPrepare(toView, animated, true); |
| 728 | dispatchOnLauncherTransitionStart(toView, animated, true); |
| 729 | dispatchOnLauncherTransitionEnd(toView, animated, true); |
| 730 | |
| 731 | // Run any queued runnables |
| 732 | if (onCompleteRunnable != null) { |
| 733 | onCompleteRunnable.run(); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | |
| 739 | /** |
| 740 | * Dispatches the prepare-transition event to suitable views. |
| 741 | */ |
| 742 | void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) { |
| 743 | if (v instanceof LauncherTransitionable) { |
| 744 | ((LauncherTransitionable) v).onLauncherTransitionPrepare(mLauncher, animated, |
| 745 | toWorkspace); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Dispatches the start-transition event to suitable views. |
| 751 | */ |
| 752 | void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) { |
| 753 | if (v instanceof LauncherTransitionable) { |
| 754 | ((LauncherTransitionable) v).onLauncherTransitionStart(mLauncher, animated, |
| 755 | toWorkspace); |
| 756 | } |
| 757 | |
| 758 | // Update the workspace transition step as well |
| 759 | dispatchOnLauncherTransitionStep(v, 0f); |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * Dispatches the step-transition event to suitable views. |
| 764 | */ |
| 765 | void dispatchOnLauncherTransitionStep(View v, float t) { |
| 766 | if (v instanceof LauncherTransitionable) { |
| 767 | ((LauncherTransitionable) v).onLauncherTransitionStep(mLauncher, t); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * Dispatches the end-transition event to suitable views. |
| 773 | */ |
| 774 | void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) { |
| 775 | if (v instanceof LauncherTransitionable) { |
| 776 | ((LauncherTransitionable) v).onLauncherTransitionEnd(mLauncher, animated, |
| 777 | toWorkspace); |
| 778 | } |
| 779 | |
| 780 | // Update the workspace transition step as well |
| 781 | dispatchOnLauncherTransitionStep(v, 1f); |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Cancels the current animation. |
| 786 | */ |
| 787 | private void cancelAnimation() { |
| 788 | if (mStateAnimation != null) { |
| 789 | mStateAnimation.setDuration(0); |
| 790 | mStateAnimation.cancel(); |
| 791 | mStateAnimation = null; |
| 792 | } |
| 793 | } |
| 794 | } |