Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
parveshneedhoo committed Sep 2, 2024
1 parent 9ac5df9 commit 6c71c7e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 52 deletions.
19 changes: 3 additions & 16 deletions src/android/AckDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,4 +30,4 @@ public static void closeInstance() {
}

public abstract UploadEventDao uploadEventDao();
}
}
32 changes: 22 additions & 10 deletions src/android/FileTransferBackground.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<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);

sendSuccess(uploadData);
} else {
// The upload was a failure
sendError(
Expand Down
24 changes: 1 addition & 23 deletions src/android/UploadEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
12 changes: 9 additions & 3 deletions src/android/UploadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
// </editor-fold>

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

Expand Down

0 comments on commit 6c71c7e

Please sign in to comment.