Add preliminary contact discovery service support.
This commit is contained in:
parent
08ace15f95
commit
a0ab252bc9
6 changed files with 299 additions and 56 deletions
10
build.gradle
10
build.gradle
|
@ -77,7 +77,7 @@ dependencies {
|
|||
compile 'com.google.android.exoplayer:exoplayer-core:2.8.4'
|
||||
compile 'com.google.android.exoplayer:exoplayer-ui:2.8.4'
|
||||
|
||||
compile 'org.whispersystems:signal-service-android:2.8.1'
|
||||
compile 'org.whispersystems:signal-service-android:2.9.0'
|
||||
compile 'org.whispersystems:webrtc-android:M69'
|
||||
|
||||
compile "me.leolin:ShortcutBadger:1.1.16"
|
||||
|
@ -171,7 +171,7 @@ dependencyVerification {
|
|||
'com.google.android.gms:play-services-maps:45e8021e7ddac4a44a82a0e9698991389ded3023d35c58f38dbd86d54211ec0e',
|
||||
'com.google.android.exoplayer:exoplayer-ui:027557b2d69b15e1852a2530b36971f0dcc177abae240ee35e05f63502cdb0a7',
|
||||
'com.google.android.exoplayer:exoplayer-core:e69b409e11887c955deb373357c30eeabf183395db0092b4817e0f80bb467d5b',
|
||||
'org.whispersystems:signal-service-android:414e91598abd941eb3be9a85702538cc9928d8c22f00e07716b83a096cbbe54d',
|
||||
'org.whispersystems:signal-service-android:bf469abcdcd2b2ba429024aca30713eaa3b83a77af34030a818b1c9fb4780044',
|
||||
'org.whispersystems:webrtc-android:5493c92141ce884fc5ce8240d783232f4fe14bd17a8d0d7d1bd4944d0bd1682f',
|
||||
'me.leolin:ShortcutBadger:e3cb3e7625892129b0c92dd5e4bc649faffdd526d5af26d9c45ee31ff8851774',
|
||||
'se.emilsjolander:stickylistheaders:a08ca948aa6b220f09d82f16bbbac395f6b78897e9eeac6a9f0b0ba755928eeb',
|
||||
|
@ -218,7 +218,7 @@ dependencyVerification {
|
|||
'com.github.bumptech.glide:gifdecoder:59ccf3bb0cec11dab4b857382cbe0b171111b6fc62bf141adce4e1180889af15',
|
||||
'com.android.support:support-annotations:af05330d997eb92a066534dbe0a3ea24347d26d7001221092113ae02a8f233da',
|
||||
'org.whispersystems:signal-protocol-android:5b8acded7f2a40178eb90ab8e8cbfec89d170d91b3ff5e78487d1098df6185a1',
|
||||
'org.whispersystems:signal-service-java:c7ab92374e9656ba86a8d859cec71d03a68bba3e7ec0b7c597b726bf720eac21',
|
||||
'org.whispersystems:signal-service-java:4db9adf763071756cfd93fe48a40850f684ca02f2dea59601841abba7715c5c1',
|
||||
'com.github.bumptech.glide:disklrucache:c1b1b6f5bbd01e2fcdc9d7f60913c8d338bdb65ed4a93bfa02b56f19daaade4b',
|
||||
'com.github.bumptech.glide:annotations:bede99ef9f71517a4274bac18fd3e483e9f2b6108d7d6fe8f4949be4aa4d9512',
|
||||
'com.nineoldandroids:library:68025a14e3e7673d6ad2f95e4b46d78d7d068343aa99256b686fe59de1b3163a',
|
||||
|
@ -232,7 +232,9 @@ dependencyVerification {
|
|||
'com.googlecode.libphonenumber:libphonenumber:183392c0565be16d3f6f86680b4106bbde6fe31a402ad21bf9823d938c0c8706',
|
||||
'com.fasterxml.jackson.core:jackson-databind:835097bcdd11f5bc8a08378c70d4c8054dfa4b911691cc2752063c75534d198d',
|
||||
'com.squareup.okhttp3:okhttp:7265adbd6f028aade307f58569d814835cd02bc9beffb70c25f72c9de50d61c4',
|
||||
'com.madgag.spongycastle:pkix:0d9cca6991f68eb373cfad309d5268c9fc38db5efb5fe00dcccf5c973af1eca1',
|
||||
'com.madgag.spongycastle:prov:b8c3fec3a59aac1aa04ccf4dad7179351e54ef7672f53f508151b614c131398a',
|
||||
'org.threeten:threetenbp:f4c23ffaaed717c3b99c003e0ee02d6d66377fd47d866fec7d971bd8644fc1a7',
|
||||
'org.whispersystems:curve25519-java:7dd659d8822c06c3aea1a47f18fac9e5761e29cab8100030b877db445005f03e',
|
||||
'com.fasterxml.jackson.core:jackson-annotations:0ca408c24202a7626ec8b861e99d85eca5e38b73311dd6dd12e3e9deecc3fe94',
|
||||
'com.fasterxml.jackson.core:jackson-core:cbf4604784b4de226262845447a1ad3bb38a6728cebe86562e2c5afada8be2c0',
|
||||
|
@ -265,11 +267,13 @@ android {
|
|||
buildConfigField "long", "BUILD_TIMESTAMP", getLastCommitTimestamp() + "L"
|
||||
buildConfigField "String", "SIGNAL_URL", "\"https://textsecure-service.whispersystems.org\""
|
||||
buildConfigField "String", "SIGNAL_CDN_URL", "\"https://cdn.signal.org\""
|
||||
buildConfigField "String", "SIGNAL_CONTACT_DISCOVERY_URL", "\"https://api.directory.signal.org\""
|
||||
buildConfigField "String", "SIGNAL_SERVICE_STATUS_URL", "\"uptime.signal.org\""
|
||||
buildConfigField "String", "GIPHY_PROXY_HOST", "\"giphy-proxy-production.whispersystems.org\""
|
||||
buildConfigField "int", "GIPHY_PROXY_PORT", "80"
|
||||
buildConfigField "String", "USER_AGENT", "\"OWA\""
|
||||
buildConfigField "boolean", "DEV_BUILD", "false"
|
||||
buildConfigField "String", "MRENCLAVE", "\"cd6cfc342937b23b1bdd3bbf9721aa5615ac9ff50a75c5527d441cd3276826c9\""
|
||||
|
||||
ndk {
|
||||
abiFilters "armeabi", "armeabi-v7a", "x86"
|
||||
|
|
BIN
res/raw/ias.store
Normal file
BIN
res/raw/ias.store
Normal file
Binary file not shown.
27
src/org/thoughtcrime/securesms/push/IasTrustStore.java
Normal file
27
src/org/thoughtcrime/securesms/push/IasTrustStore.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package org.thoughtcrime.securesms.push;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.whispersystems.signalservice.api.push.TrustStore;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class IasTrustStore implements TrustStore {
|
||||
|
||||
private final Context context;
|
||||
|
||||
public IasTrustStore(Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getKeyStoreInputStream() {
|
||||
return context.getResources().openRawResource(R.raw.ias);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKeyStorePassword() {
|
||||
return "whisper";
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ import org.thoughtcrime.securesms.BuildConfig;
|
|||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.whispersystems.signalservice.api.push.TrustStore;
|
||||
import org.whispersystems.signalservice.internal.configuration.SignalCdnUrl;
|
||||
import org.whispersystems.signalservice.internal.configuration.SignalContactDiscoveryUrl;
|
||||
import org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration;
|
||||
import org.whispersystems.signalservice.internal.configuration.SignalServiceUrl;
|
||||
|
||||
|
@ -54,8 +55,10 @@ public class SignalServiceNetworkAccess {
|
|||
final TrustStore trustStore = new DomainFrontingTrustStore(context);
|
||||
final SignalServiceUrl service = new SignalServiceUrl("https://cms.souqcdn.com", SERVICE_REFLECTOR_HOST, trustStore, SOUQ_CONNECTION_SPEC);
|
||||
final SignalCdnUrl serviceCdn = new SignalCdnUrl("https://cms.souqcdn.com", SERVICE_REFLECTOR_HOST, trustStore, SOUQ_CONNECTION_SPEC);
|
||||
final SignalContactDiscoveryUrl serviceContact = new SignalContactDiscoveryUrl("https://cms.souqcdn.com", SERVICE_REFLECTOR_HOST, trustStore, SOUQ_CONNECTION_SPEC);
|
||||
final SignalServiceConfiguration serviceConfig = new SignalServiceConfiguration(new SignalServiceUrl[] { service },
|
||||
new SignalCdnUrl[] { serviceCdn });
|
||||
new SignalCdnUrl[] { serviceCdn },
|
||||
new SignalContactDiscoveryUrl[] { serviceContact });
|
||||
|
||||
this.censorshipConfiguration = new HashMap<String, SignalServiceConfiguration>() {{
|
||||
put(COUNTRY_CODE_EGYPT, serviceConfig);
|
||||
|
@ -65,7 +68,8 @@ public class SignalServiceNetworkAccess {
|
|||
}};
|
||||
|
||||
this.uncensoredConfiguration = new SignalServiceConfiguration(new SignalServiceUrl[] {new SignalServiceUrl(BuildConfig.SIGNAL_URL, new SignalServiceTrustStore(context))},
|
||||
new SignalCdnUrl[] {new SignalCdnUrl(BuildConfig.SIGNAL_CDN_URL, new SignalServiceTrustStore(context))});
|
||||
new SignalCdnUrl[] {new SignalCdnUrl(BuildConfig.SIGNAL_CDN_URL, new SignalServiceTrustStore(context))},
|
||||
new SignalContactDiscoveryUrl[] {new SignalContactDiscoveryUrl(BuildConfig.SIGNAL_CONTACT_DISCOVERY_URL, new SignalServiceTrustStore(context))});
|
||||
|
||||
this.censoredCountries = this.censorshipConfiguration.keySet().toArray(new String[0]);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.util;
|
|||
import android.Manifest;
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.OperationApplicationException;
|
||||
|
@ -18,6 +19,7 @@ import com.annimon.stream.Collectors;
|
|||
import com.annimon.stream.Stream;
|
||||
|
||||
import org.thoughtcrime.securesms.ApplicationContext;
|
||||
import org.thoughtcrime.securesms.BuildConfig;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
||||
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.permissions.Permissions;
|
||||
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
||||
import org.thoughtcrime.securesms.push.IasTrustStore;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.sms.IncomingJoinedMessage;
|
||||
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
|
||||
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.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.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.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class DirectoryHelper {
|
||||
|
||||
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)
|
||||
throws IOException
|
||||
{
|
||||
|
@ -66,15 +85,16 @@ public class DirectoryHelper {
|
|||
if (notifyOfNewUsers) notifyNewUsers(context, newlyActiveUsers);
|
||||
}
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
private static @NonNull List<Address> refreshDirectory(@NonNull Context context, @NonNull SignalServiceAccountManager accountManager)
|
||||
throws IOException
|
||||
{
|
||||
if (TextUtils.isEmpty(TextSecurePreferences.getLocalNumber(context))) {
|
||||
return new LinkedList<>();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (!Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) {
|
||||
return new LinkedList<>();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
||||
|
@ -82,42 +102,33 @@ public class DirectoryHelper {
|
|||
Stream<String> eligibleSystemDatabaseContactNumbers = Stream.of(ContactAccessor.getInstance().getAllContactsWithNumbers(context)).map(Address::serialize);
|
||||
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<>();
|
||||
try {
|
||||
DirectoryResult legacyResult = legacyRequest.get();
|
||||
Set<String> contactServiceResult = executeAndMergeContactDiscoveryRequests(accountManager, contactServiceRequest);
|
||||
|
||||
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);
|
||||
|
||||
if (TextSecurePreferences.hasSuccessfullyRetrievedDirectory(context)) {
|
||||
return newlyActiveAddresses;
|
||||
if (legacyResult.getNumbers().size() == contactServiceResult.size() && legacyResult.getNumbers().containsAll(contactServiceResult)) {
|
||||
Log.i(TAG, "[Batch] New contact discovery service request matched existing results.");
|
||||
accountManager.reportContactDiscoveryServiceMatch();
|
||||
} else {
|
||||
TextSecurePreferences.setHasSuccessfullyRetrievedDirectory(context, true);
|
||||
return new LinkedList<>();
|
||||
Log.w(TAG, "[Batch] New contact discovery service request did NOT match existing results.");
|
||||
accountManager.reportContactDiscoveryServiceMismatch();
|
||||
}
|
||||
|
||||
return legacyResult.getNewlyActiveAddresses();
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
throw new IOException("[Batch] Operation was interrupted.", e);
|
||||
} catch (ExecutionException e) {
|
||||
if (e.getCause() instanceof IOException) {
|
||||
throw (IOException) e.getCause();
|
||||
} else {
|
||||
Log.e(TAG, "[Batch] Experienced an unexpected exception.", e);
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
return new LinkedList<>();
|
||||
}
|
||||
|
||||
public static RegisteredState refreshDirectoryFor(@NonNull Context context,
|
||||
|
@ -126,30 +137,34 @@ public class DirectoryHelper {
|
|||
{
|
||||
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(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()) {
|
||||
recipientDatabase.setRegistered(recipient, RegisteredState.REGISTERED);
|
||||
Future<RegisteredState> legacyRequest = getLegacyRegisteredState(context, accountManager, recipientDatabase, recipient);
|
||||
List<Future<Set<String>>> contactServiceRequest = getContactServiceDirectoryResult(context, accountManager, Collections.singleton(recipient.getAddress().serialize()));
|
||||
|
||||
if (Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) {
|
||||
updateContactsDatabase(context, Util.asList(recipient.getAddress()), false);
|
||||
try {
|
||||
RegisteredState legacyState = legacyRequest.get();
|
||||
Set<String> contactServiceResult = executeAndMergeContactDiscoveryRequests(accountManager, contactServiceRequest);
|
||||
RegisteredState contactServiceState = contactServiceResult.size() == 1 ? RegisteredState.REGISTERED : RegisteredState.NOT_REGISTERED;
|
||||
|
||||
if (legacyState == contactServiceState) {
|
||||
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 && TextSecurePreferences.isMultiDevice(context)) {
|
||||
ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context));
|
||||
}
|
||||
return legacyState;
|
||||
|
||||
if (!activeUser && systemContact && !TextSecurePreferences.getNeedsSqlCipherMigration(context)) {
|
||||
notifyNewUsers(context, Collections.singletonList(recipient.getAddress()));
|
||||
} catch (InterruptedException e) {
|
||||
throw new IOException("[Singular] Operation was interrupted.", e);
|
||||
} catch (ExecutionException e) {
|
||||
if (e.getCause() instanceof IOException) {
|
||||
throw (IOException) e.getCause();
|
||||
} else {
|
||||
Log.e(TAG, "[Singular] Experienced an unexpected exception.", e);
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
|
||||
return RegisteredState.REGISTERED;
|
||||
} else {
|
||||
recipientDatabase.setRegistered(recipient, RegisteredState.NOT_REGISTERED);
|
||||
return RegisteredState.NOT_REGISTERED;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 final boolean fresh;
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package org.thoughtcrime.securesms.util.concurrent;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class SignalExecutors {
|
||||
|
||||
public static final ExecutorService IO = Executors.newCachedThreadPool(new ThreadFactory() {
|
||||
private final AtomicInteger counter = new AtomicInteger();
|
||||
@Override
|
||||
public Thread newThread(@NonNull Runnable r) {
|
||||
return new Thread(r, "signal-io-" + counter.getAndIncrement());
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Reference in a new issue