Skip to content

Commit

Permalink
Analyzer: Foreground service service type
Browse files Browse the repository at this point in the history
  • Loading branch information
gerhardol committed Jun 14, 2024
1 parent b18bf3f commit 9ee83a3
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.runnerup.notification;

import android.Manifest;
import android.app.Notification;
import android.app.Service;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Build;

import androidx.core.app.ServiceCompat;
import androidx.core.content.ContextCompat;


public class ForegroundNotificationDisplayStrategy implements NotificationDisplayStrategy {
Expand All @@ -13,11 +20,21 @@ public ForegroundNotificationDisplayStrategy(Service service) {

@Override
public void notify(int notificationId, Notification notification) {
service.startForeground(notificationId, notification);
int type = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (ContextCompat.checkSelfPermission(service, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
type = ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE
&& ContextCompat.checkSelfPermission(service, Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_GRANTED) {
type |= ServiceInfo.FOREGROUND_SERVICE_TYPE_HEALTH;
}
}
ServiceCompat.startForeground(service, notificationId, notification, type);
}

@Override
public void cancel(int notificationId) {
service.stopForeground(true);
ServiceCompat.stopForeground(service, ServiceCompat.STOP_FOREGROUND_REMOVE);
}
}

0 comments on commit 9ee83a3

Please sign in to comment.