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.systemui.statusbar.notification;
18 
19 import android.util.FloatProperty;
20 import android.util.Property;
21 import android.view.View;
22 
23 import com.android.systemui.R;
24 
25 import java.util.function.BiConsumer;
26 import java.util.function.Function;
27 
28 /**
29  * An animatable property of a view. Used with {@link PropertyAnimator}
30  */
31 public abstract class AnimatableProperty {
32 
33     public static final AnimatableProperty X = AnimatableProperty.from(View.X,
34             R.id.x_animator_tag, R.id.x_animator_tag_start_value, R.id.x_animator_tag_end_value);
35     public static final AnimatableProperty Y = AnimatableProperty.from(View.Y,
36             R.id.y_animator_tag, R.id.y_animator_tag_start_value, R.id.y_animator_tag_end_value);
37 
getAnimationStartTag()38     public abstract int getAnimationStartTag();
39 
getAnimationEndTag()40     public abstract int getAnimationEndTag();
41 
getAnimatorTag()42     public abstract int getAnimatorTag();
43 
getProperty()44     public abstract Property getProperty();
45 
from(String name, BiConsumer<T, Float> setter, Function<T, Float> getter, int animatorTag, int startValueTag, int endValueTag)46     public static <T extends View> AnimatableProperty from(String name, BiConsumer<T, Float> setter,
47             Function<T, Float> getter, int animatorTag, int startValueTag, int endValueTag) {
48         Property<T, Float> property = new FloatProperty<T>(name) {
49 
50             @Override
51             public Float get(T object) {
52                 return getter.apply(object);
53             }
54 
55             @Override
56             public void setValue(T object, float value) {
57                 setter.accept(object, value);
58             }
59         };
60         return new AnimatableProperty() {
61             @Override
62             public int getAnimationStartTag() {
63                 return startValueTag;
64             }
65 
66             @Override
67             public int getAnimationEndTag() {
68                 return endValueTag;
69             }
70 
71             @Override
72             public int getAnimatorTag() {
73                 return animatorTag;
74             }
75 
76             @Override
77             public Property getProperty() {
78                 return property;
79             }
80         };
81     }
82 
83     public static <T extends View> AnimatableProperty from(Property<T, Float> property,
84             int animatorTag, int startValueTag, int endValueTag) {
85         return new AnimatableProperty() {
86             @Override
87             public int getAnimationStartTag() {
88                 return startValueTag;
89             }
90 
91             @Override
92             public int getAnimationEndTag() {
93                 return endValueTag;
94             }
95 
96             @Override
97             public int getAnimatorTag() {
98                 return animatorTag;
99             }
100 
101             @Override
102             public Property getProperty() {
103                 return property;
104             }
105         };
106     }
107 }
108