Bug 715164 - Guard against another race condition in PZC. r=pcwalton

This commit is contained in:
Kartikaya Gupta 2012-01-05 18:39:45 -08:00
parent 9b3324ba9e
commit 340cefddfb

View File

@ -125,6 +125,8 @@ public class PanZoomController
/* The timer that handles flings or bounces. */
private Timer mAnimationTimer;
/* The runnable being scheduled by the animation timer. */
private AnimationRunnable mAnimationRunnable;
/* Information about the X axis. */
private AxisX mX;
/* Information about the Y axis. */
@ -531,13 +533,14 @@ public class PanZoomController
}
/* Starts the fling or bounce animation. */
private void startAnimationTimer(final Runnable runnable) {
private void startAnimationTimer(final AnimationRunnable runnable) {
if (mAnimationTimer != null) {
Log.e(LOGTAG, "Attempted to start a new fling without canceling the old one!");
stopAnimationTimer();
}
mAnimationTimer = new Timer("Animation Timer");
mAnimationRunnable = runnable;
mAnimationTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() { mController.post(runnable); }
@ -550,6 +553,10 @@ public class PanZoomController
mAnimationTimer.cancel();
mAnimationTimer = null;
}
if (mAnimationRunnable != null) {
mAnimationRunnable.terminate();
mAnimationRunnable = null;
}
}
private boolean stopped() {
@ -587,8 +594,33 @@ public class PanZoomController
mX.displacement = mY.displacement = 0;
}
private abstract class AnimationRunnable implements Runnable {
private boolean mAnimationTerminated;
/* This should always run on the UI thread */
public final void run() {
/*
* Since the animation timer queues this runnable on the UI thread, it
* is possible that even when the animation timer is cancelled, there
* are multiple instances of this queued, so we need to have another
* mechanism to abort. This is done by using the mAnimationTerminated flag.
*/
if (mAnimationTerminated) {
return;
}
animateFrame();
}
protected abstract void animateFrame();
/* This should always run on the UI thread */
protected final void terminate() {
mAnimationTerminated = true;
}
}
/* The callback that performs the bounce animation. */
private class BounceRunnable implements Runnable {
private class BounceRunnable extends AnimationRunnable {
/* The current frame of the bounce-back animation */
private int mBounceFrame;
/*
@ -603,7 +635,7 @@ public class PanZoomController
mBounceEndMetrics = endMetrics;
}
public void run() {
protected void animateFrame() {
/*
* The pan/zoom controller might have signaled to us that it wants to abort the
* animation by setting the state to PanZoomState.NOTHING. Handle this case and bail
@ -648,8 +680,8 @@ public class PanZoomController
}
// The callback that performs the fling animation.
private class FlingRunnable implements Runnable {
public void run() {
private class FlingRunnable extends AnimationRunnable {
protected void animateFrame() {
/*
* The pan/zoom controller might have signaled to us that it wants to abort the
* animation by setting the state to PanZoomState.NOTHING. Handle this case and bail