2012-01-10 07:06:08 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-10 07:06:08 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko.ui;
|
|
|
|
|
2012-07-27 21:57:47 -07:00
|
|
|
import org.mozilla.gecko.util.FloatUtils;
|
2012-01-10 07:06:08 -08:00
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import org.json.JSONArray;
|
|
|
|
|
|
|
|
import android.util.Log;
|
2012-09-12 13:36:36 -07:00
|
|
|
import android.view.View;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
2012-01-10 07:06:08 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-04-17 08:33:42 -07:00
|
|
|
abstract class Axis {
|
2012-04-13 07:29:14 -07:00
|
|
|
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";
|
|
|
|
|
2012-01-10 07:06:08 -08:00
|
|
|
// This fraction of velocity remains after every animation frame when the velocity is low.
|
2012-04-13 07:29:14 -07:00
|
|
|
private static float FRICTION_SLOW;
|
2012-01-10 07:06:08 -08:00
|
|
|
// This fraction of velocity remains after every animation frame when the velocity is high.
|
2012-04-13 07:29:14 -07:00
|
|
|
private static float FRICTION_FAST;
|
2012-01-10 07:06:08 -08:00
|
|
|
// Below this velocity (in pixels per frame), the friction starts increasing from FRICTION_FAST
|
|
|
|
// to FRICTION_SLOW.
|
2012-04-13 07:29:14 -07:00
|
|
|
private static float VELOCITY_THRESHOLD;
|
2012-01-10 07:06:08 -08:00
|
|
|
// The maximum velocity change factor between events, per ms, in %.
|
|
|
|
// Direction changes are excluded.
|
2012-04-13 07:29:14 -07:00
|
|
|
private static float MAX_EVENT_ACCELERATION;
|
2012-01-10 07:06:08 -08:00
|
|
|
|
|
|
|
// The rate of deceleration when the surface has overscrolled.
|
2012-04-13 07:29:14 -07:00
|
|
|
private static float OVERSCROLL_DECEL_RATE;
|
2012-01-10 07:06:08 -08:00
|
|
|
// The percentage of the surface which can be overscrolled before it must snap back.
|
2012-04-13 07:29:14 -07:00
|
|
|
private static float SNAP_LIMIT;
|
2012-01-10 07:06:08 -08:00
|
|
|
|
|
|
|
// The minimum amount of space that must be present for an axis to be considered scrollable,
|
|
|
|
// in pixels.
|
2012-04-13 07:29:14 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-04-17 08:33:42 -07:00
|
|
|
static void addPrefNames(JSONArray prefs) {
|
2012-04-13 07:29:14 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-04-17 08:33:42 -07:00
|
|
|
static void setPrefs(Map<String, Integer> prefs) {
|
2012-04-13 07:29:14 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-01-10 07:06:08 -08:00
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum Overscroll {
|
|
|
|
NONE,
|
|
|
|
MINUS, // Overscrolled in the negative direction
|
|
|
|
PLUS, // Overscrolled in the positive direction
|
|
|
|
BOTH, // Overscrolled in both directions (page is zoomed to smaller than screen)
|
|
|
|
}
|
|
|
|
|
|
|
|
private final SubdocumentScrollHelper mSubscroller;
|
|
|
|
|
2012-09-12 13:36:36 -07:00
|
|
|
private int mOverscrollMode; /* Default to only overscrolling if we're allowed to scroll in a direction */
|
2012-01-10 07:06:08 -08:00
|
|
|
private float mFirstTouchPos; /* Position of the first touch event on the current drag. */
|
|
|
|
private float mTouchPos; /* Position of the most recent touch event on the current drag. */
|
|
|
|
private float mLastTouchPos; /* Position of the touch event before touchPos. */
|
|
|
|
private float mVelocity; /* Velocity in this direction; pixels per animation frame. */
|
2012-04-17 08:34:13 -07:00
|
|
|
private boolean mScrollingDisabled; /* Whether movement on this axis is locked. */
|
2012-01-10 07:06:08 -08:00
|
|
|
private boolean mDisableSnap; /* Whether overscroll snapping is disabled. */
|
|
|
|
private float mDisplacement;
|
|
|
|
|
|
|
|
private FlingStates mFlingState; /* The fling state we're in on this axis. */
|
|
|
|
|
|
|
|
protected abstract float getOrigin();
|
|
|
|
protected abstract float getViewportLength();
|
2012-05-23 07:49:52 -07:00
|
|
|
protected abstract float getPageStart();
|
2012-01-10 07:06:08 -08:00
|
|
|
protected abstract float getPageLength();
|
|
|
|
|
|
|
|
Axis(SubdocumentScrollHelper subscroller) {
|
|
|
|
mSubscroller = subscroller;
|
2012-09-12 13:36:36 -07:00
|
|
|
mOverscrollMode = View.OVER_SCROLL_IF_CONTENT_SCROLLS;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOverScrollMode(int overscrollMode) {
|
|
|
|
mOverscrollMode = overscrollMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getOverScrollMode() {
|
|
|
|
return mOverscrollMode;
|
2012-01-10 07:06:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private float getViewportEnd() {
|
|
|
|
return getOrigin() + getViewportLength();
|
|
|
|
}
|
|
|
|
|
2012-05-23 07:49:52 -07:00
|
|
|
private float getPageEnd() {
|
|
|
|
return getPageStart() + getPageLength();
|
|
|
|
}
|
|
|
|
|
2012-01-10 07:06:08 -08:00
|
|
|
void startTouch(float pos) {
|
|
|
|
mVelocity = 0.0f;
|
2012-02-14 10:55:22 -08:00
|
|
|
mScrollingDisabled = false;
|
2012-01-10 07:06:08 -08:00
|
|
|
mFirstTouchPos = mTouchPos = mLastTouchPos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
float panDistance(float currentPos) {
|
|
|
|
return currentPos - mFirstTouchPos;
|
|
|
|
}
|
|
|
|
|
2012-02-14 10:55:22 -08:00
|
|
|
void setScrollingDisabled(boolean disabled) {
|
|
|
|
mScrollingDisabled = disabled;
|
2012-01-10 07:06:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void saveTouchPos() {
|
|
|
|
mLastTouchPos = mTouchPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateWithTouchAt(float pos, float timeDelta) {
|
|
|
|
float newVelocity = (mTouchPos - pos) / timeDelta * MS_PER_FRAME;
|
|
|
|
|
|
|
|
// If there's a direction change, or current velocity is very low,
|
|
|
|
// allow setting of the velocity outright. Otherwise, use the current
|
|
|
|
// velocity and a maximum change factor to set the new velocity.
|
|
|
|
boolean curVelocityIsLow = Math.abs(mVelocity) < 1.0f;
|
|
|
|
boolean directionChange = (mVelocity > 0) != (newVelocity > 0);
|
|
|
|
if (curVelocityIsLow || (directionChange && !FloatUtils.fuzzyEquals(newVelocity, 0.0f))) {
|
|
|
|
mVelocity = newVelocity;
|
|
|
|
} else {
|
|
|
|
float maxChange = Math.abs(mVelocity * timeDelta * MAX_EVENT_ACCELERATION);
|
|
|
|
mVelocity = Math.min(mVelocity + maxChange, Math.max(mVelocity - maxChange, newVelocity));
|
|
|
|
}
|
|
|
|
|
|
|
|
mTouchPos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean overscrolled() {
|
|
|
|
return getOverscroll() != Overscroll.NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Overscroll getOverscroll() {
|
2012-05-23 07:49:52 -07:00
|
|
|
boolean minus = (getOrigin() < getPageStart());
|
|
|
|
boolean plus = (getViewportEnd() > getPageEnd());
|
2012-01-10 07:06:08 -08:00
|
|
|
if (minus && plus) {
|
|
|
|
return Overscroll.BOTH;
|
|
|
|
} else if (minus) {
|
|
|
|
return Overscroll.MINUS;
|
|
|
|
} else if (plus) {
|
|
|
|
return Overscroll.PLUS;
|
|
|
|
} else {
|
|
|
|
return Overscroll.NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the amount that the page has been overscrolled. If the page hasn't been
|
|
|
|
// overscrolled on this axis, returns 0.
|
|
|
|
private float getExcess() {
|
|
|
|
switch (getOverscroll()) {
|
2012-05-23 07:49:52 -07:00
|
|
|
case MINUS: return getPageStart() - getOrigin();
|
|
|
|
case PLUS: return getViewportEnd() - getPageEnd();
|
|
|
|
case BOTH: return (getViewportEnd() - getPageEnd()) + (getPageStart() - getOrigin());
|
2012-01-10 07:06:08 -08:00
|
|
|
default: return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-02-14 10:55:22 -08:00
|
|
|
* Returns true if the page is zoomed in to some degree along this axis such that scrolling is
|
|
|
|
* possible and this axis has not been scroll locked while panning. Otherwise, returns false.
|
2012-01-10 07:06:08 -08:00
|
|
|
*/
|
2012-09-17 07:55:58 -07:00
|
|
|
boolean scrollable() {
|
2012-05-02 10:18:36 -07:00
|
|
|
// If we're scrolling a subdocument, ignore the viewport length restrictions (since those
|
|
|
|
// apply to the top-level document) and only take into account axis locking.
|
|
|
|
if (mSubscroller.scrolling()) {
|
|
|
|
return !mScrollingDisabled;
|
|
|
|
}
|
2012-09-12 13:36:36 -07:00
|
|
|
|
|
|
|
// if we are axis locked, return false
|
|
|
|
if (mScrollingDisabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// there is scrollable space, and we're not disabled, or the document fits the viewport
|
|
|
|
// but we always allow overscroll anyway
|
|
|
|
return getViewportLength() <= getPageLength() - MIN_SCROLLABLE_DISTANCE ||
|
|
|
|
getOverScrollMode() == View.OVER_SCROLL_ALWAYS;
|
2012-01-10 07:06:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the resistance, as a multiplier, that should be taken into account when
|
|
|
|
* tracking or pinching.
|
|
|
|
*/
|
2012-08-11 10:32:04 -07:00
|
|
|
float getEdgeResistance(boolean forPinching) {
|
2012-01-10 07:06:08 -08:00
|
|
|
float excess = getExcess();
|
2012-08-11 10:32:04 -07:00
|
|
|
if (excess > 0.0f && (getOverscroll() == Overscroll.BOTH || !forPinching)) {
|
2012-01-30 21:42:40 -08:00
|
|
|
// excess can be greater than viewport length, but the resistance
|
|
|
|
// must never drop below 0.0
|
|
|
|
return Math.max(0.0f, SNAP_LIMIT - excess / getViewportLength());
|
|
|
|
}
|
|
|
|
return 1.0f;
|
2012-01-10 07:06:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the velocity. If the axis is locked, returns 0. */
|
|
|
|
float getRealVelocity() {
|
2012-02-14 10:55:22 -08:00
|
|
|
return scrollable() ? mVelocity : 0f;
|
2012-01-10 07:06:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void startPan() {
|
|
|
|
mFlingState = FlingStates.PANNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
void startFling(boolean stopped) {
|
|
|
|
mDisableSnap = mSubscroller.scrolling();
|
|
|
|
|
|
|
|
if (stopped) {
|
|
|
|
mFlingState = FlingStates.STOPPED;
|
|
|
|
} else {
|
|
|
|
mFlingState = FlingStates.FLINGING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advances a fling animation by one step. */
|
|
|
|
boolean advanceFling() {
|
|
|
|
if (mFlingState != FlingStates.FLINGING) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-21 09:44:44 -08:00
|
|
|
if (mSubscroller.scrolling() && !mSubscroller.lastScrollSucceeded()) {
|
|
|
|
// if the subdocument stopped scrolling, it's because it reached the end
|
|
|
|
// of the subdocument. we don't do overscroll on subdocuments, so there's
|
|
|
|
// no point in continuing this fling.
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-10 07:06:08 -08:00
|
|
|
|
|
|
|
float excess = getExcess();
|
2012-06-10 16:44:50 -07:00
|
|
|
Overscroll overscroll = getOverscroll();
|
|
|
|
boolean decreasingOverscroll = false;
|
|
|
|
if ((overscroll == Overscroll.MINUS && mVelocity > 0) ||
|
|
|
|
(overscroll == Overscroll.PLUS && mVelocity < 0))
|
|
|
|
{
|
|
|
|
decreasingOverscroll = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDisableSnap || FloatUtils.fuzzyEquals(excess, 0.0f) || decreasingOverscroll) {
|
2012-01-10 07:06:08 -08:00
|
|
|
// If we aren't overscrolled, just apply friction.
|
|
|
|
if (Math.abs(mVelocity) >= VELOCITY_THRESHOLD) {
|
|
|
|
mVelocity *= FRICTION_FAST;
|
|
|
|
} else {
|
|
|
|
float t = mVelocity / VELOCITY_THRESHOLD;
|
|
|
|
mVelocity *= FloatUtils.interpolate(FRICTION_SLOW, FRICTION_FAST, t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, decrease the velocity linearly.
|
|
|
|
float elasticity = 1.0f - excess / (getViewportLength() * SNAP_LIMIT);
|
2012-06-10 16:44:50 -07:00
|
|
|
if (overscroll == Overscroll.MINUS) {
|
2012-01-10 07:06:08 -08:00
|
|
|
mVelocity = Math.min((mVelocity + OVERSCROLL_DECEL_RATE) * elasticity, 0.0f);
|
|
|
|
} else { // must be Overscroll.PLUS
|
|
|
|
mVelocity = Math.max((mVelocity - OVERSCROLL_DECEL_RATE) * elasticity, 0.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopFling() {
|
|
|
|
mVelocity = 0.0f;
|
|
|
|
mFlingState = FlingStates.STOPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Performs displacement of the viewport position according to the current velocity.
|
|
|
|
void displace() {
|
2012-09-12 13:36:36 -07:00
|
|
|
// if this isn't scrollable just return
|
|
|
|
if (!scrollable())
|
2012-01-10 07:06:08 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (mFlingState == FlingStates.PANNING)
|
2012-08-11 10:32:04 -07:00
|
|
|
mDisplacement += (mLastTouchPos - mTouchPos) * getEdgeResistance(false);
|
2012-01-10 07:06:08 -08:00
|
|
|
else
|
|
|
|
mDisplacement += mVelocity;
|
2012-09-12 13:36:36 -07:00
|
|
|
|
|
|
|
// if overscroll is disabled and we're trying to overscroll, reset the displacement
|
|
|
|
// to remove any excess. Using getExcess alone isn't enough here since it relies on
|
|
|
|
// getOverscroll which doesn't take into account any new displacment being applied
|
|
|
|
if (getOverScrollMode() == View.OVER_SCROLL_NEVER) {
|
|
|
|
if (mDisplacement + getOrigin() < getPageStart()) {
|
|
|
|
mDisplacement = getPageStart() - getOrigin();
|
|
|
|
stopFling();
|
|
|
|
} else if (mDisplacement + getViewportEnd() > getPageEnd()) {
|
|
|
|
mDisplacement = getPageEnd() - getViewportEnd();
|
|
|
|
stopFling();
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 07:06:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
float resetDisplacement() {
|
|
|
|
float d = mDisplacement;
|
|
|
|
mDisplacement = 0.0f;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|