1

I've successfully populated the contextual voice menu of my glassware immersion but I want to be able to change the menu after the initial set up in onPreparePanel() or onCreatePanelMenu() as described here: Programmatically populated contextual "ok glass" menu.

Ideally one of the callbacks would be called after receiving the 'ok, glass' voice command but as far as I can tell, none of them are. Outcome, you're stuck with the version of the voice commands menu as it was the first time you cam into the activity.

So far, the only hack that's come close was when I pulled the GlassVoice.apk off the device and set up a manual VoiceListener (as discussed here: Glass voice command nearest match from given list). When the voice command is detected an onVoiceCommand is called, I try to force a refresh of the menu by calling invalidateOptionsMenu() (no effect) or getWindow().invalidatePanelMenu(WindowUtils.FEATURE_VOICE_COMMANDS). This second one looks like it tries to pull up the menu but then instantly hides it again.

How can I dynamically change the contextual voice menu later on in the lifetime of the activity?

Community
  • 1
  • 1
Manning
  • 21
  • 2

1 Answers1

4

I actually just wrote a sample app and this is working fine for me.

Be sure to use onCreatePanelMenu() to create the voice menu and check

if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS || 
    featureId == Window.FEATURE_OPTIONS_PANEL) ...

to inflate the menu for both touch menu and voice menu.

To refresh both, in onMenuItemSelected(), make sure to call both invalidateOptionsMenu() to refresh the touch menu and getWindow().invalidatePanelMenu(WindowUtils.FEATURE_VOICE_COMMANDS) to refresh the voice menu. I just flipped a boolean on the first menu item selection, which I used to determine whether an old or new menu should be inflated.

What version of Glass are you running on?

Lisa Wray
  • 2,242
  • 16
  • 23
  • Ok, calling the two invalidate method works fine but if it's added in onMenuItemSelected it means the menu is rebuilt immediately after something is selected. What I'm really after is rebuilding the menu immediately before it needs to be displayed based on anything that's happened since it was last called up (ie. what happens if the boolean is flipped at any point after selecting the menu item?) – Manning Jul 16 '14 at 16:58
  • We talked about this in person, but just for posterity in case someone else is wondering: Calling invalidate() does not rebuild the menu, it marks it as dirty. It's OK to call invalidate many times, whenever your data changes. The menu will only be rebuilt once, when it's invoked the next time. – Lisa Wray Jul 17 '14 at 17:23