session-android/src/org/thoughtcrime/securesms/recipients/RecipientFactory.java

145 lines
4.8 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
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.
*
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;
import android.content.Intent;
import android.support.annotation.NonNull;
2014-11-12 19:15:05 +00:00
import android.text.TextUtils;
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.libsignal.util.guava.Optional;
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 {
public static final String RECIPIENT_CLEAR_ACTION = "org.thoughtcrime.securesms.database.RecipientFactory.CLEAR";
private static final RecipientProvider provider = new RecipientProvider();
public static Recipients getRecipientsForIds(Context context, String recipientIds, boolean asynchronous) {
2014-11-12 19:15:05 +00:00
if (TextUtils.isEmpty(recipientIds))
return new Recipients();
2012-12-01 03:48:48 +00:00
return getRecipientsForIds(context, Util.split(recipientIds, " "), asynchronous);
}
public static @NonNull Recipients getRecipientsFor(Context context, Collection<Recipient> recipients, boolean asynchronous) {
long[] ids = new long[recipients.size()];
int i = 0;
for (Recipient recipient : recipients) {
ids[i++] = recipient.getRecipientId();
2011-12-20 18:20:44 +00:00
}
return provider.getRecipients(context, ids, asynchronous);
2011-12-20 18:20:44 +00:00
}
public static Recipients getRecipientsFor(Context context, Recipient recipient, boolean asynchronous) {
long[] ids = new long[1];
ids[0] = recipient.getRecipientId();
return provider.getRecipients(context, ids, asynchronous);
}
public @NonNull static Recipient getRecipientForId(Context context, long recipientId, boolean asynchronous) {
return provider.getRecipient(context, recipientId, asynchronous);
2011-12-20 18:20:44 +00:00
}
public @NonNull static Recipients getRecipientsForIds(Context context, long[] recipientIds, boolean asynchronous) {
return provider.getRecipients(context, recipientIds, asynchronous);
}
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, ",");
List<String> ids = new LinkedList<>();
2011-12-20 18:20:44 +00:00
while (tokenizer.hasMoreTokens()) {
Optional<Long> id = getRecipientIdFromNumber(context, tokenizer.nextToken());
if (id.isPresent()) {
ids.add(String.valueOf(id.get()));
}
}
return getRecipientsForIds(context, ids, asynchronous);
}
public static @NonNull Recipients getRecipientsFromStrings(@NonNull Context context, @NonNull List<String> numbers, boolean asynchronous) {
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);
}
private static @NonNull Recipients getRecipientsForIds(Context context, List<String> idStrings, boolean asynchronous) {
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
}
return provider.getRecipients(context, ids, asynchronous);
2011-12-20 18:20:44 +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
}
return Optional.of(CanonicalAddressDatabase.getInstance(context).getCanonicalAddressId(number));
2011-12-20 18:20:44 +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);
}
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);
return value;
2011-12-20 18:20:44 +00:00
}
public static void clearCache(Context context) {
provider.clearCache();
context.sendBroadcast(new Intent(RECIPIENT_CLEAR_ACTION));
2011-12-20 18:20:44 +00:00
}
2011-12-20 18:20:44 +00:00
}