]> git.ktnx.net Git - mobile-ledger.git/commitdiff
convert AsyncTask descender classes to pure Thread's
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 27 Aug 2021 20:11:51 +0000 (20:11 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 27 Aug 2021 20:11:51 +0000 (20:11 +0000)
perhaps these should use a cachedThreadExecutor for handling long jobs
such as network transfer? doesn't seem any better

app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java
app/src/main/java/net/ktnx/mobileledger/async/TransactionDateFinder.java
app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java

index 7470f0eaf3970bdf1f4b380d86b1380d0c1d490f..8add70b298080bb8fc7dc9338b5c031a03dae0f3 100644 (file)
@@ -18,7 +18,6 @@
 package net.ktnx.mobileledger.async;
 
 import android.annotation.SuppressLint;
-import android.os.AsyncTask;
 import android.os.OperationCanceledException;
 
 import androidx.annotation.NonNull;
@@ -43,7 +42,6 @@ import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerAccount;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
-import net.ktnx.mobileledger.ui.MainModel;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.NetworkUtil;
 
@@ -67,8 +65,7 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 
-public class RetrieveTransactionsTask extends
-        AsyncTask<Void, RetrieveTransactionsTask.Progress, RetrieveTransactionsTask.Result> {
+public class RetrieveTransactionsTask extends Thread {
     private static final int MATCHING_TRANSACTIONS_LIMIT = 150;
     private static final Pattern reComment = Pattern.compile("^\\s*;");
     private static final Pattern reTransactionStart = Pattern.compile(
@@ -88,11 +85,9 @@ public class RetrieveTransactionsTask extends
             Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
     private final Pattern reAccountValue = Pattern.compile(
             "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
-    private final MainModel mainModel;
     private final Profile profile;
     private int expectedPostingsCount = -1;
-    public RetrieveTransactionsTask(@NonNull MainModel mainModel, @NonNull Profile profile) {
-        this.mainModel = mainModel;
+    public RetrieveTransactionsTask(@NonNull Profile profile) {
         this.profile = profile;
     }
     private static void L(String msg) {
@@ -125,25 +120,19 @@ public class RetrieveTransactionsTask extends
             return null;
         }
     }
-    @Override
-    protected void onProgressUpdate(Progress... values) {
-        super.onProgressUpdate(values);
-        Data.backgroundTaskProgress.postValue(values[0]);
+    private void publishProgress(Progress progress) {
+        Data.backgroundTaskProgress.postValue(progress);
     }
-    @Override
-    protected void onPostExecute(Result result) {
-        super.onPostExecute(result);
+    private void finish(Result result) {
         Progress progress = new Progress();
         progress.setState(ProgressState.FINISHED);
         progress.setError(result.error);
-        onProgressUpdate(progress);
+        publishProgress(progress);
     }
-    @Override
-    protected void onCancelled() {
-        super.onCancelled();
+    private void cancel() {
         Progress progress = new Progress();
         progress.setState(ProgressState.FINISHED);
-        onProgressUpdate(progress);
+        publishProgress(progress);
     }
     private void retrieveTransactionListLegacy(List<LedgerAccount> accounts,
                                                List<LedgerTransaction> transactions)
@@ -549,7 +538,7 @@ public class RetrieveTransactionsTask extends
 
     @SuppressLint("DefaultLocale")
     @Override
-    protected Result doInBackground(Void... params) {
+    public void run() {
         Data.backgroundTaskStarted();
         List<LedgerAccount> accounts;
         List<LedgerTransaction> transactions;
@@ -574,43 +563,43 @@ public class RetrieveTransactionsTask extends
 
             Data.lastUpdateDate.postValue(new Date());
 
-            return new Result(null);
+            finish(new Result(null));
         }
         catch (MalformedURLException e) {
             e.printStackTrace();
-            return new Result("Invalid server URL");
+            finish(new Result("Invalid server URL"));
         }
         catch (HTTPException e) {
             e.printStackTrace();
-            return new Result(
-                    String.format("HTTP error %d: %s", e.getResponseCode(), e.getMessage()));
+            finish(new Result(
+                    String.format("HTTP error %d: %s", e.getResponseCode(), e.getMessage())));
         }
         catch (IOException e) {
             e.printStackTrace();
-            return new Result(e.getLocalizedMessage());
+            finish(new Result(e.getLocalizedMessage()));
         }
         catch (RuntimeJsonMappingException e) {
             e.printStackTrace();
-            return new Result(Result.ERR_JSON_PARSER_ERROR);
+            finish(new Result(Result.ERR_JSON_PARSER_ERROR));
         }
         catch (ParseException e) {
             e.printStackTrace();
-            return new Result("Network error");
+            finish(new Result("Network error"));
         }
         catch (OperationCanceledException e) {
             e.printStackTrace();
-            return new Result("Operation cancelled");
+            finish(new Result("Operation cancelled"));
         }
         catch (ApiNotSupportedException e) {
             e.printStackTrace();
-            return new Result("Server version not supported");
+            finish(new Result("Server version not supported"));
         }
         finally {
             Data.backgroundTaskFinished();
         }
     }
     public void throwIfCancelled() {
-        if (isCancelled())
+        if (isInterrupted())
             throw new OperationCanceledException(null);
     }
     private enum ParserState {
index e7a26beb852f443073c3fcf58022335a467189b6..79a58173f10e502d11dfbd752fcfdef355aeb4b3 100644 (file)
@@ -17,7 +17,6 @@
 
 package net.ktnx.mobileledger.async;
 
-import android.os.AsyncTask;
 import android.util.Log;
 
 import net.ktnx.mobileledger.db.Profile;
@@ -28,6 +27,7 @@ import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.utils.Globals;
 import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
 import net.ktnx.mobileledger.utils.NetworkUtil;
 import net.ktnx.mobileledger.utils.SimpleDate;
 import net.ktnx.mobileledger.utils.UrlEncodedFormData;
@@ -45,7 +45,6 @@ import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import static android.os.SystemClock.sleep;
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
 /* TODO: get rid of the custom session/cookie and auth code?
@@ -54,26 +53,22 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
  *       at which point the HTML form emulation can be dropped entirely
  */
 
-public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void> {
+public class SendTransactionTask extends Thread {
     private final TaskCallback taskCallback;
     private final Profile mProfile;
     private final boolean simulate;
+    private final LedgerTransaction transaction;
     protected String error;
     private String token;
     private String session;
-    private LedgerTransaction transaction;
 
     public SendTransactionTask(TaskCallback callback, Profile profile,
-                               boolean simulate) {
+                               LedgerTransaction transaction, boolean simulate) {
         taskCallback = callback;
         mProfile = profile;
+        this.transaction = transaction;
         this.simulate = simulate;
     }
-    public SendTransactionTask(TaskCallback callback, Profile profile) {
-        taskCallback = callback;
-        mProfile = profile;
-        simulate = false;
-    }
     private void sendOK(API apiVersion) throws IOException, ApiNotSupportedException {
         HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add");
         http.setRequestMethod("PUT");
@@ -248,11 +243,9 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
         }
     }
     @Override
-    protected Void doInBackground(LedgerTransaction... ledgerTransactions) {
+    public void run() {
         error = null;
         try {
-            transaction = ledgerTransactions[0];
-
             final API profileApiVersion = API.valueOf(mProfile.getApiVersion());
             switch (profileApiVersion) {
                 case auto:
@@ -293,7 +286,9 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
             error = e.getMessage();
         }
 
-        return null;
+        Misc.onMainThread(()->{
+            taskCallback.onTransactionSaveDone(error, transaction);
+        });
     }
     private void legacySendOkWithRetry() throws IOException {
         int tried = 0;
@@ -301,13 +296,13 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
             tried++;
             if (tried >= 2)
                 throw new IOException(String.format("aborting after %d tries", tried));
-            sleep(100);
+            try {
+                sleep(100);
+            }
+            catch (InterruptedException e) {
+                e.printStackTrace();
+                break;
+            }
         }
     }
-    @Override
-    protected void onPostExecute(Void aVoid) {
-        super.onPostExecute(aVoid);
-        taskCallback.onTransactionSaveDone(error, transaction);
-    }
-
 }
\ No newline at end of file
index 2bce07e1ca08fc4218aa7aba911491c9fc2c5c87..18cea62fa2b3f415f49598d1054bda02106d5d87 100644 (file)
@@ -17,8 +17,6 @@
 
 package net.ktnx.mobileledger.async;
 
-import android.os.AsyncTask;
-
 import net.ktnx.mobileledger.model.TransactionListItem;
 import net.ktnx.mobileledger.ui.MainModel;
 import net.ktnx.mobileledger.utils.Logger;
@@ -32,22 +30,21 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Objects;
 
-public class TransactionDateFinder extends AsyncTask<TransactionDateFinder.Params, Void, Integer> {
-    private MainModel model;
-    @Override
-    protected void onPostExecute(Integer pos) {
-        model.foundTransactionItemIndex.setValue(pos);
+public class TransactionDateFinder extends Thread {
+    private final MainModel model;
+    private final SimpleDate date;
+    public TransactionDateFinder(MainModel model, SimpleDate date) {
+        this.model = model;
+        this.date = date;
     }
     @Override
-    protected Integer doInBackground(Params... param) {
-        this.model = param[0].model;
-        SimpleDate date = param[0].date;
+    public void run() {
         Logger.debug("go-to-date",
                 String.format(Locale.US, "Looking for date %04d-%02d-%02d", date.year, date.month,
                         date.day));
         List<TransactionListItem> transactions = Objects.requireNonNull(
-                param[0].model.getDisplayedTransactions()
-                              .getValue());
+                model.getDisplayedTransactions()
+                     .getValue());
         final int transactionCount = transactions.size();
         Logger.debug("go-to-date",
                 String.format(Locale.US, "List contains %d transactions", transactionCount));
@@ -55,19 +52,10 @@ public class TransactionDateFinder extends AsyncTask<TransactionDateFinder.Param
         TransactionListItem target = new TransactionListItem(date, true);
         int found =
                 Collections.binarySearch(transactions, target, new TransactionListItemComparator());
-        if (found >= 0)
-            return found;
-        else
-            return -1 - found;
-    }
+        if (found < 0)
+            found = -1 - found;
 
-    public static class Params {
-        public final MainModel model;
-        public final SimpleDate date;
-        public Params(@NotNull MainModel model, @NotNull SimpleDate date) {
-            this.model = model;
-            this.date = date;
-        }
+        model.foundTransactionItemIndex.postValue(found);
     }
 
     static class TransactionListItemComparator implements Comparator<TransactionListItem> {
index e95aae8c2bc17864d819ae47628bb1966462271e..329109fdccf3947f578e0eb2ac20a6abd58fcb14 100644 (file)
@@ -17,8 +17,6 @@
 
 package net.ktnx.mobileledger.ui;
 
-import android.os.AsyncTask;
-
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.ViewModel;
@@ -84,14 +82,14 @@ public class MainModel extends ViewModel {
         }
         Profile profile = Data.getProfile();
 
-        retrieveTransactionsTask = new RetrieveTransactionsTask(this, profile);
+        retrieveTransactionsTask = new RetrieveTransactionsTask(profile);
         Logger.debug("db", "Created a background transaction retrieval task");
 
-        retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
+        retrieveTransactionsTask.start();
     }
     public synchronized void stopTransactionsRetrieval() {
         if (retrieveTransactionsTask != null)
-            retrieveTransactionsTask.cancel(true);
+            retrieveTransactionsTask.interrupt();
         else
             Data.backgroundTaskProgress.setValue(null);
     }
index d4355a9b012df33398474e2658eaba5dd660058c..a1d9e74d904877bb800eebffd811e126f591e4ff 100644 (file)
@@ -160,8 +160,8 @@ public class NewTransactionActivity extends ProfileThemedActivity
         try {
 
             SendTransactionTask saver =
-                    new SendTransactionTask(this, mProfile, model.getSimulateSaveFlag());
-            saver.execute(tr);
+                    new SendTransactionTask(this, mProfile, tr, model.getSimulateSaveFlag());
+            saver.start();
         }
         catch (Exception e) {
             debug("new-transaction", "Unknown error: " + e);
index 5a01bde0b9c435c32dd5c48e6ccd431f6da7a4c9..a31b657fdc52b106e6ffd327e040b0d9cc3118e1 100644 (file)
@@ -17,7 +17,6 @@
 
 package net.ktnx.mobileledger.ui.transaction_list;
 
-import android.os.AsyncTask;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.Menu;
@@ -230,9 +229,8 @@ public class TransactionListFragment extends MobileLedgerListFragment
     @Override
     public void onDatePicked(int year, int month, int day) {
         RecyclerView list = requireActivity().findViewById(R.id.transaction_root);
-        AsyncTask<TransactionDateFinder.Params, Void, Integer> finder = new TransactionDateFinder();
+        TransactionDateFinder finder = new TransactionDateFinder(model, new SimpleDate(year, month + 1, day));
 
-        finder.execute(
-                new TransactionDateFinder.Params(model, new SimpleDate(year, month + 1, day)));
+        finder.start();
     }
 }