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-12-04 20:12:48 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.SessionUtil;
|
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-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;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
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.IncomingJoinedMessage;
|
2015-09-29 21:26:37 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DirectoryHelper.UserCapabilities.Capability;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
|
|
|
import org.whispersystems.signalservice.api.util.InvalidNumberException;
|
2014-02-07 02:06:23 +00:00
|
|
|
|
2014-11-27 23:24:26 +00:00
|
|
|
import java.io.IOException;
|
2015-12-19 12:07:44 +00:00
|
|
|
import java.util.Calendar;
|
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
|
|
|
|
{
|
2017-01-05 20:42:28 +00:00
|
|
|
RefreshResult result = refreshDirectory(context,
|
|
|
|
AccountManagerFactory.createManager(context),
|
|
|
|
TextSecurePreferences.getLocalNumber(context));
|
2015-10-27 19:18:02 +00:00
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
if (!result.getNewUsers().isEmpty() && TextSecurePreferences.isMultiDevice(context)) {
|
2015-10-27 19:18:02 +00:00
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
if (!result.isFresh()) {
|
|
|
|
notifyNewUsers(context, masterSecret, result.getNewUsers());
|
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
public static @NonNull RefreshResult refreshDirectory(@NonNull Context context,
|
|
|
|
@NonNull SignalServiceAccountManager 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);
|
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-11-09 22:51:53 +00:00
|
|
|
return updateContactsDatabase(context, localNumber, activeTokens, true);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
2015-10-27 19:18:02 +00:00
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
return new RefreshResult(new LinkedList<String>(), false);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
2014-02-26 08:30:29 +00:00
|
|
|
|
2015-11-09 22:51:53 +00:00
|
|
|
public static UserCapabilities refreshDirectoryFor(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
@NonNull Recipients recipients,
|
|
|
|
@NonNull String localNumber)
|
2015-09-22 00:41:27 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
try {
|
2015-11-09 22:51:53 +00:00
|
|
|
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
|
2016-12-20 17:55:52 +00:00
|
|
|
SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
|
2015-11-09 22:51:53 +00:00
|
|
|
String number = Util.canonicalizeNumber(context, recipients.getPrimaryRecipient().getNumber());
|
|
|
|
Optional<ContactTokenDetails> details = accountManager.getContact(number);
|
2015-09-22 00:41:27 +00:00
|
|
|
|
|
|
|
if (details.isPresent()) {
|
|
|
|
directory.setNumber(details.get(), true);
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
RefreshResult result = updateContactsDatabase(context, localNumber, details.get());
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
if (!result.getNewUsers().isEmpty() && TextSecurePreferences.isMultiDevice(context)) {
|
2015-11-09 22:51:53 +00:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
if (!result.isFresh()) {
|
|
|
|
notifyNewUsers(context, masterSecret, result.getNewUsers());
|
|
|
|
}
|
2015-11-09 22:51:53 +00:00
|
|
|
|
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-10-19 18:23:12 +00:00
|
|
|
public static @NonNull UserCapabilities getUserCapabilities(@NonNull Context context,
|
|
|
|
@Nullable 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
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
private static @NonNull RefreshResult updateContactsDatabase(@NonNull Context context,
|
|
|
|
@NonNull String localNumber,
|
|
|
|
@NonNull final ContactTokenDetails activeToken)
|
2015-11-09 22:51:53 +00:00
|
|
|
{
|
|
|
|
return updateContactsDatabase(context, localNumber,
|
|
|
|
new LinkedList<ContactTokenDetails>() {{add(activeToken);}},
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
private static @NonNull RefreshResult updateContactsDatabase(@NonNull Context context,
|
|
|
|
@NonNull String localNumber,
|
|
|
|
@NonNull List<ContactTokenDetails> activeTokens,
|
|
|
|
boolean removeMissing)
|
2015-11-09 22:51:53 +00:00
|
|
|
{
|
2017-01-05 20:42:28 +00:00
|
|
|
Optional<AccountHolder> account = getOrCreateAccount(context);
|
2015-11-09 22:51:53 +00:00
|
|
|
|
|
|
|
if (account.isPresent()) {
|
|
|
|
try {
|
2017-01-05 20:42:28 +00:00
|
|
|
List<String> newUsers = DatabaseFactory.getContactsDatabase(context)
|
|
|
|
.setRegisteredUsers(account.get().getAccount(), localNumber, activeTokens, removeMissing);
|
|
|
|
|
|
|
|
return new RefreshResult(newUsers, account.get().isFresh());
|
2015-11-09 22:51:53 +00:00
|
|
|
} catch (RemoteException | OperationApplicationException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
return new RefreshResult(new LinkedList<String>(), false);
|
2015-11-09 22:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void notifyNewUsers(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
@NonNull List<String> newUsers)
|
|
|
|
{
|
2015-12-19 12:07:44 +00:00
|
|
|
if (!TextSecurePreferences.isNewContactsNotificationEnabled(context)) return;
|
|
|
|
|
2015-11-09 22:51:53 +00:00
|
|
|
for (String newUser : newUsers) {
|
2016-02-14 13:58:39 +00:00
|
|
|
if (!SessionUtil.hasSession(context, masterSecret, newUser) && !Util.isOwnNumber(context, newUser)) {
|
2015-12-04 20:12:48 +00:00
|
|
|
IncomingJoinedMessage message = new IncomingJoinedMessage(newUser);
|
|
|
|
Pair<Long, Long> smsAndThreadId = DatabaseFactory.getSmsDatabase(context).insertMessageInbox(message);
|
2015-12-19 12:07:44 +00:00
|
|
|
|
|
|
|
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
|
|
|
|
if (hour >= 9 && hour < 23) {
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret, false, smsAndThreadId.second, true);
|
|
|
|
} else {
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret, false, smsAndThreadId.second, false);
|
|
|
|
}
|
2015-12-04 20:12:48 +00:00
|
|
|
}
|
2015-11-09 22:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
private static Optional<AccountHolder> getOrCreateAccount(Context context) {
|
2015-07-14 21:31:03 +00:00
|
|
|
AccountManager accountManager = AccountManager.get(context);
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("org.thoughtcrime.securesms");
|
2015-02-25 00:37:37 +00:00
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
Optional<AccountHolder> account;
|
2015-10-27 19:18:02 +00:00
|
|
|
|
|
|
|
if (accounts.length == 0) account = createAccount(context);
|
2017-01-05 20:42:28 +00:00
|
|
|
else account = Optional.of(new AccountHolder(accounts[0], false));
|
2015-10-27 19:18:02 +00:00
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
if (account.isPresent() && !ContentResolver.getSyncAutomatically(account.get().getAccount(), ContactsContract.AUTHORITY)) {
|
|
|
|
ContentResolver.setSyncAutomatically(account.get().getAccount(), ContactsContract.AUTHORITY, true);
|
2015-10-27 19:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return account;
|
2015-07-14 21:31:03 +00:00
|
|
|
}
|
2015-02-25 00:37:37 +00:00
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
private static Optional<AccountHolder> createAccount(Context context) {
|
2015-07-14 21:31:03 +00:00
|
|
|
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);
|
2017-01-05 20:42:28 +00:00
|
|
|
return Optional.of(new AccountHolder(account, true));
|
2015-07-14 21:31:03 +00:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Failed to create account!");
|
|
|
|
return Optional.absent();
|
2015-02-25 00:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-05 20:42:28 +00:00
|
|
|
|
|
|
|
private static class AccountHolder {
|
|
|
|
|
|
|
|
private final boolean fresh;
|
|
|
|
private final Account account;
|
|
|
|
|
|
|
|
private AccountHolder(Account account, boolean fresh) {
|
|
|
|
this.fresh = fresh;
|
|
|
|
this.account = account;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFresh() {
|
|
|
|
return fresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Account getAccount() {
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class RefreshResult {
|
|
|
|
|
|
|
|
private final List<String> newUsers;
|
|
|
|
private final boolean fresh;
|
|
|
|
|
|
|
|
private RefreshResult(List<String> newUsers, boolean fresh) {
|
|
|
|
this.newUsers = newUsers;
|
|
|
|
this.fresh = fresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getNewUsers() {
|
|
|
|
return newUsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFresh() {
|
|
|
|
return fresh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|