PageRenderTime 47ms CodeModel.GetById 14ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

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