diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e13cc3..f5d81f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 657f5b6..1a8109e 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Add this dependency to your project's POM: video.api android-api-client - 1.4.2 + 1.5.0 compile ``` @@ -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 @@ -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 diff --git a/build.gradle b/build.gradle index a16a2db..02a3502 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { diff --git a/maven-push.gradle b/maven-push.gradle index 0cb84de..491f13f 100644 --- a/maven-push.gradle +++ b/maven-push.gradle @@ -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() { @@ -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" diff --git a/pom.xml b/pom.xml index 4ce1b49..6af3a09 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ android-api-client jar ${project.groupId}:${project.artifactId} - 1.4.2 + 1.5.0 https://github.com/apivideo/api.video-android-client api.video Android API client diff --git a/src/main/java/video/api/client/api/ApiClient.java b/src/main/java/video/api/client/api/ApiClient.java index fc62290..a70d12c 100644 --- a/src/main/java/video/api/client/api/ApiClient.java +++ b/src/main/java/video/api/client/api/ApiClient.java @@ -118,7 +118,7 @@ private OkHttpClient initHttpClient(List 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) { diff --git a/src/main/java/video/api/client/api/work/UploadWorkerHelper.kt b/src/main/java/video/api/client/api/work/UploadWorkerHelper.kt index d22b090..57216e9 100644 --- a/src/main/java/video/api/client/api/work/UploadWorkerHelper.kt +++ b/src/main/java/video/api/client/api/work/UploadWorkerHelper.kt @@ -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. * @@ -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" } diff --git a/src/main/java/video/api/client/api/work/WorkManagerExtensions.kt b/src/main/java/video/api/client/api/work/WorkManagerExtensions.kt index 4c1be82..ecac41f 100644 --- a/src/main/java/video/api/client/api/work/WorkManagerExtensions.kt +++ b/src/main/java/video/api/client/api/work/WorkManagerExtensions.kt @@ -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. * @@ -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)