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;
|
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.IncomingJoinedMessage;
|
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-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
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-11-09 22:51:53 +00:00
|
|
|
notifyNewUsers(context, masterSecret, newUsers);
|
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);
|
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
|
|
|
|
|
|
|
return new LinkedList<>();
|
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);
|
|
|
|
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
|
|
|
|
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
|
|
|
|
|
|
|
List<String> newUsers = updateContactsDatabase(context, localNumber, details.get());
|
|
|
|
|
|
|
|
if (!newUsers.isEmpty() && TextSecurePreferences.isMultiDevice(context)) {
|
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyNewUsers(context, masterSecret, newUsers);
|
|
|
|
|
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
|
|
|
|
2015-11-09 22:51:53 +00:00
|
|
|
private static @NonNull List<String> updateContactsDatabase(@NonNull Context context,
|
|
|
|
@NonNull String localNumber,
|
|
|
|
@NonNull final ContactTokenDetails activeToken)
|
|
|
|
{
|
|
|
|
return updateContactsDatabase(context, localNumber,
|
|
|
|
new LinkedList<ContactTokenDetails>() {{add(activeToken);}},
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static @NonNull List<String> updateContactsDatabase(@NonNull Context context,
|
|
|
|
@NonNull String localNumber,
|
|
|
|
@NonNull List<ContactTokenDetails> activeTokens,
|
|
|
|
boolean removeMissing)
|
|
|
|
{
|
|
|
|
Optional<Account> account = getOrCreateAccount(context);
|
|
|
|
|
|
|
|
if (account.isPresent()) {
|
|
|
|
try {
|
|
|
|
return DatabaseFactory.getContactsDatabase(context)
|
|
|
|
.setRegisteredUsers(account.get(), localNumber, activeTokens, removeMissing);
|
|
|
|
} catch (RemoteException | OperationApplicationException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new LinkedList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|