1/*
2 * Copyright (C) 2015 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
17package android.support.design.widget;
18
19import android.util.StateSet;
20import android.view.View;
21import android.view.animation.Animation;
22
23import java.lang.ref.WeakReference;
24import java.util.ArrayList;
25
26final class StateListAnimator {
27
28    private final ArrayList<Tuple> mTuples = new ArrayList<>();
29
30    private Tuple mLastMatch = null;
31    private Animation mRunningAnimation = null;
32    private WeakReference<View> mViewRef;
33
34    private Animation.AnimationListener mAnimationListener = new Animation.AnimationListener() {
35        @Override
36        public void onAnimationEnd(Animation animation) {
37            if (mRunningAnimation == animation) {
38                mRunningAnimation = null;
39            }
40        }
41
42        @Override
43        public void onAnimationStart(Animation animation) {
44            // no-op
45        }
46
47        @Override
48        public void onAnimationRepeat(Animation animation) {
49            // no-op
50        }
51    };
52
53    /**
54     * Associates the given Animation with the provided drawable state specs so that it will be run
55     * when the View's drawable state matches the specs.
56     *
57     * @param specs    The drawable state specs to match against
58     * @param animation The Animation to run when the specs match
59     */
60    public void addState(int[] specs, Animation animation) {
61        Tuple tuple = new Tuple(specs, animation);
62        animation.setAnimationListener(mAnimationListener);
63        mTuples.add(tuple);
64    }
65
66    /**
67     * Returns the current {@link Animation} which is started because of a state
68     * change.
69     *
70     * @return The currently running Animation or null if no Animation is running
71     */
72    Animation getRunningAnimation() {
73        return mRunningAnimation;
74    }
75
76
77    View getTarget() {
78        return mViewRef == null ? null : mViewRef.get();
79    }
80
81    void setTarget(View view) {
82        final View current = getTarget();
83        if (current == view) {
84            return;
85        }
86        if (current != null) {
87            clearTarget();
88        }
89        if (view != null) {
90            mViewRef = new WeakReference<>(view);
91        }
92    }
93
94    private void clearTarget() {
95        final View view = getTarget();
96        final int size = mTuples.size();
97        for (int i = 0; i < size; i++) {
98            Animation anim = mTuples.get(i).mAnimation;
99            if (view.getAnimation() == anim) {
100                view.clearAnimation();
101            }
102        }
103        mViewRef = null;
104        mLastMatch = null;
105        mRunningAnimation = null;
106    }
107
108    /**
109     * Called by View
110     */
111    void setState(int[] state) {
112        Tuple match = null;
113        final int count = mTuples.size();
114        for (int i = 0; i < count; i++) {
115            final Tuple tuple = mTuples.get(i);
116            if (StateSet.stateSetMatches(tuple.mSpecs, state)) {
117                match = tuple;
118                break;
119            }
120        }
121        if (match == mLastMatch) {
122            return;
123        }
124        if (mLastMatch != null) {
125            cancel();
126        }
127
128        mLastMatch = match;
129
130        View view = mViewRef.get();
131        if (match != null && view != null && view.getVisibility() == View.VISIBLE ) {
132            start(match);
133        }
134    }
135
136    private void start(Tuple match) {
137        mRunningAnimation = match.mAnimation;
138
139        View view = getTarget();
140        if (view != null) {
141            view.startAnimation(mRunningAnimation);
142        }
143    }
144
145    private void cancel() {
146        if (mRunningAnimation != null) {
147            final View view = getTarget();
148            if (view != null && view.getAnimation() == mRunningAnimation) {
149                view.clearAnimation();
150            }
151            mRunningAnimation = null;
152        }
153    }
154
155    /**
156     * @hide
157     */
158    ArrayList<Tuple> getTuples() {
159        return mTuples;
160    }
161
162    /**
163     * If there is an animation running for a recent state change, ends it. <p> This causes the
164     * animation to assign the end value(s) to the View.
165     */
166    public void jumpToCurrentState() {
167        if (mRunningAnimation != null) {
168            final View view = getTarget();
169            if (view != null && view.getAnimation() == mRunningAnimation) {
170                view.clearAnimation();
171            }
172        }
173    }
174
175    static class Tuple {
176        final int[] mSpecs;
177        final Animation mAnimation;
178
179        private Tuple(int[] specs, Animation Animation) {
180            mSpecs = specs;
181            mAnimation = Animation;
182        }
183
184        int[] getSpecs() {
185            return mSpecs;
186        }
187
188        Animation getAnimation() {
189            return mAnimation;
190        }
191    }
192}