2012-09-12 01:23:19 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-12 01:23:19 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00: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-12 01:23:19 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00: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;
|
2012-12-12 11:56:38 +00:00
|
|
|
import android.database.Cursor;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.net.Uri;
|
2012-12-24 16:40:37 +00:00
|
|
|
import android.os.AsyncTask;
|
2012-12-12 11:56:38 +00:00
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.provider.ContactsContract.Contacts;
|
|
|
|
import android.provider.ContactsContract.PhoneLookup;
|
2012-12-24 16:40:37 +00:00
|
|
|
import android.util.Log;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
import org.thoughtcrime.securesms.util.LRUCache;
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2012-12-12 11:56:38 +00:00
|
|
|
import java.io.InputStream;
|
2012-12-24 16:40:37 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Map;
|
2012-12-12 11:56:38 +00:00
|
|
|
|
|
|
|
public class RecipientProvider {
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
private static final Map<String,Recipient> recipientCache = Collections.synchronizedMap(new LRUCache<String,Recipient>(1000));
|
|
|
|
// private static final ExecutorService asyncRecipientResolver = Executors.newSingleThreadExecutor();
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2012-12-12 11:56:38 +00:00
|
|
|
private static final String[] CALLER_ID_PROJECTION = new String[] {
|
|
|
|
PhoneLookup.DISPLAY_NAME,
|
|
|
|
PhoneLookup.LOOKUP_KEY,
|
|
|
|
PhoneLookup._ID,
|
|
|
|
};
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
public Recipient getRecipient(Context context, String number, boolean asynchronous) {
|
|
|
|
Recipient cachedRecipient = recipientCache.get(number);
|
2012-12-12 11:56:38 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
if (cachedRecipient != null) return cachedRecipient;
|
|
|
|
else if (asynchronous) return getAsynchronousRecipient(context, number);
|
|
|
|
else return getSynchronousRecipient(context, number);
|
|
|
|
}
|
2012-12-12 11:56:38 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
private Recipient getSynchronousRecipient(Context context, String number) {
|
|
|
|
Log.w("RecipientProvider", "Cache miss [SYNC]!");
|
|
|
|
RecipientDetails details = getRecipientDetails(context, number);
|
|
|
|
Recipient recipient;
|
2012-12-12 11:56:38 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
if (details != null) {
|
|
|
|
recipient = new Recipient(details.name, number, details.contactUri, details.avatar);
|
|
|
|
} else {
|
|
|
|
recipient = new Recipient(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(context));
|
|
|
|
}
|
2012-12-12 11:56:38 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
recipientCache.put(number, recipient);
|
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Recipient getAsynchronousRecipient(final Context context, final String number) {
|
|
|
|
Log.w("RecipientProvider", "Cache miss [ASYNC]!");
|
|
|
|
|
|
|
|
Recipient recipient = new Recipient(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(context));
|
|
|
|
recipientCache.put(number, recipient);
|
|
|
|
|
|
|
|
new AsyncTask<Recipient, Void, RecipientDetails>() {
|
|
|
|
private Recipient recipient;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected RecipientDetails doInBackground(Recipient... recipient) {
|
|
|
|
this.recipient = recipient[0];
|
|
|
|
return getRecipientDetails(context, number);
|
2012-12-12 11:56:38 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(RecipientDetails result) {
|
|
|
|
recipient.updateAsynchronousContent(result);
|
|
|
|
}
|
|
|
|
}.execute(recipient);
|
|
|
|
|
|
|
|
return recipient;
|
|
|
|
|
|
|
|
// ListenableFutureTask<RecipientDetails> future =
|
|
|
|
// new ListenableFutureTask<RecipientDetails>(new Callable<RecipientDetails>() {
|
|
|
|
// @Override
|
|
|
|
// public RecipientDetails call() throws Exception {
|
|
|
|
// return getRecipientDetails(context, number);
|
|
|
|
//// RecipientDetails recipientDetails = getRecipientDetails();
|
|
|
|
////
|
|
|
|
//// if (recipientDeta)
|
|
|
|
////
|
|
|
|
//// Recipient cachedRecipient = recipientCache.get(number);
|
|
|
|
////
|
|
|
|
//// if (cachedRecipient != null) {
|
|
|
|
//// return new RecipientDetails(cachedRecipient.getName(), cachedRecipient.getContactPhoto());
|
|
|
|
//// }
|
|
|
|
////
|
|
|
|
//// Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
|
|
|
//// Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION,
|
|
|
|
//// null, null, null);
|
|
|
|
////
|
|
|
|
//// try {
|
|
|
|
//// if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
//// Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
|
|
|
//// Bitmap contactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
|
|
|
|
//// cursor.getLong(2)+""));
|
|
|
|
////
|
|
|
|
//// recipientCache.put(number, new Recipient(cursor.getString(0), number, contactPhoto));
|
|
|
|
//// return new RecipientDetails(cursor.getString(0), contactPhoto);
|
|
|
|
//// } else {
|
|
|
|
//// recipientCache.put(number, new Recipient(null, number, ContactPhotoFactory.getDefaultContactPhoto(context)));
|
|
|
|
//// }
|
|
|
|
//// } finally {
|
|
|
|
//// if (cursor != null)
|
|
|
|
//// cursor.close();
|
|
|
|
//// }
|
|
|
|
////
|
|
|
|
//// return null;
|
|
|
|
// }
|
|
|
|
// }, null);
|
|
|
|
//
|
|
|
|
// asyncRecipientResolver.submit(future);
|
|
|
|
// Recipient recipient = new Recipient(number, ContactPhotoFactory.getDefaultContactPhoto(context), future);
|
|
|
|
// recipientCache.put(number, recipient);
|
|
|
|
//
|
|
|
|
// return recipient;
|
|
|
|
//// return new Recipient(null, number, ContactPhotoFactory.getDefaultContactPhoto(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearCache() {
|
|
|
|
recipientCache.clear();
|
2012-12-12 11:56:38 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
private RecipientDetails getRecipientDetails(Context context, String number) {
|
2012-12-12 11:56:38 +00:00
|
|
|
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
2012-12-24 16:40:37 +00:00
|
|
|
Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION,
|
|
|
|
null, null, null);
|
2012-12-12 11:56:38 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
|
|
|
Bitmap contactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
|
|
|
|
cursor.getLong(2)+""));
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
return new RecipientDetails(cursor.getString(0), contactUri, contactPhoto);
|
2012-12-12 11:56:38 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-12-12 11:56:38 +00:00
|
|
|
private Bitmap getContactPhoto(Context context, Uri uri) {
|
|
|
|
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
|
|
|
|
|
|
|
|
if (inputStream == null)
|
2012-12-24 16:40:37 +00:00
|
|
|
return ContactPhotoFactory.getDefaultContactPhoto(context);
|
2012-12-12 11:56:38 +00:00
|
|
|
else
|
|
|
|
return BitmapFactory.decodeStream(inputStream);
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
public static class RecipientDetails {
|
|
|
|
public final String name;
|
|
|
|
public final Bitmap avatar;
|
|
|
|
public final Uri contactUri;
|
|
|
|
|
|
|
|
public RecipientDetails(String name, Uri contactUri, Bitmap avatar) {
|
|
|
|
this.name = name;
|
|
|
|
this.avatar = avatar;
|
|
|
|
this.contactUri = contactUri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|