From 50e40f8dac9c69d223fdd3020abdcef934c3de38 Mon Sep 17 00:00:00 2001 From: parveshneedhoo Date: Tue, 10 Sep 2024 10:38:14 +0400 Subject: [PATCH] refactor android upload duration logic --- src/android/FileTransferBackground.java | 32 ++++++++++++++----------- src/android/UploadEvent.java | 2 +- src/android/UploadTask.java | 6 ++--- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/android/FileTransferBackground.java b/src/android/FileTransferBackground.java index 434148e0..476e20bf 100644 --- a/src/android/FileTransferBackground.java +++ b/src/android/FileTransferBackground.java @@ -201,11 +201,8 @@ private void initManager(String options, final CallbackContext callbackContext) int ackDelay = 0; for (UploadEvent ack : uploadEvents) { - long startUploadTime = ack.getOutputData().getLong("output_upload_start_time", 0); - long endUploadTime = ack.getOutputData().getLong("output_upload_end_time", 0); - long uploadDuration = endUploadTime - startUploadTime; executorService.schedule(() -> { - handleAck(ack.getOutputData(), uploadDuration, endUploadTime); + handleAck(ack.getOutputData()); }, ackDelay, TimeUnit.MILLISECONDS); ackDelay += 200; } @@ -239,10 +236,7 @@ private void initManager(String options, final CallbackContext callbackContext) // The corresponding ACK is already in the DB, if it not, the task is just a leftover String id = info.getOutputData().getString(UploadTask.KEY_OUTPUT_ID); if (ackDatabase.uploadEventDao().exists(id)) { - long startUploadTime = info.getOutputData().getLong("output_upload_start_time", 0); - long endUploadTime = info.getOutputData().getLong("output_upload_end_time", 0); - long uploadDuration = endUploadTime - startUploadTime; - handleAck(info.getOutputData(), uploadDuration, endUploadTime); + handleAck(info.getOutputData()); } }, 0, TimeUnit.MILLISECONDS); break; @@ -415,7 +409,7 @@ private void acknowledgeEvent(String eventId, CallbackContext context) { /** * Handle ACK data and send it to the JS. */ - private void handleAck(final Data ackData, final long uploadDuration, final long finishUploadTime) { + private void handleAck(final Data ackData) { // If upload was successful if (!ackData.getBoolean(UploadTask.KEY_OUTPUT_IS_ERROR, false)) { // Read response from file if present @@ -424,12 +418,22 @@ private void handleAck(final Data ackData, final long uploadDuration, final long response = readFileToStringNoThrow(ackData.getString(UploadTask.KEY_OUTPUT_RESPONSE_FILE)); } + long startUploadTime = ackData.getLong("output_upload_start_time", 0); + long finishUploadTime = ackData.getLong("output_upload_finish_time", 0); + long uploadDuration = finishUploadTime - startUploadTime; + HashMap uploadData = new HashMap<>(); - uploadData.put("outputId", ackData.getString(UploadTask.KEY_OUTPUT_ID)); - uploadData.put("response", response); - uploadData.put("statusCode", ackData.getInt(UploadTask.KEY_OUTPUT_STATUS_CODE, -1)); - uploadData.put("uploadDuration", uploadDuration); - uploadData.put("finishUploadTime", finishUploadTime); + if (uploadDuration <= 0) { + uploadData.put("outputId", ackData.getString(UploadTask.KEY_OUTPUT_ID)); + uploadData.put("response", response); + uploadData.put("statusCode", ackData.getInt(UploadTask.KEY_OUTPUT_STATUS_CODE, -1)); + } else { + uploadData.put("outputId", ackData.getString(UploadTask.KEY_OUTPUT_ID)); + uploadData.put("response", response); + uploadData.put("statusCode", ackData.getInt(UploadTask.KEY_OUTPUT_STATUS_CODE, -1)); + uploadData.put("uploadDuration", uploadDuration); + uploadData.put("finishUploadTime", finishUploadTime); + } sendSuccess(uploadData); } else { diff --git a/src/android/UploadEvent.java b/src/android/UploadEvent.java index f5047681..33cf12f7 100644 --- a/src/android/UploadEvent.java +++ b/src/android/UploadEvent.java @@ -16,7 +16,7 @@ public class UploadEvent { @NonNull private Data outputData; - public UploadEvent(@NonNull final String id, @NonNull final Data outputData ) { + public UploadEvent(@NonNull final String id, @NonNull final Data outputData) { this.id = id; this.outputData = outputData; } diff --git a/src/android/UploadTask.java b/src/android/UploadTask.java index 2424ff7f..55fe02f4 100644 --- a/src/android/UploadTask.java +++ b/src/android/UploadTask.java @@ -80,7 +80,7 @@ public final class UploadTask extends Worker { public static final String KEY_OUTPUT_FAILURE_REASON = "output_failure_reason"; public static final String KEY_OUTPUT_FAILURE_CANCELED = "output_failure_canceled"; public static final String KEY_OUTPUT_UPLOAD_START_TIME = "output_upload_start_time"; - public static final String KEY_OUTPUT_UPLOAD_END_TIME = "output_upload_end_time"; + public static final String KEY_OUTPUT_UPLOAD_FINISH_TIME = "output_upload_finish_time"; // private static UploadNotification uploadNotification = null; @@ -242,7 +242,7 @@ public Result doWork() { .putString(KEY_OUTPUT_FAILURE_REASON, "User cancelled") .putBoolean(KEY_OUTPUT_FAILURE_CANCELED, true) .putLong(KEY_OUTPUT_UPLOAD_START_TIME, startTime) - .putLong(KEY_OUTPUT_UPLOAD_END_TIME, endTime) + .putLong(KEY_OUTPUT_UPLOAD_FINISH_TIME, endTime) .build(); AckDatabase.getInstance(getApplicationContext()).uploadEventDao().insert(new UploadEvent(id, data)); return Result.success(data); @@ -264,7 +264,7 @@ public Result doWork() { .putBoolean(KEY_OUTPUT_IS_ERROR, false) .putInt(KEY_OUTPUT_STATUS_CODE, (!DEBUG_SKIP_UPLOAD) ? response.code() : 200) .putLong(KEY_OUTPUT_UPLOAD_START_TIME, startTime) - .putLong(KEY_OUTPUT_UPLOAD_END_TIME, endTime); + .putLong(KEY_OUTPUT_UPLOAD_FINISH_TIME, endTime); // Try read the response body, if any try {