X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FSendTransactionTask.java;h=9f441f9ca6148b5ddbabd0e24a6059ae9eaecc10;hb=a87079ed41bdc3ad89fe8bd15dfba10e37b29b76;hp=496624bf52475a6c22e6e2a2388d9d48d44aa53b;hpb=0fa11b9a1009c706c3f75ef61a3a95bcfd05cab3;p=mobile-ledger-staging.git 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 496624bf..9f441f9c 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 © 2019 Damyan Ivanov. + * Copyright © 2020 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,19 +17,22 @@ package net.ktnx.mobileledger.async; +import android.content.res.Resources; import android.os.AsyncTask; import android.util.Log; +import android.util.SparseArray; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; -import net.ktnx.mobileledger.json.ParsedLedgerTransaction; +import net.ktnx.mobileledger.R; 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.NetworkUtil; +import net.ktnx.mobileledger.utils.SimpleDate; import net.ktnx.mobileledger.utils.UrlEncodedFormData; import java.io.BufferedReader; @@ -39,8 +42,6 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.nio.charset.StandardCharsets; -import java.util.Date; -import java.util.GregorianCalendar; import java.util.List; import java.util.Locale; import java.util.Map; @@ -50,14 +51,20 @@ 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 { private final TaskCallback taskCallback; + private final MobileLedgerProfile mProfile; + private final boolean simulate; protected String error; private String token; private String session; - private LedgerTransaction ltr; - private MobileLedgerProfile mProfile; - private boolean simulate = false; + private LedgerTransaction transaction; public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile, boolean simulate) { @@ -70,18 +77,39 @@ public class SendTransactionTask extends AsyncTask= 2) - throw new IOException(String.format("aborting after %d tries", tried)); - sleep(100); - } + transaction = ledgerTransactions[0]; + + switch (mProfile.getApiVersion()) { + case auto: + Logger.debug("network", "Trying version 1.5."); + if (!send_1_15_OK()) { + Logger.debug("network", "Version 1.5 request failed. Trying with 1.14"); + if (!send_1_14_OK()) { + Logger.debug("network", + "Version 1.14 failed too. Trying HTML form emulation"); + legacySendOkWithRetry(); + } + else { + Logger.debug("network", "Version 1.14 request succeeded"); + } + } + else { + Logger.debug("network", "Version 1.15 request succeeded"); + } + break; + case html: + legacySendOkWithRetry(); + break; + case v1_14: + send_1_14_OK(); + break; + case v1_15: + send_1_15_OK(); + break; + default: + throw new IllegalStateException( + "Unexpected API version: " + mProfile.getApiVersion()); } } catch (Exception e) { @@ -251,10 +311,74 @@ public class SendTransactionTask extends AsyncTask= 2) + throw new IOException(String.format("aborting after %d tries", tried)); + sleep(100); + } + } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); taskCallback.done(error); } -} + + public enum API { + auto(0), html(-1), v1_14(-2), v1_15(-3), v1_19_1(-4); + private static final SparseArray map = new SparseArray<>(); + public static API[] allVersions = {v1_19_1, v1_15, v1_14}; + + static { + for (API item : API.values()) { + map.put(item.value, item); + } + } + + private final int value; + + API(int value) { + this.value = value; + } + public static API valueOf(int i) { + return map.get(i, auto); + } + public int toInt() { + return this.value; + } + public String getDescription(Resources resources) { + switch (this) { + case auto: + return resources.getString(R.string.api_auto); + case html: + return resources.getString(R.string.api_html); + case v1_14: + return resources.getString(R.string.api_1_14); + case v1_15: + return resources.getString(R.string.api_1_15); + case v1_19_1: + return resources.getString(R.string.api_1_19_1); + default: + throw new IllegalStateException("Unexpected value: " + value); + } + } + public String getDescription() { + switch (this) { + case auto: + return "(automatic)"; + case html: + return "(HTML)"; + case v1_14: + return "1.14"; + case v1_15: + return "1.15"; + case v1_19_1: + return "1.19.1"; + default: + throw new IllegalStateException("Unexpected value: " + this); + } + } + } +} \ No newline at end of file