/shell/src/com/google/marvin/shell/ScrollSidebarView.java

http://eyes-free.googlecode.com/ · Java · 65 lines · 43 code · 15 blank · 7 comment · 4 complexity · f4bda6e70e7bad4d0c4837ce2731010c MD5 · raw file

  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. package com.google.marvin.shell;
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import com.googlecode.eyesfree.utils.compat.MotionEventCompatUtils;
  8. /**
  9. * Scrolling gesture detector for scrolling through the list of apps. Works with
  10. * touch exploration.
  11. *
  12. * @author clchen@google.com (Charles Chen)
  13. */
  14. public class ScrollSidebarView extends View {
  15. interface OnScrollDetectedListener {
  16. public void onScrollDetected(int direction);
  17. }
  18. private OnScrollDetectedListener cb;
  19. private float p2DownY;
  20. public ScrollSidebarView(Context context, AttributeSet attrs) {
  21. super(context, attrs);
  22. }
  23. public void setOnScrollDetectedListener(OnScrollDetectedListener listener) {
  24. cb = listener;
  25. }
  26. public boolean onHoverEvent(MotionEvent event) {
  27. return onTouchEvent(event);
  28. }
  29. @Override
  30. public boolean onTouchEvent(MotionEvent event) {
  31. int action = event.getAction();
  32. float x = event.getX();
  33. float y = event.getY();
  34. switch (action) {
  35. case MotionEventCompatUtils.ACTION_HOVER_EXIT:
  36. case MotionEvent.ACTION_UP:
  37. float p2DeltaY = y - p2DownY;
  38. if (p2DeltaY < -100) {
  39. cb.onScrollDetected(-1);
  40. } else if (p2DeltaY > 100) {
  41. cb.onScrollDetected(1);
  42. }
  43. break;
  44. case MotionEventCompatUtils.ACTION_HOVER_ENTER:
  45. case MotionEvent.ACTION_DOWN:
  46. p2DownY = y;
  47. }
  48. return true;
  49. }
  50. }