2014-02-07 02:06:23 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
import android.content.ContentResolver;
|
2014-02-07 02:06:23 +00:00
|
|
|
import android.content.Context;
|
2015-07-14 21:31:03 +00:00
|
|
|
import android.content.OperationApplicationException;
|
|
|
|
import android.os.RemoteException;
|
|
|
|
import android.provider.ContactsContract;
|
2014-02-26 08:30:29 +00:00
|
|
|
import android.util.Log;
|
2014-02-07 02:06:23 +00:00
|
|
|
|
2015-09-22 00:41:27 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2014-03-18 06:25:09 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-07-14 21:31:03 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.database.NotInDirectoryException;
|
|
|
|
import org.thoughtcrime.securesms.database.TextSecureDirectory;
|
2015-09-22 00:41:27 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.DirectoryRefreshJob;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
|
2014-03-26 21:49:40 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2015-07-14 21:31:03 +00:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
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;
|
2015-07-14 21:31:03 +00:00
|
|
|
import java.util.LinkedList;
|
2014-02-07 02:06:23 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class DirectoryHelper {
|
2015-09-22 00:41:27 +00:00
|
|
|
|
|
|
|
public enum RegistrationState {
|
|
|
|
REGISTERED, NOT_REGISTERED, UNKNOWN
|
|
|
|
}
|
|
|
|
|
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-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);
|
2015-07-14 21:31:03 +00:00
|
|
|
Optional<Account> account = getOrCreateAccount(context);
|
2014-02-17 23:31:42 +00:00
|
|
|
Set<String> eligibleContactNumbers = directory.getPushEligibleContactNumbers(localNumber);
|
2015-02-27 23:35:18 +00:00
|
|
|
List<ContactTokenDetails> activeTokens = accountManager.getContacts(eligibleContactNumbers);
|
2014-02-07 02:06:23 +00:00
|
|
|
|
|
|
|
if (activeTokens != null) {
|
|
|
|
for (ContactTokenDetails activeToken : activeTokens) {
|
2015-02-27 23:35:18 +00:00
|
|
|
eligibleContactNumbers.remove(activeToken.getNumber());
|
|
|
|
activeToken.setNumber(activeToken.getNumber());
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 23:31:42 +00:00
|
|
|
directory.setNumbers(activeTokens, eligibleContactNumbers);
|
2015-07-14 21:31:03 +00:00
|
|
|
|
|
|
|
if (account.isPresent()) {
|
|
|
|
List<String> e164numbers = new LinkedList<>();
|
|
|
|
|
|
|
|
for (ContactTokenDetails contactTokenDetails : activeTokens) {
|
|
|
|
e164numbers.add(contactTokenDetails.getNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-07-14 21:31:03 +00:00
|
|
|
DatabaseFactory.getContactsDatabase(context).setRegisteredUsers(account.get(), e164numbers);
|
2015-07-14 21:31:03 +00:00
|
|
|
} catch (RemoteException | OperationApplicationException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 08:30:29 +00:00
|
|
|
|
2015-09-22 00:41:27 +00:00
|
|
|
public static RegistrationState refreshDirectoryFor(Context context, Recipients recipients)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
|
|
|
|
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
|
|
|
|
String number = Util.canonicalizeNumber(context, recipients.getPrimaryRecipient().getNumber());
|
|
|
|
|
|
|
|
Optional<ContactTokenDetails> details = accountManager.getContact(number);
|
|
|
|
|
|
|
|
if (details.isPresent()) {
|
|
|
|
directory.setNumber(details.get(), true);
|
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(context));
|
|
|
|
return RegistrationState.REGISTERED;
|
|
|
|
} else {
|
|
|
|
ContactTokenDetails absent = new ContactTokenDetails();
|
|
|
|
absent.setNumber(number);
|
|
|
|
directory.setNumber(absent, false);
|
|
|
|
return RegistrationState.NOT_REGISTERED;
|
|
|
|
}
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return RegistrationState.NOT_REGISTERED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static RegistrationState isTextSecureEnabledRecipient(Context context, Recipients recipients) {
|
2014-02-26 08:30:29 +00:00
|
|
|
try {
|
2014-03-26 21:49:40 +00:00
|
|
|
if (recipients == null) {
|
2015-09-22 00:41:27 +00:00
|
|
|
return RegistrationState.NOT_REGISTERED;
|
2014-03-26 21:49:40 +00:00
|
|
|
}
|
2014-04-03 17:47:51 +00:00
|
|
|
|
2014-03-26 21:49:40 +00:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
2015-09-22 00:41:27 +00:00
|
|
|
return RegistrationState.NOT_REGISTERED;
|
2014-03-26 21:49:40 +00:00
|
|
|
}
|
2014-04-03 17:47:51 +00:00
|
|
|
|
2014-03-26 21:49:40 +00:00
|
|
|
if (!recipients.isSingleRecipient()) {
|
2015-09-22 00:41:27 +00:00
|
|
|
return RegistrationState.NOT_REGISTERED;
|
2014-03-26 21:49:40 +00:00
|
|
|
}
|
2014-04-03 17:47:51 +00:00
|
|
|
|
2014-03-26 21:49:40 +00:00
|
|
|
if (recipients.isGroupRecipient()) {
|
2015-09-22 00:41:27 +00:00
|
|
|
return RegistrationState.REGISTERED;
|
2014-03-26 21:49:40 +00:00
|
|
|
}
|
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) {
|
2015-09-22 00:41:27 +00:00
|
|
|
return RegistrationState.NOT_REGISTERED;
|
2014-04-03 17:47:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 00:02:39 +00:00
|
|
|
final String e164number = Util.canonicalizeNumber(context, number);
|
2014-02-26 08:30:29 +00:00
|
|
|
|
2015-09-22 00:41:27 +00:00
|
|
|
return TextSecureDirectory.getInstance(context).isActiveNumber(e164number) ?
|
|
|
|
RegistrationState.REGISTERED : RegistrationState.NOT_REGISTERED;
|
2014-02-26 08:30:29 +00:00
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-09-22 00:41:27 +00:00
|
|
|
return RegistrationState.NOT_REGISTERED;
|
2014-02-26 08:30:29 +00:00
|
|
|
} catch (NotInDirectoryException e) {
|
2015-09-22 00:41:27 +00:00
|
|
|
return RegistrationState.UNKNOWN;
|
2014-02-26 08:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-18 06:25:09 +00:00
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
private static Optional<Account> getOrCreateAccount(Context context) {
|
|
|
|
AccountManager accountManager = AccountManager.get(context);
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("org.thoughtcrime.securesms");
|
2015-02-25 00:37:37 +00:00
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
if (accounts.length == 0) return createAccount(context);
|
|
|
|
else return Optional.of(accounts[0]);
|
|
|
|
}
|
2015-02-25 00:37:37 +00:00
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
private static Optional<Account> createAccount(Context context) {
|
|
|
|
AccountManager accountManager = AccountManager.get(context);
|
|
|
|
Account account = new Account(context.getString(R.string.app_name), "org.thoughtcrime.securesms");
|
|
|
|
|
|
|
|
if (accountManager.addAccountExplicitly(account, null, null)) {
|
|
|
|
Log.w(TAG, "Created new account...");
|
|
|
|
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
|
|
|
|
return Optional.of(account);
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Failed to create account!");
|
|
|
|
return Optional.absent();
|
2015-02-25 00:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|