Search code examples
google-glassgoogle-gdk

Contextual Voice Commands Google Glass


what I'm trying to do is to make an activity which should be able to display the "ok, glass" command to open contextual voice commands. I already achieved it but only when I tap on the Touchpad of the activity. Is there a possibility that when I say "OK, glass" --> Start App --> then it should show my live card and afterwards the "ok, glass" appears?

Looking forward to your answers Greetings


Solution

  • This is from the LiveCard Dev Guide:

    1. Indicate that your MenuActivity supports contextual voice commands:

      // Initialize your LiveCard as usual.
      mLiveCard.setVoiceActionEnabled(true);
      mLiveCard.publish(LiveCard.PublishMode.REVEAL); // or SILENT
      
    2. Modify your MenuActivity to support invocation through the voice flow:

      /**
       * Activity showing the options menu.
       */
      public class MenuActivity extends Activity {
      
          private boolean mFromLiveCardVoice;
          private boolean mIsFinishing;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              mFromLiveCardVoice =
                      getIntent().getBooleanExtra(LiveCard.EXTRA_FROM_LIVECARD_VOICE, false);
              if (mFromLiveCardVoice) {
                  // When activated by voice from a live card, enable voice commands. The     menu
                  // will automatically "jump" ahead to the items (skipping the guard phrase
                  // that was already said at the live card).
                  getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);
              }
          }
      
          @Override
          public void onAttachedToWindow() {
              super.onAttachedToWindow();
              if (!mFromLiveCardVoice) {
                  openOptionsMenu();
              }
          }
      
          @Override
          public boolean onCreatePanelMenu(int featureId, Menu menu) {
              if (isMyMenu(featureId)) {
                  getMenuInflater().inflate(R.menu.stopwatch, menu);
                  return true;
              }
              return super.onCreatePanelMenu(featureId, menu);
          }
      
          @Override
          public boolean onPreparePanel(int featureId, View view, Menu menu) {
              if (isMyMenu(featureId)) {
                  // Don't reopen menu once we are finishing. This is necessary
                  // since voice menus reopen themselves while in focus.
                  return !mIsFinishing;
              }
              return super.onPreparePanel(featureId, view, menu);
          }
      
          @Override
          public boolean onMenuItemSelected(int featureId, MenuItem item) {
              if (isMyMenu(featureId)) {
                  // Handle item selection.
                  switch (item.getItemId()) {
                      case R.id.stop_this:
                          stopService(new Intent(this, StopwatchService.class));
                          return true;
                  }
              }
              return super.onMenuItemSelected(featureId, item);
          }
      
          @Override
          public void onPanelClosed(int featureId, Menu menu) {
              super.onPanelClosed(featureId, menu);
              if (isMyMenu(featureId)) {
                  // When the menu panel closes, either an item is selected from the menu or the
                  // menu is dismissed by swiping down. Either way, we end the activity.
                  isFinishing = true;
                  finish();
              }
          }
      
          /**
           * Returns {@code true} when the {@code featureId} belongs to the options menu or     voice
           * menu that are controlled by this menu activity.
           */
          private boolean isMyMenu(int featureId) {
              return featureId == Window.FEATURE_OPTIONS_PANEL ||
                     featureId == WindowUtils.FEATURE_VOICE_COMMANDS;
          }
      }