Bug 701594 - Part 5: Factor out the fling timer setting functions into separate functions. r=kats
authorPatrick Walton <pwalton@mozilla.com>
Wed, 07 Dec 2011 13:07:24 -0800
changeset 83849 e5b56fcb36af789064d478e728df90d46a498dd8
parent 83848 31b6da96f5e03574f666b908727575f158fa4f9f
child 83850 331e25310bf790a5b5262c0aa59247c2ad1aa448
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskats
bugs701594
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 701594 - Part 5: Factor out the fling timer setting functions into separate functions. r=kats
mobile/android/base/ui/PanZoomController.java
--- a/mobile/android/base/ui/PanZoomController.java
+++ b/mobile/android/base/ui/PanZoomController.java
@@ -107,17 +107,18 @@ public class PanZoomController
         0.80794f,   /* 10 */
         0.86069f,   /* 11 */
         0.90651f,   /* 12 */
         0.94471f,   /* 13 */
         0.97401f,   /* 14 */
         0.99309f,   /* 15 */
     };
 
-    private Timer mFlingTimer;
+    /* The timer that handles flings or bounces. */
+    private Timer mAnimationTimer;
     /* Information about the X axis. */
     private AxisX mX;
     /* Information about the Y axis. */
     private AxisY mY;
     /* The zoom focus at the first zoom event (in page coordinates). */
     private PointF mLastZoomFocus;
     /* The time the last motion event took place. */
     private long mLastEventTime;
@@ -246,20 +247,17 @@ public class PanZoomController
 
     /*
      * Panning/scrolling
      */
 
     private boolean onTouchStart(MotionEvent event) {
         // user is taking control of movement, so stop
         // any auto-movement we have going
-        if (mFlingTimer != null) {
-            mFlingTimer.cancel();
-            mFlingTimer = null;
-        }
+        stopAnimationTimer();
         mOverridePanning = false;
 
         switch (mState) {
         case ANIMATED_ZOOM:
             return false;
         case FLING:
         case NOTHING:
             mState = PanZoomState.TOUCHING;
@@ -472,28 +470,46 @@ public class PanZoomController
         if (mState != PanZoomState.FLING)
             mX.velocity = mY.velocity = 0.0f;
 
         mX.disableSnap = mY.disableSnap = mOverridePanning;
 
         mX.displace(); mY.displace();
         updatePosition();
 
-        if (mFlingTimer != null)
-            mFlingTimer.cancel();
+        stopAnimationTimer();
 
         boolean stopped = stopped();
         mX.startFling(stopped); mY.startFling(stopped);
 
-        mFlingTimer = new Timer();
-        mFlingTimer.scheduleAtFixedRate(new TimerTask() {
-            public void run() { mController.post(new FlingRunnable()); }
+        startAnimationTimer(new FlingRunnable());
+    }
+
+    /* Starts the fling or bounce animation. */
+    private void startAnimationTimer(final Runnable runnable) {
+        if (mAnimationTimer != null) {
+            Log.e(LOGTAG, "Attempted to start a new fling without canceling the old one!");
+            stopAnimationTimer();
+        }
+
+        mAnimationTimer = new Timer();
+        mAnimationTimer.scheduleAtFixedRate(new TimerTask() {
+            @Override
+            public void run() { mController.post(runnable); }
         }, 0, 1000L/60L);
     }
 
+    /* Stops the fling or bounce animation. */
+    private void stopAnimationTimer() {
+        if (mAnimationTimer != null) {
+            mAnimationTimer.cancel();
+            mAnimationTimer = null;
+        }
+    }
+
     private boolean stopped() {
         float absVelocity = (float)Math.sqrt(mX.velocity * mX.velocity +
                                              mY.velocity * mY.velocity);
         return absVelocity < STOPPED_THRESHOLD;
     }
 
     private void updatePosition() {
         if (mOverridePanning) {
@@ -552,21 +568,17 @@ public class PanZoomController
             if (mX.getFlingState() == Axis.FlingStates.STOPPED &&
                     mY.getFlingState() == Axis.FlingStates.STOPPED) {
                 stop();
             }
         }
 
         private void stop() {
             mState = PanZoomState.NOTHING;
-
-            if (mFlingTimer != null) {
-                mFlingTimer.cancel();
-                mFlingTimer = null;
-            }
+            stopAnimationTimer();
 
             // Force a viewport synchronisation
             mController.setForceRedraw();
             mController.notifyLayerClientOfGeometryChange();
         }
     }
 
     private float computeElasticity(float excess, float viewportLength) {