OSDN Git Service

Prevent memory leaks coming from LauncherAnimUtils
authorMichael Jurka <mikejurka@google.com>
Wed, 23 Oct 2013 13:21:32 +0000 (15:21 +0200)
committerMichael Jurka <mikejurka@google.com>
Wed, 23 Oct 2013 13:22:17 +0000 (15:22 +0200)
Use weak references for global animation list

Bug: 11322014

Change-Id: I38bb9184e45840113129781fa83ab40ec96d3868

src/com/android/launcher3/LauncherAnimUtils.java

index 5d4f9c6..e6c220b 100644 (file)
@@ -25,12 +25,13 @@ import android.view.View;
 import android.view.ViewTreeObserver;
 
 import java.util.HashSet;
+import java.util.WeakHashMap;
 
 public class LauncherAnimUtils {
-    static HashSet<Animator> sAnimators = new HashSet<Animator>();
+    static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>();
     static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
         public void onAnimationStart(Animator animation) {
-            sAnimators.add(animation);
+            sAnimators.put(animation, null);
         }
 
         public void onAnimationRepeat(Animator animation) {
@@ -74,13 +75,12 @@ public class LauncherAnimUtils {
     }
 
     public static void onDestroyActivity() {
-        HashSet<Animator> animators = new HashSet<Animator>(sAnimators);
+        HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
         for (Animator a : animators) {
             if (a.isRunning()) {
                 a.cancel();
-            } else {
-                sAnimators.remove(a);
             }
+            sAnimators.remove(a);
         }
     }