Skip to content

Commit 6e35852

Browse files
android upload duration logic
1 parent 8fb2cf8 commit 6e35852

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

src/android/AckDatabase.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@
22

33
import android.content.Context;
44

5+
import androidx.annotation.NonNull;
56
import androidx.room.Database;
67
import androidx.room.Room;
78
import androidx.room.RoomDatabase;
89
import androidx.room.TypeConverters;
10+
import androidx.room.migration.Migration;
11+
import androidx.sqlite.db.SupportSQLiteDatabase;
912
import androidx.work.Data;
1013

11-
@Database(entities = {UploadEvent.class}, version = 5)
14+
@Database(entities = {UploadEvent.class}, version = 6)
1215
@TypeConverters(value = {Data.class})
1316
public abstract class AckDatabase extends RoomDatabase {
1417
private static AckDatabase instance;
1518

16-
public static AckDatabase getInstance(final Context context) {
19+
static final Migration MIGRATION_5_6 = new Migration(5, 6) {
20+
@Override
21+
public void migrate(@NonNull SupportSQLiteDatabase database) {
22+
// Add the new column to the existing table
23+
database.execSQL("ALTER TABLE upload_events ADD COLUMN uploadDuration INTEGER NOT NULL DEFAULT 0");
24+
}
25+
};
26+
27+
public static synchronized AckDatabase getInstance(final Context context) {
1728
if (instance == null) {
1829
instance = Room
1930
.databaseBuilder(context, AckDatabase.class, "cordova-plugin-background-upload.db")
2031
.fallbackToDestructiveMigration()
32+
.addMigrations(MIGRATION_5_6)
2133
.build();
2234
}
2335
return instance;

src/android/FileTransferBackground.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private void sendProgress(final String id, int progressPercent) {
8181
}
8282
}
8383

84-
private void sendSuccess(final String id, final String response, int statusCode) {
84+
private void sendSuccess(final String id, final String response, int statusCode, long uploadDuration) {
8585
if (response != null && !response.isEmpty()) {
8686
logMessage("eventLabel='Uploader onSuccess' uploadId='" + id + "' response='" + response.substring(0, Math.min(2000, response.length() - 1)) + "'");
8787
} else {
@@ -95,6 +95,7 @@ private void sendSuccess(final String id, final String response, int statusCode)
9595
.put("state", "UPLOADED")
9696
.put("serverResponse", response)
9797
.put("statusCode", statusCode)
98+
.put("uploadDuration", uploadDuration)
9899
);
99100
} catch (JSONException e) {
100101
// Can't really happen but just in case
@@ -412,7 +413,8 @@ private void handleAck(final Data ackData) {
412413
sendSuccess(
413414
ackData.getString(UploadTask.KEY_OUTPUT_ID),
414415
response,
415-
ackData.getInt(UploadTask.KEY_OUTPUT_STATUS_CODE, -1 /* If this is sent, something is really wrong */)
416+
ackData.getInt(UploadTask.KEY_OUTPUT_STATUS_CODE, -1 /* If this is sent, something is really wrong */),
417+
ackData.getLong(UploadTask.KEY_OUTPUT_UPLOAD_DURATION, 0)
416418
);
417419

418420
} else {

src/android/UploadEvent.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class UploadEvent {
1111
@PrimaryKey
1212
@NonNull
1313
private String id;
14+
private long uploadDuration;
1415

1516
@ColumnInfo(name = "output_data")
1617
@NonNull
@@ -19,6 +20,7 @@ public class UploadEvent {
1920
public UploadEvent(@NonNull final String id, @NonNull final Data outputData) {
2021
this.id = id;
2122
this.outputData = outputData;
23+
this.uploadDuration = outputData.getLong(UploadTask.KEY_OUTPUT_UPLOAD_DURATION, 0);
2224
}
2325

2426
@NonNull
@@ -30,4 +32,12 @@ public String getId() {
3032
public Data getOutputData() {
3133
return outputData;
3234
}
35+
36+
public long getUploadDuration() {
37+
return uploadDuration;
38+
}
39+
40+
public void setUploadDuration(long uploadDuration) {
41+
this.uploadDuration = uploadDuration;
42+
}
3343
}

src/android/UploadEventDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import androidx.room.Insert;
66
import androidx.room.OnConflictStrategy;
77
import androidx.room.Query;
8+
import androidx.room.Update;
89

910
import java.util.List;
1011

@@ -32,4 +33,7 @@ default void delete(final String id) {
3233
delete(ack);
3334
}
3435
}
36+
37+
@Update
38+
void update(UploadEvent uploadEvent);
3539
}

src/android/UploadTask.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public final class UploadTask extends Worker {
7979
public static final String KEY_OUTPUT_STATUS_CODE = "output_status_code";
8080
public static final String KEY_OUTPUT_FAILURE_REASON = "output_failure_reason";
8181
public static final String KEY_OUTPUT_FAILURE_CANCELED = "output_failure_canceled";
82+
public static final String KEY_OUTPUT_UPLOAD_DURATION = "output_upload_duration";
83+
8284
// </editor-fold>
8385

8486
private static UploadNotification uploadNotification = null;
@@ -96,6 +98,9 @@ public void release() { }
9698
private static int concurrency = 1;
9799
private static Semaphore concurrentUploads = new Semaphore(concurrency, true);
98100
private static Mutex concurrencyLock = new Mutex();
101+
private long startUploadTime;
102+
private long endUploadTime;
103+
private long uploadDuration;
99104

100105
public UploadTask(@NonNull Context context, @NonNull WorkerParameters workerParams) {
101106

@@ -189,6 +194,8 @@ public Result doWork() {
189194
return Result.retry();
190195
}
191196

197+
startUploadTime = System.currentTimeMillis();
198+
192199
// Register me
193200
uploadForegroundNotification.progress(getId(), 0f);
194201
handleNotification();
@@ -246,6 +253,8 @@ public Result doWork() {
246253
return Result.retry();
247254
}
248255
} finally {
256+
endUploadTime = System.currentTimeMillis();
257+
uploadDuration = endUploadTime - startUploadTime;
249258
// Always remove ourselves from the notification
250259
uploadForegroundNotification.done(getId());
251260
}
@@ -254,7 +263,8 @@ public Result doWork() {
254263
final Data.Builder outputData = new Data.Builder()
255264
.putString(KEY_OUTPUT_ID, id)
256265
.putBoolean(KEY_OUTPUT_IS_ERROR, false)
257-
.putInt(KEY_OUTPUT_STATUS_CODE, (!DEBUG_SKIP_UPLOAD) ? response.code() : 200);
266+
.putInt(KEY_OUTPUT_STATUS_CODE, (!DEBUG_SKIP_UPLOAD) ? response.code() : 200)
267+
.putLong(KEY_OUTPUT_UPLOAD_DURATION, uploadDuration);
258268

259269
// Try read the response body, if any
260270
try {

0 commit comments

Comments
 (0)