From: Damyan Ivanov Date: Fri, 3 May 2019 15:22:39 +0000 (+0300) Subject: MLDB: asyncronous variant of getOption X-Git-Tag: v0.10.0~48 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=9579cf8c416181bc54107a3577a0da8bd62516df;p=mobile-ledger.git MLDB: asyncronous variant of getOption --- diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/GetOptCallback.java b/app/src/main/java/net/ktnx/mobileledger/utils/GetOptCallback.java new file mode 100644 index 00000000..c30c1504 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/utils/GetOptCallback.java @@ -0,0 +1,22 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.utils; + +abstract public class GetOptCallback { + protected abstract void onResult(String result); +} 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 a735ee90..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; @@ -94,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();