Skip to content

Commit 9f1db18

Browse files
authored
fix(storage): Remove work manager restricted API usage and upgrade lib. (#2674)
1 parent 051988a commit 9f1db18

File tree

7 files changed

+21
-36
lines changed

7 files changed

+21
-36
lines changed

aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchRouterWorker.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
*/
1515
package com.amplifyframework.logging.cloudwatch.worker
1616

17-
import android.annotation.SuppressLint
1817
import android.content.Context
1918
import androidx.concurrent.futures.CallbackToFutureAdapter
19+
import androidx.work.CoroutineWorker
2020
import androidx.work.ListenableWorker
2121
import androidx.work.WorkerParameters
2222
import com.google.common.util.concurrent.ListenableFuture
@@ -28,7 +28,7 @@ internal class CloudwatchRouterWorker(appContext: Context, private val parameter
2828
private val workerClassName =
2929
parameter.inputData.getString(WORKER_CLASS_NAME)
3030
?: throw IllegalArgumentException("Worker class name is missing")
31-
private var delegateWorker: ListenableWorker? = null
31+
private var delegateWorker: CoroutineWorker? = null
3232

3333
companion object {
3434
internal const val WORKER_CLASS_NAME = "WORKER_CLASS_NAME"
@@ -70,14 +70,8 @@ internal class CloudwatchRouterWorker(appContext: Context, private val parameter
7070
}
7171
}
7272

73-
@SuppressLint("RestrictedApi")
7473
override fun onStopped() {
7574
super.onStopped()
76-
delegateWorker?.stop()
77-
}
78-
79-
@SuppressLint("RestrictedApi")
80-
override fun isRunInForeground(): Boolean {
81-
return delegateWorker?.isRunInForeground ?: false
75+
delegateWorker?.onStopped()
8276
}
8377
}

aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchWorkerFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.amplifyframework.logging.cloudwatch.worker
1616

1717
import android.content.Context
18-
import androidx.work.ListenableWorker
18+
import androidx.work.CoroutineWorker
1919
import androidx.work.WorkerFactory
2020
import androidx.work.WorkerParameters
2121
import com.amplifyframework.logging.cloudwatch.CloudWatchLogManager
@@ -30,7 +30,7 @@ internal class CloudwatchWorkerFactory(
3030
appContext: Context,
3131
workerClassName: String,
3232
workerParameters: WorkerParameters
33-
): ListenableWorker {
33+
): CoroutineWorker {
3434
return when (workerClassName) {
3535
CloudwatchLogsSyncWorker::class.java.simpleName -> {
3636
CloudwatchLogsSyncWorker(

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/BaseTransferWorker.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import java.io.File
5151
import java.lang.Exception
5252
import java.net.SocketException
5353
import kotlinx.coroutines.CancellationException
54+
import kotlinx.coroutines.currentCoroutineContext
55+
import kotlinx.coroutines.isActive
5456

5557
/**
5658
* Base worker to perform transfer file task.
@@ -74,9 +76,6 @@ internal abstract class BaseTransferWorker(
7476
internal const val PART_RECORD_ID = "PART_RECORD_ID"
7577
internal const val RUN_AS_FOREGROUND_TASK = "RUN_AS_FOREGROUND_TASK"
7678
internal const val WORKER_ID = "WORKER_ID"
77-
private const val OBJECT_TAGS_DELIMITER = "&"
78-
private const val OBJECT_TAG_KEY_VALUE_SEPARATOR = "="
79-
private const val REQUESTER_PAYS = "requester"
8079
private val CANNED_ACL_MAP =
8180
ObjectCannedAcl.values().associateBy { it.value }
8281
internal const val MULTI_PART_UPLOAD_ID = "multipartUploadId"
@@ -112,10 +111,10 @@ internal abstract class BaseTransferWorker(
112111
}
113112
else -> {
114113
val ex = result.exceptionOrNull()
115-
if (!isStopped) {
114+
if (currentCoroutineContext().isActive) {
116115
logger.error("${this.javaClass.simpleName} failed with exception: ${Log.getStackTraceString(ex)}")
117116
}
118-
if (isRetryableError(ex)) {
117+
if (!currentCoroutineContext().isActive && isRetryableError(ex)) {
119118
Result.retry()
120119
} else {
121120
transferStatusUpdater.updateOnError(transferRecord.id, Exception(ex))
@@ -151,8 +150,7 @@ internal abstract class BaseTransferWorker(
151150
}
152151

153152
private fun isRetryableError(e: Throwable?): Boolean {
154-
return isStopped ||
155-
!isNetworkAvailable(applicationContext) ||
153+
return !isNetworkAvailable(applicationContext) ||
156154
runAttemptCount < maxRetryCount ||
157155
e is CancellationException ||
158156
// SocketException is thrown when download is terminated due to network disconnection.
@@ -200,7 +198,7 @@ internal abstract class BaseTransferWorker(
200198
return false
201199
}
202200

203-
internal suspend fun createPutObjectRequest(
201+
internal fun createPutObjectRequest(
204202
transferRecord: TransferRecord,
205203
progressListener: ProgressListener?
206204
): PutObjectRequest {

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/DownloadWorker.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import com.amplifyframework.storage.s3.transfer.TransferStatusUpdater
3131
import java.io.BufferedOutputStream
3232
import java.io.File
3333
import java.io.FileOutputStream
34-
import kotlinx.coroutines.CoroutineScope
3534
import kotlinx.coroutines.Dispatchers
35+
import kotlinx.coroutines.currentCoroutineContext
36+
import kotlinx.coroutines.isActive
3637
import kotlinx.coroutines.withContext
3738

3839
/**
@@ -48,7 +49,6 @@ internal class DownloadWorker(
4849

4950
private lateinit var downloadProgressListener: DownloadProgressListener
5051
private val defaultBufferSize = 8192L
51-
val coroutineScope = CoroutineScope(Dispatchers.IO)
5252
override suspend fun performWork(): Result {
5353
s3.withConfig {
5454
enableAccelerate = transferRecord.useAccelerateEndpoint == 1
@@ -104,7 +104,7 @@ internal class DownloadWorker(
104104
var totalRead = 0L
105105
BufferedOutputStream(fileOutputStream).use { fileOutput ->
106106
val copied = 0L
107-
while (!isStopped) {
107+
while (currentCoroutineContext().isActive) {
108108
val remaining = limit - copied
109109
if (remaining == 0L) break
110110
val readBytes =

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/PartUploadTransferWorker.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import com.amplifyframework.storage.s3.transfer.TransferDB
2626
import com.amplifyframework.storage.s3.transfer.TransferStatusUpdater
2727
import com.amplifyframework.storage.s3.transfer.UploadProgressListenerInterceptor
2828
import java.io.File
29+
import kotlinx.coroutines.currentCoroutineContext
30+
import kotlinx.coroutines.isActive
2931

3032
/**
3133
* Worker to upload a part for multipart upload
@@ -43,7 +45,7 @@ internal class PartUploadTransferWorker(
4345
override var maxRetryCount = 3
4446

4547
override suspend fun performWork(): Result {
46-
if (isStopped) {
48+
if (!currentCoroutineContext().isActive) {
4749
return Result.retry()
4850
}
4951
transferStatusUpdater.updateTransferState(transferRecord.mainUploadId, TransferState.IN_PROGRESS)

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/RouterWorker.kt

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
package com.amplifyframework.storage.s3.transfer.worker
1717

18-
import android.annotation.SuppressLint
1918
import android.content.Context
2019
import androidx.concurrent.futures.CallbackToFutureAdapter
2120
import androidx.work.ListenableWorker
@@ -29,8 +28,7 @@ import java.lang.IllegalStateException
2928
/**
3029
* Worker to route transfer WorkRequest to appropriate WorkerFactory
3130
*/
32-
@SuppressLint("RestrictedApi")
33-
internal class RouterWorker constructor(
31+
internal class RouterWorker(
3432
appContext: Context,
3533
private val parameter: WorkerParameters
3634
) : ListenableWorker(appContext, parameter) {
@@ -45,7 +43,7 @@ internal class RouterWorker constructor(
4543
?: throw IllegalArgumentException("Worker class name is missing")
4644
private val workerId = parameter.inputData.getString(BaseTransferWorker.WORKER_ID)
4745

48-
private var delegateWorker: ListenableWorker? = null
46+
private var delegateWorker: BaseTransferWorker? = null
4947

5048
companion object {
5149
internal const val WORKER_CLASS_NAME = "WORKER_CLASS_NAME"
@@ -86,15 +84,9 @@ internal class RouterWorker constructor(
8684
}
8785
}
8886

89-
@SuppressLint("RestrictedApi")
9087
override fun onStopped() {
9188
super.onStopped()
9289
logger.debug("onStopped for $id")
93-
delegateWorker?.stop()
94-
}
95-
96-
@SuppressLint("RestrictedApi")
97-
override fun isRunInForeground(): Boolean {
98-
return delegateWorker?.isRunInForeground ?: false
90+
delegateWorker?.onStopped()
9991
}
10092
}

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/TransferWorkerFactory.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.amplifyframework.storage.s3.transfer.worker
1616

1717
import android.content.Context
18-
import androidx.work.ListenableWorker
1918
import androidx.work.WorkerFactory
2019
import androidx.work.WorkerParameters
2120
import aws.sdk.kotlin.services.s3.S3Client
@@ -34,7 +33,7 @@ internal class TransferWorkerFactory(
3433
appContext: Context,
3534
workerClassName: String,
3635
workerParameters: WorkerParameters
37-
): ListenableWorker {
36+
): BaseTransferWorker {
3837
when (workerClassName) {
3938
DownloadWorker::class.java.name ->
4039
return DownloadWorker(

0 commit comments

Comments
 (0)