Refactor ContactPhotoFactory to handle changing local contact URIs.

This commit is contained in:
Moxie Marlinspike 2012-12-30 16:42:33 -08:00
parent 9b45e6068b
commit 61d4192798
4 changed files with 26 additions and 20 deletions

View File

@ -42,6 +42,7 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import org.thoughtcrime.securesms.contacts.ContactIdentityManager; import org.thoughtcrime.securesms.contacts.ContactIdentityManager;
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.MmsDatabase; import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord; import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
@ -51,7 +52,6 @@ import org.thoughtcrime.securesms.database.model.NotificationMmsMessageRecord;
import org.thoughtcrime.securesms.mms.Slide; import org.thoughtcrime.securesms.mms.Slide;
import org.thoughtcrime.securesms.mms.SlideDeck; import org.thoughtcrime.securesms.mms.SlideDeck;
import org.thoughtcrime.securesms.protocol.Tag; import org.thoughtcrime.securesms.protocol.Tag;
import org.thoughtcrime.securesms.recipients.ContactPhotoFactory;
import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.service.SendReceiveService; import org.thoughtcrime.securesms.service.SendReceiveService;

View File

@ -1,4 +1,4 @@
package org.thoughtcrime.securesms.recipients; package org.thoughtcrime.securesms.contacts;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
@ -9,16 +9,19 @@ import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Contacts;
import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.util.LRUCache;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
public class ContactPhotoFactory { public class ContactPhotoFactory {
private static final Object defaultPhotoLock = new Object(); private static final Object defaultPhotoLock = new Object();
private static final Object localUserLock = new Object();
private static Bitmap defaultContactPhoto; private static Bitmap defaultContactPhoto;
private static Bitmap localUserContactPhoto; private static final Map<Uri,Bitmap> localUserContactPhotoCache =
Collections.synchronizedMap(new LRUCache<Uri,Bitmap>(2));
private static final String[] CONTENT_URI_PROJECTION = new String[] { private static final String[] CONTENT_URI_PROJECTION = new String[] {
ContactsContract.Contacts._ID, ContactsContract.Contacts._ID,
@ -31,33 +34,34 @@ public class ContactPhotoFactory {
if (defaultContactPhoto == null) if (defaultContactPhoto == null)
defaultContactPhoto = BitmapFactory.decodeResource(context.getResources(), defaultContactPhoto = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_contact_picture); R.drawable.ic_contact_picture);
return defaultContactPhoto;
} }
return defaultContactPhoto;
} }
public static Bitmap getLocalUserContactPhoto(Context context, Uri uri) { public static Bitmap getLocalUserContactPhoto(Context context, Uri uri) {
synchronized (localUserLock) { if (uri == null) return getDefaultContactPhoto(context);
if (localUserContactPhoto == null) {
Cursor cursor = context.getContentResolver().query(uri, CONTENT_URI_PROJECTION,
null, null, null);
if (cursor != null && cursor.moveToFirst()) { Bitmap contactPhoto = localUserContactPhotoCache.get(uri);
localUserContactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
cursor.getLong(0) + "")); if (contactPhoto == null) {
} else { Cursor cursor = context.getContentResolver().query(uri, CONTENT_URI_PROJECTION,
localUserContactPhoto = getDefaultContactPhoto(context); null, null, null);
}
if (cursor != null && cursor.moveToFirst()) {
contactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
cursor.getLong(0) + ""));
} else {
contactPhoto = getDefaultContactPhoto(context);
} }
localUserContactPhotoCache.put(uri, contactPhoto);
} }
return localUserContactPhoto; return contactPhoto;
} }
public static void clearCache() { public static void clearCache() {
synchronized (localUserLock) { localUserContactPhotoCache.clear();
localUserContactPhoto = null;
}
} }
private static Bitmap getContactPhoto(Context context, Uri uri) { private static Bitmap getContactPhoto(Context context, Uri uri) {

View File

@ -18,6 +18,7 @@ package org.thoughtcrime.securesms.recipients;
import android.content.Context; import android.content.Context;
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.util.NumberUtil; import org.thoughtcrime.securesms.util.NumberUtil;

View File

@ -27,6 +27,7 @@ import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup; import android.provider.ContactsContract.PhoneLookup;
import android.util.Log; import android.util.Log;
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
import org.thoughtcrime.securesms.util.LRUCache; import org.thoughtcrime.securesms.util.LRUCache;
import org.thoughtcrime.securesms.util.ListenableFutureTask; import org.thoughtcrime.securesms.util.ListenableFutureTask;
import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.Util;