/services/java/com/android/server/accessibility/GestureUtils.java

https://github.com/aizuzi/platform_frameworks_base · Java · 102 lines · 72 code · 19 blank · 11 comment · 14 complexity · 6cddfc3310bea094f993e5b985d46c06 MD5 · raw file

  1. package com.android.server.accessibility;
  2. import android.util.MathUtils;
  3. import android.view.MotionEvent;
  4. /**
  5. * Some helper functions for gesture detection.
  6. */
  7. final class GestureUtils {
  8. private GestureUtils() {
  9. /* cannot be instantiated */
  10. }
  11. public static boolean isTap(MotionEvent down, MotionEvent up, int tapTimeSlop,
  12. int tapDistanceSlop, int actionIndex) {
  13. return eventsWithinTimeAndDistanceSlop(down, up, tapTimeSlop, tapDistanceSlop, actionIndex);
  14. }
  15. public static boolean isMultiTap(MotionEvent firstUp, MotionEvent secondUp,
  16. int multiTapTimeSlop, int multiTapDistanceSlop, int actionIndex) {
  17. return eventsWithinTimeAndDistanceSlop(firstUp, secondUp, multiTapTimeSlop,
  18. multiTapDistanceSlop, actionIndex);
  19. }
  20. private static boolean eventsWithinTimeAndDistanceSlop(MotionEvent first, MotionEvent second,
  21. int timeout, int distance, int actionIndex) {
  22. if (isTimedOut(first, second, timeout)) {
  23. return false;
  24. }
  25. final double deltaMove = computeDistance(first, second, actionIndex);
  26. if (deltaMove >= distance) {
  27. return false;
  28. }
  29. return true;
  30. }
  31. public static double computeDistance(MotionEvent first, MotionEvent second, int pointerIndex) {
  32. return MathUtils.dist(first.getX(pointerIndex), first.getY(pointerIndex),
  33. second.getX(pointerIndex), second.getY(pointerIndex));
  34. }
  35. public static boolean isTimedOut(MotionEvent firstUp, MotionEvent secondUp, int timeout) {
  36. final long deltaTime = secondUp.getEventTime() - firstUp.getEventTime();
  37. return (deltaTime >= timeout);
  38. }
  39. public static boolean isSamePointerContext(MotionEvent first, MotionEvent second) {
  40. return (first.getPointerIdBits() == second.getPointerIdBits()
  41. && first.getPointerId(first.getActionIndex())
  42. == second.getPointerId(second.getActionIndex()));
  43. }
  44. /**
  45. * Determines whether a two pointer gesture is a dragging one.
  46. *
  47. * @param event The event with the pointer data.
  48. * @return True if the gesture is a dragging one.
  49. */
  50. public static boolean isDraggingGesture(float firstPtrDownX, float firstPtrDownY,
  51. float secondPtrDownX, float secondPtrDownY, float firstPtrX, float firstPtrY,
  52. float secondPtrX, float secondPtrY, float maxDraggingAngleCos) {
  53. // Check if the pointers are moving in the same direction.
  54. final float firstDeltaX = firstPtrX - firstPtrDownX;
  55. final float firstDeltaY = firstPtrY - firstPtrDownY;
  56. if (firstDeltaX == 0 && firstDeltaY == 0) {
  57. return true;
  58. }
  59. final float firstMagnitude =
  60. (float) Math.sqrt(firstDeltaX * firstDeltaX + firstDeltaY * firstDeltaY);
  61. final float firstXNormalized =
  62. (firstMagnitude > 0) ? firstDeltaX / firstMagnitude : firstDeltaX;
  63. final float firstYNormalized =
  64. (firstMagnitude > 0) ? firstDeltaY / firstMagnitude : firstDeltaY;
  65. final float secondDeltaX = secondPtrX - secondPtrDownX;
  66. final float secondDeltaY = secondPtrY - secondPtrDownY;
  67. if (secondDeltaX == 0 && secondDeltaY == 0) {
  68. return true;
  69. }
  70. final float secondMagnitude =
  71. (float) Math.sqrt(secondDeltaX * secondDeltaX + secondDeltaY * secondDeltaY);
  72. final float secondXNormalized =
  73. (secondMagnitude > 0) ? secondDeltaX / secondMagnitude : secondDeltaX;
  74. final float secondYNormalized =
  75. (secondMagnitude > 0) ? secondDeltaY / secondMagnitude : secondDeltaY;
  76. final float angleCos =
  77. firstXNormalized * secondXNormalized + firstYNormalized * secondYNormalized;
  78. if (angleCos < maxDraggingAngleCos) {
  79. return false;
  80. }
  81. return true;
  82. }
  83. }