mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-22 16:07:30 +00:00
Refactor ContactPhotoFactory to handle changing local contact URIs.
This commit is contained in:
parent
9b45e6068b
commit
61d4192798
@ -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;
|
||||||
|
|
||||||
|
@ -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) {
|
|
||||||
|
Bitmap contactPhoto = localUserContactPhotoCache.get(uri);
|
||||||
|
|
||||||
|
if (contactPhoto == null) {
|
||||||
Cursor cursor = context.getContentResolver().query(uri, CONTENT_URI_PROJECTION,
|
Cursor cursor = context.getContentResolver().query(uri, CONTENT_URI_PROJECTION,
|
||||||
null, null, null);
|
null, null, null);
|
||||||
|
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
localUserContactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
|
contactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
|
||||||
cursor.getLong(0) + ""));
|
cursor.getLong(0) + ""));
|
||||||
} else {
|
} else {
|
||||||
localUserContactPhoto = getDefaultContactPhoto(context);
|
contactPhoto = getDefaultContactPhoto(context);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return localUserContactPhoto;
|
localUserContactPhotoCache.put(uri, contactPhoto);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
@ -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;
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user