diff --git a/app/src/main/java/org/thoughtcrime/securesms/service/TelephonyHandler.kt b/app/src/main/java/org/thoughtcrime/securesms/service/TelephonyHandler.kt new file mode 100644 index 0000000000..3e78c223fa --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/service/TelephonyHandler.kt @@ -0,0 +1,46 @@ +package org.thoughtcrime.securesms.service + +import android.os.Build +import android.telephony.PhoneStateListener +import android.telephony.PhoneStateListener.LISTEN_NONE +import android.telephony.TelephonyManager +import androidx.annotation.RequiresApi +import org.thoughtcrime.securesms.webrtc.HangUpRtcOnPstnCallAnsweredListener +import org.thoughtcrime.securesms.webrtc.HangUpRtcTelephonyCallback +import java.util.concurrent.ExecutorService + +internal interface TelephonyHandler { + fun register(telephonyManager: TelephonyManager) + fun unregister(telephonyManager: TelephonyManager) +} + +internal fun TelephonyHandler(serviceExecutor: ExecutorService, callback: () -> Unit) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + TelephonyHandlerV31(serviceExecutor, callback) +} else { + TelephonyHandlerV23(callback) +} + +@RequiresApi(Build.VERSION_CODES.S) +private class TelephonyHandlerV31(val serviceExecutor: ExecutorService, callback: () -> Unit): TelephonyHandler { + private val callback = HangUpRtcTelephonyCallback(callback) + + override fun register(telephonyManager: TelephonyManager) { + telephonyManager.registerTelephonyCallback(serviceExecutor, callback) + } + + override fun unregister(telephonyManager: TelephonyManager) { + telephonyManager.unregisterTelephonyCallback(callback) + } +} + +private class TelephonyHandlerV23(callback: () -> Unit): TelephonyHandler { + val callback = HangUpRtcOnPstnCallAnsweredListener(callback) + + override fun register(telephonyManager: TelephonyManager) { + telephonyManager.listen(callback, PhoneStateListener.LISTEN_CALL_STATE) + } + + override fun unregister(telephonyManager: TelephonyManager) { + telephonyManager.listen(callback, LISTEN_NONE) + } +} 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 27d2eec067..ffcf311d21 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/WebRtcCallService.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/service/WebRtcCallService.kt @@ -59,6 +59,7 @@ import org.webrtc.RtpReceiver import org.webrtc.SessionDescription import java.util.UUID import java.util.concurrent.ExecutionException +import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import java.util.concurrent.ScheduledFuture import java.util.concurrent.TimeUnit @@ -226,46 +227,11 @@ class WebRtcCallService : LifecycleService(), CallManager.WebRtcListener { private val serviceExecutor = Executors.newSingleThreadExecutor() private val timeoutExecutor = Executors.newScheduledThreadPool(1) - private val hangupOnCallAnswered by lazy { - HangUpRtcOnPstnCallAnsweredListener { - ContextCompat.startForegroundService(this, hangupIntent(this)) - } - } - - private interface TelephonyHandler { - fun register(telephonyManager: TelephonyManager) - fun unregister(telephonyManager: TelephonyManager) - } - - @RequiresApi(Build.VERSION_CODES.S) - private inner class TelephonyHandlerV31: TelephonyHandler { - private val callback = HangUpRtcTelephonyCallback { - ContextCompat.startForegroundService(this@WebRtcCallService, hangupIntent(this@WebRtcCallService)) - } - - override fun register(telephonyManager: TelephonyManager) { - telephonyManager.registerTelephonyCallback(serviceExecutor, callback) - } - - override fun unregister(telephonyManager: TelephonyManager) { - telephonyManager.unregisterTelephonyCallback(callback) - } - } - - private inner class TelephonyHandlerV23: TelephonyHandler { - override fun register(telephonyManager: TelephonyManager) { - telephonyManager.listen(hangupOnCallAnswered, PhoneStateListener.LISTEN_CALL_STATE) - } - - override fun unregister(telephonyManager: TelephonyManager) { - telephonyManager.listen(hangupOnCallAnswered, LISTEN_NONE) - } - } - - private val telephonyHandler = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - TelephonyHandlerV31() - } else { - TelephonyHandlerV23() + private val telephonyHandler = TelephonyHandler(serviceExecutor) { + ContextCompat.startForegroundService( + this@WebRtcCallService, + hangupIntent(this@WebRtcCallService) + ) } private var networkChangedReceiver: NetworkChangeReceiver? = null