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 8582a5e277..3e23bd9b16 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 @@ -55,14 +55,13 @@ public class PersistentNotificationPlugin extends PluginBase { private Notification notification; public static PersistentNotificationPlugin getPlugin() { - if (plugin == null) plugin = new PersistentNotificationPlugin(MainApp.instance()); + if (plugin == null) plugin = new PersistentNotificationPlugin(); return plugin; } public static final String CHANNEL_ID = "AndroidAPS-Ongoing"; public static final int ONGOING_NOTIFICATION_ID = 4711; - private final Context ctx; /// For Android Auto /// Intents are not declared in manifest and not consumed, this is intentionally because actually we can't do anything with @@ -76,7 +75,7 @@ public class PersistentNotificationPlugin extends PluginBase { /// End Android Auto - private PersistentNotificationPlugin(Context ctx) { + private PersistentNotificationPlugin() { super(new PluginDescription() .mainType(PluginType.GENERAL) .neverVisible(true) @@ -86,7 +85,6 @@ public class PersistentNotificationPlugin extends PluginBase { .showInList(false) .description(R.string.description_persistent_notification) ); - this.ctx = ctx; } @Override @@ -101,7 +99,7 @@ public class PersistentNotificationPlugin extends PluginBase { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager mNotificationManager = - (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); + (NotificationManager) MainApp.instance().getSystemService(Context.NOTIFICATION_SERVICE); @SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH); @@ -186,7 +184,7 @@ public class PersistentNotificationPlugin extends PluginBase { .setPackage(PACKAGE); PendingIntent msgReadPendingIntent = - PendingIntent.getBroadcast(ctx, + PendingIntent.getBroadcast(MainApp.instance(), ONGOING_NOTIFICATION_ID, msgReadIntent, PendingIntent.FLAG_UPDATE_CURRENT); @@ -198,7 +196,7 @@ public class PersistentNotificationPlugin extends PluginBase { .setPackage(PACKAGE); PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast( - ctx, + MainApp.instance(), ONGOING_NOTIFICATION_ID, msgReplyIntent, PendingIntent.FLAG_UPDATE_CURRENT); @@ -218,12 +216,12 @@ public class PersistentNotificationPlugin extends PluginBase { /// End Android Auto } - NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID); + NotificationCompat.Builder builder = new NotificationCompat.Builder(MainApp.instance(), CHANNEL_ID); builder.setOngoing(true); builder.setOnlyAlertOnce(true); builder.setCategory(NotificationCompat.CATEGORY_STATUS); builder.setSmallIcon(MainApp.getNotificationIcon()); - Bitmap largeIcon = BitmapFactory.decodeResource(ctx.getResources(), MainApp.getIcon()); + 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)); @@ -236,9 +234,9 @@ public class PersistentNotificationPlugin extends PluginBase { /// End Android Auto - Intent resultIntent = new Intent(ctx, MainActivity.class); + Intent resultIntent = new Intent(MainApp.instance(), MainActivity.class); - TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx); + TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainApp.instance()); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = @@ -248,7 +246,7 @@ public class PersistentNotificationPlugin extends PluginBase { ); builder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = - (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); + (NotificationManager) MainApp.instance().getSystemService(Context.NOTIFICATION_SERVICE); android.app.Notification notification = builder.build(); mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification); 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 feec4dcab8..485ad1823a 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/LocationService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/LocationService.java @@ -86,6 +86,8 @@ public class LocationService extends Service { @Override public void onCreate() { + super.onCreate(); + startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification()); if (L.isEnabled(L.LOCATION)) log.debug("onCreate");