- public int get_option_value(String name, int default_value) {
- String s = get_option_value(name, String.valueOf(default_value));
- try {
- return Integer.parseInt(s);
- }
- catch (Exception e) {
- return default_value;
- }
- }
-
- public long get_option_value(String name, long default_value) {
- String s = get_option_value(name, String.valueOf(default_value));
- try {
- return Long.parseLong(s);
- }
- catch (Exception e) {
- Log.d("db", "returning default long value of "+name, e);
- return default_value;
- }
- }
-
- public String get_option_value(String name, String default_value) {
- Log.d("db", "about to fetch option "+name);
- try(SQLiteDatabase db = getReadableDatabase()) {
- try (Cursor cursor = db
- .rawQuery("select value from options where name=?", new String[]{name}))
- {
- if (cursor.moveToFirst()) {
- String result = cursor.getString(0);
-
- if (result == null) result = default_value;
-
- Log.d("db", "option " + name + "=" + result);
- return result;
- }
- else return default_value;
- }
- catch (Exception e) {
- Log.d("db", "returning default value for " + name, e);
- return default_value;
- }
- }
- }
-
- public void set_option_value(String name, String value) {
- Log.d("db", "setting option "+name+"="+value);
- try(SQLiteDatabase db = getWritableDatabase()) {
- db.execSQL("insert or replace into options(name, value) values(?, ?);",
- new String[]{name, value});
- }
- }
-
- public void set_option_value(String name, long value) {
- set_option_value(name, String.valueOf(value));
- }
- public static long get_option_value(Context context, String name, long default_value) {
- try(MobileLedgerDatabase db = new MobileLedgerDatabase(context)) {
- return db.get_option_value(name, default_value);
- }
- }
- public static void set_option_value(Context context, String name, String value) {
- try(MobileLedgerDatabase db = new MobileLedgerDatabase(context)) {
- db.set_option_value(name, value);
- }
- }