/widgets/access-shim/src/com/googlecode/eyesfree/widget/FlickGestureListener.java

http://eyes-free.googlecode.com/ · Java · 101 lines · 59 code · 23 blank · 19 comment · 2 complexity · d491cfd4497d727ce0e167fb521aea5f MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.googlecode.eyesfree.widget;
  17. import android.view.GestureDetector.SimpleOnGestureListener;
  18. import android.view.MotionEvent;
  19. /**
  20. * @author alanv@google.com (Alan Viverette)
  21. */
  22. public abstract class FlickGestureListener extends SimpleOnGestureListener {
  23. private final int FLICK_TIMEOUT = 250;
  24. // constants for flick directions
  25. public static final int FLICK_UP = 0;
  26. public static final int FLICK_RIGHT = 1;
  27. public static final int FLICK_LEFT = 2;
  28. public static final int FLICK_DOWN = 3;
  29. @Override
  30. public boolean onSingleTapConfirmed(MotionEvent e) {
  31. float y = e.getY();
  32. float x = e.getX();
  33. float rawX = e.getRawX();
  34. float rawY = e.getRawY();
  35. return onSingleTap(x, y, rawX, rawY);
  36. }
  37. protected abstract boolean onSingleTap(float x, float y, float rawX, float rawY);
  38. @Override
  39. public boolean onDoubleTap(MotionEvent e) {
  40. float y = e.getY();
  41. float x = e.getX();
  42. float rawX = e.getRawX();
  43. float rawY = e.getRawY();
  44. return onDoubleTap(x, y, rawX, rawY);
  45. }
  46. protected abstract boolean onDoubleTap(float x, float y, float rawX, float rawY);
  47. @Override
  48. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  49. long duration = e2.getEventTime() - e1.getDownTime();
  50. if (duration < FLICK_TIMEOUT) {
  51. return false;
  52. }
  53. float y = e2.getY();
  54. float x = e2.getX();
  55. float rawX = e2.getRawX();
  56. float rawY = e2.getRawY();
  57. return onMove(x, y, rawX, rawY);
  58. }
  59. protected abstract boolean onMove(float x, float y, float rawX, float rawY);
  60. @Override
  61. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  62. long duration = e2.getEventTime() - e1.getDownTime();
  63. if (duration > FLICK_TIMEOUT) {
  64. return false;
  65. }
  66. float y = e1.getY();
  67. float x = e1.getX();
  68. float rawX = e1.getRawX();
  69. float rawY = e1.getRawY();
  70. float distanceY = e2.getY() - y;
  71. float distanceX = e2.getX() - x;
  72. boolean a = (distanceY > distanceX);
  73. boolean b = (distanceY > -distanceX);
  74. int direction = (a ? 2 : 0) | (b ? 1 : 0);
  75. return onFlick(x, y, rawX, rawY, direction);
  76. }
  77. protected abstract boolean onFlick(float x, float y, float rawX, float rawY, int direction);
  78. }