1 /*
2  * Copyright (C) 2018 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.util;
17 
18 import android.animation.AnimatorSet;
19 import android.annotation.TargetApi;
20 import android.os.Build;
21 
22 import java.util.ArrayList;
23 import java.util.function.Consumer;
24 
25 /**
26  * Utility class to keep track of a running animation.
27  *
28  * This class allows attaching end callbacks to an animation is intended to be used with
29  * {@link com.android.launcher3.anim.AnimatorPlaybackController}, since in that case
30  * AnimationListeners are not properly dispatched.
31  */
32 @TargetApi(Build.VERSION_CODES.O)
33 public class PendingAnimation {
34 
35     private final ArrayList<Consumer<OnEndListener>> mEndListeners = new ArrayList<>();
36 
37     public final AnimatorSet anim;
38 
PendingAnimation(AnimatorSet anim)39     public PendingAnimation(AnimatorSet anim) {
40         this.anim = anim;
41     }
42 
finish(boolean isSuccess, int logAction)43     public void finish(boolean isSuccess, int logAction) {
44         for (Consumer<OnEndListener> listeners : mEndListeners) {
45             listeners.accept(new OnEndListener(isSuccess, logAction));
46         }
47         mEndListeners.clear();
48     }
49 
addEndListener(Consumer<OnEndListener> listener)50     public void addEndListener(Consumer<OnEndListener> listener) {
51         mEndListeners.add(listener);
52     }
53 
54     public static class OnEndListener {
55         public boolean isSuccess;
56         public int logAction;
57 
OnEndListener(boolean isSuccess, int logAction)58         public OnEndListener(boolean isSuccess, int logAction) {
59             this.isSuccess = isSuccess;
60             this.logAction = logAction;
61         }
62     }
63 }
64