Fix bug 5581874 - Animated drawables don't start as expected

Fix a bug that caused animated drawables to not schedule properly when
a view has not yet been attached. Also make ImageViews update their
drawable visibility state properly, which will handle scheduling
concerns as ImageViews are attached and detached from their windows.

This should also fix the bug where animated notification icons in the
status bar do not animate until the posting app posts an update to the
notification.

Change-Id: I24c403182831258d1f251736e920c9efe1d38299
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 62e6ebd..dc46d42 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -11402,8 +11402,12 @@
      *        {@link SystemClock#uptimeMillis} timebase.
      */
     public void scheduleDrawable(Drawable who, Runnable what, long when) {
-        if (verifyDrawable(who) && what != null && mAttachInfo != null) {
-            mAttachInfo.mHandler.postAtTime(what, who, when);
+        if (verifyDrawable(who) && what != null) {
+            if (mAttachInfo != null) {
+                mAttachInfo.mHandler.postAtTime(what, who, when);
+            } else {
+                ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
+            }
         }
     }
 
@@ -11414,8 +11418,12 @@
      * @param what the action to cancel
      */
     public void unscheduleDrawable(Drawable who, Runnable what) {
-        if (verifyDrawable(who) && what != null && mAttachInfo != null) {
-            mAttachInfo.mHandler.removeCallbacks(what, who);
+        if (verifyDrawable(who) && what != null) {
+            if (mAttachInfo != null) {
+                mAttachInfo.mHandler.removeCallbacks(what, who);
+            } else {
+                ViewRootImpl.getRunQueue().removeCallbacks(what);
+            }
         }
     }