1 /*
2  * Copyright (C) 2017 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.anim;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 
22 /**
23  * Extension of {@link AnimatorListenerAdapter} for listening for non-cancelled animations
24  */
25 public abstract class AnimationSuccessListener extends AnimatorListenerAdapter {
26 
27     protected boolean mCancelled = false;
28 
29     @Override
onAnimationCancel(Animator animation)30     public void onAnimationCancel(Animator animation) {
31         mCancelled = true;
32     }
33 
34     @Override
onAnimationEnd(Animator animation)35     public void onAnimationEnd(Animator animation) {
36         if (!mCancelled) {
37             onAnimationSuccess(animation);
38         }
39     }
40 
onAnimationSuccess(Animator animator)41     public abstract void onAnimationSuccess(Animator animator);
42 
43     /**
44      * Returns an AnimationSuccessListener which runs the provided action on success
45      */
forRunnable(Runnable r)46     public static AnimationSuccessListener forRunnable(Runnable r) {
47         return new RunnableSuccessListener(r);
48     }
49 
50     private static class RunnableSuccessListener extends AnimationSuccessListener {
51 
52         private final Runnable mRunnable;
53 
RunnableSuccessListener(Runnable r)54         private RunnableSuccessListener(Runnable r) {
55             mRunnable = r;
56         }
57 
58         @Override
onAnimationSuccess(Animator animator)59         public void onAnimationSuccess(Animator animator) {
60             mRunnable.run();
61         }
62     }
63 }
64