diff --git a/src/org/thoughtcrime/securesms/recipients/Recipient.java b/src/org/thoughtcrime/securesms/recipients/Recipient.java index 3c92803340..d40173cdc4 100644 --- a/src/org/thoughtcrime/securesms/recipients/Recipient.java +++ b/src/org/thoughtcrime/securesms/recipients/Recipient.java @@ -17,7 +17,6 @@ package org.thoughtcrime.securesms.recipients; import android.content.Context; -import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.Uri; import android.util.Log; @@ -28,13 +27,16 @@ import org.thoughtcrime.securesms.util.FutureTaskListener; import org.thoughtcrime.securesms.util.GroupUtil; import org.thoughtcrime.securesms.util.ListenableFutureTask; +import java.util.Collections; import java.util.HashSet; +import java.util.Set; +import java.util.WeakHashMap; public class Recipient { private final static String TAG = Recipient.class.getSimpleName(); - private final HashSet listeners = new HashSet<>(); + private final Set listeners = Collections.newSetFromMap(new WeakHashMap()); private final long recipientId; @@ -55,7 +57,7 @@ public class Recipient { @Override public void onSuccess(RecipientDetails result) { if (result != null) { - HashSet localListeners; + Set localListeners; synchronized (Recipient.this) { Recipient.this.name = result.name; @@ -63,7 +65,7 @@ public class Recipient { Recipient.this.contactUri = result.contactUri; Recipient.this.contactPhoto = result.avatar; - localListeners = (HashSet) listeners.clone(); + localListeners = new HashSet<>(listeners); listeners.clear(); } @@ -115,18 +117,6 @@ public class Recipient { listeners.remove(listener); } - public void notifyListeners() { - HashSet localListeners; - - synchronized (this) { - localListeners = (HashSet)listeners.clone(); - } - - for (RecipientModifiedListener listener : localListeners) { - listener.onModified(this); - } - } - public synchronized String toShortString() { return (name == null ? number : name); } diff --git a/src/org/thoughtcrime/securesms/service/ApplicationMigrationService.java b/src/org/thoughtcrime/securesms/service/ApplicationMigrationService.java index d0bd5c253a..03b3a149c0 100644 --- a/src/org/thoughtcrime/securesms/service/ApplicationMigrationService.java +++ b/src/org/thoughtcrime/securesms/service/ApplicationMigrationService.java @@ -23,9 +23,11 @@ import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.database.SmsMigrator; import org.thoughtcrime.securesms.database.SmsMigrator.ProgressDescription; +import java.lang.ref.WeakReference; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +// FIXME: This class is nuts. public class ApplicationMigrationService extends Service implements SmsMigrator.SmsMigrationProgressListener { @@ -39,9 +41,9 @@ public class ApplicationMigrationService extends Service private final Binder binder = new ApplicationMigrationBinder(); private final Executor executor = Executors.newSingleThreadExecutor(); - private Handler handler = null; + private WeakReference handler = null; private NotificationCompat.Builder notification = null; - private ImportState state = new ImportState(ImportState.STATE_IDLE, null); + private ImportState state = new ImportState(ImportState.STATE_IDLE, null); @Override public void onCreate() { @@ -70,7 +72,7 @@ public class ApplicationMigrationService extends Service } public void setImportStateHandler(Handler handler) { - this.handler = handler; + this.handler = new WeakReference<>(handler); } private void registerCompletedReceiver() { @@ -103,6 +105,8 @@ public class ApplicationMigrationService extends Service private void setState(ImportState state) { this.state = state; + Handler handler = this.handler.get(); + if (handler != null) { handler.obtainMessage(state.state, state.progress).sendToTarget(); } diff --git a/src/org/thoughtcrime/securesms/service/RegistrationService.java b/src/org/thoughtcrime/securesms/service/RegistrationService.java index 9650958c44..3d0d3451a8 100644 --- a/src/org/thoughtcrime/securesms/service/RegistrationService.java +++ b/src/org/thoughtcrime/securesms/service/RegistrationService.java @@ -21,7 +21,6 @@ import org.thoughtcrime.securesms.jobs.GcmRefreshJob; import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientFactory; -import org.thoughtcrime.securesms.recipients.RecipientFormattingException; import org.thoughtcrime.securesms.util.DirectoryHelper; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.Util; @@ -34,6 +33,7 @@ import org.whispersystems.textsecure.api.TextSecureAccountManager; import org.whispersystems.textsecure.api.push.exceptions.ExpectationFailedException; import java.io.IOException; +import java.lang.ref.WeakReference; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -77,7 +77,7 @@ public class RegistrationService extends Service { private volatile RegistrationState registrationState = new RegistrationState(RegistrationState.STATE_IDLE); - private volatile Handler registrationStateHandler; + private volatile WeakReference registrationStateHandler; private volatile ChallengeReceiver challengeReceiver; private String challenge; private long verificationStartTime; @@ -298,6 +298,8 @@ public class RegistrationService extends Service { private void setState(RegistrationState state) { this.registrationState = state; + Handler registrationStateHandler = this.registrationStateHandler.get(); + if (registrationStateHandler != null) { registrationStateHandler.obtainMessage(state.state, state).sendToTarget(); } @@ -319,7 +321,7 @@ public class RegistrationService extends Service { } public void setRegistrationStateHandler(Handler registrationStateHandler) { - this.registrationStateHandler = registrationStateHandler; + this.registrationStateHandler = new WeakReference<>(registrationStateHandler); } public class RegistrationServiceBinder extends Binder {