AndroidAPS/app/src/main/java/info/nightscout/utils/SP.java

143 lines
5 KiB
Java
Raw Normal View History

2017-02-17 16:16:20 +01:00
package info.nightscout.utils;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import info.nightscout.androidaps.MainApp;
/**
* Created by mike on 17.02.2017.
*/
public class SP {
static SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
static public boolean contains(String key) {
return sharedPreferences.contains(key);
}
static public String getString(int resourceID, String defaultValue) {
return sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue);
}
static public String getString(String key, String defaultValue) {
return sharedPreferences.getString(key, defaultValue);
}
2018-04-16 23:29:53 +02:00
static public boolean getBoolean(int resourceID, Boolean defaultValue) {
2017-02-17 16:16:20 +01:00
try {
return sharedPreferences.getBoolean(MainApp.sResources.getString(resourceID), defaultValue);
} catch (Exception e) {
return defaultValue;
}
}
2018-04-16 23:29:53 +02:00
static public boolean getBoolean(String key, Boolean defaultValue) {
2017-02-17 16:16:20 +01:00
try {
return sharedPreferences.getBoolean(key, defaultValue);
} catch (Exception e) {
return defaultValue;
}
}
static public Double getDouble(int resourceID, Double defaultValue) {
return SafeParse.stringToDouble(sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue.toString()));
}
static public Double getDouble(String key, Double defaultValue) {
return SafeParse.stringToDouble(sharedPreferences.getString(key, defaultValue.toString()));
}
static public int getInt(int resourceID, Integer defaultValue) {
2017-06-25 11:16:19 +02:00
try {
return sharedPreferences.getInt(MainApp.sResources.getString(resourceID), defaultValue);
} catch (Exception e) {
return SafeParse.stringToInt(sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue.toString()));
}
2017-02-17 16:16:20 +01:00
}
static public int getInt(String key, Integer defaultValue) {
2017-06-25 11:16:19 +02:00
try {
return sharedPreferences.getInt(key, defaultValue);
} catch (Exception e) {
return SafeParse.stringToInt(sharedPreferences.getString(key, defaultValue.toString()));
}
2017-02-17 16:16:20 +01:00
}
static public long getLong(int resourceID, Long defaultValue) {
try {
return sharedPreferences.getLong(MainApp.sResources.getString(resourceID), defaultValue);
} catch (Exception e) {
return SafeParse.stringToLong(sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue.toString()));
}
2017-02-17 16:16:20 +01:00
}
static public long getLong(String key, Long defaultValue) {
2017-04-05 21:39:39 +02:00
try {
return sharedPreferences.getLong(key, defaultValue);
} catch (Exception e) {
return SafeParse.stringToLong(sharedPreferences.getString(key, defaultValue.toString()));
}
2017-02-17 16:16:20 +01:00
}
static public void putBoolean(String key, boolean value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
2017-02-17 18:14:33 +01:00
static public void putBoolean(int resourceID, boolean value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(MainApp.sResources.getString(resourceID), value);
editor.apply();
}
2017-02-17 21:24:30 +01:00
2017-04-05 21:39:39 +02:00
static public void putLong(String key, long value) {
2017-02-17 21:24:30 +01:00
SharedPreferences.Editor editor = sharedPreferences.edit();
2017-04-05 21:39:39 +02:00
editor.putLong(key, value);
2017-02-17 21:24:30 +01:00
editor.apply();
}
2017-06-25 11:16:19 +02:00
static public void putLong(int resourceID, long value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong(MainApp.sResources.getString(resourceID), value);
editor.apply();
}
static public void putInt(String key, int value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
}
static public void putInt(int resourceID, int value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(MainApp.sResources.getString(resourceID), value);
editor.apply();
}
2017-02-17 21:24:30 +01:00
static public void putString(int resourceID, String value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(MainApp.sResources.getString(resourceID), value);
editor.apply();
}
2017-02-22 20:29:41 +01:00
2017-06-02 10:25:49 +02:00
static public void putString(String key, String value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
2017-08-05 18:09:19 +02:00
static public void remove(int resourceID) {
2017-02-22 20:29:41 +01:00
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(MainApp.sResources.getString(resourceID));
editor.apply();
}
2017-08-05 18:09:19 +02:00
static public void remove(String key) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(key);
editor.apply();
}
2017-02-17 16:16:20 +01:00
}