/ime/latinime/src/com/googlecode/eyesfree/inputmethod/SimpleMultitouchGestureListener.java

http://eyes-free.googlecode.com/ · Java · 135 lines · 85 code · 35 blank · 15 comment · 3 complexity · b90e243b173355941d868e51f4f825fb MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  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. package com.googlecode.eyesfree.inputmethod;
  17. import android.graphics.PointF;
  18. import android.view.MotionEvent;
  19. import com.googlecode.eyesfree.inputmethod.MultitouchGestureDetector.MultitouchGestureListener;
  20. public abstract class SimpleMultitouchGestureListener implements MultitouchGestureListener {
  21. public static final int FLICK_LEFT = 0;
  22. public static final int FLICK_DOWN = 1;
  23. public static final int FLICK_UP = 2;
  24. public static final int FLICK_RIGHT = 3;
  25. @Override
  26. public boolean onDown(MotionEvent ev) {
  27. PointF centroid = pointerCentroid(ev);
  28. int pointerCount = getFakePointerCount(ev);
  29. return onSimpleDown(pointerCount, centroid.x, centroid.y);
  30. }
  31. public abstract boolean onSimpleDown(int pointerCount, float centroidX, float centroidY);
  32. @Override
  33. public boolean onTap(MotionEvent ev) {
  34. PointF centroid = pointerCentroid(ev);
  35. int pointerCount = getFakePointerCount(ev);
  36. return onSimpleTap(pointerCount, centroid.x, centroid.y);
  37. }
  38. @Override
  39. public boolean onSlideTap(MotionEvent ev) {
  40. int pointerCount = getFakePointerCount(ev);
  41. return onSimpleTap(pointerCount, ev.getX(), ev.getX());
  42. }
  43. public abstract boolean onSimpleTap(int pointerCount, float centroidX, float centroidY);
  44. @Override
  45. public boolean onDoubleTap(MotionEvent ev) {
  46. PointF centroid = pointerCentroid(ev);
  47. int pointerCount = getFakePointerCount(ev);
  48. return onSimpleDoubleTap(pointerCount, centroid.x, centroid.y);
  49. }
  50. public abstract boolean onSimpleDoubleTap(int pointerCount, float centroidX, float centroidY);
  51. @Override
  52. public boolean onLongPress(MotionEvent ev) {
  53. PointF centroid = pointerCentroid(ev);
  54. int pointerCount = getFakePointerCount(ev);
  55. return onSimpleLongPress(pointerCount, centroid.x, centroid.y);
  56. }
  57. public abstract boolean onSimpleLongPress(int pointerCount, float centroidX, float centroidY);
  58. @Override
  59. public boolean onMove(MotionEvent ev) {
  60. PointF centroid = pointerCentroid(ev);
  61. int pointerCount = getFakePointerCount(ev);
  62. return onSimpleMove(pointerCount, centroid.x, centroid.y);
  63. }
  64. public abstract boolean onSimpleMove(int pointerCount, float centroidX, float centroidY);
  65. @Override
  66. public boolean onFlick(MotionEvent e1, MotionEvent e2) {
  67. PointF down = pointerCentroid(e1);
  68. PointF up = pointerCentroid(e2);
  69. float deltaX = (up.x - down.x);
  70. float deltaY = (up.y - down.y);
  71. boolean a = (deltaY > deltaX);
  72. boolean b = (deltaY > -deltaX);
  73. int pointerCount = getFakePointerCount(e1);
  74. int direction = (a ? 2 : 0) | (b ? 1 : 0);
  75. return onSimpleFlick(pointerCount, direction);
  76. }
  77. public abstract boolean onSimpleFlick(int pointerCount, int direction);
  78. private PointF pointerCentroid(MotionEvent ev) {
  79. float x = 0;
  80. float y = 0;
  81. int pointerCount = ev.getPointerCount();
  82. for (int i = 0; i < pointerCount; i++) {
  83. y += ev.getX(i);
  84. x += ev.getY(i);
  85. }
  86. x /= ev.getPointerCount();
  87. y /= ev.getPointerCount();
  88. return new PointF(x, y);
  89. }
  90. public static void setFakePointerCount(MotionEvent e, int pointerCount) {
  91. e.setEdgeFlags((pointerCount << 4) | e.getEdgeFlags());
  92. }
  93. public static int getFakePointerCount(MotionEvent e) {
  94. final int fakePointerCount = (e.getEdgeFlags() >> 4);
  95. if (fakePointerCount == 0) {
  96. return e.getPointerCount();
  97. }
  98. return fakePointerCount;
  99. }
  100. }