]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/RetrieveAccountsTask.java
increment background task count while retrieving accounts too
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveAccountsTask.java
index a342ca4dcc9b61b845d8adc25a0d7e591b8cc8bb..e046040e137b92431615455e20b9e89adaff7829 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2018 Damyan Ivanov.
+ * Copyright © 2019 Damyan Ivanov.
  * This file is part of Mobile-Ledger.
  * Mobile-Ledger is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
@@ -19,11 +19,13 @@ package net.ktnx.mobileledger.async;
 
 import android.content.SharedPreferences;
 import android.database.sqlite.SQLiteDatabase;
+import android.os.OperationCanceledException;
 import android.util.Log;
 
-import net.ktnx.mobileledger.AccountSummary;
 import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerAccount;
+import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.utils.MLDB;
 import net.ktnx.mobileledger.utils.NetworkUtil;
 
@@ -41,10 +43,10 @@ import java.util.regex.Pattern;
 
 public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
     int error;
-    WeakReference<AccountSummary> mContext;
+    WeakReference<MainActivity> mContext;
     private SharedPreferences pref;
 
-    public RetrieveAccountsTask(WeakReference<AccountSummary> context) {
+    public RetrieveAccountsTask(WeakReference<MainActivity> context) {
         mContext = context;
         error = 0;
     }
@@ -54,10 +56,11 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Vo
     }
 
     protected Void doInBackground(Void... params) {
+        Data.backgroundTaskCount.incrementAndGet();
         try {
             HttpURLConnection http = NetworkUtil.prepare_connection(pref, "add");
             publishProgress(0);
-            try (SQLiteDatabase db = MLDB.getWritableDatabase(mContext.get())) {
+            try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
                 try (InputStream resp = http.getInputStream()) {
                     Log.d("update_accounts", String.valueOf(http.getResponseCode()));
                     if (http.getResponseCode() != 200) {
@@ -81,15 +84,17 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Vo
                             // %3A is '='
                             Pattern account_name_re =
                                     Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
-                            Pattern value_re = Pattern.compile(
+                            Pattern account_value_re = Pattern.compile(
                                     "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
-                            Pattern tr_re = Pattern.compile("</tr>");
+                            Pattern tr_end_re = Pattern.compile("</tr>");
                             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);
@@ -105,7 +110,7 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Vo
                                     continue;
                                 }
 
-                                Matcher tr_m = tr_re.matcher(line);
+                                Matcher tr_m = tr_end_re.matcher(line);
                                 if (tr_m.find()) {
                                     Log.d("account-parser", "<tr> - another account expected");
                                     last_account_name = null;
@@ -113,9 +118,11 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Vo
                                 }
 
                                 if (last_account_name != null) {
-                                    m = value_re.matcher(line);
+                                    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);
@@ -137,6 +144,8 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Vo
                                     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;
 
@@ -153,13 +162,14 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Vo
 
                             db.execSQL("delete from account_values where keep=0;");
                             db.execSQL("delete from accounts where keep=0;");
-//                        db.execSQL("delete from description_history where keep=0;");
                             db.setTransactionSuccessful();
                         }
+                        catch (OperationCanceledException e) {
+                            Log.w("async", "Account retrieval cancelled");
+                        }
                         finally {
                             db.endTransaction();
                         }
-
                     }
                 }
             }
@@ -180,9 +190,16 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Vo
             error = R.string.err_net_error;
             e.printStackTrace();
         }
+        finally {
+            Log.d("RAT", "decrementing background task count");
+            Data.backgroundTaskCount.decrementAndGet();
+        }
 
         return null;
     }
+    private void throwIfCancelled() {
+        if (isCancelled()) throw new OperationCanceledException(null);
+    }
 
     private void addAccount(SQLiteDatabase db, String name) {
         do {
@@ -197,9 +214,9 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Vo
     }
     @Override
     protected void onPostExecute(Void result) {
-        AccountSummary ctx = mContext.get();
+        MainActivity ctx = mContext.get();
         if (ctx == null) return;
-        ctx.onAccountRefreshDone(this.error);
+        ctx.onRetrieveDone(this.error == 0);
     }
 
 }