X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FMLDB.java;h=832d57d759da8ea2b6457170ceec7392d9682fc1;hb=9579cf8c416181bc54107a3577a0da8bd62516df;hp=30b77f9c9a4e006b484022f21431a1dd9ac3880c;hpb=f27308fd9e4c8c158e64e6a35967b773f0f7e601;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java index 30b77f9c..832d57d7 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java @@ -26,6 +26,7 @@ import android.database.MatrixCursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.os.AsyncTask; import android.os.Build; import android.provider.FontsContract; import android.util.Log; @@ -34,6 +35,7 @@ import android.widget.AutoCompleteTextView; import android.widget.FilterQueryProvider; import android.widget.SimpleCursorAdapter; +import net.ktnx.mobileledger.async.DbOpQueue; import net.ktnx.mobileledger.async.DescriptionSelectedCallback; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.MobileLedgerProfile; @@ -71,6 +73,7 @@ public final class MLDB { db.execSQL("pragma case_sensitive_like=ON;"); return db; } + @SuppressWarnings("unused") static public int getIntOption(String name, int default_value) { String s = getOption(name, String.valueOf(default_value)); try { @@ -81,6 +84,7 @@ public final class MLDB { return default_value; } } + @SuppressWarnings("unused") static public long getLongOption(String name, long default_value) { String s = getOption(name, String.valueOf(default_value)); try { @@ -91,6 +95,38 @@ public final class MLDB { return default_value; } } + static public void getOption(String name, String defaultValue, GetOptCallback cb) { + AsyncTask t = new AsyncTask() { + @Override + protected String doInBackground(Void... params) { + SQLiteDatabase db = getDatabase(); + try (Cursor cursor = db + .rawQuery("select value from options where profile = ? and name=?", + new String[]{NO_PROFILE, name})) + { + if (cursor.moveToFirst()) { + String result = cursor.getString(0); + + if (result == null) result = defaultValue; + + debug("async-db", "option " + name + "=" + result); + return result; + } + else return defaultValue; + } + catch (Exception e) { + debug("db", "returning default value for " + name, e); + return defaultValue; + } + } + @Override + protected void onPostExecute(String result) { + cb.onResult(result); + } + }; + + t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); + } static public String getOption(String name, String default_value) { debug("db", "about to fetch option " + name); SQLiteDatabase db = getDatabase(); @@ -114,10 +150,10 @@ public final class MLDB { } static public void setOption(String name, String value) { debug("option", String.format("%s := %s", name, value)); - SQLiteDatabase db = MLDB.getDatabase(); - db.execSQL("insert or replace into options(profile, name, value) values(?, ?, ?);", + DbOpQueue.add("insert or replace into options(profile, name, value) values(?, ?, ?);", new String[]{NO_PROFILE, name, value}); } + @SuppressWarnings("unused") static public void setLongOption(String name, long value) { setOption(name, String.valueOf(value)); } @@ -126,7 +162,8 @@ public final class MLDB { final AutoCompleteTextView view, final String table, final String field, final boolean profileSpecific) { - hookAutocompletionAdapter(context, view, table, field, profileSpecific, null, null, Data.profile.get()); + hookAutocompletionAdapter(context, view, table, field, profileSpecific, null, null, + Data.profile.getValue()); } @TargetApi(Build.VERSION_CODES.N) public static void hookAutocompletionAdapter(final Context context, @@ -177,7 +214,8 @@ public final class MLDB { while (matches.moveToNext()) { String match = matches.getString(0); int order = matches.getInt(1); - debug("autocompletion", String.format("match: %s |%d", match, order)); + debug("autocompletion", + String.format(Locale.ENGLISH, "match: %s |%d", match, order)); c.newRow().add(i++).add(match); } } @@ -221,7 +259,7 @@ class MobileLedgerDatabase extends SQLiteOpenHelper { private final Application mContext; - public MobileLedgerDatabase(Application context) { + MobileLedgerDatabase(Application context) { super(context, DB_NAME, null, LATEST_REVISION); debug("db", "creating helper instance"); mContext = context;