Capture Glass D-Pad events in Android

Guides | Tutorial By 4 years ago

In this blog i will quickly go through about how to capture the google glass d-pad events in android. If you didn’t watch the session called “Void Your Warranty” then you might not know that Android apps can run natively on Glass and Glass itself running Android ICS4. But as we don’t have any touch screen to interact with device, we have to develop our apps in a way that it could be controller using Glass d-pad.

How i started – I tried few events first like onClick, onTouch etc to see if these events are compatible with it at all.
Result – Single tap worked with the focused child, other than that no big success.

Second try – I tried using GestureDetector.
Result – Successful.
Event that can be captured – Swipe Down, Swipe Up, Swipe forward, Swipe backward, Single tap and Double tap.
Event that can not be captured – Long press as Google hardcode it for “ok glass”.

Implementation – Following are the steps you need to follow in order to listen to all six events i mentioned above.

Step 1. – Declare a GestureDetector as a global variable in your activity and initialise it in onCreate method.

Declaration

private GestureDetector mGestureDetector;

Initialisation

mGestureDetector = new GestureDetector(this, new GlassDPadController()));

Step 2 – Override method called onGenericMotionEvent in your activity. You need this method to listen to the all touch events coming from d-pad

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

Step 3 – Now create a class that extends SimpleOnGestureListener and implement (override) all the required methods. We can also implement GestureDetector.OnGestureListener but it won’t help you capturing double tap so if you want all possible events in your app GestureDetector.SimpleOnGestureListener is a best choice.

public class GlassDPadController extends GestureDetector.SimpleOnGestureListener {
}

That is all you need to capture the event. Here is full working code –

public class GlassDPadController extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_MIN_DISTANCE = 100;
    private static final int SWIPE_THRESHOLD_VELOCITY = 1000;

    @Override
    public boolean onScroll(MotionEvent start, MotionEvent finish, float distanceX, float distanceY) {
        if(finish.getX() > start.getX()) {
            Log.d("Event", "On Scroll Forward");
        }
        else {
            Log.d("Event", "On Scroll Backward");
        }
        return true;
    }

    @Override
    public boolean onFling(MotionEvent start, MotionEvent finish, float velocityX, float velocityY) {
        try {
            float totalXTraveled = finish.getX() - start.getX();
            float totalYTraveled = finish.getY() - start.getY();
            if (Math.abs(totalXTraveled) > Math.abs(totalYTraveled)) {
                if (Math.abs(totalXTraveled) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (totalXTraveled > 10) {
                        Log.d("Event", "On Fling Forward");
                    } else {
                        Log.d("Event", "On Fling Backward");
                    }
                }
            } else {
                if (Math.abs(totalYTraveled) > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                    if(totalYTraveled > 0) {
                        Log.d("Event", "On Fling Down");
                    } else {
                        Log.d("Event", "On Fling Up");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        super.onLongPress(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.d("Event", "On Double Tap");
        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.d("Event", "On Single Tap");
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return super.onSingleTapUp(e);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return super.onDoubleTapEvent(e);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return super.onDown(e);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void onShowPress(MotionEvent e) {
        super.onShowPress(e);    //To change body of overridden methods use File | Settings | File Templates.
    }
}