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.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<Void, Void, Void>() {
@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() {

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.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)