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