mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-25 05:29:04 +00:00
Switch MMS groups to use the group database infrastructure
Eliminate the concept of 'Recipients' (plural). There is now just a 'Recipient', which contains an Address that is either an individual or a group ID. MMS groups now exist as part of the group database, just like push groups. // FREEBIE
This commit is contained in:
@@ -26,31 +26,41 @@ import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientProvider.RecipientDetails;
|
||||
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
||||
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class Recipient {
|
||||
public class Recipient implements RecipientModifiedListener {
|
||||
|
||||
private final static String TAG = Recipient.class.getSimpleName();
|
||||
|
||||
private final Set<RecipientModifiedListener> listeners = Collections.newSetFromMap(new WeakHashMap<RecipientModifiedListener, Boolean>());
|
||||
|
||||
private final @NonNull Address address;
|
||||
private final @NonNull List<Recipient> participants = new LinkedList<>();
|
||||
|
||||
private @Nullable String name;
|
||||
private @Nullable String customLabel;
|
||||
private boolean stale;
|
||||
private boolean resolving;
|
||||
private boolean stale;
|
||||
private boolean resolving;
|
||||
|
||||
private ContactPhoto contactPhoto;
|
||||
private Uri contactUri;
|
||||
private Uri ringtone = null;
|
||||
private long mutedUntil = 0;
|
||||
private boolean blocked = false;
|
||||
private VibrateState vibrate = VibrateState.DEFAULT;
|
||||
private int expireMessages = 0;
|
||||
|
||||
@Nullable private MaterialColor color;
|
||||
|
||||
@@ -64,11 +74,16 @@ public class Recipient {
|
||||
this.resolving = true;
|
||||
|
||||
if (stale != null) {
|
||||
this.name = stale.name;
|
||||
this.contactUri = stale.contactUri;
|
||||
this.contactPhoto = stale.contactPhoto;
|
||||
this.color = stale.color;
|
||||
this.customLabel = stale.customLabel;
|
||||
this.name = stale.name;
|
||||
this.contactUri = stale.contactUri;
|
||||
this.contactPhoto = stale.contactPhoto;
|
||||
this.color = stale.color;
|
||||
this.customLabel = stale.customLabel;
|
||||
this.ringtone = stale.ringtone;
|
||||
this.mutedUntil = stale.mutedUntil;
|
||||
this.blocked = stale.blocked;
|
||||
this.vibrate = stale.vibrate;
|
||||
this.expireMessages = stale.expireMessages;
|
||||
}
|
||||
|
||||
future.addListener(new FutureTaskListener<RecipientDetails>() {
|
||||
@@ -76,12 +91,22 @@ public class Recipient {
|
||||
public void onSuccess(RecipientDetails result) {
|
||||
if (result != null) {
|
||||
synchronized (Recipient.this) {
|
||||
Recipient.this.name = result.name;
|
||||
Recipient.this.contactUri = result.contactUri;
|
||||
Recipient.this.contactPhoto = result.avatar;
|
||||
Recipient.this.color = result.color;
|
||||
Recipient.this.customLabel = result.customLabel;
|
||||
Recipient.this.resolving = false;
|
||||
Recipient.this.name = result.name;
|
||||
Recipient.this.contactUri = result.contactUri;
|
||||
Recipient.this.contactPhoto = result.avatar;
|
||||
Recipient.this.color = result.color;
|
||||
Recipient.this.customLabel = result.customLabel;
|
||||
Recipient.this.ringtone = result.ringtone;
|
||||
Recipient.this.mutedUntil = result.mutedUntil;
|
||||
Recipient.this.blocked = result.blocked;
|
||||
Recipient.this.vibrate = result.vibrateState;
|
||||
Recipient.this.expireMessages = result.expireMessages;
|
||||
Recipient.this.participants.addAll(result.participants);
|
||||
Recipient.this.resolving = false;
|
||||
|
||||
if (!listeners.isEmpty()) {
|
||||
for (Recipient recipient : participants) recipient.addListener(Recipient.this);
|
||||
}
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
@@ -95,14 +120,20 @@ public class Recipient {
|
||||
});
|
||||
}
|
||||
|
||||
Recipient(Address address, RecipientDetails details) {
|
||||
this.address = address;
|
||||
this.contactUri = details.contactUri;
|
||||
this.name = details.name;
|
||||
this.contactPhoto = details.avatar;
|
||||
this.color = details.color;
|
||||
Recipient(@NonNull Address address, @NonNull RecipientDetails details) {
|
||||
this.address = address;
|
||||
this.contactUri = details.contactUri;
|
||||
this.name = details.name;
|
||||
this.contactPhoto = details.avatar;
|
||||
this.color = details.color;
|
||||
this.customLabel = details.customLabel;
|
||||
this.ringtone = details.ringtone;
|
||||
this.mutedUntil = details.mutedUntil;
|
||||
this.blocked = details.blocked;
|
||||
this.vibrate = details.vibrateState;
|
||||
this.expireMessages = details.expireMessages;
|
||||
this.participants.addAll(details.participants);
|
||||
this.resolving = false;
|
||||
this.customLabel = details.customLabel;
|
||||
}
|
||||
|
||||
public synchronized @Nullable Uri getContactUri() {
|
||||
@@ -110,13 +141,24 @@ public class Recipient {
|
||||
}
|
||||
|
||||
public synchronized @Nullable String getName() {
|
||||
if (this.name == null && isMmsGroupRecipient()) {
|
||||
List<String> names = new LinkedList<>();
|
||||
|
||||
for (Recipient recipient : participants) {
|
||||
names.add(recipient.toShortString());
|
||||
}
|
||||
|
||||
return Util.join(names, ", ");
|
||||
}
|
||||
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public synchronized @NonNull MaterialColor getColor() {
|
||||
if (color != null) return color;
|
||||
else if (name != null) return ContactColors.generateFor(name);
|
||||
else return ContactColors.UNKNOWN_COLOR;
|
||||
if (isGroupRecipient()) return MaterialColor.GROUP;
|
||||
else if (color != null) return color;
|
||||
else if (name != null) return ContactColors.generateFor(name);
|
||||
else return ContactColors.UNKNOWN_COLOR;
|
||||
}
|
||||
|
||||
public void setColor(@NonNull MaterialColor color) {
|
||||
@@ -139,22 +181,101 @@ public class Recipient {
|
||||
return address.isGroup();
|
||||
}
|
||||
|
||||
public boolean isMmsGroupRecipient() {
|
||||
return address.isMmsGroup();
|
||||
}
|
||||
|
||||
public boolean isPushGroupRecipient() {
|
||||
return address.isGroup() && !address.isMmsGroup();
|
||||
}
|
||||
|
||||
public List<Recipient> getParticipants() {
|
||||
return participants;
|
||||
}
|
||||
|
||||
public synchronized void addListener(RecipientModifiedListener listener) {
|
||||
if (listeners.isEmpty()) {
|
||||
for (Recipient recipient : participants) recipient.addListener(this);
|
||||
}
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public synchronized void removeListener(RecipientModifiedListener listener) {
|
||||
listeners.remove(listener);
|
||||
|
||||
if (listeners.isEmpty()) {
|
||||
for (Recipient recipient : participants) recipient.removeListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized String toShortString() {
|
||||
return (name == null ? address.serialize() : name);
|
||||
return (getName() == null ? address.serialize() : getName());
|
||||
}
|
||||
|
||||
public synchronized @NonNull ContactPhoto getContactPhoto() {
|
||||
return contactPhoto;
|
||||
}
|
||||
|
||||
public synchronized @Nullable Uri getRingtone() {
|
||||
return ringtone;
|
||||
}
|
||||
|
||||
public void setRingtone(Uri ringtone) {
|
||||
synchronized (this) {
|
||||
this.ringtone = ringtone;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public synchronized boolean isMuted() {
|
||||
return System.currentTimeMillis() <= mutedUntil;
|
||||
}
|
||||
|
||||
public void setMuted(long mutedUntil) {
|
||||
synchronized (this) {
|
||||
this.mutedUntil = mutedUntil;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public synchronized boolean isBlocked() {
|
||||
return blocked;
|
||||
}
|
||||
|
||||
public void setBlocked(boolean blocked) {
|
||||
synchronized (this) {
|
||||
this.blocked = blocked;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public synchronized VibrateState getVibrate() {
|
||||
return vibrate;
|
||||
}
|
||||
|
||||
public void setVibrate(VibrateState vibrate) {
|
||||
synchronized (this) {
|
||||
this.vibrate = vibrate;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public synchronized int getExpireMessages() {
|
||||
return expireMessages;
|
||||
}
|
||||
|
||||
public void setExpireMessages(int expireMessages) {
|
||||
synchronized (this) {
|
||||
this.expireMessages = expireMessages;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
@@ -182,8 +303,9 @@ public class Recipient {
|
||||
listener.onModified(this);
|
||||
}
|
||||
|
||||
public interface RecipientModifiedListener {
|
||||
public void onModified(Recipient recipient);
|
||||
@Override
|
||||
public void onModified(Recipient recipient) {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
boolean isStale() {
|
||||
|
||||
@@ -22,37 +22,12 @@ import android.support.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class RecipientFactory {
|
||||
|
||||
public static final String RECIPIENT_CLEAR_ACTION = "org.thoughtcrime.securesms.database.RecipientFactory.CLEAR";
|
||||
|
||||
private static final RecipientProvider provider = new RecipientProvider();
|
||||
|
||||
public static @NonNull Recipients getRecipientsFor(Context context, Collection<Recipient> recipients, boolean asynchronous) {
|
||||
Address[] addresses= new Address[recipients.size()];
|
||||
int i = 0;
|
||||
|
||||
for (Recipient recipient : recipients) {
|
||||
addresses[i++] = recipient.getAddress();
|
||||
}
|
||||
|
||||
return provider.getRecipients(context, addresses, asynchronous);
|
||||
}
|
||||
|
||||
public static Recipients getRecipientsFor(Context context, Recipient recipient, boolean asynchronous) {
|
||||
Address[] addresses = new Address[1];
|
||||
addresses[0] = recipient.getAddress();
|
||||
|
||||
return provider.getRecipients(context, addresses, asynchronous);
|
||||
}
|
||||
|
||||
public static @NonNull Recipients getRecipientsFor(@NonNull Context context, @NonNull Address[] addresses, boolean asynchronous) {
|
||||
if (addresses == null || addresses.length == 0) throw new AssertionError(addresses);
|
||||
return provider.getRecipients(context, addresses, asynchronous);
|
||||
}
|
||||
|
||||
public static @NonNull Recipient getRecipientFor(@NonNull Context context, @NonNull Address address, boolean asynchronous) {
|
||||
if (address == null) throw new AssertionError(address);
|
||||
return provider.getRecipient(context, address, asynchronous);
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.thoughtcrime.securesms.recipients;
|
||||
|
||||
|
||||
public interface RecipientModifiedListener {
|
||||
public void onModified(Recipient recipient);
|
||||
}
|
||||
@@ -28,21 +28,18 @@ import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.color.MaterialColor;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
||||
import org.thoughtcrime.securesms.util.GroupUtil;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
|
||||
import org.thoughtcrime.securesms.util.LRUCache;
|
||||
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -55,7 +52,6 @@ class RecipientProvider {
|
||||
private static final String TAG = RecipientProvider.class.getSimpleName();
|
||||
|
||||
private static final RecipientCache recipientCache = new RecipientCache();
|
||||
private static final RecipientsCache recipientsCache = new RecipientsCache();
|
||||
private static final ExecutorService asyncRecipientResolver = Util.newSingleThreadedLifoExecutor();
|
||||
|
||||
private static final String[] CALLER_ID_PROJECTION = new String[] {
|
||||
@@ -69,7 +65,7 @@ class RecipientProvider {
|
||||
private static final Map<String, RecipientDetails> STATIC_DETAILS = new HashMap<String, RecipientDetails>() {{
|
||||
put("262966", new RecipientDetails("Amazon", null, null,
|
||||
ContactPhotoFactory.getResourceContactPhoto(R.drawable.ic_amazon),
|
||||
ContactColors.UNKNOWN_COLOR));
|
||||
null, null));
|
||||
}};
|
||||
|
||||
@NonNull Recipient getRecipient(Context context, Address address, boolean asynchronous) {
|
||||
@@ -81,35 +77,15 @@ class RecipientProvider {
|
||||
if (asynchronous) {
|
||||
cachedRecipient = new Recipient(address, cachedRecipient, getRecipientDetailsAsync(context, address));
|
||||
} else {
|
||||
cachedRecipient = new Recipient(address, getRecipientDetailsSync(context, address));
|
||||
cachedRecipient = new Recipient(address, getRecipientDetailsSync(context, address, false));
|
||||
}
|
||||
|
||||
recipientCache.set(address, cachedRecipient);
|
||||
return cachedRecipient;
|
||||
}
|
||||
|
||||
@NonNull Recipients getRecipients(Context context, Address[] recipientAddresses, boolean asynchronous) {
|
||||
Recipients cachedRecipients = recipientsCache.get(new RecipientAddresses(recipientAddresses));
|
||||
if (cachedRecipients != null && !cachedRecipients.isStale() && (asynchronous || !cachedRecipients.isResolving())) {
|
||||
return cachedRecipients;
|
||||
}
|
||||
|
||||
List<Recipient> recipientList = new LinkedList<>();
|
||||
|
||||
for (Address address : recipientAddresses) {
|
||||
recipientList.add(getRecipient(context, address, asynchronous));
|
||||
}
|
||||
|
||||
if (asynchronous) cachedRecipients = new Recipients(recipientList, cachedRecipients, getRecipientsPreferencesAsync(context, recipientAddresses));
|
||||
else cachedRecipients = new Recipients(recipientList, getRecipientsPreferencesSync(context, recipientAddresses));
|
||||
|
||||
recipientsCache.set(new RecipientAddresses(recipientAddresses), cachedRecipients);
|
||||
return cachedRecipients;
|
||||
}
|
||||
|
||||
void clearCache() {
|
||||
recipientCache.reset();
|
||||
recipientsCache.reset();
|
||||
}
|
||||
|
||||
private @NonNull ListenableFutureTask<RecipientDetails> getRecipientDetailsAsync(final Context context, final @NonNull Address address)
|
||||
@@ -117,7 +93,7 @@ class RecipientProvider {
|
||||
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
||||
@Override
|
||||
public RecipientDetails call() throws Exception {
|
||||
return getRecipientDetailsSync(context, address);
|
||||
return getRecipientDetailsSync(context, address, true);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -126,14 +102,13 @@ class RecipientProvider {
|
||||
return future;
|
||||
}
|
||||
|
||||
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, @NonNull Address address) {
|
||||
if (address.isGroup()) return getGroupRecipientDetails(context, address);
|
||||
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, @NonNull Address address, boolean nestedAsynchronous) {
|
||||
if (address.isGroup()) return getGroupRecipientDetails(context, address, nestedAsynchronous);
|
||||
else return getIndividualRecipientDetails(context, address);
|
||||
}
|
||||
|
||||
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, @NonNull Address address) {
|
||||
Optional<RecipientsPreferences> preferences = DatabaseFactory.getRecipientPreferenceDatabase(context).getRecipientsPreferences(new Address[]{address});
|
||||
MaterialColor color = preferences.isPresent() ? preferences.get().getColor() : null;
|
||||
Optional<RecipientsPreferences> preferences = DatabaseFactory.getRecipientPreferenceDatabase(context).getRecipientsPreferences(address);
|
||||
|
||||
if (address.isPhone() && !TextUtils.isEmpty(address.toPhoneString())) {
|
||||
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address.toPhoneString()));
|
||||
@@ -149,7 +124,7 @@ class RecipientProvider {
|
||||
Uri.withAppendedPath(Contacts.CONTENT_URI, cursor.getLong(2) + ""),
|
||||
name);
|
||||
|
||||
return new RecipientDetails(cursor.getString(0), cursor.getString(4), contactUri, contactPhoto, color);
|
||||
return new RecipientDetails(cursor.getString(0), cursor.getString(4), contactUri, contactPhoto, preferences.orNull(), null);
|
||||
} else {
|
||||
Log.w(TAG, "resultNumber is null");
|
||||
}
|
||||
@@ -161,84 +136,61 @@ class RecipientProvider {
|
||||
}
|
||||
|
||||
if (STATIC_DETAILS.containsKey(address.serialize())) return STATIC_DETAILS.get(address.serialize());
|
||||
else return new RecipientDetails(null, null, null, ContactPhotoFactory.getDefaultContactPhoto(null), color);
|
||||
else return new RecipientDetails(null, null, null, ContactPhotoFactory.getDefaultContactPhoto(null), preferences.orNull(), null);
|
||||
}
|
||||
|
||||
private @NonNull RecipientDetails getGroupRecipientDetails(Context context, Address groupId) {
|
||||
try {
|
||||
GroupDatabase.GroupRecord record = DatabaseFactory.getGroupDatabase(context)
|
||||
.getGroup(GroupUtil.getDecodedId(groupId.toGroupString()));
|
||||
private @NonNull RecipientDetails getGroupRecipientDetails(Context context, Address groupId, boolean asynchronous) {
|
||||
GroupDatabase.GroupRecord record = DatabaseFactory.getGroupDatabase(context).getGroup(groupId.toGroupString());
|
||||
|
||||
if (record != null) {
|
||||
ContactPhoto contactPhoto = ContactPhotoFactory.getGroupContactPhoto(record.getAvatar());
|
||||
String title = record.getTitle();
|
||||
if (record != null) {
|
||||
ContactPhoto contactPhoto = ContactPhotoFactory.getGroupContactPhoto(record.getAvatar());
|
||||
String title = record.getTitle();
|
||||
List<Address> memberAddresses = record.getMembers();
|
||||
List<Recipient> members = new LinkedList<>();
|
||||
|
||||
if (title == null) {
|
||||
title = context.getString(R.string.RecipientProvider_unnamed_group);;
|
||||
}
|
||||
|
||||
return new RecipientDetails(title, null, null, contactPhoto, null);
|
||||
for (Address memberAddress : memberAddresses) {
|
||||
members.add(getRecipient(context, memberAddress, asynchronous));
|
||||
}
|
||||
|
||||
return new RecipientDetails(context.getString(R.string.RecipientProvider_unnamed_group), null, null, ContactPhotoFactory.getDefaultGroupPhoto(), null);
|
||||
} catch (IOException e) {
|
||||
Log.w("RecipientProvider", e);
|
||||
return new RecipientDetails(context.getString(R.string.RecipientProvider_unnamed_group), null, null, ContactPhotoFactory.getDefaultGroupPhoto(), null);
|
||||
if (!groupId.isMmsGroup() && title == null) {
|
||||
title = context.getString(R.string.RecipientProvider_unnamed_group);;
|
||||
}
|
||||
|
||||
return new RecipientDetails(title, null, null, contactPhoto, null, members);
|
||||
}
|
||||
|
||||
return new RecipientDetails(context.getString(R.string.RecipientProvider_unnamed_group), null, null, ContactPhotoFactory.getDefaultGroupPhoto(), null, null);
|
||||
}
|
||||
|
||||
private @Nullable RecipientsPreferences getRecipientsPreferencesSync(Context context, Address[] addresses) {
|
||||
return DatabaseFactory.getRecipientPreferenceDatabase(context)
|
||||
.getRecipientsPreferences(addresses)
|
||||
.orNull();
|
||||
}
|
||||
|
||||
private ListenableFutureTask<RecipientsPreferences> getRecipientsPreferencesAsync(final Context context, final Address[] addresses) {
|
||||
ListenableFutureTask<RecipientsPreferences> task = new ListenableFutureTask<>(new Callable<RecipientsPreferences>() {
|
||||
@Override
|
||||
public RecipientsPreferences call() throws Exception {
|
||||
return getRecipientsPreferencesSync(context, addresses);
|
||||
}
|
||||
});
|
||||
|
||||
asyncRecipientResolver.execute(task);
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
public static class RecipientDetails {
|
||||
@Nullable public final String name;
|
||||
@Nullable public final String customLabel;
|
||||
@NonNull public final ContactPhoto avatar;
|
||||
@Nullable public final Uri contactUri;
|
||||
@Nullable public final MaterialColor color;
|
||||
static class RecipientDetails {
|
||||
@Nullable public final String name;
|
||||
@Nullable public final String customLabel;
|
||||
@NonNull public final ContactPhoto avatar;
|
||||
@Nullable public final Uri contactUri;
|
||||
@Nullable public final MaterialColor color;
|
||||
@Nullable public final Uri ringtone;
|
||||
public final long mutedUntil;
|
||||
@Nullable public final VibrateState vibrateState;
|
||||
public final boolean blocked;
|
||||
public final int expireMessages;
|
||||
@NonNull public final List<Recipient> participants;
|
||||
|
||||
public RecipientDetails(@Nullable String name, @Nullable String customLabel,
|
||||
@Nullable Uri contactUri, @NonNull ContactPhoto avatar,
|
||||
@Nullable MaterialColor color)
|
||||
@Nullable RecipientsPreferences preferences,
|
||||
@Nullable List<Recipient> participants)
|
||||
{
|
||||
this.name = name;
|
||||
this.customLabel = customLabel;
|
||||
this.avatar = avatar;
|
||||
this.contactUri = contactUri;
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
|
||||
private static class RecipientAddresses {
|
||||
private final Address[] addresses;
|
||||
|
||||
private RecipientAddresses(Address[] addresses) {
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public boolean equals(Object other) {
|
||||
if (other == null || !(other instanceof RecipientAddresses)) return false;
|
||||
return Arrays.equals(this.addresses, ((RecipientAddresses) other).addresses);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(addresses);
|
||||
this.name = name;
|
||||
this.customLabel = customLabel;
|
||||
this.avatar = avatar;
|
||||
this.contactUri = contactUri;
|
||||
this.color = preferences != null ? preferences.getColor() : null;
|
||||
this.ringtone = preferences != null ? preferences.getRingtone() : null;
|
||||
this.mutedUntil = preferences != null ? preferences.getMuteUntil() : 0;
|
||||
this.vibrateState = preferences != null ? preferences.getVibrateState() : null;
|
||||
this.blocked = preferences != null && preferences.isBlocked();
|
||||
this.expireMessages = preferences != null ? preferences.getExpireMessages() : 0;
|
||||
this.participants = participants == null ? new LinkedList<Recipient>() : participants;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,26 +214,4 @@ class RecipientProvider {
|
||||
|
||||
}
|
||||
|
||||
private static class RecipientsCache {
|
||||
|
||||
private final Map<RecipientAddresses,Recipients> cache = new LRUCache<>(1000);
|
||||
|
||||
public synchronized Recipients get(RecipientAddresses addresses) {
|
||||
return cache.get(addresses);
|
||||
}
|
||||
|
||||
public synchronized void set(RecipientAddresses addresses, Recipients recipients) {
|
||||
cache.put(addresses, recipients);
|
||||
}
|
||||
|
||||
public synchronized void reset() {
|
||||
for (Recipients recipients : cache.values()) {
|
||||
recipients.setStale();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,321 +0,0 @@
|
||||
/**
|
||||
* Copyright (C) 2015 Open Whisper Systems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.thoughtcrime.securesms.recipients;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.color.MaterialColor;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient.RecipientModifiedListener;
|
||||
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
||||
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class Recipients implements Iterable<Recipient>, RecipientModifiedListener {
|
||||
|
||||
private static final String TAG = Recipients.class.getSimpleName();
|
||||
|
||||
private final Set<RecipientsModifiedListener> listeners = Collections.newSetFromMap(new WeakHashMap<RecipientsModifiedListener, Boolean>());
|
||||
private final List<Recipient> recipients;
|
||||
|
||||
private Uri ringtone = null;
|
||||
private long mutedUntil = 0;
|
||||
private boolean blocked = false;
|
||||
private VibrateState vibrate = VibrateState.DEFAULT;
|
||||
private int expireMessages = 0;
|
||||
private boolean stale = false;
|
||||
|
||||
Recipients() {
|
||||
this(new LinkedList<Recipient>(), null);
|
||||
}
|
||||
|
||||
Recipients(List<Recipient> recipients, @Nullable RecipientsPreferences preferences) {
|
||||
this.recipients = recipients;
|
||||
|
||||
if (preferences != null) {
|
||||
ringtone = preferences.getRingtone();
|
||||
mutedUntil = preferences.getMuteUntil();
|
||||
vibrate = preferences.getVibrateState();
|
||||
blocked = preferences.isBlocked();
|
||||
expireMessages = preferences.getExpireMessages();
|
||||
}
|
||||
}
|
||||
|
||||
Recipients(@NonNull List<Recipient> recipients,
|
||||
@Nullable Recipients stale,
|
||||
@NonNull ListenableFutureTask<RecipientsPreferences> preferences)
|
||||
{
|
||||
this.recipients = recipients;
|
||||
|
||||
if (stale != null) {
|
||||
ringtone = stale.ringtone;
|
||||
mutedUntil = stale.mutedUntil;
|
||||
vibrate = stale.vibrate;
|
||||
blocked = stale.blocked;
|
||||
expireMessages = stale.expireMessages;
|
||||
}
|
||||
|
||||
preferences.addListener(new FutureTaskListener<RecipientsPreferences>() {
|
||||
@Override
|
||||
public void onSuccess(RecipientsPreferences result) {
|
||||
if (result != null) {
|
||||
|
||||
Set<RecipientsModifiedListener> localListeners;
|
||||
|
||||
synchronized (Recipients.this) {
|
||||
ringtone = result.getRingtone();
|
||||
mutedUntil = result.getMuteUntil();
|
||||
vibrate = result.getVibrateState();
|
||||
blocked = result.isBlocked();
|
||||
expireMessages = result.getExpireMessages();
|
||||
|
||||
localListeners = new HashSet<>(listeners);
|
||||
}
|
||||
|
||||
for (RecipientsModifiedListener listener : localListeners) {
|
||||
listener.onModified(Recipients.this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(ExecutionException error) {
|
||||
Log.w(TAG, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public synchronized @Nullable Uri getRingtone() {
|
||||
return ringtone;
|
||||
}
|
||||
|
||||
public void setRingtone(Uri ringtone) {
|
||||
synchronized (this) {
|
||||
this.ringtone = ringtone;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public synchronized boolean isMuted() {
|
||||
return System.currentTimeMillis() <= mutedUntil;
|
||||
}
|
||||
|
||||
public void setMuted(long mutedUntil) {
|
||||
synchronized (this) {
|
||||
this.mutedUntil = mutedUntil;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public synchronized boolean isBlocked() {
|
||||
return blocked;
|
||||
}
|
||||
|
||||
public void setBlocked(boolean blocked) {
|
||||
synchronized (this) {
|
||||
this.blocked = blocked;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public synchronized VibrateState getVibrate() {
|
||||
return vibrate;
|
||||
}
|
||||
|
||||
public void setVibrate(VibrateState vibrate) {
|
||||
synchronized (this) {
|
||||
this.vibrate = vibrate;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public @NonNull ContactPhoto getContactPhoto() {
|
||||
if (recipients.size() == 1) return recipients.get(0).getContactPhoto();
|
||||
else return ContactPhotoFactory.getDefaultGroupPhoto();
|
||||
}
|
||||
|
||||
public synchronized @NonNull MaterialColor getColor() {
|
||||
if (!isSingleRecipient() || isGroupRecipient()) return MaterialColor.GROUP;
|
||||
else if (isEmpty()) return ContactColors.UNKNOWN_COLOR;
|
||||
else return recipients.get(0).getColor();
|
||||
}
|
||||
|
||||
public synchronized void setColor(@NonNull MaterialColor color) {
|
||||
if (!isSingleRecipient() || isGroupRecipient()) throw new AssertionError("Groups don't have colors!");
|
||||
else if (!isEmpty()) recipients.get(0).setColor(color);
|
||||
}
|
||||
|
||||
public synchronized int getExpireMessages() {
|
||||
return expireMessages;
|
||||
}
|
||||
|
||||
public void setExpireMessages(int expireMessages) {
|
||||
synchronized (this) {
|
||||
this.expireMessages = expireMessages;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public synchronized void addListener(RecipientsModifiedListener listener) {
|
||||
if (listeners.isEmpty()) {
|
||||
for (Recipient recipient : recipients) {
|
||||
recipient.addListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public synchronized void removeListener(RecipientsModifiedListener listener) {
|
||||
listeners.remove(listener);
|
||||
|
||||
if (listeners.isEmpty()) {
|
||||
for (Recipient recipient : recipients) {
|
||||
recipient.removeListener(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmailRecipient() {
|
||||
for (Recipient recipient : recipients) {
|
||||
if (recipient.getAddress().isEmail()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isGroupRecipient() {
|
||||
return isSingleRecipient() && recipients.get(0).getAddress().isGroup();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.recipients.isEmpty();
|
||||
}
|
||||
|
||||
public boolean isSingleRecipient() {
|
||||
return this.recipients.size() == 1;
|
||||
}
|
||||
|
||||
public @Nullable Recipient getPrimaryRecipient() {
|
||||
if (!isEmpty())
|
||||
return this.recipients.get(0);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Recipient> getRecipientsList() {
|
||||
return this.recipients;
|
||||
}
|
||||
|
||||
public Address[] getAddresses() {
|
||||
Address[] addresses = new Address[recipients.size()];
|
||||
for (int i=0;i<recipients.size();i++) {
|
||||
addresses[i] = recipients.get(i).getAddress();
|
||||
}
|
||||
|
||||
Arrays.sort(addresses);
|
||||
|
||||
return addresses;
|
||||
}
|
||||
|
||||
public List<Address> getAddressesList() {
|
||||
List<Address> results = new LinkedList<>();
|
||||
Collections.addAll(results, getAddresses());
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public String toShortString() {
|
||||
String fromString = "";
|
||||
|
||||
for (int i=0;i<recipients.size();i++) {
|
||||
fromString += recipients.get(i).toShortString();
|
||||
|
||||
if (i != recipients.size() -1 )
|
||||
fromString += ", ";
|
||||
}
|
||||
|
||||
return fromString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Recipient> iterator() {
|
||||
return recipients.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onModified(Recipient recipient) {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
private void notifyListeners() {
|
||||
Set<RecipientsModifiedListener> localListeners;
|
||||
|
||||
synchronized (this) {
|
||||
localListeners = new HashSet<>(listeners);
|
||||
}
|
||||
|
||||
for (RecipientsModifiedListener listener : localListeners) {
|
||||
listener.onModified(this);
|
||||
}
|
||||
}
|
||||
|
||||
boolean isStale() {
|
||||
return stale;
|
||||
}
|
||||
|
||||
void setStale() {
|
||||
this.stale = true;
|
||||
}
|
||||
|
||||
boolean isResolving() {
|
||||
for (Recipient recipient : recipients) {
|
||||
if (recipient.isResolving()) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public interface RecipientsModifiedListener {
|
||||
public void onModified(Recipients recipient);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user