Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.survey.util;
- import android.content.Context;
- import android.content.SharedPreferences;
- import com.survey.R;
- public class PreferenceUtils {
- private static final String TAG = PreferenceUtils.class.getSimpleName();
- private static final String KEY_DEBUG_PREFS = TAG + "debugSharedPrefs";
- private static final String KEY_VALUES_PREFS = TAG + "nodeValues";
- private static final String KEY_USER_ID = "userId";
- private static final String KEY_PASSWORD = "password";
- private static final String KEY_USERNAME = "username";
- private static final String KEY_SETTINGS = "settings";
- private static final String KEY_DEVICE_ID = "deviceId";
- private static final String KEY_LOGIN_STATUS = "login";
- private static final String KEY_PROJECT_ID = "projectId";
- private static final String KEY_LOCATION_ID = "location_id";
- private static final String KEY_PROJECT_NAME = "mProjectName";
- private static final String KEY_PROJECT_URL = "mProjectUrl";
- private static final String KEY_DOWNLOAD_STATUS = "download_status";
- private static final String KEY_OFFICE_LOCATION_ID = "office_location";
- private static final String KEY_LOCATION_POSITION = "locationPosition";
- private static final String KEY_DISTRIBUTION_NODE_ID = "distributionId";
- public static void saveLoginData(Context context, String userId, String username,
- String password, String deviceId) {
- SharedPreferences prefs = getPreference(context);
- SharedPreferences.Editor editor = prefs.edit();
- editor.putString(KEY_USER_ID, userId);
- editor.putString(KEY_USERNAME, username);
- editor.putString(KEY_PASSWORD, password);
- editor.putString(KEY_DEVICE_ID, deviceId);
- editor.apply();
- }
- public static String getUserName(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getString(KEY_USERNAME, Constants.EMPTY_STRING);
- }
- public static String getPassword(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getString(KEY_PASSWORD, Constants.EMPTY_STRING);
- }
- public static String getUserId(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getString(KEY_USER_ID, Constants.EMPTY_STRING);
- }
- public static String getDeviceId(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getString(KEY_DEVICE_ID, Constants.EMPTY_STRING);
- }
- public static void saveLoginStatus(Context context, boolean isLogin) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putBoolean(KEY_LOGIN_STATUS, isLogin);
- editor.apply();
- }
- public static Boolean isLoggedIn(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getBoolean(KEY_LOGIN_STATUS, false);
- }
- private static SharedPreferences getPreference(Context context) {
- return context.getSharedPreferences(KEY_DEBUG_PREFS, Context.MODE_PRIVATE);
- }
- public static void saveGeneralSettings(Context context, String value) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putString(KEY_SETTINGS, value);
- editor.apply();
- }
- public static String getGeneralSettings(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getString(KEY_SETTINGS, Constants.EMPTY_STRING);
- }
- private static SharedPreferences.Editor getEditor(Context context) {
- SharedPreferences sharedPreferences = getPreference(context);
- return sharedPreferences.edit();
- }
- public static void saveProjectId(Context context, int value) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putInt(KEY_PROJECT_ID, value);
- editor.apply();
- }
- public static int getProjectId(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getInt(KEY_PROJECT_ID, 0);
- }
- public static void saveLocationId(Context context, int value) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putInt(KEY_LOCATION_ID, value);
- editor.apply();
- }
- public static int getLocationId(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getInt(KEY_LOCATION_ID, 0);
- }
- public static void saveOfficeLocationId(Context context, int value) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putInt(KEY_OFFICE_LOCATION_ID, value);
- editor.apply();
- }
- public static int getOfficeLocationId(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getInt(KEY_OFFICE_LOCATION_ID, 0);
- }
- public static void saveDownloadStatus(Context context, boolean status) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putBoolean(KEY_DOWNLOAD_STATUS, status);
- editor.apply();
- }
- public static Boolean isDataDownloaded(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getBoolean(KEY_DOWNLOAD_STATUS, false);
- }
- public static void saveProjectName(Context context, String value) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putString(KEY_PROJECT_NAME, value);
- editor.apply();
- }
- public static void saveProjectUrl(Context context, String url) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putString(KEY_PROJECT_URL, url);
- editor.apply();
- }
- public static String getProjectUrl(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getString(KEY_PROJECT_URL, context.getString(R.string.base_url));
- }
- public static String getProjectName(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getString(KEY_PROJECT_NAME, null);
- }
- public static void savePropertyValue(Context context, String value, String key) {
- SharedPreferences.Editor editor = getNodeValuesEditor(context);
- editor.putString(key, value);
- editor.apply();
- }
- public static String getSelectedProperty(Context context, String key) {
- SharedPreferences prefs = getNodeValuesPreference(context);
- return prefs.getString(key, null);
- }
- public static void clearPreferenceValues(Context context) {
- SharedPreferences.Editor editor = getNodeValuesEditor(context);
- editor.clear().commit();
- }
- private static SharedPreferences getNodeValuesPreference(Context context) {
- return context.getSharedPreferences(KEY_VALUES_PREFS, Context.MODE_PRIVATE);
- }
- private static SharedPreferences.Editor getNodeValuesEditor(Context context) {
- SharedPreferences sharedPreferences = getNodeValuesPreference(context);
- return sharedPreferences.edit();
- }
- public static void saveLocationPosition(Context context, int value) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putInt(KEY_LOCATION_POSITION, value);
- editor.apply();
- }
- public static int getLocationPosition(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getInt(KEY_LOCATION_POSITION, 0);
- }
- public static int getDistributionNodeId(Context context) {
- SharedPreferences prefs = getPreference(context);
- return prefs.getInt(KEY_DISTRIBUTION_NODE_ID, 0);
- }
- public static void saveDistributionNodeId(Context context, int id) {
- SharedPreferences.Editor editor = getEditor(context);
- editor.putInt(KEY_DISTRIBUTION_NODE_ID, id);
- editor.apply();
- }
- }
Add Comment
Please, Sign In to add comment