X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FSendTransactionTask.java;h=9754a84e008f419430a1b35cafdeffbe6b5181c2;hb=20c03b7a5eb152d42fbbe9ecbaae27530563b398;hp=cfbf5222c30a24ff41c9c1282a664da17e61a46e;hpb=4c2d1b2095d022074deed016e95cd3cca3b9f624;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 cfbf5222..9754a84e 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; protected String error; private String token; private String session; - private LedgerTransaction ltr; - private MobileLedgerProfile mProfile; - private boolean simulate = false; + private LedgerTransaction transaction; + private final MobileLedgerProfile mProfile; + private final boolean simulate; public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile, boolean simulate) { @@ -70,8 +77,41 @@ public class SendTransactionTask extends AsyncTask 0.3) @@ -84,17 +124,6 @@ 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 pre_1_15: + send_1_14_OK(); + break; + case post_1_14: + send_1_15_OK(); + break; + default: + throw new IllegalStateException( + "Unexpected API version: " + mProfile.getApiVersion()); } } catch (Exception e) { @@ -250,10 +298,55 @@ 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), pre_1_15(-2), post_1_14(-3); + private static final SparseArray map = new SparseArray<>(); + + static { + for (API item : API.values()) { + map.put(item.value, item); + } + } + + private 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 pre_1_15: + return resources.getString(R.string.api_pre_1_15); + case post_1_14: + return resources.getString(R.string.api_post_1_14); + default: + throw new IllegalStateException("Unexpected value: " + value); + } + } + } }