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.AnimatorSet;
21import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
23import android.animation.ValueAnimator;
24
25import java.util.HashSet;
26
27public class LauncherAnimUtils {
28    static HashSet<Animator> sAnimators = new HashSet<Animator>();
29    static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
30        public void onAnimationStart(Animator animation) {
31        }
32
33        public void onAnimationRepeat(Animator animation) {
34        }
35
36        public void onAnimationEnd(Animator animation) {
37            sAnimators.remove(animation);
38        }
39
40        public void onAnimationCancel(Animator animation) {
41            sAnimators.remove(animation);
42        }
43    };
44
45    public static void cancelOnDestroyActivity(Animator a) {
46        sAnimators.add(a);
47        a.addListener(sEndAnimListener);
48    }
49
50    public static void onDestroyActivity() {
51        HashSet<Animator> animators = new HashSet<Animator>(sAnimators);
52        for (Animator a : animators) {
53            if (a.isRunning()) {
54                a.cancel();
55            } else {
56                sAnimators.remove(a);
57            }
58        }
59    }
60
61    public static AnimatorSet createAnimatorSet() {
62        AnimatorSet anim = new AnimatorSet();
63        cancelOnDestroyActivity(anim);
64        return anim;
65    }
66
67    public static ValueAnimator ofFloat(float... values) {
68        ValueAnimator anim = new ValueAnimator();
69        anim.setFloatValues(values);
70        cancelOnDestroyActivity(anim);
71        return anim;
72    }
73
74    public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
75        ObjectAnimator anim = new ObjectAnimator();
76        anim.setTarget(target);
77        anim.setPropertyName(propertyName);
78        anim.setFloatValues(values);
79        cancelOnDestroyActivity(anim);
80        return anim;
81    }
82
83    public static ObjectAnimator ofPropertyValuesHolder(Object target,
84            PropertyValuesHolder... values) {
85        ObjectAnimator anim = new ObjectAnimator();
86        anim.setTarget(target);
87        anim.setValues(values);
88        cancelOnDestroyActivity(anim);
89        return anim;
90    }
91}
92