From 2f135a6bf6b754202d020d80ae28f6ef8c4f9a13 Mon Sep 17 00:00:00 2001 From: ThomasSession Date: Thu, 1 Aug 2024 16:50:20 +1000 Subject: [PATCH] Cleaning up foreground service calls --- .../securesms/service/KeyCachingService.java | 18 +++++++++++++----- .../securesms/service/WebRtcCallService.kt | 13 +++++++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/service/KeyCachingService.java b/app/src/main/java/org/thoughtcrime/securesms/service/KeyCachingService.java index f919af7ad6..47b5c52f16 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/KeyCachingService.java +++ b/app/src/main/java/org/thoughtcrime/securesms/service/KeyCachingService.java @@ -35,6 +35,7 @@ import android.os.SystemClock; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; +import androidx.core.app.ServiceCompat; import org.session.libsession.utilities.ServiceUtil; import org.session.libsession.utilities.TextSecurePreferences; @@ -122,7 +123,7 @@ public class KeyCachingService extends Service { KeyCachingService.masterSecret = masterSecret; foregroundService(); - + new AsyncTask() { @Override protected Void doInBackground(Void... params) { @@ -252,11 +253,18 @@ public class KeyCachingService extends Service { builder.setContentIntent(buildLaunchIntent()); stopForeground(true); - if (Build.VERSION.SDK_INT >= 34) { - startForeground(SERVICE_RUNNING_ID, builder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE); - } else { - startForeground(SERVICE_RUNNING_ID, builder.build()); + + int type = 0; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + type = ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE; } + + ServiceCompat.startForeground( + this, + SERVICE_RUNNING_ID, + builder.build(), + type + ); } private PendingIntent buildLockIntent() { diff --git a/app/src/main/java/org/thoughtcrime/securesms/service/WebRtcCallService.kt b/app/src/main/java/org/thoughtcrime/securesms/service/WebRtcCallService.kt index f40a075d8d..6ee33721e8 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/WebRtcCallService.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/service/WebRtcCallService.kt @@ -7,9 +7,12 @@ import android.content.Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT import android.content.Intent.FLAG_ACTIVITY_NEW_TASK import android.content.IntentFilter import android.content.pm.PackageManager +import android.content.pm.ServiceInfo import android.media.AudioManager +import android.os.Build import android.os.ResultReceiver import android.telephony.TelephonyManager +import androidx.core.app.ServiceCompat import androidx.core.content.ContextCompat import androidx.core.os.bundleOf import androidx.lifecycle.LifecycleService @@ -723,9 +726,15 @@ class WebRtcCallService : LifecycleService(), CallManager.WebRtcListener { private fun setCallInProgressNotification(type: Int, recipient: Recipient?) { try { - startForeground( + ServiceCompat.startForeground( + this, CallNotificationBuilder.WEBRTC_NOTIFICATION, - CallNotificationBuilder.getCallInProgressNotification(this, type, recipient) + CallNotificationBuilder.getCallInProgressNotification(this, type, recipient), + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE + } else { + 0 + } ) } catch (e: IllegalStateException) { Log.e(TAG, "Failed to setCallInProgressNotification as a foreground service for type: ${type}, trying to update instead", e)