Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wrapper class around SharedPreferences
/*
* Copyright (c) 2015, Arseny Nasokin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.common.base.Preconditions;
/**
* Wrapper class around SharedPreferences.
*
* @author eirnym
*/
public final class PreferencesManager {
/**
* Available preference types. Checkout {@link SharedPreferences} for all supported types
*/
private enum DefaultsType {
Boolean, Integer, String
}
/**
* Keys, which you can write to preferences with this class
*/
public enum Defaults {
/* TODO: add your preferences here */
;
private final DefaultsType type;
private final boolean secure;
/**
* Creates enum value
* @param type value type
* @param secure {@code true} if you to force commit(), otherwise apply() method will be called.
*/
Defaults(DefaultsType type, boolean secure) {
Preconditions.checkNotNull(type);
this.type = type;
this.secure = secure;
}
/**
* Remove this key from preferences
*/
public void remove() {
PreferencesManager.removeKey(this.toString(), secure);
}
/**
* Puts int value
* @param value value to put
*/
public void put(int value) {
Preconditions.checkState(type == DefaultsType.Integer);
PreferencesManager.putInt(this.toString(), value, secure);
}
/**
* Puts boolean value
* @param value value to put
*/
public void put(boolean value) {
Preconditions.checkState(type == DefaultsType.Boolean);
PreferencesManager.putBoolean(this.toString(), value, secure);
}
/**
* Puts String value
* @param value value to put
*/
public void put(String value) {
Preconditions.checkState(type == DefaultsType.String);
if (value == null) {
PreferencesManager.removeKey(this.toString(), secure);
}
PreferencesManager.putString(this.toString(), value, secure);
}
/**
* Retrieves int value
*/
public int getInt() {
Preconditions.checkState(type == DefaultsType.Integer);
return PreferencesManager.getInt(this.toString());
}
/**
* Retrieves boolean value
*/
public boolean getBoolean() {
Preconditions.checkState(type == DefaultsType.Boolean);
return PreferencesManager.getBoolean(this.toString());
}
/**
* Retrieves string value
*/
public String getString() {
Preconditions.checkState(type == DefaultsType.String);
return PreferencesManager.getString(this.toString());
}
}
private static SharedPreferences preferences;
/**
* Default boolean value to use
*/
private static final boolean DEFAULT_BOOLEAN = false;
/**
* Default boolean value to use
*/
private static final int DEFAULT_INT = 0;
/**
* Default boolean value to use
*/
private static final String DEFAULT_STRING = null;
/**
* This is utility class managed with enum.
*/
private PreferencesManager() {
throw new UnsupportedOperationException("Please, don't instantiate this utility class");
}
/**
* Initializes preferences with default shared preferences
* @param context context to use
*/
public static void setup(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
}
private static boolean getBoolean(String key) {
return preferences.getBoolean(key, DEFAULT_BOOLEAN);
}
private static int getInt(String key) {
return preferences.getInt(key, DEFAULT_INT);
}
private static String getString(String key) {
return preferences.getString(key, DEFAULT_STRING);
}
private static void putBoolean(String key, boolean value, boolean forceCommit) {
forceCommit(preferences.edit().putBoolean(key, value), forceCommit);
}
private static void putInt(String key, int value, boolean forceCommit) {
forceCommit(preferences.edit().putInt(key, value), forceCommit);
}
private static void putString(String key, String value, boolean forceCommit) {
forceCommit(preferences.edit().putString(key, value), forceCommit);
}
private static void removeKey(String key, boolean forceCommit) {
forceCommit(preferences.edit().remove(key), forceCommit);
}
private static void forceCommit(SharedPreferences.Editor editor, boolean forceCommit) {
if (forceCommit) {
editor.commit();
} else {
editor.apply();
}
}
/**
* Removes all keys and commit.
*/
public static void clear() {
preferences.edit().clear().commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment