Skip to content

Commit

Permalink
Merge pull request #80 from apivideo/feature/android_workmanager_cancel
Browse files Browse the repository at this point in the history
feat(java): improve cancellation of WorkManager uploads
  • Loading branch information
bot-api-video committed Aug 21, 2023
2 parents 564c82a + 3e0d750 commit 54d1243
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 91 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All changes to this project will be documented in this file.

## [1.5.0] - 2023-08-21
- Improve cancel of upload workers for the WorkManager API

## [1.4.2] - 2023-08-10
- Fix upload with upload token and video id when video is smaller than chunk size

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Add this dependency to your project's POM:
<dependency>
<groupId>video.api</groupId>
<artifactId>android-api-client</artifactId>
<version>1.4.2</version>
<version>1.5.0</version>
<scope>compile</scope>
</dependency>
```
Expand All @@ -68,7 +68,7 @@ Add this dependency to your project's POM:
Add this dependency to your project's build file:

```groovy
implementation "video.api:android-api-client:1.4.2"
implementation "video.api:android-api-client:1.5.0"
```

### Others
Expand All @@ -81,7 +81,7 @@ mvn clean package

Then manually install the following JARs:

* `target/android-api-client-1.4.2.jar`
* `target/android-api-client-1.5.0.jar`
* `target/lib/*.jar`

## Code sample
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'maven-publish'
apply plugin: 'kotlin-android'

group = 'video.api'
version = '1.4.2'
version = '1.5.0'

buildscript {
repositories {
Expand Down
4 changes: 2 additions & 2 deletions maven-push.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ apply plugin: 'maven-publish'
apply plugin: 'signing'

def isReleaseBuild() {
return !"1.4.2".contains("SNAPSHOT")
return !"1.5.0".contains("SNAPSHOT")
}

def getReleaseRepositoryUrl() {
Expand Down Expand Up @@ -47,7 +47,7 @@ afterEvaluate { project ->

groupId = "video.api"
artifactId = "android-api-client"
version = "1.4.2"
version = "1.5.0"

pom {
name = "video.api:android-api-client"
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>android-api-client</artifactId>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<version>1.4.2</version>
<version>1.5.0</version>
<url>https://github.com/apivideo/api.video-android-client</url>
<description>api.video Android API client</description>
<scm>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/video/api/client/api/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private OkHttpClient initHttpClient(List<Interceptor> interceptors) {
private void init() {
verifyingSsl = true;
json = new JSON();
addDefaultHeader("AV-Origin-Client", "android:1.4.2");
addDefaultHeader("AV-Origin-Client", "android:1.5.0");
}

private boolean isValid(String regex, String field) {
Expand Down
155 changes: 88 additions & 67 deletions src/main/java/video/api/client/api/work/UploadWorkerHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -209,78 +209,16 @@ object UploadWorkerHelper {
file
)
)
.addTag("android-api-client")
.addTag(getTagForUpload(videoId, token))
.addTag(ARTIFACT_ID)
.apply {
videoId?.let { addTag(getTagForVideoId(it)) }
token?.let { addTag(getTagForUploadToken(it)) }
tags.forEach { addTag(it) }
}
.build()
return OperationWithRequest(workManager.enqueue(workRequest), workRequest)
}

/**
* Cancels all works related to a video id that was added with [upload].
* Works with upload token are not cancelled.
*
* @param context The application context
* @param videoId The video id
*/
@JvmStatic
fun cancel(context: Context, videoId: String) =
cancel(WorkManager.getInstance(context), videoId)

/**
* Cancels all works related to a video id that was added with [upload].
* Works with upload token are not cancelled.
*
* @param workManager The WorkManager instance
* @param videoId The video id
*/
@JvmStatic
fun cancel(workManager: WorkManager, videoId: String) =
workManager.cancelAllWorkByTag(getTagForUpload(videoId, null))

/**
* Cancels all works related to an upload token and possibly a video id that was added with [uploadWithUploadToken].
* Works without upload token are not cancelled.
*
* @param context The application context
* @param token The upload token
* @param videoId The video id.Must be the same as the one used in [uploadWithUploadToken].
*/
@JvmStatic
fun cancelWithUploadToken(context: Context, token: String, videoId: String? = null) =
cancelWithUploadToken(WorkManager.getInstance(context), token, videoId)

/**
* Cancels all works related to an upload token and possibly a video id that was added with [uploadWithUploadToken].
* Works without upload token are not cancelled.
*
* @param workManager The WorkManager instance
* @param token The upload token
* @param videoId The video id. Must be the same as the one used in [uploadWithUploadToken].
*/
@JvmStatic
fun cancelWithUploadToken(workManager: WorkManager, token: String, videoId: String? = null) =
workManager.cancelAllWorkByTag(getTagForUpload(videoId, token))

