0

I am trying to customise the menu entries for my Glass app by using the answer provided here How do you customise Glass contextual voice menu in an immersion *after* its initial setup? but the menu never gets recreated.

The app is my final university project for home automation. When the user turns on the light I would like the "turn on light" option to hide and the "turn off light" to become visible.

Here is some of my code:

 @Override
    public boolean onPreparePanel(int featureId, View view, Menu menu) {
        mPreparePanelCalled = true;
    if (isMyMenu(featureId)) {
        shouldFinishOnMenuClose = true;

        MenuItem menuLightOn = menu.findItem(R.id.action_turn_on_light);
        MenuItem menuLightOff = menu.findItem(R.id.action_turn_off_light);
        if (menuLight) {
            menuLightOn.setVisible(false);
            menuLightOff.setVisible(true);
        } else {
            menuLightOn.setVisible(true);
            menuLightOff.setVisible(false);
        }
        return !mIsFinishing;
    }
    return super.onPreparePanel(featureId, view, menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (isMyMenu(featureId)) {
        shouldFinishOnMenuClose = true;
        // Handle item selection.
        switch (item.getItemId()) {
            case R.id.action_unlock_door:
                try {
                    handleUnlockDoor();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.action_lock_door:
                try {
                    handleLockDoor();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.action_turn_on_light:
                try {
                    handleTurnOnLight();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.action_turn_off_light:
                try {
                    handleTurnOffLight();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.action_set_thermostat:
                handleSetThermostat();
                break;
            case R.id.action_turn_on_kettle:
                try {
                    handleTurnOnKettle();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.action_turn_off_kettle:
                try {
                    handleTurnOffKettle();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.action_stop:
                handleStop();
                break;
        }
    }

    invalidateOptionsMenu();
    getWindow().invalidatePanelMenu(WindowUtils.FEATURE_VOICE_COMMANDS);
    return super.onMenuItemSelected(featureId, item);
}

And then when the corresponding function is called, the boolean value is changed:

private void handleTurnOnLight() throws IOException {
    String retrievedData = null;
    try {
        retrievedData = new RetrieveData().execute(ServerUrl.serverUrl + "/lighton").get();
        menuLight = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    Toast.makeText(this, retrievedData, Toast.LENGTH_LONG).show();
    LiveCardService.refreshLiveCard(this);

}

private void handleTurnOffLight() throws IOException {
    String retrievedData = null;
    try {
        retrievedData = new RetrieveData().execute(ServerUrl.serverUrl + "/lightoff").get();
        menuLight = false;
    } catch (Exception e) {
        e.printStackTrace();
    }
    Toast.makeText(this, retrievedData, Toast.LENGTH_LONG).show();
    LiveCardService.refreshLiveCard(this);
}

I'm a real newbie to Glass development, so any advice would be greatly appreciated.

Thank you.

Community
  • 1
  • 1

2 Answers2

0

If the changes need to be done on the options menu (and not a submenu), you will need to make the changes in onPrepareOptionsMenu. I think calling it the way you do would work, but only if you call invalidatePanelMenu before invalidateOptionsMenu. However, do take note that it makes more sense to move your code to onPrepareOptionsMenu.

Koh
  • 1,570
  • 1
  • 8
  • 6
  • Thank you so much for the fast reply, I tried both of your suggestions, but unfortunately they did not work for me. If you would like to look at all of my code, it is available at https://github.com/sephallen/HomeAutomationForGlass – Joseph Allen Apr 11 '15 at 17:38
0

for the contextual menu to update you need to invalidate that too. This can be done by using the following

getWindow().invalidatePanelMenu(WindowUtils.FEATURE_VOICE_COMMANDS);
NoSixties
  • 2,443
  • 2
  • 28
  • 65
  • Thanks for the reply, I am calling this already in onMenuItemSelected override, but its not working for me. Perhaps this is the wrong place to call it? – Joseph Allen Apr 13 '15 at 07:36
  • I tried using the `setVisible(true)` on my own project and it doesn't seem to work for me either. What I'm doing in my own `onPreparePanel` is inflating a new menu depending on a certain state. You could try using this. But it would seem to a lot of work compared to what you are trying to do here. – NoSixties Apr 14 '15 at 07:16
  • This is something I have considered, but wouldn't I have to create every possible version of combination of things being on and off as a separate menu? – Joseph Allen Apr 14 '15 at 07:23
  • yes you would. I feel like that shouldn't be the most effective way to do it for you – NoSixties Apr 14 '15 at 07:26