Эх сурвалжийг харах

Fixes notification manager

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 жил өмнө
parent
commit
cb40293932

+ 1 - 1
app/src/main/java/com/nextcloud/client/notifications/AppNotificationManagerImpl.kt

@@ -45,7 +45,7 @@ class AppNotificationManagerImpl @Inject constructor(
         val icon = BitmapFactory.decodeResource(resources, R.drawable.notification_icon)
         return builder(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
             .setContentTitle(resources.getString(R.string.app_name))
-            .setContentText(resources.getString(R.string.foreground_service_download))
+            .setContentText(resources.getString(R.string.worker_download))
             .setSmallIcon(R.drawable.notification_icon)
             .setLargeIcon(icon)
             .build()

+ 46 - 42
app/src/main/java/com/nextcloud/client/notifications/download/DownloadNotificationManager.kt

@@ -48,14 +48,14 @@ class DownloadNotificationManager(private val context: Context, private val view
     private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
 
     fun init() {
-        notificationBuilder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils)
-            .setContentTitle(context.resources.getString(R.string.app_name))
-            .setContentText(context.resources.getString(R.string.foreground_service_download))
-            .setSmallIcon(R.drawable.notification_icon)
-            .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.notification_icon))
-
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
-            notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
+        notificationBuilder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils).apply {
+            setContentTitle(context.resources.getString(R.string.app_name))
+            setContentText(context.resources.getString(R.string.worker_download))
+            setSmallIcon(R.drawable.notification_icon)
+            setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.notification_icon))
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+                setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
+            }
         }
 
         notification = notificationBuilder.build()
@@ -63,44 +63,50 @@ class DownloadNotificationManager(private val context: Context, private val view
 
     @Suppress("MagicNumber")
     fun notifyForStart(operation: DownloadFileOperation) {
-        notificationBuilder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils)
-            .setSmallIcon(R.drawable.notification_icon)
-            .setTicker(context.getString(R.string.downloader_download_in_progress_ticker))
-            .setContentTitle(context.getString(R.string.downloader_download_in_progress_ticker))
-            .setOngoing(true)
-            .setProgress(100, 0, operation.size < 0)
-            .setContentText(
+        notificationBuilder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils).apply {
+            setSmallIcon(R.drawable.notification_icon)
+            setTicker(context.getString(R.string.downloader_download_in_progress_ticker))
+            setContentTitle(context.getString(R.string.downloader_download_in_progress_ticker))
+            setOngoing(true)
+            setProgress(100, 0, operation.size < 0)
+            setContentText(
                 String.format(
                     context.getString(R.string.downloader_download_in_progress_content), 0,
                     File(operation.savePath).name
                 )
             )
 
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
-            notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+                setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
+            }
         }
     }
 
+    fun prepareForResult(
+        downloadResult: RemoteOperationResult<*>,
+        needsToUpdateCredentials: Boolean
+    ) {
+        val tickerId = getTickerId(downloadResult.isSuccess, needsToUpdateCredentials)
+
+        notificationBuilder
+            .setTicker(context.getString(tickerId))
+            .setContentTitle(context.getString(tickerId))
+            .setAutoCancel(true)
+            .setOngoing(false)
+            .setProgress(0, 0, false)
+    }
+
     @Suppress("MagicNumber")
     fun notifyForResult(result: RemoteOperationResult<*>, download: DownloadFileOperation) {
         dismissDownloadInProgressNotification()
 
         val tickerId = getTickerId(result.isSuccess, null)
         val notifyId = SecureRandom().nextInt()
-
-        val contentText = if (result.isSuccess) {
-            download.file.fileName
-        } else {
-            ErrorMessageAdapter.getErrorCauseMessage(
-                result,
-                download,
-                context.resources
-            )
-        }
+        val resultText = getResultText(result, download)
 
         notificationBuilder.run {
             setTicker(context.getString(tickerId))
-            setContentText(contentText)
+            setContentText(resultText)
             notificationManager.notify(notifyId, this.build())
         }
 
@@ -111,6 +117,18 @@ class DownloadNotificationManager(private val context: Context, private val view
         )
     }
 
+    private fun getResultText(result: RemoteOperationResult<*>, download: DownloadFileOperation): String {
+        return if (result.isSuccess) {
+            download.file.fileName
+        } else {
+            ErrorMessageAdapter.getErrorCauseMessage(
+                result,
+                download,
+                context.resources
+            )
+        }
+    }
+
     private fun getTickerId(isSuccess: Boolean, needsToUpdateCredentials: Boolean?): Int {
         return if (needsToUpdateCredentials == true) {
             R.string.downloader_download_failed_credentials_error
@@ -123,20 +141,6 @@ class DownloadNotificationManager(private val context: Context, private val view
         }
     }
 
-    fun prepareForResult(
-        downloadResult: RemoteOperationResult<*>,
-        needsToUpdateCredentials: Boolean
-    ) {
-        val tickerId = getTickerId(downloadResult.isSuccess, needsToUpdateCredentials)
-
-        notificationBuilder
-            .setTicker(context.getString(tickerId))
-            .setContentTitle(context.getString(tickerId))
-            .setAutoCancel(true)
-            .setOngoing(false)
-            .setProgress(0, 0, false)
-    }
-
     @Suppress("MagicNumber")
     fun updateDownloadProgressNotification(filePath: String, percent: Int, totalToTransfer: Long) {
         notificationBuilder.setProgress(100, percent, totalToTransfer < 0)

+ 0 - 5
app/src/main/java/com/nextcloud/utils/extensions/ContextExtensions.kt

@@ -27,7 +27,6 @@ import android.content.Context
 import android.content.Intent
 import android.content.IntentFilter
 import android.os.Build
-import androidx.work.WorkManager
 import com.owncloud.android.datamodel.ReceiverFlag
 
 @SuppressLint("UnspecifiedRegisterReceiverFlag")
@@ -38,7 +37,3 @@ fun Context.registerBroadcastReceiver(receiver: BroadcastReceiver?, filter: Inte
         registerReceiver(receiver, filter)
     }
 }
-
-fun Context.isWorkScheduled(tag: String): Boolean {
-    return WorkManager.getInstance(this).isWorkScheduled(tag)
-}

+ 1 - 1
app/src/main/res/values/strings.xml

@@ -636,7 +636,7 @@
     <string name="resharing_is_not_allowed">Resharing is not allowed</string>
 
     <string name="foreground_service_upload">Uploading files…</string>
-    <string name="foreground_service_download">Downloading files…</string>
+    <string name="worker_download">Downloading files…</string>
 
     <string name="prefs_sourcecode">Get source code</string>
     <string name="prefs_license">License</string>