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;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.util.NumberUtil;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedHashMap;
|
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.Map;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
|
|
|
public class RecipientFactory {
|
|
|
|
|
|
|
|
private static final Map<String,Recipient> recipientCache = Collections.synchronizedMap(new LRUHashMap<String,Recipient>());
|
|
|
|
private static final Map<String,Recipient> recipientIdCache = Collections.synchronizedMap(new LRUHashMap<String,Recipient>());
|
2012-09-12 01:23:19 +00:00
|
|
|
private static final Map<Uri,Recipient> recipientUriCache = Collections.synchronizedMap(new HashMap<Uri,Recipient>());
|
|
|
|
|
2012-12-12 11:56:38 +00:00
|
|
|
private static final RecipientProvider provider = new RecipientProvider();
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static RecipientProvider getRecipientProvider() {
|
|
|
|
return provider;
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static Recipient getRecipientForUri(Context context, Uri uri) {
|
|
|
|
Recipient recipient = recipientUriCache.get(uri);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (recipient == null)
|
|
|
|
recipient = getRecipientFromProvider(context, uri);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return recipient;
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static Recipients getRecipientsForIds(Context context, String recipientIds) {
|
2012-12-01 03:48:48 +00:00
|
|
|
if (recipientIds == null || recipientIds.trim().length() == 0)
|
|
|
|
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-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
Recipient recipient = recipientIdCache.get(recipientId);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (recipient == null)
|
|
|
|
recipient = getRecipientFromProviderId(context, recipientId);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (recipient == null)
|
|
|
|
recipient = getNullIdRecipient(context, recipientId);
|
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
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static Recipient getRecipientForNumber(Context context, String number) {
|
|
|
|
Recipient recipient = recipientCache.get(number);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (recipient == null)
|
|
|
|
recipient = getRecipientFromProvider(context, number);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (recipient == null)
|
|
|
|
recipient = getNullRecipient(context, number);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return recipient;
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static Recipients getRecipientsFromString(Context context, String rawText) throws RecipientFormattingException {
|
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()) {
|
|
|
|
Recipient recipient = parseRecipient(context, tokenizer.nextToken());
|
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
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static Recipient getNullIdRecipient(Context context, String recipientId) {
|
|
|
|
String address = DatabaseFactory.getAddressDatabase(context).getAddressFromId(recipientId);
|
|
|
|
Recipient recipient = getNullRecipient(context, address);
|
|
|
|
recipientIdCache.put(recipientId, recipient);
|
|
|
|
return recipient;
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static Recipient getNullRecipient(Context context, String number) {
|
|
|
|
Recipient nullRecipient = new Recipient(null, number, BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_contact_picture));
|
|
|
|
recipientCache.put(number, nullRecipient);
|
|
|
|
return nullRecipient;
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static Recipient getRecipientFromProviderId(Context context, String recipientId) {
|
|
|
|
Log.w("RecipientFactory", "Hitting recipient provider [ID].");
|
|
|
|
|
|
|
|
String address = DatabaseFactory.getAddressDatabase(context).getAddressFromId(recipientId);
|
|
|
|
Recipient recipient = getRecipientFromProvider(context, address);
|
|
|
|
|
|
|
|
recipientIdCache.put(recipientId, recipient);
|
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Recipient getRecipientFromProvider(Context context, Uri uri) {
|
|
|
|
Recipient recipient = provider.getRecipient(context, uri);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
|
|
|
if (recipient != null)
|
2011-12-20 18:20:44 +00:00
|
|
|
recipientUriCache.put(uri, recipient);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Recipient getRecipientFromProvider(Context context, String number) {
|
|
|
|
Recipient recipient = provider.getRecipient(context, number);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (recipient != null)
|
|
|
|
recipientCache.put(number, recipient);
|
|
|
|
|
|
|
|
return recipient;
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static String parseBracketedNumber(String recipient) throws RecipientFormattingException {
|
|
|
|
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
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
if (NumberUtil.isValidSmsOrEmail(value))
|
2011-12-20 18:20:44 +00:00
|
|
|
return value;
|
|
|
|
else
|
|
|
|
throw new RecipientFormattingException("Bracketed value: " + value + " is not valid.");
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static Recipient parseRecipient(Context context, String recipient) throws RecipientFormattingException {
|
|
|
|
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))
|
2011-12-20 18:20:44 +00:00
|
|
|
return getRecipientForNumber(context, parseBracketedNumber(recipient));
|
|
|
|
|
|
|
|
if (NumberUtil.isValidSmsOrEmail(recipient))
|
|
|
|
return getRecipientForNumber(context, recipient);
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
throw new RecipientFormattingException("Recipient: " + recipient + " is badly formatted.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void clearCache() {
|
|
|
|
recipientCache.clear();
|
|
|
|
recipientIdCache.clear();
|
|
|
|
recipientUriCache.clear();
|
|
|
|
}
|
2012-09-12 01:23:19 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static class LRUHashMap<K,V> extends LinkedHashMap<K,V> {
|
|
|
|
private static final int MAX_SIZE = 1000;
|
2012-09-12 01:23:19 +00:00
|
|
|
@Override
|
2011-12-20 18:20:44 +00:00
|
|
|
protected boolean removeEldestEntry (Map.Entry<K,V> eldest) {
|
2012-09-12 01:23:19 +00:00
|
|
|
return size() > MAX_SIZE;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|