Skip to content

Commit ac15b3b

Browse files
committed
fix(file-upload-worker): pausing upload
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 836a039 commit ac15b3b

7 files changed

Lines changed: 30 additions & 24 deletions

File tree

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadBroadcastReceiver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class FileUploadBroadcastReceiver : BroadcastReceiver() {
5757

5858
val remove = intent.getBooleanExtra(REMOVE, false)
5959

60-
FileUploadWorker.cancelCurrentUpload(remotePath, accountName, onCompleted = {})
60+
FileUploadWorker.cancelUpload(remotePath, accountName)
6161

6262
if (remove) {
6363
uploadsStorageManager.removeUpload(uploadId)

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import com.nextcloud.client.database.entity.toUploadEntity
1919
import com.nextcloud.client.device.BatteryStatus
2020
import com.nextcloud.client.device.PowerManagementService
2121
import com.nextcloud.client.jobs.BackgroundJobManager
22-
import com.nextcloud.client.jobs.upload.FileUploadWorker.Companion.currentUploadFileOperation
2322
import com.nextcloud.client.network.Connectivity
2423
import com.nextcloud.client.network.ConnectivityService
2524
import com.nextcloud.client.notifications.AppWideNotificationManager
@@ -440,7 +439,7 @@ class FileUploadHelper {
440439

441440
@Suppress("ReturnCount")
442441
fun isUploadingNow(upload: OCUpload?): Boolean {
443-
val currentUploadFileOperation = currentUploadFileOperation
442+
val currentUploadFileOperation = FileUploadWorker.getCurrentUpload(upload?.uploadId)
444443
if (currentUploadFileOperation == null || currentUploadFileOperation.user == null) return false
445444
if (upload == null || upload.accountName != currentUploadFileOperation.user.accountName) return false
446445

@@ -610,7 +609,7 @@ class FileUploadHelper {
610609
return
611610
}
612611

613-
FileUploadWorker.cancelCurrentUpload(remotePath, accountName, onCompleted = {
612+
FileUploadWorker.cancelUpload(remotePath, accountName, onCompleted = {
614613
instance().updateUploadStatus(remotePath, accountName, UploadStatus.UPLOAD_CANCELLED)
615614
})
616615
}

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadWorker.kt

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import kotlinx.coroutines.Dispatchers
4545
import kotlinx.coroutines.ensureActive
4646
import kotlinx.coroutines.withContext
4747
import java.io.File
48+
import java.util.concurrent.ConcurrentHashMap
4849
import kotlin.random.Random
4950

5051
@Suppress("LongParameterList", "TooGenericExceptionCaught")
@@ -73,8 +74,6 @@ class FileUploadWorker(
7374
const val TOTAL_UPLOAD_SIZE = "total_upload_size"
7475
const val SHOW_SAME_FILE_ALREADY_EXISTS_NOTIFICATION = "show_same_file_already_exists_notification"
7576

76-
var currentUploadFileOperation: UploadFileOperation? = null
77-
7877
private const val BATCH_SIZE = 100
7978

8079
const val EXTRA_ACCOUNT_NAME = "ACCOUNT_NAME"
@@ -84,21 +83,25 @@ class FileUploadWorker(
8483
const val LOCAL_BEHAVIOUR_FORGET = 2
8584
const val LOCAL_BEHAVIOUR_DELETE = 3
8685

87-
fun cancelCurrentUpload(remotePath: String, accountName: String, onCompleted: () -> Unit) {
88-
currentUploadFileOperation?.let {
89-
if (it.remotePath == remotePath && it.user.accountName == accountName) {
90-
it.cancel(ResultCode.USER_CANCELLED)
91-
onCompleted()
92-
}
86+
private val activeOperations = ConcurrentHashMap<Long, UploadFileOperation>()
87+
88+
@JvmOverloads
89+
fun cancelUpload(remotePath: String?, accountName: String?, onCompleted: () -> Unit = {}) {
90+
val operation =
91+
activeOperations.values.find { it.remotePath == remotePath && it.user.accountName == accountName }
92+
93+
operation?.let {
94+
operation.cancel(ResultCode.USER_CANCELLED)
95+
activeOperations.remove(operation.ocUploadId)
9396
}
97+
98+
onCompleted()
9499
}
95100

96-
fun isUploading(remotePath: String?, accountName: String?): Boolean {
97-
currentUploadFileOperation?.let {
98-
return it.remotePath == remotePath && it.user.accountName == accountName
99-
}
101+
fun getCurrentUpload(id: Long?): UploadFileOperation? = activeOperations[id]
100102

101-
return false
103+
fun isUploading(remotePath: String?, accountName: String?): Boolean = activeOperations.values.any {
104+
it.remotePath == remotePath && it.user.accountName == accountName
102105
}
103106

104107
fun getUploadAction(action: String): Int = when (action) {
@@ -128,7 +131,8 @@ class FileUploadWorker(
128131
result
129132
} catch (t: Throwable) {
130133
Log_OC.e(TAG, "exception $t")
131-
currentUploadFileOperation?.cancel(null)
134+
activeOperations.values.forEach { it.cancel(null) }
135+
activeOperations.clear()
132136
Result.failure()
133137
} finally {
134138
// Ensure all database operations are complete before signaling completion
@@ -238,7 +242,7 @@ class FileUploadWorker(
238242

239243
fileUploadEventBroadcaster.sendUploadEnqueued(context)
240244
val operation = createUploadFileOperation(upload, user)
241-
currentUploadFileOperation = operation
245+
activeOperations[upload.uploadId] = operation
242246

243247
val currentIndex = (index + 1)
244248
val currentUploadIndex = (currentIndex + previouslyUploadedFileSize)
@@ -252,7 +256,7 @@ class FileUploadWorker(
252256
val result = withContext(Dispatchers.IO) {
253257
upload(upload, operation, user, client)
254258
}
255-
currentUploadFileOperation = null
259+
activeOperations.remove(upload.uploadId)
256260

257261
if (result.code == ResultCode.QUOTA_EXCEEDED) {
258262
Log_OC.w(TAG, "Quota exceeded, stopping uploads")
@@ -389,6 +393,9 @@ class FileUploadWorker(
389393

390394
if (percent != lastPercent && (currentTime - lastUpdateTime) >= minProgressUpdateInterval) {
391395
notificationManager.run {
396+
val currentUploadFileOperation =
397+
activeOperations.values.find { it.originalStoragePath == fileAbsoluteName }
398+
392399
val accountName = currentUploadFileOperation?.user?.accountName
393400
val remotePath = currentUploadFileOperation?.remotePath
394401

app/src/main/java/com/nextcloud/client/jobs/upload/UploadNotificationManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class UploadNotificationManager(private val context: Context, viewThemeUtils: Vi
106106
notificationManager.cancel(getId())
107107

108108
notificationBuilder.run {
109+
clearActions()
109110
setContentTitle(context.getString(R.string.file_upload_worker_error_notification_title))
110111
setContentText("")
111112
}

app/src/main/java/com/nextcloud/client/jobs/utils/UploadErrorNotificationManager.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ object UploadErrorNotificationManager {
131131
)
132132

133133
// actions for all error types
134-
addAction(UploadBroadcastAction.PauseAndCancel(operation).pauseAction(context))
135134
addAction(UploadBroadcastAction.PauseAndCancel(operation).cancelAction(context))
136135

137136
if (result.code == ResultCode.SYNC_CONFLICT) {

app/src/main/java/com/owncloud/android/ui/adapter/UploadListAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public void onBindHeaderViewHolder(SectionedViewHolder holder, int section, bool
186186
uploadHelper.updateUploadStatus(upload.getRemotePath(), accountName, UploadStatus.UPLOAD_CANCELLED, new Function0<Unit>() {
187187
@Override
188188
public Unit invoke() {
189-
FileUploadWorker.Companion.cancelCurrentUpload(upload.getRemotePath(), accountName, new Function0<Unit>() {
189+
FileUploadWorker.Companion.cancelUpload(upload.getRemotePath(), accountName, new Function0<Unit>() {
190190
@Override
191191
public Unit invoke() {
192192
completedCount[0]++;
@@ -428,7 +428,7 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
428428
itemViewHolder.binding.uploadRightButton.setVisibility(View.VISIBLE);
429429
itemViewHolder.binding.uploadRightButton.setOnClickListener(v -> {
430430
uploadHelper.updateUploadStatus(item.getRemotePath(), item.getAccountName(), UploadStatus.UPLOAD_CANCELLED, () -> {
431-
FileUploadWorker.Companion.cancelCurrentUpload(item.getRemotePath(), item.getAccountName(), () -> {
431+
FileUploadWorker.Companion.cancelUpload(item.getRemotePath(), item.getAccountName(), () -> {
432432
loadUploadItemsFromDb(() -> {});
433433
return Unit.INSTANCE;
434434
});

app/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ public void cancelTransference(OCFile file) {
10431043

10441044
final var fileUploadHelper = FileUploadHelper.Companion.instance();
10451045
if (fileUploadHelper.isUploading(file.getRemotePath(), currentUser.getAccountName())) {
1046-
FileUploadWorker.Companion.cancelCurrentUpload(file.getRemotePath(), currentUser.getAccountName(), () -> {
1046+
FileUploadWorker.Companion.cancelUpload(file.getRemotePath(), currentUser.getAccountName(), () -> {
10471047
fileUploadHelper.updateUploadStatus(file.getRemotePath(), currentUser.getAccountName(), UploadsStorageManager.UploadStatus.UPLOAD_CANCELLED);
10481048
return Unit.INSTANCE;
10491049
});

0 commit comments

Comments
 (0)