@UiThread
  private void updatePowerMenuItems(boolean canPowerOn, boolean canPowerOff) {
    boolean connected = isConnected();

    // The fragment may no longer be attached to the parent activity.  If so, do nothing.
    if (!isAdded()) {
      return;
    }

    if (menu_item_poweron != null) {
      if (canPowerOn && connected) {
        Player player = getActivePlayer();
        String playerName = player != null ? player.getName() : "";
        menu_item_poweron.setTitle(getString(R.string.menu_item_poweron, playerName));
        menu_item_poweron.setVisible(true);
      } else {
        menu_item_poweron.setVisible(false);
      }
    }

    if (menu_item_poweroff != null) {
      if (canPowerOff && connected) {
        Player player = getActivePlayer();
        String playerName = player != null ? player.getName() : "";
        menu_item_poweroff.setTitle(getString(R.string.menu_item_poweroff, playerName));
        menu_item_poweroff.setVisible(true);
      } else {
        menu_item_poweroff.setVisible(false);
      }
    }
  }
  /**
   * Manages the list of connected players in the action bar.
   *
   * @param players A list of players to show. May be empty (use {@code
   *     Collections.<Player>emptyList()}) but not null.
   * @param activePlayer The currently active player. May be null.
   */
  @UiThread
  private void updatePlayerDropDown(
      @NonNull Collection<Player> players, @Nullable Player activePlayer) {
    if (!isAdded()) {
      return;
    }

    // Only include players that are connected to the server.
    ArrayList<Player> connectedPlayers = new ArrayList<Player>();
    for (Player player : players) {
      if (player.getConnected()) {
        connectedPlayers.add(player);
      }
    }

    ActionBar actionBar = mActivity.getSupportActionBar();

    // If there are multiple players connected then show a spinner allowing the user to
    // choose between them.
    if (connectedPlayers.size() > 1) {
      actionBar.setDisplayShowTitleEnabled(false);
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
      final ArrayAdapter<Player> playerAdapter =
          new ArrayAdapter<Player>(
              actionBar.getThemedContext(),
              android.R.layout.simple_spinner_dropdown_item,
              connectedPlayers) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
              return Util.getActionBarSpinnerItemView(
                  getContext(), convertView, parent, getItem(position).getName());
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
              return Util.getActionBarSpinnerItemView(
                  getContext(), convertView, parent, getItem(position).getName());
            }
          };
      actionBar.setListNavigationCallbacks(
          playerAdapter,
          new ActionBar.OnNavigationListener() {
            @Override
            public boolean onNavigationItemSelected(int position, long id) {
              if (!playerAdapter.getItem(position).equals(mService.getActivePlayer())) {
                Log.i(
                    TAG,
                    "onNavigationItemSelected.setActivePlayer("
                        + playerAdapter.getItem(position)
                        + ")");
                mService.setActivePlayer(playerAdapter.getItem(position));
                updateUiFromPlayerState(mService.getActivePlayerState());
              }
              return true;
            }
          });
      if (activePlayer != null) {
        actionBar.setSelectedNavigationItem(playerAdapter.getPosition(activePlayer));
      }
    } else {
      // 0 or 1 players, disable the spinner, and either show the sole player in the
      // action bar, or the app name if there are no players.
      actionBar.setDisplayShowTitleEnabled(true);
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

      if (connectedPlayers.size() == 1) {
        actionBar.setTitle(connectedPlayers.get(0).getName());
      } else {
        // TODO: Alert the user if there are no connected players.
        actionBar.setTitle(R.string.app_name);
      }
    }
  }