Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Glass GDK Tap Gesture

With GDK and sample code provide by Google the TAP gesture is not being recognized as a TAP. Is returned as LONG_PRESS everytime. Below is the code:

import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;

public class MainActivity extends Activity {

Logger log = Logger.getLogger("MainActivity");

private GestureDetector mGestureDetector;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...
    mGestureDetector = createGestureDetector(this);
}

private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetector = new GestureDetector(context);
    //Create a base listener for generic gestures
    gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
        @Override
        public boolean onGesture(Gesture gesture) {
            log.info(gesture.name());
            if (gesture == Gesture.TAP) {
                // do something on tap
                return true;
            } else if (gesture == Gesture.TWO_TAP) {
                // do something on two finger tap
                return true;
            } else if (gesture == Gesture.SWIPE_RIGHT) {
                // do something on right (forward) swipe
                return true;
            } else if (gesture == Gesture.SWIPE_LEFT) {
                // do something on left (backwards) swipe
                return true;
            }
            return false;
        }
    });
    gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
        @Override
        public void onFingerCountChanged(int previousCount, int currentCount) {
          // do something on finger count changes
        }
    });
    gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {
        @Override
        public boolean onScroll(float displacement, float delta, float velocity) {
            // do something on scrolling
        }
    });
    return gestureDetector;
}

/*
 * Send generic motion events to the gesture detector
 */
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if (mGestureDetector != null) {
        return mGestureDetector.onMotionEvent(event);
    }
    return false;
}

}

Am I missing something here or is this a bug?

like image 251
Patrick Avatar asked Oct 21 '22 20:10

Patrick


2 Answers

I had the same problem as you and my answer could be strange but I avoided it using a switch instead of the if else structure. Moreover with this new code you will be able to capture the rest of the gestures.

I hope it will help you as well.

private GestureDetector createGestureDetector(Context context){
    GestureDetector gestureDetector = new GestureDetector(context);
    //Create a base listener for generic gestures
    gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
        @Override
        public boolean onGesture(Gesture gesture) {
            Log.e(TAG,"gesture = " + gesture);
            switch (gesture) {
                case TAP:
                    Log.e(TAG,"TAP called.");
                    handleGestureTap();
                    break;
                case LONG_PRESS:
                    Log.e(TAG,"LONG_PRESS called.");
                    return true;
                case SWIPE_DOWN:
                    Log.e(TAG,"SWIPE_DOWN called.");
                    return true;
                case SWIPE_LEFT:
                    Log.e(TAG,"SWIPE_LEFT called.");
                    return true;
                case SWIPE_RIGHT:
                    Log.e(TAG,"SWIPE_RIGHT called.");
                    return true;
                case SWIPE_UP:
                    Log.e(TAG,"SWIPE_UP called.");
                    return true;
                case THREE_LONG_PRESS:
                    Log.e(TAG,"THREE_LONG_PRESS called.");
                    return true;
                case THREE_TAP:
                    Log.e(TAG,"THREE_TAP called.");
                    return true;
                case TWO_LONG_PRESS:
                    Log.e(TAG,"TWO_LONG_PRESS called.");
                    return true;
                case TWO_SWIPE_DOWN:
                    Log.e(TAG,"TWO_SWIPE_DOWN called.");
                    return true;
                case TWO_SWIPE_LEFT:
                    Log.e(TAG,"TWO_SWIPE_LEFT called.");
                    return true;
                case TWO_SWIPE_RIGHT:
                    Log.e(TAG,"TWO_SWIPE_RIGHT called.");
                    return true;
                case TWO_SWIPE_UP:
                    Log.e(TAG,"TWO_SWIPE_UP called.");
                    return true;
                case TWO_TAP:
                    Log.e(TAG,"TWO_TAP called.");
                    return true;
            }

            return false;
        }
    });
    gestureDetector.setFingerListener(new com.google.android.glass.touchpad.GestureDetector.FingerListener() {
        @Override
        public void onFingerCountChanged(int previousCount, int currentCount) {
            // do something on finger count changes
            Log.e(TAG,"onFingerCountChanged()");

        }
    });
    gestureDetector.setScrollListener(new com.google.android.glass.touchpad.GestureDetector.ScrollListener() {
        @Override
        public boolean onScroll(float displacement, float delta, float velocity) {
            // do something on scrolling
            Log.e(TAG,"onScroll()");
            return false;
        }
    });
    return gestureDetector;
}
like image 23
Titogelo Avatar answered Oct 24 '22 02:10

Titogelo


If you're only looking to capture tap events for a UI (without using GestureDetector and everything), in Glass touchpad taps are registered as center clicking a d-pad, so you can simply intercept the KEYCODE_DPAD_CENTER key presses.

Try this:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
        // The touchpad was tapped
        return true;
    }

    return false;
}
like image 86
Victor KP Avatar answered Oct 24 '22 02:10

Victor KP