Question

I was able to capture most of the events triggered by the touchpad of a google glass using the SimpleOnGestureListener in a native app.

With the following code you can capture these events

MainActivity.java:

private GestureDetector gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    gestureDetector = new GestureDetector(this, new MyGestureListener());
}

@Override
public boolean onGenericMotionEvent(MotionEvent event)
{
    gestureDetector.onTouchEvent(event);
    return true;
}

MyGestureListener:

public class MyGestureListener extends android.view.GestureDetector.SimpleOnGestureListener
{
    @Override
    public boolean onFling(MotionEvent start, MotionEvent finish, float velocityX, float velocityY)
    {     
        // check for velocity direction to identify swipe forward / backward / up and down
        return true;
    } 
}

I found two different sources for gesture processing I tried:

  1. Capture Glass D-Pad events in Android
  2. Capturing Gesture Controls for Use in Native Android Glass Apps

But with none of them I was able to catch the swipeDown event.

The callback onFling() is only called on "swipe forward", "swipe backward" and "swipe up" but never called when I do a "swipe down".

Any hints or have you already managed to catch the swipe down? I am really clueless here.

Was it helpful?

Solution

Here is the (weird) solution.

It seems that the swipeDown gesture is not really a gesture but more a button click.

That means that you should use the callback methods of your activity to capture these events.

private static final int KEY_SWIPE_DOWN = 4;

@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
    if (keyCode == KEY_SWIPE_DOWN)
    {
        // there was a swipe down event
        return true;
    }
    return false;
}

I think you do not need to care about the onKeyDown() callback because this callback is only triggered directly before the onKeyUp() event and not when you are beginning the gesture.

OTHER TIPS

Interesting I think this may be some kind of bug as at least with XE12 installed my Glass seems to catch the down swipe Gesture about one out of every ten times. Actual test where I counted was 5, 15, 3, and 8 Activity onKeyUp to GestureDetector.SimpleOnGestureListener onFling.

So I'm catching both, and performing the same function in each.

According to the GDK documentation, this is how it is done. The downward swipe translates to KEYCODE_BACK.

    public boolean onKeyDown(int keycode, KeyEvent event) {
          if (keycode == KeyEvent.KEYCODE_BACK) {
           // Do something here
              return true;
          }
      ...
        super.onKeyDown(keyCode, event)
        } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top