From 37419d7321e71edb179faa0eafd2a2acf12b62c1 Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Thu, 10 Nov 2011 11:32:09 -0800 Subject: [PATCH] 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 --- core/java/android/view/View.java | 16 ++++++++++++---- core/java/android/widget/ImageView.java | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 62e6ebdd2754f..dc46d424450f6 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -11402,8 +11402,12 @@ public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Cal * {@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 @@ public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Cal * @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); + } } } diff --git a/core/java/android/widget/ImageView.java b/core/java/android/widget/ImageView.java index b24dd696c2b2b..1e2d0d19d712b 100644 --- a/core/java/android/widget/ImageView.java +++ b/core/java/android/widget/ImageView.java @@ -1035,4 +1035,28 @@ public class ImageView extends View { mDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8); } } + + @Override + public void setVisibility(int visibility) { + super.setVisibility(visibility); + if (mDrawable != null) { + mDrawable.setVisible(visibility == VISIBLE, false); + } + } + + @Override + public void onAttachedToWindow() { + super.onAttachedToWindow(); + if (mDrawable != null) { + mDrawable.setVisible(getVisibility() == VISIBLE, false); + } + } + + @Override + public void onDetachedFromWindow() { + super.onDetachedFromWindow(); + if (mDrawable != null) { + mDrawable.setVisible(false, false); + } + } }