Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Making sure we shorten delay between call to service and its start
  • Loading branch information
ThomasSession committed Apr 7, 2026
commit 604794649e47a691aa34d44214850f4e99ff6ef6
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package org.thoughtcrime.securesms.service

import android.Manifest
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder
import android.os.SystemClock
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.IntentCompat
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.session.libsession.utilities.Address
import org.session.libsession.utilities.recipients.Recipient
import org.session.libsignal.utilities.Log
Expand All @@ -32,6 +42,8 @@ class CallForegroundService : Service() {
@Inject
lateinit var notificationManager: NotificationManagerCompat

private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)

companion object {
const val EXTRA_RECIPIENT_ADDRESS = "RECIPIENT_ID"
const val EXTRA_TYPE = "CALL_STEP_TYPE"
Expand All @@ -51,27 +63,58 @@ class CallForegroundService : Service() {
return recipientRepository.getRecipientSync(remoteAddress)
}

private fun startForeground(type: Int, recipient: Recipient?) {
if (notificationManager.areNotificationsEnabled()) {
private fun buildNotification(type: Int, recipient: Recipient?) =
CallNotificationBuilder.getCallInProgressNotification(
this,
type,
recipient,
notificationChannelManager
)

private fun startForeground(type: Int): Boolean {
try {
ServiceCompat.startForeground(
this,
NotificationId.WEBRTC_CALL,
buildNotification(type, recipient = null),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL or ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
} else {
0
}
)
return true
} catch (e: IllegalStateException) {
Log.e("", "Failed to start CallForegroundService for type: ${type}", e)
}

stopSelf()
return false
}

private fun updateNotificationWithRecipient(intent: Intent, type: Int) {
serviceScope.launch {
val recipient = withContext(Dispatchers.IO) {
getRemoteRecipient(intent)
}

if (ActivityCompat.checkSelfPermission(
this@CallForegroundService,
Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
return@launch
}

try {
ServiceCompat.startForeground(
this,
notificationManager.notify(
NotificationId.WEBRTC_CALL,
CallNotificationBuilder.getCallInProgressNotification(this, type, recipient, notificationChannelManager),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL or ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
} else {
0
}
buildNotification(type, recipient)
)
return
} catch (e: IllegalStateException) {
Log.e("", "Failed to setCallInProgressNotification as a foreground service for type: ${type}", e)
} catch (e: SecurityException) {
Log.w("", "Failed to update call notification", e)
}
}

// if we failed to start in foreground, stop service
stopSelf()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Expand All @@ -80,14 +123,22 @@ class CallForegroundService : Service() {
Log.d("", "CallForegroundService onStartCommand: ${intent}")

// check if the intent has the appropriate data to start this service, otherwise stop
if(intent?.hasExtra(EXTRA_TYPE) == true){
startForeground(intent.getIntExtra(EXTRA_TYPE, TYPE_INCOMING_CONNECTING), getRemoteRecipient(intent))
if (intent?.hasExtra(EXTRA_TYPE) == true) {
val type = intent.getIntExtra(EXTRA_TYPE, TYPE_INCOMING_CONNECTING)
if (startForeground(type)) {
updateNotificationWithRecipient(intent, type)
}
} else {
stopSelf()
}

return START_STICKY
}

override fun onDestroy() {
serviceScope.cancel()
super.onDestroy()
}

override fun onBind(intent: Intent?): IBinder? = null
}
}