1 /* 2 * Copyright (C) 2021 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 package com.android.launcher3.anim; 17 18 import static com.android.launcher3.LauncherAnimUtils.SUCCESS_TRANSITION_PROGRESS; 19 20 import android.animation.Animator; 21 import android.animation.Animator.AnimatorListener; 22 import android.animation.AnimatorListenerAdapter; 23 import android.animation.ValueAnimator; 24 25 import java.util.function.Consumer; 26 27 /** 28 * Utility class for creating common {@link AnimatorListener} 29 */ 30 public class AnimatorListeners { 31 32 /** 33 * Returns an AnimatorListener which executes the callback on successful animation completion 34 */ forSuccessCallback(Runnable callback)35 public static AnimatorListener forSuccessCallback(Runnable callback) { 36 return new RunnableSuccessListener(callback); 37 } 38 39 /** 40 * Returns an AnimatorListener which executes the callback on animation completion, 41 * with the boolean representing success 42 */ forEndCallback(Consumer<Boolean> callback)43 public static AnimatorListener forEndCallback(Consumer<Boolean> callback) { 44 return new EndStateCallbackWrapper(callback); 45 } 46 47 /** 48 * Returns an AnimatorListener which executes the callback on animation completion 49 */ forEndCallback(Runnable callback)50 public static AnimatorListener forEndCallback(Runnable callback) { 51 return new AnimatorListenerAdapter() { 52 @Override 53 public void onAnimationEnd(Animator animation) { 54 callback.run(); 55 } 56 }; 57 } 58 59 private static class EndStateCallbackWrapper extends AnimatorListenerAdapter { 60 61 private final Consumer<Boolean> mListener; 62 private boolean mListenerCalled = false; 63 64 EndStateCallbackWrapper(Consumer<Boolean> listener) { 65 mListener = listener; 66 } 67 68 @Override 69 public void onAnimationCancel(Animator animation) { 70 if (!mListenerCalled) { 71 mListenerCalled = true; 72 mListener.accept(false); 73 } 74 } 75 76 @Override 77 public void onAnimationEnd(Animator anim) { 78 if (!mListenerCalled) { 79 mListenerCalled = true; 80 mListener.accept(anim instanceof ValueAnimator 81 ? ((ValueAnimator) anim).getAnimatedFraction() > SUCCESS_TRANSITION_PROGRESS 82 : true); 83 } 84 } 85 } 86 87 private static class RunnableSuccessListener extends AnimationSuccessListener { 88 89 private final Runnable mRunnable; 90 91 private RunnableSuccessListener(Runnable r) { 92 mRunnable = r; 93 } 94 95 @Override 96 public void onAnimationSuccess(Animator animator) { 97 mRunnable.run(); 98 } 99 } 100 } 101