private const val PREFIX_VIDEO_ID = "videoId="
private const val PREFIX_TOKEN = "token="

/**
* Returns the tag used to identify works related to a video id or an upload token.
*
* @param videoId The video id
* @param token The upload token
* @return The tag
*/
fun getTagForUpload(videoId: String?, token: String?): String {
require((token != null) || (videoId != null)) {
"You must provide either a token or a videoId"
}
return "($PREFIX_VIDEO_ID$videoId, $PREFIX_TOKEN$token)"
}

/**
* Enqueues a work to upload a part of a file.
*
Expand Down Expand Up @@ -404,13 +342,96 @@ object UploadWorkerHelper {
partId
)
)
.addTag("android-api-client")
.addTag(ARTIFACT_ID)
.addTag("progressive")
.addTag(getTagForUpload(session.videoId, session.token))
.apply {
session.videoId?.let { addTag(getTagForVideoId(it)) }
session.token?.let { addTag(getTagForUploadToken(it)) }
tags.forEach { addTag(it) }
}
.build()
return OperationWithRequest(workManager.enqueue(workRequest), workRequest)
}

/**
* Cancels all upload works.
*
* @param context The application context
*/
@JvmStatic
fun cancelAll(context: Context) =
cancelAll(WorkManager.getInstance(context))

/**
* Cancels all upload works.
*
* @param workManager The WorkManager instance
*/
@JvmStatic
fun cancelAll(workManager: WorkManager) =
workManager.cancelAllWorkByTag(ARTIFACT_ID)

/**
* Cancels all works related to a video id.
*
* @param context The application context
* @param videoId The video id
*/
@JvmStatic
fun cancel(context: Context, videoId: String) =
cancel(WorkManager.getInstance(context), videoId)

/**
* Cancels all works related to a video id.
*
* @param workManager The WorkManager instance
* @param videoId The video id
*/
@JvmStatic
fun cancel(workManager: WorkManager, videoId: String) =
workManager.cancelAllWorkByTag(getTagForVideoId(videoId))

/**
* Cancels all works related to an upload token that was added with [uploadWithUploadToken].
*
* @param context The application context
* @param token The upload token
*/
@JvmStatic
fun cancelWithUploadToken(context: Context, token: String) =
cancelWithUploadToken(WorkManager.getInstance(context), token)

/**
* Cancels all works related to an upload token that was added with [uploadWithUploadToken].
*
* @param workManager The WorkManager instance
* @param token The upload token
*/
@JvmStatic
fun cancelWithUploadToken(workManager: WorkManager, token: String) =
workManager.cancelAllWorkByTag(getTagForUploadToken(token))

/**
* Returns the tag used to identify works related to a video id.
*
* @param videoId The video id
* @return The tag
*/
fun getTagForVideoId(videoId: String): String {
return "($PREFIX_VIDEO_ID$videoId)"
}

/**
* Returns the tag used to identify works related to an upload token.
*
* @param token The upload token
* @return The tag
*/
fun getTagForUploadToken(token: String): String {
return "($PREFIX_TOKEN$token)"
}

private const val PREFIX_VIDEO_ID = "videoId="
private const val PREFIX_TOKEN = "token="
private const val ARTIFACT_ID = "android-api-client"
}
36 changes: 20 additions & 16 deletions src/main/java/video/api/client/api/work/WorkManagerExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,6 @@ fun WorkManager.uploadWithUploadToken(
workerClass
)

/**
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.upload].
*
* @param videoId The video id
*/
fun WorkManager.cancel(videoId: String) = UploadWorkerHelper.cancel(this, videoId)

/**
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.uploadWithUploadToken].
*
* @param token The upload token
* @param videoId The video id. Must be the same as the one used in [WorkManager.uploadWithUploadToken].
*/
fun WorkManager.cancelWithUploadToken(token: String, videoId: String? = null) =
UploadWorkerHelper.cancelWithUploadToken(this, token, videoId)

/**
* Extension functions for [WorkManager] to enqueue upload works for progressive upload.
*
Expand Down Expand Up @@ -147,3 +131,23 @@ fun WorkManager.uploadPart(
tags,
workerClass
)

/**
* Extension functions for [WorkManager] to cancel all upload works.
*/
fun WorkManager.cancelAllUploads() = UploadWorkerHelper.cancelAll(this)

/**
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.upload].
*
* @param videoId The video id
*/
fun WorkManager.cancel(videoId: String) = UploadWorkerHelper.cancel(this, videoId)

/**
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.uploadWithUploadToken].
*
* @param token The upload token
*/
fun WorkManager.cancelWithUploadToken(token: String) =
UploadWorkerHelper.cancelWithUploadToken(this, token)

0 comments on commit 54d1243

Please sign in to comment.