1 /*
2  * Copyright (C) 2016 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.systemui.statusbar.notification.stack;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.util.ArrayMap;
22 import android.util.Property;
23 import android.view.View;
24 import android.view.animation.Interpolator;
25 
26 import java.util.function.Consumer;
27 
28 /**
29  * Properties for a View animation
30  */
31 public class AnimationProperties {
32     public long duration;
33     public long delay;
34     private ArrayMap<Property, Interpolator> mInterpolatorMap;
35     private Consumer<Property> mAnimationCancelAction;
36     private Consumer<Property> mAnimationEndAction;
37 
38     /**
39      * @return an animation filter for this animation.
40      */
getAnimationFilter()41     public AnimationFilter getAnimationFilter() {
42         return new AnimationFilter() {
43             @Override
44             public boolean shouldAnimateProperty(Property property) {
45                 return true;
46             }
47         };
48     }
49 
50     /**
51      * @return a listener that will be added for a given property during its animation.
52      */
53     public AnimatorListenerAdapter getAnimationFinishListener(Property property) {
54         if (mAnimationEndAction == null && mAnimationCancelAction == null) {
55             return null;
56         }
57         Consumer<Property> cancelAction = mAnimationCancelAction;
58         Consumer<Property> endAction = mAnimationEndAction;
59         return new AnimatorListenerAdapter() {
60             private boolean mCancelled;
61 
62             @Override
63             public void onAnimationCancel(Animator animation) {
64                 mCancelled = true;
65                 if (cancelAction != null) {
66                     cancelAction.accept(property);
67                 }
68             }
69 
70             @Override
71             public void onAnimationEnd(Animator animation) {
72                 if (!mCancelled && endAction != null) {
73                     endAction.accept(property);
74                 }
75             }
76         };
77     }
78 
79     /**
80      * Add a callback for animation cancellation.
81      */
82     public AnimationProperties setAnimationCancelAction(Consumer<Property> listener) {
83         mAnimationCancelAction = listener;
84         return this;
85     }
86 
87     /**
88      * Add a callback for animation ending successfully. The callback will not be called when the
89      * animations is cancelled.
90      */
91     public AnimationProperties setAnimationEndAction(Consumer<Property> listener) {
92         mAnimationEndAction = listener;
93         return this;
94     }
95 
96     public boolean wasAdded(View view) {
97         return false;
98     }
99 
100     /**
101      * Get a custom interpolator for a property instead of the normal one.
102      */
103     public Interpolator getCustomInterpolator(View child, Property property) {
104         return mInterpolatorMap != null ? mInterpolatorMap.get(property) : null;
105     }
106 
107 
108     public void combineCustomInterpolators(AnimationProperties iconAnimationProperties) {
109         ArrayMap<Property, Interpolator> map = iconAnimationProperties.mInterpolatorMap;
110         if (map != null) {
111             if (mInterpolatorMap == null) {
112                 mInterpolatorMap = new ArrayMap<>();
113             }
114             mInterpolatorMap.putAll(map);
115         }
116     }
117 
118     /**
119      * Set a custom interpolator to use for all views for a property.
120      */
121     public AnimationProperties setCustomInterpolator(Property property, Interpolator interpolator) {
122         if (mInterpolatorMap == null) {
123             mInterpolatorMap = new ArrayMap<>();
124         }
125         mInterpolatorMap.put(property, interpolator);
126         return this;
127     }
128 
129     public AnimationProperties setDuration(long duration) {
130         this.duration = duration;
131         return this;
132     }
133 
134     public AnimationProperties setDelay(long delay) {
135         this.delay = delay;
136         return this;
137     }
138 
139     public AnimationProperties resetCustomInterpolators() {
140         mInterpolatorMap = null;
141         return this;
142     }
143 }
144