mirror of
https://github.com/oxen-io/session-android.git
synced 2025-01-13 11:33:53 +00:00
Add TelephonyHandler.kt
This commit is contained in:
parent
8037502f35
commit
5a931845cb
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -59,6 +59,7 @@ import org.webrtc.RtpReceiver
|
|||||||
import org.webrtc.SessionDescription
|
import org.webrtc.SessionDescription
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import java.util.concurrent.ExecutionException
|
import java.util.concurrent.ExecutionException
|
||||||
|
import java.util.concurrent.ExecutorService
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
import java.util.concurrent.ScheduledFuture
|
import java.util.concurrent.ScheduledFuture
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
@ -226,46 +227,11 @@ class WebRtcCallService : LifecycleService(), CallManager.WebRtcListener {
|
|||||||
private val serviceExecutor = Executors.newSingleThreadExecutor()
|
private val serviceExecutor = Executors.newSingleThreadExecutor()
|
||||||
private val timeoutExecutor = Executors.newScheduledThreadPool(1)
|
private val timeoutExecutor = Executors.newScheduledThreadPool(1)
|
||||||
|
|
||||||
private val hangupOnCallAnswered by lazy {
|
private val telephonyHandler = TelephonyHandler(serviceExecutor) {
|
||||||
HangUpRtcOnPstnCallAnsweredListener {
|
ContextCompat.startForegroundService(
|
||||||
ContextCompat.startForegroundService(this, hangupIntent(this))
|
this@WebRtcCallService,
|
||||||
}
|
hangupIntent(this@WebRtcCallService)
|
||||||
}
|
)
|
||||||
|
|
||||||
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 var networkChangedReceiver: NetworkChangeReceiver? = null
|
private var networkChangedReceiver: NetworkChangeReceiver? = null
|
||||||
|
Loading…
x
Reference in New Issue
Block a user