Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FileUploadBroadcastReceiver : BroadcastReceiver() {

val remove = intent.getBooleanExtra(REMOVE, false)

FileUploadWorker.cancelCurrentUpload(remotePath, accountName, onCompleted = {})
FileUploadWorker.cancelUpload(remotePath, accountName)

if (remove) {
uploadsStorageManager.removeUpload(uploadId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.nextcloud.client.database.entity.toUploadEntity
import com.nextcloud.client.device.BatteryStatus
import com.nextcloud.client.device.PowerManagementService
import com.nextcloud.client.jobs.BackgroundJobManager
import com.nextcloud.client.jobs.upload.FileUploadWorker.Companion.currentUploadFileOperation
import com.nextcloud.client.network.Connectivity
import com.nextcloud.client.network.ConnectivityService
import com.nextcloud.client.notifications.AppWideNotificationManager
Expand Down Expand Up @@ -440,7 +439,7 @@ class FileUploadHelper {

@Suppress("ReturnCount")
fun isUploadingNow(upload: OCUpload?): Boolean {
val currentUploadFileOperation = currentUploadFileOperation
val currentUploadFileOperation = FileUploadWorker.getCurrentUpload(upload?.uploadId)
if (currentUploadFileOperation == null || currentUploadFileOperation.user == null) return false
if (upload == null || upload.accountName != currentUploadFileOperation.user.accountName) return false

Expand Down Expand Up @@ -610,7 +609,7 @@ class FileUploadHelper {
return
}

FileUploadWorker.cancelCurrentUpload(remotePath, accountName, onCompleted = {
FileUploadWorker.cancelUpload(remotePath, accountName, onCompleted = {
instance().updateUploadStatus(remotePath, accountName, UploadStatus.UPLOAD_CANCELLED)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.withContext
import java.io.File
import java.util.concurrent.ConcurrentHashMap
import kotlin.random.Random

@Suppress("LongParameterList", "TooGenericExceptionCaught")
Expand Down Expand Up @@ -73,8 +74,6 @@ class FileUploadWorker(
const val TOTAL_UPLOAD_SIZE = "total_upload_size"
const val SHOW_SAME_FILE_ALREADY_EXISTS_NOTIFICATION = "show_same_file_already_exists_notification"

var currentUploadFileOperation: UploadFileOperation? = null

private const val BATCH_SIZE = 100

const val EXTRA_ACCOUNT_NAME = "ACCOUNT_NAME"
Expand All @@ -84,21 +83,26 @@ class FileUploadWorker(
const val LOCAL_BEHAVIOUR_FORGET = 2
const val LOCAL_BEHAVIOUR_DELETE = 3

fun cancelCurrentUpload(remotePath: String, accountName: String, onCompleted: () -> Unit) {
currentUploadFileOperation?.let {
if (it.remotePath == remotePath && it.user.accountName == accountName) {
it.cancel(ResultCode.USER_CANCELLED)
onCompleted()
}
private val activeOperations = ConcurrentHashMap<Long, UploadFileOperation>()

@JvmOverloads
fun cancelUpload(remotePath: String?, accountName: String?, onCompleted: () -> Unit = {}) {
val operation =
activeOperations.values.find { it.remotePath == remotePath && it.user.accountName == accountName }

operation?.let {
Log_OC.d(TAG, "upload operation is cancelled: $remotePath")
operation.cancel(ResultCode.USER_CANCELLED)
activeOperations.remove(operation.ocUploadId)
}

onCompleted()
}

fun isUploading(remotePath: String?, accountName: String?): Boolean {
currentUploadFileOperation?.let {
return it.remotePath == remotePath && it.user.accountName == accountName
}
fun getCurrentUpload(id: Long?): UploadFileOperation? = activeOperations[id]

return false
fun isUploading(remotePath: String?, accountName: String?): Boolean = activeOperations.values.any {
it.remotePath == remotePath && it.user.accountName == accountName
}

fun getUploadAction(action: String): Int = when (action) {
Expand Down Expand Up @@ -128,7 +132,8 @@ class FileUploadWorker(
result
} catch (t: Throwable) {
Log_OC.e(TAG, "exception $t")
currentUploadFileOperation?.cancel(null)
activeOperations.values.forEach { it.cancel(null) }
activeOperations.clear()
Result.failure()
} finally {
// Ensure all database operations are complete before signaling completion
Expand Down Expand Up @@ -238,7 +243,7 @@ class FileUploadWorker(

fileUploadEventBroadcaster.sendUploadEnqueued(context)
val operation = createUploadFileOperation(upload, user)
currentUploadFileOperation = operation
activeOperations[upload.uploadId] = operation

val currentIndex = (index + 1)
val currentUploadIndex = (currentIndex + previouslyUploadedFileSize)
Expand All @@ -252,7 +257,7 @@ class FileUploadWorker(
val result = withContext(Dispatchers.IO) {
upload(upload, operation, user, client)
}
currentUploadFileOperation = null
activeOperations.remove(upload.uploadId)

if (result.code == ResultCode.QUOTA_EXCEEDED) {
Log_OC.w(TAG, "Quota exceeded, stopping uploads")
Expand Down Expand Up @@ -347,24 +352,24 @@ class FileUploadWorker(
}
}
result = RemoteOperationResult(e)
} finally {
if (!isStopped) {
UploadErrorNotificationManager.handleResult(
context,
notificationManager,
operation,
result,
onSameFileConflict = {
withContext(Dispatchers.Main) {
val showSameFileAlreadyExistsNotification =
inputData.getBoolean(SHOW_SAME_FILE_ALREADY_EXISTS_NOTIFICATION, false)
if (showSameFileAlreadyExistsNotification) {
notificationManager.showSameFileAlreadyExistsNotification(operation.fileName)
}
}

if (!isStopped) {
UploadErrorNotificationManager.handleResult(
context,
notificationManager,
operation,
result,
onSameFileConflict = {
withContext(Dispatchers.Main) {
val showSameFileAlreadyExistsNotification =
inputData.getBoolean(SHOW_SAME_FILE_ALREADY_EXISTS_NOTIFICATION, false)
if (showSameFileAlreadyExistsNotification) {
notificationManager.showSameFileAlreadyExistsNotification(operation.fileName)
}
}
)
}
}
)
}

return@withContext result
Expand All @@ -389,6 +394,9 @@ class FileUploadWorker(

if (percent != lastPercent && (currentTime - lastUpdateTime) >= minProgressUpdateInterval) {
notificationManager.run {
val currentUploadFileOperation =
activeOperations.values.find { it.originalStoragePath == fileAbsoluteName }

val accountName = currentUploadFileOperation?.user?.accountName
val remotePath = currentUploadFileOperation?.remotePath

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ sealed class UploadBroadcastAction {
context,
requestCode,
intent,
PendingIntent.FLAG_IMMUTABLE
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class UploadNotificationManager(private val context: Context, viewThemeUtils: Vi
notificationManager.cancel(getId())

notificationBuilder.run {
clearActions()
setContentTitle(context.getString(R.string.file_upload_worker_error_notification_title))
setContentText("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object UploadErrorNotificationManager {
) {
Log_OC.d(TAG, "handle upload result with result code: " + result.code)

if (result.isSuccess || result.isCancelled || operation.isMissingPermissionThrown) {
if (result.isSuccess || operation.isMissingPermissionThrown) {
Log_OC.d(TAG, "operation is successful, cancelled or lack of storage permission, notification skipped")
return
}
Expand All @@ -59,7 +59,9 @@ object UploadErrorNotificationManager {
ResultCode.DELAYED_FOR_CHARGING,
ResultCode.DELAYED_IN_POWER_SAVE_MODE,
ResultCode.LOCAL_FILE_NOT_FOUND,
ResultCode.LOCK_FAILED
ResultCode.LOCK_FAILED,
ResultCode.CANCELLED,
ResultCode.USER_CANCELLED
)

if (result.code in silentCodes) {
Expand Down Expand Up @@ -131,7 +133,6 @@ object UploadErrorNotificationManager {
)

// actions for all error types
addAction(UploadBroadcastAction.PauseAndCancel(operation).pauseAction(context))
addAction(UploadBroadcastAction.PauseAndCancel(operation).cancelAction(context))

if (result.code == ResultCode.SYNC_CONFLICT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void onBindHeaderViewHolder(SectionedViewHolder holder, int section, bool
uploadHelper.updateUploadStatus(upload.getRemotePath(), accountName, UploadStatus.UPLOAD_CANCELLED, new Function0<Unit>() {
@Override
public Unit invoke() {
FileUploadWorker.Companion.cancelCurrentUpload(upload.getRemotePath(), accountName, new Function0<Unit>() {
FileUploadWorker.Companion.cancelUpload(upload.getRemotePath(), accountName, new Function0<Unit>() {
@Override
public Unit invoke() {
completedCount[0]++;
Expand Down Expand Up @@ -428,7 +428,7 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
itemViewHolder.binding.uploadRightButton.setVisibility(View.VISIBLE);
itemViewHolder.binding.uploadRightButton.setOnClickListener(v -> {
uploadHelper.updateUploadStatus(item.getRemotePath(), item.getAccountName(), UploadStatus.UPLOAD_CANCELLED, () -> {
FileUploadWorker.Companion.cancelCurrentUpload(item.getRemotePath(), item.getAccountName(), () -> {
FileUploadWorker.Companion.cancelUpload(item.getRemotePath(), item.getAccountName(), () -> {
loadUploadItemsFromDb(() -> {});
return Unit.INSTANCE;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ public void cancelTransference(OCFile file) {

final var fileUploadHelper = FileUploadHelper.Companion.instance();
if (fileUploadHelper.isUploading(file.getRemotePath(), currentUser.getAccountName())) {
FileUploadWorker.Companion.cancelCurrentUpload(file.getRemotePath(), currentUser.getAccountName(), () -> {
FileUploadWorker.Companion.cancelUpload(file.getRemotePath(), currentUser.getAccountName(), () -> {
fileUploadHelper.updateUploadStatus(file.getRemotePath(), currentUser.getAccountName(), UploadsStorageManager.UploadStatus.UPLOAD_CANCELLED);
return Unit.INSTANCE;
});
Expand Down
Loading