2011-11-18 10:28:17 -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/. */
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko.ui;
|
|
|
|
|
2012-04-17 08:33:19 -07:00
|
|
|
import org.json.JSONArray;
|
2011-12-06 07:13:14 -08:00
|
|
|
import org.json.JSONException;
|
2012-04-17 08:33:19 -07:00
|
|
|
import org.json.JSONObject;
|
2012-07-15 13:20:43 -07:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
import org.mozilla.gecko.gfx.LayerController;
|
2011-11-15 13:41:19 -08:00
|
|
|
import org.mozilla.gecko.gfx.PointUtils;
|
2011-12-07 10:44:36 -08:00
|
|
|
import org.mozilla.gecko.gfx.ViewportMetrics;
|
2011-11-23 11:07:47 -08:00
|
|
|
import org.mozilla.gecko.FloatUtils;
|
2011-11-18 10:28:17 -08:00
|
|
|
import org.mozilla.gecko.GeckoApp;
|
|
|
|
import org.mozilla.gecko.GeckoAppShell;
|
|
|
|
import org.mozilla.gecko.GeckoEvent;
|
2011-11-15 13:41:19 -08:00
|
|
|
import org.mozilla.gecko.GeckoEventListener;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.graphics.PointF;
|
|
|
|
import android.graphics.RectF;
|
2012-01-10 07:05:51 -08:00
|
|
|
import android.util.FloatMath;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.GestureDetector;
|
|
|
|
import android.view.MotionEvent;
|
2012-04-17 08:33:19 -07:00
|
|
|
import java.util.Arrays;
|
2012-04-17 08:33:42 -07:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.HashMap;
|
2012-04-17 08:33:19 -07:00
|
|
|
import java.util.StringTokenizer;
|
2011-11-18 10:28:17 -08:00
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handles the kinetic scrolling and zooming physics for a layer controller.
|
|
|
|
*
|
|
|
|
* Many ideas are from Joe Hewitt's Scrollability:
|
|
|
|
* https://github.com/joehewitt/scrollability/
|
|
|
|
*/
|
2012-07-15 13:20:43 -07:00
|
|
|
public final class PanZoomController
|
2011-11-18 10:28:17 -08:00
|
|
|
extends GestureDetector.SimpleOnGestureListener
|
2012-01-23 19:18:24 -08:00
|
|
|
implements SimpleScaleGestureDetector.SimpleScaleGestureListener, GeckoEventListener
|
2011-11-18 10:28:17 -08:00
|
|
|
{
|
|
|
|
private static final String LOGTAG = "GeckoPanZoomController";
|
|
|
|
|
2012-01-10 07:06:10 -08:00
|
|
|
private static String MESSAGE_ZOOM_RECT = "Browser:ZoomToRect";
|
|
|
|
private static String MESSAGE_ZOOM_PAGE = "Browser:ZoomToPageWidth";
|
2012-04-17 08:33:19 -07:00
|
|
|
private static String MESSAGE_PREFS_GET = "Preferences:Get";
|
|
|
|
private static String MESSAGE_PREFS_DATA = "Preferences:Data";
|
|
|
|
|
|
|
|
private static final String PREF_ZOOM_ANIMATION_FRAMES = "ui.zooming.animation_frames";
|
2012-01-10 07:06:10 -08:00
|
|
|
|
2011-12-15 12:04:49 -08:00
|
|
|
// Animation stops if the velocity is below this value when overscrolled or panning.
|
2011-11-18 10:28:17 -08:00
|
|
|
private static final float STOPPED_THRESHOLD = 4.0f;
|
2012-02-14 11:43:45 -08:00
|
|
|
|
2011-12-15 12:04:49 -08:00
|
|
|
// Animation stops is the velocity is below this threshold when flinging.
|
|
|
|
private static final float FLING_STOPPED_THRESHOLD = 0.1f;
|
2012-02-14 11:43:45 -08:00
|
|
|
|
|
|
|
// The distance the user has to pan before we recognize it as such (e.g. to avoid 1-pixel pans
|
|
|
|
// between the touch-down and touch-up of a click). In units of density-independent pixels.
|
|
|
|
public static final float PAN_THRESHOLD = 1/16f * GeckoAppShell.getDpi();
|
|
|
|
|
2011-11-18 13:08:17 -08:00
|
|
|
// Angle from axis within which we stay axis-locked
|
2011-11-22 22:01:45 -08:00
|
|
|
private static final double AXIS_LOCK_ANGLE = Math.PI / 6.0; // 30 degrees
|
2012-02-14 11:43:45 -08:00
|
|
|
|
2011-12-09 09:03:19 -08:00
|
|
|
// The maximum amount we allow you to zoom into a page
|
|
|
|
private static final float MAX_ZOOM = 4.0f;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-07 10:41:58 -08:00
|
|
|
/* 16 precomputed frames of the _ease-out_ animation from the CSS Transitions specification. */
|
2012-04-17 08:33:19 -07:00
|
|
|
private static float[] ZOOM_ANIMATION_FRAMES = new float[] {
|
2011-12-07 10:41:58 -08:00
|
|
|
0.00000f, /* 0 */
|
|
|
|
0.10211f, /* 1 */
|
|
|
|
0.19864f, /* 2 */
|
|
|
|
0.29043f, /* 3 */
|
|
|
|
0.37816f, /* 4 */
|
|
|
|
0.46155f, /* 5 */
|
|
|
|
0.54054f, /* 6 */
|
|
|
|
0.61496f, /* 7 */
|
|
|
|
0.68467f, /* 8 */
|
|
|
|
0.74910f, /* 9 */
|
|
|
|
0.80794f, /* 10 */
|
|
|
|
0.86069f, /* 11 */
|
|
|
|
0.90651f, /* 12 */
|
|
|
|
0.94471f, /* 13 */
|
|
|
|
0.97401f, /* 14 */
|
|
|
|
0.99309f, /* 15 */
|
|
|
|
};
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
private enum PanZoomState {
|
|
|
|
NOTHING, /* no touch-start events received */
|
|
|
|
FLING, /* all touches removed, but we're still scrolling page */
|
|
|
|
TOUCHING, /* one touch-start event received */
|
2011-11-18 13:08:17 -08:00
|
|
|
PANNING_LOCKED, /* touch-start followed by move (i.e. panning with axis lock) */
|
|
|
|
PANNING, /* panning without axis lock */
|
|
|
|
PANNING_HOLD, /* in panning, but not moving.
|
2011-11-18 10:28:17 -08:00
|
|
|
* similar to TOUCHING but after starting a pan */
|
2011-11-18 13:08:17 -08:00
|
|
|
PANNING_HOLD_LOCKED, /* like PANNING_HOLD, but axis lock still in effect */
|
2011-11-18 10:28:17 -08:00
|
|
|
PINCHING, /* nth touch-start, where n > 1. this mode allows pan and zoom */
|
2012-03-13 09:14:28 -07:00
|
|
|
ANIMATED_ZOOM, /* animated zoom to a new rect */
|
2012-05-15 10:22:26 -07:00
|
|
|
BOUNCE, /* in a bounce animation */
|
|
|
|
|
|
|
|
WAITING_LISTENERS, /* a state halfway between NOTHING and TOUCHING - the user has
|
|
|
|
put a finger down, but we don't yet know if a touch listener has
|
|
|
|
prevented the default actions yet. we still need to abort animations. */
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-01-10 07:05:46 -08:00
|
|
|
private final LayerController mController;
|
2012-01-10 07:06:05 -08:00
|
|
|
private final SubdocumentScrollHelper mSubscroller;
|
2012-01-10 07:05:46 -08:00
|
|
|
private final Axis mX;
|
|
|
|
private final Axis mY;
|
|
|
|
|
2012-01-11 14:08:14 -08:00
|
|
|
private Thread mMainThread;
|
|
|
|
|
2012-01-10 07:05:46 -08:00
|
|
|
/* The timer that handles flings or bounces. */
|
|
|
|
private Timer mAnimationTimer;
|
|
|
|
/* The runnable being scheduled by the animation timer. */
|
|
|
|
private AnimationRunnable mAnimationRunnable;
|
|
|
|
/* 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;
|
|
|
|
/* Current state the pan/zoom UI is in. */
|
2011-11-18 10:28:17 -08:00
|
|
|
private PanZoomState mState;
|
|
|
|
|
|
|
|
public PanZoomController(LayerController controller) {
|
|
|
|
mController = controller;
|
2012-01-10 07:06:05 -08:00
|
|
|
mSubscroller = new SubdocumentScrollHelper(this);
|
|
|
|
mX = new AxisX(mSubscroller);
|
|
|
|
mY = new AxisY(mSubscroller);
|
|
|
|
|
2012-01-11 14:08:14 -08:00
|
|
|
mMainThread = GeckoApp.mAppContext.getMainLooper().getThread();
|
|
|
|
checkMainThread();
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
mState = PanZoomState.NOTHING;
|
|
|
|
|
2012-01-10 07:06:10 -08:00
|
|
|
GeckoAppShell.registerGeckoEventListener(MESSAGE_ZOOM_RECT, this);
|
|
|
|
GeckoAppShell.registerGeckoEventListener(MESSAGE_ZOOM_PAGE, this);
|
2012-04-17 08:33:19 -07:00
|
|
|
GeckoAppShell.registerGeckoEventListener(MESSAGE_PREFS_DATA, this);
|
|
|
|
|
|
|
|
JSONArray prefs = new JSONArray();
|
|
|
|
prefs.put(PREF_ZOOM_ANIMATION_FRAMES);
|
2012-04-17 08:33:42 -07:00
|
|
|
Axis.addPrefNames(prefs);
|
2012-04-17 08:33:19 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(MESSAGE_PREFS_GET, prefs.toString()));
|
2011-11-15 13:41:19 -08:00
|
|
|
}
|
|
|
|
|
2012-07-13 07:17:03 -07:00
|
|
|
public void destroy() {
|
|
|
|
GeckoAppShell.unregisterGeckoEventListener(MESSAGE_ZOOM_RECT, this);
|
|
|
|
GeckoAppShell.unregisterGeckoEventListener(MESSAGE_ZOOM_PAGE, this);
|
|
|
|
GeckoAppShell.unregisterGeckoEventListener(MESSAGE_PREFS_DATA, this);
|
|
|
|
mSubscroller.destroy();
|
|
|
|
}
|
|
|
|
|
2012-01-11 14:08:14 -08:00
|
|
|
// for debugging bug 713011; it can be taken out once that is resolved.
|
|
|
|
private void checkMainThread() {
|
|
|
|
if (mMainThread != Thread.currentThread()) {
|
|
|
|
// log with full stack trace
|
|
|
|
Log.e(LOGTAG, "Uh-oh, we're running on the wrong thread!", new Exception());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-15 13:41:19 -08:00
|
|
|
public void handleMessage(String event, JSONObject message) {
|
|
|
|
try {
|
2012-01-10 07:06:10 -08:00
|
|
|
if (MESSAGE_ZOOM_RECT.equals(event)) {
|
2012-01-10 07:05:46 -08:00
|
|
|
float x = (float)message.getDouble("x");
|
|
|
|
float y = (float)message.getDouble("y");
|
|
|
|
final RectF zoomRect = new RectF(x, y,
|
|
|
|
x + (float)message.getDouble("w"),
|
|
|
|
y + (float)message.getDouble("h"));
|
|
|
|
mController.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
animatedZoomTo(zoomRect);
|
|
|
|
}
|
|
|
|
});
|
2012-01-10 07:06:10 -08:00
|
|
|
} else if (MESSAGE_ZOOM_PAGE.equals(event)) {
|
2012-05-23 07:49:52 -07:00
|
|
|
RectF cssPageRect = mController.getCssPageRect();
|
2012-01-10 07:05:46 -08:00
|
|
|
|
2012-04-14 10:18:10 -07:00
|
|
|
RectF viewableRect = mController.getCssViewport();
|
2012-01-10 07:05:46 -08:00
|
|
|
float y = viewableRect.top;
|
|
|
|
// attempt to keep zoom keep focused on the center of the viewport
|
2012-05-23 07:49:52 -07:00
|
|
|
float newHeight = viewableRect.height() * cssPageRect.width() / viewableRect.width();
|
2012-02-02 06:11:50 -08:00
|
|
|
float dh = viewableRect.height() - newHeight; // increase in the height
|
2012-01-10 07:05:46 -08:00
|
|
|
final RectF r = new RectF(0.0f,
|
|
|
|
y + dh/2,
|
2012-05-23 07:49:52 -07:00
|
|
|
cssPageRect.width(),
|
2012-02-02 06:11:50 -08:00
|
|
|
y + dh/2 + newHeight);
|
2012-01-10 07:05:46 -08:00
|
|
|
mController.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
animatedZoomTo(r);
|
|
|
|
}
|
|
|
|
});
|
2012-04-17 08:33:19 -07:00
|
|
|
} else if (MESSAGE_PREFS_DATA.equals(event)) {
|
|
|
|
JSONArray jsonPrefs = message.getJSONArray("preferences");
|
2012-04-17 08:33:42 -07:00
|
|
|
Map<String, Integer> axisPrefs = new HashMap<String, Integer>();
|
|
|
|
String zoomAnimationFrames = null;
|
2012-04-17 08:33:19 -07:00
|
|
|
for (int i = jsonPrefs.length() - 1; i >= 0; i--) {
|
|
|
|
JSONObject pref = jsonPrefs.getJSONObject(i);
|
|
|
|
String name = pref.getString("name");
|
|
|
|
if (PREF_ZOOM_ANIMATION_FRAMES.equals(name)) {
|
2012-04-17 08:33:42 -07:00
|
|
|
zoomAnimationFrames = pref.getString("value");
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
axisPrefs.put(name, pref.getInt("value"));
|
|
|
|
} catch (JSONException je) {
|
|
|
|
// the value could not be parsed as an int. ignore this
|
|
|
|
// pref and continue
|
|
|
|
}
|
2012-04-17 08:33:19 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-17 08:33:42 -07:00
|
|
|
// check for null to make sure the batch of preferences we got notified
|
|
|
|
// of are in the fact the ones we requested and not those requested by
|
|
|
|
// other java code
|
|
|
|
if (zoomAnimationFrames != null) {
|
|
|
|
setZoomAnimationFrames(zoomAnimationFrames);
|
|
|
|
Axis.setPrefs(axisPrefs);
|
|
|
|
GeckoAppShell.unregisterGeckoEventListener(MESSAGE_PREFS_DATA, this);
|
|
|
|
}
|
2011-11-15 13:41:19 -08:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-04-17 08:33:19 -07:00
|
|
|
private void setZoomAnimationFrames(String frames) {
|
|
|
|
try {
|
2012-04-17 08:33:42 -07:00
|
|
|
if (frames.length() > 0) {
|
2012-04-17 08:33:19 -07:00
|
|
|
StringTokenizer st = new StringTokenizer(frames, ",");
|
|
|
|
float[] values = new float[st.countTokens()];
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
values[i] = Float.parseFloat(st.nextToken());
|
|
|
|
}
|
|
|
|
ZOOM_ANIMATION_FRAMES = values;
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
Log.e(LOGTAG, "Error setting zoom animation frames", e);
|
|
|
|
} finally {
|
|
|
|
Log.i(LOGTAG, "Zoom animation frames: " + Arrays.toString(ZOOM_ANIMATION_FRAMES));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
2012-02-14 12:28:27 -08:00
|
|
|
switch (event.getAction() & MotionEvent.ACTION_MASK) {
|
2011-11-18 10:28:17 -08:00
|
|
|
case MotionEvent.ACTION_DOWN: return onTouchStart(event);
|
|
|
|
case MotionEvent.ACTION_MOVE: return onTouchMove(event);
|
|
|
|
case MotionEvent.ACTION_UP: return onTouchEnd(event);
|
|
|
|
case MotionEvent.ACTION_CANCEL: return onTouchCancel(event);
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 13:53:39 -08:00
|
|
|
/** This function must be called from the UI thread. */
|
2011-12-20 13:53:39 -08:00
|
|
|
public void abortAnimation() {
|
2012-01-11 14:08:14 -08:00
|
|
|
checkMainThread();
|
2011-12-20 13:53:39 -08:00
|
|
|
// this happens when gecko changes the viewport on us or if the device is rotated.
|
|
|
|
// if that's the case, abort any animation in progress and re-zoom so that the page
|
|
|
|
// snaps to edges. for other cases (where the user's finger(s) are down) don't do
|
|
|
|
// anything special.
|
2012-03-12 09:03:38 -07:00
|
|
|
switch (mState) {
|
|
|
|
case FLING:
|
2012-01-10 07:06:01 -08:00
|
|
|
mX.stopFling();
|
|
|
|
mY.stopFling();
|
2012-03-12 09:03:38 -07:00
|
|
|
// fall through
|
2012-03-13 09:14:28 -07:00
|
|
|
case BOUNCE:
|
2012-03-12 09:03:38 -07:00
|
|
|
case ANIMATED_ZOOM:
|
|
|
|
// the zoom that's in progress likely makes no sense any more (such as if
|
|
|
|
// the screen orientation changed) so abort it
|
2012-03-13 09:14:28 -07:00
|
|
|
mState = PanZoomState.NOTHING;
|
2012-03-12 09:03:38 -07:00
|
|
|
// fall through
|
|
|
|
case NOTHING:
|
|
|
|
// Don't do animations here; they're distracting and can cause flashes on page
|
|
|
|
// transitions.
|
2012-03-15 07:40:55 -07:00
|
|
|
synchronized (mController) {
|
|
|
|
mController.setViewportMetrics(getValidViewportMetrics());
|
|
|
|
mController.notifyLayerClientOfGeometryChange();
|
|
|
|
}
|
2012-03-12 09:03:38 -07:00
|
|
|
break;
|
2011-12-01 06:37:37 -08:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-05-15 10:22:26 -07:00
|
|
|
/** This function must be called on the UI thread. */
|
2012-07-10 09:46:49 -07:00
|
|
|
public void startingNewEventBlock(MotionEvent event, boolean waitingForTouchListeners) {
|
2012-05-15 10:22:26 -07:00
|
|
|
checkMainThread();
|
2012-07-10 09:46:49 -07:00
|
|
|
mSubscroller.cancel();
|
|
|
|
if (waitingForTouchListeners && (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
|
2012-05-15 10:22:26 -07:00
|
|
|
// this is the first touch point going down, so we enter the pending state
|
|
|
|
// seting the state will kill any animations in progress, possibly leaving
|
|
|
|
// the page in overscroll
|
|
|
|
mState = PanZoomState.WAITING_LISTENERS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This function must be called on the UI thread. */
|
|
|
|
public void preventedTouchFinished() {
|
|
|
|
checkMainThread();
|
|
|
|
if (mState == PanZoomState.WAITING_LISTENERS) {
|
|
|
|
// if we enter here, we just finished a block of events whose default actions
|
|
|
|
// were prevented by touch listeners. Now there are no touch points left, so
|
|
|
|
// we need to reset our state and re-bounce because we might be in overscroll
|
|
|
|
mState = PanZoomState.NOTHING;
|
|
|
|
bounce();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 20:43:36 -08:00
|
|
|
/** This must be called on the UI thread. */
|
2012-05-23 07:49:52 -07:00
|
|
|
public void pageRectUpdated() {
|
2012-01-30 20:43:36 -08:00
|
|
|
if (mState == PanZoomState.NOTHING) {
|
2012-03-15 07:40:55 -07:00
|
|
|
synchronized (mController) {
|
|
|
|
ViewportMetrics validated = getValidViewportMetrics();
|
|
|
|
if (! (new ViewportMetrics(mController.getViewportMetrics())).fuzzyEquals(validated)) {
|
|
|
|
// page size changed such that we are now in overscroll. snap to the
|
|
|
|
// the nearest valid viewport
|
|
|
|
mController.setViewportMetrics(validated);
|
|
|
|
mController.notifyLayerClientOfGeometryChange();
|
|
|
|
}
|
2012-01-30 20:43:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
/*
|
|
|
|
* Panning/scrolling
|
|
|
|
*/
|
|
|
|
|
|
|
|
private boolean onTouchStart(MotionEvent event) {
|
2011-11-21 12:26:45 -08:00
|
|
|
// user is taking control of movement, so stop
|
|
|
|
// any auto-movement we have going
|
2011-12-07 13:07:24 -08:00
|
|
|
stopAnimationTimer();
|
2011-11-21 12:26:45 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
switch (mState) {
|
2011-11-15 13:41:19 -08:00
|
|
|
case ANIMATED_ZOOM:
|
2012-04-11 13:46:02 -07:00
|
|
|
// We just interrupted a double-tap animation, so force a redraw in
|
|
|
|
// case this touchstart is just a tap that doesn't end up triggering
|
|
|
|
// a redraw
|
|
|
|
mController.setForceRedraw();
|
|
|
|
mController.notifyLayerClientOfGeometryChange();
|
|
|
|
// fall through
|
2011-11-18 10:28:17 -08:00
|
|
|
case FLING:
|
2012-03-13 09:14:28 -07:00
|
|
|
case BOUNCE:
|
2011-11-18 10:28:17 -08:00
|
|
|
case NOTHING:
|
2012-05-15 10:22:26 -07:00
|
|
|
case WAITING_LISTENERS:
|
2012-01-10 07:05:48 -08:00
|
|
|
startTouch(event.getX(0), event.getY(0), event.getEventTime());
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
|
|
|
case TOUCHING:
|
|
|
|
case PANNING:
|
2011-11-18 13:08:17 -08:00
|
|
|
case PANNING_LOCKED:
|
2011-11-18 10:28:17 -08:00
|
|
|
case PANNING_HOLD:
|
2011-11-18 13:08:17 -08:00
|
|
|
case PANNING_HOLD_LOCKED:
|
2011-11-18 10:28:17 -08:00
|
|
|
case PINCHING:
|
2012-01-10 07:05:43 -08:00
|
|
|
Log.e(LOGTAG, "Received impossible touch down while in " + mState);
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Log.e(LOGTAG, "Unhandled case " + mState + " in onTouchStart");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean onTouchMove(MotionEvent event) {
|
2011-12-19 19:32:41 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
switch (mState) {
|
|
|
|
case FLING:
|
2012-03-13 09:14:28 -07:00
|
|
|
case BOUNCE:
|
2012-05-15 10:22:26 -07:00
|
|
|
case WAITING_LISTENERS:
|
2011-11-18 10:28:17 -08:00
|
|
|
// should never happen
|
|
|
|
Log.e(LOGTAG, "Received impossible touch move while in " + mState);
|
2012-04-11 13:46:02 -07:00
|
|
|
// fall through
|
|
|
|
case ANIMATED_ZOOM:
|
|
|
|
case NOTHING:
|
|
|
|
// may happen if user double-taps and drags without lifting after the
|
|
|
|
// second tap. ignore the move if this happens.
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
2012-02-14 10:55:46 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
case TOUCHING:
|
2012-02-14 11:43:45 -08:00
|
|
|
if (panDistance(event) < PAN_THRESHOLD) {
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
2012-01-10 07:05:51 -08:00
|
|
|
}
|
2011-11-30 13:10:25 -08:00
|
|
|
cancelTouch();
|
2012-02-14 10:55:46 -08:00
|
|
|
startPanning(event.getX(0), event.getY(0), event.getEventTime());
|
|
|
|
track(event);
|
|
|
|
return true;
|
|
|
|
|
2011-11-18 13:08:17 -08:00
|
|
|
case PANNING_HOLD_LOCKED:
|
|
|
|
mState = PanZoomState.PANNING_LOCKED;
|
|
|
|
// fall through
|
|
|
|
case PANNING_LOCKED:
|
2011-11-21 12:26:45 -08:00
|
|
|
track(event);
|
2011-11-18 13:08:17 -08:00
|
|
|
return true;
|
2012-02-14 10:55:46 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
case PANNING_HOLD:
|
|
|
|
mState = PanZoomState.PANNING;
|
|
|
|
// fall through
|
|
|
|
case PANNING:
|
2011-11-21 12:26:45 -08:00
|
|
|
track(event);
|
2011-11-18 10:28:17 -08:00
|
|
|
return true;
|
2012-02-14 10:55:46 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
case PINCHING:
|
|
|
|
// scale gesture listener will handle this
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Log.e(LOGTAG, "Unhandled case " + mState + " in onTouchMove");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean onTouchEnd(MotionEvent event) {
|
2011-12-19 19:32:41 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
switch (mState) {
|
|
|
|
case FLING:
|
2012-03-13 09:14:28 -07:00
|
|
|
case BOUNCE:
|
2012-05-15 10:22:26 -07:00
|
|
|
case WAITING_LISTENERS:
|
2011-11-18 10:28:17 -08:00
|
|
|
// should never happen
|
|
|
|
Log.e(LOGTAG, "Received impossible touch end while in " + mState);
|
2012-04-11 13:46:02 -07:00
|
|
|
// fall through
|
|
|
|
case ANIMATED_ZOOM:
|
|
|
|
case NOTHING:
|
|
|
|
// may happen if user double-taps and drags without lifting after the
|
|
|
|
// second tap. ignore if this happens.
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
2012-04-11 13:46:02 -07:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
case TOUCHING:
|
|
|
|
mState = PanZoomState.NOTHING;
|
2011-11-21 12:26:45 -08:00
|
|
|
// the switch into TOUCHING might have happened while the page was
|
|
|
|
// snapping back after overscroll. we need to finish the snap if that
|
|
|
|
// was the case
|
2011-12-07 13:33:55 -08:00
|
|
|
bounce();
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
2012-04-11 13:46:02 -07:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
case PANNING:
|
2011-11-18 13:08:17 -08:00
|
|
|
case PANNING_LOCKED:
|
2011-11-18 10:28:17 -08:00
|
|
|
case PANNING_HOLD:
|
2011-11-18 13:08:17 -08:00
|
|
|
case PANNING_HOLD_LOCKED:
|
2011-11-18 10:28:17 -08:00
|
|
|
mState = PanZoomState.FLING;
|
2011-11-21 12:26:45 -08:00
|
|
|
fling();
|
2011-11-18 10:28:17 -08:00
|
|
|
return true;
|
2012-04-11 13:46:02 -07:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
case PINCHING:
|
2012-01-10 07:05:43 -08:00
|
|
|
mState = PanZoomState.NOTHING;
|
2011-11-18 10:28:17 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Log.e(LOGTAG, "Unhandled case " + mState + " in onTouchEnd");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean onTouchCancel(MotionEvent event) {
|
2012-05-15 10:22:26 -07:00
|
|
|
if (mState == PanZoomState.WAITING_LISTENERS) {
|
|
|
|
// we might get a cancel event from the TouchEventHandler while in the
|
|
|
|
// WAITING_LISTENERS state if the touch listeners prevent-default the
|
|
|
|
// block of events. at this point being in WAITING_LISTENERS is equivalent
|
|
|
|
// to being in NOTHING with the exception of possibly being in overscroll.
|
|
|
|
// so here we don't want to do anything right now; the overscroll will be
|
|
|
|
// corrected in preventedTouchFinished().
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-07 00:09:26 -07:00
|
|
|
cancelTouch();
|
2012-05-15 10:22:26 -07:00
|
|
|
mState = PanZoomState.NOTHING;
|
2011-11-21 12:26:45 -08:00
|
|
|
// ensure we snap back if we're overscrolled
|
2011-12-07 13:33:55 -08:00
|
|
|
bounce();
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-10 07:05:48 -08:00
|
|
|
private void startTouch(float x, float y, long time) {
|
|
|
|
mX.startTouch(x);
|
|
|
|
mY.startTouch(y);
|
|
|
|
mState = PanZoomState.TOUCHING;
|
|
|
|
mLastEventTime = time;
|
|
|
|
}
|
|
|
|
|
2012-02-14 10:55:46 -08:00
|
|
|
private void startPanning(float x, float y, long time) {
|
|
|
|
float dx = mX.panDistance(x);
|
|
|
|
float dy = mY.panDistance(y);
|
|
|
|
double angle = Math.atan2(dy, dx); // range [-pi, pi]
|
|
|
|
angle = Math.abs(angle); // range [0, pi]
|
|
|
|
|
|
|
|
// When the touch move breaks through the pan threshold, reposition the touch down origin
|
|
|
|
// so the page won't jump when we start panning.
|
|
|
|
mX.startTouch(x);
|
|
|
|
mY.startTouch(y);
|
|
|
|
mLastEventTime = time;
|
|
|
|
|
|
|
|
if (angle < AXIS_LOCK_ANGLE || angle > (Math.PI - AXIS_LOCK_ANGLE)) {
|
|
|
|
mY.setScrollingDisabled(true);
|
|
|
|
mState = PanZoomState.PANNING_LOCKED;
|
|
|
|
} else if (Math.abs(angle - (Math.PI / 2)) < AXIS_LOCK_ANGLE) {
|
|
|
|
mX.setScrollingDisabled(true);
|
|
|
|
mState = PanZoomState.PANNING_LOCKED;
|
|
|
|
} else {
|
|
|
|
mState = PanZoomState.PANNING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
private float panDistance(MotionEvent move) {
|
2012-01-10 07:05:51 -08:00
|
|
|
float dx = mX.panDistance(move.getX(0));
|
|
|
|
float dy = mY.panDistance(move.getY(0));
|
|
|
|
return FloatMath.sqrt(dx * dx + dy * dy);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-01-10 07:05:49 -08:00
|
|
|
private void track(float x, float y, long time) {
|
|
|
|
float timeDelta = (float)(time - mLastEventTime);
|
2011-12-19 06:26:41 -08:00
|
|
|
if (FloatUtils.fuzzyEquals(timeDelta, 0)) {
|
|
|
|
// probably a duplicate event, ignore it. using a zero timeDelta will mess
|
|
|
|
// up our velocity
|
|
|
|
return;
|
|
|
|
}
|
2012-01-10 07:05:49 -08:00
|
|
|
mLastEventTime = time;
|
2011-12-19 06:26:41 -08:00
|
|
|
|
2012-01-10 07:05:49 -08:00
|
|
|
mX.updateWithTouchAt(x, timeDelta);
|
|
|
|
mY.updateWithTouchAt(y, timeDelta);
|
2011-11-30 03:32:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void track(MotionEvent event) {
|
2012-01-10 07:05:53 -08:00
|
|
|
mX.saveTouchPos();
|
|
|
|
mY.saveTouchPos();
|
2011-11-30 03:32:49 -08:00
|
|
|
|
|
|
|
for (int i = 0; i < event.getHistorySize(); i++) {
|
2012-01-10 07:05:49 -08:00
|
|
|
track(event.getHistoricalX(0, i),
|
|
|
|
event.getHistoricalY(0, i),
|
|
|
|
event.getHistoricalEventTime(i));
|
2011-11-30 03:32:49 -08:00
|
|
|
}
|
2012-01-10 07:05:49 -08:00
|
|
|
track(event.getX(0), event.getY(0), event.getEventTime());
|
2011-11-18 13:08:17 -08:00
|
|
|
|
|
|
|
if (stopped()) {
|
|
|
|
if (mState == PanZoomState.PANNING) {
|
|
|
|
mState = PanZoomState.PANNING_HOLD;
|
|
|
|
} else if (mState == PanZoomState.PANNING_LOCKED) {
|
|
|
|
mState = PanZoomState.PANNING_HOLD_LOCKED;
|
|
|
|
} else {
|
|
|
|
// should never happen, but handle anyway for robustness
|
|
|
|
Log.e(LOGTAG, "Impossible case " + mState + " when stopped in track");
|
|
|
|
mState = PanZoomState.PANNING_HOLD_LOCKED;
|
|
|
|
}
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-01-10 07:06:03 -08:00
|
|
|
mX.startPan();
|
|
|
|
mY.startPan();
|
2011-11-18 10:28:17 -08:00
|
|
|
updatePosition();
|
|
|
|
}
|
|
|
|
|
2011-11-21 12:26:45 -08:00
|
|
|
private void fling() {
|
2011-11-18 10:28:17 -08:00
|
|
|
updatePosition();
|
|
|
|
|
2011-12-07 13:07:24 -08:00
|
|
|
stopAnimationTimer();
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
boolean stopped = stopped();
|
2012-01-10 07:06:05 -08:00
|
|
|
mX.startFling(stopped);
|
|
|
|
mY.startFling(stopped);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-07 13:07:24 -08:00
|
|
|
startAnimationTimer(new FlingRunnable());
|
|
|
|
}
|
|
|
|
|
2011-12-07 13:33:55 -08:00
|
|
|
/* Performs a bounce-back animation to the given viewport metrics. */
|
|
|
|
private void bounce(ViewportMetrics metrics) {
|
|
|
|
stopAnimationTimer();
|
|
|
|
|
2012-01-02 16:06:49 -08:00
|
|
|
ViewportMetrics bounceStartMetrics = new ViewportMetrics(mController.getViewportMetrics());
|
|
|
|
if (bounceStartMetrics.fuzzyEquals(metrics)) {
|
2011-12-16 14:01:02 -08:00
|
|
|
mState = PanZoomState.NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-11 13:46:02 -07:00
|
|
|
// At this point we have already set mState to BOUNCE or ANIMATED_ZOOM, so
|
|
|
|
// getRedrawHint() is returning false. This means we can safely call
|
|
|
|
// setAnimationTarget to set the new final display port and not have it get
|
|
|
|
// clobbered by display ports from intermediate animation frames.
|
2012-04-02 14:14:33 -07:00
|
|
|
mController.setAnimationTarget(metrics);
|
2012-01-02 16:06:49 -08:00
|
|
|
startAnimationTimer(new BounceRunnable(bounceStartMetrics, metrics));
|
2011-12-07 13:33:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Performs a bounce-back animation to the nearest valid viewport metrics. */
|
|
|
|
private void bounce() {
|
2012-04-11 13:46:02 -07:00
|
|
|
mState = PanZoomState.BOUNCE;
|
2011-12-07 13:33:55 -08:00
|
|
|
bounce(getValidViewportMetrics());
|
|
|
|
}
|
|
|
|
|
2011-12-07 13:07:24 -08:00
|
|
|
/* Starts the fling or bounce animation. */
|
2012-01-05 18:13:25 -08:00
|
|
|
private void startAnimationTimer(final AnimationRunnable runnable) {
|
2011-12-07 13:07:24 -08:00
|
|
|
if (mAnimationTimer != null) {
|
|
|
|
Log.e(LOGTAG, "Attempted to start a new fling without canceling the old one!");
|
|
|
|
stopAnimationTimer();
|
|
|
|
}
|
|
|
|
|
2011-12-12 00:27:35 -08:00
|
|
|
mAnimationTimer = new Timer("Animation Timer");
|
2012-01-05 18:13:25 -08:00
|
|
|
mAnimationRunnable = runnable;
|
2011-12-07 13:07:24 -08:00
|
|
|
mAnimationTimer.scheduleAtFixedRate(new TimerTask() {
|
|
|
|
@Override
|
|
|
|
public void run() { mController.post(runnable); }
|
2011-11-18 10:28:17 -08:00
|
|
|
}, 0, 1000L/60L);
|
|
|
|
}
|
|
|
|
|
2011-12-07 13:07:24 -08:00
|
|
|
/* Stops the fling or bounce animation. */
|
|
|
|
private void stopAnimationTimer() {
|
|
|
|
if (mAnimationTimer != null) {
|
|
|
|
mAnimationTimer.cancel();
|
|
|
|
mAnimationTimer = null;
|
|
|
|
}
|
2012-01-05 18:13:25 -08:00
|
|
|
if (mAnimationRunnable != null) {
|
|
|
|
mAnimationRunnable.terminate();
|
|
|
|
mAnimationRunnable = null;
|
|
|
|
}
|
2011-12-07 13:07:24 -08:00
|
|
|
}
|
|
|
|
|
2012-01-10 07:05:57 -08:00
|
|
|
private float getVelocity() {
|
|
|
|
float xvel = mX.getRealVelocity();
|
|
|
|
float yvel = mY.getRealVelocity();
|
|
|
|
return FloatMath.sqrt(xvel * xvel + yvel * yvel);
|
|
|
|
}
|
|
|
|
|
2012-03-26 10:15:50 -07:00
|
|
|
public PointF getVelocityVector() {
|
|
|
|
return new PointF(mX.getRealVelocity(), mY.getRealVelocity());
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
private boolean stopped() {
|
2012-01-10 07:05:59 -08:00
|
|
|
return getVelocity() < STOPPED_THRESHOLD;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-05-02 10:12:46 -07:00
|
|
|
PointF resetDisplacement() {
|
2012-01-10 07:06:05 -08:00
|
|
|
return new PointF(mX.resetDisplacement(), mY.resetDisplacement());
|
|
|
|
}
|
2011-12-06 07:13:14 -08:00
|
|
|
|
2012-01-10 07:06:05 -08:00
|
|
|
private void updatePosition() {
|
|
|
|
mX.displace();
|
|
|
|
mY.displace();
|
2012-05-02 10:12:46 -07:00
|
|
|
PointF displacement = resetDisplacement();
|
2012-03-31 23:42:35 -07:00
|
|
|
if (FloatUtils.fuzzyEquals(displacement.x, 0.0f) && FloatUtils.fuzzyEquals(displacement.y, 0.0f)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-01-10 07:06:05 -08:00
|
|
|
if (! mSubscroller.scrollBy(displacement)) {
|
2011-12-13 14:43:08 -08:00
|
|
|
synchronized (mController) {
|
2012-01-10 07:06:05 -08:00
|
|
|
mController.scrollBy(displacement);
|
2011-12-13 14:43:08 -08:00
|
|
|
}
|
2011-12-06 07:13:14 -08:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-01-05 18:13:25 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-07 13:33:55 -08:00
|
|
|
/* The callback that performs the bounce animation. */
|
2012-07-15 13:20:43 -07:00
|
|
|
private final class BounceRunnable extends AnimationRunnable {
|
2012-01-02 16:06:49 -08:00
|
|
|
/* The current frame of the bounce-back animation */
|
|
|
|
private int mBounceFrame;
|
|
|
|
/*
|
|
|
|
* The viewport metrics that represent the start and end of the bounce-back animation,
|
|
|
|
* respectively.
|
|
|
|
*/
|
|
|
|
private ViewportMetrics mBounceStartMetrics;
|
|
|
|
private ViewportMetrics mBounceEndMetrics;
|
|
|
|
|
|
|
|
BounceRunnable(ViewportMetrics startMetrics, ViewportMetrics endMetrics) {
|
|
|
|
mBounceStartMetrics = startMetrics;
|
|
|
|
mBounceEndMetrics = endMetrics;
|
|
|
|
}
|
|
|
|
|
2012-01-05 18:13:25 -08:00
|
|
|
protected void animateFrame() {
|
2011-12-07 13:33:55 -08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* out.
|
|
|
|
*/
|
2012-04-11 13:46:02 -07:00
|
|
|
if (!(mState == PanZoomState.BOUNCE || mState == PanZoomState.ANIMATED_ZOOM)) {
|
2011-12-07 13:33:55 -08:00
|
|
|
finishAnimation();
|
|
|
|
return;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2011-12-07 13:33:55 -08:00
|
|
|
/* Perform the next frame of the bounce-back animation. */
|
2012-04-17 08:33:19 -07:00
|
|
|
if (mBounceFrame < ZOOM_ANIMATION_FRAMES.length) {
|
2011-12-07 13:33:55 -08:00
|
|
|
advanceBounce();
|
|
|
|
return;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
2011-12-07 13:33:55 -08:00
|
|
|
|
|
|
|
/* Finally, if there's nothing else to do, complete the animation and go to sleep. */
|
|
|
|
finishBounce();
|
|
|
|
finishAnimation();
|
2011-12-20 13:53:39 -08:00
|
|
|
mState = PanZoomState.NOTHING;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2011-12-07 13:33:55 -08:00
|
|
|
/* Performs one frame of a bounce animation. */
|
|
|
|
private void advanceBounce() {
|
2011-12-13 14:43:08 -08:00
|
|
|
synchronized (mController) {
|
2012-04-17 08:33:19 -07:00
|
|
|
float t = ZOOM_ANIMATION_FRAMES[mBounceFrame];
|
2011-12-13 14:43:08 -08:00
|
|
|
ViewportMetrics newMetrics = mBounceStartMetrics.interpolate(mBounceEndMetrics, t);
|
|
|
|
mController.setViewportMetrics(newMetrics);
|
|
|
|
mController.notifyLayerClientOfGeometryChange();
|
|
|
|
mBounceFrame++;
|
|
|
|
}
|
2011-12-07 13:33:55 -08:00
|
|
|
}
|
2011-11-23 11:08:20 -08:00
|
|
|
|
2011-12-07 13:33:55 -08:00
|
|
|
/* Concludes a bounce animation and snaps the viewport into place. */
|
|
|
|
private void finishBounce() {
|
2011-12-13 14:43:08 -08:00
|
|
|
synchronized (mController) {
|
|
|
|
mController.setViewportMetrics(mBounceEndMetrics);
|
|
|
|
mController.notifyLayerClientOfGeometryChange();
|
|
|
|
mBounceFrame = -1;
|
|
|
|
}
|
2011-12-07 13:33:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The callback that performs the fling animation.
|
2012-07-15 13:20:43 -07:00
|
|
|
private final class FlingRunnable extends AnimationRunnable {
|
2012-01-05 18:13:25 -08:00
|
|
|
protected void animateFrame() {
|
2011-12-07 13:33:55 -08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* out.
|
|
|
|
*/
|
|
|
|
if (mState != PanZoomState.FLING) {
|
|
|
|
finishAnimation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance flings, if necessary. */
|
2012-01-10 07:05:57 -08:00
|
|
|
boolean flingingX = mX.advanceFling();
|
|
|
|
boolean flingingY = mY.advanceFling();
|
|
|
|
|
2012-01-21 09:44:29 -08:00
|
|
|
boolean overscrolled = (mX.overscrolled() || mY.overscrolled());
|
2011-12-07 13:33:55 -08:00
|
|
|
|
2011-12-09 12:06:06 -08:00
|
|
|
/* If we're still flinging in any direction, update the origin. */
|
2011-12-07 13:33:55 -08:00
|
|
|
if (flingingX || flingingY) {
|
|
|
|
updatePosition();
|
|
|
|
|
2011-12-29 19:29:16 -08:00
|
|
|
/*
|
2012-01-10 07:05:57 -08:00
|
|
|
* Check to see if we're still flinging with an appreciable velocity. The threshold is
|
2011-12-29 19:29:16 -08:00
|
|
|
* higher in the case of overscroll, so we bounce back eagerly when overscrolling but
|
2012-01-10 07:05:57 -08:00
|
|
|
* coast smoothly to a stop when not. In other words, require a greater velocity to
|
|
|
|
* maintain the fling once we enter overscroll.
|
2011-12-29 19:29:16 -08:00
|
|
|
*/
|
2012-01-21 09:44:29 -08:00
|
|
|
float threshold = (overscrolled && !mSubscroller.scrolling() ? STOPPED_THRESHOLD : FLING_STOPPED_THRESHOLD);
|
2012-01-10 07:05:57 -08:00
|
|
|
if (getVelocity() >= threshold) {
|
|
|
|
// we're still flinging
|
2011-12-29 19:29:16 -08:00
|
|
|
return;
|
2012-01-10 07:05:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mX.stopFling();
|
|
|
|
mY.stopFling();
|
2011-12-29 19:29:16 -08:00
|
|
|
}
|
2011-12-09 12:06:06 -08:00
|
|
|
|
2012-01-21 09:44:29 -08:00
|
|
|
/* Perform a bounce-back animation if overscrolled. */
|
2012-01-10 07:05:57 -08:00
|
|
|
if (overscrolled) {
|
2011-12-07 13:33:55 -08:00
|
|
|
bounce();
|
2011-12-20 13:53:39 -08:00
|
|
|
} else {
|
2011-12-07 13:33:55 -08:00
|
|
|
finishAnimation();
|
2011-12-20 13:53:39 -08:00
|
|
|
mState = PanZoomState.NOTHING;
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-07 13:33:55 -08:00
|
|
|
private void finishAnimation() {
|
2012-01-11 14:08:14 -08:00
|
|
|
checkMainThread();
|
|
|
|
|
2011-12-07 13:33:55 -08:00
|
|
|
stopAnimationTimer();
|
|
|
|
|
|
|
|
// Force a viewport synchronisation
|
|
|
|
mController.setForceRedraw();
|
|
|
|
mController.notifyLayerClientOfGeometryChange();
|
|
|
|
}
|
|
|
|
|
2011-12-07 10:44:36 -08:00
|
|
|
/* Returns the nearest viewport metrics with no overscroll visible. */
|
|
|
|
private ViewportMetrics getValidViewportMetrics() {
|
2012-02-02 06:15:37 -08:00
|
|
|
return getValidViewportMetrics(new ViewportMetrics(mController.getViewportMetrics()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private ViewportMetrics getValidViewportMetrics(ViewportMetrics viewportMetrics) {
|
2011-12-07 10:44:36 -08:00
|
|
|
/* First, we adjust the zoom factor so that we can make no overscrolled area visible. */
|
|
|
|
float zoomFactor = viewportMetrics.getZoomFactor();
|
2012-05-23 07:49:52 -07:00
|
|
|
RectF pageRect = viewportMetrics.getPageRect();
|
2011-12-07 10:44:36 -08:00
|
|
|
RectF viewport = viewportMetrics.getViewport();
|
|
|
|
|
2012-01-12 19:47:56 -08:00
|
|
|
float focusX = viewport.width() / 2.0f;
|
|
|
|
float focusY = viewport.height() / 2.0f;
|
2012-05-18 08:24:27 -07:00
|
|
|
|
2011-12-07 10:44:36 -08:00
|
|
|
float minZoomFactor = 0.0f;
|
2012-05-18 08:24:27 -07:00
|
|
|
float maxZoomFactor = MAX_ZOOM;
|
|
|
|
|
2012-05-18 08:24:27 -07:00
|
|
|
if (mController.getMinZoom() > 0)
|
|
|
|
minZoomFactor = mController.getMinZoom();
|
|
|
|
if (mController.getMaxZoom() > 0)
|
|
|
|
maxZoomFactor = mController.getMaxZoom();
|
|
|
|
|
2012-05-18 08:24:27 -07:00
|
|
|
if (!mController.getAllowZoom()) {
|
|
|
|
// If allowZoom is false, clamp to the default zoom level.
|
|
|
|
maxZoomFactor = minZoomFactor = mController.getDefaultZoom();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure minZoomFactor keeps the page at least as big as the viewport.
|
2012-05-23 07:49:52 -07:00
|
|
|
if (pageRect.width() > 0) {
|
|
|
|
float scaleFactor = viewport.width() / pageRect.width();
|
2012-01-06 12:21:49 -08:00
|
|
|
minZoomFactor = Math.max(minZoomFactor, zoomFactor * scaleFactor);
|
2012-05-23 07:49:52 -07:00
|
|
|
if (viewport.width() > pageRect.width())
|
2012-05-18 08:24:27 -07:00
|
|
|
focusX = 0.0f;
|
2011-12-07 10:44:36 -08:00
|
|
|
}
|
2012-05-23 07:49:52 -07:00
|
|
|
if (pageRect.height() > 0) {
|
|
|
|
float scaleFactor = viewport.height() / pageRect.height();
|
2012-01-06 12:21:49 -08:00
|
|
|
minZoomFactor = Math.max(minZoomFactor, zoomFactor * scaleFactor);
|
2012-05-23 07:49:52 -07:00
|
|
|
if (viewport.height() > pageRect.height())
|
2012-05-18 08:24:27 -07:00
|
|
|
focusY = 0.0f;
|
2011-12-07 10:44:36 -08:00
|
|
|
}
|
|
|
|
|
2012-05-18 08:24:27 -07:00
|
|
|
maxZoomFactor = Math.max(maxZoomFactor, minZoomFactor);
|
|
|
|
|
|
|
|
if (zoomFactor < minZoomFactor) {
|
2012-01-12 19:47:56 -08:00
|
|
|
// if one (or both) of the page dimensions is smaller than the viewport,
|
|
|
|
// zoom using the top/left as the focus on that axis. this prevents the
|
|
|
|
// scenario where, if both dimensions are smaller than the viewport, but
|
|
|
|
// by different scale factors, we end up scrolled to the end on one axis
|
|
|
|
// after applying the scale
|
|
|
|
PointF center = new PointF(focusX, focusY);
|
2011-12-07 10:44:36 -08:00
|
|
|
viewportMetrics.scaleTo(minZoomFactor, center);
|
2012-05-18 08:24:27 -07:00
|
|
|
} else if (zoomFactor > maxZoomFactor) {
|
2011-12-09 09:03:19 -08:00
|
|
|
PointF center = new PointF(viewport.width() / 2.0f, viewport.height() / 2.0f);
|
2012-05-18 08:24:27 -07:00
|
|
|
viewportMetrics.scaleTo(maxZoomFactor, center);
|
2011-12-07 10:44:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we pan to the right origin. */
|
|
|
|
viewportMetrics.setViewport(viewportMetrics.getClampedViewport());
|
|
|
|
|
|
|
|
return viewportMetrics;
|
|
|
|
}
|
|
|
|
|
2012-07-15 13:20:43 -07:00
|
|
|
private final class AxisX extends Axis {
|
2012-01-10 07:06:05 -08:00
|
|
|
AxisX(SubdocumentScrollHelper subscroller) { super(subscroller); }
|
2011-12-07 10:44:02 -08:00
|
|
|
@Override
|
|
|
|
public float getOrigin() { return mController.getOrigin().x; }
|
|
|
|
@Override
|
|
|
|
protected float getViewportLength() { return mController.getViewportSize().width; }
|
|
|
|
@Override
|
2012-05-23 07:49:52 -07:00
|
|
|
protected float getPageStart() { return mController.getPageRect().left; }
|
|
|
|
@Override
|
|
|
|
protected float getPageLength() { return mController.getPageRect().width(); }
|
2011-12-07 10:44:02 -08:00
|
|
|
}
|
|
|
|
|
2012-07-15 13:20:43 -07:00
|
|
|
private final class AxisY extends Axis {
|
2012-01-10 07:06:05 -08:00
|
|
|
AxisY(SubdocumentScrollHelper subscroller) { super(subscroller); }
|
2011-12-07 10:44:02 -08:00
|
|
|
@Override
|
|
|
|
public float getOrigin() { return mController.getOrigin().y; }
|
|
|
|
@Override
|
|
|
|
protected float getViewportLength() { return mController.getViewportSize().height; }
|
|
|
|
@Override
|
2012-05-23 07:49:52 -07:00
|
|
|
protected float getPageStart() { return mController.getPageRect().top; }
|
|
|
|
@Override
|
|
|
|
protected float getPageLength() { return mController.getPageRect().height(); }
|
2011-12-07 10:44:02 -08:00
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
/*
|
|
|
|
* Zooming
|
|
|
|
*/
|
2012-01-10 07:05:46 -08:00
|
|
|
@Override
|
2012-01-23 19:18:24 -08:00
|
|
|
public boolean onScaleBegin(SimpleScaleGestureDetector detector) {
|
2012-01-10 07:05:46 -08:00
|
|
|
if (mState == PanZoomState.ANIMATED_ZOOM)
|
|
|
|
return false;
|
|
|
|
|
2012-05-18 08:24:27 -07:00
|
|
|
if (!mController.getAllowZoom())
|
|
|
|
return false;
|
|
|
|
|
2012-01-10 07:05:46 -08:00
|
|
|
mState = PanZoomState.PINCHING;
|
|
|
|
mLastZoomFocus = new PointF(detector.getFocusX(), detector.getFocusY());
|
|
|
|
cancelTouch();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
@Override
|
2012-01-23 19:18:24 -08:00
|
|
|
public boolean onScale(SimpleScaleGestureDetector detector) {
|
2012-07-13 07:17:03 -07:00
|
|
|
if (GeckoApp.mAppContext == null || GeckoApp.mAppContext.mDOMFullScreen)
|
2012-03-08 07:53:01 -08:00
|
|
|
return false;
|
|
|
|
|
2012-05-03 19:24:05 -07:00
|
|
|
if (mState != PanZoomState.PINCHING)
|
2011-11-15 13:41:19 -08:00
|
|
|
return false;
|
|
|
|
|
2011-12-20 14:15:09 -08:00
|
|
|
float prevSpan = detector.getPreviousSpan();
|
|
|
|
if (FloatUtils.fuzzyEquals(prevSpan, 0.0f)) {
|
|
|
|
// let's eat this one to avoid setting the new zoom to infinity (bug 711453)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
float spanRatio = detector.getCurrentSpan() / prevSpan;
|
2011-12-09 12:06:15 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Apply edge resistance if we're zoomed out smaller than the page size by scaling the zoom
|
|
|
|
* factor toward 1.0.
|
|
|
|
*/
|
|
|
|
float resistance = Math.min(mX.getEdgeResistance(), mY.getEdgeResistance());
|
|
|
|
if (spanRatio > 1.0f)
|
|
|
|
spanRatio = 1.0f + (spanRatio - 1.0f) * resistance;
|
|
|
|
else
|
|
|
|
spanRatio = 1.0f - (1.0f - spanRatio) * resistance;
|
|
|
|
|
2011-12-13 14:43:08 -08:00
|
|
|
synchronized (mController) {
|
|
|
|
float newZoomFactor = mController.getZoomFactor() * spanRatio;
|
2012-05-18 08:24:27 -07:00
|
|
|
float minZoomFactor = 0.0f;
|
|
|
|
float maxZoomFactor = MAX_ZOOM;
|
|
|
|
|
|
|
|
if (mController.getMinZoom() > 0)
|
|
|
|
minZoomFactor = mController.getMinZoom();
|
|
|
|
if (mController.getMaxZoom() > 0)
|
|
|
|
maxZoomFactor = mController.getMaxZoom();
|
|
|
|
|
|
|
|
if (newZoomFactor < minZoomFactor) {
|
|
|
|
// apply resistance when zooming past minZoomFactor,
|
|
|
|
// such that it asymptotically reaches minZoomFactor / 2.0
|
|
|
|
// but never exceeds that
|
|
|
|
final float rate = 0.5f; // controls how quickly we approach the limit
|
|
|
|
float excessZoom = minZoomFactor - newZoomFactor;
|
|
|
|
excessZoom = 1.0f - (float)Math.exp(-excessZoom * rate);
|
|
|
|
newZoomFactor = minZoomFactor * (1.0f - excessZoom / 2.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newZoomFactor > maxZoomFactor) {
|
|
|
|
// apply resistance when zooming past maxZoomFactor,
|
|
|
|
// such that it asymptotically reaches maxZoomFactor + 1.0
|
2012-05-18 08:24:27 -07:00
|
|
|
// but never exceeds that
|
2012-05-18 08:24:27 -07:00
|
|
|
float excessZoom = newZoomFactor - maxZoomFactor;
|
2011-12-26 21:56:57 -08:00
|
|
|
excessZoom = 1.0f - (float)Math.exp(-excessZoom);
|
2012-05-18 08:24:27 -07:00
|
|
|
newZoomFactor = maxZoomFactor + excessZoom;
|
2011-12-26 21:56:57 -08:00
|
|
|
}
|
2011-11-23 11:07:47 -08:00
|
|
|
|
2011-12-13 14:43:08 -08:00
|
|
|
mController.scrollBy(new PointF(mLastZoomFocus.x - detector.getFocusX(),
|
|
|
|
mLastZoomFocus.y - detector.getFocusY()));
|
|
|
|
PointF focus = new PointF(detector.getFocusX(), detector.getFocusY());
|
|
|
|
mController.scaleWithFocus(newZoomFactor, focus);
|
|
|
|
}
|
2011-11-23 11:07:47 -08:00
|
|
|
|
|
|
|
mLastZoomFocus.set(detector.getFocusX(), detector.getFocusY());
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-01-23 19:18:24 -08:00
|
|
|
public void onScaleEnd(SimpleScaleGestureDetector detector) {
|
2011-11-15 13:41:19 -08:00
|
|
|
if (mState == PanZoomState.ANIMATED_ZOOM)
|
|
|
|
return;
|
|
|
|
|
2012-01-10 07:05:48 -08:00
|
|
|
// switch back to the touching state
|
|
|
|
startTouch(detector.getFocusX(), detector.getFocusY(), detector.getEventTime());
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-07 13:34:52 -08:00
|
|
|
// Force a viewport synchronisation
|
|
|
|
mController.setForceRedraw();
|
|
|
|
mController.notifyLayerClientOfGeometryChange();
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-01-10 07:05:46 -08:00
|
|
|
public boolean getRedrawHint() {
|
2012-03-13 09:14:28 -07:00
|
|
|
switch (mState) {
|
|
|
|
case PINCHING:
|
|
|
|
case ANIMATED_ZOOM:
|
|
|
|
case BOUNCE:
|
|
|
|
// don't redraw during these because the zoom is (or might be, in the case
|
|
|
|
// of BOUNCE) be changing rapidly and gecko will have to redraw the entire
|
|
|
|
// display port area. we trigger a force-redraw upon exiting these states.
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
// allow redrawing in other states
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-10 07:05:46 -08:00
|
|
|
}
|
|
|
|
|
2012-01-10 07:05:42 -08:00
|
|
|
private void sendPointToGecko(String event, MotionEvent motionEvent) {
|
2012-01-06 12:21:49 -08:00
|
|
|
String json;
|
2011-11-18 10:28:17 -08:00
|
|
|
try {
|
|
|
|
PointF point = new PointF(motionEvent.getX(), motionEvent.getY());
|
|
|
|
point = mController.convertViewPointToLayerPoint(point);
|
|
|
|
if (point == null) {
|
|
|
|
return;
|
|
|
|
}
|
2012-01-06 12:21:49 -08:00
|
|
|
json = PointUtils.toJSON(point).toString();
|
2012-01-10 07:05:42 -08:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Unable to convert point to JSON for " + event, e);
|
|
|
|
return;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-02-08 23:18:27 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(event, json));
|
2012-01-10 07:05:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLongPress(MotionEvent motionEvent) {
|
|
|
|
sendPointToGecko("Gesture:LongPress", motionEvent);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
2011-11-23 11:08:20 -08:00
|
|
|
|
2012-05-18 08:24:27 -07:00
|
|
|
@Override
|
|
|
|
public boolean onSingleTapUp(MotionEvent motionEvent) {
|
|
|
|
// When zooming is enabled, wait to see if there's a double-tap.
|
2012-07-03 07:04:10 -07:00
|
|
|
if (!mController.getAllowZoom()) {
|
|
|
|
sendPointToGecko("Gesture:SingleTap", motionEvent);
|
|
|
|
}
|
|
|
|
// return false because we still want to get the ACTION_UP event that triggers this
|
|
|
|
return false;
|
2012-05-18 08:24:27 -07:00
|
|
|
}
|
|
|
|
|
2011-11-30 13:10:25 -08:00
|
|
|
@Override
|
|
|
|
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
|
2012-05-18 08:24:27 -07:00
|
|
|
// When zooming is disabled, we handle this in onSingleTapUp.
|
2012-07-03 07:04:10 -07:00
|
|
|
if (mController.getAllowZoom()) {
|
|
|
|
sendPointToGecko("Gesture:SingleTap", motionEvent);
|
|
|
|
}
|
2011-11-30 13:10:25 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-15 13:41:19 -08:00
|
|
|
@Override
|
|
|
|
public boolean onDoubleTap(MotionEvent motionEvent) {
|
2012-07-03 07:04:10 -07:00
|
|
|
if (mController.getAllowZoom()) {
|
|
|
|
sendPointToGecko("Gesture:DoubleTap", motionEvent);
|
|
|
|
}
|
2011-11-15 13:41:19 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-07 00:09:26 -07:00
|
|
|
private void cancelTouch() {
|
2012-02-08 23:18:27 -08:00
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Gesture:CancelTouch", "");
|
2012-01-10 07:05:46 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
}
|
|
|
|
|
2012-04-14 10:18:10 -07:00
|
|
|
/**
|
|
|
|
* Zoom to a specified rect IN CSS PIXELS.
|
|
|
|
*
|
|
|
|
* While we usually use device pixels, @zoomToRect must be specified in CSS
|
|
|
|
* pixels.
|
|
|
|
*/
|
2011-12-20 13:53:39 -08:00
|
|
|
private boolean animatedZoomTo(RectF zoomToRect) {
|
2011-11-15 13:41:19 -08:00
|
|
|
mState = PanZoomState.ANIMATED_ZOOM;
|
|
|
|
final float startZoom = mController.getZoomFactor();
|
|
|
|
|
|
|
|
RectF viewport = mController.getViewport();
|
2012-02-02 06:15:37 -08:00
|
|
|
// 1. adjust the aspect ratio of zoomToRect to match that of the current viewport,
|
|
|
|
// enlarging as necessary (if it gets too big, it will get shrunk in the next step).
|
|
|
|
// while enlarging make sure we enlarge equally on both sides to keep the target rect
|
|
|
|
// centered.
|
|
|
|
float targetRatio = viewport.width() / viewport.height();
|
|
|
|
float rectRatio = zoomToRect.width() / zoomToRect.height();
|
|
|
|
if (FloatUtils.fuzzyEquals(targetRatio, rectRatio)) {
|
|
|
|
// all good, do nothing
|
|
|
|
} else if (targetRatio < rectRatio) {
|
|
|
|
// need to increase zoomToRect height
|
|
|
|
float newHeight = zoomToRect.width() / targetRatio;
|
|
|
|
zoomToRect.top -= (newHeight - zoomToRect.height()) / 2;
|
2011-11-15 13:41:19 -08:00
|
|
|
zoomToRect.bottom = zoomToRect.top + newHeight;
|
2012-02-02 06:15:37 -08:00
|
|
|
} else { // targetRatio > rectRatio) {
|
|
|
|
// need to increase zoomToRect width
|
|
|
|
float newWidth = targetRatio * zoomToRect.height();
|
|
|
|
zoomToRect.left -= (newWidth - zoomToRect.width()) / 2;
|
|
|
|
zoomToRect.right = zoomToRect.left + newWidth;
|
2011-11-15 13:41:19 -08:00
|
|
|
}
|
|
|
|
|
2012-04-14 10:18:10 -07:00
|
|
|
float finalZoom = viewport.width() / zoomToRect.width();
|
2011-11-15 13:41:19 -08:00
|
|
|
|
2011-12-07 13:34:52 -08:00
|
|
|
ViewportMetrics finalMetrics = new ViewportMetrics(mController.getViewportMetrics());
|
2012-04-14 10:18:10 -07:00
|
|
|
finalMetrics.setOrigin(new PointF(zoomToRect.left * finalMetrics.getZoomFactor(),
|
|
|
|
zoomToRect.top * finalMetrics.getZoomFactor()));
|
2011-12-07 13:34:52 -08:00
|
|
|
finalMetrics.scaleTo(finalZoom, new PointF(0.0f, 0.0f));
|
2011-11-15 13:41:19 -08:00
|
|
|
|
2012-02-02 06:15:37 -08:00
|
|
|
// 2. now run getValidViewportMetrics on it, so that the target viewport is
|
|
|
|
// clamped down to prevent overscroll, over-zoom, and other bad conditions.
|
|
|
|
finalMetrics = getValidViewportMetrics(finalMetrics);
|
|
|
|
|
2011-12-07 13:34:52 -08:00
|
|
|
bounce(finalMetrics);
|
2011-11-15 13:41:19 -08:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|