From 85b5f181999a9895488a19549f9199ab3f91f8df Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 2 Dec 2024 10:56:17 +0100 Subject: [PATCH] add cancel logic Signed-off-by: alperozturk --- app/src/main/AndroidManifest.xml | 2 ++ .../client/jobs/BackgroundJobManagerImpl.kt | 1 + .../nextcloud/client/jobs/sync/SyncWorker.kt | 7 ++++++ .../sync/SyncWorkerNotificationManager.kt | 23 +++++++++++++++++++ .../client/jobs/sync/SyncWorkerReceiver.kt | 21 +++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerReceiver.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2d3bfe327d..e1f03a2bdf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -573,6 +573,8 @@ + + + if (isStopped) { + notificationManager.dismiss() + return@withContext Result.failure() + } + fileDataStorageManager.getFileByDecryptedRemotePath(path)?.let { file -> withContext(Dispatchers.Main) { @@ -86,6 +91,8 @@ class SyncWorker( withContext(Dispatchers.Main) { notificationManager.showErrorNotification() + delay(1000) + notificationManager.dismiss() } Result.failure() diff --git a/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerNotificationManager.kt b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerNotificationManager.kt index 7a2590713b..c21e23a168 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerNotificationManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerNotificationManager.kt @@ -9,7 +9,9 @@ package com.nextcloud.client.jobs.sync import android.app.Notification import android.app.NotificationManager +import android.app.PendingIntent import android.content.Context +import android.content.Intent import android.os.Build import androidx.core.app.NotificationCompat import com.owncloud.android.ui.notifications.NotificationUtils @@ -31,6 +33,12 @@ class SyncWorkerNotificationManager(private val context: Context) { progress?.let { setProgress(100, progress, false) + + addAction( + android.R.drawable.ic_menu_close_clear_cancel, + "Cancel", + getCancelPendingIntent() + ) } setAutoCancel(true) @@ -43,6 +51,17 @@ class SyncWorkerNotificationManager(private val context: Context) { }.build() } + private fun getCancelPendingIntent(): PendingIntent { + val intent = Intent(context, SyncWorkerReceiver::class.java) + + return PendingIntent.getBroadcast( + context, + notificationId, + intent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE + ) + } + fun showStartNotification() { val notification = getNotification("Sync Operation Started", progress = 0) notificationManager.notify(notificationId, notification) @@ -65,4 +84,8 @@ class SyncWorkerNotificationManager(private val context: Context) { val notification = getNotification("Download Failed", "Error downloading file") notificationManager.notify(notificationId, notification) } + + fun dismiss() { + notificationManager.cancel(notificationId) + } } diff --git a/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerReceiver.kt b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerReceiver.kt new file mode 100644 index 0000000000..f7522ced1b --- /dev/null +++ b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerReceiver.kt @@ -0,0 +1,21 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2024 Alper Ozturk + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +package com.nextcloud.client.jobs.sync + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import androidx.work.WorkManager +import com.nextcloud.client.jobs.BackgroundJobManagerImpl + +class SyncWorkerReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val tag = BackgroundJobManagerImpl.JOB_SYNC_FOLDER + WorkManager.getInstance(context).cancelAllWorkByTag(tag) + } +}