Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.mobiledimension.mobilereg.settings;
- import android.content.Context;
- import android.content.SharedPreferences;
- import static ru.mobiledimension.mobilereg.utils.Logger.l;
- public class AppSettings {
- public enum Key {
- token,
- name,
- version,
- rid,
- gpsNotifyTime
- }
- private static AppSettings instance = null;
- private Context context;
- private SharedPreferences preferences;
- private AppSettings() {}
- private AppSettings(Context context) {
- this.context = context.getApplicationContext();
- this.preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
- }
- public static synchronized AppSettings getInstance(Context context) {
- if (instance == null) {
- instance = new AppSettings(context);
- }
- return instance;
- }
- public AppSettings getInstance() {
- return instance;
- }
- public void clear() {
- preferences.edit().clear();
- preferences.edit().commit();
- }
- public void setToken (String token) {
- set(Key.token, token);
- }
- public String getToken () {
- return getString(Key.token);
- }
- public void setName (String name) {
- l("setName " + name);
- set(Key.name, name);
- }
- public String getName () {
- l("getName " + getString(Key.name));
- return getString(Key.name);
- }
- public void setRid (String rid) {
- set(Key.rid, rid);
- }
- public String getRid () {
- return getString(Key.rid);
- }
- public void setVersion (int version) {
- set(Key.version, version);
- }
- public int getVersion () {
- return getInt(Key.version, Integer.MIN_VALUE);
- }
- public void setGpsNotifyTime (long when) { set(Key.gpsNotifyTime, when); }
- public long getGpsNotifyTime () { return getLong(Key.gpsNotifyTime, 0L); }
- /*
- internal methods
- */
- private void set(Key key, boolean value) {
- preferences.edit().putBoolean(key.toString(), value).commit();
- }
- private void set(Key key, int value) {
- preferences.edit().putInt(key.toString(), value).commit();
- }
- private void set(Key key, Long value) {
- preferences.edit().putLong(key.toString(), value).commit();
- }
- private void set(Key key, String value) {
- preferences.edit().putString(key.toString(), value).commit();
- }
- private void sep(String key, boolean value) {
- preferences.edit().putBoolean(key, value).commit();
- }
- private int getInt(Key key, Integer defaultValue) {
- return preferences.getInt(key.toString(), defaultValue);
- }
- private int getInt(Key key) {
- return getInt(key, 0);
- }
- private long getLong(Key key, long defaultValue) {
- return preferences.getLong(key.toString(), defaultValue);
- }
- private String getString(Key key) {
- return preferences.getString(key.toString(), null);
- }
- private Boolean getBool(Key key) {
- return preferences.getBoolean(key.toString(), false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement