2

Is there any way to populate the custom "ok glass" menu in my glassware programmatically?

I have an application where the user will be in an immersion and interact with the system mainly by voice commands. The immersion consists of a CardScrollView displaying different sets of data. These sets are added and removed dynamically from a bluetooth service talking to a phone and the glass unit can't know in advance what new sets will appear.

What I want the user to be able to do is to list all current sets in the voice menu and from there choose which set to switch to. For example, if I at the moment have the sets A, B, C and D, I want the user to be able to say "ok glass, go to set", see a sub menu with A, B, C and D and then say for example "C" to switch to set C in the view.

Is this at all possible?

The glassware is going to run in a closed environment with no connection to MyGlass at all, so custom voice commands for the menu with the development permission is not a problem.

Kezo
  • 130
  • 2
  • 7

1 Answers1

3

From what I understand you want you application to be already running when the user speaks. If this is correct then you can simply implement a custom menu with contextual voice commands. I believe you can always repopulate the menu just before it is being shown by overriding onPreparePanel.

I haven't tested it but guessing from the guide something like:

  @Override
  public boolean onPreparePanel(int featureId, View view, Menu menu) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
      menu.clear();
      for (MyMenuItem item : mCurrentMenuItems) {
        menu.add(Menu.NONE, item.getId(), Menu.NONE, item.getTitle());
      }
    }
    return super.onPreparePanel(featureId, view, menu);
  }

  @Override
  public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
      switch (item.getItemId()) {
        case MENU_ITEM_A:
          // do something
          break;
        default:
          return true;
      }
      return true;
    }
    return super.onMenuItemSelected(featureId, item);
  }

MyMenuItem would be a simple class which holds a unique id of an item and its title. mCurrentMenuItems is a list of items to be shown at the moment. You can change its content using a background service, for example.

Jakub K
  • 1,713
  • 1
  • 13
  • 21
  • This looks like it is exactly along the lines of what I need to do. Now, without having tried the code yet, what I need to do is actually one step further down, the part of the menu I need to change is just a sub menu of other stuff. So basically something like [this](http://hastebin.com/mipuneqoyi.1c), where "Dynamic item" is the items I should be able to change at runtime. Is it possible to access just a submenu and clear/fill that? – Kezo Jul 01 '14 at 09:27
  • 1
    It should be possible. You can create a menu layout with an empty submenu and fill it up programatically. Take a look at http://stackoverflow.com/questions/6453016/create-submenus-programmatically-for-existing-menu-created-from-xml – Jakub K Jul 01 '14 at 13:11
  • This looks amazing, thanks a lot! I guess it's this or nothing, if it doesn't work I'll have to wait for more built-in voice functionality in the GDK. – Kezo Jul 02 '14 at 06:12