prevent Missing BG notification on start

This commit is contained in:
Milos Kozak 2021-11-01 18:43:07 +01:00
parent 6f6b379156
commit f700c22966

View file

@ -22,6 +22,7 @@ import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -42,14 +43,15 @@ class PersistentNotificationPlugin @Inject constructor(
private val dummyServiceHelper: DummyServiceHelper, private val dummyServiceHelper: DummyServiceHelper,
private val iconsProvider: IconsProvider, private val iconsProvider: IconsProvider,
private val glucoseStatusProvider: GlucoseStatusProvider private val glucoseStatusProvider: GlucoseStatusProvider
) : PluginBase(PluginDescription() ) : PluginBase(
.mainType(PluginType.GENERAL) PluginDescription()
.neverVisible(true) .mainType(PluginType.GENERAL)
.pluginName(R.string.ongoingnotificaction) .neverVisible(true)
.enableByDefault(true) .pluginName(R.string.ongoingnotificaction)
.alwaysEnabled(true) .enableByDefault(true)
.showInList(false) .alwaysEnabled(true)
.description(R.string.description_persistent_notification), .showInList(false)
.description(R.string.description_persistent_notification),
aapsLogger, resourceHelper, injector aapsLogger, resourceHelper, injector
) { ) {
@ -63,49 +65,48 @@ class PersistentNotificationPlugin @Inject constructor(
// End Android auto // End Android auto
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
private var channel: NotificationChannel? = null
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
createNotificationChannel() // make sure channels exist before triggering updates through the bus disposable += rxBus
disposable.add(rxBus
.toObservable(EventRefreshOverview::class.java) .toObservable(EventRefreshOverview::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)) .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)
disposable.add(rxBus disposable += rxBus
.toObservable(EventExtendedBolusChange::class.java) .toObservable(EventExtendedBolusChange::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)) .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)
disposable.add(rxBus disposable += rxBus
.toObservable(EventTempBasalChange::class.java) .toObservable(EventTempBasalChange::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)) .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)
disposable.add(rxBus disposable += rxBus
.toObservable(EventTreatmentChange::class.java) .toObservable(EventTreatmentChange::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)) .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)
disposable.add(rxBus disposable += rxBus
.toObservable(EventInitializationChanged::class.java) .toObservable(EventInitializationChanged::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)) .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)
disposable.add(rxBus disposable += rxBus
.toObservable(EventEffectiveProfileSwitchChanged::class.java) .toObservable(EventEffectiveProfileSwitchChanged::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)) .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)
disposable.add(rxBus disposable += rxBus
.toObservable(EventAutosensCalculationFinished::class.java) .toObservable(EventAutosensCalculationFinished::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)) .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)
disposable.add(rxBus disposable += rxBus
.toObservable(EventPreferenceChange::class.java) .toObservable(EventPreferenceChange::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)) .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)
triggerNotificationUpdate()
} }
private fun createNotificationChannel() { private fun createNotificationChannel() {
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(notificationHolder.channelID, notificationHolder.channelID as CharSequence, NotificationManager.IMPORTANCE_HIGH) channel = NotificationChannel(notificationHolder.channelID, notificationHolder.channelID as CharSequence, NotificationManager.IMPORTANCE_HIGH)
mNotificationManager.createNotificationChannel(channel) channel?.let { mNotificationManager.createNotificationChannel(it) }
} }
override fun onStop() { override fun onStop() {
@ -115,6 +116,8 @@ class PersistentNotificationPlugin @Inject constructor(
} }
private fun triggerNotificationUpdate() { private fun triggerNotificationUpdate() {
if (channel == null)
createNotificationChannel() // make sure channels exist before triggering updates through the bus
updateNotification() updateNotification()
dummyServiceHelper.startService(context) dummyServiceHelper.startService(context)
} }
@ -155,8 +158,16 @@ class PersistentNotificationPlugin @Inject constructor(
//IOB //IOB
val bolusIob = iobCobCalculator.calculateIobFromBolus().round() val bolusIob = iobCobCalculator.calculateIobFromBolus().round()
val basalIob = iobCobCalculator.calculateIobFromTempBasalsIncludingConvertedExtended().round() val basalIob = iobCobCalculator.calculateIobFromTempBasalsIncludingConvertedExtended().round()
line2 = resourceHelper.gs(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U " + resourceHelper.gs(R.string.cob) + ": " + iobCobCalculator.getCobInfo(false, "PersistentNotificationPlugin").generateCOBString() line2 =
val line2aa = resourceHelper.gs(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U. " + resourceHelper.gs(R.string.cob) + ": " + iobCobCalculator.getCobInfo(false, "PersistentNotificationPlugin").generateCOBString() + "." resourceHelper.gs(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U " + resourceHelper.gs(R.string.cob) + ": " + iobCobCalculator.getCobInfo(
false,
"PersistentNotificationPlugin"
).generateCOBString()
val line2aa =
resourceHelper.gs(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U. " + resourceHelper.gs(R.string.cob) + ": " + iobCobCalculator.getCobInfo(
false,
"PersistentNotificationPlugin"
).generateCOBString() + "."
line3 = DecimalFormatter.to2Decimal(pump.baseBasalRate) + " U/h" line3 = DecimalFormatter.to2Decimal(pump.baseBasalRate) + " U/h"
var line3aa = DecimalFormatter.to2Decimal(pump.baseBasalRate) + " U/h." var line3aa = DecimalFormatter.to2Decimal(pump.baseBasalRate) + " U/h."
line3 += " - " + profileFunction.getProfileName() line3 += " - " + profileFunction.getProfileName()
@ -167,10 +178,12 @@ class PersistentNotificationPlugin @Inject constructor(
.setAction(READ_ACTION) .setAction(READ_ACTION)
.putExtra(CONVERSATION_ID, notificationHolder.notificationID) .putExtra(CONVERSATION_ID, notificationHolder.notificationID)
.setPackage(PACKAGE) .setPackage(PACKAGE)
val msgReadPendingIntent = PendingIntent.getBroadcast(context, val msgReadPendingIntent = PendingIntent.getBroadcast(
context,
notificationHolder.notificationID, notificationHolder.notificationID,
msgReadIntent, msgReadIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
val msgReplyIntent = Intent() val msgReplyIntent = Intent()
.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
.setAction(REPLY_ACTION) .setAction(REPLY_ACTION)
@ -180,7 +193,8 @@ class PersistentNotificationPlugin @Inject constructor(
context, context,
notificationHolder.notificationID, notificationHolder.notificationID,
msgReplyIntent, msgReplyIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
// Build a RemoteInput for receiving voice input from devices // Build a RemoteInput for receiving voice input from devices
val remoteInput = RemoteInput.Builder(EXTRA_VOICE_REPLY).build() val remoteInput = RemoteInput.Builder(EXTRA_VOICE_REPLY).build()
// Create the UnreadConversation // Create the UnreadConversation
@ -205,8 +219,10 @@ class PersistentNotificationPlugin @Inject constructor(
if (line3 != null) builder.setSubText(line3) if (line3 != null) builder.setSubText(line3)
/// Android Auto /// Android Auto
if (unreadConversationBuilder != null) { if (unreadConversationBuilder != null) {
builder.extend(NotificationCompat.CarExtender() builder.extend(
.setUnreadConversation(unreadConversationBuilder.build())) NotificationCompat.CarExtender()
.setUnreadConversation(unreadConversationBuilder.build())
)
} }
/// End Android Auto /// End Android Auto
builder.setContentIntent(notificationHolder.openAppIntent(context)) builder.setContentIntent(notificationHolder.openAppIntent(context))