]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java
move API version enum in a standalone class
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / SendTransactionTask.java
index f2ed99ff9e09d4170b538a2409341914eafab0cf..dc0c24875469a74b5d6404097a21034d1690672a 100644 (file)
 
 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.R;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
@@ -59,12 +56,12 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 
 public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void> {
     private final TaskCallback taskCallback;
+    private final MobileLedgerProfile mProfile;
+    private final boolean simulate;
     protected String error;
     private String token;
     private String session;
     private LedgerTransaction transaction;
-    private MobileLedgerProfile mProfile;
-    private boolean simulate;
 
     public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile,
                                boolean simulate) {
@@ -137,8 +134,8 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
             req.write(bodyBytes);
 
             final int responseCode = http.getResponseCode();
-            debug("network",
-                    String.format("Response: %d %s", responseCode, http.getResponseMessage()));
+            debug("network", String.format(Locale.US, "Response: %d %s", responseCode,
+                    http.getResponseMessage()));
 
             try (InputStream resp = http.getErrorStream()) {
 
@@ -147,8 +144,19 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
                     case 201:
                         break;
                     case 400:
-                    case 405:
+                    case 405: {
+                        BufferedReader reader = new BufferedReader(new InputStreamReader(resp));
+                        String line;
+                        int count = 0;
+                        while (count <= 5) {
+                            line = reader.readLine();
+                            if (line == null)
+                                break;
+                            Logger.debug("network", line);
+                            count++;
+                        }
                         return false; // will cause a retry with the legacy method
+                    }
                     default:
                         BufferedReader reader = new BufferedReader(new InputStreamReader(resp));
                         String line = reader.readLine();
@@ -177,10 +185,9 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
         if (token != null)
             params.addPair("_token", token);
 
-        SimpleDate transactionDate = transaction.getDate();
-        if (transactionDate == null) {
+        SimpleDate transactionDate = transaction.getDateIfAny();
+        if (transactionDate == null)
             transactionDate = SimpleDate.today();
-        }
 
         params.addPair("date", Globals.formatLedgerDate(transactionDate));
         params.addPair("description", transaction.getDescription());
@@ -283,10 +290,10 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
                 case html:
                     legacySendOkWithRetry();
                     break;
-                case pre_1_15:
+                case v1_14:
                     send_1_14_OK();
                     break;
-                case post_1_14:
+                case v1_15:
                     send_1_15_OK();
                     break;
                 default:
@@ -316,40 +323,4 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
         taskCallback.done(error);
     }
 
-    public enum API {
-        auto(0), html(-1), pre_1_15(-2), post_1_14(-3);
-        private static SparseArray<API> 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);
-            }
-        }
-    }
-}
+}
\ No newline at end of file