diff --git a/app/src/main/java/org/thoughtcrime/securesms/dependencies/AppDependencies.kt b/app/src/main/java/org/thoughtcrime/securesms/dependencies/AppDependencies.kt index 778e081aff6..549dd5ff758 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/dependencies/AppDependencies.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/dependencies/AppDependencies.kt @@ -339,7 +339,7 @@ object AppDependencies { fun provideMegaphoneRepository(): MegaphoneRepository fun provideEarlyMessageCache(): EarlyMessageCache fun provideMessageNotifier(): MessageNotifier - fun provideIncomingMessageObserver(): IncomingMessageObserver + fun provideIncomingMessageObserver(signalWebSocket: SignalWebSocket): IncomingMessageObserver fun provideTrimThreadsByDateManager(): TrimThreadsByDateManager fun provideViewOnceMessageManager(): ViewOnceMessageManager fun provideExpiringStoriesManager(): ExpiringStoriesManager diff --git a/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java b/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java index 1fff6ff1f4c..a479429e75b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java +++ b/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java @@ -207,8 +207,8 @@ public ApplicationDependencyProvider(@NonNull Application context) { } @Override - public @NonNull IncomingMessageObserver provideIncomingMessageObserver() { - return new IncomingMessageObserver(context); + public @NonNull IncomingMessageObserver provideIncomingMessageObserver(@NonNull SignalWebSocket signalWebSocket) { + return new IncomingMessageObserver(context, signalWebSocket); } @Override diff --git a/app/src/main/java/org/thoughtcrime/securesms/dependencies/NetworkDependenciesModule.kt b/app/src/main/java/org/thoughtcrime/securesms/dependencies/NetworkDependenciesModule.kt index 0e253e5b476..a5348627aee 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/dependencies/NetworkDependenciesModule.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/dependencies/NetworkDependenciesModule.kt @@ -75,7 +75,7 @@ class NetworkDependenciesModule( val signalServiceMessageSender: SignalServiceMessageSender by _signalServiceMessageSender val incomingMessageObserver: IncomingMessageObserver by lazy { - provider.provideIncomingMessageObserver() + provider.provideIncomingMessageObserver(signalWebSocket) } val pushServiceSocket: PushServiceSocket by lazy { diff --git a/app/src/main/java/org/thoughtcrime/securesms/messages/IncomingMessageObserver.kt b/app/src/main/java/org/thoughtcrime/securesms/messages/IncomingMessageObserver.kt index 6ee92aa2968..9edee874272 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/messages/IncomingMessageObserver.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/messages/IncomingMessageObserver.kt @@ -31,6 +31,7 @@ import org.thoughtcrime.securesms.util.AlarmSleepTimer import org.thoughtcrime.securesms.util.AppForegroundObserver import org.thoughtcrime.securesms.util.SignalLocalMetrics import org.thoughtcrime.securesms.util.asChain +import org.whispersystems.signalservice.api.SignalWebSocket import org.whispersystems.signalservice.api.push.ServiceId import org.whispersystems.signalservice.api.util.SleepTimer import org.whispersystems.signalservice.api.util.UptimeSleepTimer @@ -54,7 +55,7 @@ import kotlin.time.Duration.Companion.seconds * * This class is responsible for opening/closing the websocket based on the app's state and observing new inbound messages received on the websocket. */ -class IncomingMessageObserver(private val context: Application) { +class IncomingMessageObserver(private val context: Application, private val signalWebSocket: SignalWebSocket) { companion object { private val TAG = Log.tag(IncomingMessageObserver::class.java) @@ -238,7 +239,7 @@ class IncomingMessageObserver(private val context: Application) { } private fun disconnect() { - AppDependencies.signalWebSocket.disconnect() + signalWebSocket.disconnect() } @JvmOverloads @@ -378,8 +379,7 @@ class IncomingMessageObserver(private val context: Application) { waitForConnectionNecessary() Log.i(TAG, "Making websocket connection....") - val signalWebSocket = AppDependencies.signalWebSocket - val webSocketDisposable = AppDependencies.webSocketObserver.subscribe { state: WebSocketConnectionState -> + val webSocketDisposable = signalWebSocket.webSocketState.subscribe { state: WebSocketConnectionState -> Log.d(TAG, "WebSocket State: $state") // Any change to a non-connected state means that we are not drained @@ -394,7 +394,7 @@ class IncomingMessageObserver(private val context: Application) { signalWebSocket.connect() try { - while (isConnectionNecessary()) { + while (!terminated && isConnectionNecessary()) { try { Log.d(TAG, "Reading message...") diff --git a/app/src/test/java/org/thoughtcrime/securesms/dependencies/MockApplicationDependencyProvider.kt b/app/src/test/java/org/thoughtcrime/securesms/dependencies/MockApplicationDependencyProvider.kt index f800ac79e5b..1f7bc7f6349 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/dependencies/MockApplicationDependencyProvider.kt +++ b/app/src/test/java/org/thoughtcrime/securesms/dependencies/MockApplicationDependencyProvider.kt @@ -101,7 +101,7 @@ class MockApplicationDependencyProvider : AppDependencies.Provider { return mockk() } - override fun provideIncomingMessageObserver(): IncomingMessageObserver { + override fun provideIncomingMessageObserver(signalWebSocket: SignalWebSocket): IncomingMessageObserver { return mockk() }