Bug 742115 - Make the java panning constants preffable; original patch stolen from :joe. r=Cwiiis
authorKartikaya Gupta <kgupta@mozilla.com>
Fri, 13 Apr 2012 10:29:14 -0400
changeset 91612 84351dbcd62d12769fb601e11cc098849faee073
parent 91611 17e8b7678990045f4eea5e80fdfbb8b0c54c2060
child 91613 4cfb132c7c16f6b76b9397091a7f54fc5e62d82a
push id22465
push usermak77@bonardo.net
push dateSat, 14 Apr 2012 11:58:29 +0000
treeherdermozilla-central@6880c195054f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersCwiiis
bugs742115
milestone14.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 742115 - Make the java panning constants preffable; original patch stolen from :joe. r=Cwiiis
mobile/android/app/mobile.js
mobile/android/base/gfx/GeckoLayerClient.java
mobile/android/base/ui/Axis.java
--- a/mobile/android/app/mobile.js
+++ b/mobile/android/app/mobile.js
@@ -719,8 +719,26 @@ pref("full-screen-api.enabled", true);
 pref("direct-texture.force.enabled", false);
 pref("direct-texture.force.disabled", false);
 
 // show checkerboard pattern on android; we use background colour instead
 pref("gfx.show_checkerboard_pattern", false);
 
 pref("remote-debugger.enabled", false);
 pref("remote-debugger.port", 6000);
+
+// This fraction in 1000ths of velocity remains after every animation frame when the velocity is low.
+pref("ui.scrolling.friction_slow", -1);
+// This fraction in 1000ths of velocity remains after every animation frame when the velocity is high.
+pref("ui.scrolling.friction_fast", -1);
+// Below this velocity (in pixels per frame), the friction starts increasing from friction_fast
+// to friction_slow.
+pref("ui.scrolling.velocity_threshold", -1);
+// The maximum velocity change factor between events, per ms, in 1000ths.
+// Direction changes are excluded.
+pref("ui.scrolling.max_event_acceleration", -1);
+// The rate of deceleration when the surface has overscrolled, in 1000ths.
+pref("ui.scrolling.overscroll_decel_rate", -1);
+// The fraction of the surface which can be overscrolled before it must snap back, in 1000ths.
+pref("ui.scrolling.overscroll_snap_limit", -1);
+// The minimum amount of space that must be present for an axis to be considered scrollable,
+// in 1/1000ths of pixels.
+pref("ui.scrolling.min_scrollable_distance", -1);
--- a/mobile/android/base/gfx/GeckoLayerClient.java
+++ b/mobile/android/base/gfx/GeckoLayerClient.java
@@ -38,16 +38,17 @@
 
 package org.mozilla.gecko.gfx;
 
 import org.mozilla.gecko.FloatUtils;
 import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.GeckoEventResponder;
+import org.mozilla.gecko.ui.Axis;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Point;
 import android.graphics.PointF;
 import android.graphics.Rect;
@@ -118,16 +119,17 @@ public class GeckoLayerClient implements
         view.setListener(this);
         view.setLayerRenderer(mLayerRenderer);
         layerController.setRoot(mRootLayer);
 
         sendResizeEventIfNecessary(true);
 
         JSONArray prefs = new JSONArray();
         DisplayPortCalculator.addPrefNames(prefs);
+        Axis.addPrefNames(prefs);
         GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Preferences:Get", prefs.toString()));
     }
 
     DisplayPortMetrics getDisplayPort() {
         return mDisplayPort;
     }
 
     /* Informs Gecko that the screen size has changed. */
@@ -256,17 +258,21 @@ public class GeckoLayerClient implements
                 Log.i(LOGTAG, "Showing checks: " + showChecks);
             } else if ("Preferences:Data".equals(event)) {
                 JSONArray jsonPrefs = message.getJSONArray("preferences");
                 Map<String, Integer> prefValues = new HashMap<String, Integer>();
                 for (int i = jsonPrefs.length() - 1; i >= 0; i--) {
                     JSONObject pref = jsonPrefs.getJSONObject(i);
                     prefValues.put(pref.getString("name"), pref.getInt("value"));
                 }
+                // check return value from setStrategy to make sure that this is the
+                // right batch of prefs, since other java code may also have sent requests
+                // for prefs.
                 if (DisplayPortCalculator.setStrategy(prefValues)) {
+                    Axis.setPrefs(prefValues);
                     GeckoAppShell.unregisterGeckoEventListener("Preferences:Data", this);
                 }
             }
         } catch (JSONException e) {
             Log.e(LOGTAG, "Error decoding JSON in " + event + " handler", e);
         }
     }
 
--- a/mobile/android/base/ui/Axis.java
+++ b/mobile/android/base/ui/Axis.java
@@ -33,44 +33,95 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko.ui;
 
+import java.util.Map;
+import android.util.Log;
+import org.json.JSONArray;
 import org.mozilla.gecko.FloatUtils;
 
 /**
  * This class represents the physics for one axis of movement (i.e. either
  * horizontal or vertical). It tracks the different properties of movement
  * like displacement, velocity, viewport dimensions, etc. pertaining to
  * a particular axis.
  */
