Search code examples
javagoogle-glassvoicegoogle-gdk

Losing Activity focus after Contextual Voice Commands


I am trying to implement contextual voice commands in my Glass application described in the documentation.

I have a FrameLayout inside my layout, and in my activity I'm implementing OnKeyDown in order to recognize taps. However, when the user says "Ok glass" and then selects one of the available commands, the same activity is returned, but unresponsive (I can't even close the app by sliding down).

I tried to use requestFocus() on the FrameLayout in a couple of different ways, but it did not have any effects. How could I have the focus back without having to restart the activity?

Essentially, this is the code I have so far for the main activity:

public class MainActivity extends Activity {

    public static String TAG = "DummyProject::MainActivity";

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

        getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);

        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
            getMenuInflater().inflate(R.layout.main, menu);
            return true;
        }

        return super.onCreatePanelMenu(featureId, menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.layout.main, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
            switch (item.getItemId()) {
            case R.id.awesome:
                Log.i(TAG, "Selected!");
                break;
            default:
                return true;
            }

            return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public boolean onKeyDown(int keycode, KeyEvent event) {
        if(keycode == KeyEvent.KEYCODE_DPAD_CENTER) {
            Log.i(TAG, "tap!");
            return true;
        }

        return super.onKeyDown(keycode, event);
    }
}

For this example, onKeyDown does not get called after I select any of the "ok glass" menu options.

Thank you in advance.


Solution

  • It turns out that the problem was with the Manifest file. I was setting a theme to the application, ie.

    <application android:theme="@android:style/Theme.NoTitleBar"> ...
    

    Once I removed the theme, everything worked perfectly. The GDK demo sample by Google was helpful when I was trying to find this odd error.