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

146 lines
4.8 KiB
Java
Raw Normal View History

2019-02-26 20:38:27 +01:00
package info.nightscout.androidaps.utils;
2017-02-17 16:16:20 +01:00
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
2019-12-08 23:14:46 +01:00
import java.util.Map;
2017-02-17 16:16:20 +01:00
import info.nightscout.androidaps.MainApp;
/**
* Created by mike on 17.02.2017.
*/
public class SP {
2019-12-03 15:29:14 +01:00
private static SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
2019-12-08 23:14:46 +01:00
static public Map<String, ?> getAll() {
return sharedPreferences.getAll();
}
2019-12-03 15:29:14 +01:00
static public void clear() {
sharedPreferences.edit().clear().apply();
}
2017-02-17 16:16:20 +01:00
static public boolean contains(String key) {
return sharedPreferences.contains(key);
}
2018-04-20 17:27:31 +02:00
static public boolean contains(int resourceId) {
return sharedPreferences.contains(MainApp.gs(resourceId));
}
2017-02-17 16:16:20 +01:00
static public String getString(int resourceID, String defaultValue) {
2018-05-02 13:32:41 +02:00
return sharedPreferences.getString(MainApp.gs(resourceID), defaultValue);
2017-02-17 16:16:20 +01:00
}
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 {
2018-05-02 13:32:41 +02:00
return sharedPreferences.getBoolean(MainApp.gs(resourceID), defaultValue);
2017-02-17 16:16:20 +01:00
} 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) {
2018-05-02 13:32:41 +02:00
return SafeParse.stringToDouble(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString()));
2017-02-17 16:16:20 +01:00
}
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 {
2018-05-02 13:32:41 +02:00
return sharedPreferences.getInt(MainApp.gs(resourceID), defaultValue);
2017-06-25 11:16:19 +02:00
} catch (Exception e) {
2018-05-02 13:32:41 +02:00
return SafeParse.stringToInt(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString()));
2017-06-25 11:16:19 +02:00
}
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 {
2018-05-02 13:32:41 +02:00
return sharedPreferences.getLong(MainApp.gs(resourceID), defaultValue);
} catch (Exception e) {
2018-05-02 13:32:41 +02:00
return SafeParse.stringToLong(sharedPreferences.getString(MainApp.gs(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) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putBoolean(key, value).apply();
2017-02-17 16:16:20 +01:00
}
2017-02-17 18:14:33 +01:00
static public void putBoolean(int resourceID, boolean value) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putBoolean(MainApp.gs(resourceID), value).apply();
2017-02-17 18:14:33 +01:00
}
2017-02-17 21:24:30 +01:00
2019-02-25 20:17:02 +01:00
static public void putDouble(String key, double value) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putString(key, Double.toString(value)).apply();
2019-02-25 20:17:02 +01:00
}
2017-04-05 21:39:39 +02:00
static public void putLong(String key, long value) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putLong(key, value).apply();
2017-02-17 21:24:30 +01:00
}
2017-06-25 11:16:19 +02:00
static public void putLong(int resourceID, long value) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putLong(MainApp.gs(resourceID), value).apply();
2017-06-25 11:16:19 +02:00
}
static public void putInt(String key, int value) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putInt(key, value).apply();
2017-06-25 11:16:19 +02:00
}
static public void putInt(int resourceID, int value) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply();
2017-06-25 11:16:19 +02:00
}
2019-07-16 21:36:37 +02:00
2019-09-02 23:38:14 +02:00
static public void incInt(int resourceID) {
int value = SP.getInt(resourceID, 0) + 1;
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply();
2019-09-02 23:38:14 +02:00
}
2017-02-17 21:24:30 +01:00
static public void putString(int resourceID, String value) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putString(MainApp.gs(resourceID), value).apply();
2017-02-17 21:24:30 +01:00
}
2017-02-22 20:29:41 +01:00
2017-06-02 10:25:49 +02:00
static public void putString(String key, String value) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().putString(key, value).apply();
2017-06-02 10:25:49 +02:00
}
2017-08-05 18:09:19 +02:00
static public void remove(int resourceID) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().remove(MainApp.gs(resourceID)).apply();
2017-02-22 20:29:41 +01:00
}
2017-08-05 18:09:19 +02:00
static public void remove(String key) {
2019-12-03 15:29:14 +01:00
sharedPreferences.edit().remove(key).apply();
2017-08-05 18:09:19 +02:00
}
2017-02-17 16:16:20 +01:00
}