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;
|
2013-09-09 23:46:03 +00:00
|
|
|
import android.util.Log;
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2012-12-31 00:42:33 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
2014-02-03 03:38:06 +00:00
|
|
|
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
|
2012-09-12 01:23:19 +00:00
|
|
|
import org.thoughtcrime.securesms.util.NumberUtil;
|
2013-09-09 23:46:03 +00:00
|
|
|
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
|
|
|
import org.whispersystems.textsecure.util.Util;
|
2012-09-12 01:23:19 +00:00
|
|
|
|
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 {
|
|
|
|
|
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-01-14 08:26:43 +00:00
|
|
|
if (Util.isEmpty(recipientIds))
|
2012-12-01 03:48:48 +00:00
|
|
|
return new Recipients(new LinkedList<Recipient>());
|
|
|
|
|
|
|
|
List<Recipient> results = new LinkedList<Recipient>();
|
|
|
|
StringTokenizer tokenizer = new StringTokenizer(recipientIds.trim(), " ");
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
String recipientId = tokenizer.nextToken();
|
2012-12-24 16:40:37 +00:00
|
|
|
Recipient recipient = getRecipientFromProviderId(context, recipientId, asynchronous);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
results.add(recipient);
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return new Recipients(results);
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2014-02-18 04:19:35 +00:00
|
|
|
private static Recipient getRecipientForNumber(Context context, String number, boolean asynchronous) {
|
2014-02-03 03:38:06 +00:00
|
|
|
long recipientId = CanonicalAddressDatabase.getInstance(context).getCanonicalAddress(number);
|
|
|
|
return provider.getRecipient(context, recipientId, asynchronous);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
public static Recipients getRecipientsFromString(Context context, String rawText, boolean asynchronous)
|
|
|
|
throws RecipientFormattingException
|
|
|
|
{
|
2013-05-16 20:18:06 +00:00
|
|
|
if (rawText == null) {
|
|
|
|
throw new RecipientFormattingException("Null recipient string specified");
|
|
|
|
}
|
|
|
|
|
2012-12-01 03:48:48 +00:00
|
|
|
List<Recipient> results = new LinkedList<Recipient>();
|
|
|
|
StringTokenizer tokenizer = new StringTokenizer(rawText, ",");
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
2012-12-24 16:40:37 +00:00
|
|
|
Recipient recipient = parseRecipient(context, tokenizer.nextToken(), asynchronous);
|
2012-09-12 01:23:19 +00:00
|
|
|
if( recipient != null )
|
|
|
|
results.add(recipient);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Recipients(results);
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2013-09-09 23:46:03 +00:00
|
|
|
public static Recipients getRecipientsFromMessage(Context context,
|
|
|
|
IncomingPushMessage message,
|
|
|
|
boolean asynchronous)
|
|
|
|
{
|
|
|
|
try {
|
2014-01-14 08:26:43 +00:00
|
|
|
return getRecipientsFromString(context, message.getSource(), asynchronous);
|
2013-09-09 23:46:03 +00:00
|
|
|
} catch (RecipientFormattingException e) {
|
|
|
|
Log.w("RecipientFactory", e);
|
2014-02-03 03:38:06 +00:00
|
|
|
return new Recipients(Recipient.getUnknownRecipient(context));
|
2013-09-09 23:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
private static Recipient getRecipientFromProviderId(Context context, String recipientId, boolean asynchronous) {
|
2014-02-03 03:38:06 +00:00
|
|
|
try {
|
|
|
|
return provider.getRecipient(context, Long.parseLong(recipientId), asynchronous);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
Log.w("RecipientFactory", e);
|
|
|
|
return Recipient.getUnknownRecipient(context);
|
2014-01-14 08:26:43 +00:00
|
|
|
}
|
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
|
|
|
|
2014-06-03 21:50:37 +00:00
|
|
|
private static Recipient parseRecipient(Context context, String recipient, boolean asynchronous) {
|
2011-12-20 18:20:44 +00:00
|
|
|
recipient = recipient.trim();
|
|
|
|
|
2012-09-12 01:23:19 +00:00
|
|
|
if( recipient.length() == 0 )
|
2011-12-20 18:20:44 +00:00
|
|
|
return null;
|
|
|
|
|
2012-12-01 03:42:30 +00:00
|
|
|
if (hasBracketedNumber(recipient))
|
2012-12-24 16:40:37 +00:00
|
|
|
return getRecipientForNumber(context, parseBracketedNumber(recipient), asynchronous);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2014-06-03 21:50:37 +00:00
|
|
|
return getRecipientForNumber(context, recipient, asynchronous);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void clearCache() {
|
2012-12-24 16:40:37 +00:00
|
|
|
ContactPhotoFactory.clearCache();
|
|
|
|
provider.clearCache();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2014-02-20 23:41:52 +00:00
|
|
|
public static void clearCache(Recipient recipient) {
|
|
|
|
ContactPhotoFactory.clearCache(recipient);
|
|
|
|
provider.clearCache(recipient);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|