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-03-18 06:25:09 +00:00
|
|
|
import android.widget.Toast;
|
2014-02-07 02:06:23 +00:00
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
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-03-26 21:49:40 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
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
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public static void refreshDirectoryWithProgressDialog(final Context context) {
|
|
|
|
refreshDirectoryWithProgressDialog(context, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void refreshDirectoryWithProgressDialog(final Context context, final DirectoryUpdateFinishedListener listener) {
|
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
|
|
|
Toast.makeText(context.getApplicationContext(),
|
|
|
|
context.getString(R.string.SingleContactSelectionActivity_you_are_not_registered_with_the_push_service),
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new ProgressDialogAsyncTask<Void,Void,Void>(context,
|
|
|
|
R.string.SingleContactSelectionActivity_updating_directory,
|
|
|
|
R.string.SingleContactSelectionActivity_updating_push_directory)
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... voids) {
|
|
|
|
DirectoryHelper.refreshDirectory(context.getApplicationContext());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void aVoid) {
|
|
|
|
super.onPostExecute(aVoid);
|
|
|
|
if (listener != null) listener.onUpdateFinished();
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-03-26 21:49:40 +00:00
|
|
|
public static boolean isPushDestination(Context context, Recipients recipients) {
|
2014-02-26 08:30:29 +00:00
|
|
|
try {
|
2014-03-26 21:49:40 +00:00
|
|
|
if (recipients == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-03 17:47:51 +00:00
|
|
|
|
2014-03-26 21:49:40 +00:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-03 17:47:51 +00:00
|
|
|
|
2014-03-26 21:49:40 +00:00
|
|
|
if (!recipients.isSingleRecipient()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-03 17:47:51 +00:00
|
|
|
|
2014-03-26 21:49:40 +00:00
|
|
|
if (recipients.isGroupRecipient()) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-26 08:30:29 +00:00
|
|
|
|
2014-04-03 17:47:51 +00:00
|
|
|
final String number = recipients.getPrimaryRecipient().getNumber();
|
|
|
|
|
|
|
|
if (number == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-25 00:02:39 +00:00
|
|
|
final String e164number = Util.canonicalizeNumber(context, number);
|
2014-02-26 08:30:29 +00:00
|
|
|
|
|
|
|
return Directory.getInstance(context).isActiveNumber(e164number);
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return false;
|
|
|
|
} catch (NotInDirectoryException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-03-18 06:25:09 +00:00
|
|
|
|
|
|
|
public static interface DirectoryUpdateFinishedListener {
|
|
|
|
public void onUpdateFinished();
|
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|