Fix crash in draw listener

Bug: 8528246

Change-Id: Ie3600bed58dc393fcf71f735213a32b51551b52d
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 9e651c6..56a854c 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -1272,15 +1272,22 @@
                 // layers on all the workspace pages, so that transitioning to Launcher from other
                 // apps is nice and speedy.
                 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
+                    private boolean mStarted = false;
                     public void onDraw() {
+                        if (mStarted) return;
+                        mStarted = true;
                         // We delay the layer building a bit in order to give
                         // other message processing a time to run.  In particular
                         // this avoids a delay in hiding the IME if it was
                         // currently shown, because doing that may involve
                         // some communication back with the app.
                         mWorkspace.postDelayed(mBuildLayersRunnable, 500);
-
-                        observer.removeOnDrawListener(this);
+                        final ViewTreeObserver.OnDrawListener listener = this;
+                        mWorkspace.post(new Runnable() {
+                                public void run() {
+                                    mWorkspace.getViewTreeObserver().removeOnDrawListener(listener);
+                                }
+                            });
                         return;
                     }
                 });
diff --git a/src/com/android/launcher2/LauncherAnimUtils.java b/src/com/android/launcher2/LauncherAnimUtils.java
index 5055d35..a89cb46 100644
--- a/src/com/android/launcher2/LauncherAnimUtils.java
+++ b/src/com/android/launcher2/LauncherAnimUtils.java
@@ -52,15 +52,23 @@
     // Helper method. Assumes a draw is pending, and that if the animation's duration is 0
     // it should be cancelled
     public static void startAnimationAfterNextDraw(final Animator animator, final View view) {
-        final ViewTreeObserver observer = view.getViewTreeObserver();
-        observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
+        view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
+                private boolean mStarted = false;
                 public void onDraw() {
+                    if (mStarted) return;
+                    mStarted = true;
                     // Use this as a signal that the animation was cancelled
                     if (animator.getDuration() == 0) {
                         return;
                     }
                     animator.start();
-                    view.getViewTreeObserver().removeOnDrawListener(this);
+
+                    final ViewTreeObserver.OnDrawListener listener = this;
+                    view.post(new Runnable() {
+                            public void run() {
+                                view.getViewTreeObserver().removeOnDrawListener(listener);
+                            }
+                        });
                 }
             });
     }