Handle null keys better (#1628)

This commit is contained in:
Mitchell Syer
2025-09-09 18:13:40 -04:00
committed by GitHub
parent 679e2c0da9
commit aaaae4e719

View File

@@ -10,6 +10,9 @@ package androidx.preference;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
/** /**
@@ -22,7 +25,7 @@ public class Preference {
@JsonIgnore @JsonIgnore
protected Context context; protected Context context;
private boolean isVisible; private boolean isVisible = true;
private boolean isEnabled = true; private boolean isEnabled = true;
private String key; private String key;
private CharSequence title; private CharSequence title;
@@ -130,33 +133,34 @@ public class Preference {
/** Tachidesk specific API */ /** Tachidesk specific API */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Object getCurrentValue() { public Object getCurrentValue() {
switch (getDefaultValueType()) { if (key == null) {
case "String": return Objects.requireNonNullElseGet(defaultValue, () -> switch (getDefaultValueType()) {
return sharedPreferences.getString(key, (String)defaultValue); case "String" -> "";
case "Boolean": case "Boolean" -> false;
return sharedPreferences.getBoolean(key, (Boolean)defaultValue); case "Set<String>" -> new HashSet<>();
case "Set<String>": default -> throw new RuntimeException("Unsupported type");
return sharedPreferences.getStringSet(key, (Set<String>)defaultValue); });
default:
throw new RuntimeException("Unsupported type");
} }
return switch (getDefaultValueType()) {
case "String" -> sharedPreferences.getString(key, (String) defaultValue);
case "Boolean" -> sharedPreferences.getBoolean(key, (Boolean) defaultValue);
case "Set<String>" -> sharedPreferences.getStringSet(key, (Set<String>) defaultValue);
default -> throw new RuntimeException("Unsupported type");
};
} }
/** Tachidesk specific API */ /** Tachidesk specific API */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void saveNewValue(Object value) { public void saveNewValue(Object value) {
if (key == null) {
return;
}
switch (getDefaultValueType()) { switch (getDefaultValueType()) {
case "String": case "String" -> sharedPreferences.edit().putString(key, (String) value).apply();
sharedPreferences.edit().putString(key, (String)value).apply(); case "Boolean" -> sharedPreferences.edit().putBoolean(key, (Boolean) value).apply();
break; case "Set<String>" ->
case "Boolean": sharedPreferences.edit().putStringSet(key, (Set<String>) value).apply();
sharedPreferences.edit().putBoolean(key, (Boolean)value).apply(); default -> throw new RuntimeException("Unsupported type");
break;
case "Set<String>":
sharedPreferences.edit().putStringSet(key, (Set<String>)value).apply();
break;
default:
throw new RuntimeException("Unsupported type");
} }
} }
} }