Skip to content

Commit

Permalink
Duplicate set before using putStringSet()
Browse files Browse the repository at this point in the history
As per the documentation:

> Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

Fix #1129
  • Loading branch information
Neamar committed Jan 5, 2019
1 parent 56d356b commit 5ca394f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions app/src/main/java/fr/neamar/kiss/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,17 @@ public Set<String> getExcluded() {
}

public void addToExcludedFromHistory(AppPojo app) {
Set<String> excluded = getExcludedFromHistory();
// The set needs to be cloned and then edited,
// modifying in place is not supported by putStringSet()
Set<String> excluded = new HashSet<>(getExcludedFromHistory());
excluded.add(app.id);
PreferenceManager.getDefaultSharedPreferences(context).edit().putStringSet("excluded-apps-from-history", excluded).apply();
}

public void addToExcluded(AppPojo app) {
Set<String> excluded = getExcluded();
// The set needs to be cloned and then edited,
// modifying in place is not supported by putStringSet()
Set<String> excluded = new HashSet<>(getExcluded());
excluded.add(app.getComponentName());
PreferenceManager.getDefaultSharedPreferences(context).edit().putStringSet("excluded-apps", excluded).apply();
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/fr/neamar/kiss/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ private void addExcludedAppSettings(final SharedPreferences prefs) {
@Override
@SuppressWarnings("unchecked")
public boolean onPreferenceChange(Preference preference, Object newValue) {
Set<String> appListToBeExcluded = (HashSet<String>) newValue;
// Duplicate then save to make sure we're not editing in place
// (can't be done with sharedpreferences)
Set<String> appListToBeExcluded = new HashSet<>((HashSet<String>) newValue);

prefs.edit().putStringSet("excluded-apps", appListToBeExcluded).apply();
loadExcludedAppsToPreference(multiPreference);
Expand Down

0 comments on commit 5ca394f

Please sign in to comment.