-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathUploadTask.java
More file actions
433 lines (371 loc) · 18 KB
/
UploadTask.java
File metadata and controls
433 lines (371 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package com.spoon.backgroundfileupload;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Build;
import android.util.Log;
import android.webkit.MimeTypeMap;
import androidx.annotation.NonNull;
import androidx.work.ForegroundInfo;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLException;
import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public final class UploadTask extends Worker {
private static final boolean DEBUG_SKIP_UPLOAD = false;
public static final long DELAY_BETWEEN_NOTIFICATION_UPDATE_MS = 200;
public static final String TAG = "CordovaBackgroundUpload";
public static final String NOTIFICATION_CHANNEL_ID = "com.spoon.backgroundfileupload.channel";
public static final String NOTIFICATION_CHANNEL_NAME = "upload channel";
public static final int MAX_TRIES = 10;
// Key stuff
// <editor-fold>
// Keys used in the input data
public static final String KEY_INPUT_ID = "input_id";
public static final String KEY_INPUT_URL = "input_url";
public static final String KEY_INPUT_FILEPATH = "input_filepath";
public static final String KEY_INPUT_FILE_KEY = "input_file_key";
public static final String KEY_INPUT_HTTP_METHOD = "input_http_method";
public static final String KEY_INPUT_HEADERS_COUNT = "input_headers_count";
public static final String KEY_INPUT_HEADERS_NAMES = "input_headers_names";
public static final String KEY_INPUT_HEADER_VALUE_PREFIX = "input_header_";
public static final String KEY_INPUT_PARAMETERS_COUNT = "input_parameters_count";
public static final String KEY_INPUT_PARAMETERS_NAMES = "input_parameters_names";
public static final String KEY_INPUT_PARAMETER_VALUE_PREFIX = "input_parameter_";
public static final String KEY_INPUT_NOTIFICATION_TITLE = "input_notification_title";
public static final String KEY_INPUT_NOTIFICATION_ICON = "input_notification_icon";
// Input keys but used for configuring the OkHttp instance
public static final String KEY_INPUT_CONFIG_CONCURRENT_DOWNLOADS = "input_config_concurrent_downloads";
public static final String KEY_INPUT_CONFIG_INTENT_ACTIVITY = "input_config_intent_activity";
// Keys used for the progress data
public static final String KEY_PROGRESS_ID = "progress_id";
public static final String KEY_PROGRESS_PERCENT = "progress_percent";
// Keys used for the result
public static final String KEY_OUTPUT_ID = "output_id";
public static final String KEY_OUTPUT_IS_ERROR = "output_is_error";
public static final String KEY_OUTPUT_RESPONSE_FILE = "output_response";
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_FINISH_TIME = "output_upload_finish_time";
// </editor-fold>
private static UploadNotification uploadNotification = null;
private static UploadForegroundNotification uploadForegroundNotification = null;
public static class Mutex {
public void acquire() throws InterruptedException { }
public void release() { }
}
private static OkHttpClient httpClient;
private Call currentCall;
private static int concurrency = 1;
private static Semaphore concurrentUploads = new Semaphore(concurrency, true);
private static Mutex concurrencyLock = new Mutex();
long startTime = 0;
long endTime = 0;
public UploadTask(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
int concurrencyConfig = workerParams.getInputData().getInt(KEY_INPUT_CONFIG_CONCURRENT_DOWNLOADS, 1);
try {
concurrencyLock.acquire();
try {
if (concurrency != concurrencyConfig) {
concurrency = concurrencyConfig;
concurrentUploads = new Semaphore(concurrencyConfig, true);
}
} finally {
concurrencyLock.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (httpClient == null) {
httpClient = new OkHttpClient.Builder()
.followRedirects(true)
.followSslRedirects(true)
.retryOnConnectionFailure(true)
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.cache(null)
.build();
}
httpClient.dispatcher().setMaxRequests(workerParams.getInputData().getInt(KEY_INPUT_CONFIG_CONCURRENT_DOWNLOADS, 2));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
UploadForegroundNotification.configure(
workerParams.getInputData().getString(UploadTask.KEY_INPUT_NOTIFICATION_TITLE),
getApplicationContext().getResources().getIdentifier(workerParams.getInputData().getString(KEY_INPUT_NOTIFICATION_ICON), null, null),
workerParams.getInputData().getString(UploadTask.KEY_INPUT_CONFIG_INTENT_ACTIVITY)
);
uploadForegroundNotification = new UploadForegroundNotification();
} else {
UploadNotification.configure(
workerParams.getInputData().getString(UploadTask.KEY_INPUT_NOTIFICATION_TITLE),
getApplicationContext().getResources().getIdentifier(workerParams.getInputData().getString(KEY_INPUT_NOTIFICATION_ICON), null, null),
workerParams.getInputData().getString(UploadTask.KEY_INPUT_CONFIG_INTENT_ACTIVITY)
);
uploadNotification = new UploadNotification(getApplicationContext());
}
}
@NonNull
@Override
public Result doWork() {
if (!hasNetworkConnection()) {
return Result.retry();
}
final String id = getInputData().getString(KEY_INPUT_ID);
if (id == null) {
Log.e(TAG, "doWork: ID is invalid !");
return Result.failure();
}
// Check retry count
if (getRunAttemptCount() > MAX_TRIES) {
return Result.success(new Data.Builder()
.putString(KEY_OUTPUT_ID, id)
.putBoolean(KEY_OUTPUT_IS_ERROR, true)
.putString(KEY_OUTPUT_FAILURE_REASON, "Too many retries")
.putBoolean(KEY_OUTPUT_FAILURE_CANCELED, false)
.build()
);
}
Request request = null;
try {
request = createRequest();
} catch (FileNotFoundException e) {
Log.e(TAG, "doWork: File not found !", e);
return Result.success(new Data.Builder()
.putString(KEY_OUTPUT_ID, id)
.putBoolean(KEY_OUTPUT_IS_ERROR, true)
.putString(KEY_OUTPUT_FAILURE_REASON, "File not found !")
.putBoolean(KEY_OUTPUT_FAILURE_CANCELED, false)
.build()
);
} catch (NullPointerException e) {
return Result.retry();
}
startTime = System.currentTimeMillis();
// Register me and start foreground service IMMEDIATELY
// This must be done before any blocking operations to ensure uploads continue when app is killed
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
uploadForegroundNotification.progress(getId(), 0f);
setForegroundAsync(uploadForegroundNotification.getForegroundInfo(getApplicationContext()));
} else {
// Android 12+: Still need to start foreground service for WorkManager to continue after app kill
uploadNotification.updateProgress();
setForegroundAsync(uploadNotification.getForegroundInfo());
}
// Start call
currentCall = httpClient.newCall(request);
// Block until call is finished (or cancelled)
Response response = null;
try {
if (!DEBUG_SKIP_UPLOAD) {
try {
try {
concurrentUploads.acquire();
try {
response = currentCall.execute();
} catch (SocketTimeoutException e) {
return Result.retry();
} finally {
concurrentUploads.release();
}
} catch (InterruptedException e) {
return Result.retry();
}
} catch (SocketException | ProtocolException | SSLException e) {
currentCall.cancel();
return Result.retry();
}
} else {
for (int i = 0; i < 10; i++) {
handleProgress(i * 100, 1000);
// Can be interrupted
Thread.sleep(200);
if (isStopped()) {
throw new InterruptedException("Stopped");
}
}
}
} catch (IOException | InterruptedException e) {
// If it was user cancelled its ok
// See #handleProgress for cancel code
if (isStopped()) {
final Data data = new Data.Builder()
.putString(KEY_OUTPUT_ID, id)
.putBoolean(KEY_OUTPUT_IS_ERROR, true)
.putString(KEY_OUTPUT_FAILURE_REASON, "User cancelled")
.putBoolean(KEY_OUTPUT_FAILURE_CANCELED, true)
.build();
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
// something similar so we retry later
Log.e(TAG, "doWork: Call failed, retrying later", e);
return Result.retry();
}
} finally {
endTime = System.currentTimeMillis();
// Always remove ourselves from the notification
uploadForegroundNotification.done(getId());
}
// Start building the output data
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)
.putLong(KEY_OUTPUT_UPLOAD_START_TIME, startTime)
.putLong(KEY_OUTPUT_UPLOAD_FINISH_TIME, endTime);
// Try read the response body, if any
try {
final String res;
if (!DEBUG_SKIP_UPLOAD) {
res = response.body() != null ? response.body().string() : "";
} else {
res = "<span>heyo</span>";
}
final String filename = "upload-response-" + getId() + ".cached-response";
try (FileOutputStream fos = getApplicationContext().openFileOutput(filename, Context.MODE_PRIVATE)) {
fos.write(res.getBytes(StandardCharsets.UTF_8));
}
outputData.putString(KEY_OUTPUT_RESPONSE_FILE, filename);
} catch (IOException e) {
// Should never happen, but if it does it has something to do with reading the response
Log.e(TAG, "doWork: Error while reading the response body", e);
// But recover and replace the body with something else
outputData.putString(KEY_OUTPUT_RESPONSE_FILE, null);
}
final Data data = outputData.build();
AckDatabase.getInstance(getApplicationContext()).uploadEventDao().insert(new UploadEvent(id, data));
return Result.success(data);
}
/**
* Called internally by the custom request body provider each time 8kio are written.
*/
private void handleProgress(long bytesWritten, long totalBytes) {
// The cancel mechanism is best-effort and wont actually halt work, we need to
// take care of it ourselves.
if (isStopped()) {
currentCall.cancel();
return;
}
float percent = (float) bytesWritten / (float) totalBytes;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
UploadForegroundNotification.progress(getId(), percent);
}
// On Android 12+, progress is tracked via WorkManager progress data
Log.i(TAG, "handleProgress: " + getId() + " Progress: " + (int) (percent * 100f));
final Data data = new Data.Builder()
.putString(KEY_PROGRESS_ID, getInputData().getString(KEY_INPUT_ID))
.putInt(KEY_PROGRESS_PERCENT, (int) (percent * 100f))
.build();
Log.d(TAG, "handleProgress: Progress data: " + data);
setProgressAsync(data);
handleNotification();
}
/**
* Create the OkHttp request that will be used, already filled with input data.
*
* @return A ready to use OkHttp request
* @throws FileNotFoundException If the file to upload can't be found
*/
@NonNull
private Request createRequest() throws FileNotFoundException {
final String filepath = getInputData().getString(KEY_INPUT_FILEPATH);
assert filepath != null;
final String fileKey = getInputData().getString(KEY_INPUT_FILE_KEY);
assert fileKey != null;
// Build URL
HttpUrl url = Objects.requireNonNull(HttpUrl.parse(getInputData().getString(KEY_INPUT_URL))).newBuilder().build();
// Build file reader
String extension = MimeTypeMap.getFileExtensionFromUrl(filepath);
MediaType mediaType;
if (extension.equals("json") || extension.equals("db")) {
// Does not support devices less than Android 10 (Stop Execution)
// https://stackoverflow.com/questions/44667125/getmimetypefromextension-returns-null-when-i-pass-json-as-extension
mediaType = MediaType.parse(MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) + "; charset=utf-8");
} else {
mediaType = MediaType.parse(MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension));
}
File file = new File(filepath);
ProgressRequestBody fileRequestBody = new ProgressRequestBody(mediaType, file.length(), new FileInputStream(file), this::handleProgress);
// Build body
final MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
// With the parameters
final int parametersCount = getInputData().getInt(KEY_INPUT_PARAMETERS_COUNT, 0);
if (parametersCount > 0) {
final String[] parameterNames = getInputData().getStringArray(KEY_INPUT_PARAMETERS_NAMES);
assert parameterNames != null;
for (int i = 0; i < parametersCount; i++) {
final String key = parameterNames[i];
final Object value = getInputData().getKeyValueMap().get(KEY_INPUT_PARAMETER_VALUE_PREFIX + i);
bodyBuilder.addFormDataPart(key, value.toString());
}
}
bodyBuilder.addFormDataPart(fileKey, filepath, fileRequestBody);
bodyBuilder.setType(MultipartBody.FORM);
// Start build request
String method = getInputData().getString(KEY_INPUT_HTTP_METHOD);
if (method == null) {
method = "POST";
}
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.method(method.toUpperCase(), bodyBuilder.build());
// Write headers
final int headersCount = getInputData().getInt(KEY_INPUT_HEADERS_COUNT, 0);
final String[] headerNames = getInputData().getStringArray(KEY_INPUT_HEADERS_NAMES);
assert headerNames != null;
for (int i = 0; i < headersCount; i++) {
final String key = headerNames[i];
final Object value = getInputData().getKeyValueMap().get(KEY_INPUT_HEADER_VALUE_PREFIX + i);
requestBuilder.addHeader(key, value.toString());
}
// Ok
return requestBuilder.build();
}
private void handleNotification() {
Log.d(TAG, "Upload Notification");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
setForegroundAsync(uploadForegroundNotification.getForegroundInfo(getApplicationContext()));
} else {
uploadNotification.updateProgress();
}
Log.d(TAG, "Upload Notification Exit");
}
@NonNull
@Override
public ForegroundInfo getForegroundInfo() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
return uploadForegroundNotification.getForegroundInfo(getApplicationContext());
}
return uploadNotification.getForegroundInfo();
}
private synchronized boolean hasNetworkConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if((connectivityManager == null) || (connectivityManager.getActiveNetworkInfo() == null) || (connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting() == false)) {
Log.d(TAG, "No internet connection");
return false;
}
return true;
}
}