From 6c71c7edec6a03d98a8e712ff97dc5355cfa6595 Mon Sep 17 00:00:00 2001 From: parveshneedhoo Date: Mon, 2 Sep 2024 17:58:53 +0400 Subject: [PATCH] refactor --- src/android/AckDatabase.java | 19 +++------------ src/android/FileTransferBackground.java | 32 +++++++++++++++++-------- src/android/UploadEvent.java | 24 +------------------ src/android/UploadTask.java | 12 +++++++--- 4 files changed, 35 insertions(+), 52 deletions(-) diff --git a/src/android/AckDatabase.java b/src/android/AckDatabase.java index 6403cbf3..24e5def6 100644 --- a/src/android/AckDatabase.java +++ b/src/android/AckDatabase.java @@ -2,35 +2,22 @@ import android.content.Context; -import androidx.annotation.NonNull; import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; import androidx.room.TypeConverters; -import androidx.room.migration.Migration; -import androidx.sqlite.db.SupportSQLiteDatabase; import androidx.work.Data; -@Database(entities = {UploadEvent.class}, version = 6) +@Database(entities = {UploadEvent.class}, version = 5) @TypeConverters(value = {Data.class}) public abstract class AckDatabase extends RoomDatabase { private static AckDatabase instance; - static final Migration MIGRATION_5_6 = new Migration(5, 6) { - @Override - public void migrate(@NonNull SupportSQLiteDatabase database) { - database.execSQL("ALTER TABLE upload_events ADD COLUMN start_upload_time INTEGER NOT NULL DEFAULT 0"); - database.execSQL("ALTER TABLE upload_events ADD COLUMN finish_upload_time INTEGER NOT NULL DEFAULT 0"); - } - }; - - - public static synchronized AckDatabase getInstance(final Context context) { + public static AckDatabase getInstance(final Context context) { if (instance == null) { instance = Room .databaseBuilder(context, AckDatabase.class, "cordova-plugin-background-upload.db") .fallbackToDestructiveMigration() - .addMigrations(MIGRATION_5_6) .build(); } return instance; @@ -43,4 +30,4 @@ public static void closeInstance() { } public abstract UploadEventDao uploadEventDao(); -} +} \ No newline at end of file diff --git a/src/android/FileTransferBackground.java b/src/android/FileTransferBackground.java index 37cde16d..434148e0 100644 --- a/src/android/FileTransferBackground.java +++ b/src/android/FileTransferBackground.java @@ -82,7 +82,13 @@ private void sendProgress(final String id, int progressPercent) { } } - private void sendSuccess(final String id, final String response, int statusCode, long uploadDuration, long finishUploadTime) { + private void sendSuccess(HashMap uploadData) { + String id = (String) uploadData.get("outputId"); + String response = (String) uploadData.get("response"); + int statusCode = (int) uploadData.get("statusCode"); + long uploadDuration = (long) uploadData.get("uploadDuration"); + long finishUploadTime = (long) uploadData.get("finishUploadTime"); + if (response != null && !response.isEmpty()) { logMessage("eventLabel='Uploader onSuccess' uploadId='" + id + "' response='" + response.substring(0, Math.min(2000, response.length() - 1)) + "'"); } else { @@ -195,8 +201,11 @@ 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(), ack.calculateUploadDuration(), ack.getFinishUploadTime()); + handleAck(ack.getOutputData(), uploadDuration, endUploadTime); }, ackDelay, TimeUnit.MILLISECONDS); ackDelay += 200; } @@ -230,7 +239,10 @@ 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)) { - handleAck(info.getOutputData(), ackDatabase.uploadEventDao().getById(id).calculateUploadDuration(), ackDatabase.uploadEventDao().getById(id).getFinishUploadTime()); + 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); } }, 0, TimeUnit.MILLISECONDS); break; @@ -412,14 +424,14 @@ private void handleAck(final Data ackData, final long uploadDuration, final long response = readFileToStringNoThrow(ackData.getString(UploadTask.KEY_OUTPUT_RESPONSE_FILE)); } - sendSuccess( - ackData.getString(UploadTask.KEY_OUTPUT_ID), - response, - ackData.getInt(UploadTask.KEY_OUTPUT_STATUS_CODE, -1 /* If this is sent, something is really wrong */), - uploadDuration, - finishUploadTime - ); + 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); + sendSuccess(uploadData); } else { // The upload was a failure sendError( diff --git a/src/android/UploadEvent.java b/src/android/UploadEvent.java index 691d8995..f5047681 100644 --- a/src/android/UploadEvent.java +++ b/src/android/UploadEvent.java @@ -16,19 +16,9 @@ public class UploadEvent { @NonNull private Data outputData; - @ColumnInfo(name = "start_upload_time") - @NonNull - private long startUploadTime; - - @ColumnInfo(name = "finish_upload_time") - @NonNull - private long finishUploadTime; - - public UploadEvent(@NonNull final String id, @NonNull final Data outputData, @NonNull final long startUploadTime, @NonNull final long finishUploadTime) { + public UploadEvent(@NonNull final String id, @NonNull final Data outputData ) { this.id = id; this.outputData = outputData; - this.startUploadTime = startUploadTime; - this.finishUploadTime = finishUploadTime; } @NonNull @@ -40,16 +30,4 @@ public String getId() { public Data getOutputData() { return outputData; } - - public long getStartUploadTime() { - return startUploadTime; - } - - public long getFinishUploadTime() { - return finishUploadTime; - } - - public long calculateUploadDuration() { - return finishUploadTime - startUploadTime; - } } diff --git a/src/android/UploadTask.java b/src/android/UploadTask.java index 08c3368c..2424ff7f 100644 --- a/src/android/UploadTask.java +++ b/src/android/UploadTask.java @@ -79,6 +79,8 @@ public final class UploadTask extends Worker { public static final String KEY_OUTPUT_STATUS_CODE = "output_status_code"; 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"; // private static UploadNotification uploadNotification = null; @@ -239,8 +241,10 @@ public Result doWork() { .putBoolean(KEY_OUTPUT_IS_ERROR, true) .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) .build(); - AckDatabase.getInstance(getApplicationContext()).uploadEventDao().insert(new UploadEvent(id, data, startTime, endTime)); + AckDatabase.getInstance(getApplicationContext()).uploadEventDao().insert(new UploadEvent(id, data)); return Result.success(data); } else { // But if it was not it must be a connectivity problem or @@ -258,7 +262,9 @@ public Result doWork() { final Data.Builder outputData = new Data.Builder() .putString(KEY_OUTPUT_ID, id) .putBoolean(KEY_OUTPUT_IS_ERROR, false) - .putInt(KEY_OUTPUT_STATUS_CODE, (!DEBUG_SKIP_UPLOAD) ? response.code() : 200); + .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); // Try read the response body, if any try { @@ -285,7 +291,7 @@ public Result doWork() { } final Data data = outputData.build(); - AckDatabase.getInstance(getApplicationContext()).uploadEventDao().insert(new UploadEvent(id, data, startTime, endTime)); + AckDatabase.getInstance(getApplicationContext()).uploadEventDao().insert(new UploadEvent(id, data)); return Result.success(data); }