@@ -209,78 +209,16 @@ object UploadWorkerHelper {
209
209
file
210
210
)
211
211
)
212
- .addTag(" android-video-uploader" )
213
- .addTag(getTagForUpload(videoId, token))
212
+ .addTag(ARTIFACT_ID )
214
213
.apply {
214
+ videoId?.let { addTag(getTagForVideoId(it)) }
215
+ token?.let { addTag(getTagForUploadToken(it)) }
215
216
tags.forEach { addTag(it) }
216
217
}
217
218
.build()
218
219
return OperationWithRequest (workManager.enqueue(workRequest), workRequest)
219
220
}
220
221
221
- /* *
222
- * Cancels all works related to a video id that was added with [upload].
223
- * Works with upload token are not cancelled.
224
- *
225
- * @param context The application context
226
- * @param videoId The video id
227
- */
228
- @JvmStatic
229
- fun cancel (context : Context , videoId : String ) =
230
- cancel(WorkManager .getInstance(context), videoId)
231
-
232
- /* *
233
- * Cancels all works related to a video id that was added with [upload].
234
- * Works with upload token are not cancelled.
235
- *
236
- * @param workManager The WorkManager instance
237
- * @param videoId The video id
238
- */
239
- @JvmStatic
240
- fun cancel (workManager : WorkManager , videoId : String ) =
241
- workManager.cancelAllWorkByTag(getTagForUpload(videoId, null ))
242
-
243
- /* *
244
- * Cancels all works related to an upload token and possibly a video id that was added with [uploadWithUploadToken].
245
- * Works without upload token are not cancelled.
246
- *
247
- * @param context The application context
248
- * @param token The upload token
249
- * @param videoId The video id.Must be the same as the one used in [uploadWithUploadToken].
250
- */
251
- @JvmStatic
252
- fun cancelWithUploadToken (context : Context , token : String , videoId : String? = null) =
253
- cancelWithUploadToken(WorkManager .getInstance(context), token, videoId)
254
-
255
- /* *
256
- * Cancels all works related to an upload token and possibly a video id that was added with [uploadWithUploadToken].
257
- * Works without upload token are not cancelled.
258
- *
259
- * @param workManager The WorkManager instance
260
- * @param token The upload token
261
- * @param videoId The video id. Must be the same as the one used in [uploadWithUploadToken].
262
- */
263
- @JvmStatic
264
- fun cancelWithUploadToken (workManager : WorkManager , token : String , videoId : String? = null) =
265
- workManager.cancelAllWorkByTag(getTagForUpload(videoId, token))
266
-
267
- private const val PREFIX_VIDEO_ID = " videoId="
268
- private const val PREFIX_TOKEN = " token="
269
-
270
- /* *
271
- * Returns the tag used to identify works related to a video id or an upload token.
272
- *
273
- * @param videoId The video id
274
- * @param token The upload token
275
- * @return The tag
276
- */
277
- fun getTagForUpload (videoId : String? , token : String? ): String {
278
- require((token != null ) || (videoId != null )) {
279
- " You must provide either a token or a videoId"
280
- }
281
- return " ($PREFIX_VIDEO_ID$videoId , $PREFIX_TOKEN$token )"
282
- }
283
-
284
222
/* *
285
223
* Enqueues a work to upload a part of a file.
286
224
*
@@ -404,13 +342,96 @@ object UploadWorkerHelper {
404
342
partId
405
343
)
406
344
)
407
- .addTag(" android-video-uploader " )
345
+ .addTag(ARTIFACT_ID )
408
346
.addTag(" progressive" )
409
- .addTag(getTagForUpload(session.videoId, session.token))
410
347
.apply {
348
+ session.videoId?.let { addTag(getTagForVideoId(it)) }
349
+ session.token?.let { addTag(getTagForUploadToken(it)) }
411
350
tags.forEach { addTag(it) }
412
351
}
413
352
.build()
414
353
return OperationWithRequest (workManager.enqueue(workRequest), workRequest)
415
354
}
355
+
356
+ /* *
357
+ * Cancels all upload works.
358
+ *
359
+ * @param context The application context
360
+ */
361
+ @JvmStatic
362
+ fun cancelAll (context : Context ) =
363
+ cancelAll(WorkManager .getInstance(context))
364
+
365
+ /* *
366
+ * Cancels all upload works.
367
+ *
368
+ * @param workManager The WorkManager instance
369
+ */
370
+ @JvmStatic
371
+ fun cancelAll (workManager : WorkManager ) =
372
+ workManager.cancelAllWorkByTag(ARTIFACT_ID )
373
+
374
+ /* *
375
+ * Cancels all works related to a video id.
376
+ *
377
+ * @param context The application context
378
+ * @param videoId The video id
379
+ */
380
+ @JvmStatic
381
+ fun cancel (context : Context , videoId : String ) =
382
+ cancel(WorkManager .getInstance(context), videoId)
383
+
384
+ /* *
385
+ * Cancels all works related to a video id.
386
+ *
387
+ * @param workManager The WorkManager instance
388
+ * @param videoId The video id
389
+ */
390
+ @JvmStatic
391
+ fun cancel (workManager : WorkManager , videoId : String ) =
392
+ workManager.cancelAllWorkByTag(getTagForVideoId(videoId))
393
+
394
+ /* *
395
+ * Cancels all works related to an upload token that was added with [uploadWithUploadToken].
396
+ *
397
+ * @param context The application context
398
+ * @param token The upload token
399
+ */
400
+ @JvmStatic
401
+ fun cancelWithUploadToken (context : Context , token : String ) =
402
+ cancelWithUploadToken(WorkManager .getInstance(context), token)
403
+
404
+ /* *
405
+ * Cancels all works related to an upload token that was added with [uploadWithUploadToken].
406
+ *
407
+ * @param workManager The WorkManager instance
408
+ * @param token The upload token
409
+ */
410
+ @JvmStatic
411
+ fun cancelWithUploadToken (workManager : WorkManager , token : String ) =
412
+ workManager.cancelAllWorkByTag(getTagForUploadToken(token))
413
+
414
+ /* *
415
+ * Returns the tag used to identify works related to a video id.
416
+ *
417
+ * @param videoId The video id
418
+ * @return The tag
419
+ */
420
+ fun getTagForVideoId (videoId : String ): String {
421
+ return " ($PREFIX_VIDEO_ID$videoId )"
422
+ }
423
+
424
+ /* *
425
+ * Returns the tag used to identify works related to an upload token.
426
+ *
427
+ * @param token The upload token
428
+ * @return The tag
429
+ */
430
+ fun getTagForUploadToken (token : String ): String {
431
+ return " ($PREFIX_TOKEN$token )"
432
+ }
433
+
434
+ private const val PREFIX_VIDEO_ID = " videoId="
435
+ private const val PREFIX_TOKEN = " token="
436
+ private const val ARTIFACT_ID = " android-video-uploader"
416
437
}
0 commit comments