blob: 5e07e1a7c86dd815373843c664ebc0a2fd32d6d0 [file] [log] [blame]
Adam Powellae542ff2010-01-13 16:29:27 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
19import android.content.Context;
Adam Powell380b5252010-01-19 16:33:46 -080020import android.util.DisplayMetrics;
Adam Powell346c8fb2010-03-12 10:52:35 -080021import android.util.FloatMath;
Adam Powell08180202011-03-07 16:48:29 -080022import android.util.Log;
Adam Powellae542ff2010-01-13 16:29:27 -080023
24/**
25 * Detects transformation gestures involving more than one pointer ("multitouch")
26 * using the supplied {@link MotionEvent}s. The {@link OnScaleGestureListener}
27 * callback will notify users when a particular gesture event has occurred.
28 * This class should only be used with {@link MotionEvent}s reported via touch.
Erik47c41e82010-09-01 15:39:08 -070029 *
Adam Powellae542ff2010-01-13 16:29:27 -080030 * To use this class:
31 * <ul>
32 * <li>Create an instance of the {@code ScaleGestureDetector} for your
33 * {@link View}
34 * <li>In the {@link View#onTouchEvent(MotionEvent)} method ensure you call
35 * {@link #onTouchEvent(MotionEvent)}. The methods defined in your
36 * callback will be executed when the events occur.
37 * </ul>
Adam Powellae542ff2010-01-13 16:29:27 -080038 */
39public class ScaleGestureDetector {
Adam Powell08180202011-03-07 16:48:29 -080040 private static final String TAG = "ScaleGestureDetector";
41
Adam Powellae542ff2010-01-13 16:29:27 -080042 /**
43 * The listener for receiving notifications when gestures occur.
44 * If you want to listen for all the different gestures then implement
45 * this interface. If you only want to listen for a subset it might
46 * be easier to extend {@link SimpleOnScaleGestureListener}.
Erik47c41e82010-09-01 15:39:08 -070047 *
Adam Powellae542ff2010-01-13 16:29:27 -080048 * An application will receive events in the following order:
49 * <ul>
Adam Powellab905c872010-02-03 11:01:58 -080050 * <li>One {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)}
51 * <li>Zero or more {@link OnScaleGestureListener#onScale(ScaleGestureDetector)}
52 * <li>One {@link OnScaleGestureListener#onScaleEnd(ScaleGestureDetector)}
Adam Powellae542ff2010-01-13 16:29:27 -080053 * </ul>
54 */
55 public interface OnScaleGestureListener {
56 /**
57 * Responds to scaling events for a gesture in progress.
58 * Reported by pointer motion.
Erik47c41e82010-09-01 15:39:08 -070059 *
Adam Powellae542ff2010-01-13 16:29:27 -080060 * @param detector The detector reporting the event - use this to
61 * retrieve extended info about event state.
62 * @return Whether or not the detector should consider this event
63 * as handled. If an event was not handled, the detector
64 * will continue to accumulate movement until an event is
65 * handled. This can be useful if an application, for example,
66 * only wants to update scaling factors if the change is
67 * greater than 0.01.
68 */
69 public boolean onScale(ScaleGestureDetector detector);
70
71 /**
72 * Responds to the beginning of a scaling gesture. Reported by
73 * new pointers going down.
Erik47c41e82010-09-01 15:39:08 -070074 *
Adam Powellae542ff2010-01-13 16:29:27 -080075 * @param detector The detector reporting the event - use this to
76 * retrieve extended info about event state.
77 * @return Whether or not the detector should continue recognizing
78 * this gesture. For example, if a gesture is beginning
79 * with a focal point outside of a region where it makes
80 * sense, onScaleBegin() may return false to ignore the
81 * rest of the gesture.
82 */
83 public boolean onScaleBegin(ScaleGestureDetector detector);
84
85 /**
86 * Responds to the end of a scale gesture. Reported by existing
Adam Powell216bccf2010-02-01 15:03:17 -080087 * pointers going up.
Erik47c41e82010-09-01 15:39:08 -070088 *
Adam Powellae542ff2010-01-13 16:29:27 -080089 * Once a scale has ended, {@link ScaleGestureDetector#getFocusX()}
90 * and {@link ScaleGestureDetector#getFocusY()} will return the location
91 * of the pointer remaining on the screen.
Erik47c41e82010-09-01 15:39:08 -070092 *
Adam Powellae542ff2010-01-13 16:29:27 -080093 * @param detector The detector reporting the event - use this to
94 * retrieve extended info about event state.
95 */
96 public void onScaleEnd(ScaleGestureDetector detector);
97 }
Erik47c41e82010-09-01 15:39:08 -070098
Adam Powellae542ff2010-01-13 16:29:27 -080099 /**
100 * A convenience class to extend when you only want to listen for a subset
101 * of scaling-related events. This implements all methods in
102 * {@link OnScaleGestureListener} but does nothing.
Adam Powell346c8fb2010-03-12 10:52:35 -0800103 * {@link OnScaleGestureListener#onScale(ScaleGestureDetector)} returns
104 * {@code false} so that a subclass can retrieve the accumulated scale
105 * factor in an overridden onScaleEnd.
106 * {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)} returns
Erik47c41e82010-09-01 15:39:08 -0700107 * {@code true}.
Adam Powellae542ff2010-01-13 16:29:27 -0800108 */
Adam Powell216bccf2010-02-01 15:03:17 -0800109 public static class SimpleOnScaleGestureListener implements OnScaleGestureListener {
Adam Powellae542ff2010-01-13 16:29:27 -0800110
111 public boolean onScale(ScaleGestureDetector detector) {
Adam Powell346c8fb2010-03-12 10:52:35 -0800112 return false;
Adam Powellae542ff2010-01-13 16:29:27 -0800113 }
114
115 public boolean onScaleBegin(ScaleGestureDetector detector) {
116 return true;
117 }
118
119 public void onScaleEnd(ScaleGestureDetector detector) {
120 // Intentionally empty
121 }
122 }
123
Adam Powell346c8fb2010-03-12 10:52:35 -0800124 /**
125 * This value is the threshold ratio between our previous combined pressure
126 * and the current combined pressure. We will only fire an onScale event if
127 * the computed ratio between the current and previous event pressures is
128 * greater than this value. When pressure decreases rapidly between events
129 * the position values can often be imprecise, as it usually indicates
130 * that the user is in the process of lifting a pointer off of the device.
131 * Its value was tuned experimentally.
132 */
Adam Powellae542ff2010-01-13 16:29:27 -0800133 private static final float PRESSURE_THRESHOLD = 0.67f;
134
Adam Powell346c8fb2010-03-12 10:52:35 -0800135 private final Context mContext;
136 private final OnScaleGestureListener mListener;
Adam Powellae542ff2010-01-13 16:29:27 -0800137 private boolean mGestureInProgress;
138
139 private MotionEvent mPrevEvent;
140 private MotionEvent mCurrEvent;
141
142 private float mFocusX;
143 private float mFocusY;
144 private float mPrevFingerDiffX;
145 private float mPrevFingerDiffY;
146 private float mCurrFingerDiffX;
147 private float mCurrFingerDiffY;
148 private float mCurrLen;
149 private float mPrevLen;
150 private float mScaleFactor;
151 private float mCurrPressure;
152 private float mPrevPressure;
153 private long mTimeDelta;
Erik47c41e82010-09-01 15:39:08 -0700154
Adam Powell346c8fb2010-03-12 10:52:35 -0800155 private final float mEdgeSlop;
Adam Powell380b5252010-01-19 16:33:46 -0800156 private float mRightSlopEdge;
157 private float mBottomSlopEdge;
158 private boolean mSloppyGesture;
Adam Powelld0197f32011-03-17 00:09:03 -0700159 private boolean mInvalidGesture;
Adam Powellae542ff2010-01-13 16:29:27 -0800160
Adam Powelle33cef82011-02-23 16:51:20 -0800161 // Pointer IDs currently responsible for the two fingers controlling the gesture
162 private int mActiveId0;
163 private int mActiveId1;
164 private boolean mActive0MostRecent;
165
Jeff Brown21bc5c92011-02-28 18:27:14 -0800166 /**
167 * Consistency verifier for debugging purposes.
168 */
169 private final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
170 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
171 new InputEventConsistencyVerifier(this, 0) : null;
172
Adam Powellae542ff2010-01-13 16:29:27 -0800173 public ScaleGestureDetector(Context context, OnScaleGestureListener listener) {
Adam Powell380b5252010-01-19 16:33:46 -0800174 ViewConfiguration config = ViewConfiguration.get(context);
Adam Powellae542ff2010-01-13 16:29:27 -0800175 mContext = context;
176 mListener = listener;
Adam Powell380b5252010-01-19 16:33:46 -0800177 mEdgeSlop = config.getScaledEdgeSlop();
Adam Powellae542ff2010-01-13 16:29:27 -0800178 }
179
180 public boolean onTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -0800181 if (mInputEventConsistencyVerifier != null) {
182 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
183 }
184
Adam Powelle33cef82011-02-23 16:51:20 -0800185 final int action = event.getActionMasked();
Adam Powellae542ff2010-01-13 16:29:27 -0800186
Adam Powell08180202011-03-07 16:48:29 -0800187 if (action == MotionEvent.ACTION_DOWN) {
188 reset(); // Start fresh
189 }
190
Jeff Brownbbdc50b2011-04-19 23:46:52 -0700191 boolean handled = true;
192 if (mInvalidGesture) {
193 handled = false;
194 } else if (!mGestureInProgress) {
Adam Powelle33cef82011-02-23 16:51:20 -0800195 switch (action) {
196 case MotionEvent.ACTION_DOWN: {
197 mActiveId0 = event.getPointerId(0);
198 mActive0MostRecent = true;
199 }
200 break;
201
202 case MotionEvent.ACTION_UP:
203 reset();
204 break;
205
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800206 case MotionEvent.ACTION_POINTER_DOWN: {
Adam Powellae542ff2010-01-13 16:29:27 -0800207 // We have a new multi-finger gesture
Adam Powell380b5252010-01-19 16:33:46 -0800208
Grace Klobad5ada832010-01-20 23:06:43 -0800209 // as orientation can change, query the metrics in touch down
210 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
211 mRightSlopEdge = metrics.widthPixels - mEdgeSlop;
212 mBottomSlopEdge = metrics.heightPixels - mEdgeSlop;
213
Adam Powelle33cef82011-02-23 16:51:20 -0800214 if (mPrevEvent != null) mPrevEvent.recycle();
Adam Powellae542ff2010-01-13 16:29:27 -0800215 mPrevEvent = MotionEvent.obtain(event);
216 mTimeDelta = 0;
Adam Powell380b5252010-01-19 16:33:46 -0800217
Adam Powelle33cef82011-02-23 16:51:20 -0800218 int index1 = event.getActionIndex();
219 int index0 = event.findPointerIndex(mActiveId0);
220 mActiveId1 = event.getPointerId(index1);
Adam Powell08180202011-03-07 16:48:29 -0800221 if (index0 < 0 || index0 == index1) {
222 // Probably someone sending us a broken event stream.
223 index0 = findNewActiveIndex(event, index0 == index1 ? -1 : mActiveId1, index0);
224 mActiveId0 = event.getPointerId(index0);
225 }
Adam Powelle33cef82011-02-23 16:51:20 -0800226 mActive0MostRecent = false;
227
Adam Powellae542ff2010-01-13 16:29:27 -0800228 setContext(event);
Adam Powell380b5252010-01-19 16:33:46 -0800229
230 // Check if we have a sloppy gesture. If so, delay
231 // the beginning of the gesture until we're sure that's
232 // what the user wanted. Sloppy gestures can happen if the
233 // edge of the user's hand is touching the screen, for example.
234 final float edgeSlop = mEdgeSlop;
235 final float rightSlop = mRightSlopEdge;
236 final float bottomSlop = mBottomSlopEdge;
Adam Powelle33cef82011-02-23 16:51:20 -0800237 float x0 = getRawX(event, index0);
238 float y0 = getRawY(event, index0);
239 float x1 = getRawX(event, index1);
240 float y1 = getRawY(event, index1);
Adam Powell380b5252010-01-19 16:33:46 -0800241
Grace Kloba8f9fbb02010-01-20 14:10:31 -0800242 boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop
243 || x0 > rightSlop || y0 > bottomSlop;
244 boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop
245 || x1 > rightSlop || y1 > bottomSlop;
Adam Powell380b5252010-01-19 16:33:46 -0800246
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800247 if (p0sloppy && p1sloppy) {
Grace Kloba8f9fbb02010-01-20 14:10:31 -0800248 mFocusX = -1;
249 mFocusY = -1;
250 mSloppyGesture = true;
251 } else if (p0sloppy) {
Adam Powelle33cef82011-02-23 16:51:20 -0800252 mFocusX = event.getX(index1);
253 mFocusY = event.getY(index1);
Adam Powell380b5252010-01-19 16:33:46 -0800254 mSloppyGesture = true;
255 } else if (p1sloppy) {
Adam Powelle33cef82011-02-23 16:51:20 -0800256 mFocusX = event.getX(index0);
257 mFocusY = event.getY(index0);
Adam Powell380b5252010-01-19 16:33:46 -0800258 mSloppyGesture = true;
259 } else {
Adam Powelle33cef82011-02-23 16:51:20 -0800260 mSloppyGesture = false;
Adam Powell380b5252010-01-19 16:33:46 -0800261 mGestureInProgress = mListener.onScaleBegin(this);
262 }
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800263 }
264 break;
Erik47c41e82010-09-01 15:39:08 -0700265
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800266 case MotionEvent.ACTION_MOVE:
267 if (mSloppyGesture) {
268 // Initiate sloppy gestures if we've moved outside of the slop area.
269 final float edgeSlop = mEdgeSlop;
270 final float rightSlop = mRightSlopEdge;
271 final float bottomSlop = mBottomSlopEdge;
Adam Powelle33cef82011-02-23 16:51:20 -0800272 int index0 = event.findPointerIndex(mActiveId0);
273 int index1 = event.findPointerIndex(mActiveId1);
Adam Powell0fe4a132011-02-28 13:32:17 -0800274
Adam Powelle33cef82011-02-23 16:51:20 -0800275 float x0 = getRawX(event, index0);
276 float y0 = getRawY(event, index0);
277 float x1 = getRawX(event, index1);
278 float y1 = getRawY(event, index1);
Adam Powell380b5252010-01-19 16:33:46 -0800279
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800280 boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop
Adam Powelle33cef82011-02-23 16:51:20 -0800281 || x0 > rightSlop || y0 > bottomSlop;
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800282 boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop
Adam Powelle33cef82011-02-23 16:51:20 -0800283 || x1 > rightSlop || y1 > bottomSlop;
284
285 if (p0sloppy) {
286 // Do we have a different pointer that isn't sloppy?
287 int index = findNewActiveIndex(event, mActiveId1, index0);
288 if (index >= 0) {
289 index0 = index;
290 mActiveId0 = event.getPointerId(index);
291 x0 = getRawX(event, index);
292 y0 = getRawY(event, index);
293 p0sloppy = false;
294 }
295 }
296
297 if (p1sloppy) {
298 // Do we have a different pointer that isn't sloppy?
299 int index = findNewActiveIndex(event, mActiveId0, index1);
300 if (index >= 0) {
301 index1 = index;
302 mActiveId1 = event.getPointerId(index);
303 x1 = getRawX(event, index);
304 y1 = getRawY(event, index);
305 p1sloppy = false;
306 }
307 }
Adam Powell380b5252010-01-19 16:33:46 -0800308
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800309 if(p0sloppy && p1sloppy) {
310 mFocusX = -1;
311 mFocusY = -1;
312 } else if (p0sloppy) {
Adam Powelle33cef82011-02-23 16:51:20 -0800313 mFocusX = event.getX(index1);
314 mFocusY = event.getY(index1);
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800315 } else if (p1sloppy) {
Adam Powelle33cef82011-02-23 16:51:20 -0800316 mFocusX = event.getX(index0);
317 mFocusY = event.getY(index0);
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800318 } else {
319 mSloppyGesture = false;
320 mGestureInProgress = mListener.onScaleBegin(this);
321 }
Adam Powell380b5252010-01-19 16:33:46 -0800322 }
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800323 break;
Erik47c41e82010-09-01 15:39:08 -0700324
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800325 case MotionEvent.ACTION_POINTER_UP:
326 if (mSloppyGesture) {
Adam Powelle33cef82011-02-23 16:51:20 -0800327 final int pointerCount = event.getPointerCount();
328 final int actionIndex = event.getActionIndex();
329 final int actionId = event.getPointerId(actionIndex);
330
331 if (pointerCount > 2) {
332 if (actionId == mActiveId0) {
333 final int newIndex = findNewActiveIndex(event, mActiveId1, actionIndex);
334 if (newIndex >= 0) mActiveId0 = event.getPointerId(newIndex);
335 } else if (actionId == mActiveId1) {
336 final int newIndex = findNewActiveIndex(event, mActiveId0, actionIndex);
337 if (newIndex >= 0) mActiveId1 = event.getPointerId(newIndex);
338 }
339 } else {
340 // Set focus point to the remaining finger
341 final int index = event.findPointerIndex(actionId == mActiveId0 ?
342 mActiveId1 : mActiveId0);
343 mActiveId0 = event.getPointerId(index);
Adam Powell08180202011-03-07 16:48:29 -0800344
Adam Powelle33cef82011-02-23 16:51:20 -0800345 mActive0MostRecent = true;
346 mActiveId1 = -1;
347 mFocusX = event.getX(index);
348 mFocusY = event.getY(index);
349 }
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800350 }
351 break;
Adam Powellae542ff2010-01-13 16:29:27 -0800352 }
353 } else {
354 // Transform gesture in progress - attempt to handle it
Adam Powelle33cef82011-02-23 16:51:20 -0800355 switch (action) {
356 case MotionEvent.ACTION_POINTER_DOWN: {
357 // End the old gesture and begin a new one with the most recent two fingers.
358 mListener.onScaleEnd(this);
359 final int oldActive0 = mActiveId0;
360 final int oldActive1 = mActiveId1;
361 reset();
362
363 mPrevEvent = MotionEvent.obtain(event);
364 mActiveId0 = mActive0MostRecent ? oldActive0 : oldActive1;
365 mActiveId1 = event.getPointerId(event.getActionIndex());
366 mActive0MostRecent = false;
367
Adam Powell08180202011-03-07 16:48:29 -0800368 int index0 = event.findPointerIndex(mActiveId0);
369 if (index0 < 0 || mActiveId0 == mActiveId1) {
370 // Probably someone sending us a broken event stream.
371 Log.e(TAG, "Got " + MotionEvent.actionToString(action) +
372 " with bad state while a gesture was in progress. " +
373 "Did you forget to pass an event to " +
374 "ScaleGestureDetector#onTouchEvent?");
375 index0 = findNewActiveIndex(event,
376 mActiveId0 == mActiveId1 ? -1 : mActiveId1, index0);
377 mActiveId0 = event.getPointerId(index0);
378 }
379
Adam Powellae542ff2010-01-13 16:29:27 -0800380 setContext(event);
Adam Powell380b5252010-01-19 16:33:46 -0800381
Adam Powelle33cef82011-02-23 16:51:20 -0800382 mGestureInProgress = mListener.onScaleBegin(this);
383 }
384 break;
Adam Powell380b5252010-01-19 16:33:46 -0800385
Adam Powelle33cef82011-02-23 16:51:20 -0800386 case MotionEvent.ACTION_POINTER_UP: {
387 final int pointerCount = event.getPointerCount();
388 final int actionIndex = event.getActionIndex();
389 final int actionId = event.getPointerId(actionIndex);
390
391 boolean gestureEnded = false;
392 if (pointerCount > 2) {
393 if (actionId == mActiveId0) {
394 final int newIndex = findNewActiveIndex(event, mActiveId1, actionIndex);
395 if (newIndex >= 0) {
Adam Powell0fe4a132011-02-28 13:32:17 -0800396 mListener.onScaleEnd(this);
Adam Powelle33cef82011-02-23 16:51:20 -0800397 mActiveId0 = event.getPointerId(newIndex);
Adam Powell0fe4a132011-02-28 13:32:17 -0800398 mActive0MostRecent = true;
399 mPrevEvent = MotionEvent.obtain(event);
400 setContext(event);
401 mGestureInProgress = mListener.onScaleBegin(this);
Adam Powelle33cef82011-02-23 16:51:20 -0800402 } else {
403 gestureEnded = true;
404 }
405 } else if (actionId == mActiveId1) {
406 final int newIndex = findNewActiveIndex(event, mActiveId0, actionIndex);
407 if (newIndex >= 0) {
Adam Powell0fe4a132011-02-28 13:32:17 -0800408 mListener.onScaleEnd(this);
Adam Powelle33cef82011-02-23 16:51:20 -0800409 mActiveId1 = event.getPointerId(newIndex);
Adam Powell0fe4a132011-02-28 13:32:17 -0800410 mActive0MostRecent = false;
411 mPrevEvent = MotionEvent.obtain(event);
412 setContext(event);
413 mGestureInProgress = mListener.onScaleBegin(this);
Adam Powelle33cef82011-02-23 16:51:20 -0800414 } else {
415 gestureEnded = true;
416 }
417 }
418 mPrevEvent.recycle();
419 mPrevEvent = MotionEvent.obtain(event);
420 setContext(event);
421 } else {
422 gestureEnded = true;
Adam Powell380b5252010-01-19 16:33:46 -0800423 }
Adam Powellae542ff2010-01-13 16:29:27 -0800424
Adam Powelle33cef82011-02-23 16:51:20 -0800425 if (gestureEnded) {
426 // Gesture ended
427 setContext(event);
428
429 // Set focus point to the remaining finger
430 final int activeId = actionId == mActiveId0 ? mActiveId1 : mActiveId0;
431 final int index = event.findPointerIndex(activeId);
432 mFocusX = event.getX(index);
433 mFocusY = event.getY(index);
434
435 mListener.onScaleEnd(this);
436 reset();
437 mActiveId0 = activeId;
438 mActive0MostRecent = true;
439 }
440 }
441 break;
Adam Powellae542ff2010-01-13 16:29:27 -0800442
443 case MotionEvent.ACTION_CANCEL:
Adam Powelle33cef82011-02-23 16:51:20 -0800444 mListener.onScaleEnd(this);
Adam Powellae542ff2010-01-13 16:29:27 -0800445 reset();
446 break;
447
Adam Powelle33cef82011-02-23 16:51:20 -0800448 case MotionEvent.ACTION_UP:
449 reset();
450 break;
451
452 case MotionEvent.ACTION_MOVE: {
Adam Powellae542ff2010-01-13 16:29:27 -0800453 setContext(event);
454
455 // Only accept the event if our relative pressure is within
456 // a certain limit - this can help filter shaky data as a
457 // finger is lifted.
458 if (mCurrPressure / mPrevPressure > PRESSURE_THRESHOLD) {
459 final boolean updatePrevious = mListener.onScale(this);
460
461 if (updatePrevious) {
462 mPrevEvent.recycle();
463 mPrevEvent = MotionEvent.obtain(event);
464 }
465 }
Adam Powelle33cef82011-02-23 16:51:20 -0800466 }
467 break;
Adam Powellae542ff2010-01-13 16:29:27 -0800468 }
469 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -0700470
471 if (!handled && mInputEventConsistencyVerifier != null) {
472 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
473 }
Adam Powellae542ff2010-01-13 16:29:27 -0800474 return handled;
475 }
Erik47c41e82010-09-01 15:39:08 -0700476
Adam Powelle33cef82011-02-23 16:51:20 -0800477 private int findNewActiveIndex(MotionEvent ev, int otherActiveId, int oldIndex) {
478 final int pointerCount = ev.getPointerCount();
479
480 // It's ok if this isn't found and returns -1, it simply won't match.
481 final int otherActiveIndex = ev.findPointerIndex(otherActiveId);
482 int newActiveIndex = -1;
483
484 // Pick a new id and update tracking state. Only pick pointers not on the slop edges.
485 for (int i = 0; i < pointerCount; i++) {
486 if (i != oldIndex && i != otherActiveIndex) {
487 final float edgeSlop = mEdgeSlop;
488 final float rightSlop = mRightSlopEdge;
489 final float bottomSlop = mBottomSlopEdge;
490 float x = getRawX(ev, i);
491 float y = getRawY(ev, i);
492 if (x >= edgeSlop && y >= edgeSlop && x <= rightSlop && y <= bottomSlop) {
493 newActiveIndex = i;
494 break;
495 }
496 }
497 }
498
499 return newActiveIndex;
500 }
501
Adam Powell380b5252010-01-19 16:33:46 -0800502 /**
Erik47c41e82010-09-01 15:39:08 -0700503 * MotionEvent has no getRawX(int) method; simulate it pending future API approval.
Adam Powell380b5252010-01-19 16:33:46 -0800504 */
505 private static float getRawX(MotionEvent event, int pointerIndex) {
Adam Powell0fe4a132011-02-28 13:32:17 -0800506 if (pointerIndex < 0) return Float.MIN_VALUE;
Adam Powelle33cef82011-02-23 16:51:20 -0800507 if (pointerIndex == 0) return event.getRawX();
Adam Powell9bccdb72010-08-10 17:50:11 -0700508 float offset = event.getRawX() - event.getX();
Adam Powell380b5252010-01-19 16:33:46 -0800509 return event.getX(pointerIndex) + offset;
510 }
Erik47c41e82010-09-01 15:39:08 -0700511
Adam Powell380b5252010-01-19 16:33:46 -0800512 /**
Erik47c41e82010-09-01 15:39:08 -0700513 * MotionEvent has no getRawY(int) method; simulate it pending future API approval.
Adam Powell380b5252010-01-19 16:33:46 -0800514 */
515 private static float getRawY(MotionEvent event, int pointerIndex) {
Adam Powell0fe4a132011-02-28 13:32:17 -0800516 if (pointerIndex < 0) return Float.MIN_VALUE;
Adam Powelle33cef82011-02-23 16:51:20 -0800517 if (pointerIndex == 0) return event.getRawY();
Adam Powell9bccdb72010-08-10 17:50:11 -0700518 float offset = event.getRawY() - event.getY();
Adam Powell380b5252010-01-19 16:33:46 -0800519 return event.getY(pointerIndex) + offset;
520 }
Adam Powellae542ff2010-01-13 16:29:27 -0800521
522 private void setContext(MotionEvent curr) {
523 if (mCurrEvent != null) {
524 mCurrEvent.recycle();
525 }
526 mCurrEvent = MotionEvent.obtain(curr);
527
528 mCurrLen = -1;
529 mPrevLen = -1;
530 mScaleFactor = -1;
531
532 final MotionEvent prev = mPrevEvent;
533
Adam Powelle33cef82011-02-23 16:51:20 -0800534 final int prevIndex0 = prev.findPointerIndex(mActiveId0);
535 final int prevIndex1 = prev.findPointerIndex(mActiveId1);
536 final int currIndex0 = curr.findPointerIndex(mActiveId0);
537 final int currIndex1 = curr.findPointerIndex(mActiveId1);
538
Adam Powelld0197f32011-03-17 00:09:03 -0700539 if (prevIndex0 < 0 || prevIndex1 < 0 || currIndex0 < 0 || currIndex1 < 0) {
540 mInvalidGesture = true;
541 Log.e(TAG, "Invalid MotionEvent stream detected.", new Throwable());
542 if (mGestureInProgress) {
543 mListener.onScaleEnd(this);
544 }
545 return;
546 }
547
Adam Powelle33cef82011-02-23 16:51:20 -0800548 final float px0 = prev.getX(prevIndex0);
549 final float py0 = prev.getY(prevIndex0);
550 final float px1 = prev.getX(prevIndex1);
551 final float py1 = prev.getY(prevIndex1);
552 final float cx0 = curr.getX(currIndex0);
553 final float cy0 = curr.getY(currIndex0);
554 final float cx1 = curr.getX(currIndex1);
555 final float cy1 = curr.getY(currIndex1);
Adam Powellae542ff2010-01-13 16:29:27 -0800556
557 final float pvx = px1 - px0;
558 final float pvy = py1 - py0;
559 final float cvx = cx1 - cx0;
560 final float cvy = cy1 - cy0;
561 mPrevFingerDiffX = pvx;
562 mPrevFingerDiffY = pvy;
563 mCurrFingerDiffX = cvx;
564 mCurrFingerDiffY = cvy;
565
566 mFocusX = cx0 + cvx * 0.5f;
567 mFocusY = cy0 + cvy * 0.5f;
568 mTimeDelta = curr.getEventTime() - prev.getEventTime();
Adam Powelle33cef82011-02-23 16:51:20 -0800569 mCurrPressure = curr.getPressure(currIndex0) + curr.getPressure(currIndex1);
570 mPrevPressure = prev.getPressure(prevIndex0) + prev.getPressure(prevIndex1);
Adam Powellae542ff2010-01-13 16:29:27 -0800571 }
572
573 private void reset() {
574 if (mPrevEvent != null) {
575 mPrevEvent.recycle();
576 mPrevEvent = null;
577 }
578 if (mCurrEvent != null) {
579 mCurrEvent.recycle();
580 mCurrEvent = null;
581 }
Adam Powell380b5252010-01-19 16:33:46 -0800582 mSloppyGesture = false;
583 mGestureInProgress = false;
Adam Powelle33cef82011-02-23 16:51:20 -0800584 mActiveId0 = -1;
585 mActiveId1 = -1;
Adam Powelld0197f32011-03-17 00:09:03 -0700586 mInvalidGesture = false;
Adam Powellae542ff2010-01-13 16:29:27 -0800587 }
588
589 /**
590 * Returns {@code true} if a two-finger scale gesture is in progress.
591 * @return {@code true} if a scale gesture is in progress, {@code false} otherwise.
592 */
593 public boolean isInProgress() {
594 return mGestureInProgress;
595 }
596
597 /**
598 * Get the X coordinate of the current gesture's focal point.
599 * If a gesture is in progress, the focal point is directly between
600 * the two pointers forming the gesture.
601 * If a gesture is ending, the focal point is the location of the
602 * remaining pointer on the screen.
Adam Powellab905c872010-02-03 11:01:58 -0800603 * If {@link #isInProgress()} would return false, the result of this
Adam Powellae542ff2010-01-13 16:29:27 -0800604 * function is undefined.
Erik47c41e82010-09-01 15:39:08 -0700605 *
Adam Powellae542ff2010-01-13 16:29:27 -0800606 * @return X coordinate of the focal point in pixels.
607 */
608 public float getFocusX() {
609 return mFocusX;
610 }
611
612 /**
613 * Get the Y coordinate of the current gesture's focal point.
614 * If a gesture is in progress, the focal point is directly between
615 * the two pointers forming the gesture.
616 * If a gesture is ending, the focal point is the location of the
617 * remaining pointer on the screen.
Adam Powellab905c872010-02-03 11:01:58 -0800618 * If {@link #isInProgress()} would return false, the result of this
Adam Powellae542ff2010-01-13 16:29:27 -0800619 * function is undefined.
Erik47c41e82010-09-01 15:39:08 -0700620 *
Adam Powellae542ff2010-01-13 16:29:27 -0800621 * @return Y coordinate of the focal point in pixels.
622 */
623 public float getFocusY() {
624 return mFocusY;
625 }
626
627 /**
628 * Return the current distance between the two pointers forming the
629 * gesture in progress.
Erik47c41e82010-09-01 15:39:08 -0700630 *
Adam Powellae542ff2010-01-13 16:29:27 -0800631 * @return Distance between pointers in pixels.
632 */
633 public float getCurrentSpan() {
634 if (mCurrLen == -1) {
635 final float cvx = mCurrFingerDiffX;
636 final float cvy = mCurrFingerDiffY;
Adam Powell346c8fb2010-03-12 10:52:35 -0800637 mCurrLen = FloatMath.sqrt(cvx*cvx + cvy*cvy);
Adam Powellae542ff2010-01-13 16:29:27 -0800638 }
639 return mCurrLen;
640 }
641
642 /**
Erik47c41e82010-09-01 15:39:08 -0700643 * Return the current x distance between the two pointers forming the
644 * gesture in progress.
645 *
646 * @return Distance between pointers in pixels.
647 */
648 public float getCurrentSpanX() {
649 return mCurrFingerDiffX;
650 }
651
652 /**
653 * Return the current y distance between the two pointers forming the
654 * gesture in progress.
655 *
656 * @return Distance between pointers in pixels.
657 */
658 public float getCurrentSpanY() {
659 return mCurrFingerDiffY;
660 }
661
662 /**
Adam Powellae542ff2010-01-13 16:29:27 -0800663 * Return the previous distance between the two pointers forming the
664 * gesture in progress.
Erik47c41e82010-09-01 15:39:08 -0700665 *
Adam Powellae542ff2010-01-13 16:29:27 -0800666 * @return Previous distance between pointers in pixels.
667 */
668 public float getPreviousSpan() {
669 if (mPrevLen == -1) {
670 final float pvx = mPrevFingerDiffX;
671 final float pvy = mPrevFingerDiffY;
Adam Powell346c8fb2010-03-12 10:52:35 -0800672 mPrevLen = FloatMath.sqrt(pvx*pvx + pvy*pvy);
Adam Powellae542ff2010-01-13 16:29:27 -0800673 }
674 return mPrevLen;
675 }
676
677 /**
Erik47c41e82010-09-01 15:39:08 -0700678 * Return the previous x distance between the two pointers forming the
679 * gesture in progress.
680 *
681 * @return Previous distance between pointers in pixels.
682 */
683 public float getPreviousSpanX() {
684 return mPrevFingerDiffX;
685 }
686
687 /**
688 * Return the previous y distance between the two pointers forming the
689 * gesture in progress.
690 *
691 * @return Previous distance between pointers in pixels.
692 */
693 public float getPreviousSpanY() {
694 return mPrevFingerDiffY;
695 }
696
697 /**
Adam Powellae542ff2010-01-13 16:29:27 -0800698 * Return the scaling factor from the previous scale event to the current
699 * event. This value is defined as
Adam Powellab905c872010-02-03 11:01:58 -0800700 * ({@link #getCurrentSpan()} / {@link #getPreviousSpan()}).
Erik47c41e82010-09-01 15:39:08 -0700701 *
Adam Powellae542ff2010-01-13 16:29:27 -0800702 * @return The current scaling factor.
703 */
704 public float getScaleFactor() {
705 if (mScaleFactor == -1) {
706 mScaleFactor = getCurrentSpan() / getPreviousSpan();
707 }
708 return mScaleFactor;
709 }
Erik47c41e82010-09-01 15:39:08 -0700710
Adam Powellae542ff2010-01-13 16:29:27 -0800711 /**
712 * Return the time difference in milliseconds between the previous
713 * accepted scaling event and the current scaling event.
Erik47c41e82010-09-01 15:39:08 -0700714 *
Adam Powellae542ff2010-01-13 16:29:27 -0800715 * @return Time difference since the last scaling event in milliseconds.
716 */
717 public long getTimeDelta() {
718 return mTimeDelta;
719 }
Erik47c41e82010-09-01 15:39:08 -0700720
Adam Powellae542ff2010-01-13 16:29:27 -0800721 /**
722 * Return the event time of the current event being processed.
Erik47c41e82010-09-01 15:39:08 -0700723 *
Adam Powellae542ff2010-01-13 16:29:27 -0800724 * @return Current event time in milliseconds.
725 */
726 public long getEventTime() {
727 return mCurrEvent.getEventTime();
728 }
729}