From 1aadcdeec93fc8892a8323735334890cd8859c80 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 5 Jan 2019 07:58:34 +0000 Subject: [PATCH] stop closing acuired db handles and leave that for the application termination these aren't reference-counted and can be destroyed by one thread while another still uses them --- .../mobileledger/MobileLedgerApplication.java | 5 + .../async/RetrieveAccountsTask.java | 203 +++++++++--------- .../async/UpdateTransactionsTask.java | 15 +- .../TransactionListAdapter.java | 20 +- .../net/ktnx/mobileledger/utils/MLDB.java | 78 +++---- 5 files changed, 162 insertions(+), 159 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java index f4a72159..20762c07 100644 --- a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java +++ b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java @@ -34,6 +34,11 @@ public class MobileLedgerApplication extends Application { MLDB.init(this); } @Override + public void onTerminate() { + MLDB.done(); + super.onTerminate(); + } + @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); updateColorValues(); diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveAccountsTask.java index e046040e..8602ed14 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveAccountsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveAccountsTask.java @@ -60,116 +60,113 @@ public class RetrieveAccountsTask extends android.os.AsyncTask\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?"); + Pattern tr_end_re = Pattern.compile(""); + Pattern descriptions_line_re = + Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b"); + Pattern description_items_re = Pattern.compile("\"value\":\"([^\"]+)\""); + int count = 0; + while ((line = buf.readLine()) != null) { + throwIfCancelled(); + + Matcher m = account_name_re.matcher(line); + if (m.find()) { + String acct_encoded = m.group(1); + String acct_name = URLDecoder.decode(acct_encoded, "UTF-8"); + acct_name = acct_name.replace("\"", ""); + Log.d("account-parser", acct_name); + + addAccount(db, acct_name); + publishProgress(++count); + + last_account_name = acct_name; + + continue; + } - Matcher tr_m = tr_end_re.matcher(line); - if (tr_m.find()) { - Log.d("account-parser", " - another account expected"); - last_account_name = null; - continue; - } + Matcher tr_m = tr_end_re.matcher(line); + if (tr_m.find()) { + Log.d("account-parser", " - another account expected"); + last_account_name = null; + continue; + } - if (last_account_name != null) { - m = account_value_re.matcher(line); - boolean match_found = false; - while (m.find()) { - throwIfCancelled(); - - match_found = true; - String value = m.group(1); - String currency = m.group(2); - if (currency == null) currency = ""; - value = value.replace(',', '.'); - Log.d("db", "curr=" + currency + ", value=" + value); - db.execSQL( - "insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);", - new Object[]{last_account_name, currency, - Float.valueOf(value) - }); - } - - if (match_found) continue; + if (last_account_name != null) { + m = account_value_re.matcher(line); + boolean match_found = false; + while (m.find()) { + throwIfCancelled(); + + match_found = true; + String value = m.group(1); + String currency = m.group(2); + if (currency == null) currency = ""; + value = value.replace(',', '.'); + Log.d("db", "curr=" + currency + ", value=" + value); + db.execSQL( + "insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);", + new Object[]{last_account_name, currency, + Float.valueOf(value) + }); } - m = descriptions_line_re.matcher(line); - if (m.find()) { - db.execSQL("update description_history set keep=0;"); - m = description_items_re.matcher(line); - while (m.find()) { - throwIfCancelled(); - - String description = m.group(1); - if (description.isEmpty()) continue; - - Log.d("db", String.format("Stored description: %s", - description)); - db.execSQL("insert or replace into description_history" + - "(description, description_upper, keep) " + - "values(?, ?, 1);", - new Object[]{description, description.toUpperCase() - }); - } - } + if (match_found) continue; } - db.execSQL("delete from account_values where keep=0;"); - db.execSQL("delete from accounts where keep=0;"); - db.setTransactionSuccessful(); - } - catch (OperationCanceledException e) { - Log.w("async", "Account retrieval cancelled"); - } - finally { - db.endTransaction(); + m = descriptions_line_re.matcher(line); + if (m.find()) { + db.execSQL("update description_history set keep=0;"); + m = description_items_re.matcher(line); + while (m.find()) { + throwIfCancelled(); + + String description = m.group(1); + if (description.isEmpty()) continue; + + Log.d("db", + String.format("Stored description: %s", description)); + db.execSQL("insert or replace into description_history" + + "(description, description_upper, keep) " + + "values(?, ?, 1);", + new Object[]{description, description.toUpperCase() + }); + } + } } + + db.execSQL("delete from account_values where keep=0;"); + db.execSQL("delete from accounts where keep=0;"); + db.setTransactionSuccessful(); + } + catch (OperationCanceledException e) { + Log.w("async", "Account retrieval cancelled"); + } + finally { + db.endTransaction(); } } } diff --git a/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java index 0509f353..b3e2e0c5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java @@ -52,16 +52,15 @@ public class UpdateTransactionsTask extends AsyncTask