2012-09-11 18:23:19 -07:00
|
|
|
/**
|
2011-12-20 10:20:44 -08:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-11 18:23:19 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-09-11 18:23:19 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.recipients;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2014-04-24 16:40:54 -07:00
|
|
|
import android.database.ContentObserver;
|
2012-12-12 03:56:38 -08:00
|
|
|
import android.database.Cursor;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.net.Uri;
|
2012-12-12 03:56:38 -08:00
|
|
|
import android.provider.ContactsContract.Contacts;
|
|
|
|
import android.provider.ContactsContract.PhoneLookup;
|
2012-12-24 08:40:37 -08:00
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2012-12-30 16:42:33 -08:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
2014-02-02 19:38:06 -08:00
|
|
|
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
|
2014-01-14 00:26:43 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
2014-03-07 00:58:26 +01:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
2014-02-02 19:38:06 -08:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2012-12-24 08:40:37 -08:00
|
|
|
import org.thoughtcrime.securesms.util.LRUCache;
|
2012-12-27 12:00:14 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2014-03-07 00:58:26 +01:00
|
|
|
import org.whispersystems.textsecure.util.ListenableFutureTask;
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
import java.io.IOException;
|
2012-12-24 08:40:37 -08:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Map;
|
2012-12-27 12:00:14 -08:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
2012-12-12 03:56:38 -08:00
|
|
|
|
|
|
|
public class RecipientProvider {
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
private static final Map<Long,Recipient> recipientCache = Collections.synchronizedMap(new LRUCache<Long,Recipient>(1000));
|
|
|
|
private static final ExecutorService asyncRecipientResolver = Util.newSingleThreadedLifoExecutor();
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2012-12-12 03:56:38 -08:00
|
|
|
private static final String[] CALLER_ID_PROJECTION = new String[] {
|
|
|
|
PhoneLookup.DISPLAY_NAME,
|
|
|
|
PhoneLookup.LOOKUP_KEY,
|
|
|
|
PhoneLookup._ID,
|
2014-04-24 16:40:54 -07:00
|
|
|
PhoneLookup.NUMBER
|
2012-12-12 03:56:38 -08:00
|
|
|
};
|
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
public Recipient getRecipient(Context context, long recipientId, boolean asynchronous) {
|
|
|
|
Recipient cachedRecipient = recipientCache.get(recipientId);
|
2012-12-12 03:56:38 -08:00
|
|
|
|
2012-12-24 08:40:37 -08:00
|
|
|
if (cachedRecipient != null) return cachedRecipient;
|
2014-02-02 19:38:06 -08:00
|
|
|
else if (asynchronous) return getAsynchronousRecipient(context, recipientId);
|
|
|
|
else return getSynchronousRecipient(context, recipientId);
|
2012-12-24 08:40:37 -08:00
|
|
|
}
|
2012-12-12 03:56:38 -08:00
|
|
|
|
2014-04-24 16:40:54 -07:00
|
|
|
private Recipient getSynchronousRecipient(final Context context, final long recipientId) {
|
2012-12-24 08:40:37 -08:00
|
|
|
Log.w("RecipientProvider", "Cache miss [SYNC]!");
|
2012-12-12 03:56:38 -08:00
|
|
|
|
2014-04-24 16:40:54 -07:00
|
|
|
final Recipient recipient;
|
2014-02-02 19:38:06 -08:00
|
|
|
RecipientDetails details;
|
2014-04-24 16:40:54 -07:00
|
|
|
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
2014-02-17 21:42:30 -08:00
|
|
|
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
|
2012-12-24 08:40:37 -08:00
|
|
|
|
2014-02-17 21:42:30 -08:00
|
|
|
if (isGroupRecipient) details = getGroupRecipientDetails(context, number);
|
|
|
|
else details = getRecipientDetails(context, number);
|
2014-01-14 00:26:43 -08:00
|
|
|
|
|
|
|
if (details != null) {
|
2014-04-24 16:40:54 -07:00
|
|
|
recipient = new Recipient(details.name, details.number, recipientId, details.contactUri, details.avatar,
|
2014-06-21 15:31:45 +02:00
|
|
|
details.croppedAvatar);
|
2014-01-14 00:26:43 -08:00
|
|
|
} else {
|
2014-06-21 15:31:45 +02:00
|
|
|
final Bitmap defaultPhoto = isGroupRecipient
|
|
|
|
? ContactPhotoFactory.getDefaultGroupPhoto(context)
|
|
|
|
: ContactPhotoFactory.getDefaultContactPhoto(context);
|
|
|
|
final Bitmap defaultCroppedPhoto = isGroupRecipient
|
|
|
|
? ContactPhotoFactory.getDefaultGroupPhotoCropped(context)
|
|
|
|
: ContactPhotoFactory.getDefaultContactPhotoCropped(context);
|
|
|
|
|
|
|
|
recipient = new Recipient(null, number, recipientId, null, defaultPhoto, defaultCroppedPhoto);
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
recipientCache.put(recipientId, recipient);
|
2014-01-14 00:26:43 -08:00
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
private Recipient getAsynchronousRecipient(final Context context, final long recipientId) {
|
2012-12-24 08:40:37 -08:00
|
|
|
Log.w("RecipientProvider", "Cache miss [ASYNC]!");
|
|
|
|
|
2014-04-24 16:40:54 -07:00
|
|
|
final String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
2014-02-17 21:42:30 -08:00
|
|
|
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
|
2014-01-14 00:26:43 -08:00
|
|
|
|
|
|
|
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
|
|
|
@Override
|
|
|
|
public RecipientDetails call() throws Exception {
|
2014-02-17 21:42:30 -08:00
|
|
|
if (isGroupRecipient) return getGroupRecipientDetails(context, number);
|
|
|
|
else return getRecipientDetails(context, number);
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-28 02:23:00 -05:00
|
|
|
ListenableFutureTask<RecipientDetails> future = new ListenableFutureTask<RecipientDetails>(task);
|
2014-01-14 00:26:43 -08:00
|
|
|
|
|
|
|
asyncRecipientResolver.submit(future);
|
|
|
|
|
2014-03-07 00:58:26 +01:00
|
|
|
Bitmap contactPhoto;
|
|
|
|
Bitmap contactPhotoCropped;
|
|
|
|
|
|
|
|
if (isGroupRecipient) {
|
|
|
|
contactPhoto = ContactPhotoFactory.getDefaultGroupPhoto(context);
|
|
|
|
contactPhotoCropped = ContactPhotoFactory.getDefaultGroupPhotoCropped(context);
|
|
|
|
} else {
|
|
|
|
contactPhoto = ContactPhotoFactory.getDefaultContactPhoto(context);
|
|
|
|
contactPhotoCropped = ContactPhotoFactory.getDefaultContactPhotoCropped(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Recipient recipient = new Recipient(number, contactPhoto, contactPhotoCropped, recipientId, future);
|
2014-02-02 19:38:06 -08:00
|
|
|
recipientCache.put(recipientId, recipient);
|
2014-01-14 00:26:43 -08:00
|
|
|
|
|
|
|
return recipient;
|
2012-12-24 08:40:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clearCache() {
|
|
|
|
recipientCache.clear();
|
2012-12-12 03:56:38 -08:00
|
|
|
}
|
|
|
|
|
2014-02-20 15:41:52 -08:00
|
|
|
public void clearCache(Recipient recipient) {
|
|
|
|
if (recipientCache.containsKey(recipient.getRecipientId()))
|
|
|
|
recipientCache.remove(recipient.getRecipientId());
|
|
|
|
}
|
|
|
|
|
2012-12-24 08:40:37 -08:00
|
|
|
private RecipientDetails getRecipientDetails(Context context, String number) {
|
2012-12-12 03:56:38 -08:00
|
|
|
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
2012-12-24 08:40:37 -08:00
|
|
|
Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION,
|
|
|
|
null, null, null);
|
2012-12-12 03:56:38 -08:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
2014-03-17 23:25:09 -07:00
|
|
|
Bitmap contactPhoto = ContactPhotoFactory.getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
|
|
|
|
cursor.getLong(2)+""));
|
2014-04-24 16:40:54 -07:00
|
|
|
return new RecipientDetails(cursor.getString(0), cursor.getString(3), contactUri, contactPhoto,
|
2014-03-07 00:58:26 +01:00
|
|
|
BitmapUtil.getCircleCroppedBitmap(contactPhoto));
|
2012-12-12 03:56:38 -08:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-01-14 00:26:43 -08:00
|
|
|
private RecipientDetails getGroupRecipientDetails(Context context, String groupId) {
|
|
|
|
try {
|
2014-02-21 17:51:25 -08:00
|
|
|
GroupDatabase.GroupRecord record = DatabaseFactory.getGroupDatabase(context)
|
|
|
|
.getGroup(GroupUtil.getDecodedId(groupId));
|
2014-02-02 19:38:06 -08:00
|
|
|
|
2014-02-21 17:51:25 -08:00
|
|
|
if (record != null) {
|
|
|
|
byte[] avatarBytes = record.getAvatar();
|
|
|
|
Bitmap avatar;
|
2014-02-02 19:38:06 -08:00
|
|
|
|
2014-03-02 13:17:51 -08:00
|
|
|
if (avatarBytes == null) avatar = ContactPhotoFactory.getDefaultGroupPhoto(context);
|
2014-02-21 17:51:25 -08:00
|
|
|
else avatar = BitmapFactory.decodeByteArray(avatarBytes, 0, avatarBytes.length);
|
2014-01-14 00:26:43 -08:00
|
|
|
|
2014-04-24 16:40:54 -07:00
|
|
|
return new RecipientDetails(record.getTitle(), groupId, null, avatar, BitmapUtil.getCircleCroppedBitmap(avatar));
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
return null;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("RecipientProvider", e);
|
|
|
|
return null;
|
|
|
|
}
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2012-12-24 08:40:37 -08:00
|
|
|
public static class RecipientDetails {
|
|
|
|
public final String name;
|
2014-04-24 16:40:54 -07:00
|
|
|
public final String number;
|
2012-12-24 08:40:37 -08:00
|
|
|
public final Bitmap avatar;
|
2014-03-07 00:58:26 +01:00
|
|
|
public final Bitmap croppedAvatar;
|
|
|
|
public final Uri contactUri;
|
|
|
|
|
2014-04-24 16:40:54 -07:00
|
|
|
public RecipientDetails(String name, String number, Uri contactUri, Bitmap avatar, Bitmap croppedAvatar) {
|
2014-03-07 00:58:26 +01:00
|
|
|
this.name = name;
|
2014-04-24 16:40:54 -07:00
|
|
|
this.number = number;
|
2014-03-07 00:58:26 +01:00
|
|
|
this.avatar = avatar;
|
|
|
|
this.croppedAvatar = croppedAvatar;
|
|
|
|
this.contactUri = contactUri;
|
2012-12-24 08:40:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|