From 9579cf8c416181bc54107a3577a0da8bd62516df Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 3 May 2019 18:22:39 +0300 Subject: [PATCH] MLDB: asyncronous variant of getOption --- .../mobileledger/utils/GetOptCallback.java | 22 +++++++++++++ .../net/ktnx/mobileledger/utils/MLDB.java | 33 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 app/src/main/java/net/ktnx/mobileledger/utils/GetOptCallback.java 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(); -- 2.39.2