Advertisement
Guest User

Untitled

a guest
May 26th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. package ru.mobiledimension.mobilereg.settings;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5.  
  6. import static ru.mobiledimension.mobilereg.utils.Logger.l;
  7.  
  8. public class AppSettings {
  9. public enum Key {
  10. token,
  11. name,
  12. version,
  13. rid,
  14. gpsNotifyTime
  15. }
  16.  
  17. private static AppSettings instance = null;
  18. private Context context;
  19. private SharedPreferences preferences;
  20.  
  21. private AppSettings() {}
  22. private AppSettings(Context context) {
  23. this.context = context.getApplicationContext();
  24. this.preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
  25. }
  26.  
  27. public static synchronized AppSettings getInstance(Context context) {
  28. if (instance == null) {
  29. instance = new AppSettings(context);
  30. }
  31. return instance;
  32. }
  33. public AppSettings getInstance() {
  34. return instance;
  35. }
  36.  
  37. public void clear() {
  38. preferences.edit().clear();
  39. preferences.edit().commit();
  40. }
  41.  
  42. public void setToken (String token) {
  43. set(Key.token, token);
  44. }
  45. public String getToken () {
  46. return getString(Key.token);
  47. }
  48.  
  49. public void setName (String name) {
  50. l("setName " + name);
  51. set(Key.name, name);
  52. }
  53. public String getName () {
  54. l("getName " + getString(Key.name));
  55. return getString(Key.name);
  56. }
  57.  
  58. public void setRid (String rid) {
  59. set(Key.rid, rid);
  60. }
  61. public String getRid () {
  62. return getString(Key.rid);
  63. }
  64.  
  65. public void setVersion (int version) {
  66. set(Key.version, version);
  67. }
  68. public int getVersion () {
  69. return getInt(Key.version, Integer.MIN_VALUE);
  70. }
  71.  
  72. public void setGpsNotifyTime (long when) { set(Key.gpsNotifyTime, when); }
  73. public long getGpsNotifyTime () { return getLong(Key.gpsNotifyTime, 0L); }
  74.  
  75. /*
  76. internal methods
  77. */
  78. private void set(Key key, boolean value) {
  79. preferences.edit().putBoolean(key.toString(), value).commit();
  80. }
  81.  
  82. private void set(Key key, int value) {
  83. preferences.edit().putInt(key.toString(), value).commit();
  84. }
  85.  
  86. private void set(Key key, Long value) {
  87. preferences.edit().putLong(key.toString(), value).commit();
  88. }
  89.  
  90. private void set(Key key, String value) {
  91. preferences.edit().putString(key.toString(), value).commit();
  92. }
  93.  
  94. private void sep(String key, boolean value) {
  95. preferences.edit().putBoolean(key, value).commit();
  96. }
  97.  
  98. private int getInt(Key key, Integer defaultValue) {
  99. return preferences.getInt(key.toString(), defaultValue);
  100. }
  101.  
  102. private int getInt(Key key) {
  103. return getInt(key, 0);
  104. }
  105.  
  106. private long getLong(Key key, long defaultValue) {
  107. return preferences.getLong(key.toString(), defaultValue);
  108. }
  109.  
  110. private String getString(Key key) {
  111. return preferences.getString(key.toString(), null);
  112. }
  113.  
  114. private Boolean getBool(Key key) {
  115. return preferences.getBoolean(key.toString(), false);
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement