PageRenderTime 16ms CodeModel.GetById 4ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.googlecode.eyesfree.widget;
 18
 19import android.view.GestureDetector.SimpleOnGestureListener;
 20import android.view.MotionEvent;
 21
 22/**
 23 * @author alanv@google.com (Alan Viverette)
 24 */
 25public abstract class FlickGestureListener extends SimpleOnGestureListener {
 26    private final int FLICK_TIMEOUT = 250;
 27
 28    // constants for flick directions
 29    public static final int FLICK_UP = 0;
 30    public static final int FLICK_RIGHT = 1;
 31    public static final int FLICK_LEFT = 2;
 32    public static final int FLICK_DOWN = 3;
 33
 34    @Override
 35    public boolean onSingleTapConfirmed(MotionEvent e) {
 36        float y = e.getY();
 37        float x = e.getX();
 38        float rawX = e.getRawX();
 39        float rawY = e.getRawY();
 40
 41        return onSingleTap(x, y, rawX, rawY);
 42    }
 43
 44    protected abstract boolean onSingleTap(float x, float y, float rawX, float rawY);
 45
 46    @Override
 47    public boolean onDoubleTap(MotionEvent e) {
 48        float y = e.getY();
 49        float x = e.getX();
 50        float rawX = e.getRawX();
 51        float rawY = e.getRawY();
 52
 53        return onDoubleTap(x, y, rawX, rawY);
 54    }
 55
 56    protected abstract boolean onDoubleTap(float x, float y, float rawX, float rawY);
 57
 58    @Override
 59    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
 60        long duration = e2.getEventTime() - e1.getDownTime();
 61
 62        if (duration < FLICK_TIMEOUT) {
 63            return false;
 64        }
 65
 66        float y = e2.getY();
 67        float x = e2.getX();
 68        float rawX = e2.getRawX();
 69        float rawY = e2.getRawY();
 70
 71        return onMove(x, y, rawX, rawY);
 72    }
 73
 74    protected abstract boolean onMove(float x, float y, float rawX, float rawY);
 75
 76    @Override
 77    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
 78        long duration = e2.getEventTime() - e1.getDownTime();
 79
 80        if (duration > FLICK_TIMEOUT) {
 81            return false;
 82        }
 83
 84        float y = e1.getY();
 85        float x = e1.getX();
 86        float rawX = e1.getRawX();
 87        float rawY = e1.getRawY();
 88
 89        float distanceY = e2.getY() - y;
 90        float distanceX = e2.getX() - x;
 91
 92        boolean a = (distanceY > distanceX);
 93        boolean b = (distanceY > -distanceX);
 94
 95        int direction = (a ? 2 : 0) | (b ? 1 : 0);
 96
 97        return onFlick(x, y, rawX, rawY, direction);
 98    }
 99
100    protected abstract boolean onFlick(float x, float y, float rawX, float rawY, int direction);
101}