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;
|
|
|
|
|
|
|
|
import org.mozilla.gecko.GeckoApp;
|
|
|
|
import org.mozilla.gecko.GeckoAppShell;
|
|
|
|
import org.mozilla.gecko.GeckoEvent;
|
2012-08-07 07:39:03 -07:00
|
|
|
import org.mozilla.gecko.ZoomConstraints;
|
2012-08-07 07:39:03 -07:00
|
|
|
import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
|
2012-08-01 14:42:11 -07:00
|
|
|
import org.mozilla.gecko.gfx.PointUtils;
|
2012-08-04 00:33:07 -07:00
|
|
|
import org.mozilla.gecko.util.EventDispatcher;
|
2012-07-27 21:57:47 -07:00
|
|
|
import org.mozilla.gecko.util.FloatUtils;
|
2012-08-02 17:13:40 -07:00
|
|
|
import org.mozilla.gecko.util.GeckoEventListener;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
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-07-27 17:53:54 -07:00
|
|
|
|
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-17 17:54:54 -07:00
|
|
|
public 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
|
|
|
|
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
|
|
|
|
2013-01-06 14:35:01 -08:00
|
|
|
// The maximum amount we would like to scroll with the mouse
|
|
|
|
private static final float MAX_SCROLL = 0.075f * GeckoAppShell.getDpi();
|
|
|
|
|
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-08-07 07:39:03 -07:00
|
|
|
private final PanZoomTarget mTarget;
|
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-08-04 00:33:07 -07:00
|
|
|
private final EventDispatcher mEventDispatcher;
|
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;
|
|
|
|
|
2012-08-04 00:33:07 -07:00
|
|
|
public PanZoomController(PanZoomTarget target, EventDispatcher eventDispatcher) {
|
2012-08-07 07:39:03 -07:00
|
|
|
mTarget = target;
|
2013-02-08 06:11:37 -08:00
|
|
|
mSubscroller = new SubdocumentScrollHelper(eventDispatcher);
|
2012-01-10 07:06:05 -08:00
|
|
|
mX = new AxisX(mSubscroller);
|
|
|
|
mY = new AxisY(mSubscroller);
|
|
|
|
|
2012-01-11 14:08:14 -08:00
|
|
|
mMainThread = GeckoApp.mAppContext.getMainLooper().getThread();
|
|
|
|
checkMainThread();
|
|
|
|
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.NOTHING);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-08-04 00:33:07 -07:00
|
|
|
mEventDispatcher = eventDispatcher;
|
|
|
|
registerEventListener(MESSAGE_ZOOM_RECT);
|
|
|
|
registerEventListener(MESSAGE_ZOOM_PAGE);
|
2012-04-17 08:33:19 -07:00
|
|
|
|
2012-09-17 11:22:28 -07:00
|
|
|
Axis.initPrefs();
|
2011-11-15 13:41:19 -08:00
|
|
|
}
|
|
|
|
|
2012-07-13 07:17:03 -07:00
|
|
|
public void destroy() {
|
2012-08-04 00:33:07 -07:00
|
|
|
unregisterEventListener(MESSAGE_ZOOM_RECT);
|
|
|
|
unregisterEventListener(MESSAGE_ZOOM_PAGE);
|
2012-07-13 07:17:03 -07:00
|
|
|
mSubscroller.destroy();
|
|
|
|
}
|
|
|
|
|
2012-10-11 09:58:53 -07:00
|
|
|
private final static float easeOut(float t) {
|
|
|
|
// ease-out approx.
|
|
|
|
// -(t-1)^2+1
|
|
|
|
t = t-1;
|
|
|
|
return -t*t+1;
|
|
|
|
}
|
|
|
|
|
2012-08-04 00:33:07 -07:00
|
|
|
private void registerEventListener(String event) {
|
|
|
|
mEventDispatcher.registerEventListener(event, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void unregisterEventListener(String event) {
|
|
|
|
mEventDispatcher.unregisterEventListener(event, this);
|
|
|
|
}
|
|
|
|
|
2012-07-23 06:57:46 -07:00
|
|
|
private void setState(PanZoomState state) {
|
2013-01-09 11:37:01 -08:00
|
|
|
if (state != mState) {
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("PanZoom:StateChange", state.toString()));
|
|
|
|
mState = state;
|
|
|
|
}
|
2012-07-23 06:57:46 -07:00
|
|
|
}
|
|
|
|
|
2012-08-07 07:39:03 -07:00
|
|
|
private ImmutableViewportMetrics getMetrics() {
|
2012-08-07 07:39:03 -07:00
|
|
|
return mTarget.getViewportMetrics();
|
2012-08-07 07:39:03 -07:00
|
|
|
}
|
|
|
|
|
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"));
|
2012-08-07 07:39:03 -07:00
|
|
|
mTarget.post(new Runnable() {
|
2012-01-10 07:05:46 -08:00
|
|
|
public void run() {
|
|
|
|
animatedZoomTo(zoomRect);
|
|
|
|
}
|
|
|
|
});
|
2012-01-10 07:06:10 -08:00
|
|
|
} else if (MESSAGE_ZOOM_PAGE.equals(event)) {
|
2012-08-07 07:39:03 -07:00
|
|
|
ImmutableViewportMetrics metrics = getMetrics();
|
|
|
|
RectF cssPageRect = metrics.getCssPageRect();
|
2012-01-10 07:05:46 -08:00
|
|
|
|
2012-08-07 07:39:03 -07:00
|
|
|
RectF viewableRect = metrics.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-08-07 07:39:03 -07:00
|
|
|
mTarget.post(new Runnable() {
|
2012-01-10 07:05:46 -08:00
|
|
|
public void run() {
|
|
|
|
animatedZoomTo(r);
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-01-06 14:35:01 -08:00
|
|
|
case MotionEvent.ACTION_SCROLL: return onScroll(event);
|
2011-11-18 10:28:17 -08:00
|
|
|
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-07-23 06:57:46 -07:00
|
|
|
setState(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-08-07 07:39:03 -07:00
|
|
|
synchronized (mTarget.getLock()) {
|
|
|
|
mTarget.setViewportMetrics(getValidViewportMetrics());
|
2013-02-04 07:02:11 -08:00
|
|
|
mTarget.forceRedraw();
|
2012-03-15 07:40:55 -07:00
|
|
|
}
|
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
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.WAITING_LISTENERS);
|
2012-05-15 10:22:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
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-08-07 07:39:03 -07:00
|
|
|
synchronized (mTarget.getLock()) {
|
2012-11-07 08:47:07 -08:00
|
|
|
ImmutableViewportMetrics validated = getValidViewportMetrics();
|
|
|
|
if (!getMetrics().fuzzyEquals(validated)) {
|
2012-03-15 07:40:55 -07:00
|
|
|
// page size changed such that we are now in overscroll. snap to the
|
|
|
|
// the nearest valid viewport
|
2012-08-07 07:39:03 -07:00
|
|
|
mTarget.setViewportMetrics(validated);
|
2012-03-15 07:40:55 -07:00
|
|
|
}
|
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
|
2013-01-09 08:21:04 -08:00
|
|
|
mTarget.forceRedraw();
|
2012-04-11 13:46:02 -07:00
|
|
|
// 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-11-24 14:32:09 -08:00
|
|
|
// Don't allow panning if there is an element in full-screen mode. See bug 775511.
|
|
|
|
if (mTarget.isFullScreen() || 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:
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.PANNING_LOCKED);
|
2011-11-18 13:08:17 -08:00
|
|
|
// 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:
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.PANNING);
|
2011-11-18 10:28:17 -08:00
|
|
|
// 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:
|
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:
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(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-07-23 06:57:46 -07:00
|
|
|
setState(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-09-11 10:36:32 -07:00
|
|
|
cancelTouch();
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-06 14:35:01 -08:00
|
|
|
private boolean onScroll(MotionEvent event) {
|
|
|
|
if (mState == PanZoomState.NOTHING || mState == PanZoomState.FLING) {
|
|
|
|
float scrollX = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
|
|
|
|
float scrollY = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
|
|
|
|
|
|
|
|
scrollBy(scrollX * MAX_SCROLL, scrollY * MAX_SCROLL);
|
|
|
|
bounce();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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);
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.TOUCHING);
|
2012-01-10 07:05:48 -08:00
|
|
|
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;
|
|
|
|
|
2012-09-17 07:55:58 -07:00
|
|
|
if (!mX.scrollable() || !mY.scrollable()) {
|
|
|
|
setState(PanZoomState.PANNING);
|
|
|
|
} else if (angle < AXIS_LOCK_ANGLE || angle > (Math.PI - AXIS_LOCK_ANGLE)) {
|
2012-02-14 10:55:46 -08:00
|
|
|
mY.setScrollingDisabled(true);
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.PANNING_LOCKED);
|
2012-02-14 10:55:46 -08:00
|
|
|
} else if (Math.abs(angle - (Math.PI / 2)) < AXIS_LOCK_ANGLE) {
|
|
|
|
mX.setScrollingDisabled(true);
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.PANNING_LOCKED);
|
2012-02-14 10:55:46 -08:00
|
|
|
} else {
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.PANNING);
|
2012-02-14 10:55:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.PANNING_HOLD);
|
2011-11-18 13:08:17 -08:00
|
|
|
} else if (mState == PanZoomState.PANNING_LOCKED) {
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.PANNING_HOLD_LOCKED);
|
2011-11-18 13:08:17 -08:00
|
|
|
} else {
|
|
|
|
// should never happen, but handle anyway for robustness
|
|
|
|
Log.e(LOGTAG, "Impossible case " + mState + " when stopped in track");
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.PANNING_HOLD_LOCKED);
|
2011-11-18 13:08:17 -08:00
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2012-11-07 08:47:17 -08:00
|
|
|
private void scrollBy(float dx, float dy) {
|
|
|
|
ImmutableViewportMetrics scrolled = getMetrics().offsetViewportBy(dx, dy);
|
2012-11-07 08:47:08 -08:00
|
|
|
mTarget.setViewportMetrics(scrolled);
|
2012-08-07 07:39:04 -07:00
|
|
|
}
|
|
|
|
|
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. */
|
2013-01-09 11:37:01 -08:00
|
|
|
private void bounce(ImmutableViewportMetrics metrics, PanZoomState state) {
|
2011-12-07 13:33:55 -08:00
|
|
|
stopAnimationTimer();
|
|
|
|
|
2012-11-07 08:47:07 -08:00
|
|
|
ImmutableViewportMetrics bounceStartMetrics = getMetrics();
|
2012-01-02 16:06:49 -08:00
|
|
|
if (bounceStartMetrics.fuzzyEquals(metrics)) {
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.NOTHING);
|
2011-12-16 14:01:02 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-09 11:37:01 -08:00
|
|
|
setState(state);
|
|
|
|
|
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-08-07 07:39:03 -07:00
|
|
|
mTarget.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() {
|
2013-01-09 11:37:01 -08:00
|
|
|
bounce(getValidViewportMetrics(), PanZoomState.BOUNCE);
|
2011-12-07 13:33:55 -08:00
|
|
|
}
|
|
|
|
|
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
|
2012-08-07 07:39:03 -07:00
|
|
|
public void run() { mTarget.post(runnable); }
|
2012-10-11 09:58:53 -07:00
|
|
|
}, 0, (int)Axis.MS_PER_FRAME);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
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)) {
|
2012-08-07 07:39:03 -07:00
|
|
|
synchronized (mTarget.getLock()) {
|
2012-11-07 08:47:17 -08:00
|
|
|
scrollBy(displacement.x, displacement.y);
|
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-17 17:54:54 -07:00
|
|
|
private 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.
|
|
|
|
*/
|
2012-11-07 08:47:07 -08:00
|
|
|
private ImmutableViewportMetrics mBounceStartMetrics;
|
|
|
|
private ImmutableViewportMetrics mBounceEndMetrics;
|
2012-01-02 16:06:49 -08:00
|
|
|
|
2012-11-07 08:47:07 -08:00
|
|
|
BounceRunnable(ImmutableViewportMetrics startMetrics, ImmutableViewportMetrics endMetrics) {
|
|
|
|
mBounceStartMetrics = startMetrics;
|
|
|
|
mBounceEndMetrics = endMetrics;
|
2012-01-02 16:06:49 -08:00
|
|
|
}
|
|
|
|
|
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-10-11 09:58:53 -07:00
|
|
|
if (mBounceFrame < (int)(256f/Axis.MS_PER_FRAME)) {
|
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();
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(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() {
|
2012-08-07 07:39:03 -07:00
|
|
|
synchronized (mTarget.getLock()) {
|
2012-10-11 09:58:53 -07:00
|
|
|
float t = easeOut(mBounceFrame * Axis.MS_PER_FRAME / 256f);
|
2012-11-07 08:47:07 -08:00
|
|
|
ImmutableViewportMetrics newMetrics = mBounceStartMetrics.interpolate(mBounceEndMetrics, t);
|
2012-11-07 08:47:07 -08:00
|
|
|
mTarget.setViewportMetrics(newMetrics);
|
2011-12-13 14:43:08 -08:00
|
|
|
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() {
|
2012-08-07 07:39:03 -07:00
|
|
|
synchronized (mTarget.getLock()) {
|
2012-11-07 08:47:07 -08:00
|
|
|
mTarget.setViewportMetrics(mBounceEndMetrics);
|
2011-12-13 14:43:08 -08:00
|
|
|
mBounceFrame = -1;
|
|
|
|
}
|
2011-12-07 13:33:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The callback that performs the fling animation.
|
2012-07-17 17:54:54 -07:00
|
|
|
private 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();
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.NOTHING);
|
2011-12-20 13:53:39 -08:00
|
|
|
}
|
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
|
2013-01-09 08:21:04 -08:00
|
|
|
mTarget.forceRedraw();
|
2011-12-07 13:33:55 -08:00
|
|
|
}
|
|
|
|
|
2011-12-07 10:44:36 -08:00
|
|
|
/* Returns the nearest viewport metrics with no overscroll visible. */
|
2012-11-07 08:47:07 -08:00
|
|
|
private ImmutableViewportMetrics getValidViewportMetrics() {
|
2012-11-07 08:47:08 -08:00
|
|
|
return getValidViewportMetrics(getMetrics());
|
2012-02-02 06:15:37 -08:00
|
|
|
}
|
|
|
|
|
2012-11-07 08:47:08 -08:00
|
|
|
private ImmutableViewportMetrics getValidViewportMetrics(ImmutableViewportMetrics viewportMetrics) {
|
2011-12-07 10:44:36 -08:00
|
|
|
/* First, we adjust the zoom factor so that we can make no overscrolled area visible. */
|
2012-11-07 08:47:08 -08:00
|
|
|
float zoomFactor = viewportMetrics.zoomFactor;
|
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-08-07 07:39:03 -07:00
|
|
|
ZoomConstraints constraints = mTarget.getZoomConstraints();
|
2012-05-18 08:24:27 -07:00
|
|
|
|
2012-08-07 07:39:03 -07:00
|
|
|
if (constraints.getMinZoom() > 0)
|
|
|
|
minZoomFactor = constraints.getMinZoom();
|
|
|
|
if (constraints.getMaxZoom() > 0)
|
|
|
|
maxZoomFactor = constraints.getMaxZoom();
|
|
|
|
|
|
|
|
if (!constraints.getAllowZoom()) {
|
2012-05-18 08:24:27 -07:00
|
|
|
// If allowZoom is false, clamp to the default zoom level.
|
2012-08-07 07:39:03 -07:00
|
|
|
maxZoomFactor = minZoomFactor = constraints.getDefaultZoom();
|
2012-05-18 08:24:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2012-11-07 08:47:08 -08:00
|
|
|
viewportMetrics = 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-11-07 08:47:08 -08:00
|
|
|
viewportMetrics = viewportMetrics.scaleTo(maxZoomFactor, center);
|
2011-12-07 10:44:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we pan to the right origin. */
|
2012-11-07 08:47:08 -08:00
|
|
|
viewportMetrics = viewportMetrics.clamp();
|
2011-12-07 10:44:36 -08:00
|
|
|
|
2012-11-07 08:47:08 -08:00
|
|
|
return viewportMetrics;
|
2011-12-07 10:44:36 -08:00
|
|
|
}
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
private 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
|
2012-08-07 07:39:03 -07:00
|
|
|
public float getOrigin() { return getMetrics().viewportRectLeft; }
|
2011-12-07 10:44:02 -08:00
|
|
|
@Override
|
2012-08-07 07:39:03 -07:00
|
|
|
protected float getViewportLength() { return getMetrics().getWidth(); }
|
2011-12-07 10:44:02 -08:00
|
|
|
@Override
|
2012-08-07 07:39:03 -07:00
|
|
|
protected float getPageStart() { return getMetrics().pageRectLeft; }
|
2012-05-23 07:49:52 -07:00
|
|
|
@Override
|
2012-08-07 07:39:03 -07:00
|
|
|
protected float getPageLength() { return getMetrics().getPageWidth(); }
|
2011-12-07 10:44:02 -08:00
|
|
|
}
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
private 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
|
2012-08-07 07:39:03 -07:00
|
|
|
public float getOrigin() { return getMetrics().viewportRectTop; }
|
2011-12-07 10:44:02 -08:00
|
|
|
@Override
|
2012-08-07 07:39:03 -07:00
|
|
|
protected float getViewportLength() { return getMetrics().getHeight(); }
|
2011-12-07 10:44:02 -08:00
|
|
|
@Override
|
2012-08-07 07:39:03 -07:00
|
|
|
protected float getPageStart() { return getMetrics().pageRectTop; }
|
2012-05-23 07:49:52 -07:00
|
|
|
@Override
|
2012-08-07 07:39:03 -07:00
|
|
|
protected float getPageLength() { return getMetrics().getPageHeight(); }
|
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-08-07 07:39:03 -07:00
|
|
|
if (!mTarget.getZoomConstraints().getAllowZoom())
|
2012-05-18 08:24:27 -07:00
|
|
|
return false;
|
|
|
|
|
2012-07-23 06:57:46 -07:00
|
|
|
setState(PanZoomState.PINCHING);
|
2012-01-10 07:05:46 -08:00
|
|
|
mLastZoomFocus = new PointF(detector.getFocusX(), detector.getFocusY());
|
|
|
|
cancelTouch();
|
|
|
|
|
2012-10-02 13:18:21 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createNativeGestureEvent(GeckoEvent.ACTION_MAGNIFY_START, mLastZoomFocus, getMetrics().zoomFactor));
|
|
|
|
|
2012-01-10 07:05:46 -08:00
|
|
|
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-11-24 14:32:08 -08:00
|
|
|
if (mTarget.isFullScreen())
|
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.
|
|
|
|
*/
|
2012-08-11 10:32:04 -07:00
|
|
|
float resistance = Math.min(mX.getEdgeResistance(true), mY.getEdgeResistance(true));
|
2011-12-09 12:06:15 -08:00
|
|
|
if (spanRatio > 1.0f)
|
|
|
|
spanRatio = 1.0f + (spanRatio - 1.0f) * resistance;
|
|
|
|
else
|
|
|
|
spanRatio = 1.0f - (1.0f - spanRatio) * resistance;
|
|
|
|
|
2012-08-07 07:39:03 -07:00
|
|
|
synchronized (mTarget.getLock()) {
|
2012-08-07 07:39:03 -07:00
|
|
|
float newZoomFactor = getMetrics().zoomFactor * spanRatio;
|
2012-05-18 08:24:27 -07:00
|
|
|
float minZoomFactor = 0.0f;
|
|
|
|
float maxZoomFactor = MAX_ZOOM;
|
|
|
|
|
2012-08-07 07:39:03 -07:00
|
|
|
ZoomConstraints constraints = mTarget.getZoomConstraints();
|
2012-08-07 07:39:03 -07:00
|
|
|
|
|
|
|
if (constraints.getMinZoom() > 0)
|
|
|
|
minZoomFactor = constraints.getMinZoom();
|
|
|
|
if (constraints.getMaxZoom() > 0)
|
|
|
|
maxZoomFactor = constraints.getMaxZoom();
|
2012-05-18 08:24:27 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
2012-11-07 08:47:17 -08:00
|
|
|
scrollBy(mLastZoomFocus.x - detector.getFocusX(),
|
|
|
|
mLastZoomFocus.y - detector.getFocusY());
|
2011-12-13 14:43:08 -08:00
|
|
|
PointF focus = new PointF(detector.getFocusX(), detector.getFocusY());
|
2012-08-07 07:39:04 -07:00
|
|
|
scaleWithFocus(newZoomFactor, focus);
|
2011-12-13 14:43:08 -08:00
|
|
|
}
|
2011-11-23 11:07:47 -08:00
|
|
|
|
|
|
|
mLastZoomFocus.set(detector.getFocusX(), detector.getFocusY());
|
|
|
|
|
2012-10-02 13:18:21 -07:00
|
|
|
GeckoEvent event = GeckoEvent.createNativeGestureEvent(GeckoEvent.ACTION_MAGNIFY, mLastZoomFocus, getMetrics().zoomFactor);
|
|
|
|
GeckoAppShell.sendEventToGecko(event);
|
|
|
|
|
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
|
2013-01-09 08:21:04 -08:00
|
|
|
mTarget.forceRedraw();
|
2012-10-02 13:18:21 -07:00
|
|
|
|
|
|
|
PointF point = new PointF(detector.getFocusX(), detector.getFocusY());
|
|
|
|
GeckoEvent event = GeckoEvent.createNativeGestureEvent(GeckoEvent.ACTION_MAGNIFY_END, point, getMetrics().zoomFactor);
|
2012-10-31 09:39:44 -07:00
|
|
|
|
|
|
|
if (event == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-02 13:18:21 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(event);
|
2012-08-01 07:38:30 -07:00
|
|
|
}
|
|
|
|
|
2012-08-07 07:39:04 -07:00
|
|
|
/**
|
|
|
|
* Scales the viewport, keeping the given focus point in the same place before and after the
|
|
|
|
* scale operation. You must hold the monitor while calling this.
|
|
|
|
*/
|
|
|
|
private void scaleWithFocus(float zoomFactor, PointF focus) {
|
2012-11-07 08:47:08 -08:00
|
|
|
ImmutableViewportMetrics viewportMetrics = getMetrics();
|
|
|
|
viewportMetrics = viewportMetrics.scaleTo(zoomFactor, focus);
|
|
|
|
mTarget.setViewportMetrics(viewportMetrics);
|
2012-08-07 07:39:04 -07: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());
|
2012-08-07 07:39:03 -07:00
|
|
|
point = mTarget.convertViewPointToLayerPoint(point);
|
2011-11-18 10:28:17 -08:00
|
|
|
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-08-07 07:39:03 -07:00
|
|
|
if (!mTarget.getZoomConstraints().getAllowZoom()) {
|
2012-07-03 07:04:10 -07:00
|
|
|
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-08-07 07:39:03 -07:00
|
|
|
if (mTarget.getZoomConstraints().getAllowZoom()) {
|
2012-07-03 07:04:10 -07:00
|
|
|
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-08-07 07:39:03 -07:00
|
|
|
if (mTarget.getZoomConstraints().getAllowZoom()) {
|
2012-07-03 07:04:10 -07:00
|
|
|
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) {
|
2012-08-07 07:39:03 -07:00
|
|
|
final float startZoom = getMetrics().zoomFactor;
|
2011-11-15 13:41:19 -08:00
|
|
|
|
2012-08-07 07:39:03 -07:00
|
|
|
RectF viewport = getMetrics().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
|
|
|
|
2012-11-07 08:47:08 -08:00
|
|
|
ImmutableViewportMetrics finalMetrics = getMetrics();
|
|
|
|
finalMetrics = finalMetrics.setViewportOrigin(
|
|
|
|
zoomToRect.left * finalMetrics.zoomFactor,
|
|
|
|
zoomToRect.top * finalMetrics.zoomFactor);
|
|
|
|
finalMetrics = 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.
|
2012-11-07 08:47:08 -08:00
|
|
|
finalMetrics = getValidViewportMetrics(finalMetrics);
|
2012-02-02 06:15:37 -08:00
|
|
|
|
2013-01-09 11:37:01 -08:00
|
|
|
bounce(finalMetrics, PanZoomState.ANIMATED_ZOOM);
|
2011-11-15 13:41:19 -08:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-18 17:58:56 -07:00
|
|
|
|
|
|
|
/** This function must be called from the UI thread. */
|
|
|
|
public void abortPanning() {
|
|
|
|
checkMainThread();
|
|
|
|
bounce();
|
|
|
|
}
|
2012-09-12 13:36:36 -07:00
|
|
|
|
|
|
|
public void setOverScrollMode(int overscrollMode) {
|
|
|
|
mX.setOverScrollMode(overscrollMode);
|
|
|
|
mY.setOverScrollMode(overscrollMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getOverScrollMode() {
|
|
|
|
return mX.getOverScrollMode();
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|