Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/ContentViewGestureHandler.java

Issue 150983003: [Android] Share gesture types between Java and C++ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix WebView Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.content.browser; 5 package org.chromium.content.browser;
6 6
7 import android.annotation.TargetApi; 7 import android.annotation.TargetApi;
8 import android.content.Context; 8 import android.content.Context;
9 import android.os.Build; 9 import android.os.Build;
10 import android.os.Bundle; 10 import android.os.Bundle;
(...skipping 12 matching lines...) Expand all
23 import org.chromium.content.common.TraceEvent; 23 import org.chromium.content.common.TraceEvent;
24 24
25 /** 25 /**
26 * This class handles all MotionEvent handling done in ContentViewCore including the gesture 26 * This class handles all MotionEvent handling done in ContentViewCore including the gesture
27 * recognition. It sends all related native calls through the interface MotionEv entDelegate. 27 * recognition. It sends all related native calls through the interface MotionEv entDelegate.
28 */ 28 */
29 class ContentViewGestureHandler { 29 class ContentViewGestureHandler {
30 30
31 private static final String TAG = "ContentViewGestureHandler"; 31 private static final String TAG = "ContentViewGestureHandler";
32 /** 32 /**
33 * Used for GESTURE_FLING_START x velocity 33 * Used for FLING_START x velocity
34 */ 34 */
35 static final String VELOCITY_X = "Velocity X"; 35 static final String VELOCITY_X = "Velocity X";
36 /** 36 /**
37 * Used for GESTURE_FLING_START y velocity 37 * Used for FLING_START y velocity
38 */ 38 */
39 static final String VELOCITY_Y = "Velocity Y"; 39 static final String VELOCITY_Y = "Velocity Y";
40 /** 40 /**
41 * Used for GESTURE_SCROLL_BY x distance (scroll offset of update) 41 * Used for SCROLL_BY x distance (scroll offset of update)
42 */ 42 */
43 static final String DISTANCE_X = "Distance X"; 43 static final String DISTANCE_X = "Distance X";
44 /** 44 /**
45 * Used for GESTURE_SCROLL_BY y distance (scroll offset of update) 45 * Used for SCROLL_BY y distance (scroll offset of update)
46 */ 46 */
47 static final String DISTANCE_Y = "Distance Y"; 47 static final String DISTANCE_Y = "Distance Y";
48 /** 48 /**
49 * Used for GESTURE_SCROLL_START delta X hint (movement triggering scroll) 49 * Used for SCROLL_START delta X hint (movement triggering scroll)
50 */ 50 */
51 static final String DELTA_HINT_X = "Delta Hint X"; 51 static final String DELTA_HINT_X = "Delta Hint X";
52 /** 52 /**
53 * Used for GESTURE_SCROLL_START delta Y hint (movement triggering scroll) 53 * Used for SCROLL_START delta Y hint (movement triggering scroll)
54 */ 54 */
55 static final String DELTA_HINT_Y = "Delta Hint Y"; 55 static final String DELTA_HINT_Y = "Delta Hint Y";
56 /** 56 /**
57 * Used in GESTURE_SINGLE_TAP_CONFIRMED to check whether ShowPress has been called before. 57 * Used in SINGLE_TAP_CONFIRMED to check whether ShowPress has been called b efore.
58 */ 58 */
59 static final String SHOW_PRESS = "ShowPress"; 59 static final String SHOW_PRESS = "ShowPress";
60 /** 60 /**
61 * Used for GESTURE_PINCH_BY delta 61 * Used for PINCH_BY delta
62 */ 62 */
63 static final String DELTA = "Delta"; 63 static final String DELTA = "Delta";
64 64
65 private final Bundle mExtraParamBundleSingleTap; 65 private final Bundle mExtraParamBundleSingleTap;
66 private final Bundle mExtraParamBundleFling; 66 private final Bundle mExtraParamBundleFling;
67 private final Bundle mExtraParamBundleScroll; 67 private final Bundle mExtraParamBundleScroll;
68 private final Bundle mExtraParamBundleScrollStart; 68 private final Bundle mExtraParamBundleScrollStart;
69 private final Bundle mExtraParamBundleDoubleTapDragZoom; 69 private final Bundle mExtraParamBundleDoubleTapDragZoom;
70 private final Bundle mExtraParamBundlePinchBy; 70 private final Bundle mExtraParamBundlePinchBy;
71 private GestureDetector mGestureDetector; 71 private GestureDetector mGestureDetector;
72 private OnGestureListener mListener; 72 private OnGestureListener mListener;
73 private OnDoubleTapListener mDoubleTapListener; 73 private OnDoubleTapListener mDoubleTapListener;
74 private ScaleGestureDetector mMultiTouchDetector; 74 private ScaleGestureDetector mMultiTouchDetector;
75 private ScaleGestureListener mMultiTouchListener; 75 private ScaleGestureListener mMultiTouchListener;
76 private MotionEvent mCurrentDownEvent; 76 private MotionEvent mCurrentDownEvent;
77 private final MotionEventDelegate mMotionEventDelegate; 77 private final MotionEventDelegate mMotionEventDelegate;
78 78
79 // Remember whether onShowPress() is called. If it is not, in onSingleTapCon firmed() 79 // Remember whether onShowPress() is called. If it is not, in onSingleTapCon firmed()
80 // we will first show the press state, then trigger the click. 80 // we will first show the press state, then trigger the click.
81 private boolean mShowPressIsCalled; 81 private boolean mShowPressIsCalled;
82 82
83 // Whether a sent GESTURE_TAP_DOWN event has yet to be accompanied by a corr esponding 83 // Whether a sent TAP_DOWN event has yet to be accompanied by a correspondin g
84 // GESTURE_SINGLE_TAP_UP, GESTURE_SINGLE_TAP_CONFIRMED, GESTURE_TAP_CANCEL o r 84 // SINGLE_TAP_UP, SINGLE_TAP_CONFIRMED, TAP_CANCEL or DOUBLE_TAP.
85 // GESTURE_DOUBLE_TAP.
86 private boolean mNeedsTapEndingEvent; 85 private boolean mNeedsTapEndingEvent;
87 86
88 // This flag is used for ignoring the remaining touch events, i.e., All the events until the 87 // This flag is used for ignoring the remaining touch events, i.e., All the events until the
89 // next ACTION_DOWN. This is automatically set to false on the next ACTION_D OWN. 88 // next ACTION_DOWN. This is automatically set to false on the next ACTION_D OWN.
90 private boolean mIgnoreRemainingTouchEvents; 89 private boolean mIgnoreRemainingTouchEvents;
91 90
92 // TODO(klobag): this is to avoid a bug in GestureDetector. With multi-touch , 91 // TODO(klobag): this is to avoid a bug in GestureDetector. With multi-touch ,
93 // mAlwaysInTapRegion is not reset. So when the last finger is up, onSingleT apUp() 92 // mAlwaysInTapRegion is not reset. So when the last finger is up, onSingleT apUp()
94 // will be mistakenly fired. 93 // will be mistakenly fired.
95 private boolean mIgnoreSingleTap; 94 private boolean mIgnoreSingleTap;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 // Used to track the accumulated scroll error over time. This is used to rem ove the 130 // Used to track the accumulated scroll error over time. This is used to rem ove the
132 // rounding error we introduced by passing integers to webkit. 131 // rounding error we introduced by passing integers to webkit.
133 private float mAccumulatedScrollErrorX = 0; 132 private float mAccumulatedScrollErrorX = 0;
134 private float mAccumulatedScrollErrorY = 0; 133 private float mAccumulatedScrollErrorY = 0;
135 134
136 // The page's viewport and scale sometimes allow us to disable double tap ge sture detection, 135 // The page's viewport and scale sometimes allow us to disable double tap ge sture detection,
137 // according to the logic in ContentViewCore.onRenderCoordinatesUpdated(). 136 // according to the logic in ContentViewCore.onRenderCoordinatesUpdated().
138 private boolean mShouldDisableDoubleTap; 137 private boolean mShouldDisableDoubleTap;
139 138
140 // Keeps track of the last long press event, if we end up opening a context menu, we would need 139 // Keeps track of the last long press event, if we end up opening a context menu, we would need
141 // to potentially use the event to send GESTURE_TAP_CANCEL to remove ::activ e styling 140 // to potentially use the event to send TAP_CANCEL to remove ::active stylin g
142 private MotionEvent mLastLongPressEvent; 141 private MotionEvent mLastLongPressEvent;
143 142
144 // Whether the click delay should always be disabled by sending clicks for d ouble tap gestures. 143 // Whether the click delay should always be disabled by sending clicks for d ouble tap gestures.
145 private final boolean mDisableClickDelay; 144 private final boolean mDisableClickDelay;
146 145
147 // DO NOT change these constants without also changing the corresponding val ues
148 // found in content_view_core_impl.cc
149 // TODO(jdduke): Initialize these from their corresponding WebInputEvent::Ty pe, crbug/339530.
150 static final int GESTURE_SHOW_PRESS = 0;
151 static final int GESTURE_DOUBLE_TAP = 1;
152 static final int GESTURE_SINGLE_TAP_UP = 2;
153 static final int GESTURE_SINGLE_TAP_CONFIRMED = 3;
154 static final int GESTURE_SINGLE_TAP_UNCONFIRMED = 4;
155 static final int GESTURE_LONG_PRESS = 5;
156 static final int GESTURE_SCROLL_START = 6;
157 static final int GESTURE_SCROLL_BY = 7;
158 static final int GESTURE_SCROLL_END = 8;
159 static final int GESTURE_FLING_START = 9;
160 static final int GESTURE_FLING_CANCEL = 10;
161 static final int GESTURE_FLING_END = 11;
162 static final int GESTURE_PINCH_BEGIN = 12;
163 static final int GESTURE_PINCH_BY = 13;
164 static final int GESTURE_PINCH_END = 14;
165 static final int GESTURE_TAP_CANCEL = 15;
166 static final int GESTURE_LONG_TAP = 16;
167 static final int GESTURE_TAP_DOWN = 17;
168
169 private final float mPxToDp; 146 private final float mPxToDp;
170 147
171 static final int DOUBLE_TAP_MODE_NONE = 0; 148 static final int DOUBLE_TAP_MODE_NONE = 0;
172 static final int DOUBLE_TAP_MODE_DRAG_DETECTION_IN_PROGRESS = 1; 149 static final int DOUBLE_TAP_MODE_DRAG_DETECTION_IN_PROGRESS = 1;
173 static final int DOUBLE_TAP_MODE_DRAG_ZOOM = 2; 150 static final int DOUBLE_TAP_MODE_DRAG_ZOOM = 2;
174 static final int DOUBLE_TAP_MODE_DISABLED = 3; 151 static final int DOUBLE_TAP_MODE_DISABLED = 3;
175 152
176 /** 153 /**
177 * This is an interface to handle MotionEvent related communication with the native side also 154 * This is an interface to handle MotionEvent related communication with the native side also
178 * access some ContentView specific parameters. 155 * access some ContentView specific parameters.
179 */ 156 */
180 public interface MotionEventDelegate { 157 public interface MotionEventDelegate {
181 /** 158 /**
182 * Signal the start of gesture detection for the provided {@link MotionE vent}. 159 * Signal the start of gesture detection for the provided {@link MotionE vent}.
183 * @param event The {@link MotionEvent} being fed to the gesture detecto rs. 160 * @param event The {@link MotionEvent} being fed to the gesture detecto rs.
184 */ 161 */
185 public void onTouchEventHandlingBegin(MotionEvent event); 162 public void onTouchEventHandlingBegin(MotionEvent event);
186 163
187 /** 164 /**
188 * Signal that all gestures for the current {@link MotionEvent} have bee n dispatched. 165 * Signal that all gestures for the current {@link MotionEvent} have bee n dispatched.
189 */ 166 */
190 public void onTouchEventHandlingEnd(); 167 public void onTouchEventHandlingEnd();
191 168
192 /** 169 /**
193 * Forward a generated event to the client. This will normally be wrapp ed by 170 * Forward a generated event to the client. This will normally be wrapp ed by
194 * calls to {@link #onTouchEventHandlingBegin(MotionEvent)} and 171 * calls to {@link #onTouchEventHandlingBegin(MotionEvent)} and
195 * {@link #onTouchEventHandlingEnd()}, unless the gesture is generated f rom 172 * {@link #onTouchEventHandlingEnd()}, unless the gesture is generated f rom
196 * a touch timeout, e.g., GESTURE_LONG_PRESS. 173 * a touch timeout, e.g., LONG_PRESS.
197 * @param type The type of the gesture event. 174 * @param type The type of the gesture event.
198 * @param timeMs The time the gesture event occurred at. 175 * @param timeMs The time the gesture event occurred at.
199 * @param x The x location for the gesture event. 176 * @param x The x location for the gesture event.
200 * @param y The y location for the gesture event. 177 * @param y The y location for the gesture event.
201 * @param extraParams A bundle that holds specific extra parameters for certain gestures. 178 * @param extraParams A bundle that holds specific extra parameters for certain gestures.
202 * This is read-only and should not be modified in th is function. 179 * This is read-only and should not be modified in th is function.
203 * Refer to gesture type definition for more information. 180 * Refer to gesture type definition for more information.
204 * @return Whether the gesture was forwarded successfully. 181 * @return Whether the gesture was forwarded successfully.
205 */ 182 */
206 boolean onGestureEventCreated(int type, long timeMs, int x, int y, Bundl e extraParams); 183 boolean onGestureEventCreated(int type, long timeMs, int x, int y, Bundl e extraParams);
(...skipping 29 matching lines...) Expand all
236 mShowPressIsCalled = false; 213 mShowPressIsCalled = false;
237 mIgnoreSingleTap = false; 214 mIgnoreSingleTap = false;
238 mTouchScrolling = false; 215 mTouchScrolling = false;
239 mSeenFirstScrollEvent = false; 216 mSeenFirstScrollEvent = false;
240 mLastRawX = e.getRawX(); 217 mLastRawX = e.getRawX();
241 mLastRawY = e.getRawY(); 218 mLastRawY = e.getRawY();
242 mAccumulatedScrollErrorX = 0; 219 mAccumulatedScrollErrorX = 0;
243 mAccumulatedScrollErrorY = 0; 220 mAccumulatedScrollErrorY = 0;
244 mLastLongPressEvent = null; 221 mLastLongPressEvent = null;
245 mNeedsTapEndingEvent = false; 222 mNeedsTapEndingEvent = false;
246 if (sendMotionEventAsGesture(GESTURE_TAP_DOWN, e, null)) { 223 if (sendMotionEventAsGesture(GestureEventType.TAP_DOWN, e, null)) {
247 mNeedsTapEndingEvent = true; 224 mNeedsTapEndingEvent = true;
248 } 225 }
249 // Return true to indicate that we want to handle touch 226 // Return true to indicate that we want to handle touch
250 return true; 227 return true;
251 } 228 }
252 229
253 @Override 230 @Override
254 public boolean onScroll(MotionEvent e1, MotionEvent e2, 231 public boolean onScroll(MotionEvent e1, MotionEvent e2,
255 float rawDistanceX, float rawDistanceY) { 232 float rawDistanceX, float rawDistanceY) {
256 assert e1.getEventTime() <= e2.getEventTime(); 233 assert e1.getEventTime() <= e2.getEventTime();
(...skipping 23 matching lines...) Expand all
280 257
281 mLastRawX = e2.getRawX(); 258 mLastRawX = e2.getRawX();
282 mLastRawY = e2.getRawY(); 259 mLastRawY = e2.getRawY();
283 if (!mTouchScrolling) { 260 if (!mTouchScrolling) {
284 sendTapCancelIfNecessary(e1); 261 sendTapCancelIfNecessary(e1);
285 // Note that scroll start hints are in distance trav eled, where 262 // Note that scroll start hints are in distance trav eled, where
286 // scroll deltas are in the opposite direction. 263 // scroll deltas are in the opposite direction.
287 mExtraParamBundleScrollStart.putInt(DELTA_HINT_X, (i nt) -rawDistanceX); 264 mExtraParamBundleScrollStart.putInt(DELTA_HINT_X, (i nt) -rawDistanceX);
288 mExtraParamBundleScrollStart.putInt(DELTA_HINT_Y, (i nt) -rawDistanceY); 265 mExtraParamBundleScrollStart.putInt(DELTA_HINT_Y, (i nt) -rawDistanceY);
289 assert mExtraParamBundleScrollStart.size() == 2; 266 assert mExtraParamBundleScrollStart.size() == 2;
290 if (sendGesture(GESTURE_SCROLL_START, e2.getEventTim e(), 267 if (sendGesture(GestureEventType.SCROLL_START, e2.ge tEventTime(),
291 (int) e1.getX(), (int) e1.getY(), 268 (int) e1.getX(), (int) e1.getY(),
292 mExtraParamBundleScrollStart)) { 269 mExtraParamBundleScrollStart)) {
293 mTouchScrolling = true; 270 mTouchScrolling = true;
294 } 271 }
295 } 272 }
296 273
297 // distanceX and distanceY is the scrolling offset since last onScroll. 274 // distanceX and distanceY is the scrolling offset since last onScroll.
298 // Because we are passing integers to webkit, this could introduce 275 // Because we are passing integers to webkit, this could introduce
299 // rounding errors. The rounding errors will accumulate overtime. 276 // rounding errors. The rounding errors will accumulate overtime.
300 // To solve this, we should be adding back the rounding errors each time 277 // To solve this, we should be adding back the rounding errors each time
301 // when we calculate the new offset. 278 // when we calculate the new offset.
302 int x = (int) e2.getX(); 279 int x = (int) e2.getX();
303 int y = (int) e2.getY(); 280 int y = (int) e2.getY();
304 int dx = (int) (distanceX + mAccumulatedScrollErrorX); 281 int dx = (int) (distanceX + mAccumulatedScrollErrorX);
305 int dy = (int) (distanceY + mAccumulatedScrollErrorY); 282 int dy = (int) (distanceY + mAccumulatedScrollErrorY);
306 mAccumulatedScrollErrorX = distanceX + mAccumulatedScrol lErrorX - dx; 283 mAccumulatedScrollErrorX = distanceX + mAccumulatedScrol lErrorX - dx;
307 mAccumulatedScrollErrorY = distanceY + mAccumulatedScrol lErrorY - dy; 284 mAccumulatedScrollErrorY = distanceY + mAccumulatedScrol lErrorY - dy;
308 285
309 mExtraParamBundleScroll.putInt(DISTANCE_X, dx); 286 mExtraParamBundleScroll.putInt(DISTANCE_X, dx);
310 mExtraParamBundleScroll.putInt(DISTANCE_Y, dy); 287 mExtraParamBundleScroll.putInt(DISTANCE_Y, dy);
311 assert mExtraParamBundleScroll.size() == 2; 288 assert mExtraParamBundleScroll.size() == 2;
312 289
313 if ((dx | dy) != 0) { 290 if ((dx | dy) != 0) {
314 sendGesture(GESTURE_SCROLL_BY, 291 sendGesture(GestureEventType.SCROLL_BY,
315 e2.getEventTime(), x, y, mExtraParamBundleSc roll); 292 e2.getEventTime(), x, y, mExtraParamBundleSc roll);
316 } 293 }
317 294
318 return true; 295 return true;
319 } 296 }
320 297
321 @Override 298 @Override
322 public boolean onFling(MotionEvent e1, MotionEvent e2, 299 public boolean onFling(MotionEvent e1, MotionEvent e2,
323 float velocityX, float velocityY) { 300 float velocityX, float velocityY) {
324 assert e1.getEventTime() <= e2.getEventTime(); 301 assert e1.getEventTime() <= e2.getEventTime();
325 if (mSnapScrollController.isSnappingScrolls()) { 302 if (mSnapScrollController.isSnappingScrolls()) {
326 if (mSnapScrollController.isSnapHorizontal()) { 303 if (mSnapScrollController.isSnapHorizontal()) {
327 velocityY = 0; 304 velocityY = 0;
328 } else { 305 } else {
329 velocityX = 0; 306 velocityX = 0;
330 } 307 }
331 } 308 }
332 309
333 fling(e2.getEventTime(), (int) e1.getX(0), (int) e1.getY (0), 310 fling(e2.getEventTime(), (int) e1.getX(0), (int) e1.getY (0),
334 (int) velocityX, (int) velocityY); 311 (int) velocityX, (int) velocityY);
335 return true; 312 return true;
336 } 313 }
337 314
338 @Override 315 @Override
339 public void onShowPress(MotionEvent e) { 316 public void onShowPress(MotionEvent e) {
340 mShowPressIsCalled = true; 317 mShowPressIsCalled = true;
341 sendMotionEventAsGesture(GESTURE_SHOW_PRESS, e, null); 318 sendMotionEventAsGesture(GestureEventType.SHOW_PRESS, e, null);
342 } 319 }
343 320
344 @Override 321 @Override
345 public boolean onSingleTapUp(MotionEvent e) { 322 public boolean onSingleTapUp(MotionEvent e) {
346 if (isDistanceBetweenDownAndUpTooLong(e.getRawX(), e.get RawY())) { 323 if (isDistanceBetweenDownAndUpTooLong(e.getRawX(), e.get RawY())) {
347 sendTapCancelIfNecessary(e); 324 sendTapCancelIfNecessary(e);
348 mIgnoreSingleTap = true; 325 mIgnoreSingleTap = true;
349 return true; 326 return true;
350 } 327 }
351 // This is a hack to address the issue where user hovers 328 // This is a hack to address the issue where user hovers
352 // over a link for longer than DOUBLE_TAP_TIMEOUT, then 329 // over a link for longer than DOUBLE_TAP_TIMEOUT, then
353 // onSingleTapConfirmed() is not triggered. But we still 330 // onSingleTapConfirmed() is not triggered. But we still
354 // want to trigger the tap event at UP. So we override 331 // want to trigger the tap event at UP. So we override
355 // onSingleTapUp() in this case. This assumes singleTapU p 332 // onSingleTapUp() in this case. This assumes singleTapU p
356 // gets always called before singleTapConfirmed. 333 // gets always called before singleTapConfirmed.
357 if (!mIgnoreSingleTap) { 334 if (!mIgnoreSingleTap) {
358 if (e.getEventTime() - e.getDownTime() > DOUBLE_TAP_ TIMEOUT) { 335 if (e.getEventTime() - e.getDownTime() > DOUBLE_TAP_ TIMEOUT) {
359 if (sendTapEndingEventAsGesture(GESTURE_SINGLE_T AP_UP, e, null)) { 336 if (sendTapEndingEventAsGesture(
337 GestureEventType.SINGLE_TAP_UP, e, n ull)) {
360 mIgnoreSingleTap = true; 338 mIgnoreSingleTap = true;
361 } 339 }
362 return true; 340 return true;
363 } else if (isDoubleTapDisabled() || mDisableClickDel ay) { 341 } else if (isDoubleTapDisabled() || mDisableClickDel ay) {
364 // If double tap has been disabled, there is no need to wait 342 // If double tap has been disabled, there is no need to wait
365 // for the double tap timeout. 343 // for the double tap timeout.
366 return onSingleTapConfirmed(e); 344 return onSingleTapConfirmed(e);
367 } else { 345 } else {
368 // Notify Blink about this tapUp event anyway, 346 // Notify Blink about this tapUp event anyway,
369 // when none of the above conditions applied. 347 // when none of the above conditions applied.
370 sendMotionEventAsGesture(GESTURE_SINGLE_TAP_UNCO NFIRMED, e, null); 348 sendMotionEventAsGesture(
349 GestureEventType.SINGLE_TAP_UNCONFIRMED, e, null);
371 } 350 }
372 } 351 }
373 352
374 return triggerLongTapIfNeeded(e); 353 return triggerLongTapIfNeeded(e);
375 } 354 }
376 355
377 @Override 356 @Override
378 public boolean onSingleTapConfirmed(MotionEvent e) { 357 public boolean onSingleTapConfirmed(MotionEvent e) {
379 // Long taps in the edges of the screen have their event s delayed by 358 // Long taps in the edges of the screen have their event s delayed by
380 // ContentViewHolder for tab swipe operations. As a cons equence of the delay 359 // ContentViewHolder for tab swipe operations. As a cons equence of the delay
381 // this method might be called after receiving the up ev ent. 360 // this method might be called after receiving the up ev ent.
382 // These corner cases should be ignored. 361 // These corner cases should be ignored.
383 if (mIgnoreSingleTap) return true; 362 if (mIgnoreSingleTap) return true;
384 363
385 mExtraParamBundleSingleTap.putBoolean(SHOW_PRESS, mShowP ressIsCalled); 364 mExtraParamBundleSingleTap.putBoolean(SHOW_PRESS, mShowP ressIsCalled);
386 assert mExtraParamBundleSingleTap.size() == 1; 365 assert mExtraParamBundleSingleTap.size() == 1;
387 if (sendTapEndingEventAsGesture(GESTURE_SINGLE_TAP_CONFI RMED, e, 366 if (sendTapEndingEventAsGesture(GestureEventType.SINGLE_ TAP_CONFIRMED, e,
388 mExtraParamBundleSingleTap)) { 367 mExtraParamBundleSingleTap)) {
389 mIgnoreSingleTap = true; 368 mIgnoreSingleTap = true;
390 } 369 }
391 return true; 370 return true;
392 } 371 }
393 372
394 @Override 373 @Override
395 public boolean onDoubleTapEvent(MotionEvent e) { 374 public boolean onDoubleTapEvent(MotionEvent e) {
396 switch (e.getActionMasked()) { 375 switch (e.getActionMasked()) {
397 case MotionEvent.ACTION_DOWN: 376 case MotionEvent.ACTION_DOWN:
(...skipping 18 matching lines...) Expand all
416 395
417 // Begin double tap drag zoom mode if the mo ve distance is 396 // Begin double tap drag zoom mode if the mo ve distance is
418 // further than the threshold. 397 // further than the threshold.
419 if (isDistanceGreaterThanTouchSlop(distanceX , distanceY)) { 398 if (isDistanceGreaterThanTouchSlop(distanceX , distanceY)) {
420 sendTapCancelIfNecessary(e); 399 sendTapCancelIfNecessary(e);
421 mExtraParamBundleScrollStart.putInt(DELT A_HINT_X, 400 mExtraParamBundleScrollStart.putInt(DELT A_HINT_X,
422 (int) -distanceX); 401 (int) -distanceX);
423 mExtraParamBundleScrollStart.putInt(DELT A_HINT_Y, 402 mExtraParamBundleScrollStart.putInt(DELT A_HINT_Y,
424 (int) -distanceY); 403 (int) -distanceY);
425 assert mExtraParamBundleScrollStart.size () == 2; 404 assert mExtraParamBundleScrollStart.size () == 2;
426 sendGesture(GESTURE_SCROLL_START, e.getE ventTime(), 405 sendGesture(GestureEventType.SCROLL_STAR T, e.getEventTime(),
427 (int) e.getX(), (int) e.getY(), 406 (int) e.getX(), (int) e.getY(),
428 mExtraParamBundleScrollStart); 407 mExtraParamBundleScrollStart);
429 pinchBegin(e.getEventTime(), 408 pinchBegin(e.getEventTime(),
430 Math.round(mDoubleTapDragZoomAnc horX), 409 Math.round(mDoubleTapDragZoomAnc horX),
431 Math.round(mDoubleTapDragZoomAnc horY)); 410 Math.round(mDoubleTapDragZoomAnc horY));
432 mDoubleTapMode = DOUBLE_TAP_MODE_DRAG_ZO OM; 411 mDoubleTapMode = DOUBLE_TAP_MODE_DRAG_ZO OM;
433 } 412 }
434 } else if (mDoubleTapMode == DOUBLE_TAP_MODE_DRA G_ZOOM) { 413 } else if (mDoubleTapMode == DOUBLE_TAP_MODE_DRA G_ZOOM) {
435 assert mExtraParamBundleDoubleTapDragZoom.is Empty(); 414 assert mExtraParamBundleDoubleTapDragZoom.is Empty();
436 sendGesture(GESTURE_SCROLL_BY, e.getEventTim e(), 415 sendGesture(GestureEventType.SCROLL_BY, e.ge tEventTime(),
437 (int) e.getX(), (int) e.getY(), 416 (int) e.getX(), (int) e.getY(),
438 mExtraParamBundleDoubleTapDragZoom); 417 mExtraParamBundleDoubleTapDragZoom);
439 418
440 float dy = mDoubleTapY - e.getY(); 419 float dy = mDoubleTapY - e.getY();
441 pinchBy(e.getEventTime(), 420 pinchBy(e.getEventTime(),
442 Math.round(mDoubleTapDragZoomAnchorX ), 421 Math.round(mDoubleTapDragZoomAnchorX ),
443 Math.round(mDoubleTapDragZoomAnchorY ), 422 Math.round(mDoubleTapDragZoomAnchorY ),
444 (float) Math.pow(dy > 0 ? 423 (float) Math.pow(dy > 0 ?
445 1.0f - DOUBLE_TAP_DRAG_ZOOM_ SPEED : 424 1.0f - DOUBLE_TAP_DRAG_ZOOM_ SPEED :
446 1.0f + DOUBLE_TAP_DRAG_ZOOM_ SPEED, 425 1.0f + DOUBLE_TAP_DRAG_ZOOM_ SPEED,
447 Math.abs(dy * mPxToDp))); 426 Math.abs(dy * mPxToDp)));
448 } 427 }
449 break; 428 break;
450 case MotionEvent.ACTION_UP: 429 case MotionEvent.ACTION_UP:
451 if (mDoubleTapMode != DOUBLE_TAP_MODE_DRAG_ZOOM) { 430 if (mDoubleTapMode != DOUBLE_TAP_MODE_DRAG_ZOOM) {
452 // Normal double tap gesture. 431 // Normal double tap gesture.
453 sendTapEndingEventAsGesture(GESTURE_DOUBLE_T AP, e, null); 432 sendTapEndingEventAsGesture(
433 GestureEventType.DOUBLE_TAP, e, null );
454 } 434 }
455 endDoubleTapDragIfNecessary(e); 435 endDoubleTapDragIfNecessary(e);
456 break; 436 break;
457 case MotionEvent.ACTION_CANCEL: 437 case MotionEvent.ACTION_CANCEL:
458 sendTapCancelIfNecessary(e); 438 sendTapCancelIfNecessary(e);
459 endDoubleTapDragIfNecessary(e); 439 endDoubleTapDragIfNecessary(e);
460 break; 440 break;
461 default: 441 default:
462 break; 442 break;
463 } 443 }
464 mDoubleTapY = e.getY(); 444 mDoubleTapY = e.getY();
465 return true; 445 return true;
466 } 446 }
467 447
468 @Override 448 @Override
469 public boolean onLongPress(MotionEvent e) { 449 public boolean onLongPress(MotionEvent e) {
470 assert !isDoubleTapActive(); 450 assert !isDoubleTapActive();
471 if (isScaleGestureDetectionInProgress()) return false; 451 if (isScaleGestureDetectionInProgress()) return false;
472 setIgnoreSingleTap(true); 452 setIgnoreSingleTap(true);
473 mLastLongPressEvent = e; 453 mLastLongPressEvent = e;
474 sendMotionEventAsGesture(GESTURE_LONG_PRESS, e, null); 454 sendMotionEventAsGesture(GestureEventType.LONG_PRESS, e, null);
475 // Returning true puts the GestureDetector in "longpress " mode, disabling 455 // Returning true puts the GestureDetector in "longpress " mode, disabling
476 // further scrolling. This is undesirable, as it is qui te common for a 456 // further scrolling. This is undesirable, as it is qui te common for a
477 // longpress gesture to fire on content that won't trigg er a context menu. 457 // longpress gesture to fire on content that won't trigg er a context menu.
478 return false; 458 return false;
479 } 459 }
480 460
481 /** 461 /**
482 * This method inspects the distance between where the user started touching 462 * This method inspects the distance between where the user started touching
483 * the surface, and where she released. If the points are to o far apart, we 463 * the surface, and where she released. If the points are to o far apart, we
484 * should assume that the web page has consumed the scroll-e vents in-between, 464 * should assume that the web page has consumed the scroll-e vents in-between,
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 * @param velocityX Initial velocity of the fling (X) measured in pixels per second. 575 * @param velocityX Initial velocity of the fling (X) measured in pixels per second.
596 * @param velocityY Initial velocity of the fling (Y) measured in pixels per second. 576 * @param velocityY Initial velocity of the fling (Y) measured in pixels per second.
597 */ 577 */
598 void fling(long timeMs, int x, int y, int velocityX, int velocityY) { 578 void fling(long timeMs, int x, int y, int velocityX, int velocityY) {
599 if (velocityX == 0 && velocityY == 0) { 579 if (velocityX == 0 && velocityY == 0) {
600 endTouchScrollIfNecessary(timeMs, true); 580 endTouchScrollIfNecessary(timeMs, true);
601 return; 581 return;
602 } 582 }
603 583
604 if (!mTouchScrolling) { 584 if (!mTouchScrolling) {
605 // The native side needs a GESTURE_SCROLL_BEGIN before GESTURE_FLING _START 585 // The native side needs a SCROLL_BEGIN before FLING_START
606 // to send the fling to the correct target. Send if it has not sent. 586 // to send the fling to the correct target. Send if it has not sent.
607 // The distance traveled in one second is a reasonable scroll start hint. 587 // The distance traveled in one second is a reasonable scroll start hint.
608 mExtraParamBundleScrollStart.putInt(DELTA_HINT_X, velocityX); 588 mExtraParamBundleScrollStart.putInt(DELTA_HINT_X, velocityX);
609 mExtraParamBundleScrollStart.putInt(DELTA_HINT_Y, velocityY); 589 mExtraParamBundleScrollStart.putInt(DELTA_HINT_Y, velocityY);
610 assert mExtraParamBundleScrollStart.size() == 2; 590 assert mExtraParamBundleScrollStart.size() == 2;
611 sendGesture(GESTURE_SCROLL_START, timeMs, x, y, mExtraParamBundleScr ollStart); 591 sendGesture(GestureEventType.SCROLL_START, timeMs, x, y, mExtraParam BundleScrollStart);
612 } 592 }
613 endTouchScrollIfNecessary(timeMs, false); 593 endTouchScrollIfNecessary(timeMs, false);
614 594
615 mExtraParamBundleFling.putInt(VELOCITY_X, velocityX); 595 mExtraParamBundleFling.putInt(VELOCITY_X, velocityX);
616 mExtraParamBundleFling.putInt(VELOCITY_Y, velocityY); 596 mExtraParamBundleFling.putInt(VELOCITY_Y, velocityY);
617 assert mExtraParamBundleFling.size() == 2; 597 assert mExtraParamBundleFling.size() == 2;
618 sendGesture(GESTURE_FLING_START, timeMs, x, y, mExtraParamBundleFling); 598 sendGesture(GestureEventType.FLING_START, timeMs, x, y, mExtraParamBundl eFling);
619 } 599 }
620 600
621 /** 601 /**π
622 * End DOUBLE_TAP_MODE_DRAG_ZOOM by sending GESTURE_SCROLL_END and GESTURE_P INCH_END events. 602 * End DOUBLE_TAP_MODE_DRAG_ZOOM by sending SCROLL_END and PINCH_END events.
623 * @param event A hint event that its x, y, and eventTime will be used for t he ending events 603 * @param event A hint event that its x, y, and eventTime will be used for t he ending events
624 * to send. This argument is an optional and can be null. 604 * to send. This argument is an optional and can be null.
625 */ 605 */
626 private void endDoubleTapDragIfNecessary(MotionEvent event) { 606 private void endDoubleTapDragIfNecessary(MotionEvent event) {
627 assert event != null; 607 assert event != null;
628 if (!isDoubleTapActive()) return; 608 if (!isDoubleTapActive()) return;
629 if (mDoubleTapMode == DOUBLE_TAP_MODE_DRAG_ZOOM) { 609 if (mDoubleTapMode == DOUBLE_TAP_MODE_DRAG_ZOOM) {
630 pinchEnd(event.getEventTime()); 610 pinchEnd(event.getEventTime());
631 sendGesture(GESTURE_SCROLL_END, event.getEventTime(), 611 sendGesture(GestureEventType.SCROLL_END, event.getEventTime(),
632 (int) event.getX(), (int) event.getY(), null); 612 (int) event.getX(), (int) event.getY(), null);
633 } 613 }
634 mDoubleTapMode = DOUBLE_TAP_MODE_NONE; 614 mDoubleTapMode = DOUBLE_TAP_MODE_NONE;
635 updateDoubleTapListener(); 615 updateDoubleTapListener();
636 } 616 }
637 617
638 /** 618 /**
639 * Reset touch scroll flag and optionally send a GESTURE_SCROLL_END event if necessary. 619 * Reset touch scroll flag and optionally send a SCROLL_END event if necessa ry.
640 * @param timeMs The time in ms for the event initiating this gesture. 620 * @param timeMs The time in ms for the event initiating this gesture.
641 * @param sendScrollEndEvent Whether to send GESTURE_SCROLL_END event. 621 * @param sendScrollEndEvent Whether to send SCROLL_END event.
642 */ 622 */
643 private void endTouchScrollIfNecessary(long timeMs, boolean sendScrollEndEve nt) { 623 private void endTouchScrollIfNecessary(long timeMs, boolean sendScrollEndEve nt) {
644 if (!mTouchScrolling) return; 624 if (!mTouchScrolling) return;
645 mTouchScrolling = false; 625 mTouchScrolling = false;
646 if (sendScrollEndEvent) { 626 if (sendScrollEndEvent) {
647 sendGesture(GESTURE_SCROLL_END, timeMs, 0, 0, null); 627 sendGesture(GestureEventType.SCROLL_END, timeMs, 0, 0, null);
648 } 628 }
649 } 629 }
650 630
651 /** 631 /**
652 * @return Whether native is tracking a scroll. 632 * @return Whether native is tracking a scroll.
653 */ 633 */
654 boolean isNativeScrolling() { 634 boolean isNativeScrolling() {
655 // TODO(wangxianzhu): Also return true when fling is active once the UI knows exactly when 635 // TODO(wangxianzhu): Also return true when fling is active once the UI knows exactly when
656 // the fling ends. 636 // the fling ends.
657 return mTouchScrolling; 637 return mTouchScrolling;
658 } 638 }
659 639
660 /** 640 /**
661 * @return Whether native is tracking a pinch (i.e. between sending GESTURE_ PINCH_BEGIN and 641 * @return Whether native is tracking a pinch (i.e. between sending PINCH_BE GIN and PINCH_END).
662 * GESTURE_PINCH_END).
663 */ 642 */
664 boolean isNativePinching() { 643 boolean isNativePinching() {
665 return mPinchInProgress; 644 return mPinchInProgress;
666 } 645 }
667 646
668 /** 647 /**
669 * Starts a pinch gesture. 648 * Starts a pinch gesture.
670 * @param timeMs The time in ms for the event initiating this gesture. 649 * @param timeMs The time in ms for the event initiating this gesture.
671 * @param x The x coordinate for the event initiating this gesture. 650 * @param x The x coordinate for the event initiating this gesture.
672 * @param y The x coordinate for the event initiating this gesture. 651 * @param y The x coordinate for the event initiating this gesture.
673 */ 652 */
674 private void pinchBegin(long timeMs, int x, int y) { 653 private void pinchBegin(long timeMs, int x, int y) {
675 sendGesture(GESTURE_PINCH_BEGIN, timeMs, x, y, null); 654 sendGesture(GestureEventType.PINCH_BEGIN, timeMs, x, y, null);
676 } 655 }
677 656
678 /** 657 /**
679 * Pinch by a given percentage. 658 * Pinch by a given percentage.
680 * @param timeMs The time in ms for the event initiating this gesture. 659 * @param timeMs The time in ms for the event initiating this gesture.
681 * @param anchorX The x coordinate for the anchor point to be used in pinch. 660 * @param anchorX The x coordinate for the anchor point to be used in pinch.
682 * @param anchorY The y coordinate for the anchor point to be used in pinch. 661 * @param anchorY The y coordinate for the anchor point to be used in pinch.
683 * @param delta The percentage to pinch by. 662 * @param delta The percentage to pinch by.
684 */ 663 */
685 private void pinchBy(long timeMs, int anchorX, int anchorY, float delta) { 664 private void pinchBy(long timeMs, int anchorX, int anchorY, float delta) {
686 mExtraParamBundlePinchBy.putFloat(DELTA, delta); 665 mExtraParamBundlePinchBy.putFloat(DELTA, delta);
687 assert mExtraParamBundlePinchBy.size() == 1; 666 assert mExtraParamBundlePinchBy.size() == 1;
688 sendGesture(GESTURE_PINCH_BY, timeMs, anchorX, anchorY, mExtraParamBundl ePinchBy); 667 sendGesture(GestureEventType.PINCH_BY, timeMs, anchorX, anchorY, mExtraP aramBundlePinchBy);
689 mPinchInProgress = true; 668 mPinchInProgress = true;
690 } 669 }
691 670
692 /** 671 /**
693 * End a pinch gesture. 672 * End a pinch gesture.
694 * @param timeMs The time in ms for the event initiating this gesture. 673 * @param timeMs The time in ms for the event initiating this gesture.
695 */ 674 */
696 private void pinchEnd(long timeMs) { 675 private void pinchEnd(long timeMs) {
697 sendGesture(GESTURE_PINCH_END, timeMs, 0, 0, null); 676 sendGesture(GestureEventType.PINCH_END, timeMs, 0, 0, null);
698 mPinchInProgress = false; 677 mPinchInProgress = false;
699 } 678 }
700 679
701 /** 680 /**
702 * Ignore singleTap gestures. 681 * Ignore singleTap gestures.
703 */ 682 */
704 void setIgnoreSingleTap(boolean value) { 683 void setIgnoreSingleTap(boolean value) {
705 mIgnoreSingleTap = value; 684 mIgnoreSingleTap = value;
706 } 685 }
707 686
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 (int) event.getX(), (int) event.getY(), extraParams); 851 (int) event.getX(), (int) event.getY(), extraParams);
873 } 852 }
874 853
875 private boolean sendGesture( 854 private boolean sendGesture(
876 int type, long timeMs, int x, int y, Bundle extraParams) { 855 int type, long timeMs, int x, int y, Bundle extraParams) {
877 assert timeMs != 0; 856 assert timeMs != 0;
878 // The only valid gestures that can occur after the touch sequence has 857 // The only valid gestures that can occur after the touch sequence has
879 // ended are SHOW_PRESS and SINGLE_TAP_CONFIRMED, potentially triggered 858 // ended are SHOW_PRESS and SINGLE_TAP_CONFIRMED, potentially triggered
880 // after the double-tap delay window times out. 859 // after the double-tap delay window times out.
881 if (mCurrentDownEvent == null 860 if (mCurrentDownEvent == null
882 && type != GESTURE_SINGLE_TAP_CONFIRMED 861 && type != GestureEventType.SINGLE_TAP_CONFIRMED
883 && type != GESTURE_SHOW_PRESS) { 862 && type != GestureEventType.SHOW_PRESS) {
884 return false; 863 return false;
885 } 864 }
886 return mMotionEventDelegate.onGestureEventCreated(type, timeMs, x, y, ex traParams); 865 return mMotionEventDelegate.onGestureEventCreated(type, timeMs, x, y, ex traParams);
887 } 866 }
888 867
889 private boolean sendTapEndingEventAsGesture(int type, MotionEvent e, Bundle extraParams) { 868 private boolean sendTapEndingEventAsGesture(int type, MotionEvent e, Bundle extraParams) {
890 if (!sendMotionEventAsGesture(type, e, extraParams)) return false; 869 if (!sendMotionEventAsGesture(type, e, extraParams)) return false;
891 mNeedsTapEndingEvent = false; 870 mNeedsTapEndingEvent = false;
892 return true; 871 return true;
893 } 872 }
894 873
895 private void sendTapCancelIfNecessary(MotionEvent e) { 874 private void sendTapCancelIfNecessary(MotionEvent e) {
896 if (!mNeedsTapEndingEvent) return; 875 if (!mNeedsTapEndingEvent) return;
897 if (!sendTapEndingEventAsGesture(GESTURE_TAP_CANCEL, e, null)) return; 876 if (!sendTapEndingEventAsGesture(GestureEventType.TAP_CANCEL, e, null)) return;
898 mLastLongPressEvent = null; 877 mLastLongPressEvent = null;
899 } 878 }
900 879
901 /** 880 /**
902 * @return Whether the ContentViewGestureHandler can handle a MotionEvent ri ght now. True only 881 * @return Whether the ContentViewGestureHandler can handle a MotionEvent ri ght now. True only
903 * if it's the start of a new stream (ACTION_DOWN), or a continuation of the current stream. 882 * if it's the start of a new stream (ACTION_DOWN), or a continuation of the current stream.
904 */ 883 */
905 private boolean canHandle(MotionEvent ev) { 884 private boolean canHandle(MotionEvent ev) {
906 return ev.getAction() == MotionEvent.ACTION_DOWN || 885 return ev.getAction() == MotionEvent.ACTION_DOWN ||
907 (mCurrentDownEvent != null && mCurrentDownEvent.getDownTime() == ev.getDownTime()); 886 (mCurrentDownEvent != null && mCurrentDownEvent.getDownTime() == ev.getDownTime());
908 } 887 }
909 888
910 /** 889 /**
911 * @return Whether the event can trigger a LONG_TAP gesture. True when it ca n and the event 890 * @return Whether the event can trigger a LONG_TAP gesture. True when it ca n and the event
912 * will be consumed. 891 * will be consumed.
913 */ 892 */
914 private boolean triggerLongTapIfNeeded(MotionEvent ev) { 893 private boolean triggerLongTapIfNeeded(MotionEvent ev) {
915 if (mLastLongPressEvent != null 894 if (mLastLongPressEvent != null
916 && ev.getAction() == MotionEvent.ACTION_UP 895 && ev.getAction() == MotionEvent.ACTION_UP
917 && !isScaleGestureDetectionInProgress()) { 896 && !isScaleGestureDetectionInProgress()) {
918 sendTapCancelIfNecessary(ev); 897 sendTapCancelIfNecessary(ev);
919 sendMotionEventAsGesture(GESTURE_LONG_TAP, ev, null); 898 sendMotionEventAsGesture(GestureEventType.LONG_TAP, ev, null);
920 return true; 899 return true;
921 } 900 }
922 return false; 901 return false;
923 } 902 }
924 903
925 /** 904 /**
926 * This is for testing only. 905 * This is for testing only.
927 * Sends a show pressed state gesture through mListener. This should always be called after 906 * Sends a show pressed state gesture through mListener. This should always be called after
928 * a down event; 907 * a down event;
929 */ 908 */
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 mGestureDetector.setOnDoubleTapListener(null); 983 mGestureDetector.setOnDoubleTapListener(null);
1005 } else { 984 } else {
1006 mGestureDetector.setOnDoubleTapListener(mDoubleTapListener); 985 mGestureDetector.setOnDoubleTapListener(mDoubleTapListener);
1007 } 986 }
1008 } 987 }
1009 988
1010 private boolean isDistanceGreaterThanTouchSlop(float distanceX, float distan ceY) { 989 private boolean isDistanceGreaterThanTouchSlop(float distanceX, float distan ceY) {
1011 return distanceX * distanceX + distanceY * distanceY > mScaledTouchSlopS quare; 990 return distanceX * distanceX + distanceY * distanceY > mScaledTouchSlopS quare;
1012 } 991 }
1013 } 992 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698