Update NotificationHolderImpl.kt

This commit is contained in:
AdrianLxM 2021-11-20 22:20:08 +01:00 committed by GitHub
parent 85b2d2aeac
commit e4c3e4c560
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.utils.androidNotification
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
@ -17,29 +18,47 @@ import javax.inject.Singleton
@Singleton
class NotificationHolderImpl @Inject constructor(
rh: ResourceHelper,
context: Context,
iconsProvider: IconsProvider
val rh: ResourceHelper,
val context: Context,
val iconsProvider: IconsProvider
) : NotificationHolder {
override val channelID = "AndroidAPS-Ongoing"
override val notificationID = 4711
override var notification: Notification = NotificationCompat.Builder(context, channelID)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setSmallIcon(iconsProvider.getNotificationIcon())
.setLargeIcon(rh.decodeResource(iconsProvider.getIcon()))
.setContentTitle(rh.gs(R.string.loading))
.setContentIntent(openAppIntent(context))
.build()
.also {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(notificationID, it)
private var _notification: Notification? = null
override var notification: Notification
set(value) {
_notification = value
}
get() = _notification ?: placeholderNotification()
override fun openAppIntent(context: Context): PendingIntent? = TaskStackBuilder.create(context).run {
addParentStack(MainActivity::class.java)
addNextIntent(Intent(context, MainActivity::class.java))
getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun placeholderNotification(): Notification {
createNotificationChannel()
return NotificationCompat.Builder(context, channelID)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setSmallIcon(iconsProvider.getNotificationIcon())
.setLargeIcon(rh.decodeResource(iconsProvider.getIcon()))
.setContentTitle(rh.gs(R.string.loading))
.setContentIntent(openAppIntent(context))
.build()
.also {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(notificationID, it)
}
}
override fun createNotificationChannel() {
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(channelID, channelID as CharSequence, NotificationManager.IMPORTANCE_HIGH)
mNotificationManager.createNotificationChannel(channel)
}
}