mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-22 18:00:02 +01:00
Improve auth WebSocket lifecycle.
This commit is contained in:
committed by
Alex Hart
parent
6bbd899507
commit
323697dfc9
@@ -17,7 +17,6 @@ import android.net.ConnectivityManager
|
||||
import android.os.Build
|
||||
import android.telephony.PhoneStateListener
|
||||
import android.telephony.TelephonyManager
|
||||
import androidx.annotation.MainThread
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.os.bundleOf
|
||||
@@ -27,7 +26,6 @@ import io.reactivex.rxjava3.disposables.Disposable
|
||||
import io.reactivex.rxjava3.kotlin.subscribeBy
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import org.signal.core.util.PendingIntentFlags
|
||||
import org.signal.core.util.ThreadUtil
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.thoughtcrime.securesms.dependencies.AppDependencies
|
||||
import org.thoughtcrime.securesms.jobs.UnableToStartException
|
||||
@@ -45,8 +43,6 @@ import org.thoughtcrime.securesms.webrtc.audio.SignalAudioManager.Companion.crea
|
||||
import org.thoughtcrime.securesms.webrtc.locks.LockManager
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
import kotlin.concurrent.withLock
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
|
||||
/**
|
||||
* Entry point for [SignalCallManager] and friends to interact with the Android system.
|
||||
@@ -61,6 +57,8 @@ class ActiveCallManager(
|
||||
companion object {
|
||||
private val TAG = Log.tag(ActiveCallManager::class.java)
|
||||
|
||||
private const val WEBSOCKET_KEEP_ALIVE_TOKEN: String = "ActiveCall"
|
||||
|
||||
private val requiresAsyncNotificationLoad = Build.VERSION.SDK_INT <= 29
|
||||
|
||||
private var activeCallManager: ActiveCallManager? = null
|
||||
@@ -142,7 +140,6 @@ class ActiveCallManager(
|
||||
private var networkReceiver: NetworkReceiver? = null
|
||||
private var powerButtonReceiver: PowerButtonReceiver? = null
|
||||
private var uncaughtExceptionHandlerManager: UncaughtExceptionHandlerManager? = null
|
||||
private val webSocketKeepAliveTask: WebSocketKeepAliveTask = WebSocketKeepAliveTask()
|
||||
private var signalAudioManager: SignalAudioManager? = null
|
||||
private var previousNotificationId = -1
|
||||
private var previousNotificationDisposable = Disposable.disposed()
|
||||
@@ -153,7 +150,8 @@ class ActiveCallManager(
|
||||
registerUncaughtExceptionHandler()
|
||||
registerNetworkReceiver()
|
||||
|
||||
webSocketKeepAliveTask.start()
|
||||
AppDependencies.authWebSocket.registerKeepAliveToken(WEBSOCKET_KEEP_ALIVE_TOKEN)
|
||||
AppDependencies.unauthWebSocket.registerKeepAliveToken(WEBSOCKET_KEEP_ALIVE_TOKEN)
|
||||
}
|
||||
|
||||
fun shutdown() {
|
||||
@@ -170,7 +168,8 @@ class ActiveCallManager(
|
||||
unregisterNetworkReceiver()
|
||||
unregisterPowerButtonReceiver()
|
||||
|
||||
webSocketKeepAliveTask.stop()
|
||||
AppDependencies.authWebSocket.removeKeepAliveToken(WEBSOCKET_KEEP_ALIVE_TOKEN)
|
||||
AppDependencies.unauthWebSocket.removeKeepAliveToken(WEBSOCKET_KEEP_ALIVE_TOKEN)
|
||||
|
||||
if (!ActiveCallForegroundService.stop(application) && previousNotificationId != -1) {
|
||||
NotificationManagerCompat.from(application).cancel(previousNotificationId)
|
||||
@@ -433,42 +432,6 @@ class ActiveCallManager(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Periodically request the web socket stay open if we are doing anything call related.
|
||||
*/
|
||||
private class WebSocketKeepAliveTask : Runnable {
|
||||
|
||||
companion object {
|
||||
private val REQUEST_WEBSOCKET_STAY_OPEN_DELAY: Duration = 1.minutes
|
||||
private val WEBSOCKET_KEEP_ALIVE_TOKEN: String = WebSocketKeepAliveTask::class.java.simpleName
|
||||
}
|
||||
|
||||
private var keepRunning = false
|
||||
|
||||
@MainThread
|
||||
fun start() {
|
||||
if (!keepRunning) {
|
||||
keepRunning = true
|
||||
run()
|
||||
}
|
||||
}
|
||||
|
||||
@MainThread
|
||||
fun stop() {
|
||||
keepRunning = false
|
||||
ThreadUtil.cancelRunnableOnMain(this)
|
||||
AppDependencies.incomingMessageObserver.removeKeepAliveToken(WEBSOCKET_KEEP_ALIVE_TOKEN)
|
||||
}
|
||||
|
||||
@MainThread
|
||||
override fun run() {
|
||||
if (keepRunning) {
|
||||
AppDependencies.incomingMessageObserver.registerKeepAliveToken(WEBSOCKET_KEEP_ALIVE_TOKEN)
|
||||
ThreadUtil.runOnMainDelayed(this, REQUEST_WEBSOCKET_STAY_OPEN_DELAY.inWholeMilliseconds)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class NetworkReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
Reference in New Issue
Block a user