Skip to content

Commit

Permalink
refactor android upload duration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
parveshneedhoo committed Sep 10, 2024
1 parent c8cb416 commit 50e40f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
32 changes: 18 additions & 14 deletions src/android/FileTransferBackground.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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<String, Object> 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/android/UploadEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/android/UploadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
// </editor-fold>

private static UploadNotification uploadNotification = null;
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down

0 comments on commit 50e40f8

Please sign in to comment.