Add SyncWorkerNotificationManager

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-02 10:48:10 +01:00 committed by Alper Öztürk
parent 2b576a052b
commit bc5a87d56e
2 changed files with 93 additions and 2 deletions

View file

@ -16,6 +16,7 @@ import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.operations.DownloadFileOperation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
class SyncWorker(
@ -29,10 +30,15 @@ class SyncWorker(
private const val TAG = "SyncWorker"
}
private val notificationManager = SyncWorkerNotificationManager(context)
@Suppress("DEPRECATION")
override suspend fun doWork(): Result {
withContext(Dispatchers.Main) {
notificationManager.showStartNotification()
}
return withContext(Dispatchers.IO) {
// TODO add notifications
Log_OC.d(TAG, "SyncWorker started")
val filePaths = inputData.getStringArray(FILE_PATHS)
@ -46,8 +52,15 @@ class SyncWorker(
val client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(account, context)
var result = true
filePaths.forEach { path ->
filePaths.forEachIndexed { index, path ->
fileDataStorageManager.getFileByDecryptedRemotePath(path)?.let { file ->
withContext(Dispatchers.Main) {
notificationManager.showProgressNotification(file.fileName, index, filePaths.size)
}
delay(1000)
// TODO dont download downloaded files??
val operation = DownloadFileOperation(user, file, context).execute(client)
Log_OC.d(TAG, "Syncing file: " + file.decryptedRemotePath)
@ -62,9 +75,19 @@ class SyncWorker(
if (result) {
Log_OC.d(TAG, "SyncWorker completed")
withContext(Dispatchers.Main) {
notificationManager.showSuccessNotification()
}
Result.success()
} else {
Log_OC.d(TAG, "SyncWorker failed")
withContext(Dispatchers.Main) {
notificationManager.showErrorNotification()
}
Result.failure()
}
}

View file

@ -0,0 +1,68 @@
/*
* 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.app.Notification
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import com.owncloud.android.ui.notifications.NotificationUtils
class SyncWorkerNotificationManager(private val context: Context) {
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val notificationId = 129
private val channelId = NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD
private fun getNotification(title: String, description: String? = null, progress: Int? = null): Notification {
return NotificationCompat.Builder(context, channelId).apply {
setSmallIcon(android.R.drawable.stat_sys_download_done)
setContentTitle(title)
description?.let {
setContentText(description)
}
progress?.let {
setProgress(100, progress, false)
}
setAutoCancel(true)
setStyle(NotificationCompat.BigTextStyle())
priority = NotificationCompat.PRIORITY_LOW
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
}
}.build()
}
fun showStartNotification() {
val notification = getNotification("Sync Operation Started", progress = 0)
notificationManager.notify(notificationId, notification)
}
fun showProgressNotification(filename: String, currentIndex: Int, totalFileSize: Int) {
val currentFileIndex = (currentIndex + 1)
val title = "$currentFileIndex / $totalFileSize - $filename"
val progress = (currentFileIndex * 100) / totalFileSize
val notification = getNotification(title, progress = progress)
notificationManager.notify(notificationId, notification)
}
fun showSuccessNotification() {
val notification = getNotification("Download Complete", "File downloaded successfully")
notificationManager.notify(notificationId, notification)
}
fun showErrorNotification() {
val notification = getNotification("Download Failed", "Error downloading file")
notificationManager.notify(notificationId, notification)
}
}