Instantly share code, notes, and snippets.
Last active
April 22, 2017 01:46
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(1)
1
You must be signed in to fork a gist
-
Save shakil807g/414cf6b46a51512964120ef7c422017b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppPref { | |
private static final String SETTINGS_NAME = "com.example.shakil.finalproject.appsettings"; | |
private static AppPref sSharedPrefs; | |
private SharedPreferences mPref; | |
private SharedPreferences.Editor mEditor; | |
public enum Key { | |
/* Recommended naming convention: | |
* ints, floats, doubles, longs: | |
* SAMPLE_NUM or SAMPLE_COUNT or SAMPLE_INT, SAMPLE_LONG etc. | |
* | |
* boolean: IS_SAMPLE, HAS_SAMPLE, CONTAINS_SAMPLE | |
* | |
* String: SAMPLE_KEY, SAMPLE_STR or just SAMPLE | |
*/ | |
SAMPLE_STR, | |
SAMPLE_INT, | |
IS_DEVICE_IS_LOCKED, | |
USER_LOGIN | |
} | |
private AppPref(Context context) { | |
mPref = context.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE); | |
} | |
public static AppPref getInstance(Context context) { | |
if (sSharedPrefs == null) { | |
sSharedPrefs = new AppPref(context.getApplicationContext()); | |
} | |
return sSharedPrefs; | |
} | |
public void put(Key key, String val) { | |
doEdit(); | |
mEditor.putString(key.name(), val); | |
doCommit(); | |
} | |
public void put(Key key, int val) { | |
doEdit(); | |
mEditor.putInt(key.name(), val); | |
doCommit(); | |
} | |
public void put(Key key, boolean val) { | |
doEdit(); | |
mEditor.putBoolean(key.name(), val); | |
doCommit(); | |
} | |
public void put(Key key, float val) { | |
doEdit(); | |
mEditor.putFloat(key.name(), val); | |
doCommit(); | |
} | |
public void put(Key key, long val) { | |
doEdit(); | |
mEditor.putLong(key.name(), val); | |
doCommit(); | |
} | |
public void put(Key key, double val) { | |
doEdit(); | |
mEditor.putString(key.name(), String.valueOf(val)); | |
doCommit(); | |
} | |
public String getString(Key key, String defaultValue) { | |
return mPref.getString(key.name(), defaultValue); | |
} | |
public String getString(Key key) { | |
return mPref.getString(key.name(), null); | |
} | |
public int getInt(Key key) { | |
return mPref.getInt(key.name(), 0); | |
} | |
public int getInt(Key key, int defaultValue) { | |
return mPref.getInt(key.name(), defaultValue); | |
} | |
public long getLong(Key key) { | |
return mPref.getLong(key.name(), 0); | |
} | |
public long getLong(Key key, long defaultValue) { | |
return mPref.getLong(key.name(), defaultValue); | |
} | |
public float getFloat(Key key) { | |
return mPref.getFloat(key.name(), 0); | |
} | |
public float getFloat(Key key, float defaultValue) { | |
return mPref.getFloat(key.name(), defaultValue); | |
} | |
public boolean getBoolean(Key key, boolean defaultValue) { | |
return mPref.getBoolean(key.name(), defaultValue); | |
} | |
public boolean getBoolean(Key key) { | |
return mPref.getBoolean(key.name(), false); | |
} | |
public double getDouble(Key key) { | |
return getDouble(key, 0); | |
} | |
public double getDouble(Key key, double defaultValue) { | |
try { | |
return Double.valueOf(mPref.getString(key.name(), String.valueOf(defaultValue))); | |
} catch (NumberFormatException nfe) { | |
return defaultValue; | |
} | |
} | |
public void putUser(Key key,UserInfo user) { | |
Gson gson = new Gson(); | |
String json = gson.toJson(user); | |
doEdit(); | |
put(key,json); | |
doCommit(); | |
} | |
public UserInfo getUser(Key key) { | |
Gson gson = new Gson(); | |
String json = mPref.getString(key.name(),null); | |
if(json != null) { | |
UserInfo obj = gson.fromJson(json, UserInfo.class); | |
return obj; | |
} | |
return null; | |
} | |
public void remove(Key... keys) { | |
doEdit(); | |
for (Key key : keys) { | |
mEditor.remove(key.name()); | |
} | |
doCommit(); | |
} | |
public void clear() { | |
doEdit(); | |
mEditor.clear(); | |
doCommit(); | |
} | |
private void doEdit() { | |
if (mEditor == null) { | |
mEditor = mPref.edit(); | |
} | |
} | |
private void doCommit() { | |
if (mEditor != null) { | |
mEditor.commit(); | |
mEditor = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment