mirror of
https://github.com/nextcloud/android.git
synced 2024-12-18 23:11:58 +03:00
add cancel logic
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
bc5a87d56e
commit
85b5f18199
5 changed files with 54 additions and 0 deletions
|
@ -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"
|
||||
|
|
|
@ -725,6 +725,7 @@ internal class BackgroundJobManagerImpl(
|
|||
.build()
|
||||
|
||||
val request = oneTimeRequestBuilder(SyncWorker::class, JOB_SYNC_FOLDER)
|
||||
.addTag(JOB_SYNC_FOLDER)
|
||||
.setInputData(data)
|
||||
.build()
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue