Skip to content

Commit 313df7e

Browse files
authored
feat(java): improve cancellation of WorkManager uploads
1 parent a828193 commit 313df7e

File tree

8 files changed

+119
-91
lines changed

8 files changed

+119
-91
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22
All changes to this project will be documented in this file.
33

4+
## [1.3.0] - 2023-08-21
5+
- Improve cancel of upload workers for the WorkManager API
6+
47
## [1.2.4] - 2023-08-10
58
- Fix upload with upload token and video id when video is smaller than chunk size
69

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Add this dependency to your project's POM:
4949
<dependency>
5050
<groupId>video.api</groupId>
5151
<artifactId>android-video-uploader</artifactId>
52-
<version>1.2.4</version>
52+
<version>1.3.0</version>
5353
<scope>compile</scope>
5454
</dependency>
5555
```
@@ -59,7 +59,7 @@ Add this dependency to your project's POM:
5959
Add this dependency to your project's build file:
6060

6161
```groovy
62-
implementation "video.api:android-video-uploader:1.2.4"
62+
implementation "video.api:android-video-uploader:1.3.0"
6363
```
6464

6565
### Others
@@ -72,7 +72,7 @@ mvn clean package
7272

7373
Then manually install the following JARs:
7474

75-
* `target/android-video-uploader-1.2.4.jar`
75+
* `target/android-video-uploader-1.3.0.jar`
7676
* `target/lib/*.jar`
7777

7878
## Code sample

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: 'maven-publish'
55
apply plugin: 'kotlin-android'
66

77
group = 'video.api'
8-
version = '1.2.4'
8+
version = '1.3.0'
99

1010
buildscript {
1111
repositories {

maven-push.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ apply plugin: 'maven-publish'
1818
apply plugin: 'signing'
1919

2020
def isReleaseBuild() {
21-
return !"1.2.4".contains("SNAPSHOT")
21+
return !"1.3.0".contains("SNAPSHOT")
2222
}
2323

2424
def getReleaseRepositoryUrl() {
@@ -47,7 +47,7 @@ afterEvaluate { project ->
4747

4848
groupId = "video.api"
4949
artifactId = "android-video-uploader"
50-
version = "1.2.4"
50+
version = "1.3.0"
5151

5252
pom {
5353
name = "video.api:android-video-uploader"

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<artifactId>android-video-uploader</artifactId>
66
<packaging>jar</packaging>
77
<name>${project.groupId}:${project.artifactId}</name>
8-
<version>1.2.4</version>
8+
<version>1.3.0</version>
99
<url>https://github.com/apivideo/api.video-android-uploader</url>
1010
<description>api.video Android API video uploader</description>
1111
<scm>

src/main/java/video/api/uploader/api/ApiClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private OkHttpClient initHttpClient(List<Interceptor> interceptors) {
118118
private void init() {
119119
verifyingSsl = true;
120120
json = new JSON();
121-
addDefaultHeader("AV-Origin-Client", "android-uploader:1.2.4");
121+
addDefaultHeader("AV-Origin-Client", "android-uploader:1.3.0");
122122
}
123123

124124
private boolean isValid(String regex, String field) {

src/main/java/video/api/uploader/api/work/UploadWorkerHelper.kt

Lines changed: 88 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -209,78 +209,16 @@ object UploadWorkerHelper {
209209
file
210210
)
211211
)
212-
.addTag("android-video-uploader")
213-
.addTag(getTagForUpload(videoId, token))
212+
.addTag(ARTIFACT_ID)
214213
.apply {
214+
videoId?.let { addTag(getTagForVideoId(it)) }
215+
token?.let { addTag(getTagForUploadToken(it)) }
215216
tags.forEach { addTag(it) }
216217
}
217218
.build()
218219
return OperationWithRequest(workManager.enqueue(workRequest), workRequest)
219220
}
220221

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-
284222
/**
285223
* Enqueues a work to upload a part of a file.
286224
*
@@ -404,13 +342,96 @@ object UploadWorkerHelper {
404342
partId
405343
)
406344
)
407-
.addTag("android-video-uploader")
345+
.addTag(ARTIFACT_ID)
408346
.addTag("progressive")
409-
.addTag(getTagForUpload(session.videoId, session.token))
410347
.apply {
348+
session.videoId?.let { addTag(getTagForVideoId(it)) }
349+
session.token?.let { addTag(getTagForUploadToken(it)) }
411350
tags.forEach { addTag(it) }
412351
}
413352
.build()
414353
return OperationWithRequest(workManager.enqueue(workRequest), workRequest)
415354
}
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"
416437
}

src/main/java/video/api/uploader/api/work/WorkManagerExtensions.kt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,6 @@ fun WorkManager.uploadWithUploadToken(
8484
workerClass
8585
)
8686

87-
/**
88-
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.upload].
89-
*
90-
* @param videoId The video id
91-
*/
92-
fun WorkManager.cancel(videoId: String) = UploadWorkerHelper.cancel(this, videoId)
93-
94-
/**
95-
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.uploadWithUploadToken].
96-
*
97-
* @param token The upload token
98-
* @param videoId The video id. Must be the same as the one used in [WorkManager.uploadWithUploadToken].
99-
*/
100-
fun WorkManager.cancelWithUploadToken(token: String, videoId: String? = null) =
101-
UploadWorkerHelper.cancelWithUploadToken(this, token, videoId)
102-
10387
/**
10488
* Extension functions for [WorkManager] to enqueue upload works for progressive upload.
10589
*
@@ -147,3 +131,23 @@ fun WorkManager.uploadPart(
147131
tags,
148132
workerClass
149133
)
134+
135+
/**
136+
* Extension functions for [WorkManager] to cancel all upload works.
137+
*/
138+
fun WorkManager.cancelAllUploads() = UploadWorkerHelper.cancelAll(this)
139+
140+
/**
141+
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.upload].
142+
*
143+
* @param videoId The video id
144+
*/
145+
fun WorkManager.cancel(videoId: String) = UploadWorkerHelper.cancel(this, videoId)
146+
147+
/**
148+
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.uploadWithUploadToken].
149+
*
150+
* @param token The upload token
151+
*/
152+
fun WorkManager.cancelWithUploadToken(token: String) =
153+
UploadWorkerHelper.cancelWithUploadToken(this, token)

0 commit comments

Comments
 (0)