1/*
2 * Copyright (C) 2012 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 com.android.launcher2;
18
19import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.TimeInterpolator;
22import android.view.ViewPropertyAnimator;
23import android.view.View;
24
25import java.util.ArrayList;
26import java.util.EnumSet;
27
28public class LauncherViewPropertyAnimator extends Animator implements AnimatorListener {
29    enum Properties {
30            TRANSLATION_X,
31            TRANSLATION_Y,
32            SCALE_X,
33            SCALE_Y,
34            ROTATION_Y,
35            ALPHA,
36            START_DELAY,
37            DURATION,
38            INTERPOLATOR
39    }
40    EnumSet<Properties> mPropertiesToSet = EnumSet.noneOf(Properties.class);
41    ViewPropertyAnimator mViewPropertyAnimator;
42    View mTarget;
43
44    float mTranslationX;
45    float mTranslationY;
46    float mScaleX;
47    float mScaleY;
48    float mRotationY;
49    float mAlpha;
50    long mStartDelay;
51    long mDuration;
52    TimeInterpolator mInterpolator;
53    ArrayList<Animator.AnimatorListener> mListeners;
54    boolean mRunning = false;
55
56    public LauncherViewPropertyAnimator(View target) {
57        mTarget = target;
58        mListeners = new ArrayList<Animator.AnimatorListener>();
59    }
60
61    @Override
62    public void addListener(Animator.AnimatorListener listener) {
63        mListeners.add(listener);
64    }
65
66    @Override
67    public void cancel() {
68        if (mViewPropertyAnimator != null) {
69            mViewPropertyAnimator.cancel();
70        }
71    }
72
73    @Override
74    public Animator clone() {
75        throw new RuntimeException("Not implemented");
76    }
77
78    @Override
79    public void end() {
80        throw new RuntimeException("Not implemented");
81    }
82
83    @Override
84    public long getDuration() {
85        return mDuration;
86    }
87
88    @Override
89    public ArrayList<Animator.AnimatorListener> getListeners() {
90        return mListeners;
91    }
92
93    @Override
94    public long getStartDelay() {
95        return mStartDelay;
96    }
97
98    @Override
99    public void onAnimationCancel(Animator animation) {
100        for (int i = 0; i < mListeners.size(); i++) {
101            Animator.AnimatorListener listener = mListeners.get(i);
102            listener.onAnimationCancel(this);
103        }
104        mRunning = false;
105    }
106
107    @Override
108    public void onAnimationEnd(Animator animation) {
109        for (int i = 0; i < mListeners.size(); i++) {
110            Animator.AnimatorListener listener = mListeners.get(i);
111            listener.onAnimationEnd(this);
112        }
113        mRunning = false;
114    }
115
116    @Override
117    public void onAnimationRepeat(Animator animation) {
118        for (int i = 0; i < mListeners.size(); i++) {
119            Animator.AnimatorListener listener = mListeners.get(i);
120            listener.onAnimationRepeat(this);
121        }
122    }
123
124    @Override
125    public void onAnimationStart(Animator animation) {
126        for (int i = 0; i < mListeners.size(); i++) {
127            Animator.AnimatorListener listener = mListeners.get(i);
128            listener.onAnimationStart(this);
129        }
130        mRunning = true;
131    }
132
133    @Override
134    public boolean isRunning() {
135        return mRunning;
136    }
137
138    @Override
139    public boolean isStarted() {
140        return mViewPropertyAnimator != null;
141    }
142
143    @Override
144    public void removeAllListeners() {
145        mListeners.clear();
146    }
147
148    @Override
149    public void removeListener(Animator.AnimatorListener listener) {
150        mListeners.remove(listener);
151    }
152
153    @Override
154    public Animator setDuration(long duration) {
155        mPropertiesToSet.add(Properties.DURATION);
156        mDuration = duration;
157        return this;
158    }
159
160    @Override
161    public void setInterpolator(TimeInterpolator value) {
162        mPropertiesToSet.add(Properties.INTERPOLATOR);
163        mInterpolator = value;
164    }
165
166    @Override
167    public void setStartDelay(long startDelay) {
168        mPropertiesToSet.add(Properties.START_DELAY);
169        mStartDelay = startDelay;
170    }
171
172    @Override
173    public void setTarget(Object target) {
174        throw new RuntimeException("Not implemented");
175    }
176
177    @Override
178    public void setupEndValues() {
179
180    }
181
182    @Override
183    public void setupStartValues() {
184    }
185
186    @Override
187    public void start() {
188        mViewPropertyAnimator = mTarget.animate();
189        if (mPropertiesToSet.contains(Properties.TRANSLATION_X)) {
190            mViewPropertyAnimator.translationX(mTranslationX);
191        }
192        if (mPropertiesToSet.contains(Properties.TRANSLATION_Y)) {
193            mViewPropertyAnimator.translationY(mTranslationY);
194        }
195        if (mPropertiesToSet.contains(Properties.SCALE_X)) {
196            mViewPropertyAnimator.scaleX(mScaleX);
197        }
198        if (mPropertiesToSet.contains(Properties.ROTATION_Y)) {
199            mViewPropertyAnimator.rotationY(mRotationY);
200        }
201        if (mPropertiesToSet.contains(Properties.SCALE_Y)) {
202            mViewPropertyAnimator.scaleY(mScaleY);
203        }
204        if (mPropertiesToSet.contains(Properties.ALPHA)) {
205            mViewPropertyAnimator.alpha(mAlpha);
206        }
207        if (mPropertiesToSet.contains(Properties.START_DELAY)) {
208            mViewPropertyAnimator.setStartDelay(mStartDelay);
209        }
210        if (mPropertiesToSet.contains(Properties.DURATION)) {
211            mViewPropertyAnimator.setDuration(mDuration);
212        }
213        if (mPropertiesToSet.contains(Properties.INTERPOLATOR)) {
214            mViewPropertyAnimator.setInterpolator(mInterpolator);
215        }
216        mViewPropertyAnimator.setListener(this);
217        mViewPropertyAnimator.start();
218        LauncherAnimUtils.cancelOnDestroyActivity(this);
219    }
220
221    public LauncherViewPropertyAnimator translationX(float value) {
222        mPropertiesToSet.add(Properties.TRANSLATION_X);
223        mTranslationX = value;
224        return this;
225    }
226
227    public LauncherViewPropertyAnimator translationY(float value) {
228        mPropertiesToSet.add(Properties.TRANSLATION_Y);
229        mTranslationY = value;
230        return this;
231    }
232
233    public LauncherViewPropertyAnimator scaleX(float value) {
234        mPropertiesToSet.add(Properties.SCALE_X);
235        mScaleX = value;
236        return this;
237    }
238
239    public LauncherViewPropertyAnimator scaleY(float value) {
240        mPropertiesToSet.add(Properties.SCALE_Y);
241        mScaleY = value;
242        return this;
243    }
244
245    public LauncherViewPropertyAnimator rotationY(float value) {
246        mPropertiesToSet.add(Properties.ROTATION_Y);
247        mRotationY = value;
248        return this;
249    }
250
251    public LauncherViewPropertyAnimator alpha(float value) {
252        mPropertiesToSet.add(Properties.ALPHA);
253        mAlpha = value;
254        return this;
255    }
256}
257