Android: Hook up most of the settings interface mutators

This commit is contained in:
Connor McLaughlin
2020-12-29 00:45:40 +10:00
parent 0945744f9b
commit 59b9e4b2ef
6 changed files with 229 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package com.github.stenzek.duckstation;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

View File

@ -0,0 +1,75 @@
package com.github.stenzek.duckstation;
import android.content.SharedPreferences;
import android.util.ArraySet;
import java.util.Set;
public class PreferenceHelpers {
/**
* Clears all preferences in the specified section (starting with sectionName/).
* We really don't want to have to do this with JNI...
* @param prefs Preferences object.
* @param sectionName Section to clear keys for.
*/
public static void clearSection(SharedPreferences prefs, String sectionName) {
String testSectionName = sectionName + "/";
SharedPreferences.Editor editor = prefs.edit();
for (String keyName : prefs.getAll().keySet()) {
if (keyName.startsWith(testSectionName)) {
editor.remove(keyName);
}
}
editor.commit();
}
public static Set<String> getStringSet(SharedPreferences prefs, String keyName) {
Set<String> values = null;
try {
values = prefs.getStringSet(keyName, null);
} catch (Exception e) {
try {
String singleValue = prefs.getString(keyName, null);
if (singleValue != null) {
values = new ArraySet<>();
values.add(singleValue);
}
} catch (Exception e2) {
}
}
return values;
}
public static boolean addToStringList(SharedPreferences prefs, String keyName, String valueToAdd)
{
Set<String> values = getStringSet(prefs, keyName);
if (values == null)
values = new ArraySet<>();
final boolean result = values.add(valueToAdd);
prefs.edit().putStringSet(keyName, values).commit();
return result;
}
public static boolean removeFromStringList(SharedPreferences prefs, String keyName, String valueToRemove)
{
Set<String> values = getStringSet(prefs, keyName);
if (values == null)
return false;
final boolean result = values.remove(valueToRemove);
prefs.edit().putStringSet(keyName, values).commit();
return result;
}
public static void setStringList(SharedPreferences prefs, String keyName, String[] values) {
Set<String> valueSet = new ArraySet<String>();
for (String value : values)
valueSet.add(value);
prefs.edit().putStringSet(keyName, valueSet).commit();
}
}