Skip to content

Instantly share code, notes, and snippets.

@0neel
Created April 7, 2017 12:09
Show Gist options
  • Save 0neel/360b094fda110b0d5913812307ce09dc to your computer and use it in GitHub Desktop.
Save 0neel/360b094fda110b0d5913812307ce09dc to your computer and use it in GitHub Desktop.
Some convenient utils to work with shared preferences
public class SettingsUtils {
private SettingsUtils() {}
public static abstract class OnSharedPreferenceChangeListener
implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "SharedPreferences";
@NonNull
private Context context;
public OnSharedPreferenceChangeListener(@NonNull Context context) {
this.context = context;
}
@Override
public final void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
int id = getStringId(context, key);
if (id > 0) {
onSharedPreferenceChanged(sharedPreferences, id);
} else {
onNoIdSharedPreferenceChanged(sharedPreferences, key);
}
}
public abstract void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
@StringRes @IntRange(from = 1) int id);
@SuppressWarnings("UnusedParameters")
public void onNoIdSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.w(TAG, "Preference without a corresponding ID was changed, key: " + key);
}
@NonNull
public Context getContext() {
return context;
}
}
private static final String STRING_RESOURCE_TYPE = "string";
@NonNull
public static SharedPreferences getPreferences(@NonNull Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
@NonNull
public static SharedPreferences getPreferences(@NonNull Context context, @NonNull String name) {
return context.getSharedPreferences(name, Context.MODE_PRIVATE);
}
@NonNull
public static SharedPreferences.Editor getEditor(@NonNull Context context) {
return getPreferences(context).edit();
}
public static SharedPreferences.Editor getEditor(@NonNull Context context,
@NonNull String name) {
return getPreferences(context, name).edit();
}
public static <T extends Context & SharedPreferences.OnSharedPreferenceChangeListener>
void registerListener(@NonNull T listener) {
registerListener(listener, listener);
}
public static void registerListener(@NonNull Context context,
@NonNull SharedPreferences.OnSharedPreferenceChangeListener listener) {
getPreferences(context).registerOnSharedPreferenceChangeListener(listener);
}
public static <T extends Context & SharedPreferences.OnSharedPreferenceChangeListener>
void unregisterListener(@NonNull T listener) {
unregisterListener(listener, listener);
}
public static void unregisterListener(@NonNull Context context,
@NonNull SharedPreferences.OnSharedPreferenceChangeListener listener) {
getPreferences(context).unregisterOnSharedPreferenceChangeListener(listener);
}
@Nullable
public static String getAsString(@NonNull Context context, @StringRes int keyId) {
String prefKey = context.getString(keyId);
Object value = getPreferences(context).getAll().get(prefKey);
return value == null ? null : value.toString();
}
@Nullable
public static String getString(@NonNull Context context, @StringRes int keyId) {
String key = context.getString(keyId);
return getPreferences(context).getString(key, null);
}
@NonNull
public static String getString(@NonNull Context context,
@StringRes int keyId,
@StringRes int defaultValueId) {
String key = context.getString(keyId);
String defaultValue = context.getString(defaultValueId);
return getPreferences(context).getString(key, defaultValue);
}
@NonNull
public static String getString(@NonNull Context context,
@StringRes int keyId,
@NonNull String defaultValue) {
String key = context.getString(keyId);
return getPreferences(context).getString(key, defaultValue);
}
public static long getLong(@NonNull Context context,
@StringRes int keyId,
long defaultValue) {
String key = context.getString(keyId);
return getPreferences(context).getLong(key, defaultValue);
}
public static boolean getBoolean(@NonNull Context context,
@StringRes int keyId,
boolean defaultValue) {
String key = context.getString(keyId);
return getBoolean(context, key, defaultValue);
}
public static boolean getBoolean(@NonNull Context context,
@NonNull String key,
boolean defaultValue) {
return getPreferences(context).getBoolean(key, defaultValue);
}
public static void putString(@NonNull Context context,
@StringRes int keyId,
@Nullable String value) {
String key = context.getString(keyId);
getEditor(context).putString(key, value).apply();
}
public static void putLong(@NonNull Context context,
@StringRes int keyId,
long value) {
String key = context.getString(keyId);
getEditor(context).putLong(key, value).apply();
}
public static void putBoolean(@NonNull Context context,
@StringRes int keyId,
boolean value) {
String key = context.getString(keyId);
getEditor(context).putBoolean(key, value).apply();
}
public static void putBoolean(@NonNull Context context,
@NonNull String key,
boolean value) {
getEditor(context).putBoolean(key, value).apply();
}
@NonNull
public static Preference findPreference(@NonNull PreferenceFragment fragment, @StringRes int id)
throws Resources.NotFoundException {
return fragment.findPreference(fragment.getActivity().getString(id));
}
@Nullable
public static Preference tryFindPreference(@NonNull PreferenceFragment fragment, @StringRes int id) {
try {
return findPreference(fragment, id);
} catch (Resources.NotFoundException e) {
return null;
}
}
@StringRes
public static int getStringId(@NonNull Context context, @NonNull String name) {
String packageName = context.getPackageName();
return context.getResources().getIdentifier(name, STRING_RESOURCE_TYPE, packageName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment