1/*
2 * Copyright (C) 2014 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.v7.view;
18
19import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20
21import android.support.annotation.RestrictTo;
22import android.support.v4.view.ViewPropertyAnimatorCompat;
23import android.support.v4.view.ViewPropertyAnimatorListener;
24import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
25import android.view.View;
26import android.view.animation.Interpolator;
27
28import java.util.ArrayList;
29
30/**
31 * A very naive implementation of a set of
32 * {@link android.support.v4.view.ViewPropertyAnimatorCompat}.
33 *
34 * @hide
35 */
36@RestrictTo(LIBRARY_GROUP)
37public class ViewPropertyAnimatorCompatSet {
38
39    final ArrayList<ViewPropertyAnimatorCompat> mAnimators;
40
41    private long mDuration = -1;
42    private Interpolator mInterpolator;
43    ViewPropertyAnimatorListener mListener;
44
45    private boolean mIsStarted;
46
47    public ViewPropertyAnimatorCompatSet() {
48        mAnimators = new ArrayList<ViewPropertyAnimatorCompat>();
49    }
50
51    public ViewPropertyAnimatorCompatSet play(ViewPropertyAnimatorCompat animator) {
52        if (!mIsStarted) {
53            mAnimators.add(animator);
54        }
55        return this;
56    }
57
58    public ViewPropertyAnimatorCompatSet playSequentially(ViewPropertyAnimatorCompat anim1,
59            ViewPropertyAnimatorCompat anim2) {
60        mAnimators.add(anim1);
61        anim2.setStartDelay(anim1.getDuration());
62        mAnimators.add(anim2);
63        return this;
64    }
65
66    public void start() {
67        if (mIsStarted) return;
68        for (ViewPropertyAnimatorCompat animator : mAnimators) {
69            if (mDuration >= 0) {
70                animator.setDuration(mDuration);
71            }
72            if (mInterpolator != null) {
73                animator.setInterpolator(mInterpolator);
74            }
75            if (mListener != null) {
76                animator.setListener(mProxyListener);
77            }
78            animator.start();
79        }
80
81        mIsStarted = true;
82    }
83
84    void onAnimationsEnded() {
85        mIsStarted = false;
86    }
87
88    public void cancel() {
89        if (!mIsStarted) {
90            return;
91        }
92        for (ViewPropertyAnimatorCompat animator : mAnimators) {
93            animator.cancel();
94        }
95        mIsStarted = false;
96    }
97
98    public ViewPropertyAnimatorCompatSet setDuration(long duration) {
99        if (!mIsStarted) {
100            mDuration = duration;
101        }
102        return this;
103    }
104
105    public ViewPropertyAnimatorCompatSet setInterpolator(Interpolator interpolator) {
106        if (!mIsStarted) {
107            mInterpolator = interpolator;
108        }
109        return this;
110    }
111
112    public ViewPropertyAnimatorCompatSet setListener(ViewPropertyAnimatorListener listener) {
113        if (!mIsStarted) {
114            mListener = listener;
115        }
116        return this;
117    }
118
119    private final ViewPropertyAnimatorListenerAdapter mProxyListener
120            = new ViewPropertyAnimatorListenerAdapter() {
121        private boolean mProxyStarted = false;
122        private int mProxyEndCount = 0;
123
124        @Override
125        public void onAnimationStart(View view) {
126            if (mProxyStarted) {
127                return;
128            }
129            mProxyStarted = true;
130            if (mListener != null) {
131                mListener.onAnimationStart(null);
132            }
133        }
134
135        void onEnd() {
136            mProxyEndCount = 0;
137            mProxyStarted = false;
138            onAnimationsEnded();
139        }
140
141        @Override
142        public void onAnimationEnd(View view) {
143            if (++mProxyEndCount == mAnimators.size()) {
144                if (mListener != null) {
145                    mListener.onAnimationEnd(null);
146                }
147                onEnd();
148            }
149        }
150    };
151}
152