Пример #1
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setHasOptionsMenu(true);

    mMailPrefs = MailPrefs.get(getActivity());

    // Set the shared prefs name to use prefs auto-persist behavior by default.
    // Any pref more complex than the default (say, involving migration), should set
    // "persistent=false" in the XML and manually handle preference initialization and change.
    getPreferenceManager().setSharedPreferencesName(mMailPrefs.getSharedPreferencesName());

    addPreferencesFromResource(R.xml.general_preferences);

    mAutoAdvance = (ListPreference) findPreference(AUTO_ADVANCE_WIDGET);
  }
  /**
   * Reorders the specified {@link Menu}, taking into account the user's Archive/Delete preference.
   */
  public static void reorderMenu(
      final Context context, final Account account, final Menu menu, final int maxItems) {
    final String removalAction =
        MailPrefs.get(context)
            .getRemovalAction(account.supportsCapability(AccountCapabilities.ARCHIVE));
    final boolean showArchive =
        MailPrefs.RemovalActions.ARCHIVE.equals(removalAction)
            || MailPrefs.RemovalActions.ARCHIVE_AND_DELETE.equals(removalAction);
    final boolean showDelete =
        MailPrefs.RemovalActions.DELETE.equals(removalAction)
            || MailPrefs.RemovalActions.ARCHIVE_AND_DELETE.equals(removalAction);

    // Do a first pass to extract necessary information on what is safe to display
    boolean archiveVisibleEnabled = false;
    boolean deleteVisibleEnabled = false;
    for (int i = 0; i < menu.size(); i++) {
      final MenuItem menuItem = menu.getItem(i);
      final int itemId = menuItem.getItemId();
      final boolean visible = menuItem.isVisible();
      final boolean enabled = menuItem.isEnabled();

      if (itemId == R.id.archive || itemId == R.id.remove_folder) {
        archiveVisibleEnabled |= (visible & enabled);
      } else if (itemId == R.id.delete || itemId == R.id.discard_drafts) {
        deleteVisibleEnabled |= (visible & enabled);
      }
    }

    int actionItems = 0;

    for (int i = 0; i < menu.size(); i++) {
      final MenuItem menuItem = menu.getItem(i);
      final int itemId = menuItem.getItemId();

      // We only want to promote it if it's visible and has an icon
      if (menuItem.isVisible() && menuItem.getIcon() != null) {
        if (itemId == R.id.archive || itemId == R.id.remove_folder) {
          /*
           * If this is disabled, and we want to show both archive and delete, we will
           * hide archive (rather than showing it disabled), and take up one of our
           * spaces. If we only want to show archive, we'll hide it, but not take up
           * a space.
           */
          if (!menuItem.isEnabled() && showArchive) {
            menuItem.setVisible(false);

            if (showDelete) {
              actionItems++;
            }
          } else {
            /*
             * We show this if the following are all true:
             * 1. The user wants to display archive, or delete is not visible
             * 2. We have room for it
             */
            if ((showArchive || !deleteVisibleEnabled) && actionItems < maxItems) {
              menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
              actionItems++;
            }
          }
        } else if (itemId == R.id.delete || itemId == R.id.discard_drafts) {
          /*
           * We show this if the following are all true:
           * 1. The user wants to display delete, or archive is not visible
           * 2. We have room for it
           */
          if ((showDelete || !archiveVisibleEnabled) && actionItems < maxItems) {
            menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
            actionItems++;
          }
        } else if (itemId == R.id.change_folders) {
          final boolean showChangeFolder =
              account.supportsCapability(AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV);
          menuItem.setVisible(showChangeFolder);

          if (showChangeFolder && actionItems < maxItems) {
            menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
            actionItems++;
          }
        } else if (itemId == R.id.search) {
          menuItem.setShowAsAction(
              MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
          actionItems++;
        } else {
          if (actionItems < maxItems) {
            menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
            actionItems++;
          }
        }
      }
    }
  }