diff options
author | Jeff Brown <jeffbrown@google.com> | 2012-03-05 19:05:02 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-03-05 19:05:02 -0800 |
commit | 97a355a9a10ce762ec56a1d02e9c04e54c2f0902 (patch) | |
tree | 24d009108582c0365d2fe96aa182e623a24e855f | |
parent | 4b97257979034a8031040b84d8f016d8f3175313 (diff) | |
parent | 2b6cb9a27e6f11fb30c9b9baaa5fc02f29f4072e (diff) | |
download | frameworks_base-97a355a9a10ce762ec56a1d02e9c04e54c2f0902.zip frameworks_base-97a355a9a10ce762ec56a1d02e9c04e54c2f0902.tar.gz frameworks_base-97a355a9a10ce762ec56a1d02e9c04e54c2f0902.tar.bz2 |
Merge "Support posting delayed animation/draw callbacks."
-rw-r--r-- | core/java/android/view/Choreographer.java | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/core/java/android/view/Choreographer.java b/core/java/android/view/Choreographer.java index 42c3913..58c8f97 100644 --- a/core/java/android/view/Choreographer.java +++ b/core/java/android/view/Choreographer.java @@ -81,6 +81,8 @@ public final class Choreographer { private static final int MSG_DO_ANIMATION = 0; private static final int MSG_DO_DRAW = 1; private static final int MSG_DO_SCHEDULE_VSYNC = 2; + private static final int MSG_POST_DELAYED_ANIMATION = 3; + private static final int MSG_POST_DELAYED_DRAW = 4; private final Object mLock = new Object(); @@ -150,7 +152,7 @@ public final class Choreographer { } /** - * Posts a callback to run on the next animation cycle and schedules an animation cycle. + * Posts a callback to run on the next animation cycle. * The callback only runs once and then is automatically removed. * * @param runnable The callback to run during the next animation cycle. @@ -161,6 +163,10 @@ public final class Choreographer { if (runnable == null) { throw new IllegalArgumentException("runnable must not be null"); } + postAnimationCallbackUnchecked(runnable); + } + + private void postAnimationCallbackUnchecked(Runnable runnable) { synchronized (mLock) { mAnimationCallbacks = addCallbackLocked(mAnimationCallbacks, runnable); scheduleAnimationLocked(); @@ -168,6 +174,28 @@ public final class Choreographer { } /** + * Posts a callback to run on the next animation cycle following the specified delay. + * The callback only runs once and then is automatically removed. + * + * @param runnable The callback to run during the next animation cycle following + * the specified delay. + * @param delayMillis The delay time in milliseconds. + * + * @see #removeAnimationCallback + */ + public void postAnimationCallbackDelayed(Runnable runnable, long delayMillis) { + if (runnable == null) { + throw new IllegalArgumentException("runnable must not be null"); + } + if (delayMillis <= 0) { + postAnimationCallbackUnchecked(runnable); + } else { + Message msg = mHandler.obtainMessage(MSG_POST_DELAYED_ANIMATION, runnable); + mHandler.sendMessageDelayed(msg, delayMillis); + } + } + + /** * Removes an animation callback. * Does nothing if the specified animation callback has not been posted or has already * been removed. @@ -175,6 +203,7 @@ public final class Choreographer { * @param runnable The animation callback to remove. * * @see #postAnimationCallback + * @see #postAnimationCallbackDelayed */ public void removeAnimationCallback(Runnable runnable) { if (runnable == null) { @@ -183,10 +212,11 @@ public final class Choreographer { synchronized (mLock) { mAnimationCallbacks = removeCallbackLocked(mAnimationCallbacks, runnable); } + mHandler.removeMessages(MSG_POST_DELAYED_ANIMATION, runnable); } /** - * Posts a callback to run on the next draw cycle and schedules a draw cycle. + * Posts a callback to run on the next draw cycle. * The callback only runs once and then is automatically removed. * * @param runnable The callback to run during the next draw cycle. @@ -197,6 +227,10 @@ public final class Choreographer { if (runnable == null) { throw new IllegalArgumentException("runnable must not be null"); } + postDrawCallbackUnchecked(runnable); + } + + private void postDrawCallbackUnchecked(Runnable runnable) { synchronized (mLock) { mDrawCallbacks = addCallbackLocked(mDrawCallbacks, runnable); scheduleDrawLocked(); @@ -204,6 +238,28 @@ public final class Choreographer { } /** + * Posts a callback to run on the next draw cycle following the specified delay. + * The callback only runs once and then is automatically removed. + * + * @param runnable The callback to run during the next draw cycle following + * the specified delay. + * @param delayMillis The delay time in milliseconds. + * + * @see #removeDrawCallback + */ + public void postDrawCallbackDelayed(Runnable runnable, long delayMillis) { + if (runnable == null) { + throw new IllegalArgumentException("runnable must not be null"); + } + if (delayMillis <= 0) { + postDrawCallbackUnchecked(runnable); + } else { + Message msg = mHandler.obtainMessage(MSG_POST_DELAYED_DRAW, runnable); + mHandler.sendMessageDelayed(msg, delayMillis); + } + } + + /** * Removes a draw callback. * Does nothing if the specified draw callback has not been posted or has already * been removed. @@ -211,6 +267,7 @@ public final class Choreographer { * @param runnable The draw callback to remove. * * @see #postDrawCallback + * @see #postDrawCallbackDelayed */ public void removeDrawCallback(Runnable runnable) { if (runnable == null) { @@ -219,6 +276,7 @@ public final class Choreographer { synchronized (mLock) { mDrawCallbacks = removeCallbackLocked(mDrawCallbacks, runnable); } + mHandler.removeMessages(MSG_POST_DELAYED_DRAW, runnable); } private void scheduleAnimationLocked() { @@ -438,6 +496,12 @@ public final class Choreographer { case MSG_DO_SCHEDULE_VSYNC: doScheduleVsync(); break; + case MSG_POST_DELAYED_ANIMATION: + postAnimationCallbackUnchecked((Runnable)msg.obj); + break; + case MSG_POST_DELAYED_DRAW: + postDrawCallbackUnchecked((Runnable)msg.obj); + break; } } } |