2012-01-24 03:18:24 +00:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-24 03:18:24 +00:00
|
|
|
|
2013-02-08 14:11:43 +00:00
|
|
|
package org.mozilla.gecko.gfx;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
2012-01-24 03:18:24 +00:00
|
|
|
import org.json.JSONException;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
2012-01-24 03:18:24 +00:00
|
|
|
import android.graphics.PointF;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.MotionEvent;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
2012-01-24 03:18:24 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.ListIterator;
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A less buggy, and smoother, replacement for the built-in Android ScaleGestureDetector.
|
|
|
|
*
|
|
|
|
* This gesture detector is more reliable than the built-in ScaleGestureDetector because:
|
|
|
|
*
|
|
|
|
* - It doesn't assume that pointer IDs are numbered 0 and 1.
|
|
|
|
*
|
|
|
|
* - It doesn't attempt to correct for "slop" when resting one's hand on the device. On some
|
|
|
|
* devices (e.g. the Droid X) this can cause the ScaleGestureDetector to lose track of how many
|
|
|
|
* pointers are down, with disastrous results (bug 706684).
|
|
|
|
*
|
|
|
|
* - Cancelling a zoom into a pan is handled correctly.
|
|
|
|
*
|
|
|
|
* - Starting with three or more fingers down, releasing fingers so that only two are down, and
|
|
|
|
* then performing a scale gesture is handled correctly.
|
|
|
|
*
|
|
|
|
* - It doesn't take pressure into account, which results in smoother scaling.
|
|
|
|
*/
|
2013-02-08 14:13:09 +00:00
|
|
|
class SimpleScaleGestureDetector {
|
2012-01-24 03:18:24 +00:00
|
|
|
private static final String LOGTAG = "GeckoSimpleScaleGestureDetector";
|
|
|
|
|
2014-10-10 23:17:01 +00:00
|
|
|
private final SimpleScaleGestureListener mListener;
|
2012-01-24 03:18:24 +00:00
|
|
|
private long mLastEventTime;
|
2012-11-14 23:58:12 +00:00
|
|
|
private boolean mScaleResult;
|
2012-01-24 03:18:24 +00:00
|
|
|
|
|
|
|
/* Information about all pointers that are down. */
|
2014-10-10 23:17:01 +00:00
|
|
|
private final LinkedList<PointerInfo> mPointerInfo;
|
2012-01-24 03:18:24 +00:00
|
|
|
|
|
|
|
/** Creates a new gesture detector with the given listener. */
|
2013-02-08 14:13:09 +00:00
|
|
|
SimpleScaleGestureDetector(SimpleScaleGestureListener listener) {
|
2012-01-24 03:18:24 +00:00
|
|
|
mListener = listener;
|
|
|
|
mPointerInfo = new LinkedList<PointerInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Forward touch events to this function. */
|
|
|
|
public void onTouchEvent(MotionEvent event) {
|
2012-02-14 20:28:27 +00:00
|
|
|
switch (event.getAction() & MotionEvent.ACTION_MASK) {
|
2012-01-24 03:18:24 +00:00
|
|
|
case MotionEvent.ACTION_DOWN:
|
2012-06-19 19:01:44 +00:00
|
|
|
// If we get ACTION_DOWN while still tracking any pointers,
|
|
|
|
// something is wrong. Cancel the current gesture and start over.
|
|
|
|
if (getPointersDown() > 0)
|
|
|
|
onTouchEnd(event);
|
|
|
|
onTouchStart(event);
|
|
|
|
break;
|
2012-01-24 03:18:24 +00:00
|
|
|
case MotionEvent.ACTION_POINTER_DOWN:
|
|
|
|
onTouchStart(event);
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
onTouchMove(event);
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_POINTER_UP:
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
|
|
|
onTouchEnd(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getPointersDown() {
|
|
|
|
return mPointerInfo.size();
|
|
|
|
}
|
|
|
|
|
2012-02-01 20:03:46 +00:00
|
|
|
private int getActionIndex(MotionEvent event) {
|
|
|
|
return (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
|
|
|
|
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
|
|
|
|
}
|
|
|
|
|
2012-01-24 03:18:24 +00:00
|
|
|
private void onTouchStart(MotionEvent event) {
|
|
|
|
mLastEventTime = event.getEventTime();
|
2012-02-01 20:03:46 +00:00
|
|
|
mPointerInfo.addFirst(PointerInfo.create(event, getActionIndex(event)));
|
2012-01-24 03:18:24 +00:00
|
|
|
if (getPointersDown() == 2) {
|
|
|
|
sendScaleGesture(EventType.BEGIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onTouchMove(MotionEvent event) {
|
|
|
|
mLastEventTime = event.getEventTime();
|
|
|
|
for (int i = 0; i < event.getPointerCount(); i++) {
|
|
|
|
PointerInfo pointerInfo = pointerInfoForEventIndex(event, i);
|
|
|
|
if (pointerInfo != null) {
|
|
|
|
pointerInfo.populate(event, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getPointersDown() == 2) {
|
|
|
|
sendScaleGesture(EventType.CONTINUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onTouchEnd(MotionEvent event) {
|
|
|
|
mLastEventTime = event.getEventTime();
|
|
|
|
|
2012-06-19 19:01:44 +00:00
|
|
|
int action = event.getAction() & MotionEvent.ACTION_MASK;
|
|
|
|
boolean isCancel = (action == MotionEvent.ACTION_CANCEL ||
|
|
|
|
action == MotionEvent.ACTION_DOWN);
|
|
|
|
|
2012-02-01 20:03:46 +00:00
|
|
|
int id = event.getPointerId(getActionIndex(event));
|
2012-01-24 03:18:24 +00:00
|
|
|
ListIterator<PointerInfo> iterator = mPointerInfo.listIterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
PointerInfo pointerInfo = iterator.next();
|
2012-04-07 07:09:26 +00:00
|
|
|
if (!(isCancel || pointerInfo.getId() == id)) {
|
2012-01-24 03:18:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// One of the pointers we were tracking was lifted. Remove its info object from the
|
|
|
|
// list, recycle it to avoid GC pauses, and send an onScaleEnd() notification if this
|
|
|
|
// ended the gesture.
|
|
|
|
iterator.remove();
|
|
|
|
pointerInfo.recycle();
|
|
|
|
if (getPointersDown() == 1) {
|
|
|
|
sendScaleGesture(EventType.END);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the X coordinate of the focus location (the midpoint of the two fingers). If only
|
|
|
|
* one finger is down, returns the location of that finger.
|
|
|
|
*/
|
|
|
|
public float getFocusX() {
|
|
|
|
switch (getPointersDown()) {
|
|
|
|
case 1:
|
|
|
|
return mPointerInfo.getFirst().getCurrent().x;
|
|
|
|
case 2:
|
|
|
|
PointerInfo pointerA = mPointerInfo.getFirst(), pointerB = mPointerInfo.getLast();
|
|
|
|
return (pointerA.getCurrent().x + pointerB.getCurrent().x) / 2.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.e(LOGTAG, "No gesture taking place in getFocusX()!");
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Y coordinate of the focus location (the midpoint of the two fingers). If only
|
|
|
|
* one finger is down, returns the location of that finger.
|
|
|
|
*/
|
|
|
|
public float getFocusY() {
|
|
|
|
switch (getPointersDown()) {
|
|
|
|
case 1:
|
|
|
|
return mPointerInfo.getFirst().getCurrent().y;
|
|
|
|
case 2:
|
|
|
|
PointerInfo pointerA = mPointerInfo.getFirst(), pointerB = mPointerInfo.getLast();
|
|
|
|
return (pointerA.getCurrent().y + pointerB.getCurrent().y) / 2.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.e(LOGTAG, "No gesture taking place in getFocusY()!");
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the most recent distance between the two pointers. */
|
|
|
|
public float getCurrentSpan() {
|
|
|
|
if (getPointersDown() != 2) {
|
|
|
|
Log.e(LOGTAG, "No gesture taking place in getCurrentSpan()!");
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerInfo pointerA = mPointerInfo.getFirst(), pointerB = mPointerInfo.getLast();
|
|
|
|
return PointUtils.distance(pointerA.getCurrent(), pointerB.getCurrent());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the second most recent distance between the two pointers. */
|
|
|
|
public float getPreviousSpan() {
|
|
|
|
if (getPointersDown() != 2) {
|
|
|
|
Log.e(LOGTAG, "No gesture taking place in getPreviousSpan()!");
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerInfo pointerA = mPointerInfo.getFirst(), pointerB = mPointerInfo.getLast();
|
|
|
|
PointF a = pointerA.getPrevious(), b = pointerB.getPrevious();
|
|
|
|
if (a == null || b == null) {
|
|
|
|
a = pointerA.getCurrent();
|
|
|
|
b = pointerB.getCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return PointUtils.distance(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the time of the last event related to the gesture. */
|
|
|
|
public long getEventTime() {
|
|
|
|
return mLastEventTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns true if the scale gesture is in progress and false otherwise. */
|
|
|
|
public boolean isInProgress() {
|
|
|
|
return getPointersDown() == 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sends the requested scale gesture notification to the listener. */
|
|
|
|
private void sendScaleGesture(EventType eventType) {
|
|
|
|
switch (eventType) {
|
2012-11-14 23:58:12 +00:00
|
|
|
case BEGIN:
|
|
|
|
mScaleResult = mListener.onScaleBegin(this);
|
|
|
|
break;
|
|
|
|
case CONTINUE:
|
|
|
|
if (mScaleResult) {
|
|
|
|
mListener.onScale(this);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case END:
|
|
|
|
if (mScaleResult) {
|
|
|
|
mListener.onScaleEnd(this);
|
|
|
|
}
|
|
|
|
break;
|
2012-01-24 03:18:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the pointer info corresponding to the given pointer index, or null if the pointer
|
|
|
|
* isn't one that's being tracked.
|
|
|
|
*/
|
|
|
|
private PointerInfo pointerInfoForEventIndex(MotionEvent event, int index) {
|
|
|
|
int id = event.getPointerId(index);
|
|
|
|
for (PointerInfo pointerInfo : mPointerInfo) {
|
|
|
|
if (pointerInfo.getId() == id) {
|
|
|
|
return pointerInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum EventType {
|
|
|
|
BEGIN,
|
|
|
|
CONTINUE,
|
|
|
|
END,
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encapsulates information about one of the two fingers involved in the gesture. */
|
|
|
|
private static class PointerInfo {
|
|
|
|
/* A free list that recycles pointer info objects, to reduce GC pauses. */
|
|
|
|
private static Stack<PointerInfo> sPointerInfoFreeList;
|
|
|
|
|
|
|
|
private int mId;
|
|
|
|
private PointF mCurrent, mPrevious;
|
|
|
|
|
|
|
|
private PointerInfo() {
|
|
|
|
// External users should use create() instead.
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates or recycles a new PointerInfo instance from an event and a pointer index. */
|
|
|
|
public static PointerInfo create(MotionEvent event, int index) {
|
|
|
|
if (sPointerInfoFreeList == null) {
|
|
|
|
sPointerInfoFreeList = new Stack<PointerInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerInfo pointerInfo;
|
|
|
|
if (sPointerInfoFreeList.empty()) {
|
|
|
|
pointerInfo = new PointerInfo();
|
|
|
|
} else {
|
|
|
|
pointerInfo = sPointerInfoFreeList.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
pointerInfo.populate(event, index);
|
|
|
|
return pointerInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fills in the fields of this instance from the given motion event and pointer index
|
|
|
|
* within that event.
|
|
|
|
*/
|
|
|
|
public void populate(MotionEvent event, int index) {
|
|
|
|
mId = event.getPointerId(index);
|
|
|
|
mPrevious = mCurrent;
|
|
|
|
mCurrent = new PointF(event.getX(index), event.getY(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void recycle() {
|
|
|
|
mId = -1;
|
|
|
|
mPrevious = mCurrent = null;
|
|
|
|
sPointerInfoFreeList.push(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getId() { return mId; }
|
|
|
|
public PointF getCurrent() { return mCurrent; }
|
|
|
|
public PointF getPrevious() { return mPrevious; }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
if (mId == -1) {
|
|
|
|
return "(up)";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
String prevString;
|
|
|
|
if (mPrevious == null) {
|
|
|
|
prevString = "n/a";
|
|
|
|
} else {
|
|
|
|
prevString = PointUtils.toJSON(mPrevious).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The current position should always be non-null.
|
|
|
|
String currentString = PointUtils.toJSON(mCurrent).toString();
|
|
|
|
return "id=" + mId + " cur=" + currentString + " prev=" + prevString;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static interface SimpleScaleGestureListener {
|
|
|
|
public boolean onScale(SimpleScaleGestureDetector detector);
|
|
|
|
public boolean onScaleBegin(SimpleScaleGestureDetector detector);
|
|
|
|
public void onScaleEnd(SimpleScaleGestureDetector detector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|