diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.java index 1400185afa..13310d70a8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.java @@ -31,7 +31,7 @@ public class DummyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); - startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification()); + startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification()); return START_STICKY; } @@ -48,8 +48,7 @@ public class DummyService extends Service { super.onCreate(); // TODO: I guess this was moved here in order to adhere to the 5 seconds rule to call "startForeground" after a Service was called as Foreground service? // As onCreate() is not called every time a service is started, copied to onStartCommand(). - Notification notification = PersistentNotificationPlugin.getPlugin().updateNotification(); - startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, notification); + startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification()); MainApp.bus().register(this); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java index 3e23bd9b16..655435f400 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java @@ -89,10 +89,10 @@ public class PersistentNotificationPlugin extends PluginBase { @Override protected void onStart() { + super.onStart(); createNotificationChannel(); // make sure channels exist before triggering updates through the bus MainApp.bus().register(this); - triggerNotificationUpdate(); - super.onStart(); + triggerNotificationUpdate(true); } private void createNotificationChannel() { @@ -114,21 +114,23 @@ public class PersistentNotificationPlugin extends PluginBase { super.onStop(); } - private void triggerNotificationUpdate() { + private void triggerNotificationUpdate(boolean boot) { + updateNotification(boot); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) MainApp.instance().startForegroundService(new Intent(MainApp.instance(), DummyService.class)); else MainApp.instance().startService(new Intent(MainApp.instance(), DummyService.class)); } - @Nonnull - public Notification updateNotification() { - String line1 = null; + private void updateNotification(boolean boot) { + String line1; String line2 = null; String line3 = null; NotificationCompat.CarExtender.UnreadConversation.Builder unreadConversationBuilder = null; - if (ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null && ProfileFunctions.getInstance().isProfileValid("Notification")) { + if (boot) { + line1 = MainApp.gs(R.string.loading); + } else if (ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null && ProfileFunctions.getInstance().isProfileValid("Notification")) { String line1_aa; String units = ProfileFunctions.getInstance().getProfileUnits(); @@ -214,6 +216,8 @@ public class PersistentNotificationPlugin extends PluginBase { /// Add dot to produce a "more natural sounding result" unreadConversationBuilder.addMessage(line3_aa); /// End Android Auto + } else { + line1 = MainApp.gs(R.string.noprofileset); } NotificationCompat.Builder builder = new NotificationCompat.Builder(MainApp.instance(), CHANNEL_ID); @@ -223,9 +227,9 @@ public class PersistentNotificationPlugin extends PluginBase { builder.setSmallIcon(MainApp.getNotificationIcon()); Bitmap largeIcon = BitmapFactory.decodeResource(MainApp.instance().getResources(), MainApp.getIcon()); builder.setLargeIcon(largeIcon); - builder.setContentTitle(line1 != null ? line1 : MainApp.gs(R.string.noprofileset)); - builder.setContentText(line2 != null ? line2 : MainApp.gs(R.string.noprofileset)); - builder.setSubText(line3 != null ? line3 : MainApp.gs(R.string.noprofileset)); + if (line1 != null) builder.setContentTitle(line1); + if (line2 != null) builder.setContentText(line2); + if (line3 != null) builder.setSubText(line3); /// Android Auto if (unreadConversationBuilder != null) { builder.extend(new NotificationCompat.CarExtender() @@ -251,7 +255,6 @@ public class PersistentNotificationPlugin extends PluginBase { android.app.Notification notification = builder.build(); mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification); this.notification = notification; - return notification; } private String deltastring(double deltaMGDL, double deltaMMOL, String units) { @@ -278,48 +281,50 @@ public class PersistentNotificationPlugin extends PluginBase { public Notification getLastNotification() { if (notification != null) return notification; - else return new Notification(); + else { + throw new IllegalStateException("Notification is null"); + } } @Subscribe public void onStatusEvent(final EventPreferenceChange ev) { - triggerNotificationUpdate(); + triggerNotificationUpdate(false); } @Subscribe public void onStatusEvent(final EventTreatmentChange ev) { - triggerNotificationUpdate(); + triggerNotificationUpdate(false); } @Subscribe public void onStatusEvent(final EventTempBasalChange ev) { - triggerNotificationUpdate(); + triggerNotificationUpdate(false); } @Subscribe public void onStatusEvent(final EventExtendedBolusChange ev) { - triggerNotificationUpdate(); + triggerNotificationUpdate(false); } @Subscribe public void onStatusEvent(final EventAutosensCalculationFinished ev) { - triggerNotificationUpdate(); + triggerNotificationUpdate(false); } @Subscribe public void onStatusEvent(final EventNewBasalProfile ev) { - triggerNotificationUpdate(); + triggerNotificationUpdate(false); } @Subscribe public void onStatusEvent(final EventInitializationChanged ev) { - triggerNotificationUpdate(); + triggerNotificationUpdate(false); } @Subscribe public void onStatusEvent(final EventRefreshOverview ev) { - triggerNotificationUpdate(); + triggerNotificationUpdate(false); } } diff --git a/app/src/main/java/info/nightscout/androidaps/services/LocationService.java b/app/src/main/java/info/nightscout/androidaps/services/LocationService.java index 485ad1823a..33f92d770a 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/LocationService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/LocationService.java @@ -11,10 +11,14 @@ import android.os.IBinder; import androidx.core.app.ActivityCompat; +import com.squareup.otto.Subscribe; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventLocationChange; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.bus.RxBus; @@ -80,14 +84,14 @@ public class LocationService extends Service { super.onStartCommand(intent, flags, startId); if (L.isEnabled(L.LOCATION)) log.debug("onStartCommand"); - startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification()); + startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification()); return START_STICKY; } @Override public void onCreate() { super.onCreate(); - startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification()); + startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification()); if (L.isEnabled(L.LOCATION)) log.debug("onCreate"); @@ -121,6 +125,7 @@ public class LocationService extends Service { } catch (IllegalArgumentException ex) { log.error("network provider does not exist, " + ex.getMessage()); } + MainApp.bus().register(this); } @Override @@ -138,6 +143,15 @@ public class LocationService extends Service { log.error("fail to remove location listener, ignore", ex); } } + MainApp.bus().unregister(this); + } + + @Subscribe + public void onStatusEvent(EventAppExit event) { + if (L.isEnabled(L.CORE)) + log.debug("EventAppExit received"); + + stopSelf(); } private void initializeLocationManager() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2bb2753b66..4ced519ac1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1668,6 +1668,7 @@ Bolus constraint applied: %2$.2fU to %3$.2fU]]> !!!!! Slow carbs absorption detected: %2$d%% of time. Double check your calculation. COB can be really off !!!!!]]> %1$.0f / %2$d U + Loading ... %1$d day