2017-10-16 13:11:42 -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-06-23 10:15:33 -07:00
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
2017-10-16 13:11:42 -07:00
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.FallbackContactPhoto;
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.GeneratedContactPhoto;
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.GroupRecordContactPhoto;
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ProfileContactPhoto;
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ResourceContactPhoto;
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.SystemContactPhoto;
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.TransparentContactPhoto;
|
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;
|
2017-08-07 16:47:38 -07:00
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
|
2017-08-21 18:47:37 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientSettings;
|
2017-08-22 10:44:04 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase.RegisteredState;
|
2017-08-21 18:37:39 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase.VibrateState;
|
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
|
|
|
|
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 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,
|
2017-10-16 13:11:42 -07:00
|
|
|
PhoneLookup.LABEL,
|
|
|
|
PhoneLookup.PHOTO_URI
|
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-10-16 13:11:42 -07:00
|
|
|
put("262966", new RecipientDetails("Amazon", null, null, null, new ResourceContactPhoto(R.drawable.ic_amazon), false, null, null));
|
2015-07-27 10:49:14 -07:00
|
|
|
}};
|
|
|
|
|
2017-08-21 18:47:37 -07:00
|
|
|
@NonNull Recipient getRecipient(Context context, Address address, Optional<RecipientSettings> settings, Optional<GroupRecord> groupRecord, boolean asynchronous) {
|
2017-07-26 09:59:15 -07:00
|
|
|
Recipient cachedRecipient = recipientCache.get(address);
|
2017-09-06 00:11:11 -07:00
|
|
|
|
2017-11-20 14:48:39 -08:00
|
|
|
if (cachedRecipient != null && (asynchronous || !cachedRecipient.isResolving()) && ((!groupRecord.isPresent() && !settings.isPresent()) || !cachedRecipient.isResolving() || cachedRecipient.getName() != null)) {
|
2017-01-22 21:57:23 -08:00
|
|
|
return cachedRecipient;
|
|
|
|
}
|
2015-06-23 10:15:33 -07:00
|
|
|
|
2017-08-21 18:47:37 -07:00
|
|
|
Optional<RecipientDetails> prefetchedRecipientDetails = createPrefetchedRecipientDetails(context, address, settings, groupRecord);
|
2017-08-07 16:47:38 -07:00
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
if (asynchronous) {
|
2017-08-21 18:47:37 -07:00
|
|
|
cachedRecipient = new Recipient(address, cachedRecipient, prefetchedRecipientDetails, getRecipientDetailsAsync(context, address, settings, groupRecord));
|
2015-06-23 10:15:33 -07:00
|
|
|
} else {
|
2017-08-21 18:47:37 -07:00
|
|
|
cachedRecipient = new Recipient(address, getRecipientDetailsSync(context, address, settings, groupRecord, false));
|
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-08-07 16:47:38 -07:00
|
|
|
private @NonNull Optional<RecipientDetails> createPrefetchedRecipientDetails(@NonNull Context context, @NonNull Address address,
|
2017-08-21 18:47:37 -07:00
|
|
|
@NonNull Optional<RecipientSettings> settings,
|
2017-08-07 16:47:38 -07:00
|
|
|
@NonNull Optional<GroupRecord> groupRecord)
|
|
|
|
{
|
2017-08-21 18:47:37 -07:00
|
|
|
if (address.isGroup() && settings.isPresent() && groupRecord.isPresent()) {
|
|
|
|
return Optional.of(getGroupRecipientDetails(context, address, groupRecord, settings, true));
|
|
|
|
} else if (!address.isGroup() && settings.isPresent()) {
|
2017-10-16 13:11:42 -07:00
|
|
|
return Optional.of(new RecipientDetails(null, null, null, null, new TransparentContactPhoto(), !TextUtils.isEmpty(settings.get().getSystemDisplayName()), settings.get(), null));
|
2017-08-07 16:47:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
|
|
|
|
2017-08-21 18:47:37 -07:00
|
|
|
private @NonNull ListenableFutureTask<RecipientDetails> getRecipientDetailsAsync(final Context context, final @NonNull Address address, final @NonNull Optional<RecipientSettings> settings, final @NonNull Optional<GroupRecord> groupRecord)
|
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-08-21 18:47:37 -07:00
|
|
|
return getRecipientDetailsSync(context, address, settings, groupRecord, true);
|
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-08-21 18:47:37 -07:00
|
|
|
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, @NonNull Address address, Optional<RecipientSettings> settings, Optional<GroupRecord> groupRecord, boolean nestedAsynchronous) {
|
|
|
|
if (address.isGroup()) return getGroupRecipientDetails(context, address, groupRecord, settings, nestedAsynchronous);
|
|
|
|
else return getIndividualRecipientDetails(context, address, settings);
|
2014-02-20 15:41:52 -08:00
|
|
|
}
|
|
|
|
|
2017-08-21 18:47:37 -07:00
|
|
|
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, @NonNull Address address, Optional<RecipientSettings> settings) {
|
2017-10-16 13:11:42 -07:00
|
|
|
ContactPhoto contactPhoto = null;
|
|
|
|
FallbackContactPhoto fallbackContactPhoto = new GeneratedContactPhoto("#");
|
|
|
|
|
2017-08-21 18:47:37 -07:00
|
|
|
if (!settings.isPresent()) {
|
|
|
|
settings = DatabaseFactory.getRecipientDatabase(context).getRecipientSettings(address);
|
2017-08-06 21:43:11 -07:00
|
|
|
}
|
2017-08-01 09:57:50 -07:00
|
|
|
|
2017-10-16 13:11:42 -07:00
|
|
|
if (settings.isPresent() && !TextUtils.isEmpty(settings.get().getProfileAvatar())) {
|
|
|
|
contactPhoto = new ProfileContactPhoto(address, settings.get().getProfileAvatar());
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-10-16 13:11:42 -07:00
|
|
|
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
|
|
|
String name = resultNumber.equals(cursor.getString(0)) ? null : cursor.getString(0);
|
|
|
|
String photoUri = cursor.getString(5);
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(photoUri)) {
|
|
|
|
contactPhoto = new SystemContactPhoto(address, Uri.parse(photoUri), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(name)) {
|
|
|
|
fallbackContactPhoto = new GeneratedContactPhoto(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new RecipientDetails(cursor.getString(0), cursor.getString(4), contactUri, contactPhoto, fallbackContactPhoto, true, settings.orNull(), null);
|
2017-08-01 09:57:50 -07:00
|
|
|
} 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());
|
2017-10-16 13:11:42 -07:00
|
|
|
else return new RecipientDetails(null, null, null, contactPhoto, fallbackContactPhoto, false, settings.orNull(), null);
|
2012-12-12 03:56:38 -08:00
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2017-08-21 18:47:37 -07:00
|
|
|
private @NonNull RecipientDetails getGroupRecipientDetails(Context context, Address groupId, Optional<GroupRecord> groupRecord, Optional<RecipientSettings> settings, boolean asynchronous) {
|
2017-10-16 13:11:42 -07:00
|
|
|
ContactPhoto contactPhoto = null;
|
|
|
|
FallbackContactPhoto fallbackContactPhoto = new ResourceContactPhoto(R.drawable.ic_group_white_24dp, R.drawable.ic_group_large);
|
|
|
|
|
2017-08-07 16:47:38 -07:00
|
|
|
if (!groupRecord.isPresent()) {
|
|
|
|
groupRecord = DatabaseFactory.getGroupDatabase(context).getGroup(groupId.toGroupString());
|
|
|
|
}
|
2014-02-02 19:38:06 -08:00
|
|
|
|
2017-08-21 18:47:37 -07:00
|
|
|
if (!settings.isPresent()) {
|
|
|
|
settings = DatabaseFactory.getRecipientDatabase(context).getRecipientSettings(groupId);
|
2017-08-07 16:47:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (groupRecord.isPresent()) {
|
|
|
|
String title = groupRecord.get().getTitle();
|
|
|
|
List<Address> memberAddresses = groupRecord.get().getMembers();
|
2017-08-01 08:56:00 -07:00
|
|
|
List<Recipient> members = new LinkedList<>();
|
2017-01-22 21:23:51 -08:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
for (Address memberAddress : memberAddresses) {
|
2017-08-07 16:47:38 -07:00
|
|
|
members.add(getRecipient(context, memberAddress, Optional.absent(), Optional.absent(), asynchronous));
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
if (!groupId.isMmsGroup() && title == null) {
|
|
|
|
title = context.getString(R.string.RecipientProvider_unnamed_group);;
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
|
2017-10-16 13:11:42 -07:00
|
|
|
if (groupRecord.get().getAvatar() != null) {
|
|
|
|
contactPhoto = new GroupRecordContactPhoto(groupId, groupRecord.get().getAvatarId());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new RecipientDetails(title, null, null, contactPhoto, fallbackContactPhoto, false, settings.orNull(), members);
|
2017-08-01 08:56:00 -07:00
|
|
|
}
|
2015-06-09 07:37:20 -07:00
|
|
|
|
2017-10-16 13:11:42 -07:00
|
|
|
return new RecipientDetails(context.getString(R.string.RecipientProvider_unnamed_group), null, null, contactPhoto, fallbackContactPhoto, false, settings.orNull(), null);
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
static class RecipientDetails {
|
2017-10-16 13:11:42 -07:00
|
|
|
@Nullable public final String name;
|
|
|
|
@Nullable public final String customLabel;
|
|
|
|
@Nullable public final ContactPhoto avatar;
|
|
|
|
@NonNull public final FallbackContactPhoto fallbackAvatar;
|
|
|
|
@Nullable public final Uri contactUri;
|
|
|
|
@Nullable public final MaterialColor color;
|
|
|
|
@Nullable public final Uri ringtone;
|
|
|
|
public final long mutedUntil;
|
|
|
|
@Nullable public final VibrateState vibrateState;
|
|
|
|
public final boolean blocked;
|
|
|
|
public final int expireMessages;
|
|
|
|
@NonNull public final List<Recipient> participants;
|
|
|
|
@Nullable public final String profileName;
|
|
|
|
public final boolean seenInviteReminder;
|
|
|
|
public final Optional<Integer> defaultSubscriptionId;
|
|
|
|
@NonNull public final RegisteredState registered;
|
|
|
|
@Nullable public final byte[] profileKey;
|
|
|
|
@Nullable public final String profileAvatar;
|
|
|
|
public final boolean profileSharing;
|
|
|
|
public final boolean systemContact;
|
2015-06-23 15:10:50 -07:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public RecipientDetails(@Nullable String name, @Nullable String customLabel,
|
2017-10-16 13:11:42 -07:00
|
|
|
@Nullable Uri contactUri, @Nullable ContactPhoto avatar,
|
|
|
|
@NonNull FallbackContactPhoto fallbackAvatar,
|
2017-09-06 15:49:10 -07:00
|
|
|
boolean systemContact, @Nullable RecipientSettings settings,
|
2017-08-01 08:56:00 -07:00
|
|
|
@Nullable List<Recipient> participants)
|
2015-06-23 15:10:50 -07:00
|
|
|
{
|
2017-08-22 10:44:04 -07:00
|
|
|
this.customLabel = customLabel;
|
|
|
|
this.avatar = avatar;
|
2017-10-16 13:11:42 -07:00
|
|
|
this.fallbackAvatar = fallbackAvatar;
|
2017-08-22 10:44:04 -07:00
|
|
|
this.contactUri = contactUri;
|
|
|
|
this.color = settings != null ? settings.getColor() : null;
|
|
|
|
this.ringtone = settings != null ? settings.getRingtone() : null;
|
|
|
|
this.mutedUntil = settings != null ? settings.getMuteUntil() : 0;
|
|
|
|
this.vibrateState = settings != null ? settings.getVibrateState() : null;
|
|
|
|
this.blocked = settings != null && settings.isBlocked();
|
|
|
|
this.expireMessages = settings != null ? settings.getExpireMessages() : 0;
|
|
|
|
this.participants = participants == null ? new LinkedList<Recipient>() : participants;
|
|
|
|
this.profileName = settings != null ? settings.getProfileName() : null;
|
|
|
|
this.seenInviteReminder = settings != null && settings.hasSeenInviteReminder();
|
|
|
|
this.defaultSubscriptionId = settings != null ? settings.getDefaultSubscriptionId() : Optional.absent();
|
|
|
|
this.registered = settings != null ? settings.getRegistered() : RegisteredState.UNKNOWN;
|
|
|
|
this.profileKey = settings != null ? settings.getProfileKey() : null;
|
|
|
|
this.profileAvatar = settings != null ? settings.getProfileAvatar() : null;
|
|
|
|
this.profileSharing = settings != null && settings.isProfileSharing();
|
2017-09-06 15:49:10 -07:00
|
|
|
this.systemContact = systemContact;
|
2017-08-07 16:47:38 -07:00
|
|
|
|
2017-08-21 18:47:37 -07:00
|
|
|
if (name == null && settings != null) this.name = settings.getSystemDisplayName();
|
2017-08-22 10:44:04 -07:00
|
|
|
else this.name = name;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|