@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.global_actions);

    final PreferenceScreen prefScreen = getPreferenceScreen();
    final ContentResolver contentResolver = getContext().getContentResolver();

    final String[] defaultActions =
        getContext()
            .getResources()
            .getStringArray(com.android.internal.R.array.config_globalActionsList);
    final List<String> defaultActionsList = Arrays.asList(defaultActions);

    final String[] allActions =
        getContext()
            .getResources()
            .getStringArray(com.android.internal.R.array.values_globalActionsList);

    final String enabledActions =
        Settings.System.getString(contentResolver, Settings.System.GLOBAL_ACTIONS_LIST);

    List<String> enabledActionsList = null;
    if (enabledActions != null) {
      enabledActionsList = Arrays.asList(enabledActions.split(","));
    }

    mGlobalActionsMap = new LinkedHashMap<String, Boolean>();
    for (String actionKey : allActions) {
      if (enabledActionsList != null) {
        mGlobalActionsMap.put(actionKey, enabledActionsList.contains(actionKey));
      } else {
        mGlobalActionsMap.put(actionKey, defaultActionsList.contains(actionKey));
      }
    }
    final UserManager um = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
    boolean multiUser = um.isUserSwitcherEnabled();
    Preference userPref = null;
    int count = prefScreen.getPreferenceCount();
    for (int i = 0; i < count; i++) {
      Preference p = prefScreen.getPreference(i);
      if (p instanceof SwitchPreference) {
        SwitchPreference action = (SwitchPreference) p;
        String key = action.getKey();
        if (key.equals("users") && !multiUser) {
          userPref = action;
        }
        action.setChecked(mGlobalActionsMap.get(key));
      }
    }
    if (userPref != null) {
      prefScreen.removePreference(userPref);
    }
  }
  @Override
  public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    if (preference instanceof SwitchPreference) {
      SwitchPreference action = (SwitchPreference) preference;
      mGlobalActionsMap.put(action.getKey(), action.isChecked());

      List<String> enabledActionsList = new ArrayList<String>();
      for (String actionKey : mGlobalActionsMap.keySet()) {
        Boolean checked = mGlobalActionsMap.get(actionKey);
        if (checked) {
          enabledActionsList.add(actionKey);
        }
      }
      setList(enabledActionsList);
      return true;
    }
    return super.onPreferenceTreeClick(preferenceScreen, preference);
  }
コード例 #3
0
 /**
  * Sets the Summary ON/OFF texts for a SwitchPreference.
  *
  * @param preference the preference that needs to be updated.
  */
 private void getSummaryForSwitchPreference(SwitchPreference preference) {
   switch (preference.getKey()) {
     case "pref_key_theme_dark":
       preference.setSummaryOn(R.string.pref_key_theme_dark_summary_enabled);
       preference.setSummaryOff(R.string.pref_key_theme_dark_summary_disabled);
       break;
     case "pref_key_reminder":
       preference.setSummaryOn(
           String.format(
               getString(R.string.pref_key_reminder_summary_enabled),
               Utilities.getReminderHour(),
               Utilities.getReminderMinute()));
       preference.setSummaryOff(R.string.pref_key_reminder_summary_disabled);
       break;
     default:
       preference.setSummaryOn(R.string.enabled);
       preference.setSummaryOff(R.string.disabled);
   }
 }