Cleaning up foreground service calls

This commit is contained in:
ThomasSession 2024-08-01 16:50:20 +10:00
parent 33f23e5fa1
commit 2f135a6bf6
2 changed files with 24 additions and 7 deletions

View File

@ -35,6 +35,7 @@ import android.os.SystemClock;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat;
import org.session.libsession.utilities.ServiceUtil; import org.session.libsession.utilities.ServiceUtil;
import org.session.libsession.utilities.TextSecurePreferences; import org.session.libsession.utilities.TextSecurePreferences;
@ -252,11 +253,18 @@ public class KeyCachingService extends Service {
builder.setContentIntent(buildLaunchIntent()); builder.setContentIntent(buildLaunchIntent());
stopForeground(true); stopForeground(true);
if (Build.VERSION.SDK_INT >= 34) {
startForeground(SERVICE_RUNNING_ID, builder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE); int type = 0;
} else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startForeground(SERVICE_RUNNING_ID, builder.build()); type = ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE;
} }
ServiceCompat.startForeground(
this,
SERVICE_RUNNING_ID,
builder.build(),
type
);
} }
private PendingIntent buildLockIntent() { private PendingIntent buildLockIntent() {

View File

@ -7,9 +7,12 @@ import android.content.Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.content.IntentFilter import android.content.IntentFilter
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.media.AudioManager import android.media.AudioManager
import android.os.Build
import android.os.ResultReceiver import android.os.ResultReceiver
import android.telephony.TelephonyManager import android.telephony.TelephonyManager
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.lifecycle.LifecycleService import androidx.lifecycle.LifecycleService
@ -723,9 +726,15 @@ class WebRtcCallService : LifecycleService(), CallManager.WebRtcListener {
private fun setCallInProgressNotification(type: Int, recipient: Recipient?) { private fun setCallInProgressNotification(type: Int, recipient: Recipient?) {
try { try {
startForeground( ServiceCompat.startForeground(
this,
CallNotificationBuilder.WEBRTC_NOTIFICATION, 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) { } catch (e: IllegalStateException) {
Log.e(TAG, "Failed to setCallInProgressNotification as a foreground service for type: ${type}, trying to update instead", e) Log.e(TAG, "Failed to setCallInProgressNotification as a foreground service for type: ${type}, trying to update instead", e)