From 85b2d2aeac8f77d02dbbb4395a5c846469fafb5c Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sat, 20 Nov 2021 22:18:25 +0100 Subject: [PATCH 1/3] Update NotificationHolder.kt --- .../nightscout/androidaps/interfaces/NotificationHolder.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/NotificationHolder.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/NotificationHolder.kt index b2f11d9d1c..8a052ba3c5 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/NotificationHolder.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/NotificationHolder.kt @@ -10,4 +10,5 @@ interface NotificationHolder { var notification: Notification fun openAppIntent(context: Context): PendingIntent? -} \ No newline at end of file + fun createNotificationChannel() +} From e4c3e4c5604dc3b5bdd911274220e412e3f70d65 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sat, 20 Nov 2021 22:20:08 +0100 Subject: [PATCH 2/3] Update NotificationHolderImpl.kt --- .../NotificationHolderImpl.kt | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/androidNotification/NotificationHolderImpl.kt b/app/src/main/java/info/nightscout/androidaps/utils/androidNotification/NotificationHolderImpl.kt index 1599cddb79..4345775d02 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/androidNotification/NotificationHolderImpl.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/androidNotification/NotificationHolderImpl.kt @@ -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) + } } From f668cc4af31058087d7e9a26bf5cdaa7fb849e1a Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sat, 20 Nov 2021 22:20:54 +0100 Subject: [PATCH 3/3] Update PersistentNotificationPlugin.kt --- .../PersistentNotificationPlugin.kt | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.kt index 7304cffa71..6d2b6cc4a2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.kt @@ -65,10 +65,10 @@ class PersistentNotificationPlugin @Inject constructor( // End Android auto private val disposable = CompositeDisposable() - private var channel: NotificationChannel? = null override fun onStart() { super.onStart() + notificationHolder.createNotificationChannel() disposable += rxBus .toObservable(EventRefreshOverview::class.java) .observeOn(aapsSchedulers.io) @@ -103,12 +103,6 @@ class PersistentNotificationPlugin @Inject constructor( .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException) } - private fun createNotificationChannel() { - val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - channel = NotificationChannel(notificationHolder.channelID, notificationHolder.channelID as CharSequence, NotificationManager.IMPORTANCE_HIGH) - channel?.let { mNotificationManager.createNotificationChannel(it) } - } - override fun onStop() { disposable.clear() dummyServiceHelper.stopService(context) @@ -116,8 +110,6 @@ class PersistentNotificationPlugin @Inject constructor( } private fun triggerNotificationUpdate() { - if (channel == null) - createNotificationChannel() // make sure channels exist before triggering updates through the bus updateNotification() dummyServiceHelper.startService(context) } @@ -231,4 +223,4 @@ class PersistentNotificationPlugin @Inject constructor( mNotificationManager.notify(notificationHolder.notificationID, notification) notificationHolder.notification = notification } -} \ No newline at end of file +}