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;
|
2015-10-27 19:18:02 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2014-02-26 08:30:29 +00:00
|
|
|
import android.util.Log;
|
2015-10-27 19:18:02 +00:00
|
|
|
import android.util.Pair;
|
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-10-27 19:18:02 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
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;
|
2015-10-23 19:51:28 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob;
|
2015-10-27 19:18:02 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
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-10-27 19:18:02 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingGroupMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingJoinedMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.MessageSender;
|
2015-09-29 21:26:37 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DirectoryHelper.UserCapabilities.Capability;
|
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
|
|
|
|
2015-09-29 21:26:37 +00:00
|
|
|
public static class UserCapabilities {
|
|
|
|
|
|
|
|
public static final UserCapabilities UNKNOWN = new UserCapabilities(Capability.UNKNOWN, Capability.UNKNOWN);
|
|
|
|
public static final UserCapabilities UNSUPPORTED = new UserCapabilities(Capability.UNSUPPORTED, Capability.UNSUPPORTED);
|
|
|
|
|
|
|
|
public enum Capability {
|
|
|
|
UNKNOWN, SUPPORTED, UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
|
|
|
private final Capability text;
|
|
|
|
private final Capability voice;
|
|
|
|
|
|
|
|
public UserCapabilities(Capability text, Capability voice) {
|
|
|
|
this.text = text;
|
|
|
|
this.voice = voice;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Capability getTextCapability() {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Capability getVoiceCapability() {
|
|
|
|
return voice;
|
|
|
|
}
|
2015-09-22 00:41:27 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 08:30:29 +00:00
|
|
|
private static final String TAG = DirectoryHelper.class.getSimpleName();
|
2014-02-07 02:06:23 +00:00
|
|
|
|
2015-10-27 19:18:02 +00:00
|
|
|
public static void refreshDirectory(@NonNull Context context, @Nullable MasterSecret masterSecret)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
List<String> newUsers = refreshDirectory(context,
|
|
|
|
TextSecureCommunicationFactory.createManager(context),
|
|
|
|
TextSecurePreferences.getLocalNumber(context));
|
|
|
|
|
|
|
|
if (!newUsers.isEmpty() && TextSecurePreferences.isMultiDevice(context)) {
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String newUser : newUsers) {
|
|
|
|
IncomingJoinedMessage message = new IncomingJoinedMessage(newUser);
|
|
|
|
Pair<Long, Long> smsAndThreadId = DatabaseFactory.getSmsDatabase(context).insertMessageInbox(message);
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret, smsAndThreadId.second);
|
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 19:18:02 +00:00
|
|
|
public static @NonNull List<String> refreshDirectory(@NonNull Context context,
|
|
|
|
@NonNull TextSecureAccountManager accountManager,
|
|
|
|
@NonNull String localNumber)
|
2014-11-27 23:24:26 +00:00
|
|
|
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-10-27 19:18:02 +00:00
|
|
|
return DatabaseFactory.getContactsDatabase(context)
|
|
|
|
.setRegisteredUsers(account.get(), localNumber, 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
|
|
|
}
|
2015-10-27 19:18:02 +00:00
|
|
|
|
|
|
|
return new LinkedList<>();
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
2014-02-26 08:30:29 +00:00
|
|
|
|
2015-09-29 21:26:37 +00:00
|
|
|
public static UserCapabilities refreshDirectoryFor(Context context, Recipients recipients)
|
2015-09-22 00:41:27 +00:00
|
|
|
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));
|
2015-09-29 21:26:37 +00:00
|
|
|
return new UserCapabilities(Capability.SUPPORTED, details.get().isVoice() ? Capability.SUPPORTED : Capability.UNSUPPORTED);
|
2015-09-22 00:41:27 +00:00
|
|
|
} else {
|
|
|
|
ContactTokenDetails absent = new ContactTokenDetails();
|
|
|
|
absent.setNumber(number);
|
|
|
|
directory.setNumber(absent, false);
|
2015-09-29 21:26:37 +00:00
|
|
|
return UserCapabilities.UNSUPPORTED;
|
2015-09-22 00:41:27 +00:00
|
|
|
}
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-09-29 21:26:37 +00:00
|
|
|
return UserCapabilities.UNSUPPORTED;
|
2015-09-22 00:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 21:26:37 +00:00
|
|
|
public static UserCapabilities getUserCapabilities(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-29 21:26:37 +00:00
|
|
|
return UserCapabilities.UNSUPPORTED;
|
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-29 21:26:37 +00:00
|
|
|
return UserCapabilities.UNSUPPORTED;
|
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-29 21:26:37 +00:00
|
|
|
return UserCapabilities.UNSUPPORTED;
|
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-29 21:26:37 +00:00
|
|
|
return new UserCapabilities(Capability.SUPPORTED, Capability.UNSUPPORTED);
|
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-29 21:26:37 +00:00
|
|
|
return UserCapabilities.UNSUPPORTED;
|
2014-04-03 17:47:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 21:26:37 +00:00
|
|
|
String e164number = Util.canonicalizeNumber(context, number);
|
|
|
|
boolean secureText = TextSecureDirectory.getInstance(context).isSecureTextSupported(e164number);
|
|
|
|
boolean secureVoice = TextSecureDirectory.getInstance(context).isSecureVoiceSupported(e164number);
|
|
|
|
|
|
|
|
return new UserCapabilities(secureText ? Capability.SUPPORTED : Capability.UNSUPPORTED,
|
|
|
|
secureVoice ? Capability.SUPPORTED : Capability.UNSUPPORTED);
|
2014-02-26 08:30:29 +00:00
|
|
|
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-09-29 21:26:37 +00:00
|
|
|
return UserCapabilities.UNSUPPORTED;
|
2014-02-26 08:30:29 +00:00
|
|
|
} catch (NotInDirectoryException e) {
|
2015-09-29 21:26:37 +00:00
|
|
|
return UserCapabilities.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-10-27 19:18:02 +00:00
|
|
|
Optional<Account> account;
|
|
|
|
|
|
|
|
if (accounts.length == 0) account = createAccount(context);
|
|
|
|
else account = Optional.of(accounts[0]);
|
|
|
|
|
|
|
|
if (account.isPresent() && !ContentResolver.getSyncAutomatically(account.get(), ContactsContract.AUTHORITY)) {
|
|
|
|
ContentResolver.setSyncAutomatically(account.get(), ContactsContract.AUTHORITY, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return account;
|
2015-07-14 21:31:03 +00:00
|
|
|
}
|
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
|
|
|
}
|