0
votes

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.

2

2 Answers

0
votes

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.

0
votes

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);