Răsfoiți Sursa

Fix Notifications

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 an în urmă
părinte
comite
362bf80301

+ 17 - 15
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -152,6 +152,7 @@ class FileDownloadWorker(
                 context,
                 downloadType
             )
+
             operation.addDatatransferProgressListener(this)
             operation.addDatatransferProgressListener(downloadBinder as FileDownloaderBinder)
             val putResult = pendingDownloads.putIfAbsent(
@@ -232,9 +233,11 @@ class FileDownloadWorker(
     private fun notifyDownloadStart(download: DownloadFileOperation) {
         lastPercent = 0
 
-        notificationManager.notifyForStart(download)
-        notificationManager.setContentIntent(intents.detailsIntent(download), PendingIntent.FLAG_IMMUTABLE)
-        notificationManager.showDownloadInProgressNotification()
+        notificationManager.run {
+            notifyForStart(download)
+            setContentIntent(intents.detailsIntent(download), PendingIntent.FLAG_IMMUTABLE)
+            showDownloadInProgressNotification()
+        }
     }
 
     private fun getOCAccountForDownload(): OwnCloudAccount {
@@ -285,25 +288,24 @@ class FileDownloadWorker(
         download: DownloadFileOperation,
         downloadResult: RemoteOperationResult<*>
     ) {
-        if (downloadResult.isCancelled) {
-            return
-        }
+        dismissDownloadInProgressNotification()
 
-        if (downloadResult.isSuccess) {
-            dismissDownloadInProgressNotification()
+        if (downloadResult.isCancelled) {
             return
         }
 
         val needsToUpdateCredentials = (ResultCode.UNAUTHORIZED == downloadResult.code)
-        notificationManager.prepareForResult(downloadResult, needsToUpdateCredentials)
+        notificationManager.run {
+            prepareForResult(downloadResult, needsToUpdateCredentials)
 
-        if (needsToUpdateCredentials) {
-            notificationManager.setCredentialContentIntent(download.user)
-        } else {
-            notificationManager.setContentIntent(intents.detailsIntent(null), PendingIntent.FLAG_IMMUTABLE)
-        }
+            if (needsToUpdateCredentials) {
+                setCredentialContentIntent(download.user)
+            } else {
+                setContentIntent(intents.detailsIntent(null), PendingIntent.FLAG_IMMUTABLE)
+            }
 
-        notificationManager.notifyForResult(downloadResult, download)
+            notifyForResult(downloadResult, download)
+        }
     }
 
     private fun dismissDownloadInProgressNotification() {

+ 33 - 25
app/src/main/java/com/nextcloud/client/notifications/download/DownloadNotificationManager.kt

@@ -83,41 +83,49 @@ class DownloadNotificationManager(private val context: Context, private val view
 
     @Suppress("MagicNumber")
     fun notifyForResult(result: RemoteOperationResult<*>, download: DownloadFileOperation) {
-        val errorMessage = ErrorMessageAdapter.getErrorCauseMessage(
-            result,
-            download,
-            context.resources
-        )
-
-        notificationBuilder.setContentText(errorMessage)
-
-        notificationManager.notify(SecureRandom().nextInt(), notificationBuilder.build())
+        val tickerId = getTickerId(result.isSuccess, null)
+        val notifyId = SecureRandom().nextInt()
 
-        if (result.isSuccess) {
-            NotificationUtils.cancelWithDelay(
-                notificationManager,
-                R.string.downloader_download_succeeded_ticker,
-                2000
+        val contentText = if (result.isSuccess) {
+            context.getString(R.string.downloader_download_succeeded_ticker)
+        } else {
+            ErrorMessageAdapter.getErrorCauseMessage(
+                result,
+                download,
+                context.resources
             )
         }
+
+        notificationBuilder.run {
+            setTicker(context.getString(tickerId))
+            setContentText(contentText)
+            notificationManager.notify(notifyId, this.build())
+        }
+
+        NotificationUtils.cancelWithDelay(
+            notificationManager,
+            notifyId,
+            2000
+        )
     }
 
-    fun prepareForResult(
-        downloadResult: RemoteOperationResult<*>,
-        needsToUpdateCredentials: Boolean
-    ) {
-        var tickerId =
-            if (downloadResult.isSuccess) {
+    private fun getTickerId(isSuccess: Boolean, needsToUpdateCredentials: Boolean?): Int {
+        return if (needsToUpdateCredentials == true) {
+            R.string.downloader_download_failed_credentials_error
+        } else {
+            if (isSuccess) {
                 R.string.downloader_download_succeeded_ticker
             } else {
                 R.string.downloader_download_failed_ticker
             }
-
-        tickerId = if (needsToUpdateCredentials) {
-            R.string.downloader_download_failed_credentials_error
-        } else {
-            tickerId
         }
+    }
+
+    fun prepareForResult(
+        downloadResult: RemoteOperationResult<*>,
+        needsToUpdateCredentials: Boolean
+    ) {
+        val tickerId = getTickerId(downloadResult.isSuccess, needsToUpdateCredentials)
 
         notificationBuilder
             .setTicker(context.getString(tickerId))