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;
|
2012-12-12 03:56:38 -08:00
|
|
|
import android.database.Cursor;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.net.Uri;
|
2012-12-12 03:56:38 -08:00
|
|
|
import android.provider.ContactsContract.Contacts;
|
|
|
|
import android.provider.ContactsContract.PhoneLookup;
|
2015-06-23 10:15:33 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2015-06-09 07:37:20 -07:00
|
|
|
import android.support.annotation.Nullable;
|
2017-08-04 09:28:20 -07:00
|
|
|
import android.text.TextUtils;
|
2012-12-24 08:40:37 -08:00
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2015-07-27 10:49:14 -07:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-06-30 09:16:05 -07:00
|
|
|
import org.thoughtcrime.securesms.color.MaterialColor;
|
2015-07-27 10:49:14 -07:00
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
|
2015-06-23 10:15:33 -07:00
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-01-14 00:26:43 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
2015-06-09 07:37:20 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
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;
|
2014-11-12 09:20:57 -08:00
|
|
|
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
2015-05-04 11:36:18 -07:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
import java.io.IOException;
|
2015-06-09 07:37:20 -07:00
|
|
|
import java.util.Arrays;
|
2015-07-27 10:49:14 -07:00
|
|
|
import java.util.HashMap;
|
2015-06-09 07:37:20 -07:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2012-12-24 08:40:37 -08:00
|
|
|
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
|
|
|
|
2017-01-22 21:57:23 -08:00
|
|
|
class RecipientProvider {
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
private static final String TAG = RecipientProvider.class.getSimpleName();
|
|
|
|
|
2015-07-17 10:16:14 -07:00
|
|
|
private static final RecipientCache recipientCache = new RecipientCache();
|
|
|
|
private static final RecipientsCache recipientsCache = new RecipientsCache();
|
|
|
|
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,
|
2017-02-20 16:48:55 +01:00
|
|
|
PhoneLookup.NUMBER,
|
|
|
|
PhoneLookup.LABEL
|
2012-12-12 03:56:38 -08:00
|
|
|
};
|
|
|
|
|
2015-07-27 10:49:14 -07:00
|
|
|
private static final Map<String, RecipientDetails> STATIC_DETAILS = new HashMap<String, RecipientDetails>() {{
|
2017-07-26 09:59:15 -07:00
|
|
|
put("262966", new RecipientDetails("Amazon", null, null,
|
2015-07-27 10:49:14 -07:00
|
|
|
ContactPhotoFactory.getResourceContactPhoto(R.drawable.ic_amazon),
|
|
|
|
ContactColors.UNKNOWN_COLOR));
|
|
|
|
}};
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
@NonNull Recipient getRecipient(Context context, Address address, boolean asynchronous) {
|
|
|
|
Recipient cachedRecipient = recipientCache.get(address);
|
2017-01-22 21:57:23 -08:00
|
|
|
if (cachedRecipient != null && !cachedRecipient.isStale() && (asynchronous || !cachedRecipient.isResolving())) {
|
|
|
|
return cachedRecipient;
|
|
|
|
}
|
2015-06-23 10:15:33 -07:00
|
|
|
|
|
|
|
if (asynchronous) {
|
2017-07-26 09:59:15 -07:00
|
|
|
cachedRecipient = new Recipient(address, cachedRecipient, getRecipientDetailsAsync(context, address));
|
2015-06-23 10:15:33 -07:00
|
|
|
} else {
|
2017-07-26 09:59:15 -07:00
|
|
|
cachedRecipient = new Recipient(address, getRecipientDetailsSync(context, address));
|
2015-06-23 10:15:33 -07:00
|
|
|
}
|
2012-12-12 03:56:38 -08:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
recipientCache.set(address, cachedRecipient);
|
2015-06-23 10:15:33 -07:00
|
|
|
return cachedRecipient;
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
@NonNull Recipients getRecipients(Context context, Address[] recipientAddresses, boolean asynchronous) {
|
|
|
|
Recipients cachedRecipients = recipientsCache.get(new RecipientAddresses(recipientAddresses));
|
2017-01-22 21:57:23 -08:00
|
|
|
if (cachedRecipients != null && !cachedRecipients.isStale() && (asynchronous || !cachedRecipients.isResolving())) {
|
|
|
|
return cachedRecipients;
|
|
|
|
}
|
2015-06-09 07:37:20 -07:00
|
|
|
|
|
|
|
List<Recipient> recipientList = new LinkedList<>();
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
for (Address address : recipientAddresses) {
|
|
|
|
recipientList.add(getRecipient(context, address, asynchronous));
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
if (asynchronous) cachedRecipients = new Recipients(recipientList, cachedRecipients, getRecipientsPreferencesAsync(context, recipientAddresses));
|
|
|
|
else cachedRecipients = new Recipients(recipientList, getRecipientsPreferencesSync(context, recipientAddresses));
|
2015-06-09 07:37:20 -07:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
recipientsCache.set(new RecipientAddresses(recipientAddresses), cachedRecipients);
|
2015-06-09 07:37:20 -07:00
|
|
|
return cachedRecipients;
|
2012-12-24 08:40:37 -08:00
|
|
|
}
|
2012-12-12 03:56:38 -08:00
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
void clearCache() {
|
2015-07-17 10:16:14 -07:00
|
|
|
recipientCache.reset();
|
|
|
|
recipientsCache.reset();
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private @NonNull ListenableFutureTask<RecipientDetails> getRecipientDetailsAsync(final Context context, final @NonNull Address address)
|
2015-06-23 10:15:33 -07:00
|
|
|
{
|
2014-01-14 00:26:43 -08:00
|
|
|
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
|
|
|
@Override
|
|
|
|
public RecipientDetails call() throws Exception {
|
2017-07-26 09:59:15 -07:00
|
|
|
return getRecipientDetailsSync(context, address);
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-04 11:36:18 -07:00
|
|
|
ListenableFutureTask<RecipientDetails> future = new ListenableFutureTask<>(task);
|
2014-01-14 00:26:43 -08:00
|
|
|
asyncRecipientResolver.submit(future);
|
2015-06-23 10:15:33 -07:00
|
|
|
return future;
|
2012-12-24 08:40:37 -08:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, @NonNull Address address) {
|
|
|
|
if (address.isGroup()) return getGroupRecipientDetails(context, address);
|
|
|
|
else return getIndividualRecipientDetails(context, address);
|
2014-02-20 15:41:52 -08:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, @NonNull Address address) {
|
|
|
|
Optional<RecipientsPreferences> preferences = DatabaseFactory.getRecipientPreferenceDatabase(context).getRecipientsPreferences(new Address[]{address});
|
2015-06-30 09:16:05 -07:00
|
|
|
MaterialColor color = preferences.isPresent() ? preferences.get().getColor() : null;
|
2017-08-01 09:57:50 -07:00
|
|
|
|
2017-08-04 09:28:20 -07:00
|
|
|
if (address.isPhone() && !TextUtils.isEmpty(address.toPhoneString())) {
|
2017-08-01 09:57:50 -07:00
|
|
|
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address.toPhoneString()));
|
|
|
|
Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION, null, null, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
final String resultNumber = cursor.getString(3);
|
|
|
|
if (resultNumber != null) {
|
|
|
|
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
|
|
|
String name = resultNumber.equals(cursor.getString(0)) ? null : cursor.getString(0);
|
|
|
|
ContactPhoto contactPhoto = ContactPhotoFactory.getContactPhoto(context,
|
|
|
|
Uri.withAppendedPath(Contacts.CONTENT_URI, cursor.getLong(2) + ""),
|
|
|
|
name);
|
|
|
|
|
|
|
|
return new RecipientDetails(cursor.getString(0), cursor.getString(4), contactUri, contactPhoto, color);
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "resultNumber is null");
|
|
|
|
}
|
2016-04-24 17:56:03 +02:00
|
|
|
}
|
2017-08-01 09:57:50 -07:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
2012-12-12 03:56:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 09:57:50 -07:00
|
|
|
if (STATIC_DETAILS.containsKey(address.serialize())) return STATIC_DETAILS.get(address.serialize());
|
|
|
|
else return new RecipientDetails(null, null, null, ContactPhotoFactory.getDefaultContactPhoto(null), color);
|
2012-12-12 03:56:38 -08:00
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private @NonNull RecipientDetails getGroupRecipientDetails(Context context, Address groupId) {
|
2014-01-14 00:26:43 -08:00
|
|
|
try {
|
2017-01-22 21:23:51 -08:00
|
|
|
GroupDatabase.GroupRecord record = DatabaseFactory.getGroupDatabase(context)
|
2017-07-26 09:59:15 -07:00
|
|
|
.getGroup(GroupUtil.getDecodedId(groupId.toGroupString()));
|
2014-02-02 19:38:06 -08:00
|
|
|
|
2014-02-21 17:51:25 -08:00
|
|
|
if (record != null) {
|
2015-06-23 10:15:33 -07:00
|
|
|
ContactPhoto contactPhoto = ContactPhotoFactory.getGroupContactPhoto(record.getAvatar());
|
2017-01-22 21:23:51 -08:00
|
|
|
String title = record.getTitle();
|
|
|
|
|
|
|
|
if (title == null) {
|
|
|
|
title = context.getString(R.string.RecipientProvider_unnamed_group);;
|
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
return new RecipientDetails(title, null, null, contactPhoto, null);
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
return new RecipientDetails(context.getString(R.string.RecipientProvider_unnamed_group), null, null, ContactPhotoFactory.getDefaultGroupPhoto(), null);
|
2014-02-02 19:38:06 -08:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("RecipientProvider", e);
|
2017-07-26 09:59:15 -07:00
|
|
|
return new RecipientDetails(context.getString(R.string.RecipientProvider_unnamed_group), null, null, ContactPhotoFactory.getDefaultGroupPhoto(), null);
|
2014-02-02 19:38:06 -08:00
|
|
|
}
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private @Nullable RecipientsPreferences getRecipientsPreferencesSync(Context context, Address[] addresses) {
|
2015-06-09 07:37:20 -07:00
|
|
|
return DatabaseFactory.getRecipientPreferenceDatabase(context)
|
2017-07-26 09:59:15 -07:00
|
|
|
.getRecipientsPreferences(addresses)
|
2015-06-09 07:37:20 -07:00
|
|
|
.orNull();
|
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private ListenableFutureTask<RecipientsPreferences> getRecipientsPreferencesAsync(final Context context, final Address[] addresses) {
|
2015-06-09 07:37:20 -07:00
|
|
|
ListenableFutureTask<RecipientsPreferences> task = new ListenableFutureTask<>(new Callable<RecipientsPreferences>() {
|
|
|
|
@Override
|
|
|
|
public RecipientsPreferences call() throws Exception {
|
2017-07-26 09:59:15 -07:00
|
|
|
return getRecipientsPreferencesSync(context, addresses);
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
asyncRecipientResolver.execute(task);
|
|
|
|
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
2012-12-24 08:40:37 -08:00
|
|
|
public static class RecipientDetails {
|
2015-06-30 09:16:05 -07:00
|
|
|
@Nullable public final String name;
|
2017-02-20 16:48:55 +01:00
|
|
|
@Nullable public final String customLabel;
|
2015-06-30 09:16:05 -07:00
|
|
|
@NonNull public final ContactPhoto avatar;
|
|
|
|
@Nullable public final Uri contactUri;
|
|
|
|
@Nullable public final MaterialColor color;
|
2015-06-23 15:10:50 -07:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public RecipientDetails(@Nullable String name, @Nullable String customLabel,
|
|
|
|
@Nullable Uri contactUri, @NonNull ContactPhoto avatar,
|
|
|
|
@Nullable MaterialColor color)
|
2015-06-23 15:10:50 -07:00
|
|
|
{
|
2017-02-20 16:48:55 +01:00
|
|
|
this.name = name;
|
|
|
|
this.customLabel = customLabel;
|
|
|
|
this.avatar = avatar;
|
|
|
|
this.contactUri = contactUri;
|
|
|
|
this.color = color;
|
2012-12-24 08:40:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private static class RecipientAddresses {
|
|
|
|
private final Address[] addresses;
|
2015-06-09 07:37:20 -07:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private RecipientAddresses(Address[] addresses) {
|
|
|
|
this.addresses = addresses;
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean equals(Object other) {
|
2017-07-26 09:59:15 -07:00
|
|
|
if (other == null || !(other instanceof RecipientAddresses)) return false;
|
|
|
|
return Arrays.equals(this.addresses, ((RecipientAddresses) other).addresses);
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
2017-07-26 09:59:15 -07:00
|
|
|
return Arrays.hashCode(addresses);
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 10:16:14 -07:00
|
|
|
private static class RecipientCache {
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private final Map<Address,Recipient> cache = new LRUCache<>(1000);
|
2015-07-17 10:16:14 -07:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public synchronized Recipient get(Address address) {
|
|
|
|
return cache.get(address);
|
2015-07-17 10:16:14 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public synchronized void set(Address address, Recipient recipient) {
|
|
|
|
cache.put(address, recipient);
|
2015-07-17 10:16:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void reset() {
|
|
|
|
for (Recipient recipient : cache.values()) {
|
|
|
|
recipient.setStale();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class RecipientsCache {
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private final Map<RecipientAddresses,Recipients> cache = new LRUCache<>(1000);
|
2015-07-17 10:16:14 -07:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public synchronized Recipients get(RecipientAddresses addresses) {
|
|
|
|
return cache.get(addresses);
|
2015-07-17 10:16:14 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public synchronized void set(RecipientAddresses addresses, Recipients recipients) {
|
|
|
|
cache.put(addresses, recipients);
|
2015-07-17 10:16:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void reset() {
|
|
|
|
for (Recipients recipients : cache.values()) {
|
|
|
|
recipients.setStale();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
|
2015-05-04 11:36:18 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|