2012-12-31 00:42:33 +00:00
|
|
|
package org.thoughtcrime.securesms.contacts;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.provider.ContactsContract.Contacts;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2014-02-20 23:41:52 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2012-12-31 00:42:33 +00:00
|
|
|
import org.thoughtcrime.securesms.util.LRUCache;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
2012-12-31 00:42:33 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Map;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
|
|
|
public class ContactPhotoFactory {
|
|
|
|
|
2014-03-02 21:17:51 +00:00
|
|
|
private static final Object defaultPhotoLock = new Object();
|
|
|
|
private static final Object defaultGroupPhotoLock = new Object();
|
2012-12-24 16:40:37 +00:00
|
|
|
|
|
|
|
private static Bitmap defaultContactPhoto;
|
2014-03-02 21:17:51 +00:00
|
|
|
private static Bitmap defaultGroupContactPhoto;
|
|
|
|
|
2012-12-31 00:42:33 +00:00
|
|
|
private static final Map<Uri,Bitmap> localUserContactPhotoCache =
|
|
|
|
Collections.synchronizedMap(new LRUCache<Uri,Bitmap>(2));
|
2012-12-24 16:40:37 +00:00
|
|
|
|
|
|
|
private static final String[] CONTENT_URI_PROJECTION = new String[] {
|
|
|
|
ContactsContract.Contacts._ID,
|
|
|
|
ContactsContract.Contacts.DISPLAY_NAME,
|
|
|
|
ContactsContract.Contacts.LOOKUP_KEY
|
|
|
|
};
|
|
|
|
|
|
|
|
public static Bitmap getDefaultContactPhoto(Context context) {
|
|
|
|
synchronized (defaultPhotoLock) {
|
|
|
|
if (defaultContactPhoto == null)
|
|
|
|
defaultContactPhoto = BitmapFactory.decodeResource(context.getResources(),
|
|
|
|
R.drawable.ic_contact_picture);
|
2012-12-31 00:42:33 +00:00
|
|
|
return defaultContactPhoto;
|
2012-12-24 16:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 05:42:30 +00:00
|
|
|
public static Bitmap getDefaultGroupPhoto(Context context) {
|
2014-03-02 21:17:51 +00:00
|
|
|
synchronized (defaultGroupPhotoLock) {
|
|
|
|
if (defaultGroupContactPhoto == null)
|
|
|
|
defaultGroupContactPhoto = BitmapFactory.decodeResource(context.getResources(),
|
|
|
|
R.drawable.ic_group_photo);
|
|
|
|
return defaultGroupContactPhoto;
|
2014-02-18 05:42:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
public static Bitmap getLocalUserContactPhoto(Context context, Uri uri) {
|
2012-12-31 00:42:33 +00:00
|
|
|
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()) {
|
|
|
|
contactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
|
|
|
|
cursor.getLong(0) + ""));
|
|
|
|
} else {
|
|
|
|
contactPhoto = getDefaultContactPhoto(context);
|
2012-12-24 16:40:37 +00:00
|
|
|
}
|
2012-12-31 00:42:33 +00:00
|
|
|
|
|
|
|
localUserContactPhotoCache.put(uri, contactPhoto);
|
2012-12-24 16:40:37 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 00:42:33 +00:00
|
|
|
return contactPhoto;
|
2012-12-24 16:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void clearCache() {
|
2012-12-31 00:42:33 +00:00
|
|
|
localUserContactPhotoCache.clear();
|
2012-12-24 16:40:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 23:41:52 +00:00
|
|
|
public static void clearCache(Recipient recipient) {
|
|
|
|
if (localUserContactPhotoCache.containsKey(recipient.getContactUri()))
|
|
|
|
localUserContactPhotoCache.remove(recipient.getContactUri());
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
private static Bitmap getContactPhoto(Context context, Uri uri) {
|
|
|
|
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
|
|
|
|
|
|
|
|
if (inputStream == null) return ContactPhotoFactory.getDefaultContactPhoto(context);
|
|
|
|
else return BitmapFactory.decodeStream(inputStream);
|
|
|
|
}
|
|
|
|
}
|