2014-02-07 02:06:23 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2014-02-26 08:30:29 +00:00
|
|
|
import android.util.Log;
|
2014-02-07 02:06:23 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
|
2014-02-26 08:30:29 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2014-02-07 02:06:23 +00:00
|
|
|
import org.whispersystems.textsecure.directory.Directory;
|
2014-02-26 08:30:29 +00:00
|
|
|
import org.whispersystems.textsecure.directory.NotInDirectoryException;
|
2014-02-07 02:06:23 +00:00
|
|
|
import org.whispersystems.textsecure.push.ContactTokenDetails;
|
|
|
|
import org.whispersystems.textsecure.push.PushServiceSocket;
|
|
|
|
import org.whispersystems.textsecure.util.DirectoryUtil;
|
2014-02-26 08:30:29 +00:00
|
|
|
import org.whispersystems.textsecure.util.InvalidNumberException;
|
2014-02-07 02:06:23 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class DirectoryHelper {
|
2014-02-26 08:30:29 +00:00
|
|
|
private static final String TAG = DirectoryHelper.class.getSimpleName();
|
2014-02-07 02:06:23 +00:00
|
|
|
|
|
|
|
public static void refreshDirectory(final Context context) {
|
|
|
|
refreshDirectory(context, PushServiceSocketFactory.create(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void refreshDirectory(final Context context, final PushServiceSocket socket) {
|
|
|
|
refreshDirectory(context, socket, TextSecurePreferences.getLocalNumber(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void refreshDirectory(final Context context, final PushServiceSocket socket, final String localNumber) {
|
2014-02-17 23:31:42 +00:00
|
|
|
Directory directory = Directory.getInstance(context);
|
|
|
|
Set<String> eligibleContactNumbers = directory.getPushEligibleContactNumbers(localNumber);
|
|
|
|
Map<String, String> tokenMap = DirectoryUtil.getDirectoryServerTokenMap(eligibleContactNumbers);
|
|
|
|
List<ContactTokenDetails> activeTokens = socket.retrieveDirectory(tokenMap.keySet());
|
2014-02-07 02:06:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (activeTokens != null) {
|
|
|
|
for (ContactTokenDetails activeToken : activeTokens) {
|
|
|
|
eligibleContactNumbers.remove(tokenMap.get(activeToken.getToken()));
|
2014-02-17 23:31:42 +00:00
|
|
|
activeToken.setNumber(tokenMap.get(activeToken.getToken()));
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 23:31:42 +00:00
|
|
|
directory.setNumbers(activeTokens, eligibleContactNumbers);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 08:30:29 +00:00
|
|
|
|
|
|
|
public static boolean isPushDestination(Context context, Recipient recipient) {
|
|
|
|
try {
|
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) return false;
|
|
|
|
if (GroupUtil.isEncodedGroup(recipient.getNumber())) return true;
|
|
|
|
|
|
|
|
String number = recipient.getNumber();
|
|
|
|
String e164number = Util.canonicalizeNumber(context, number);
|
|
|
|
|
|
|
|
return Directory.getInstance(context).isActiveNumber(e164number);
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return false;
|
|
|
|
} catch (NotInDirectoryException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|