From 3572408a760bc8ce8c4acb8210dadfcb9672a193 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sun, 22 Sep 2019 13:55:17 +0100 Subject: [PATCH] CheckedAsyncDownloader: use proper enum for result codes --- .../ctfwstimer/CheckedAsyncDownloader.java | 51 ++++++++++++------- .../ctfwstimer/HandbookDownloader.java | 15 +++--- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CheckedAsyncDownloader.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CheckedAsyncDownloader.java index 71907b5..38c7dab 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CheckedAsyncDownloader.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CheckedAsyncDownloader.java @@ -19,27 +19,41 @@ import java.security.NoSuchAlgorithmException; public class CheckedAsyncDownloader extends AsyncTask { - public static final long ERR_UNTRIED = -1; /* Not yet tried */ - public static final long ERR_ALREADY = -2; /* Existing file matches checksum */ - public static final long ERR_WRITE = -3; /* Local FS error */ - public static final long ERR_HOSTUNREACH = -4; /* Could not establish connection */ - public static final long ERR_XFER = -5; /* Error during transfer */ - public static final long ERR_CHECKSUM = -6; /* Checksum did not match after xfer */ - public static final long ERR_TOO_LONG = -7; /* File longer than maximum permitted */ + public enum Result { + RES_OK, + RES_UNTRIED, /* Not yet tried */ + RES_ALREADY, /* Existing file matches checksum */ + ERR_WRITE, /* Local FS error */ + ERR_HOSTUNREACH, /* Could not establish connection */ + ERR_XFER, /* Error during transfer */ + ERR_CHECKSUM, /* Checksum did not match after xfer */ + ERR_TOO_LONG, /* File longer than maximum permitted */ + } public static class DL { final URL url; final byte[] sha256; final File dest; final long lengthLimit; /* In bytes, or 0 for no limit */ - long result; + Result result; + long dlsize; public DL(URL url, byte[] sha256, long lim, File dest) { this.url = url; this.sha256 = sha256; this.dest = dest; this.lengthLimit = lim; - this.result = ERR_UNTRIED; + this.result = Result.RES_UNTRIED; + this.dlsize = 0; + } + + public DL(DL dl) { + this.url = dl.url; + this.sha256 = dl.sha256; + this.dest = dl.dest; + this.lengthLimit = dl.lengthLimit; + this.result = dl.result; + this.dlsize = dl.dlsize; } } @@ -64,7 +78,7 @@ public class CheckedAsyncDownloader extends AsyncTask 0 && xfer > dl.lengthLimit) { is.close(); os.close(); - dl.result = ERR_TOO_LONG; + dl.result = Result.ERR_TOO_LONG; oft.delete(); continue dlfor; } @@ -132,24 +146,25 @@ public class CheckedAsyncDownloader extends AsyncTask= 0) || (result == ERR_ALREADY)) { - HandbookDownloader.this.lastFetchedChecksum = dl.sha256; - } fini(); } Log.d(TAG, "Post Ex: " + result); - if (result >= 0) { + if (result == Result.RES_OK) { /* If we downloaded something new, go run the callback chain */ HandbookDownloader.this.mDLFiniCB.run(); } @@ -172,8 +167,10 @@ public class HandbookDownloader implements IMqttMessageListener { + (Character.digit(checksum_str.charAt(2*i+1),16))); } synchronized (this) { - if (lastFetchedChecksum != null - && java.util.Arrays.equals(checksum, lastFetchedChecksum)) { + if (download != null + && (download.result == CheckedAsyncDownloader.Result.RES_OK + || download.result == CheckedAsyncDownloader.Result.RES_ALREADY) + && java.util.Arrays.equals(checksum, download.sha256)) { /* Nothing to do */ Log.d(TAG, "Checksum matches last fetch"); return; -- 2.50.1