From 0a91b83546bd9cafe8d6df9ba1ea38dafe4d00e3 Mon Sep 17 00:00:00 2001 From: Dimitris Papavasiliou Date: Wed, 26 Sep 2018 20:37:45 +0300 Subject: [PATCH] Share MessageRetrievalService's pipe by reference. --- .../dependencies/SignalCommunicationModule.java | 4 +--- .../securesms/service/MessageRetrievalService.java | 11 +++++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java b/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java index a4faa115e97..37c5b1a7bdb 100644 --- a/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java +++ b/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java @@ -115,10 +115,8 @@ synchronized SignalServiceMessageSender provideSignalMessageSender() { new DynamicCredentialsProvider(context), new SignalProtocolStoreImpl(context), BuildConfig.USER_AGENT, - Optional.fromNullable(MessageRetrievalService.getPipe()), + MessageRetrievalService.getPipeReference(), Optional.of(new SecurityEventListener(context))); - } else { - this.messageSender.setMessagePipe(MessageRetrievalService.getPipe()); } return this.messageSender; diff --git a/src/org/thoughtcrime/securesms/service/MessageRetrievalService.java b/src/org/thoughtcrime/securesms/service/MessageRetrievalService.java index 27429e10a81..242328e38c2 100644 --- a/src/org/thoughtcrime/securesms/service/MessageRetrievalService.java +++ b/src/org/thoughtcrime/securesms/service/MessageRetrievalService.java @@ -35,6 +35,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import javax.inject.Inject; @@ -63,6 +64,7 @@ public class MessageRetrievalService extends Service implements InjectableType, private MessageRetrievalThread retrievalThread = null; public static SignalServiceMessagePipe pipe = null; + public static AtomicReference pipeReference = new AtomicReference<>(); @Override public void onCreate() { @@ -283,6 +285,10 @@ public static void registerActivityStopped(Context activity) { return pipe; } + public static AtomicReference getPipeReference() { + return pipeReference; + } + private class MessageRetrievalThread extends Thread implements Thread.UncaughtExceptionHandler { private AtomicBoolean stopThread = new AtomicBoolean(false); @@ -299,9 +305,10 @@ public void run() { waitForConnectionNecessary(); Log.i(TAG, "Making websocket connection...."); - pipe = receiver.createMessagePipe(); + final SignalServiceMessagePipe localPipe = receiver.createMessagePipe(); - SignalServiceMessagePipe localPipe = pipe; + pipe = localPipe; + pipeReference.set(localPipe); try { while (isConnectionNecessary() && !stopThread.get() && !interrupted()) {