-abstract class Axis {
+public abstract class Axis {
+    private static final String LOGTAG = "GeckoAxis";
+
+    private static final String PREF_SCROLLING_FRICTION_SLOW = "ui.scrolling.friction_slow";
+    private static final String PREF_SCROLLING_FRICTION_FAST = "ui.scrolling.friction_fast";
+    private static final String PREF_SCROLLING_VELOCITY_THRESHOLD = "ui.scrolling.velocity_threshold";
+    private static final String PREF_SCROLLING_MAX_EVENT_ACCELERATION = "ui.scrolling.max_event_acceleration";
+    private static final String PREF_SCROLLING_OVERSCROLL_DECEL_RATE = "ui.scrolling.overscroll_decel_rate";
+    private static final String PREF_SCROLLING_OVERSCROLL_SNAP_LIMIT = "ui.scrolling.overscroll_snap_limit";
+    private static final String PREF_SCROLLING_MIN_SCROLLABLE_DISTANCE = "ui.scrolling.min_scrollable_distance";
+
     // This fraction of velocity remains after every animation frame when the velocity is low.
-    private static final float FRICTION_SLOW = 0.85f;
+    private static float FRICTION_SLOW;
     // This fraction of velocity remains after every animation frame when the velocity is high.
-    private static final float FRICTION_FAST = 0.97f;
+    private static float FRICTION_FAST;
     // Below this velocity (in pixels per frame), the friction starts increasing from FRICTION_FAST
     // to FRICTION_SLOW.
-    private static final float VELOCITY_THRESHOLD = 10.0f;
+    private static float VELOCITY_THRESHOLD;
     // The maximum velocity change factor between events, per ms, in %.
     // Direction changes are excluded.
-    private static final float MAX_EVENT_ACCELERATION = 0.012f;
+    private static float MAX_EVENT_ACCELERATION;
 
     // The rate of deceleration when the surface has overscrolled.
-    private static final float OVERSCROLL_DECEL_RATE = 0.04f;
+    private static float OVERSCROLL_DECEL_RATE;
     // The percentage of the surface which can be overscrolled before it must snap back.
-    private static final float SNAP_LIMIT = 0.3f;
+    private static float SNAP_LIMIT;
 
     // The minimum amount of space that must be present for an axis to be considered scrollable,
     // in pixels.
-    private static final float MIN_SCROLLABLE_DISTANCE = 0.5f;
+    private static float MIN_SCROLLABLE_DISTANCE;
+
+    private static float getFloatPref(Map<String, Integer> prefs, String prefName, int defaultValue) {
+        Integer value = (prefs == null ? null : prefs.get(prefName));
+        return (float)(value == null || value < 0 ? defaultValue : value) / 1000f;
+    }
+
+    private static int getIntPref(Map<String, Integer> prefs, String prefName, int defaultValue) {
+        Integer value = (prefs == null ? null : prefs.get(prefName));
+        return (value == null || value < 0 ? defaultValue : value);
+    }
+
+    public static void addPrefNames(JSONArray prefs) {
+        prefs.put(PREF_SCROLLING_FRICTION_FAST);
+        prefs.put(PREF_SCROLLING_FRICTION_SLOW);
+        prefs.put(PREF_SCROLLING_VELOCITY_THRESHOLD);
+        prefs.put(PREF_SCROLLING_MAX_EVENT_ACCELERATION);
+        prefs.put(PREF_SCROLLING_OVERSCROLL_DECEL_RATE);
+        prefs.put(PREF_SCROLLING_OVERSCROLL_SNAP_LIMIT);
+        prefs.put(PREF_SCROLLING_MIN_SCROLLABLE_DISTANCE);
+    }
+
+    public static void setPrefs(Map<String, Integer> prefs) {
+        FRICTION_SLOW = getFloatPref(prefs, PREF_SCROLLING_FRICTION_SLOW, 850);
+        FRICTION_FAST = getFloatPref(prefs, PREF_SCROLLING_FRICTION_FAST, 970);
+        VELOCITY_THRESHOLD = getIntPref(prefs, PREF_SCROLLING_VELOCITY_THRESHOLD, 10);
+        MAX_EVENT_ACCELERATION = getFloatPref(prefs, PREF_SCROLLING_MAX_EVENT_ACCELERATION, 12);
+        OVERSCROLL_DECEL_RATE = getFloatPref(prefs, PREF_SCROLLING_OVERSCROLL_DECEL_RATE, 40);
+        SNAP_LIMIT = getFloatPref(prefs, PREF_SCROLLING_OVERSCROLL_SNAP_LIMIT, 300);
+        MIN_SCROLLABLE_DISTANCE = getFloatPref(prefs, PREF_SCROLLING_MIN_SCROLLABLE_DISTANCE, 500);
+        Log.i(LOGTAG, "Prefs: " + FRICTION_SLOW + "," + FRICTION_FAST + "," + VELOCITY_THRESHOLD + ","
+                + MAX_EVENT_ACCELERATION + "," + OVERSCROLL_DECEL_RATE + "," + SNAP_LIMIT + "," + MIN_SCROLLABLE_DISTANCE);
+    }
+
+    static {
+        // set the scrolling parameters to default values on startup
+        setPrefs(null);
+    }
+
     // The number of milliseconds per frame assuming 60 fps
     private static final float MS_PER_FRAME = 1000.0f / 60.0f;
 
     private enum FlingStates {
         STOPPED,
         PANNING,
         FLINGING,
     }