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-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.database.NotInDirectoryException;
|
|
|
|
import org.thoughtcrime.securesms.database.TextSecureDirectory;
|
|
|
|
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
|
2014-03-26 21:49:40 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.whispersystems.textsecure.api.TextSecureAccountManager;
|
2014-11-12 19:35:54 +00:00
|
|
|
import org.whispersystems.textsecure.api.push.ContactTokenDetails;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
2014-02-07 02:06:23 +00:00
|
|
|
|
2014-11-27 23:24:26 +00:00
|
|
|
import java.io.IOException;
|
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, 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) {
|
2014-11-27 23:24:26 +00:00
|
|
|
try {
|
|
|
|
DirectoryHelper.refreshDirectory(context.getApplicationContext());
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
2014-03-18 06:25:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void aVoid) {
|
|
|
|
super.onPostExecute(aVoid);
|
|
|
|
if (listener != null) listener.onUpdateFinished();
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-27 23:24:26 +00:00
|
|
|
public static void refreshDirectory(final Context context) throws IOException {
|
2014-11-10 04:35:08 +00:00
|
|
|
refreshDirectory(context, TextSecureCommunicationFactory.createManager(context));
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 23:24:26 +00:00
|
|
|
public static void refreshDirectory(final Context context, final TextSecureAccountManager accountManager)
|
|
|
|
throws IOException
|
|
|
|
{
|
2014-11-10 04:35:08 +00:00
|
|
|
refreshDirectory(context, accountManager, TextSecurePreferences.getLocalNumber(context));
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 23:24:26 +00:00
|
|
|
public static void refreshDirectory(final Context context, final TextSecureAccountManager accountManager, final String localNumber)
|
|
|
|
throws IOException
|
|
|
|
{
|
2014-11-10 04:35:08 +00:00
|
|
|
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
|
2014-02-17 23:31:42 +00:00
|
|
|
Set<String> eligibleContactNumbers = directory.getPushEligibleContactNumbers(localNumber);
|
|
|
|
Map<String, String> tokenMap = DirectoryUtil.getDirectoryServerTokenMap(eligibleContactNumbers);
|
2014-11-10 04:35:08 +00:00
|
|
|
List<ContactTokenDetails> activeTokens = accountManager.getContacts(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
|
|
|
|
2014-11-10 04:35:08 +00:00
|
|
|
return TextSecureDirectory.getInstance(context).isActiveNumber(e164number);
|
2014-02-26 08:30:29 +00:00
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return false;
|
|
|
|
} catch (NotInDirectoryException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-03-18 06:25:09 +00:00
|
|
|
|
2015-02-25 00:37:37 +00:00
|
|
|
public static boolean isSmsFallbackAllowed(Context context, Recipients recipients) {
|
|
|
|
try {
|
|
|
|
if (recipients == null || !recipients.isSingleRecipient() || recipients.isGroupRecipient()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String number = recipients.getPrimaryRecipient().getNumber();
|
|
|
|
|
|
|
|
if (number == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String e164number = Util.canonicalizeNumber(context, number);
|
|
|
|
|
|
|
|
return TextSecureDirectory.getInstance(context).isSmsFallbackSupported(e164number);
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, 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
|
|
|
}
|