Merge pull request #1983 from MilosKozak/locationforeground

Location service foreground
This commit is contained in:
Milos Kozak 2019-08-24 19:03:51 +02:00 committed by GitHub
commit b5b62d4bb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View file

@ -55,14 +55,13 @@ public class PersistentNotificationPlugin extends PluginBase {
private Notification notification; private Notification notification;
public static PersistentNotificationPlugin getPlugin() { public static PersistentNotificationPlugin getPlugin() {
if (plugin == null) plugin = new PersistentNotificationPlugin(MainApp.instance()); if (plugin == null) plugin = new PersistentNotificationPlugin();
return plugin; return plugin;
} }
public static final String CHANNEL_ID = "AndroidAPS-Ongoing"; public static final String CHANNEL_ID = "AndroidAPS-Ongoing";
public static final int ONGOING_NOTIFICATION_ID = 4711; public static final int ONGOING_NOTIFICATION_ID = 4711;
private final Context ctx;
/// For Android Auto /// For Android Auto
/// Intents are not declared in manifest and not consumed, this is intentionally because actually we can't do anything with /// 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 /// End Android Auto
private PersistentNotificationPlugin(Context ctx) { private PersistentNotificationPlugin() {
super(new PluginDescription() super(new PluginDescription()
.mainType(PluginType.GENERAL) .mainType(PluginType.GENERAL)
.neverVisible(true) .neverVisible(true)
@ -86,7 +85,6 @@ public class PersistentNotificationPlugin extends PluginBase {
.showInList(false) .showInList(false)
.description(R.string.description_persistent_notification) .description(R.string.description_persistent_notification)
); );
this.ctx = ctx;
} }
@Override @Override
@ -101,7 +99,7 @@ public class PersistentNotificationPlugin extends PluginBase {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = NotificationManager mNotificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) MainApp.instance().getSystemService(Context.NOTIFICATION_SERVICE);
@SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(CHANNEL_ID, @SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_ID, CHANNEL_ID,
NotificationManager.IMPORTANCE_HIGH); NotificationManager.IMPORTANCE_HIGH);
@ -186,7 +184,7 @@ public class PersistentNotificationPlugin extends PluginBase {
.setPackage(PACKAGE); .setPackage(PACKAGE);
PendingIntent msgReadPendingIntent = PendingIntent msgReadPendingIntent =
PendingIntent.getBroadcast(ctx, PendingIntent.getBroadcast(MainApp.instance(),
ONGOING_NOTIFICATION_ID, ONGOING_NOTIFICATION_ID,
msgReadIntent, msgReadIntent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
@ -198,7 +196,7 @@ public class PersistentNotificationPlugin extends PluginBase {
.setPackage(PACKAGE); .setPackage(PACKAGE);
PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast( PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast(
ctx, MainApp.instance(),
ONGOING_NOTIFICATION_ID, ONGOING_NOTIFICATION_ID,
msgReplyIntent, msgReplyIntent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
@ -218,12 +216,12 @@ public class PersistentNotificationPlugin extends PluginBase {
/// End Android Auto /// 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.setOngoing(true);
builder.setOnlyAlertOnce(true); builder.setOnlyAlertOnce(true);
builder.setCategory(NotificationCompat.CATEGORY_STATUS); builder.setCategory(NotificationCompat.CATEGORY_STATUS);
builder.setSmallIcon(MainApp.getNotificationIcon()); 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.setLargeIcon(largeIcon);
builder.setContentTitle(line1 != null ? line1 : MainApp.gs(R.string.noprofileset)); builder.setContentTitle(line1 != null ? line1 : MainApp.gs(R.string.noprofileset));
builder.setContentText(line2 != null ? line2 : 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 /// 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.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent); stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = PendingIntent resultPendingIntent =
@ -248,7 +246,7 @@ public class PersistentNotificationPlugin extends PluginBase {
); );
builder.setContentIntent(resultPendingIntent); builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = NotificationManager mNotificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) MainApp.instance().getSystemService(Context.NOTIFICATION_SERVICE);
android.app.Notification notification = builder.build(); android.app.Notification notification = builder.build();
mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification); mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification);

View file

@ -86,6 +86,8 @@ public class LocationService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate();
startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification());
if (L.isEnabled(L.LOCATION)) if (L.isEnabled(L.LOCATION))
log.debug("onCreate"); log.debug("onCreate");