Refactor contact photo logic to not reuse drawables.

// FREEBIE
This commit is contained in:
Moxie Marlinspike
2015-06-23 10:15:33 -07:00
parent 8bb47bbdf1
commit 64df85f3ee
16 changed files with 295 additions and 275 deletions

View File

@@ -16,12 +16,12 @@
*/
package org.thoughtcrime.securesms.recipients;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.NonNull;
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.recipients.RecipientProvider.RecipientDetails;
import org.thoughtcrime.securesms.util.FutureTaskListener;
import org.thoughtcrime.securesms.util.GroupUtil;
@@ -43,15 +43,14 @@ public class Recipient {
private String number;
private String name;
private Drawable contactPhoto;
private Uri contactUri;
private ContactPhoto contactPhoto;
private Uri contactUri;
Recipient(String number, Drawable contactPhoto,
long recipientId, ListenableFutureTask<RecipientDetails> future)
Recipient(long recipientId, String number, ListenableFutureTask<RecipientDetails> future)
{
this.number = number;
this.contactPhoto = contactPhoto;
this.recipientId = recipientId;
this.recipientId = recipientId;
this.number = number;
this.contactPhoto = ContactPhotoFactory.getLoadingPhoto();
future.addListener(new FutureTaskListener<RecipientDetails>() {
@Override
@@ -60,12 +59,12 @@ public class Recipient {
Set<RecipientModifiedListener> localListeners;
synchronized (Recipient.this) {
Recipient.this.name = result.name;
Recipient.this.number = result.number;
Recipient.this.contactUri = result.contactUri;
Recipient.this.contactPhoto = result.avatar;
Recipient.this.name = result.name;
Recipient.this.number = result.number;
Recipient.this.contactUri = result.contactUri;
Recipient.this.contactPhoto = result.avatar;
localListeners = new HashSet<>(listeners);
localListeners = new HashSet<>(listeners);
listeners.clear();
}
@@ -81,12 +80,12 @@ public class Recipient {
});
}
Recipient(String name, String number, long recipientId, Uri contactUri, Drawable contactPhoto) {
this.number = number;
this.recipientId = recipientId;
this.contactUri = contactUri;
this.name = name;
this.contactPhoto = contactPhoto;
Recipient(long recipientId, RecipientDetails details) {
this.recipientId = recipientId;
this.number = details.number;
this.contactUri = details.contactUri;
this.name = details.name;
this.contactPhoto = details.avatar;
}
public synchronized Uri getContactUri() {
@@ -121,13 +120,12 @@ public class Recipient {
return (name == null ? number : name);
}
public synchronized Drawable getContactPhoto() {
public synchronized @NonNull ContactPhoto getContactPhoto() {
return contactPhoto;
}
public static Recipient getUnknownRecipient(Context context) {
return new Recipient("Unknown", "Unknown", -1, null,
ContactPhotoFactory.getDefaultContactPhoto(context, null));
public static Recipient getUnknownRecipient() {
return new Recipient(-1, new RecipientDetails("Unknown", "Unknown", null, ContactPhotoFactory.getDefaultContactPhoto("Unknown")));
}
@Override

View File

@@ -20,7 +20,7 @@ import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.libaxolotl.util.guava.Optional;
@@ -120,7 +120,6 @@ public class RecipientFactory {
}
public static void clearCache() {
ContactPhotoFactory.clearCache();
provider.clearCache();
}

View File

@@ -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;
if (cachedRecipient != null) return cachedRecipient;
else if (asynchronous) return getAsynchronousRecipient(context, recipientId);
else return getSynchronousRecipient(context, recipientId);
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));
}
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;
}
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);
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
@Override
public RecipientDetails call() throws Exception {
if (isGroupRecipient) return getGroupRecipientDetails(context, number);
else return getRecipientDetails(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;
}
void clearCache() {
recipientCache.clear();
recipientsCache.clear();
}
private RecipientDetails getRecipientDetails(Context context, String number) {
private @NonNull ListenableFutureTask<RecipientDetails> getRecipientDetailsAsync(final Context context,
final String number)
{
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
@Override
public RecipientDetails call() throws Exception {
return getRecipientDetailsSync(context, number);
}
};
ListenableFutureTask<RecipientDetails> future = new ListenableFutureTask<>(task);
asyncRecipientResolver.submit(future);
return future;
}
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, String number) {
if (GroupUtil.isEncodedGroup(number)) return getGroupRecipientDetails(context, number);
else return getIndividualRecipientDetails(context, 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;

View File

@@ -16,14 +16,14 @@
*/
package org.thoughtcrime.securesms.recipients;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.util.Patterns;
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.RecipientPreferenceDatabase.RecipientsPreferences;
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
import org.thoughtcrime.securesms.recipients.Recipient.RecipientModifiedListener;
@@ -149,9 +149,10 @@ public class Recipients implements Iterable<Recipient>, RecipientModifiedListene
notifyListeners();
}
public Drawable getContactPhoto(Context context) {
public @NonNull
ContactPhoto getContactPhoto() {
if (recipients.size() == 1) return recipients.get(0).getContactPhoto();
else return ContactPhotoFactory.getDefaultGroupPhoto(context);
else return ContactPhotoFactory.getDefaultGroupPhoto();
}
public synchronized void addListener(RecipientsModifiedListener listener) {