|
|
|
@ -18,18 +18,18 @@ package org.thoughtcrime.securesms.recipients;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.provider.ContactsContract.Contacts;
|
|
|
|
|
import android.provider.ContactsContract.PhoneLookup;
|
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
|
|
|
|
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
|
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase;
|
|
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
|
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
|
|
|
|
import org.thoughtcrime.securesms.util.LRUCache;
|
|
|
|
@ -47,6 +47,8 @@ import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
|
|
|
|
public class RecipientProvider {
|
|
|
|
|
|
|
|
|
|
private static final String TAG = RecipientProvider.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
private static final Map<Long,Recipient> recipientCache = Collections.synchronizedMap(new LRUCache<Long,Recipient>(1000));
|
|
|
|
|
private static final Map<RecipientIds,Recipients> recipientsCache = Collections.synchronizedMap(new LRUCache<RecipientIds, Recipients>(1000));
|
|
|
|
|
private static final ExecutorService asyncRecipientResolver = Util.newSingleThreadedLifoExecutor();
|
|
|
|
@ -60,10 +62,18 @@ public class RecipientProvider {
|
|
|
|
|
|
|
|
|
|
Recipient getRecipient(Context context, long recipientId, boolean asynchronous) {
|
|
|
|
|
Recipient cachedRecipient = recipientCache.get(recipientId);
|
|
|
|
|
if (cachedRecipient != null) return cachedRecipient;
|
|
|
|
|
|
|
|
|
|
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
|
|
|
|
|
|
|
|
|
if (asynchronous) {
|
|
|
|
|
cachedRecipient = new Recipient(recipientId, number, getRecipientDetailsAsync(context, number));
|
|
|
|
|
} else {
|
|
|
|
|
cachedRecipient = new Recipient(recipientId, getRecipientDetailsSync(context, number));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cachedRecipient != null) return cachedRecipient;
|
|
|
|
|
else if (asynchronous) return getAsynchronousRecipient(context, recipientId);
|
|
|
|
|
else return getSynchronousRecipient(context, recipientId);
|
|
|
|
|
recipientCache.put(recipientId, cachedRecipient);
|
|
|
|
|
return cachedRecipient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Recipients getRecipients(Context context, long[] recipientIds, boolean asynchronous) {
|
|
|
|
@ -83,80 +93,44 @@ public class RecipientProvider {
|
|
|
|
|
return cachedRecipients;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Recipient getSynchronousRecipient(final Context context, final long recipientId) {
|
|
|
|
|
Log.w("RecipientProvider", "Cache miss [SYNC]!");
|
|
|
|
|
|
|
|
|
|
final Recipient recipient;
|
|
|
|
|
RecipientDetails details;
|
|
|
|
|
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
|
|
|
|
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
|
|
|
|
|
|
|
|
|
|
if (isGroupRecipient) details = getGroupRecipientDetails(context, number);
|
|
|
|
|
else details = getRecipientDetails(context, number);
|
|
|
|
|
|
|
|
|
|
if (details != null) {
|
|
|
|
|
recipient = new Recipient(details.name, details.number, recipientId, details.contactUri, details.avatar);
|
|
|
|
|
} else {
|
|
|
|
|
final Drawable defaultPhoto = isGroupRecipient
|
|
|
|
|
? ContactPhotoFactory.getDefaultGroupPhoto(context)
|
|
|
|
|
: ContactPhotoFactory.getDefaultContactPhoto(context, null);
|
|
|
|
|
|
|
|
|
|
recipient = new Recipient(null, number, recipientId, null, defaultPhoto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recipientCache.put(recipientId, recipient);
|
|
|
|
|
return recipient;
|
|
|
|
|
void clearCache() {
|
|
|
|
|
recipientCache.clear();
|
|
|
|
|
recipientsCache.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Recipient getAsynchronousRecipient(final Context context, final long recipientId) {
|
|
|
|
|
Log.w("RecipientProvider", "Cache miss [ASYNC]!");
|
|
|
|
|
|
|
|
|
|
final String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
|
|
|
|
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
|
|
|
|
|
|
|
|
|
|
private @NonNull ListenableFutureTask<RecipientDetails> getRecipientDetailsAsync(final Context context,
|
|
|
|
|
final String number)
|
|
|
|
|
{
|
|
|
|
|
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
|
|
|
|
@Override
|
|
|
|
|
public RecipientDetails call() throws Exception {
|
|
|
|
|
if (isGroupRecipient) return getGroupRecipientDetails(context, number);
|
|
|
|
|
else return getRecipientDetails(context, number);
|
|
|
|
|
return getRecipientDetailsSync(context, number);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ListenableFutureTask<RecipientDetails> future = new ListenableFutureTask<>(task);
|
|
|
|
|
|
|
|
|
|
asyncRecipientResolver.submit(future);
|
|
|
|
|
|
|
|
|
|
Drawable contactPhoto;
|
|
|
|
|
|
|
|
|
|
if (isGroupRecipient) {
|
|
|
|
|
contactPhoto = ContactPhotoFactory.getDefaultGroupPhoto(context);
|
|
|
|
|
} else {
|
|
|
|
|
contactPhoto = ContactPhotoFactory.getLoadingPhoto(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Recipient recipient = new Recipient(number, contactPhoto, recipientId, future);
|
|
|
|
|
recipientCache.put(recipientId, recipient);
|
|
|
|
|
|
|
|
|
|
return recipient;
|
|
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clearCache() {
|
|
|
|
|
recipientCache.clear();
|
|
|
|
|
recipientsCache.clear();
|
|
|
|
|
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, String number) {
|
|
|
|
|
if (GroupUtil.isEncodedGroup(number)) return getGroupRecipientDetails(context, number);
|
|
|
|
|
else return getIndividualRecipientDetails(context, number);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RecipientDetails getRecipientDetails(Context context, String number) {
|
|
|
|
|
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, String number) {
|
|
|
|
|
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
|
|
|
|
Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION,
|
|
|
|
|
null, null, null);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
|
|
|
|
String name = cursor.getString(3).equals(cursor.getString(0)) ? null : cursor.getString(0);
|
|
|
|
|
Drawable contactPhoto = ContactPhotoFactory.getContactPhoto(context,
|
|
|
|
|
Uri.withAppendedPath(Contacts.CONTENT_URI, cursor.getLong(2) + ""),
|
|
|
|
|
name);
|
|
|
|
|
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
|
|
|
|
String name = cursor.getString(3).equals(cursor.getString(0)) ? null : cursor.getString(0);
|
|
|
|
|
ContactPhoto contactPhoto = ContactPhotoFactory.getContactPhoto(context,
|
|
|
|
|
Uri.withAppendedPath(Contacts.CONTENT_URI, cursor.getLong(2) + ""),
|
|
|
|
|
name);
|
|
|
|
|
|
|
|
|
|
return new RecipientDetails(cursor.getString(0), cursor.getString(3), contactUri, contactPhoto);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
@ -164,23 +138,23 @@ public class RecipientProvider {
|
|
|
|
|
cursor.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(context, null));
|
|
|
|
|
return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RecipientDetails getGroupRecipientDetails(Context context, String groupId) {
|
|
|
|
|
private @NonNull RecipientDetails getGroupRecipientDetails(Context context, String groupId) {
|
|
|
|
|
try {
|
|
|
|
|
GroupDatabase.GroupRecord record = DatabaseFactory.getGroupDatabase(context)
|
|
|
|
|
.getGroup(GroupUtil.getDecodedId(groupId));
|
|
|
|
|
|
|
|
|
|
if (record != null) {
|
|
|
|
|
Drawable avatar = ContactPhotoFactory.getGroupContactPhoto(context, record.getAvatar());
|
|
|
|
|
return new RecipientDetails(record.getTitle(), groupId, null, avatar);
|
|
|
|
|
ContactPhoto contactPhoto = ContactPhotoFactory.getGroupContactPhoto(record.getAvatar());
|
|
|
|
|
return new RecipientDetails(record.getTitle(), groupId, null, contactPhoto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
return new RecipientDetails(null, groupId, null, ContactPhotoFactory.getDefaultGroupPhoto());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.w("RecipientProvider", e);
|
|
|
|
|
return null;
|
|
|
|
|
return new RecipientDetails(null, groupId, null, ContactPhotoFactory.getDefaultGroupPhoto());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -204,12 +178,12 @@ public class RecipientProvider {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class RecipientDetails {
|
|
|
|
|
public final String name;
|
|
|
|
|
public final String number;
|
|
|
|
|
public final Drawable avatar;
|
|
|
|
|
public final Uri contactUri;
|
|
|
|
|
public final String name;
|
|
|
|
|
public final String number;
|
|
|
|
|
public final ContactPhoto avatar;
|
|
|
|
|
public final Uri contactUri;
|
|
|
|
|
|
|
|
|
|
public RecipientDetails(String name, String number, Uri contactUri, Drawable avatar) {
|
|
|
|
|
public RecipientDetails(String name, String number, Uri contactUri, ContactPhoto avatar) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.number = number;
|
|
|
|
|
this.avatar = avatar;
|
|
|
|
|