Add TelephonyHandler.kt

This commit is contained in:
bemusementpark 2024-07-19 14:50:39 +09:30
parent 8037502f35
commit 5a931845cb
2 changed files with 52 additions and 40 deletions

View File

@ -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)
}
}

View File

@ -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