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;
|
|
|
|
|
2012-09-12 01:23:19 +00:00
|
|
|
import android.content.Context;
|
2016-08-16 03:23:56 +00:00
|
|
|
import android.content.Intent;
|
2015-03-03 19:44:49 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2014-11-12 19:15:05 +00:00
|
|
|
import android.text.TextUtils;
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2014-02-03 03:38:06 +00:00
|
|
|
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
|
2015-06-09 14:37:20 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2015-11-05 18:41:43 +00:00
|
|
|
import java.util.Collection;
|
2012-12-01 03:48:48 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2011-12-20 18:20:44 +00:00
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
|
|
|
public class RecipientFactory {
|
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
public static final String RECIPIENT_CLEAR_ACTION = "org.thoughtcrime.securesms.database.RecipientFactory.CLEAR";
|
|
|
|
|
2012-12-12 11:56:38 +00:00
|
|
|
private static final RecipientProvider provider = new RecipientProvider();
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
public static Recipients getRecipientsForIds(Context context, String recipientIds, boolean asynchronous) {
|
2014-11-12 19:15:05 +00:00
|
|
|
if (TextUtils.isEmpty(recipientIds))
|
2015-06-09 14:37:20 +00:00
|
|
|
return new Recipients();
|
2012-12-01 03:48:48 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
return getRecipientsForIds(context, Util.split(recipientIds, " "), asynchronous);
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2015-11-05 18:41:43 +00:00
|
|
|
public static @NonNull Recipients getRecipientsFor(Context context, Collection<Recipient> recipients, boolean asynchronous) {
|
2015-06-09 14:37:20 +00:00
|
|
|
long[] ids = new long[recipients.size()];
|
|
|
|
int i = 0;
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
for (Recipient recipient : recipients) {
|
|
|
|
ids[i++] = recipient.getRecipientId();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
return provider.getRecipients(context, ids, asynchronous);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public static Recipients getRecipientsFor(Context context, Recipient recipient, boolean asynchronous) {
|
|
|
|
long[] ids = new long[1];
|
|
|
|
ids[0] = recipient.getRecipientId();
|
2014-12-18 02:14:19 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
return provider.getRecipients(context, ids, asynchronous);
|
2014-12-18 02:14:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 17:36:21 +00:00
|
|
|
public @NonNull static Recipient getRecipientForId(Context context, long recipientId, boolean asynchronous) {
|
2014-02-03 03:38:06 +00:00
|
|
|
return provider.getRecipient(context, recipientId, asynchronous);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2015-10-28 17:36:21 +00:00
|
|
|
public @NonNull static Recipients getRecipientsForIds(Context context, long[] recipientIds, boolean asynchronous) {
|
2015-06-09 14:37:20 +00:00
|
|
|
return provider.getRecipients(context, recipientIds, asynchronous);
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
public static @NonNull Recipients getRecipientsFromString(Context context, @NonNull String rawText, boolean asynchronous) {
|
2012-12-01 03:48:48 +00:00
|
|
|
StringTokenizer tokenizer = new StringTokenizer(rawText, ",");
|
2015-06-09 14:37:20 +00:00
|
|
|
List<String> ids = new LinkedList<>();
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
2015-06-09 14:37:20 +00:00
|
|
|
Optional<Long> id = getRecipientIdFromNumber(context, tokenizer.nextToken());
|
|
|
|
|
|
|
|
if (id.isPresent()) {
|
|
|
|
ids.add(String.valueOf(id.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return getRecipientsForIds(context, ids, asynchronous);
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
public static @NonNull Recipients getRecipientsFromStrings(@NonNull Context context, @NonNull List<String> numbers, boolean asynchronous) {
|
2015-10-13 01:25:05 +00:00
|
|
|
List<String> ids = new LinkedList<>();
|
|
|
|
|
|
|
|
for (String number : numbers) {
|
|
|
|
Optional<Long> id = getRecipientIdFromNumber(context, number);
|
|
|
|
|
|
|
|
if (id.isPresent()) {
|
|
|
|
ids.add(String.valueOf(id.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return getRecipientsForIds(context, ids, asynchronous);
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
private static @NonNull Recipients getRecipientsForIds(Context context, List<String> idStrings, boolean asynchronous) {
|
2015-06-09 14:37:20 +00:00
|
|
|
long[] ids = new long[idStrings.size()];
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (String id : idStrings) {
|
|
|
|
ids[i++] = Long.parseLong(id);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
return provider.getRecipients(context, ids, asynchronous);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
private static Optional<Long> getRecipientIdFromNumber(Context context, String number) {
|
|
|
|
number = number.trim();
|
|
|
|
|
|
|
|
if (number.isEmpty()) return Optional.absent();
|
|
|
|
|
|
|
|
if (hasBracketedNumber(number)) {
|
|
|
|
number = parseBracketedNumber(number);
|
2014-01-14 08:26:43 +00:00
|
|
|
}
|
2015-06-09 14:37:20 +00:00
|
|
|
|
|
|
|
return Optional.of(CanonicalAddressDatabase.getInstance(context).getCanonicalAddressId(number));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2012-12-01 03:42:30 +00:00
|
|
|
private static boolean hasBracketedNumber(String recipient) {
|
|
|
|
int openBracketIndex = recipient.indexOf('<');
|
|
|
|
|
|
|
|
return (openBracketIndex != -1) &&
|
|
|
|
(recipient.indexOf('>', openBracketIndex) != -1);
|
|
|
|
}
|
|
|
|
|
2014-06-03 21:50:37 +00:00
|
|
|
private static String parseBracketedNumber(String recipient) {
|
2011-12-20 18:20:44 +00:00
|
|
|
int begin = recipient.indexOf('<');
|
2012-12-01 03:42:30 +00:00
|
|
|
int end = recipient.indexOf('>', begin);
|
2011-12-20 18:20:44 +00:00
|
|
|
String value = recipient.substring(begin + 1, end);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2014-06-03 21:50:37 +00:00
|
|
|
return value;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
public static void clearCache(Context context) {
|
2012-12-24 16:40:37 +00:00
|
|
|
provider.clearCache();
|
2016-08-16 03:23:56 +00:00
|
|
|
context.sendBroadcast(new Intent(RECIPIENT_CLEAR_ACTION));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|