default to microphone foreground service type during call

This commit is contained in:
Daniel Gultsch 2024-02-10 13:46:07 +01:00
parent 6ce052a337
commit 9f1c4a42f8
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -1444,7 +1444,7 @@ public class XmppConnectionService extends Service {
toggleForegroundService(false); toggleForegroundService(false);
} }
private void toggleForegroundService(boolean force) { private void toggleForegroundService(final boolean force) {
final boolean status; final boolean status;
final OngoingCall ongoing = ongoingCall.get(); final OngoingCall ongoing = ongoingCall.get();
if (force || mForceDuringOnCreate.get() || mForceForegroundService.get() || ongoing != null || (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) { if (force || mForceDuringOnCreate.get() || mForceForegroundService.get() || ongoing != null || (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) {
@ -1453,12 +1453,12 @@ public class XmppConnectionService extends Service {
if (ongoing != null) { if (ongoing != null) {
notification = this.mNotificationService.getOngoingCallNotification(ongoing); notification = this.mNotificationService.getOngoingCallNotification(ongoing);
id = NotificationService.ONGOING_CALL_NOTIFICATION_ID; id = NotificationService.ONGOING_CALL_NOTIFICATION_ID;
startForegroundOrCatch(id, notification); startForegroundOrCatch(id, notification, true);
mNotificationService.cancel(NotificationService.FOREGROUND_NOTIFICATION_ID); mNotificationService.cancel(NotificationService.FOREGROUND_NOTIFICATION_ID);
} else { } else {
notification = this.mNotificationService.createForegroundNotification(); notification = this.mNotificationService.createForegroundNotification();
id = NotificationService.FOREGROUND_NOTIFICATION_ID; id = NotificationService.FOREGROUND_NOTIFICATION_ID;
startForegroundOrCatch(id, notification); startForegroundOrCatch(id, notification, false);
} }
if (!mForceForegroundService.get()) { if (!mForceForegroundService.get()) {
@ -1478,11 +1478,17 @@ public class XmppConnectionService extends Service {
Log.d(Config.LOGTAG, "ForegroundService: " + (status ? "on" : "off")); Log.d(Config.LOGTAG, "ForegroundService: " + (status ? "on" : "off"));
} }
private void startForegroundOrCatch(final int id, final Notification notification) { private void startForegroundOrCatch(
final int id, final Notification notification, final boolean requireMicrophone) {
try { try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
final int foregroundServiceType; final int foregroundServiceType;
if (getSystemService(PowerManager.class) if (requireMicrophone
&& ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED) {
foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE;
Log.d(Config.LOGTAG, "defaulting to microphone foreground service type");
} else if (getSystemService(PowerManager.class)
.isIgnoringBatteryOptimizations(getPackageName())) { .isIgnoringBatteryOptimizations(getPackageName())) {
foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED; foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED;
} else if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)