PageRenderTime 25ms CodeModel.GetById 21ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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