Search code examples
androidgoogle-glassgoogle-gdk

Supporting voice and touch commands in Google Glass


After following the tutorial provided by Google itself still only voice commands are recognized.

https://developers.google.com/glass/develop/gdk/voice?hl=de#voice-and-touch

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View testView = new CardBuilder(this, CardBuilder.Layout.TEXT)
        .setText("test123")
        .getView();     

    getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);
    getWindow().requestFeature(Window.FEATURE_OPTIONS_PANEL);
    setContentView(testView);   
}

@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS ||
            featureId == Window.FEATURE_OPTIONS_PANEL) {
        getMenuInflater().inflate(R.menu.menu_one, menu);
        return true;
    }
    // Pass through to super to setup touch menu.
    return super.onCreatePanelMenu(featureId, menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS || 
            featureId == Window.FEATURE_OPTIONS_PANEL) {
        switch (item.getItemId()) {
            case R.id.menu_one_item:
                //Some stuff
                //...
                break;
            default:
                return true;
        }
        return true;
    }
    return super.onMenuItemSelected(featureId, item);
}   

Is it really enough to just check for FEATURE_OPTIONS_PANEL or is it necessary to add an onClick listener of some sort? Maybe it's just me having trouble understanding the instructions the way intended.

The tutorial is from July 31st, is it possible the API has changed since then and they haven't updated their post?


Solution

  • Yes, you need to somehow detect a tap so that when a user taps, your app shows the menu.

    Example:

    private GestureDetector mGestureDetector;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGestureDetector = createGestureDetector(this);
    }
    
    private GestureDetector createGestureDetector(Context context) {
        GestureDetector gestureDetector = new GestureDetector(context);
    
        gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
            @Override
            public boolean onGesture(Gesture gesture) {
                // if the user taps, play a proper sound and open the menu.
                // you might don't need the || gesture == Gesture.LONG_PRESS
                if (gesture == Gesture.TAP || gesture == Gesture.LONG_PRESS) {
                    AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                    audio.playSoundEffect(Sounds.TAP);
                    openOptionsMenu();
                    return true;
                } 
                return false;
            }
        });
        return gestureDetector;
    }
    
    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (mGestureDetector != null) {
            return mGestureDetector.onMotionEvent(event);
        }
        return false;
    }