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 
17 package androidx.constraintlayout.motion.widget;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 
22 import androidx.constraintlayout.motion.utils.ViewSpline;
23 import androidx.constraintlayout.widget.ConstraintAttribute;
24 
25 import java.util.HashMap;
26 import java.util.HashSet;
27 
28 /**
29  * Base class in an element in a KeyFrame
30  *
31  *
32  */
33 
34 public abstract class Key {
35     public static int UNSET = -1;
36     int mFramePosition = UNSET;
37     int mTargetId = UNSET;
38     String mTargetString = null;
39     protected int mType;
40 
41     @SuppressWarnings("HiddenAbstractMethod")
load(Context context, AttributeSet attrs)42     abstract void load(Context context, AttributeSet attrs);
43 
44     HashMap<String, ConstraintAttribute> mCustomConstraints;
45 
46     @SuppressWarnings("HiddenAbstractMethod")
getAttributeNames(HashSet<String> attributes)47     abstract void getAttributeNames(HashSet<String> attributes);
48 
49     public static final String ALPHA = "alpha";
50     public static final String ELEVATION = "elevation";
51     public static final String ROTATION = "rotation";
52     public static final String ROTATION_X = "rotationX";
53     public static final String ROTATION_Y = "rotationY";
54     public static final String PIVOT_X = "transformPivotX";
55     public static final String PIVOT_Y = "transformPivotY";
56     public static final String TRANSITION_PATH_ROTATE = "transitionPathRotate";
57     public static final String SCALE_X = "scaleX";
58     public static final String SCALE_Y = "scaleY";
59     public static final String WAVE_PERIOD = "wavePeriod";
60     public static final String WAVE_OFFSET = "waveOffset";
61     public static final String WAVE_PHASE = "wavePhase";
62     public static final String WAVE_VARIES_BY = "waveVariesBy";
63     public static final String TRANSLATION_X = "translationX";
64     public static final String TRANSLATION_Y = "translationY";
65     public static final String TRANSLATION_Z = "translationZ";
66     public static final String PROGRESS = "progress";
67     public static final String CUSTOM = "CUSTOM";
68     public static final String CURVEFIT = "curveFit";
69     public static final String MOTIONPROGRESS = "motionProgress";
70     public static final String TRANSITIONEASING = "transitionEasing";
71     public static final String VISIBILITY = "visibility";
72 
matches(String constraintTag)73     boolean matches(String constraintTag) {
74         if (mTargetString == null || constraintTag == null) return false;
75         return constraintTag.matches(mTargetString);
76     }
77 
78     /**
79      * Defines method to add a a view to splines derived form this key frame.
80      * The values are written to the spline
81      *
82      * @param splines splines to write values to
83      *
84      */
addValues(HashMap<String, ViewSpline> splines)85     public abstract void addValues(HashMap<String, ViewSpline> splines);
86 
87     /**
88      * Set the value associated with this tag
89      *
90      * @param tag
91      * @param value
92      *
93      */
setValue(String tag, Object value)94     public abstract void setValue(String tag, Object value);
95 
96     /**
97      * Return the float given a value. If the value is a "Float" object it is casted
98      *
99      * @param value
100      * @return
101      *
102      */
toFloat(Object value)103     float toFloat(Object value) {
104         return (value instanceof Float) ? (Float) value : Float.parseFloat(value.toString());
105     }
106 
107     /**
108      * Return the int version of an object if the value is an Integer object it is casted.
109      *
110      * @param value
111      * @return
112      *
113      */
toInt(Object value)114     int toInt(Object value) {
115         return (value instanceof Integer) ? (Integer) value : Integer.parseInt(value.toString());
116     }
117 
118     /**
119      * Return the boolean version this object if the object is a Boolean it is casted.
120      *
121      * @param value
122      * @return
123      *
124      */
toBoolean(Object value)125     boolean toBoolean(Object value) {
126         return (value instanceof Boolean) ? (Boolean) value :
127                 Boolean.parseBoolean(value.toString());
128     }
129 
130     /**
131      * Key frame can specify the type of interpolation it wants on various attributes
132      * For each string it set it to -1, CurveFit.LINEAR or  CurveFit.SPLINE
133      *
134      * @param interpolation
135      */
setInterpolation(HashMap<String, Integer> interpolation)136     public void setInterpolation(HashMap<String, Integer> interpolation) {
137     }
138 
139     /**
140      * Return a copy of this key
141      * @param src
142      * @return
143      */
copy(Key src)144     public Key copy(Key src) {
145         mFramePosition = src.mFramePosition;
146         mTargetId = src.mTargetId;
147         mTargetString = src.mTargetString;
148         mType = src.mType;
149         mCustomConstraints = src.mCustomConstraints;
150         return this;
151     }
152 
153     /**
154      * Return a copy of this
155      * @return
156      */
157     @Override
clone()158     public abstract Key clone();
159 
160     /**
161      * set the id of the view
162      * @param id
163      * @return
164      */
setViewId(int id)165     public Key setViewId(int id) {
166         mTargetId = id;
167         return this;
168     }
169 
170     /**
171      * sets the frame position
172      *
173      * @param pos
174      */
setFramePosition(int pos)175     public void setFramePosition(int pos) {
176         mFramePosition = pos;
177     }
178 
179     /**
180      * Gets the current frame position
181      *
182      * @return
183      */
getFramePosition()184     public int getFramePosition() {
185         return mFramePosition;
186     }
187 
188 }
189