AppNotificationManagerImpl.kt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.nextcloud.client.notifications
  2. import android.app.Notification
  3. import android.app.NotificationManager
  4. import android.app.PendingIntent
  5. import android.content.Context
  6. import android.content.Intent
  7. import android.content.res.Resources
  8. import android.graphics.BitmapFactory
  9. import android.os.Build
  10. import androidx.core.app.NotificationCompat
  11. import com.nextcloud.client.account.User
  12. import com.owncloud.android.R
  13. import com.owncloud.android.datamodel.OCFile
  14. import com.owncloud.android.ui.activity.FileDisplayActivity
  15. import com.owncloud.android.ui.notifications.NotificationUtils
  16. import com.owncloud.android.ui.preview.PreviewImageActivity
  17. import com.owncloud.android.ui.preview.PreviewImageFragment
  18. import com.owncloud.android.utils.theme.ThemeColorUtils
  19. import javax.inject.Inject
  20. class AppNotificationManagerImpl @Inject constructor(
  21. private val context: Context,
  22. private val resources: Resources,
  23. private val platformNotificationsManager: NotificationManager,
  24. private val themeColorUtils: ThemeColorUtils
  25. ) : AppNotificationManager {
  26. companion object {
  27. const val PROGRESS_PERCENTAGE_MAX = 100
  28. const val PROGRESS_PERCENTAGE_MIN = 0
  29. }
  30. private fun builder(channelId: String): NotificationCompat.Builder {
  31. val color = themeColorUtils.primaryColor(context, true)
  32. return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  33. NotificationCompat.Builder(context, channelId).setColor(color)
  34. } else {
  35. NotificationCompat.Builder(context).setColor(color)
  36. }
  37. }
  38. override fun buildDownloadServiceForegroundNotification(): Notification {
  39. val icon = BitmapFactory.decodeResource(resources, R.drawable.notification_icon)
  40. return builder(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
  41. .setContentTitle(resources.getString(R.string.app_name))
  42. .setContentText(resources.getString(R.string.foreground_service_download))
  43. .setSmallIcon(R.drawable.notification_icon)
  44. .setLargeIcon(icon)
  45. .build()
  46. }
  47. override fun postDownloadTransferProgress(fileOwner: User, file: OCFile, progress: Int, allowPreview: Boolean) {
  48. val builder = builder(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
  49. val content = resources.getString(
  50. R.string.downloader_download_in_progress_content,
  51. progress,
  52. file.fileName
  53. )
  54. builder
  55. .setSmallIcon(R.drawable.ic_cloud_download)
  56. .setTicker(resources.getString(R.string.downloader_download_in_progress_ticker))
  57. .setContentTitle(resources.getString(R.string.downloader_download_in_progress_ticker))
  58. .setOngoing(true)
  59. .setProgress(PROGRESS_PERCENTAGE_MAX, progress, progress <= PROGRESS_PERCENTAGE_MIN)
  60. .setContentText(content)
  61. if (allowPreview) {
  62. val openFileIntent = if (PreviewImageFragment.canBePreviewed(file)) {
  63. PreviewImageActivity.previewFileIntent(context, fileOwner, file)
  64. } else {
  65. FileDisplayActivity.openFileIntent(context, fileOwner, file)
  66. }
  67. openFileIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
  68. val pendingOpenFileIntent = PendingIntent.getActivity(
  69. context,
  70. System.currentTimeMillis().toInt(),
  71. openFileIntent,
  72. PendingIntent.FLAG_IMMUTABLE
  73. )
  74. builder.setContentIntent(pendingOpenFileIntent)
  75. }
  76. platformNotificationsManager.notify(AppNotificationManager.TRANSFER_NOTIFICATION_ID, builder.build())
  77. }
  78. override fun postUploadTransferProgress(fileOwner: User, file: OCFile, progress: Int) {
  79. val builder = builder(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
  80. val content = resources.getString(
  81. R.string.uploader_upload_in_progress_content,
  82. progress,
  83. file.fileName
  84. )
  85. builder
  86. .setSmallIcon(R.drawable.ic_cloud_upload)
  87. .setTicker(resources.getString(R.string.uploader_upload_in_progress_ticker))
  88. .setContentTitle(resources.getString(R.string.uploader_upload_in_progress_ticker))
  89. .setOngoing(true)
  90. .setProgress(PROGRESS_PERCENTAGE_MAX, progress, progress <= PROGRESS_PERCENTAGE_MIN)
  91. .setContentText(content)
  92. platformNotificationsManager.notify(AppNotificationManager.TRANSFER_NOTIFICATION_ID, builder.build())
  93. }
  94. override fun cancelTransferNotification() {
  95. platformNotificationsManager.cancel(AppNotificationManager.TRANSFER_NOTIFICATION_ID)
  96. }
  97. }