DoubleTapDetector.java
/*
* Copyright © 2012 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/package com.nokia.example.amaze.ui;
/**
* Helper class for detecting double taps.
*/
public class DoubleTapDetector {
// Constants
private static final int DOUBLE_TAP_TIMEOUT = 400; // Milliseconds
private static final int JITTER_THRESHOLD = 20;
// Members
private final Listener _listener;
private long _lastTapTime = 0;
private int[] _lastTapCoordinate = new int[2];
/**
* Constructor.
* @param listener The listener.
*/
public DoubleTapDetector(Listener listener) {
_listener = listener;
_lastTapCoordinate[0] = -1;
_lastTapCoordinate[1] = -1;
}
/**
* Checks if a double tap has occured. If not, then saves the details of
* this tap.
* @param x X coordinate of the tap event.
* @param y Y coordinate of the tap event.
* @return True if the double tap was detected, false otherwise.
*/
public boolean onTapped(int x, int y) {
long currentTime = System.currentTimeMillis();
if (currentTime - _lastTapTime < DOUBLE_TAP_TIMEOUT
&& Math.abs(_lastTapCoordinate[0] - x) < JITTER_THRESHOLD
&& Math.abs(_lastTapCoordinate[1] - y) < JITTER_THRESHOLD)
{
// Double tap!
if (_listener != null) {
// Notify the listener
_listener.onDoubleTapDetected();
}
_lastTapCoordinate[0] = -1;
_lastTapCoordinate[1] = -1;
return true;
}
// Store the current time and tap coordinates
_lastTapTime = currentTime;
_lastTapCoordinate[0] = x;
_lastTapCoordinate[1] = y;
return false;
}
/**
* Interface for the listener.
*/
public interface Listener {
void onDoubleTapDetected();
}
}