PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://eyes-free.googlecode.com/
Java | 94 lines | 75 code | 15 blank | 4 comment | 20 complexity | e145baea5845c9c81c80bb4223c015c6 MD5 | raw file
 1// Copyright 2011 Google Inc. All Rights Reserved.
 2
 3package com.googlecode.eyesfree.inputmethod;
 4
 5import android.view.MotionEvent;
 6import android.view.View;
 7
 8/**
 9 * @author alanv@google.com (Alan Viverette)
10 */
11public class SegmentDetector {
12    private static final int NUM_SEGMENTS = 5;
13    private static final float CANCEL_SIZE = 0.25f;
14
15    public static final int SEGMENT_INVALID = -2;
16    public static final int SEGMENT_CANCEL = -1;
17
18    private SegmentListener mListener;
19    private MotionEvent mLeftEvent;
20    private int mSegmentSize;
21    private int mCancelLeft;
22    private int mCancelRight;
23    private int mPreviousSegment;
24
25    public void setListener(SegmentListener listener) {
26        mListener = listener;
27    }
28
29    public void updateDimensions(View v) {
30        int width = v.getWidth();
31        int cancelSize = (int) (v.getResources().getDisplayMetrics().xdpi * CANCEL_SIZE);
32
33        mSegmentSize = (int) (0.5 * width / NUM_SEGMENTS);
34        mCancelLeft = cancelSize;
35        mCancelRight = width - cancelSize;
36    }
37
38    public boolean onTouchEvent(MotionEvent event) {
39        if (event.getY() >= 0 || event.getPointerCount() > 1) {
40            if (mLeftEvent != null) {
41                if (mListener != null) {
42                    mListener.onEnteredKeyboard();
43                }
44                mLeftEvent = null;
45                mPreviousSegment = SEGMENT_INVALID;
46            }
47            return false;
48        }
49
50        if (mLeftEvent == null) {
51            if (mListener != null) {
52                mListener.onLeftKeyboard();
53            }
54            mLeftEvent = MotionEvent.obtainNoHistory(event);
55            mPreviousSegment = SEGMENT_INVALID;
56        }
57
58        int currentSegment;
59        float x = event.getX();
60
61        if (x < mCancelLeft || x >= mCancelRight) {
62            currentSegment = SEGMENT_CANCEL;
63        } else {
64            currentSegment = (int) (Math.abs(x - mLeftEvent.getX()) / mSegmentSize);
65        }
66
67        switch (event.getAction()) {
68            case MotionEvent.ACTION_MOVE:
69                if (currentSegment != mPreviousSegment) {
70                    if (mListener != null) {
71                        mListener.onEnteredSegment(currentSegment);
72                    }
73                    mPreviousSegment = currentSegment;
74                }
75                break;
76            case MotionEvent.ACTION_UP:
77                if (mListener != null) {
78                    mListener.onSelectedSegment(currentSegment);
79                }
80                mLeftEvent = null;
81                mPreviousSegment = SEGMENT_INVALID;
82                break;
83        }
84
85        return true;
86    }
87
88    public interface SegmentListener {
89        public void onLeftKeyboard();
90        public void onEnteredKeyboard();
91        public void onEnteredSegment(int segmentId);
92        public void onSelectedSegment(int segmentId);
93    }
94}