/**
   * Uses the current state to update the current folder {@link #mFolder} and the current account
   * {@link #mAccount} shown in the actionbar. Also updates the actionbar subtitle to momentarily
   * display the unread count if it has changed.
   *
   * @param folderChanged true if folder changed in terms of URI
   */
  private void setFolderAndAccount(final boolean folderChanged) {
    // Very little can be done if the actionbar or activity is null.
    if (mActionBar == null || mActivity == null) {
      return;
    }
    if (ViewMode.isWaitingForSync(mMode)) {
      // Account is not synced: clear title and update the subtitle.
      setTitle("");
      removeUnreadCount(true);
      return;
    }
    // Check if we should be changing the actionbar at all, and back off if not.
    final boolean isShowingFolder = mIsOnTablet || ViewMode.isListMode(mMode);
    if (!isShowingFolder) {
      // It isn't necessary to set the title in this case, as the title view will
      // be hidden
      return;
    }
    if (mFolder == null) {
      // Clear the action bar title.  We don't want the app name to be shown while
      // waiting for the folder query to finish
      setTitle("");
      return;
    }
    setTitle(mFolder.name);

    final int folderUnreadCount = mFolder.isUnreadCountHidden() ? 0 : mFolder.unreadCount;
    // The user shouldn't see "999+ unread messages", and then a short while later: "999+
    // unread messages". So we set our unread count just past the limit. This way we can
    // change the subtitle the first time around but not for subsequent changes as far as the
    // unread count remains over the limit.
    final int toDisplay =
        (folderUnreadCount > UNREAD_LIMIT) ? (UNREAD_LIMIT + 1) : folderUnreadCount;
    if ((mUnreadCount != toDisplay || folderChanged) && toDisplay != 0) {
      setSubtitle(Utils.getUnreadMessageString(mActivity.getApplicationContext(), toDisplay));
    }
    // Schedule a removal of unread count for the future, if there isn't one already. If the
    // unread count dropped to zero, remove it and show the account name right away.
    removeUnreadCount(toDisplay == 0);
    // Remember the new value for the next run
    mUnreadCount = toDisplay;
  }
  // We need to do this here instead of in the fragment
  public void setConversationModeOptions(Menu menu) {
    if (mCurrentConversation == null) {
      return;
    }
    final boolean showMarkImportant = !mCurrentConversation.isImportant();
    Utils.setMenuItemVisibility(
        menu,
        R.id.mark_important,
        showMarkImportant
            && mAccount.supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT));
    Utils.setMenuItemVisibility(
        menu,
        R.id.mark_not_important,
        !showMarkImportant
            && mAccount.supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT));
    final boolean showDelete =
        mFolder != null && mFolder.supportsCapability(UIProvider.FolderCapabilities.DELETE);
    Utils.setMenuItemVisibility(menu, R.id.delete, showDelete);
    // We only want to show the discard drafts menu item if we are not showing the delete menu
    // item, and the current folder is a draft folder and the account supports discarding
    // drafts for a conversation
    final boolean showDiscardDrafts =
        !showDelete
            && mFolder != null
            && mFolder.isDraft()
            && mAccount.supportsCapability(AccountCapabilities.DISCARD_CONVERSATION_DRAFTS);
    Utils.setMenuItemVisibility(menu, R.id.discard_drafts, showDiscardDrafts);
    final boolean archiveVisible =
        mAccount.supportsCapability(AccountCapabilities.ARCHIVE)
            && mFolder != null
            && mFolder.supportsCapability(FolderCapabilities.ARCHIVE)
            && !mFolder.isTrash();
    Utils.setMenuItemVisibility(menu, R.id.archive, archiveVisible);
    Utils.setMenuItemVisibility(
        menu,
        R.id.remove_folder,
        !archiveVisible
            && mFolder != null
            && mFolder.supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)
            && !mFolder.isProviderFolder()
            && mAccount.supportsCapability(AccountCapabilities.ARCHIVE));
    Utils.setMenuItemVisibility(
        menu,
        R.id.move_to,
        mFolder != null
            && mFolder.supportsCapability(FolderCapabilities.ALLOWS_REMOVE_CONVERSATION));
    Utils.setMenuItemVisibility(
        menu,
        R.id.move_to_inbox,
        mFolder != null && mFolder.supportsCapability(FolderCapabilities.ALLOWS_MOVE_TO_INBOX));

    final MenuItem removeFolder = menu.findItem(R.id.remove_folder);
    if (mFolder != null && removeFolder != null) {
      removeFolder.setTitle(
          mActivity.getApplicationContext().getString(R.string.remove_folder, mFolder.name));
    }
    Utils.setMenuItemVisibility(
        menu,
        R.id.report_spam,
        mAccount.supportsCapability(AccountCapabilities.REPORT_SPAM)
            && mFolder != null
            && mFolder.supportsCapability(FolderCapabilities.REPORT_SPAM)
            && !mCurrentConversation.spam);
    Utils.setMenuItemVisibility(
        menu,
        R.id.mark_not_spam,
        mAccount.supportsCapability(AccountCapabilities.REPORT_SPAM)
            && mFolder != null
            && mFolder.supportsCapability(FolderCapabilities.MARK_NOT_SPAM)
            && mCurrentConversation.spam);
    Utils.setMenuItemVisibility(
        menu,
        R.id.report_phishing,
        mAccount.supportsCapability(AccountCapabilities.REPORT_PHISHING)
            && mFolder != null
            && mFolder.supportsCapability(FolderCapabilities.REPORT_PHISHING)
            && !mCurrentConversation.phishing);
    Utils.setMenuItemVisibility(
        menu,
        R.id.mute,
        mAccount.supportsCapability(AccountCapabilities.MUTE)
            && mFolder != null
            && mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE)
            && !mCurrentConversation.muted);
  }