add cancel logic

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-02 10:56:17 +01:00 committed by Alper Öztürk
parent bc5a87d56e
commit 85b5f18199
5 changed files with 54 additions and 0 deletions

View file

@ -573,6 +573,8 @@
</intent-filter>
</receiver>
<receiver android:name="com.nextcloud.client.jobs.sync.SyncWorkerReceiver" android:exported="false" />
<activity
android:name=".ui.activity.CopyToClipboardActivity"
android:exported="false"

View file

@ -725,6 +725,7 @@ internal class BackgroundJobManagerImpl(
.build()
val request = oneTimeRequestBuilder(SyncWorker::class, JOB_SYNC_FOLDER)
.addTag(JOB_SYNC_FOLDER)
.setInputData(data)
.build()

View file

@ -53,6 +53,11 @@ class SyncWorker(
var result = true
filePaths.forEachIndexed { index, path ->
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()

View file

@ -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)
}
}

View file

@ -0,0 +1,21 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* 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)
}
}