X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FSendTransactionTask.java;h=0e700cca9a510be8e210fafccbe548b88c2a427a;hp=dc0c24875469a74b5d6404097a21034d1690672a;hb=HEAD;hpb=bb789332571609eeb1bef6e39b7ad359227d1045 diff --git a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java index dc0c2487..0e700cca 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Damyan Ivanov. + * Copyright © 2023 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 @@ -17,17 +17,19 @@ package net.ktnx.mobileledger.async; -import android.os.AsyncTask; -import android.util.Log; +import static net.ktnx.mobileledger.utils.Logger.debug; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; +import android.util.Log; +import net.ktnx.mobileledger.db.Profile; +import net.ktnx.mobileledger.json.API; +import net.ktnx.mobileledger.json.ApiNotSupportedException; +import net.ktnx.mobileledger.json.Gateway; import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransactionAccount; -import net.ktnx.mobileledger.model.MobileLedgerProfile; 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,68 +47,42 @@ 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? * (the last problem with the POST was the missing content-length header) * This will resolve itself when hledger-web 1.14+ is released with Debian/stable, * at which point the HTML form emulation can be dropped entirely */ -public class SendTransactionTask extends AsyncTask { +public class SendTransactionTask extends Thread { private final TaskCallback taskCallback; - private final MobileLedgerProfile mProfile; + 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, MobileLedgerProfile profile, - boolean simulate) { + public SendTransactionTask(TaskCallback callback, Profile profile, + LedgerTransaction transaction, boolean simulate) { taskCallback = callback; mProfile = profile; + this.transaction = transaction; this.simulate = simulate; } - public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile) { - taskCallback = callback; - mProfile = profile; - simulate = false; - } - private boolean send_1_15_OK() throws IOException { - HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add"); - http.setRequestMethod("PUT"); - http.setRequestProperty("Content-Type", "application/json"); - http.setRequestProperty("Accept", "*/*"); - - net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction jsonTransaction = - net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.fromLedgerTransaction( - transaction); - ObjectMapper mapper = new ObjectMapper(); - ObjectWriter writer = - mapper.writerFor(net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.class); - String body = writer.writeValueAsString(jsonTransaction); - - return sendRequest(http, body); - } - private boolean send_1_14_OK() throws IOException { + private void sendOK(API apiVersion) throws IOException, ApiNotSupportedException { HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add"); http.setRequestMethod("PUT"); http.setRequestProperty("Content-Type", "application/json"); http.setRequestProperty("Accept", "*/*"); - net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction jsonTransaction = - net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.fromLedgerTransaction( - transaction); - ObjectMapper mapper = new ObjectMapper(); - ObjectWriter writer = - mapper.writerFor(net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.class); - String body = writer.writeValueAsString(jsonTransaction); + Gateway gateway = Gateway.forApiVersion(apiVersion); + String body = gateway.transactionSaveRequest(transaction); - return sendRequest(http, body); + Logger.debug("network", "Sending using API " + apiVersion); + sendRequest(http, body); } - private boolean sendRequest(HttpURLConnection http, String body) throws IOException { + private void sendRequest(HttpURLConnection http, String body) + throws IOException, ApiNotSupportedException { if (simulate) { debug("network", "The request would be: " + body); try { @@ -118,7 +94,7 @@ public class SendTransactionTask extends AsyncTask taskCallback.onTransactionSaveDone(error, transaction)); } private void legacySendOkWithRetry() throws IOException { int tried = 0; @@ -314,13 +295,13 @@ public class SendTransactionTask extends AsyncTask= 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.done(error); - } - } \ No newline at end of file