From a590a8be42587d000494673328dfd178157fe113 Mon Sep 17 00:00:00 2001 From: TebbeUbben Date: Sun, 17 Jun 2018 23:33:32 +0200 Subject: [PATCH] Foreground service --- app/src/main/AndroidManifest.xml | 2 + .../info/nightscout/androidaps/MainApp.java | 2 +- .../ConfigBuilder/ConfigBuilderPlugin.java | 8 ++++ .../Persistentnotification/DummyService.java | 28 +++++++++++ .../PersistentNotificationPlugin.java | 46 +++++++++++-------- 5 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/DummyService.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9f7af3102b..bcc1e93abd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -166,6 +166,8 @@ android:name=".plugins.Overview.notifications.DismissNotificationService" android:exported="false" /> + + diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 7f4a6e4a9c..49c13c4ef5 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -187,7 +187,7 @@ public class MainApp extends Application { pluginsList.add(WearPlugin.initPlugin(this)); pluginsList.add(StatuslinePlugin.initPlugin(this)); - pluginsList.add(new PersistentNotificationPlugin(this)); + pluginsList.add(PersistentNotificationPlugin.getPlugin()); pluginsList.add(NSClientPlugin.getPlugin()); pluginsList.add(sConfigBuilder = ConfigBuilderPlugin.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 1911f588b1..ab5f48bbf8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -113,9 +113,17 @@ public class ConfigBuilderPlugin extends PluginBase { pluginList = MainApp.getPluginsList(); upgradeSettings(); loadSettings(); + setAlwaysEnabledPluginsEnabled(); MainApp.bus().post(new EventAppInitialized()); } + private void setAlwaysEnabledPluginsEnabled() { + for (PluginBase plugin : pluginList) { + if (plugin.pluginDescription.alwaysEnabled) plugin.setPluginEnabled(plugin.getType(), true); + } + storeSettings("setAlwaysEnabledPluginsEnabled"); + } + public void storeSettings(String from) { if (pluginList != null) { if (Config.logPrefsChange) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/DummyService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/DummyService.java new file mode 100644 index 0000000000..b49c2490ed --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/DummyService.java @@ -0,0 +1,28 @@ +package info.nightscout.androidaps.plugins.Persistentnotification; + +import android.app.Notification; +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; +import android.support.annotation.Nullable; +import android.util.Log; + +public class DummyService extends Service { + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + Notification notification = PersistentNotificationPlugin.getPlugin().updateNotification(); + if (notification != null) startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, notification); + return START_STICKY; + } + + @Override + public void onDestroy() { + stopForeground(true); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java index 5f6d50273a..3fc7371311 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.Persistentnotification; import android.annotation.SuppressLint; +import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; @@ -35,7 +36,6 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.IobCobCalculator.CobInfo; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DecimalFormatter; @@ -46,9 +46,16 @@ import info.nightscout.utils.DecimalFormatter; public class PersistentNotificationPlugin extends PluginBase { + private static PersistentNotificationPlugin plugin; + + public static PersistentNotificationPlugin getPlugin() { + if (plugin == null) plugin = new PersistentNotificationPlugin(MainApp.instance()); + return plugin; + } + public static final String CHANNEL_ID = "AndroidAPS-Ongoing"; - private static final int ONGOING_NOTIFICATION_ID = 4711; + public static final int ONGOING_NOTIFICATION_ID = 4711; private final Context ctx; public PersistentNotificationPlugin(Context ctx) { @@ -57,6 +64,7 @@ public class PersistentNotificationPlugin extends PluginBase { .neverVisible(true) .pluginName(R.string.ongoingnotificaction) .enableByDefault(true) + .alwaysEnabled(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) .description(R.string.description_persistent_notification) ); this.ctx = ctx; @@ -66,7 +74,7 @@ public class PersistentNotificationPlugin extends PluginBase { protected void onStart() { MainApp.bus().register(this); createNotificationChannel(); - updateNotification(); + triggerNotificationUpdate(); super.onStart(); } @@ -85,20 +93,22 @@ public class PersistentNotificationPlugin extends PluginBase { @Override protected void onStop() { MainApp.bus().unregister(this); - NotificationManager mNotificationManager = - (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); - mNotificationManager.cancel(ONGOING_NOTIFICATION_ID); + MainApp.instance().stopService(new Intent(MainApp.instance(), DummyService.class)); } - private void updateNotification() { + private void triggerNotificationUpdate() { + MainApp.instance().startService(new Intent(MainApp.instance(), DummyService.class)); + } + + Notification updateNotification() { if (!isEnabled(PluginType.GENERAL)) { - return; + return null; } String line1 = ""; if (MainApp.getConfigBuilder().getActiveProfileInterface() == null || !MainApp.getConfigBuilder().isProfileValid("Notificiation")) - return; + return null; String units = MainApp.getConfigBuilder().getProfileUnits(); @@ -166,7 +176,7 @@ public class PersistentNotificationPlugin extends PluginBase { android.app.Notification notification = builder.build(); mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification); - + return notification; } private String deltastring(double deltaMGDL, double deltaMMOL, String units) { @@ -188,42 +198,42 @@ public class PersistentNotificationPlugin extends PluginBase { @Subscribe public void onStatusEvent(final EventPreferenceChange ev) { - updateNotification(); + triggerNotificationUpdate(); } @Subscribe public void onStatusEvent(final EventTreatmentChange ev) { - updateNotification(); + triggerNotificationUpdate(); } @Subscribe public void onStatusEvent(final EventTempBasalChange ev) { - updateNotification(); + triggerNotificationUpdate(); } @Subscribe public void onStatusEvent(final EventExtendedBolusChange ev) { - updateNotification(); + triggerNotificationUpdate(); } @Subscribe public void onStatusEvent(final EventNewBG ev) { - updateNotification(); + triggerNotificationUpdate(); } @Subscribe public void onStatusEvent(final EventNewBasalProfile ev) { - updateNotification(); + triggerNotificationUpdate(); } @Subscribe public void onStatusEvent(final EventInitializationChanged ev) { - updateNotification(); + triggerNotificationUpdate(); } @Subscribe public void onStatusEvent(final EventRefreshOverview ev) { - updateNotification(); + triggerNotificationUpdate(); } }