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;
|
2012-12-24 08:40:37 -08:00
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
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;
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.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;
|
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;
|
2015-06-29 15:33:36 -07:00
|
|
|
import org.whispersystems.libaxolotl.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;
|
2012-12-24 08:40:37 -08:00
|
|
|
import java.util.Collections;
|
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
|
|
|
|
|
|
|
public 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-06-09 07:37:20 -07:00
|
|
|
private static final Map<Long,Recipient> recipientCache = Collections.synchronizedMap(new LRUCache<Long,Recipient>(1000));
|
|
|
|
private static final Map<RecipientIds,Recipients> recipientsCache = Collections.synchronizedMap(new LRUCache<RecipientIds, Recipients>(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
|
|
|
};
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
Recipient getRecipient(Context context, long recipientId, boolean asynchronous) {
|
2014-02-02 19:38:06 -08:00
|
|
|
Recipient cachedRecipient = recipientCache.get(recipientId);
|
2015-06-23 10:15:33 -07:00
|
|
|
if (cachedRecipient != null) return cachedRecipient;
|
|
|
|
|
|
|
|
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
|
|
|
|
|
|
|
if (asynchronous) {
|
2015-06-29 15:33:36 -07:00
|
|
|
cachedRecipient = new Recipient(recipientId, number, getRecipientDetailsAsync(context, recipientId, number));
|
2015-06-23 10:15:33 -07:00
|
|
|
} else {
|
2015-06-29 15:33:36 -07:00
|
|
|
cachedRecipient = new Recipient(recipientId, getRecipientDetailsSync(context, recipientId, number));
|
2015-06-23 10:15:33 -07:00
|
|
|
}
|
2012-12-12 03:56:38 -08:00
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
recipientCache.put(recipientId, cachedRecipient);
|
|
|
|
return cachedRecipient;
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Recipients getRecipients(Context context, long[] recipientIds, boolean asynchronous) {
|
|
|
|
Recipients cachedRecipients = recipientsCache.get(new RecipientIds(recipientIds));
|
|
|
|
if (cachedRecipients != null) return cachedRecipients;
|
|
|
|
|
|
|
|
List<Recipient> recipientList = new LinkedList<>();
|
|
|
|
|
|
|
|
for (long recipientId : recipientIds) {
|
2015-06-22 18:01:54 -07:00
|
|
|
recipientList.add(getRecipient(context, recipientId, asynchronous));
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (asynchronous) cachedRecipients = new Recipients(recipientList, getRecipientsPreferencesAsync(context, recipientIds));
|
|
|
|
else cachedRecipients = new Recipients(recipientList, getRecipientsPreferencesSync(context, recipientIds));
|
|
|
|
|
|
|
|
recipientsCache.put(new RecipientIds(recipientIds), cachedRecipients);
|
|
|
|
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() {
|
|
|
|
recipientCache.clear();
|
|
|
|
recipientsCache.clear();
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
private @NonNull ListenableFutureTask<RecipientDetails> getRecipientDetailsAsync(final Context context,
|
2015-06-29 15:33:36 -07:00
|
|
|
final long recipientId,
|
2015-06-23 10:15:33 -07:00
|
|
|
final String number)
|
|
|
|
{
|
2014-01-14 00:26:43 -08:00
|
|
|
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
|
|
|
@Override
|
|
|
|
public RecipientDetails call() throws Exception {
|
2015-06-29 15:33:36 -07:00
|
|
|
return getRecipientDetailsSync(context, recipientId, number);
|
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
|
|
|
}
|
|
|
|
|
2015-06-29 15:33:36 -07:00
|
|
|
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, long recipientId, String number) {
|
2015-06-23 10:15:33 -07:00
|
|
|
if (GroupUtil.isEncodedGroup(number)) return getGroupRecipientDetails(context, number);
|
2015-06-29 15:33:36 -07:00
|
|
|
else return getIndividualRecipientDetails(context, recipientId, number);
|
2014-02-20 15:41:52 -08:00
|
|
|
}
|
|
|
|
|
2015-06-29 15:33:36 -07:00
|
|
|
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, long recipientId, String number) {
|
|
|
|
Optional<RecipientsPreferences> preferences = DatabaseFactory.getRecipientPreferenceDatabase(context).getRecipientsPreferences(new long[]{recipientId});
|
2015-06-30 09:16:05 -07:00
|
|
|
MaterialColor color = preferences.isPresent() ? preferences.get().getColor() : null;
|
2015-06-29 15:33:36 -07:00
|
|
|
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
|
|
|
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()) {
|
2015-06-23 10:15:33 -07:00
|
|
|
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
|
|
|
String name = cursor.getString(3).equals(cursor.getString(0)) ? null : cursor.getString(0);
|
|
|
|
ContactPhoto contactPhoto = ContactPhotoFactory.getContactPhoto(context,
|
|
|
|
Uri.withAppendedPath(Contacts.CONTENT_URI, cursor.getLong(2) + ""),
|
|
|
|
name);
|
|
|
|
|
2015-06-29 15:33:36 -07:00
|
|
|
return new RecipientDetails(cursor.getString(0), cursor.getString(3), contactUri, contactPhoto, color);
|
2012-12-12 03:56:38 -08:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
2015-06-29 15:33:36 -07:00
|
|
|
return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(null), color);
|
2012-12-12 03:56:38 -08:00
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
private @NonNull RecipientDetails getGroupRecipientDetails(Context context, String groupId) {
|
2014-01-14 00:26:43 -08:00
|
|
|
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) {
|
2015-06-23 10:15:33 -07:00
|
|
|
ContactPhoto contactPhoto = ContactPhotoFactory.getGroupContactPhoto(record.getAvatar());
|
2015-06-30 09:16:05 -07:00
|
|
|
return new RecipientDetails(record.getTitle(), groupId, null, contactPhoto, null);
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2015-06-30 09:16:05 -07:00
|
|
|
return new RecipientDetails(null, groupId, null, ContactPhotoFactory.getDefaultGroupPhoto(), null);
|
2014-02-02 19:38:06 -08:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("RecipientProvider", e);
|
2015-06-30 09:16:05 -07:00
|
|
|
return new RecipientDetails(null, groupId, null, ContactPhotoFactory.getDefaultGroupPhoto(), null);
|
2014-02-02 19:38:06 -08:00
|
|
|
}
|
2014-01-14 00:26:43 -08:00
|
|
|
}
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
private @Nullable RecipientsPreferences getRecipientsPreferencesSync(Context context, long[] recipientIds) {
|
|
|
|
return DatabaseFactory.getRecipientPreferenceDatabase(context)
|
|
|
|
.getRecipientsPreferences(recipientIds)
|
|
|
|
.orNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
private ListenableFutureTask<RecipientsPreferences> getRecipientsPreferencesAsync(final Context context, final long[] recipientIds) {
|
|
|
|
ListenableFutureTask<RecipientsPreferences> task = new ListenableFutureTask<>(new Callable<RecipientsPreferences>() {
|
|
|
|
@Override
|
|
|
|
public RecipientsPreferences call() throws Exception {
|
|
|
|
return getRecipientsPreferencesSync(context, recipientIds);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
@NonNull public final String number;
|
|
|
|
@NonNull public final ContactPhoto avatar;
|
|
|
|
@Nullable public final Uri contactUri;
|
|
|
|
@Nullable public final MaterialColor color;
|
2015-06-23 15:10:50 -07:00
|
|
|
|
|
|
|
public RecipientDetails(@Nullable String name, @NonNull String number,
|
2015-06-29 15:33:36 -07:00
|
|
|
@Nullable Uri contactUri, @NonNull ContactPhoto avatar,
|
2015-06-30 09:16:05 -07:00
|
|
|
@Nullable MaterialColor color)
|
2015-06-23 15:10:50 -07:00
|
|
|
{
|
2015-06-30 09:16:05 -07:00
|
|
|
this.name = name;
|
|
|
|
this.number = number;
|
|
|
|
this.avatar = avatar;
|
|
|
|
this.contactUri = contactUri;
|
|
|
|
this.color = color;
|
2012-12-24 08:40:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
private static class RecipientIds {
|
|
|
|
private final long[] ids;
|
|
|
|
|
|
|
|
private RecipientIds(long[] ids) {
|
|
|
|
this.ids = ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
if (other == null || !(other instanceof RecipientIds)) return false;
|
|
|
|
return Arrays.equals(this.ids, ((RecipientIds) other).ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
|
|
|
return Arrays.hashCode(ids);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-04 11:36:18 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|