Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.app.Notification
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.os.SystemClock
import androidx.core.app.NotificationManagerCompat
import androidx.media.session.MediaButtonReceiver
import io.getstream.log.taggedLogger
Expand Down Expand Up @@ -53,9 +54,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import org.threeten.bp.Duration
import org.threeten.bp.OffsetDateTime
import kotlin.math.absoluteValue
import java.util.concurrent.TimeUnit

/**
* A foreground service that is running when there is an active call.
Expand Down Expand Up @@ -117,7 +116,7 @@ internal open class CallService : Service() {

override fun onCreate() {
super.onCreate()
serviceStateController.setStartTime(OffsetDateTime.now())
serviceStateController.setStartTime(SystemClock.elapsedRealtime())
}

private fun shouldStopService(intent: Intent?): Boolean {
Expand Down Expand Up @@ -616,11 +615,10 @@ internal open class CallService : Service() {
logger.w { "[stopServiceGracefully] source: $source" }
}

serviceStateController.startTime?.let { startTime ->
serviceStateController.startTimeElapsedRealtime?.let { startTime ->

val currentTime = OffsetDateTime.now()
val duration = Duration.between(startTime, currentTime)
val differenceInSeconds = duration.seconds.absoluteValue
val elapsedMs = SystemClock.elapsedRealtime() - startTime
val differenceInSeconds = TimeUnit.MILLISECONDS.toSeconds(elapsedMs)
val debouncerThresholdTimeInSeconds = SERVICE_DESTROY_THRESHOLD_TIME_MS / 1_000
logger.d { "[stopServiceGracefully] differenceInSeconds: $differenceInSeconds" }
if (differenceInSeconds >= debouncerThresholdTimeInSeconds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import org.threeten.bp.OffsetDateTime

internal class ServiceStateController {
private val _state = MutableStateFlow(ServiceStateSnapshot())
Expand All @@ -42,8 +41,8 @@ internal class ServiceStateController {
val soundPlayer: CallSoundAndVibrationPlayer?
get() = state.value.soundPlayer

val startTime: OffsetDateTime?
get() = state.value.startTime
val startTimeElapsedRealtime: Long?
get() = state.value.startTimeElapsedRealtime

fun setCurrentCallId(callId: StreamCallId) {
_state.update { it.copy(currentCallId = callId) }
Expand All @@ -57,8 +56,8 @@ internal class ServiceStateController {
_state.update { it.copy(soundPlayer = player) }
}

fun setStartTime(time: OffsetDateTime) {
_state.update { it.copy(startTime = time) }
fun setStartTime(elapsedRealtime: Long) {
_state.update { it.copy(startTimeElapsedRealtime = elapsedRealtime) }
}

fun registerToggleCameraBroadcastReceiver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ package io.getstream.video.android.core.notifications.internal.service.models
import io.getstream.video.android.core.notifications.internal.receivers.ToggleCameraBroadcastReceiver
import io.getstream.video.android.core.sounds.CallSoundAndVibrationPlayer
import io.getstream.video.android.model.StreamCallId
import org.threeten.bp.OffsetDateTime

internal data class ServiceStateSnapshot(
val currentCallId: StreamCallId? = null,
val notificationId: Int? = null,
val soundPlayer: CallSoundAndVibrationPlayer? = null,
val toggleCameraBroadcastReceiver: ToggleCameraBroadcastReceiver? = null,
val isReceiverRegistered: Boolean = false,
val startTime: OffsetDateTime? = null,
val startTimeElapsedRealtime: Long? = null,
)
Loading