2013-02-08 14:12:59 +00:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.gfx;
|
|
|
|
|
|
|
|
import org.mozilla.gecko.GeckoAppShell;
|
|
|
|
import org.mozilla.gecko.GeckoEvent;
|
2013-04-14 13:27:02 +00:00
|
|
|
import org.mozilla.gecko.PrefsHelper;
|
2013-02-08 14:13:09 +00:00
|
|
|
import org.mozilla.gecko.Tab;
|
|
|
|
import org.mozilla.gecko.Tabs;
|
2013-02-08 14:12:59 +00:00
|
|
|
import org.mozilla.gecko.ZoomConstraints;
|
2014-02-11 17:16:00 +00:00
|
|
|
import org.mozilla.gecko.EventDispatcher;
|
2013-02-08 14:12:59 +00:00
|
|
|
import org.mozilla.gecko.util.FloatUtils;
|
2013-04-03 15:58:59 +00:00
|
|
|
import org.mozilla.gecko.util.GamepadUtils;
|
2013-02-08 14:12:59 +00:00
|
|
|
import org.mozilla.gecko.util.GeckoEventListener;
|
2013-03-15 10:52:53 +00:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import android.graphics.PointF;
|
|
|
|
import android.graphics.RectF;
|
2013-02-08 14:13:09 +00:00
|
|
|
import android.os.Build;
|
2013-02-08 14:12:59 +00:00
|
|
|
import android.util.FloatMath;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.GestureDetector;
|
2013-02-08 14:13:09 +00:00
|
|
|
import android.view.InputDevice;
|
2013-04-08 07:51:56 +00:00
|
|
|
import android.view.KeyEvent;
|
2013-02-08 14:12:59 +00:00
|
|
|
import android.view.MotionEvent;
|
2013-02-08 14:13:09 +00:00
|
|
|
import android.view.View;
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handles the kinetic scrolling and zooming physics for a layer controller.
|
|
|
|
*
|
|
|
|
* Many ideas are from Joe Hewitt's Scrollability:
|
|
|
|
* https://github.com/joehewitt/scrollability/
|
|
|
|
*/
|
2013-02-08 14:13:09 +00:00
|
|
|
class JavaPanZoomController
|
2013-02-08 14:12:59 +00:00
|
|
|
extends GestureDetector.SimpleOnGestureListener
|
|
|
|
implements PanZoomController, SimpleScaleGestureDetector.SimpleScaleGestureListener, GeckoEventListener
|
|
|
|
{
|
|
|
|
private static final String LOGTAG = "GeckoPanZoomController";
|
|
|
|
|
|
|
|
private static String MESSAGE_ZOOM_RECT = "Browser:ZoomToRect";
|
|
|
|
private static String MESSAGE_ZOOM_PAGE = "Browser:ZoomToPageWidth";
|
2013-02-08 14:13:09 +00:00
|
|
|
private static String MESSAGE_TOUCH_LISTENER = "Tab:HasTouchListener";
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
// Animation stops if the velocity is below this value when overscrolled or panning.
|
|
|
|
private static final float STOPPED_THRESHOLD = 4.0f;
|
|
|
|
|
|
|
|
// Animation stops is the velocity is below this threshold when flinging.
|
|
|
|
private static final float FLING_STOPPED_THRESHOLD = 0.1f;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// Angle from axis within which we stay axis-locked
|
|
|
|
private static final double AXIS_LOCK_ANGLE = Math.PI / 6.0; // 30 degrees
|
|
|
|
|
2013-04-14 13:27:02 +00:00
|
|
|
// Axis-lock breakout angle
|
|
|
|
private static final double AXIS_BREAKOUT_ANGLE = Math.PI / 8.0;
|
|
|
|
|
|
|
|
// The distance the user has to pan before we consider breaking out of a locked axis
|
|
|
|
public static final float AXIS_BREAKOUT_THRESHOLD = 1/32f * GeckoAppShell.getDpi();
|
|
|
|
|
2013-02-08 14:12:59 +00:00
|
|
|
// The maximum amount we allow you to zoom into a page
|
|
|
|
private static final float MAX_ZOOM = 4.0f;
|
|
|
|
|
|
|
|
// The maximum amount we would like to scroll with the mouse
|
|
|
|
private static final float MAX_SCROLL = 0.075f * GeckoAppShell.getDpi();
|
|
|
|
|
2013-03-28 21:31:09 +00:00
|
|
|
// The maximum zoom factor adjustment per frame of the AUTONAV animation
|
|
|
|
private static final float MAX_ZOOM_DELTA = 0.125f;
|
|
|
|
|
2013-09-04 18:07:12 +00:00
|
|
|
// The duration of the bounce animation in ns
|
|
|
|
private static final int BOUNCE_ANIMATION_DURATION = 250000000;
|
2013-04-01 14:16:00 +00:00
|
|
|
|
2013-02-08 14:12:59 +00:00
|
|
|
private enum PanZoomState {
|
2013-04-14 13:27:02 +00:00
|
|
|
NOTHING, /* no touch-start events received */
|
|
|
|
FLING, /* all touches removed, but we're still scrolling page */
|
|
|
|
TOUCHING, /* one touch-start event received */
|
|
|
|
PANNING_LOCKED_X, /* touch-start followed by move (i.e. panning with axis lock) X axis */
|
|
|
|
PANNING_LOCKED_Y, /* as above for Y axis */
|
|
|
|
PANNING, /* panning without axis lock */
|
|
|
|
PANNING_HOLD, /* in panning, but not moving.
|
|
|
|
* similar to TOUCHING but after starting a pan */
|
|
|
|
PANNING_HOLD_LOCKED_X, /* like PANNING_HOLD, but axis lock still in effect for X axis */
|
|
|
|
PANNING_HOLD_LOCKED_Y, /* as above but for Y axis */
|
|
|
|
PINCHING, /* nth touch-start, where n > 1. this mode allows pan and zoom */
|
|
|
|
ANIMATED_ZOOM, /* animated zoom to a new rect */
|
|
|
|
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. */
|
|
|
|
AUTONAV, /* We are scrolling using an AutonavRunnable animation. This is similar
|
|
|
|
to the FLING state except that it must be stopped manually by the code that
|
|
|
|
started it, and it's velocity can be updated while it's running. */
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum AxisLockMode {
|
|
|
|
STANDARD, /* Default axis locking mode that doesn't break out until finger release */
|
|
|
|
FREE, /* No locking at all */
|
|
|
|
STICKY /* Break out with hysteresis so that it feels as free as possible whilst locking */
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private final PanZoomTarget mTarget;
|
|
|
|
private final SubdocumentScrollHelper mSubscroller;
|
|
|
|
private final Axis mX;
|
|
|
|
private final Axis mY;
|
2013-02-08 14:13:09 +00:00
|
|
|
private final TouchEventHandler mTouchEventHandler;
|
2013-02-08 14:12:59 +00:00
|
|
|
private final EventDispatcher mEventDispatcher;
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
/* The task that handles flings, autonav or bounces. */
|
|
|
|
private PanZoomRenderTask mAnimationRenderTask;
|
2013-02-08 14:12:59 +00:00
|
|
|
/* 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. */
|
|
|
|
private PanZoomState mState;
|
2013-03-28 21:31:09 +00:00
|
|
|
/* The per-frame zoom delta for the currently-running AUTONAV animation. */
|
|
|
|
private float mAutonavZoomDelta;
|
2013-04-14 13:27:02 +00:00
|
|
|
/* The user selected panning mode */
|
|
|
|
private AxisLockMode mMode;
|
2013-06-03 14:05:59 +00:00
|
|
|
/* A medium-length tap/press is happening */
|
|
|
|
private boolean mMediumPress;
|
2013-08-28 01:01:40 +00:00
|
|
|
/* Used to change the scrollY direction */
|
|
|
|
private boolean mNegateWheelScrollY;
|
2014-02-20 14:36:48 +00:00
|
|
|
/* Whether the current event has been default-prevented. */
|
|
|
|
private boolean mDefaultPrevented;
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-09-27 05:57:57 +00:00
|
|
|
// Handler to be notified when overscroll occurs
|
|
|
|
private Overscroll mOverscroll;
|
|
|
|
|
2013-02-08 14:13:09 +00:00
|
|
|
public JavaPanZoomController(PanZoomTarget target, View view, EventDispatcher eventDispatcher) {
|
2013-02-08 14:12:59 +00:00
|
|
|
mTarget = target;
|
|
|
|
mSubscroller = new SubdocumentScrollHelper(eventDispatcher);
|
|
|
|
mX = new AxisX(mSubscroller);
|
|
|
|
mY = new AxisY(mSubscroller);
|
2013-02-08 14:13:09 +00:00
|
|
|
mTouchEventHandler = new TouchEventHandler(view.getContext(), view, this);
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
checkMainThread();
|
|
|
|
|
|
|
|
setState(PanZoomState.NOTHING);
|
|
|
|
|
|
|
|
mEventDispatcher = eventDispatcher;
|
2014-05-02 18:34:40 +00:00
|
|
|
mEventDispatcher.registerGeckoThreadListener(this,
|
|
|
|
MESSAGE_ZOOM_RECT,
|
|
|
|
MESSAGE_ZOOM_PAGE,
|
|
|
|
MESSAGE_TOUCH_LISTENER);
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-04-14 13:27:02 +00:00
|
|
|
mMode = AxisLockMode.STANDARD;
|
|
|
|
|
2013-09-05 01:58:40 +00:00
|
|
|
String[] prefs = { "ui.scrolling.axis_lock_mode",
|
|
|
|
"ui.scrolling.negate_wheel_scrollY",
|
|
|
|
"ui.scrolling.gamepad_dead_zone" };
|
2013-08-28 01:01:40 +00:00
|
|
|
mNegateWheelScrollY = false;
|
|
|
|
PrefsHelper.getPrefs(prefs, new PrefsHelper.PrefHandlerBase() {
|
2013-04-14 13:27:02 +00:00
|
|
|
@Override public void prefValue(String pref, String value) {
|
2013-08-28 01:01:40 +00:00
|
|
|
if (pref.equals("ui.scrolling.axis_lock_mode")) {
|
|
|
|
if (value.equals("standard")) {
|
|
|
|
mMode = AxisLockMode.STANDARD;
|
|
|
|
} else if (value.equals("free")) {
|
|
|
|
mMode = AxisLockMode.FREE;
|
|
|
|
} else {
|
|
|
|
mMode = AxisLockMode.STICKY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-05 01:58:40 +00:00
|
|
|
|
|
|
|
@Override public void prefValue(String pref, int value) {
|
|
|
|
if (pref.equals("ui.scrolling.gamepad_dead_zone")) {
|
|
|
|
GamepadUtils.overrideDeadZoneThreshold((float)value / 1000f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 01:01:40 +00:00
|
|
|
@Override public void prefValue(String pref, boolean value) {
|
|
|
|
if (pref.equals("ui.scrolling.negate_wheel_scrollY")) {
|
|
|
|
mNegateWheelScrollY = value;
|
2013-04-14 13:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isObserver() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-02-08 14:12:59 +00:00
|
|
|
Axis.initPrefs();
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public void destroy() {
|
2014-05-02 18:34:40 +00:00
|
|
|
mEventDispatcher.unregisterGeckoThreadListener(this,
|
|
|
|
MESSAGE_ZOOM_RECT,
|
|
|
|
MESSAGE_ZOOM_PAGE,
|
|
|
|
MESSAGE_TOUCH_LISTENER);
|
2013-02-08 14:12:59 +00:00
|
|
|
mSubscroller.destroy();
|
2013-02-08 14:13:09 +00:00
|
|
|
mTouchEventHandler.destroy();
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private final static float easeOut(float t) {
|
|
|
|
// ease-out approx.
|
|
|
|
// -(t-1)^2+1
|
|
|
|
t = t-1;
|
|
|
|
return -t*t+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setState(PanZoomState state) {
|
|
|
|
if (state != mState) {
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("PanZoom:StateChange", state.toString()));
|
|
|
|
mState = state;
|
2013-04-25 17:47:08 +00:00
|
|
|
|
|
|
|
// Let the target know we've finished with it (for now)
|
|
|
|
if (state == PanZoomState.NOTHING) {
|
|
|
|
mTarget.panZoomStopped();
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ImmutableViewportMetrics getMetrics() {
|
|
|
|
return mTarget.getViewportMetrics();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkMainThread() {
|
2013-03-15 10:52:53 +00:00
|
|
|
if (!ThreadUtils.isOnUiThread()) {
|
2013-02-08 14:12:59 +00:00
|
|
|
// log with full stack trace
|
|
|
|
Log.e(LOGTAG, "Uh-oh, we're running on the wrong thread!", new Exception());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public void handleMessage(String event, JSONObject message) {
|
|
|
|
try {
|
|
|
|
if (MESSAGE_ZOOM_RECT.equals(event)) {
|
|
|
|
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"));
|
2013-12-12 06:43:44 +00:00
|
|
|
if (message.optBoolean("animate", true)) {
|
|
|
|
mTarget.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
animatedZoomTo(zoomRect);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
mTarget.setViewportMetrics(getMetricsToZoomTo(zoomRect));
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
} else if (MESSAGE_ZOOM_PAGE.equals(event)) {
|
|
|
|
ImmutableViewportMetrics metrics = getMetrics();
|
|
|
|
RectF cssPageRect = metrics.getCssPageRect();
|
|
|
|
|
|
|
|
RectF viewableRect = metrics.getCssViewport();
|
|
|
|
float y = viewableRect.top;
|
|
|
|
// attempt to keep zoom keep focused on the center of the viewport
|
|
|
|
float newHeight = viewableRect.height() * cssPageRect.width() / viewableRect.width();
|
|
|
|
float dh = viewableRect.height() - newHeight; // increase in the height
|
|
|
|
final RectF r = new RectF(0.0f,
|
|
|
|
y + dh/2,
|
|
|
|
cssPageRect.width(),
|
|
|
|
y + dh/2 + newHeight);
|
2013-12-12 06:43:44 +00:00
|
|
|
if (message.optBoolean("animate", true)) {
|
|
|
|
mTarget.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
animatedZoomTo(r);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
mTarget.setViewportMetrics(getMetricsToZoomTo(r));
|
|
|
|
}
|
2013-02-08 14:13:09 +00:00
|
|
|
} else if (MESSAGE_TOUCH_LISTENER.equals(event)) {
|
|
|
|
int tabId = message.getInt("tabID");
|
|
|
|
final Tab tab = Tabs.getInstance().getTab(tabId);
|
|
|
|
tab.setHasTouchListeners(true);
|
|
|
|
mTarget.post(new Runnable() {
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:13:09 +00:00
|
|
|
public void run() {
|
|
|
|
if (Tabs.getInstance().isSelectedTab(tab))
|
|
|
|
mTouchEventHandler.setWaitForTouchListeners(true);
|
|
|
|
}
|
|
|
|
});
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 03:50:48 +00:00
|
|
|
/** This function MUST be called on the UI thread */
|
|
|
|
@Override
|
|
|
|
public boolean onKeyEvent(KeyEvent event) {
|
|
|
|
if (Build.VERSION.SDK_INT <= 11) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((event.getSource() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD
|
|
|
|
&& event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
|
|
|
|
|
switch (event.getKeyCode()) {
|
|
|
|
case KeyEvent.KEYCODE_ZOOM_IN:
|
|
|
|
return animatedScale(0.2f);
|
|
|
|
case KeyEvent.KEYCODE_ZOOM_OUT:
|
|
|
|
return animatedScale(-0.2f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:13:09 +00:00
|
|
|
/** This function MUST be called on the UI thread */
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:13:09 +00:00
|
|
|
public boolean onMotionEvent(MotionEvent event) {
|
|
|
|
if (Build.VERSION.SDK_INT <= 11) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event.getSource() & InputDevice.SOURCE_CLASS_MASK) {
|
|
|
|
case InputDevice.SOURCE_CLASS_POINTER:
|
|
|
|
switch (event.getAction() & MotionEvent.ACTION_MASK) {
|
|
|
|
case MotionEvent.ACTION_SCROLL: return handlePointerScroll(event);
|
|
|
|
}
|
|
|
|
break;
|
2013-03-02 03:50:48 +00:00
|
|
|
case InputDevice.SOURCE_CLASS_JOYSTICK:
|
|
|
|
switch (event.getAction() & MotionEvent.ACTION_MASK) {
|
2013-03-28 21:31:09 +00:00
|
|
|
case MotionEvent.ACTION_MOVE: return handleJoystickNav(event);
|
2013-03-02 03:50:48 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-02-08 14:13:09 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This function MUST be called on the UI thread */
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
2013-02-08 14:13:09 +00:00
|
|
|
return mTouchEventHandler.handleEvent(event);
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:36:48 +00:00
|
|
|
boolean handleEvent(MotionEvent event, boolean defaultPrevented) {
|
|
|
|
mDefaultPrevented = defaultPrevented;
|
|
|
|
|
2013-02-08 14:12:59 +00:00
|
|
|
switch (event.getAction() & MotionEvent.ACTION_MASK) {
|
2013-02-08 14:13:09 +00:00
|
|
|
case MotionEvent.ACTION_DOWN: return handleTouchStart(event);
|
|
|
|
case MotionEvent.ACTION_MOVE: return handleTouchMove(event);
|
|
|
|
case MotionEvent.ACTION_UP: return handleTouchEnd(event);
|
|
|
|
case MotionEvent.ACTION_CANCEL: return handleTouchCancel(event);
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
2013-02-08 14:13:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This function MUST be called on the UI thread */
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:13:09 +00:00
|
|
|
public void notifyDefaultActionPrevented(boolean prevented) {
|
|
|
|
mTouchEventHandler.handleEventListenerAction(!prevented);
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** This function must be called from the UI thread. */
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public void abortAnimation() {
|
|
|
|
checkMainThread();
|
|
|
|
// 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.
|
|
|
|
switch (mState) {
|
|
|
|
case FLING:
|
|
|
|
mX.stopFling();
|
|
|
|
mY.stopFling();
|
|
|
|
// fall through
|
|
|
|
case BOUNCE:
|
|
|
|
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
|
|
|
|
setState(PanZoomState.NOTHING);
|
|
|
|
// fall through
|
|
|
|
case NOTHING:
|
|
|
|
// Don't do animations here; they're distracting and can cause flashes on page
|
|
|
|
// transitions.
|
|
|
|
synchronized (mTarget.getLock()) {
|
|
|
|
mTarget.setViewportMetrics(getValidViewportMetrics());
|
2013-04-26 17:26:46 +00:00
|
|
|
mTarget.forceRedraw(null);
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This function must be called on the UI thread. */
|
|
|
|
public void startingNewEventBlock(MotionEvent event, boolean waitingForTouchListeners) {
|
|
|
|
checkMainThread();
|
|
|
|
mSubscroller.cancel();
|
|
|
|
if (waitingForTouchListeners && (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
|
|
|
|
// 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
|
|
|
|
setState(PanZoomState.WAITING_LISTENERS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This must be called on the UI thread. */
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public void pageRectUpdated() {
|
|
|
|
if (mState == PanZoomState.NOTHING) {
|
|
|
|
synchronized (mTarget.getLock()) {
|
|
|
|
ImmutableViewportMetrics validated = getValidViewportMetrics();
|
|
|
|
if (!getMetrics().fuzzyEquals(validated)) {
|
|
|
|
// page size changed such that we are now in overscroll. snap to the
|
|
|
|
// the nearest valid viewport
|
|
|
|
mTarget.setViewportMetrics(validated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Panning/scrolling
|
|
|
|
*/
|
|
|
|
|
2013-02-08 14:13:09 +00:00
|
|
|
private boolean handleTouchStart(MotionEvent event) {
|
2013-02-08 14:12:59 +00:00
|
|
|
// user is taking control of movement, so stop
|
|
|
|
// any auto-movement we have going
|
2013-09-04 18:07:11 +00:00
|
|
|
stopAnimationTask();
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case ANIMATED_ZOOM:
|
|
|
|
// 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-04-26 17:26:46 +00:00
|
|
|
mTarget.forceRedraw(null);
|
2013-02-08 14:12:59 +00:00
|
|
|
// fall through
|
|
|
|
case FLING:
|
2013-03-28 21:31:05 +00:00
|
|
|
case AUTONAV:
|
2013-02-08 14:12:59 +00:00
|
|
|
case BOUNCE:
|
|
|
|
case NOTHING:
|
|
|
|
case WAITING_LISTENERS:
|
|
|
|
startTouch(event.getX(0), event.getY(0), event.getEventTime());
|
|
|
|
return false;
|
|
|
|
case TOUCHING:
|
|
|
|
case PANNING:
|
2013-04-14 13:27:02 +00:00
|
|
|
case PANNING_LOCKED_X:
|
|
|
|
case PANNING_LOCKED_Y:
|
2013-02-08 14:12:59 +00:00
|
|
|
case PANNING_HOLD:
|
2013-04-14 13:27:02 +00:00
|
|
|
case PANNING_HOLD_LOCKED_X:
|
|
|
|
case PANNING_HOLD_LOCKED_Y:
|
2013-02-08 14:12:59 +00:00
|
|
|
case PINCHING:
|
|
|
|
Log.e(LOGTAG, "Received impossible touch down while in " + mState);
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-08 14:13:09 +00:00
|
|
|
Log.e(LOGTAG, "Unhandled case " + mState + " in handleTouchStart");
|
2013-02-08 14:12:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:13:09 +00:00
|
|
|
private boolean handleTouchMove(MotionEvent event) {
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case FLING:
|
2013-03-28 21:31:05 +00:00
|
|
|
case AUTONAV:
|
2013-02-08 14:12:59 +00:00
|
|
|
case BOUNCE:
|
|
|
|
case WAITING_LISTENERS:
|
|
|
|
// should never happen
|
|
|
|
Log.e(LOGTAG, "Received impossible touch move while in " + mState);
|
|
|
|
// 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.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TOUCHING:
|
2014-05-07 19:22:14 +00:00
|
|
|
// Don't allow panning if there is a non-root element in full-screen mode. See bug 775511 and bug 859683.
|
|
|
|
if (mTarget.getFullScreenState() == FullScreenState.NON_ROOT_ELEMENT && !mSubscroller.scrolling()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (panDistance(event) < PAN_THRESHOLD) {
|
2013-02-08 14:12:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cancelTouch();
|
|
|
|
startPanning(event.getX(0), event.getY(0), event.getEventTime());
|
|
|
|
track(event);
|
|
|
|
return true;
|
|
|
|
|
2013-04-14 13:27:02 +00:00
|
|
|
case PANNING_HOLD_LOCKED_X:
|
|
|
|
setState(PanZoomState.PANNING_LOCKED_X);
|
|
|
|
track(event);
|
|
|
|
return true;
|
|
|
|
case PANNING_HOLD_LOCKED_Y:
|
|
|
|
setState(PanZoomState.PANNING_LOCKED_Y);
|
2013-02-08 14:12:59 +00:00
|
|
|
// fall through
|
2013-04-14 13:27:02 +00:00
|
|
|
case PANNING_LOCKED_X:
|
|
|
|
case PANNING_LOCKED_Y:
|
2013-02-08 14:12:59 +00:00
|
|
|
track(event);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case PANNING_HOLD:
|
|
|
|
setState(PanZoomState.PANNING);
|
|
|
|
// fall through
|
|
|
|
case PANNING:
|
|
|
|
track(event);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case PINCHING:
|
|
|
|
// scale gesture listener will handle this
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-08 14:13:09 +00:00
|
|
|
Log.e(LOGTAG, "Unhandled case " + mState + " in handleTouchMove");
|
2013-02-08 14:12:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:13:09 +00:00
|
|
|
private boolean handleTouchEnd(MotionEvent event) {
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case FLING:
|
2013-03-28 21:31:05 +00:00
|
|
|
case AUTONAV:
|
2013-02-08 14:12:59 +00:00
|
|
|
case BOUNCE:
|
|
|
|
case ANIMATED_ZOOM:
|
|
|
|
case NOTHING:
|
|
|
|
// may happen if user double-taps and drags without lifting after the
|
|
|
|
// second tap. ignore if this happens.
|
|
|
|
return false;
|
|
|
|
|
2014-02-20 14:36:48 +00:00
|
|
|
case WAITING_LISTENERS:
|
|
|
|
if (!mDefaultPrevented) {
|
|
|
|
// should never happen
|
|
|
|
Log.e(LOGTAG, "Received impossible touch end while in " + mState);
|
|
|
|
}
|
|
|
|
// fall through
|
2013-02-08 14:12:59 +00:00
|
|
|
case TOUCHING:
|
|
|
|
// 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
|
|
|
|
bounce();
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case PANNING:
|
2013-04-14 13:27:02 +00:00
|
|
|
case PANNING_LOCKED_X:
|
|
|
|
case PANNING_LOCKED_Y:
|
2013-02-08 14:12:59 +00:00
|
|
|
case PANNING_HOLD:
|
2013-04-14 13:27:02 +00:00
|
|
|
case PANNING_HOLD_LOCKED_X:
|
|
|
|
case PANNING_HOLD_LOCKED_Y:
|
2013-02-08 14:12:59 +00:00
|
|
|
setState(PanZoomState.FLING);
|
|
|
|
fling();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case PINCHING:
|
|
|
|
setState(PanZoomState.NOTHING);
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-08 14:13:09 +00:00
|
|
|
Log.e(LOGTAG, "Unhandled case " + mState + " in handleTouchEnd");
|
2013-02-08 14:12:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:13:09 +00:00
|
|
|
private boolean handleTouchCancel(MotionEvent event) {
|
2013-02-08 14:12:59 +00:00
|
|
|
cancelTouch();
|
|
|
|
|
|
|
|
// ensure we snap back if we're overscrolled
|
|
|
|
bounce();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:13:09 +00:00
|
|
|
private boolean handlePointerScroll(MotionEvent event) {
|
2013-02-08 14:12:59 +00:00
|
|
|
if (mState == PanZoomState.NOTHING || mState == PanZoomState.FLING) {
|
|
|
|
float scrollX = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
|
|
|
|
float scrollY = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
|
2013-08-28 01:01:40 +00:00
|
|
|
if (mNegateWheelScrollY) {
|
|
|
|
scrollY *= -1.0;
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
scrollBy(scrollX * MAX_SCROLL, scrollY * MAX_SCROLL);
|
|
|
|
bounce();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-03 15:58:59 +00:00
|
|
|
private float filterDeadZone(MotionEvent event, int axis) {
|
|
|
|
return (GamepadUtils.isValueInDeadZone(event, axis) ? 0 : event.getAxisValue(axis));
|
2013-03-28 21:31:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-03 15:58:59 +00:00
|
|
|
private float normalizeJoystickScroll(MotionEvent event, int axis) {
|
|
|
|
return filterDeadZone(event, axis) * MAX_SCROLL;
|
2013-03-28 21:31:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-03 15:58:59 +00:00
|
|
|
private float normalizeJoystickZoom(MotionEvent event, int axis) {
|
2013-03-28 21:31:09 +00:00
|
|
|
// negate MAX_ZOOM_DELTA so that pushing up on the stick zooms in
|
2013-04-03 15:58:59 +00:00
|
|
|
return filterDeadZone(event, axis) * -MAX_ZOOM_DELTA;
|
2013-03-02 03:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since this event is a position-based event rather than a motion-based event, we need to
|
2013-03-28 21:31:05 +00:00
|
|
|
// set up an AUTONAV animation to keep scrolling even while we don't get events.
|
2013-03-28 21:31:09 +00:00
|
|
|
private boolean handleJoystickNav(MotionEvent event) {
|
2013-04-03 15:58:59 +00:00
|
|
|
float velocityX = normalizeJoystickScroll(event, MotionEvent.AXIS_X);
|
|
|
|
float velocityY = normalizeJoystickScroll(event, MotionEvent.AXIS_Y);
|
|
|
|
float zoomDelta = normalizeJoystickZoom(event, MotionEvent.AXIS_RZ);
|
2013-03-28 21:31:09 +00:00
|
|
|
|
|
|
|
if (velocityX == 0 && velocityY == 0 && zoomDelta == 0) {
|
2013-03-28 21:31:05 +00:00
|
|
|
if (mState == PanZoomState.AUTONAV) {
|
2013-03-02 03:50:48 +00:00
|
|
|
bounce(); // if not needed, this will automatically go to state NOTHING
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == PanZoomState.NOTHING) {
|
2013-03-28 21:31:05 +00:00
|
|
|
setState(PanZoomState.AUTONAV);
|
2013-09-04 18:07:11 +00:00
|
|
|
startAnimationRenderTask(new AutonavRenderTask());
|
2013-03-02 03:50:48 +00:00
|
|
|
}
|
2013-03-28 21:31:05 +00:00
|
|
|
if (mState == PanZoomState.AUTONAV) {
|
2013-03-02 03:50:48 +00:00
|
|
|
mX.setAutoscrollVelocity(velocityX);
|
|
|
|
mY.setAutoscrollVelocity(velocityY);
|
2013-03-28 21:31:09 +00:00
|
|
|
mAutonavZoomDelta = zoomDelta;
|
2013-03-02 03:50:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:12:59 +00:00
|
|
|
private void startTouch(float x, float y, long time) {
|
|
|
|
mX.startTouch(x);
|
|
|
|
mY.startTouch(y);
|
|
|
|
setState(PanZoomState.TOUCHING);
|
|
|
|
mLastEventTime = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-04-14 13:27:02 +00:00
|
|
|
if (mMode == AxisLockMode.STANDARD || mMode == AxisLockMode.STICKY) {
|
|
|
|
if (!mX.scrollable() || !mY.scrollable()) {
|
|
|
|
setState(PanZoomState.PANNING);
|
|
|
|
} else if (angle < AXIS_LOCK_ANGLE || angle > (Math.PI - AXIS_LOCK_ANGLE)) {
|
|
|
|
mY.setScrollingDisabled(true);
|
|
|
|
setState(PanZoomState.PANNING_LOCKED_X);
|
|
|
|
} else if (Math.abs(angle - (Math.PI / 2)) < AXIS_LOCK_ANGLE) {
|
|
|
|
mX.setScrollingDisabled(true);
|
|
|
|
setState(PanZoomState.PANNING_LOCKED_Y);
|
|
|
|
} else {
|
|
|
|
setState(PanZoomState.PANNING);
|
|
|
|
}
|
|
|
|
} else if (mMode == AxisLockMode.FREE) {
|
2013-02-08 14:12:59 +00:00
|
|
|
setState(PanZoomState.PANNING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private float panDistance(MotionEvent move) {
|
|
|
|
float dx = mX.panDistance(move.getX(0));
|
|
|
|
float dy = mY.panDistance(move.getY(0));
|
|
|
|
return FloatMath.sqrt(dx * dx + dy * dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void track(float x, float y, long time) {
|
|
|
|
float timeDelta = (float)(time - mLastEventTime);
|
|
|
|
if (FloatUtils.fuzzyEquals(timeDelta, 0)) {
|
|
|
|
// probably a duplicate event, ignore it. using a zero timeDelta will mess
|
|
|
|
// up our velocity
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mLastEventTime = time;
|
|
|
|
|
2013-04-14 13:27:02 +00:00
|
|
|
|
|
|
|
// if we're axis-locked check if the user is trying to scroll away from the lock
|
|
|
|
if (mMode == AxisLockMode.STICKY) {
|
|
|
|
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]
|
|
|
|
|
|
|
|
if (Math.abs(dx) > AXIS_BREAKOUT_THRESHOLD || Math.abs(dy) > AXIS_BREAKOUT_THRESHOLD) {
|
|
|
|
if (mState == PanZoomState.PANNING_LOCKED_X) {
|
|
|
|
if (angle > AXIS_BREAKOUT_ANGLE && angle < (Math.PI - AXIS_BREAKOUT_ANGLE)) {
|
|
|
|
mY.setScrollingDisabled(false);
|
|
|
|
setState(PanZoomState.PANNING);
|
|
|
|
}
|
|
|
|
} else if (mState == PanZoomState.PANNING_LOCKED_Y) {
|
|
|
|
if (Math.abs(angle - (Math.PI / 2)) > AXIS_BREAKOUT_ANGLE) {
|
|
|
|
mX.setScrollingDisabled(false);
|
|
|
|
setState(PanZoomState.PANNING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:12:59 +00:00
|
|
|
mX.updateWithTouchAt(x, timeDelta);
|
|
|
|
mY.updateWithTouchAt(y, timeDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void track(MotionEvent event) {
|
|
|
|
mX.saveTouchPos();
|
|
|
|
mY.saveTouchPos();
|
|
|
|
|
|
|
|
for (int i = 0; i < event.getHistorySize(); i++) {
|
|
|
|
track(event.getHistoricalX(0, i),
|
|
|
|
event.getHistoricalY(0, i),
|
|
|
|
event.getHistoricalEventTime(i));
|
|
|
|
}
|
|
|
|
track(event.getX(0), event.getY(0), event.getEventTime());
|
|
|
|
|
|
|
|
if (stopped()) {
|
|
|
|
if (mState == PanZoomState.PANNING) {
|
|
|
|
setState(PanZoomState.PANNING_HOLD);
|
2013-04-14 13:27:02 +00:00
|
|
|
} else if (mState == PanZoomState.PANNING_LOCKED_X) {
|
|
|
|
setState(PanZoomState.PANNING_HOLD_LOCKED_X);
|
|
|
|
} else if (mState == PanZoomState.PANNING_LOCKED_Y) {
|
|
|
|
setState(PanZoomState.PANNING_HOLD_LOCKED_Y);
|
2013-02-08 14:12:59 +00:00
|
|
|
} else {
|
|
|
|
// should never happen, but handle anyway for robustness
|
|
|
|
Log.e(LOGTAG, "Impossible case " + mState + " when stopped in track");
|
2013-04-14 13:27:02 +00:00
|
|
|
setState(PanZoomState.PANNING_HOLD);
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mX.startPan();
|
|
|
|
mY.startPan();
|
|
|
|
updatePosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void scrollBy(float dx, float dy) {
|
2013-04-25 17:47:08 +00:00
|
|
|
mTarget.scrollBy(dx, dy);
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void fling() {
|
|
|
|
updatePosition();
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
stopAnimationTask();
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
boolean stopped = stopped();
|
|
|
|
mX.startFling(stopped);
|
|
|
|
mY.startFling(stopped);
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
startAnimationRenderTask(new FlingRenderTask());
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Performs a bounce-back animation to the given viewport metrics. */
|
|
|
|
private void bounce(ImmutableViewportMetrics metrics, PanZoomState state) {
|
2013-09-04 18:07:11 +00:00
|
|
|
stopAnimationTask();
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
ImmutableViewportMetrics bounceStartMetrics = getMetrics();
|
|
|
|
if (bounceStartMetrics.fuzzyEquals(metrics)) {
|
|
|
|
setState(PanZoomState.NOTHING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(state);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
mTarget.setAnimationTarget(metrics);
|
2013-09-04 18:07:11 +00:00
|
|
|
startAnimationRenderTask(new BounceRenderTask(bounceStartMetrics, metrics));
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Performs a bounce-back animation to the nearest valid viewport metrics. */
|
|
|
|
private void bounce() {
|
|
|
|
bounce(getValidViewportMetrics(), PanZoomState.BOUNCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Starts the fling or bounce animation. */
|
2013-09-04 18:07:11 +00:00
|
|
|
private void startAnimationRenderTask(final PanZoomRenderTask task) {
|
|
|
|
if (mAnimationRenderTask != null) {
|
|
|
|
Log.e(LOGTAG, "Attempted to start a new task without canceling the old one!");
|
|
|
|
stopAnimationTask();
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
mAnimationRenderTask = task;
|
|
|
|
mTarget.postRenderTask(mAnimationRenderTask);
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Stops the fling or bounce animation. */
|
2013-09-04 18:07:11 +00:00
|
|
|
private void stopAnimationTask() {
|
|
|
|
if (mAnimationRenderTask != null) {
|
|
|
|
mAnimationRenderTask.terminate();
|
|
|
|
mTarget.removeRenderTask(mAnimationRenderTask);
|
|
|
|
mAnimationRenderTask = null;
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private float getVelocity() {
|
|
|
|
float xvel = mX.getRealVelocity();
|
|
|
|
float yvel = mY.getRealVelocity();
|
|
|
|
return FloatMath.sqrt(xvel * xvel + yvel * yvel);
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public PointF getVelocityVector() {
|
|
|
|
return new PointF(mX.getRealVelocity(), mY.getRealVelocity());
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean stopped() {
|
|
|
|
return getVelocity() < STOPPED_THRESHOLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointF resetDisplacement() {
|
|
|
|
return new PointF(mX.resetDisplacement(), mY.resetDisplacement());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePosition() {
|
|
|
|
mX.displace();
|
|
|
|
mY.displace();
|
|
|
|
PointF displacement = resetDisplacement();
|
|
|
|
if (FloatUtils.fuzzyEquals(displacement.x, 0.0f) && FloatUtils.fuzzyEquals(displacement.y, 0.0f)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-20 14:36:48 +00:00
|
|
|
if (mDefaultPrevented || mSubscroller.scrollBy(displacement)) {
|
2013-07-15 16:03:24 +00:00
|
|
|
synchronized (mTarget.getLock()) {
|
2014-02-20 14:36:48 +00:00
|
|
|
mTarget.scrollMarginsBy(displacement.x, displacement.y);
|
2013-07-15 16:03:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-02-08 14:12:59 +00:00
|
|
|
synchronized (mTarget.getLock()) {
|
|
|
|
scrollBy(displacement.x, displacement.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
/**
|
|
|
|
* This class is an implementation of RenderTask which enforces its implementor to run in the UI thread.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private abstract class PanZoomRenderTask extends RenderTask {
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-09-04 18:07:12 +00:00
|
|
|
/**
|
|
|
|
* the time when the current frame was started in ns.
|
|
|
|
*/
|
|
|
|
protected long mCurrentFrameStartTime;
|
2013-09-04 18:07:12 +00:00
|
|
|
/**
|
|
|
|
* The current frame duration in ns.
|
|
|
|
*/
|
|
|
|
protected long mLastFrameTimeDelta;
|
2013-09-04 18:07:12 +00:00
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
private final Runnable mRunnable = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public final void run() {
|
|
|
|
if (mContinueAnimation) {
|
|
|
|
animateFrame();
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
2013-09-04 18:07:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private boolean mContinueAnimation = true;
|
|
|
|
|
|
|
|
public PanZoomRenderTask() {
|
|
|
|
super(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected final boolean internalRun(long timeDelta, long currentFrameStartTime) {
|
|
|
|
|
2013-09-04 18:07:12 +00:00
|
|
|
mCurrentFrameStartTime = currentFrameStartTime;
|
2013-09-04 18:07:12 +00:00
|
|
|
mLastFrameTimeDelta = timeDelta;
|
2013-09-04 18:07:12 +00:00
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
mTarget.post(mRunnable);
|
|
|
|
return mContinueAnimation;
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
/**
|
|
|
|
* The method subclasses must override. This method is run on the UI thread thanks to internalRun
|
|
|
|
*/
|
2013-02-08 14:12:59 +00:00
|
|
|
protected abstract void animateFrame();
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
/**
|
|
|
|
* Terminate the animation.
|
|
|
|
*/
|
|
|
|
public void terminate() {
|
|
|
|
mContinueAnimation = false;
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
private class AutonavRenderTask extends PanZoomRenderTask {
|
|
|
|
public AutonavRenderTask() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:20:57 +00:00
|
|
|
@Override
|
2013-03-02 03:50:48 +00:00
|
|
|
protected void animateFrame() {
|
2013-03-28 21:31:05 +00:00
|
|
|
if (mState != PanZoomState.AUTONAV) {
|
2013-03-02 03:50:48 +00:00
|
|
|
finishAnimation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePosition();
|
2013-03-28 21:31:09 +00:00
|
|
|
synchronized (mTarget.getLock()) {
|
|
|
|
mTarget.setViewportMetrics(applyZoomDelta(getMetrics(), mAutonavZoomDelta));
|
|
|
|
}
|
2013-03-02 03:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
/* The task that performs the bounce animation. */
|
|
|
|
private class BounceRenderTask extends PanZoomRenderTask {
|
|
|
|
|
2013-02-08 14:12:59 +00:00
|
|
|
/*
|
|
|
|
* The viewport metrics that represent the start and end of the bounce-back animation,
|
|
|
|
* respectively.
|
|
|
|
*/
|
|
|
|
private ImmutableViewportMetrics mBounceStartMetrics;
|
|
|
|
private ImmutableViewportMetrics mBounceEndMetrics;
|
2013-09-04 18:07:12 +00:00
|
|
|
// How long ago this bounce was started in ns.
|
|
|
|
private long mBounceDuration;
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
BounceRenderTask(ImmutableViewportMetrics startMetrics, ImmutableViewportMetrics endMetrics) {
|
|
|
|
super();
|
2013-02-08 14:12:59 +00:00
|
|
|
mBounceStartMetrics = startMetrics;
|
|
|
|
mBounceEndMetrics = endMetrics;
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
protected void animateFrame() {
|
|
|
|
/*
|
|
|
|
* 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.BOUNCE || mState == PanZoomState.ANIMATED_ZOOM)) {
|
|
|
|
finishAnimation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the next frame of the bounce-back animation. */
|
2013-09-04 18:07:12 +00:00
|
|
|
mBounceDuration = mCurrentFrameStartTime - getStartTime();
|
|
|
|
if (mBounceDuration < BOUNCE_ANIMATION_DURATION) {
|
2013-02-08 14:12:59 +00:00
|
|
|
advanceBounce();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, if there's nothing else to do, complete the animation and go to sleep. */
|
|
|
|
finishBounce();
|
|
|
|
finishAnimation();
|
|
|
|
setState(PanZoomState.NOTHING);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Performs one frame of a bounce animation. */
|
|
|
|
private void advanceBounce() {
|
|
|
|
synchronized (mTarget.getLock()) {
|
2013-09-04 18:07:12 +00:00
|
|
|
float t = easeOut((float)mBounceDuration / BOUNCE_ANIMATION_DURATION);
|
2013-02-08 14:12:59 +00:00
|
|
|
ImmutableViewportMetrics newMetrics = mBounceStartMetrics.interpolate(mBounceEndMetrics, t);
|
|
|
|
mTarget.setViewportMetrics(newMetrics);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Concludes a bounce animation and snaps the viewport into place. */
|
|
|
|
private void finishBounce() {
|
|
|
|
synchronized (mTarget.getLock()) {
|
|
|
|
mTarget.setViewportMetrics(mBounceEndMetrics);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The callback that performs the fling animation.
|
2013-09-04 18:07:11 +00:00
|
|
|
private class FlingRenderTask extends PanZoomRenderTask {
|
|
|
|
|
|
|
|
public FlingRenderTask() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
protected void animateFrame() {
|
|
|
|
/*
|
|
|
|
* 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. */
|
2013-09-04 18:07:12 +00:00
|
|
|
boolean flingingX = mX.advanceFling(mLastFrameTimeDelta);
|
|
|
|
boolean flingingY = mY.advanceFling(mLastFrameTimeDelta);
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
boolean overscrolled = (mX.overscrolled() || mY.overscrolled());
|
|
|
|
|
|
|
|
/* If we're still flinging in any direction, update the origin. */
|
|
|
|
if (flingingX || flingingY) {
|
|
|
|
updatePosition();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if we're still flinging with an appreciable velocity. The threshold is
|
|
|
|
* higher in the case of overscroll, so we bounce back eagerly when overscrolling but
|
|
|
|
* coast smoothly to a stop when not. In other words, require a greater velocity to
|
|
|
|
* maintain the fling once we enter overscroll.
|
|
|
|
*/
|
|
|
|
float threshold = (overscrolled && !mSubscroller.scrolling() ? STOPPED_THRESHOLD : FLING_STOPPED_THRESHOLD);
|
|
|
|
if (getVelocity() >= threshold) {
|
|
|
|
// we're still flinging
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mX.stopFling();
|
|
|
|
mY.stopFling();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform a bounce-back animation if overscrolled. */
|
|
|
|
if (overscrolled) {
|
|
|
|
bounce();
|
|
|
|
} else {
|
|
|
|
finishAnimation();
|
|
|
|
setState(PanZoomState.NOTHING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void finishAnimation() {
|
|
|
|
checkMainThread();
|
|
|
|
|
2013-09-04 18:07:11 +00:00
|
|
|
stopAnimationTask();
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
// Force a viewport synchronisation
|
2013-04-26 17:26:46 +00:00
|
|
|
mTarget.forceRedraw(null);
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the nearest viewport metrics with no overscroll visible. */
|
|
|
|
private ImmutableViewportMetrics getValidViewportMetrics() {
|
|
|
|
return getValidViewportMetrics(getMetrics());
|
|
|
|
}
|
|
|
|
|
|
|
|
private ImmutableViewportMetrics getValidViewportMetrics(ImmutableViewportMetrics viewportMetrics) {
|
|
|
|
/* First, we adjust the zoom factor so that we can make no overscrolled area visible. */
|
|
|
|
float zoomFactor = viewportMetrics.zoomFactor;
|
|
|
|
RectF pageRect = viewportMetrics.getPageRect();
|
|
|
|
RectF viewport = viewportMetrics.getViewport();
|
|
|
|
|
|
|
|
float focusX = viewport.width() / 2.0f;
|
|
|
|
float focusY = viewport.height() / 2.0f;
|
|
|
|
|
|
|
|
float minZoomFactor = 0.0f;
|
|
|
|
float maxZoomFactor = MAX_ZOOM;
|
|
|
|
|
|
|
|
ZoomConstraints constraints = mTarget.getZoomConstraints();
|
|
|
|
|
|
|
|
if (constraints.getMinZoom() > 0)
|
|
|
|
minZoomFactor = constraints.getMinZoom();
|
|
|
|
if (constraints.getMaxZoom() > 0)
|
|
|
|
maxZoomFactor = constraints.getMaxZoom();
|
|
|
|
|
|
|
|
if (!constraints.getAllowZoom()) {
|
|
|
|
// If allowZoom is false, clamp to the default zoom level.
|
|
|
|
maxZoomFactor = minZoomFactor = constraints.getDefaultZoom();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure minZoomFactor keeps the page at least as big as the viewport.
|
|
|
|
if (pageRect.width() > 0) {
|
2013-03-07 10:17:33 +00:00
|
|
|
float pageWidth = pageRect.width() +
|
2013-04-25 17:47:08 +00:00
|
|
|
viewportMetrics.marginLeft +
|
|
|
|
viewportMetrics.marginRight;
|
2013-03-07 10:17:33 +00:00
|
|
|
float scaleFactor = viewport.width() / pageWidth;
|
2013-02-08 14:12:59 +00:00
|
|
|
minZoomFactor = Math.max(minZoomFactor, zoomFactor * scaleFactor);
|
2013-03-07 10:17:33 +00:00
|
|
|
if (viewport.width() > pageWidth)
|
2013-02-08 14:12:59 +00:00
|
|
|
focusX = 0.0f;
|
|
|
|
}
|
|
|
|
if (pageRect.height() > 0) {
|
2013-03-07 10:17:33 +00:00
|
|
|
float pageHeight = pageRect.height() +
|
2013-04-25 17:47:08 +00:00
|
|
|
viewportMetrics.marginTop +
|
|
|
|
viewportMetrics.marginBottom;
|
2013-03-07 10:17:33 +00:00
|
|
|
float scaleFactor = viewport.height() / pageHeight;
|
2013-02-08 14:12:59 +00:00
|
|
|
minZoomFactor = Math.max(minZoomFactor, zoomFactor * scaleFactor);
|
2013-03-07 10:17:33 +00:00
|
|
|
if (viewport.height() > pageHeight)
|
2013-02-08 14:12:59 +00:00
|
|
|
focusY = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxZoomFactor = Math.max(maxZoomFactor, minZoomFactor);
|
|
|
|
|
|
|
|
if (zoomFactor < minZoomFactor) {
|
|
|
|
// 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);
|
|
|
|
viewportMetrics = viewportMetrics.scaleTo(minZoomFactor, center);
|
|
|
|
} else if (zoomFactor > maxZoomFactor) {
|
|
|
|
PointF center = new PointF(viewport.width() / 2.0f, viewport.height() / 2.0f);
|
|
|
|
viewportMetrics = viewportMetrics.scaleTo(maxZoomFactor, center);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we pan to the right origin. */
|
2013-03-07 10:17:33 +00:00
|
|
|
viewportMetrics = viewportMetrics.clampWithMargins();
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
return viewportMetrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class AxisX extends Axis {
|
|
|
|
AxisX(SubdocumentScrollHelper subscroller) { super(subscroller); }
|
|
|
|
@Override
|
|
|
|
public float getOrigin() { return getMetrics().viewportRectLeft; }
|
|
|
|
@Override
|
|
|
|
protected float getViewportLength() { return getMetrics().getWidth(); }
|
|
|
|
@Override
|
2013-04-25 17:47:08 +00:00
|
|
|
protected float getPageStart() { return getMetrics().pageRectLeft; }
|
2013-02-08 14:12:59 +00:00
|
|
|
@Override
|
2013-09-23 18:46:50 +00:00
|
|
|
protected float getMarginStart() { return mTarget.getMaxMargins().left - getMetrics().marginLeft; }
|
|
|
|
@Override
|
|
|
|
protected float getMarginEnd() { return mTarget.getMaxMargins().right - getMetrics().marginRight; }
|
|
|
|
@Override
|
2013-04-25 17:47:08 +00:00
|
|
|
protected float getPageLength() { return getMetrics().getPageWidthWithMargins(); }
|
2013-08-16 12:42:23 +00:00
|
|
|
@Override
|
|
|
|
protected boolean marginsHidden() {
|
|
|
|
ImmutableViewportMetrics metrics = getMetrics();
|
|
|
|
RectF maxMargins = mTarget.getMaxMargins();
|
|
|
|
return (metrics.marginLeft < maxMargins.left || metrics.marginRight < maxMargins.right);
|
|
|
|
}
|
2013-09-27 05:57:57 +00:00
|
|
|
@Override
|
|
|
|
protected void overscrollFling(final float velocity) {
|
|
|
|
if (mOverscroll != null) {
|
|
|
|
mOverscroll.setVelocity(velocity, Overscroll.Axis.X);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected void overscrollPan(final float distance) {
|
|
|
|
if (mOverscroll != null) {
|
|
|
|
mOverscroll.setDistance(distance, Overscroll.Axis.X);
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class AxisY extends Axis {
|
|
|
|
AxisY(SubdocumentScrollHelper subscroller) { super(subscroller); }
|
|
|
|
@Override
|
|
|
|
public float getOrigin() { return getMetrics().viewportRectTop; }
|
|
|
|
@Override
|
|
|
|
protected float getViewportLength() { return getMetrics().getHeight(); }
|
|
|
|
@Override
|
2013-04-25 17:47:08 +00:00
|
|
|
protected float getPageStart() { return getMetrics().pageRectTop; }
|
2013-02-08 14:12:59 +00:00
|
|
|
@Override
|
2013-04-25 17:47:08 +00:00
|
|
|
protected float getPageLength() { return getMetrics().getPageHeightWithMargins(); }
|
2013-08-16 12:42:23 +00:00
|
|
|
@Override
|
2013-09-23 18:46:50 +00:00
|
|
|
protected float getMarginStart() { return mTarget.getMaxMargins().top - getMetrics().marginTop; }
|
|
|
|
@Override
|
|
|
|
protected float getMarginEnd() { return mTarget.getMaxMargins().bottom - getMetrics().marginBottom; }
|
|
|
|
@Override
|
2013-08-16 12:42:23 +00:00
|
|
|
protected boolean marginsHidden() {
|
|
|
|
ImmutableViewportMetrics metrics = getMetrics();
|
|
|
|
RectF maxMargins = mTarget.getMaxMargins();
|
|
|
|
return (metrics.marginTop < maxMargins.top || metrics.marginBottom < maxMargins.bottom);
|
|
|
|
}
|
2013-09-27 05:57:57 +00:00
|
|
|
@Override
|
|
|
|
protected void overscrollFling(final float velocity) {
|
|
|
|
if (mOverscroll != null) {
|
|
|
|
mOverscroll.setVelocity(velocity, Overscroll.Axis.Y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected void overscrollPan(final float distance) {
|
|
|
|
if (mOverscroll != null) {
|
|
|
|
mOverscroll.setDistance(distance, Overscroll.Axis.Y);
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Zooming
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean onScaleBegin(SimpleScaleGestureDetector detector) {
|
|
|
|
if (mState == PanZoomState.ANIMATED_ZOOM)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!mTarget.getZoomConstraints().getAllowZoom())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
setState(PanZoomState.PINCHING);
|
|
|
|
mLastZoomFocus = new PointF(detector.getFocusX(), detector.getFocusY());
|
|
|
|
cancelTouch();
|
|
|
|
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createNativeGestureEvent(GeckoEvent.ACTION_MAGNIFY_START, mLastZoomFocus, getMetrics().zoomFactor));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onScale(SimpleScaleGestureDetector detector) {
|
2014-05-07 19:22:14 +00:00
|
|
|
if (mTarget.getFullScreenState() != FullScreenState.NONE)
|
2013-02-08 14:12:59 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mState != PanZoomState.PINCHING)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-02 03:50:48 +00:00
|
|
|
synchronized (mTarget.getLock()) {
|
|
|
|
float zoomFactor = getAdjustedZoomFactor(detector.getCurrentSpan() / prevSpan);
|
|
|
|
scrollBy(mLastZoomFocus.x - detector.getFocusX(),
|
|
|
|
mLastZoomFocus.y - detector.getFocusY());
|
|
|
|
mLastZoomFocus.set(detector.getFocusX(), detector.getFocusY());
|
|
|
|
ImmutableViewportMetrics target = getMetrics().scaleTo(zoomFactor, mLastZoomFocus);
|
2013-09-23 18:47:05 +00:00
|
|
|
|
|
|
|
// If overscroll is diabled, prevent zooming outside the normal document pans.
|
|
|
|
if (mX.getOverScrollMode() == View.OVER_SCROLL_NEVER || mY.getOverScrollMode() == View.OVER_SCROLL_NEVER) {
|
|
|
|
target = getValidViewportMetrics(target);
|
|
|
|
}
|
2013-03-02 03:50:48 +00:00
|
|
|
mTarget.setViewportMetrics(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoEvent event = GeckoEvent.createNativeGestureEvent(GeckoEvent.ACTION_MAGNIFY, mLastZoomFocus, getMetrics().zoomFactor);
|
|
|
|
GeckoAppShell.sendEventToGecko(event);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-03-28 21:31:09 +00:00
|
|
|
private ImmutableViewportMetrics applyZoomDelta(ImmutableViewportMetrics metrics, float zoomDelta) {
|
|
|
|
float oldZoom = metrics.zoomFactor;
|
|
|
|
float newZoom = oldZoom + zoomDelta;
|
|
|
|
float adjustedZoom = getAdjustedZoomFactor(newZoom / oldZoom);
|
|
|
|
// since we don't have a particular focus to zoom to, just use the center
|
|
|
|
PointF center = new PointF(metrics.getWidth() / 2.0f, metrics.getHeight() / 2.0f);
|
|
|
|
metrics = metrics.scaleTo(adjustedZoom, center);
|
|
|
|
return metrics;
|
|
|
|
}
|
|
|
|
|
2013-03-02 03:50:48 +00:00
|
|
|
private boolean animatedScale(float zoomDelta) {
|
|
|
|
if (mState != PanZoomState.NOTHING && mState != PanZoomState.BOUNCE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
synchronized (mTarget.getLock()) {
|
2013-03-28 21:31:09 +00:00
|
|
|
ImmutableViewportMetrics metrics = applyZoomDelta(getMetrics(), zoomDelta);
|
2013-03-02 03:50:48 +00:00
|
|
|
bounce(getValidViewportMetrics(metrics), PanZoomState.BOUNCE);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-02 03:50:48 +00:00
|
|
|
private float getAdjustedZoomFactor(float zoomRatio) {
|
2013-02-08 14:12:59 +00: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(true), mY.getEdgeResistance(true));
|
2013-03-02 03:50:48 +00:00
|
|
|
if (zoomRatio > 1.0f)
|
|
|
|
zoomRatio = 1.0f + (zoomRatio - 1.0f) * resistance;
|
2013-02-08 14:12:59 +00:00
|
|
|
else
|
2013-03-02 03:50:48 +00:00
|
|
|
zoomRatio = 1.0f - (1.0f - zoomRatio) * resistance;
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-03-02 03:50:48 +00:00
|
|
|
float newZoomFactor = getMetrics().zoomFactor * zoomRatio;
|
|
|
|
float minZoomFactor = 0.0f;
|
|
|
|
float maxZoomFactor = MAX_ZOOM;
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-03-02 03:50:48 +00:00
|
|
|
ZoomConstraints constraints = mTarget.getZoomConstraints();
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-03-02 03:50:48 +00:00
|
|
|
if (constraints.getMinZoom() > 0)
|
|
|
|
minZoomFactor = constraints.getMinZoom();
|
|
|
|
if (constraints.getMaxZoom() > 0)
|
|
|
|
maxZoomFactor = constraints.getMaxZoom();
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-03-02 03:50:48 +00: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);
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-03-02 03:50:48 +00:00
|
|
|
if (newZoomFactor > maxZoomFactor) {
|
|
|
|
// apply resistance when zooming past maxZoomFactor,
|
|
|
|
// such that it asymptotically reaches maxZoomFactor + 1.0
|
|
|
|
// but never exceeds that
|
|
|
|
float excessZoom = newZoomFactor - maxZoomFactor;
|
|
|
|
excessZoom = 1.0f - (float)Math.exp(-excessZoom);
|
|
|
|
newZoomFactor = maxZoomFactor + excessZoom;
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-03-02 03:50:48 +00:00
|
|
|
return newZoomFactor;
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScaleEnd(SimpleScaleGestureDetector detector) {
|
|
|
|
if (mState == PanZoomState.ANIMATED_ZOOM)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// switch back to the touching state
|
|
|
|
startTouch(detector.getFocusX(), detector.getFocusY(), detector.getEventTime());
|
|
|
|
|
|
|
|
// Force a viewport synchronisation
|
2013-04-26 17:26:46 +00:00
|
|
|
mTarget.forceRedraw(null);
|
2013-02-08 14:12:59 +00:00
|
|
|
|
|
|
|
PointF point = new PointF(detector.getFocusX(), detector.getFocusY());
|
|
|
|
GeckoEvent event = GeckoEvent.createNativeGestureEvent(GeckoEvent.ACTION_MAGNIFY_END, point, getMetrics().zoomFactor);
|
|
|
|
|
|
|
|
if (event == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoAppShell.sendEventToGecko(event);
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public boolean getRedrawHint() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendPointToGecko(String event, MotionEvent motionEvent) {
|
|
|
|
String json;
|
|
|
|
try {
|
|
|
|
PointF point = new PointF(motionEvent.getX(), motionEvent.getY());
|
|
|
|
point = mTarget.convertViewPointToLayerPoint(point);
|
|
|
|
if (point == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
json = PointUtils.toJSON(point).toString();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Unable to convert point to JSON for " + event, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(event, json));
|
|
|
|
}
|
|
|
|
|
2013-06-03 14:05:59 +00:00
|
|
|
@Override
|
|
|
|
public boolean onDown(MotionEvent motionEvent) {
|
|
|
|
mMediumPress = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onShowPress(MotionEvent motionEvent) {
|
|
|
|
// If we get this, it will be followed either by a call to
|
|
|
|
// onSingleTapUp (if the user lifts their finger before the
|
|
|
|
// long-press timeout) or a call to onLongPress (if the user
|
|
|
|
// does not). In the former case, we want to make sure it is
|
|
|
|
// treated as a click. (Note that if this is called, we will
|
|
|
|
// not get a call to onDoubleTap).
|
|
|
|
mMediumPress = true;
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:12:59 +00:00
|
|
|
@Override
|
|
|
|
public void onLongPress(MotionEvent motionEvent) {
|
|
|
|
sendPointToGecko("Gesture:LongPress", motionEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-04-21 15:49:33 +00:00
|
|
|
public boolean onSingleTapUp(MotionEvent motionEvent) {
|
2014-02-25 00:21:02 +00:00
|
|
|
// When double-tapping is allowed, we have to wait to see if this is
|
|
|
|
// going to be a double-tap.
|
2013-06-03 14:05:59 +00:00
|
|
|
// However, if mMediumPress is true then we know there will be no
|
|
|
|
// double-tap so we treat this as a click.
|
2014-02-25 00:21:02 +00:00
|
|
|
if (mMediumPress || !mTarget.getZoomConstraints().getAllowDoubleTapZoom()) {
|
2014-04-21 15:49:33 +00:00
|
|
|
sendPointToGecko("Gesture:SingleTap", motionEvent);
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|
|
|
|
// return false because we still want to get the ACTION_UP event that triggers this
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
|
|
|
|
// When zooming is disabled, we handle this in onSingleTapUp.
|
2014-02-25 00:21:02 +00:00
|
|
|
if (mTarget.getZoomConstraints().getAllowDoubleTapZoom()) {
|
2013-02-08 14:12:59 +00:00
|
|
|
sendPointToGecko("Gesture:SingleTap", motionEvent);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onDoubleTap(MotionEvent motionEvent) {
|
2014-02-25 00:21:02 +00:00
|
|
|
if (mTarget.getZoomConstraints().getAllowDoubleTapZoom()) {
|
2013-02-08 14:12:59 +00:00
|
|
|
sendPointToGecko("Gesture:DoubleTap", motionEvent);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cancelTouch() {
|
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Gesture:CancelTouch", "");
|
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Zoom to a specified rect IN CSS PIXELS.
|
|
|
|
*
|
|
|
|
* While we usually use device pixels, @zoomToRect must be specified in CSS
|
|
|
|
* pixels.
|
|
|
|
*/
|
2013-12-12 06:43:44 +00:00
|
|
|
private ImmutableViewportMetrics getMetricsToZoomTo(RectF zoomToRect) {
|
2013-02-08 14:12:59 +00:00
|
|
|
final float startZoom = getMetrics().zoomFactor;
|
|
|
|
|
|
|
|
RectF viewport = getMetrics().getViewport();
|
|
|
|
// 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;
|
|
|
|
zoomToRect.bottom = zoomToRect.top + newHeight;
|
|
|
|
} else { // targetRatio > rectRatio) {
|
|
|
|
// need to increase zoomToRect width
|
|
|
|
float newWidth = targetRatio * zoomToRect.height();
|
|
|
|
zoomToRect.left -= (newWidth - zoomToRect.width()) / 2;
|
|
|
|
zoomToRect.right = zoomToRect.left + newWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
float finalZoom = viewport.width() / zoomToRect.width();
|
|
|
|
|
|
|
|
ImmutableViewportMetrics finalMetrics = getMetrics();
|
|
|
|
finalMetrics = finalMetrics.setViewportOrigin(
|
|
|
|
zoomToRect.left * finalMetrics.zoomFactor,
|
|
|
|
zoomToRect.top * finalMetrics.zoomFactor);
|
|
|
|
finalMetrics = finalMetrics.scaleTo(finalZoom, new PointF(0.0f, 0.0f));
|
|
|
|
|
|
|
|
// 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);
|
2013-12-12 06:43:44 +00:00
|
|
|
return finalMetrics;
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
|
2013-12-12 06:43:44 +00:00
|
|
|
private boolean animatedZoomTo(RectF zoomToRect) {
|
|
|
|
bounce(getMetricsToZoomTo(zoomToRect), PanZoomState.ANIMATED_ZOOM);
|
2013-02-08 14:12:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This function must be called from the UI thread. */
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public void abortPanning() {
|
|
|
|
checkMainThread();
|
|
|
|
bounce();
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public void setOverScrollMode(int overscrollMode) {
|
|
|
|
mX.setOverScrollMode(overscrollMode);
|
|
|
|
mY.setOverScrollMode(overscrollMode);
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-08 14:12:59 +00:00
|
|
|
public int getOverScrollMode() {
|
|
|
|
return mX.getOverScrollMode();
|
|
|
|
}
|
2013-07-23 02:33:05 +00:00
|
|
|
|
2013-09-27 05:57:57 +00:00
|
|
|
@Override
|
|
|
|
public void setOverscrollHandler(final Overscroll handler) {
|
|
|
|
mOverscroll = handler;
|
|
|
|
}
|
2013-02-08 14:12:59 +00:00
|
|
|
}
|