|
|
@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.util;
|
|
|
|
import android.Manifest;
|
|
|
|
import android.Manifest;
|
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.OperationApplicationException;
|
|
|
|
import android.content.OperationApplicationException;
|
|
|
@ -18,6 +19,7 @@ import com.annimon.stream.Collectors;
|
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.BuildConfig;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
|
|
|
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
|
|
|
import org.thoughtcrime.securesms.crypto.SessionUtil;
|
|
|
|
import org.thoughtcrime.securesms.crypto.SessionUtil;
|
|
|
@ -31,24 +33,41 @@ import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
|
|
|
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
|
|
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
|
|
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.push.IasTrustStore;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingJoinedMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingJoinedMessage;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
|
|
|
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
|
|
|
|
|
|
|
import org.whispersystems.signalservice.api.push.TrustStore;
|
|
|
|
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
|
|
|
|
|
|
|
import org.whispersystems.signalservice.internal.contacts.crypto.Quote;
|
|
|
|
|
|
|
|
import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedQuoteException;
|
|
|
|
|
|
|
|
import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.security.KeyStore;
|
|
|
|
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
import java.security.SignatureException;
|
|
|
|
|
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
|
|
|
|
|
|
|
public class DirectoryHelper {
|
|
|
|
public class DirectoryHelper {
|
|
|
|
|
|
|
|
|
|
|
|
private static final String TAG = DirectoryHelper.class.getSimpleName();
|
|
|
|
private static final String TAG = DirectoryHelper.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final int CONTACT_DISCOVERY_BATCH_SIZE = 2048;
|
|
|
|
|
|
|
|
|
|
|
|
public static void refreshDirectory(@NonNull Context context, boolean notifyOfNewUsers)
|
|
|
|
public static void refreshDirectory(@NonNull Context context, boolean notifyOfNewUsers)
|
|
|
|
throws IOException
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -66,15 +85,16 @@ public class DirectoryHelper {
|
|
|
|
if (notifyOfNewUsers) notifyNewUsers(context, newlyActiveUsers);
|
|
|
|
if (notifyOfNewUsers) notifyNewUsers(context, newlyActiveUsers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("CheckResult")
|
|
|
|
private static @NonNull List<Address> refreshDirectory(@NonNull Context context, @NonNull SignalServiceAccountManager accountManager)
|
|
|
|
private static @NonNull List<Address> refreshDirectory(@NonNull Context context, @NonNull SignalServiceAccountManager accountManager)
|
|
|
|
throws IOException
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (TextUtils.isEmpty(TextSecurePreferences.getLocalNumber(context))) {
|
|
|
|
if (TextUtils.isEmpty(TextSecurePreferences.getLocalNumber(context))) {
|
|
|
|
return new LinkedList<>();
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) {
|
|
|
|
if (!Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) {
|
|
|
|
return new LinkedList<>();
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
|
|
|
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
|
|
@ -82,42 +102,33 @@ public class DirectoryHelper {
|
|
|
|
Stream<String> eligibleSystemDatabaseContactNumbers = Stream.of(ContactAccessor.getInstance().getAllContactsWithNumbers(context)).map(Address::serialize);
|
|
|
|
Stream<String> eligibleSystemDatabaseContactNumbers = Stream.of(ContactAccessor.getInstance().getAllContactsWithNumbers(context)).map(Address::serialize);
|
|
|
|
Set<String> eligibleContactNumbers = Stream.concat(eligibleRecipientDatabaseContactNumbers, eligibleSystemDatabaseContactNumbers).collect(Collectors.toSet());
|
|
|
|
Set<String> eligibleContactNumbers = Stream.concat(eligibleRecipientDatabaseContactNumbers, eligibleSystemDatabaseContactNumbers).collect(Collectors.toSet());
|
|
|
|
|
|
|
|
|
|
|
|
List<ContactTokenDetails> activeTokens = accountManager.getContacts(eligibleContactNumbers);
|
|
|
|
Future<DirectoryResult> legacyRequest = getLegacyDirectoryResult(context, accountManager, recipientDatabase, eligibleContactNumbers);
|
|
|
|
|
|
|
|
List<Future<Set<String>>> contactServiceRequest = getContactServiceDirectoryResult(context, accountManager, eligibleContactNumbers);
|
|
|
|
if (activeTokens != null) {
|
|
|
|
|
|
|
|
List<Address> activeAddresses = new LinkedList<>();
|
|
|
|
|
|
|
|
List<Address> inactiveAddresses = new LinkedList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> inactiveContactNumbers = new HashSet<>(eligibleContactNumbers);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (ContactTokenDetails activeToken : activeTokens) {
|
|
|
|
try {
|
|
|
|
activeAddresses.add(Address.fromSerialized(activeToken.getNumber()));
|
|
|
|
DirectoryResult legacyResult = legacyRequest.get();
|
|
|
|
inactiveContactNumbers.remove(activeToken.getNumber());
|
|
|
|
Set<String> contactServiceResult = executeAndMergeContactDiscoveryRequests(accountManager, contactServiceRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (String inactiveContactNumber : inactiveContactNumbers) {
|
|
|
|
if (legacyResult.getNumbers().size() == contactServiceResult.size() && legacyResult.getNumbers().containsAll(contactServiceResult)) {
|
|
|
|
inactiveAddresses.add(Address.fromSerialized(inactiveContactNumber));
|
|
|
|
Log.i(TAG, "[Batch] New contact discovery service request matched existing results.");
|
|
|
|
|
|
|
|
accountManager.reportContactDiscoveryServiceMatch();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Log.w(TAG, "[Batch] New contact discovery service request did NOT match existing results.");
|
|
|
|
|
|
|
|
accountManager.reportContactDiscoveryServiceMismatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Set<Address> currentActiveAddresses = new HashSet<>(recipientDatabase.getRegistered());
|
|
|
|
return legacyResult.getNewlyActiveAddresses();
|
|
|
|
Set<Address> contactAddresses = new HashSet<>(recipientDatabase.getSystemContacts());
|
|
|
|
|
|
|
|
List<Address> newlyActiveAddresses = Stream.of(activeAddresses)
|
|
|
|
|
|
|
|
.filter(address -> !currentActiveAddresses.contains(address))
|
|
|
|
|
|
|
|
.filter(contactAddresses::contains)
|
|
|
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
recipientDatabase.setRegistered(activeAddresses, inactiveAddresses);
|
|
|
|
|
|
|
|
updateContactsDatabase(context, activeAddresses, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (TextSecurePreferences.hasSuccessfullyRetrievedDirectory(context)) {
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
return newlyActiveAddresses;
|
|
|
|
throw new IOException("[Batch] Operation was interrupted.", e);
|
|
|
|
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
|
|
|
|
if (e.getCause() instanceof IOException) {
|
|
|
|
|
|
|
|
throw (IOException) e.getCause();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
TextSecurePreferences.setHasSuccessfullyRetrievedDirectory(context, true);
|
|
|
|
Log.e(TAG, "[Batch] Experienced an unexpected exception.", e);
|
|
|
|
return new LinkedList<>();
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new LinkedList<>();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static RegisteredState refreshDirectoryFor(@NonNull Context context,
|
|
|
|
public static RegisteredState refreshDirectoryFor(@NonNull Context context,
|
|
|
@ -126,30 +137,34 @@ public class DirectoryHelper {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
|
|
|
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
|
|
|
SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
|
|
|
|
SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
|
|
|
|
boolean activeUser = recipient.resolve().getRegistered() == RegisteredState.REGISTERED;
|
|
|
|
|
|
|
|
boolean systemContact = recipient.isSystemContact();
|
|
|
|
|
|
|
|
String number = recipient.getAddress().serialize();
|
|
|
|
|
|
|
|
Optional<ContactTokenDetails> details = accountManager.getContact(number);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (details.isPresent()) {
|
|
|
|
Future<RegisteredState> legacyRequest = getLegacyRegisteredState(context, accountManager, recipientDatabase, recipient);
|
|
|
|
recipientDatabase.setRegistered(recipient, RegisteredState.REGISTERED);
|
|
|
|
List<Future<Set<String>>> contactServiceRequest = getContactServiceDirectoryResult(context, accountManager, Collections.singleton(recipient.getAddress().serialize()));
|
|
|
|
|
|
|
|
|
|
|
|
if (Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) {
|
|
|
|
try {
|
|
|
|
updateContactsDatabase(context, Util.asList(recipient.getAddress()), false);
|
|
|
|
RegisteredState legacyState = legacyRequest.get();
|
|
|
|
}
|
|
|
|
Set<String> contactServiceResult = executeAndMergeContactDiscoveryRequests(accountManager, contactServiceRequest);
|
|
|
|
|
|
|
|
RegisteredState contactServiceState = contactServiceResult.size() == 1 ? RegisteredState.REGISTERED : RegisteredState.NOT_REGISTERED;
|
|
|
|
|
|
|
|
|
|
|
|
if (!activeUser && TextSecurePreferences.isMultiDevice(context)) {
|
|
|
|
if (legacyState == contactServiceState) {
|
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
Log.i(TAG, "[Singular] New contact discovery service request matched existing results.");
|
|
|
|
|
|
|
|
accountManager.reportContactDiscoveryServiceMatch();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Log.w(TAG, "[Singular] New contact discovery service request did NOT match existing results.");
|
|
|
|
|
|
|
|
accountManager.reportContactDiscoveryServiceMismatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!activeUser && systemContact && !TextSecurePreferences.getNeedsSqlCipherMigration(context)) {
|
|
|
|
return legacyState;
|
|
|
|
notifyNewUsers(context, Collections.singletonList(recipient.getAddress()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return RegisteredState.REGISTERED;
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
|
|
throw new IOException("[Singular] Operation was interrupted.", e);
|
|
|
|
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
|
|
|
|
if (e.getCause() instanceof IOException) {
|
|
|
|
|
|
|
|
throw (IOException) e.getCause();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
recipientDatabase.setRegistered(recipient, RegisteredState.NOT_REGISTERED);
|
|
|
|
Log.e(TAG, "[Singular] Experienced an unexpected exception.", e);
|
|
|
|
return RegisteredState.NOT_REGISTERED;
|
|
|
|
throw new AssertionError(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -249,6 +264,180 @@ public class DirectoryHelper {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Future<DirectoryResult> getLegacyDirectoryResult(@NonNull Context context,
|
|
|
|
|
|
|
|
@NonNull SignalServiceAccountManager accountManager,
|
|
|
|
|
|
|
|
@NonNull RecipientDatabase recipientDatabase,
|
|
|
|
|
|
|
|
@NonNull Set<String> eligibleContactNumbers)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return SignalExecutors.IO.submit(() -> {
|
|
|
|
|
|
|
|
List<ContactTokenDetails> activeTokens = accountManager.getContacts(eligibleContactNumbers);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (activeTokens != null) {
|
|
|
|
|
|
|
|
List<Address> activeAddresses = new LinkedList<>();
|
|
|
|
|
|
|
|
List<Address> inactiveAddresses = new LinkedList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> inactiveContactNumbers = new HashSet<>(eligibleContactNumbers);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (ContactTokenDetails activeToken : activeTokens) {
|
|
|
|
|
|
|
|
activeAddresses.add(Address.fromSerialized(activeToken.getNumber()));
|
|
|
|
|
|
|
|
inactiveContactNumbers.remove(activeToken.getNumber());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (String inactiveContactNumber : inactiveContactNumbers) {
|
|
|
|
|
|
|
|
inactiveAddresses.add(Address.fromSerialized(inactiveContactNumber));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set<Address> currentActiveAddresses = new HashSet<>(recipientDatabase.getRegistered());
|
|
|
|
|
|
|
|
Set<Address> contactAddresses = new HashSet<>(recipientDatabase.getSystemContacts());
|
|
|
|
|
|
|
|
List<Address> newlyActiveAddresses = Stream.of(activeAddresses)
|
|
|
|
|
|
|
|
.filter(address -> !currentActiveAddresses.contains(address))
|
|
|
|
|
|
|
|
.filter(contactAddresses::contains)
|
|
|
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
recipientDatabase.setRegistered(activeAddresses, inactiveAddresses);
|
|
|
|
|
|
|
|
updateContactsDatabase(context, activeAddresses, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> activeContactNumbers = Stream.of(activeAddresses).map(Address::serialize).collect(Collectors.toSet());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (TextSecurePreferences.hasSuccessfullyRetrievedDirectory(context)) {
|
|
|
|
|
|
|
|
return new DirectoryResult(activeContactNumbers, newlyActiveAddresses);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
TextSecurePreferences.setHasSuccessfullyRetrievedDirectory(context, true);
|
|
|
|
|
|
|
|
return new DirectoryResult(activeContactNumbers);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DirectoryResult(Collections.emptySet(), Collections.emptyList());
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Future<RegisteredState> getLegacyRegisteredState(@NonNull Context context,
|
|
|
|
|
|
|
|
@NonNull SignalServiceAccountManager accountManager,
|
|
|
|
|
|
|
|
@NonNull RecipientDatabase recipientDatabase,
|
|
|
|
|
|
|
|
@NonNull Recipient recipient)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return SignalExecutors.IO.submit(() -> {
|
|
|
|
|
|
|
|
boolean activeUser = recipient.resolve().getRegistered() == RegisteredState.REGISTERED;
|
|
|
|
|
|
|
|
boolean systemContact = recipient.isSystemContact();
|
|
|
|
|
|
|
|
String number = recipient.getAddress().serialize();
|
|
|
|
|
|
|
|
Optional<ContactTokenDetails> details = accountManager.getContact(number);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (details.isPresent()) {
|
|
|
|
|
|
|
|
recipientDatabase.setRegistered(recipient, RegisteredState.REGISTERED);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) {
|
|
|
|
|
|
|
|
updateContactsDatabase(context, Util.asList(recipient.getAddress()), false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!activeUser && TextSecurePreferences.isMultiDevice(context)) {
|
|
|
|
|
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!activeUser && systemContact && !TextSecurePreferences.getNeedsSqlCipherMigration(context)) {
|
|
|
|
|
|
|
|
notifyNewUsers(context, Collections.singletonList(recipient.getAddress()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return RegisteredState.REGISTERED;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
recipientDatabase.setRegistered(recipient, RegisteredState.NOT_REGISTERED);
|
|
|
|
|
|
|
|
return RegisteredState.NOT_REGISTERED;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static List<Future<Set<String>>> getContactServiceDirectoryResult(@NonNull Context context,
|
|
|
|
|
|
|
|
@NonNull SignalServiceAccountManager accountManager,
|
|
|
|
|
|
|
|
@NonNull Set<String> eligibleContactNumbers)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<Set<String>> batches = splitIntoBatches(eligibleContactNumbers, CONTACT_DISCOVERY_BATCH_SIZE);
|
|
|
|
|
|
|
|
List<Future<Set<String>>> futures = new ArrayList<>(batches.size());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (Set<String> batch : batches) {
|
|
|
|
|
|
|
|
Future<Set<String>> future = SignalExecutors.IO.submit(() -> {
|
|
|
|
|
|
|
|
return new HashSet<>(accountManager.getRegisteredUsers(getIasKeyStore(context), batch, BuildConfig.MRENCLAVE));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
futures.add(future);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return futures;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static List<Set<String>> splitIntoBatches(@NonNull Set<String> numbers, int batchSize) {
|
|
|
|
|
|
|
|
List<String> numberList = new ArrayList<>(numbers);
|
|
|
|
|
|
|
|
List<Set<String>> batches = new LinkedList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < numberList.size(); i += batchSize) {
|
|
|
|
|
|
|
|
List<String> batch = numberList.subList(i, Math.min(numberList.size(), i + batchSize));
|
|
|
|
|
|
|
|
batches.add(new HashSet<>(batch));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return batches;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Set<String> executeAndMergeContactDiscoveryRequests(@NonNull SignalServiceAccountManager accountManager, @NonNull List<Future<Set<String>>> futures) {
|
|
|
|
|
|
|
|
Set<String> results = new HashSet<>();
|
|
|
|
|
|
|
|
for (Future<Set<String>> future : futures) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
results.addAll(future.get());
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
|
|
Log.w(TAG, "Contact discovery batch was interrupted.", e);
|
|
|
|
|
|
|
|
accountManager.reportContactDiscoveryServiceUnexpectedError();
|
|
|
|
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
|
|
|
|
if (isAttestationError(e.getCause())) {
|
|
|
|
|
|
|
|
Log.w(TAG, "Failed during attestation.", e);
|
|
|
|
|
|
|
|
accountManager.reportContactDiscoveryServiceAttestationError();
|
|
|
|
|
|
|
|
} else if (e.getCause() instanceof PushNetworkException) {
|
|
|
|
|
|
|
|
Log.w(TAG, "Failed due to poor network.", e);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Log.w(TAG, "Failed for an unknown reason.", e);
|
|
|
|
|
|
|
|
accountManager.reportContactDiscoveryServiceUnexpectedError();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static boolean isAttestationError(Throwable e) {
|
|
|
|
|
|
|
|
return e instanceof CertificateException ||
|
|
|
|
|
|
|
|
e instanceof SignatureException ||
|
|
|
|
|
|
|
|
e instanceof UnauthenticatedQuoteException ||
|
|
|
|
|
|
|
|
e instanceof UnauthenticatedResponseException ||
|
|
|
|
|
|
|
|
e instanceof Quote.InvalidQuoteFormatException;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static KeyStore getIasKeyStore(@NonNull Context context)
|
|
|
|
|
|
|
|
throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
TrustStore contactTrustStore = new IasTrustStore(context);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KeyStore keyStore = KeyStore.getInstance("BKS");
|
|
|
|
|
|
|
|
keyStore.load(contactTrustStore.getKeyStoreInputStream(), contactTrustStore.getKeyStorePassword().toCharArray());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return keyStore;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class DirectoryResult {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final Set<String> numbers;
|
|
|
|
|
|
|
|
private final List<Address> newlyActiveAddresses;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DirectoryResult(@NonNull Set<String> numbers) {
|
|
|
|
|
|
|
|
this(numbers, Collections.emptyList());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DirectoryResult(@NonNull Set<String> numbers, @NonNull List<Address> newlyActiveAddresses) {
|
|
|
|
|
|
|
|
this.numbers = numbers;
|
|
|
|
|
|
|
|
this.newlyActiveAddresses = newlyActiveAddresses;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> getNumbers() {
|
|
|
|
|
|
|
|
return numbers;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Address> getNewlyActiveAddresses() {
|
|
|
|
|
|
|
|
return newlyActiveAddresses;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static class AccountHolder {
|
|
|
|
private static class AccountHolder {
|
|
|
|
|
|
|
|
|
|
|
|
private final boolean fresh;
|
|
|
|
private final boolean fresh;
|
|
|
|