Guest User

Untitled

a guest
Sep 13th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. package com.survey.util;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5.  
  6. import com.survey.R;
  7.  
  8. public class PreferenceUtils {
  9. private static final String TAG = PreferenceUtils.class.getSimpleName();
  10. private static final String KEY_DEBUG_PREFS = TAG + "debugSharedPrefs";
  11. private static final String KEY_VALUES_PREFS = TAG + "nodeValues";
  12. private static final String KEY_USER_ID = "userId";
  13. private static final String KEY_PASSWORD = "password";
  14. private static final String KEY_USERNAME = "username";
  15. private static final String KEY_SETTINGS = "settings";
  16. private static final String KEY_DEVICE_ID = "deviceId";
  17. private static final String KEY_LOGIN_STATUS = "login";
  18. private static final String KEY_PROJECT_ID = "projectId";
  19. private static final String KEY_LOCATION_ID = "location_id";
  20. private static final String KEY_PROJECT_NAME = "mProjectName";
  21. private static final String KEY_PROJECT_URL = "mProjectUrl";
  22. private static final String KEY_DOWNLOAD_STATUS = "download_status";
  23. private static final String KEY_OFFICE_LOCATION_ID = "office_location";
  24. private static final String KEY_LOCATION_POSITION = "locationPosition";
  25. private static final String KEY_DISTRIBUTION_NODE_ID = "distributionId";
  26.  
  27. public static void saveLoginData(Context context, String userId, String username,
  28. String password, String deviceId) {
  29. SharedPreferences prefs = getPreference(context);
  30. SharedPreferences.Editor editor = prefs.edit();
  31. editor.putString(KEY_USER_ID, userId);
  32. editor.putString(KEY_USERNAME, username);
  33. editor.putString(KEY_PASSWORD, password);
  34. editor.putString(KEY_DEVICE_ID, deviceId);
  35. editor.apply();
  36. }
  37.  
  38. public static String getUserName(Context context) {
  39. SharedPreferences prefs = getPreference(context);
  40. return prefs.getString(KEY_USERNAME, Constants.EMPTY_STRING);
  41. }
  42.  
  43. public static String getPassword(Context context) {
  44. SharedPreferences prefs = getPreference(context);
  45. return prefs.getString(KEY_PASSWORD, Constants.EMPTY_STRING);
  46. }
  47.  
  48. public static String getUserId(Context context) {
  49. SharedPreferences prefs = getPreference(context);
  50. return prefs.getString(KEY_USER_ID, Constants.EMPTY_STRING);
  51. }
  52.  
  53. public static String getDeviceId(Context context) {
  54. SharedPreferences prefs = getPreference(context);
  55. return prefs.getString(KEY_DEVICE_ID, Constants.EMPTY_STRING);
  56. }
  57.  
  58. public static void saveLoginStatus(Context context, boolean isLogin) {
  59. SharedPreferences.Editor editor = getEditor(context);
  60. editor.putBoolean(KEY_LOGIN_STATUS, isLogin);
  61. editor.apply();
  62. }
  63.  
  64. public static Boolean isLoggedIn(Context context) {
  65. SharedPreferences prefs = getPreference(context);
  66. return prefs.getBoolean(KEY_LOGIN_STATUS, false);
  67. }
  68.  
  69. private static SharedPreferences getPreference(Context context) {
  70. return context.getSharedPreferences(KEY_DEBUG_PREFS, Context.MODE_PRIVATE);
  71. }
  72.  
  73. public static void saveGeneralSettings(Context context, String value) {
  74. SharedPreferences.Editor editor = getEditor(context);
  75. editor.putString(KEY_SETTINGS, value);
  76. editor.apply();
  77. }
  78.  
  79. public static String getGeneralSettings(Context context) {
  80. SharedPreferences prefs = getPreference(context);
  81. return prefs.getString(KEY_SETTINGS, Constants.EMPTY_STRING);
  82. }
  83.  
  84. private static SharedPreferences.Editor getEditor(Context context) {
  85. SharedPreferences sharedPreferences = getPreference(context);
  86. return sharedPreferences.edit();
  87. }
  88.  
  89. public static void saveProjectId(Context context, int value) {
  90. SharedPreferences.Editor editor = getEditor(context);
  91. editor.putInt(KEY_PROJECT_ID, value);
  92. editor.apply();
  93. }
  94.  
  95. public static int getProjectId(Context context) {
  96. SharedPreferences prefs = getPreference(context);
  97. return prefs.getInt(KEY_PROJECT_ID, 0);
  98. }
  99.  
  100. public static void saveLocationId(Context context, int value) {
  101. SharedPreferences.Editor editor = getEditor(context);
  102. editor.putInt(KEY_LOCATION_ID, value);
  103. editor.apply();
  104. }
  105.  
  106. public static int getLocationId(Context context) {
  107. SharedPreferences prefs = getPreference(context);
  108. return prefs.getInt(KEY_LOCATION_ID, 0);
  109. }
  110.  
  111. public static void saveOfficeLocationId(Context context, int value) {
  112. SharedPreferences.Editor editor = getEditor(context);
  113. editor.putInt(KEY_OFFICE_LOCATION_ID, value);
  114. editor.apply();
  115. }
  116.  
  117. public static int getOfficeLocationId(Context context) {
  118. SharedPreferences prefs = getPreference(context);
  119. return prefs.getInt(KEY_OFFICE_LOCATION_ID, 0);
  120. }
  121.  
  122. public static void saveDownloadStatus(Context context, boolean status) {
  123. SharedPreferences.Editor editor = getEditor(context);
  124. editor.putBoolean(KEY_DOWNLOAD_STATUS, status);
  125. editor.apply();
  126. }
  127.  
  128. public static Boolean isDataDownloaded(Context context) {
  129. SharedPreferences prefs = getPreference(context);
  130. return prefs.getBoolean(KEY_DOWNLOAD_STATUS, false);
  131. }
  132.  
  133. public static void saveProjectName(Context context, String value) {
  134. SharedPreferences.Editor editor = getEditor(context);
  135. editor.putString(KEY_PROJECT_NAME, value);
  136. editor.apply();
  137. }
  138.  
  139. public static void saveProjectUrl(Context context, String url) {
  140. SharedPreferences.Editor editor = getEditor(context);
  141. editor.putString(KEY_PROJECT_URL, url);
  142. editor.apply();
  143. }
  144.  
  145. public static String getProjectUrl(Context context) {
  146. SharedPreferences prefs = getPreference(context);
  147. return prefs.getString(KEY_PROJECT_URL, context.getString(R.string.base_url));
  148. }
  149.  
  150. public static String getProjectName(Context context) {
  151. SharedPreferences prefs = getPreference(context);
  152. return prefs.getString(KEY_PROJECT_NAME, null);
  153. }
  154.  
  155. public static void savePropertyValue(Context context, String value, String key) {
  156. SharedPreferences.Editor editor = getNodeValuesEditor(context);
  157. editor.putString(key, value);
  158. editor.apply();
  159. }
  160.  
  161. public static String getSelectedProperty(Context context, String key) {
  162. SharedPreferences prefs = getNodeValuesPreference(context);
  163. return prefs.getString(key, null);
  164. }
  165.  
  166. public static void clearPreferenceValues(Context context) {
  167. SharedPreferences.Editor editor = getNodeValuesEditor(context);
  168. editor.clear().commit();
  169. }
  170.  
  171. private static SharedPreferences getNodeValuesPreference(Context context) {
  172. return context.getSharedPreferences(KEY_VALUES_PREFS, Context.MODE_PRIVATE);
  173. }
  174.  
  175. private static SharedPreferences.Editor getNodeValuesEditor(Context context) {
  176. SharedPreferences sharedPreferences = getNodeValuesPreference(context);
  177. return sharedPreferences.edit();
  178. }
  179.  
  180. public static void saveLocationPosition(Context context, int value) {
  181. SharedPreferences.Editor editor = getEditor(context);
  182. editor.putInt(KEY_LOCATION_POSITION, value);
  183. editor.apply();
  184. }
  185.  
  186. public static int getLocationPosition(Context context) {
  187. SharedPreferences prefs = getPreference(context);
  188. return prefs.getInt(KEY_LOCATION_POSITION, 0);
  189. }
  190.  
  191. public static int getDistributionNodeId(Context context) {
  192. SharedPreferences prefs = getPreference(context);
  193. return prefs.getInt(KEY_DISTRIBUTION_NODE_ID, 0);
  194. }
  195.  
  196. public static void saveDistributionNodeId(Context context, int id) {
  197. SharedPreferences.Editor editor = getEditor(context);
  198. editor.putInt(KEY_DISTRIBUTION_NODE_ID, id);
  199. editor.apply();
  200. }
  201. }
Add Comment
Please, Sign In to add comment