2014-02-07 02:06:23 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
import android.Manifest;
|
2015-07-14 21:31:03 +00:00
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.AccountManager;
|
2018-07-18 15:27:05 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2015-07-14 21:31:03 +00:00
|
|
|
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;
|
2017-08-07 22:31:12 +00:00
|
|
|
import android.database.Cursor;
|
2017-11-26 18:45:39 +00:00
|
|
|
import android.net.Uri;
|
2015-07-14 21:31:03 +00:00
|
|
|
import android.os.RemoteException;
|
|
|
|
import android.provider.ContactsContract;
|
2015-10-27 19:18:02 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2017-08-02 19:51:46 +00:00
|
|
|
import android.text.TextUtils;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2014-02-07 02:06:23 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
import com.annimon.stream.Collectors;
|
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
2015-09-22 00:41:27 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2018-07-18 15:27:05 +00:00
|
|
|
import org.thoughtcrime.securesms.BuildConfig;
|
2014-03-18 06:25:09 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2017-08-07 21:24:53 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
2015-12-04 20:12:48 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.SessionUtil;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2015-07-14 21:31:03 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2017-01-22 21:52:36 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult;
|
2017-08-22 01:37:39 +00:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
2017-08-22 17:44:04 +00:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase.RegisteredState;
|
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;
|
2018-08-16 16:47:43 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
2017-11-25 06:00:30 +00:00
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
2018-07-18 15:27:05 +00:00
|
|
|
import org.thoughtcrime.securesms.push.IasTrustStore;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-10-27 19:18:02 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingJoinedMessage;
|
2018-07-18 15:27:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
|
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;
|
2018-07-18 15:27:05 +00:00
|
|
|
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;
|
2014-02-07 02:06:23 +00:00
|
|
|
|
2014-11-27 23:24:26 +00:00
|
|
|
import java.io.IOException;
|
2018-07-18 15:27:05 +00:00
|
|
|
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;
|
2015-12-19 12:07:44 +00:00
|
|
|
import java.util.Calendar;
|
2017-11-20 22:48:39 +00:00
|
|
|
import java.util.Collections;
|
2017-08-02 19:51:46 +00:00
|
|
|
import java.util.HashSet;
|
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;
|
2018-07-18 15:27:05 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.Future;
|
2014-02-07 02:06:23 +00:00
|
|
|
|
|
|
|
public class DirectoryHelper {
|
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
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
private static final int CONTACT_DISCOVERY_BATCH_SIZE = 2048;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public static void refreshDirectory(@NonNull Context context, boolean notifyOfNewUsers)
|
2015-10-27 19:18:02 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
2017-08-02 19:51:46 +00:00
|
|
|
if (TextUtils.isEmpty(TextSecurePreferences.getLocalNumber(context))) return;
|
2017-11-25 06:00:30 +00:00
|
|
|
if (!Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) return;
|
2017-08-02 19:51:46 +00:00
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
List<Address> newlyActiveUsers = refreshDirectory(context, AccountManagerFactory.createManager(context));
|
2015-10-27 19:18:02 +00:00
|
|
|
|
2018-01-04 19:56:55 +00:00
|
|
|
if (TextSecurePreferences.isMultiDevice(context)) {
|
2015-10-27 19:18:02 +00:00
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
if (notifyOfNewUsers) notifyNewUsers(context, newlyActiveUsers);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
@SuppressLint("CheckResult")
|
2017-12-07 19:53:17 +00:00
|
|
|
private static @NonNull List<Address> refreshDirectory(@NonNull Context context, @NonNull SignalServiceAccountManager accountManager)
|
2014-11-27 23:24:26 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
2017-08-02 19:51:46 +00:00
|
|
|
if (TextUtils.isEmpty(TextSecurePreferences.getLocalNumber(context))) {
|
2018-07-18 15:27:05 +00:00
|
|
|
return Collections.emptyList();
|
2017-08-02 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
if (!Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) {
|
2018-07-18 15:27:05 +00:00
|
|
|
return Collections.emptyList();
|
2017-11-25 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
2017-11-26 18:45:39 +00:00
|
|
|
Stream<String> eligibleRecipientDatabaseContactNumbers = Stream.of(recipientDatabase.getAllAddresses()).filter(Address::isPhone).map(Address::toPhoneString);
|
2017-08-22 17:44:04 +00:00
|
|
|
Stream<String> eligibleSystemDatabaseContactNumbers = Stream.of(ContactAccessor.getInstance().getAllContactsWithNumbers(context)).map(Address::serialize);
|
|
|
|
Set<String> eligibleContactNumbers = Stream.concat(eligibleRecipientDatabaseContactNumbers, eligibleSystemDatabaseContactNumbers).collect(Collectors.toSet());
|
2017-08-02 19:51:46 +00:00
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
Future<DirectoryResult> legacyRequest = getLegacyDirectoryResult(context, accountManager, recipientDatabase, eligibleContactNumbers);
|
|
|
|
List<Future<Set<String>>> contactServiceRequest = getContactServiceDirectoryResult(context, accountManager, eligibleContactNumbers);
|
2017-08-22 17:44:04 +00:00
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
try {
|
2018-10-02 16:16:37 +00:00
|
|
|
DirectoryResult legacyResult = legacyRequest.get();
|
|
|
|
Optional<Set<String>> contactServiceResult = executeAndMergeContactDiscoveryRequests(accountManager, contactServiceRequest);
|
2017-08-07 21:24:53 +00:00
|
|
|
|
2018-10-02 16:16:37 +00:00
|
|
|
if (!contactServiceResult.isPresent()) {
|
|
|
|
Log.i(TAG, "[Batch] New contact discovery service failed, so we're skipping the comparison.");
|
|
|
|
return legacyResult.getNewlyActiveAddresses();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (legacyResult.getNumbers().size() == contactServiceResult.get().size() && legacyResult.getNumbers().containsAll(contactServiceResult.get())) {
|
2018-07-18 15:27:05 +00:00
|
|
|
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();
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
return legacyResult.getNewlyActiveAddresses();
|
2017-11-20 22:48:39 +00:00
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new IOException("[Batch] Operation was interrupted.", e);
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
if (e.getCause() instanceof IOException) {
|
|
|
|
throw (IOException) e.getCause();
|
2017-11-21 19:54:18 +00:00
|
|
|
} else {
|
2018-07-18 15:27:05 +00:00
|
|
|
Log.e(TAG, "[Batch] Experienced an unexpected exception.", e);
|
|
|
|
throw new AssertionError(e);
|
2017-11-21 19:54:18 +00:00
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 08:30:29 +00:00
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
public static RegisteredState refreshDirectoryFor(@NonNull Context context,
|
|
|
|
@NonNull Recipient recipient)
|
2015-09-22 00:41:27 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
2017-08-22 01:47:37 +00:00
|
|
|
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
2017-08-07 21:24:53 +00:00
|
|
|
SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
|
2015-09-22 00:41:27 +00:00
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
Future<RegisteredState> legacyRequest = getLegacyRegisteredState(context, accountManager, recipientDatabase, recipient);
|
|
|
|
List<Future<Set<String>>> contactServiceRequest = getContactServiceDirectoryResult(context, accountManager, Collections.singleton(recipient.getAddress().serialize()));
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
try {
|
2018-10-02 16:16:37 +00:00
|
|
|
RegisteredState legacyState = legacyRequest.get();
|
|
|
|
Optional<Set<String>> contactServiceResult = executeAndMergeContactDiscoveryRequests(accountManager, contactServiceRequest);
|
|
|
|
|
|
|
|
if (!contactServiceResult.isPresent()) {
|
|
|
|
Log.i(TAG, "[Singular] New contact discovery service failed, so we're skipping the comparison.");
|
|
|
|
return legacyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegisteredState contactServiceState = contactServiceResult.get().size() == 1 ? RegisteredState.REGISTERED : RegisteredState.NOT_REGISTERED;
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
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();
|
2017-07-26 16:59:15 +00:00
|
|
|
}
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
return legacyState;
|
2017-07-26 16:59:15 +00:00
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2017-08-07 21:24:53 +00:00
|
|
|
}
|
2014-02-26 08:30:29 +00:00
|
|
|
}
|
2014-03-18 06:25:09 +00:00
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
private static void updateContactsDatabase(@NonNull Context context, @NonNull List<Address> activeAddresses, boolean removeMissing) {
|
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 {
|
2018-04-05 12:37:36 +00:00
|
|
|
DatabaseFactory.getContactsDatabase(context).removeDeletedRawContacts(account.get().getAccount());
|
2017-11-20 22:48:39 +00:00
|
|
|
DatabaseFactory.getContactsDatabase(context).setRegisteredUsers(account.get().getAccount(), activeAddresses, removeMissing);
|
2017-08-07 22:31:12 +00:00
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
Cursor cursor = ContactAccessor.getInstance().getAllSystemContacts(context);
|
2017-11-26 18:45:39 +00:00
|
|
|
RecipientDatabase.BulkOperationsHandle handle = DatabaseFactory.getRecipientDatabase(context).resetAllSystemContactInfo();
|
2017-08-07 22:31:12 +00:00
|
|
|
|
2017-08-16 02:23:42 +00:00
|
|
|
try {
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
|
2017-08-07 22:31:12 +00:00
|
|
|
|
2017-08-16 02:23:42 +00:00
|
|
|
if (!TextUtils.isEmpty(number)) {
|
2017-11-26 18:45:39 +00:00
|
|
|
Address address = Address.fromExternal(context, number);
|
|
|
|
String displayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
|
|
|
|
String contactPhotoUri = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
|
|
|
|
String contactLabel = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.LABEL));
|
|
|
|
Uri contactUri = ContactsContract.Contacts.getLookupUri(cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone._ID)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY)));
|
|
|
|
|
|
|
|
handle.setSystemContactInfo(address, displayName, contactPhotoUri, contactLabel, contactUri.toString());
|
2017-08-16 02:23:42 +00:00
|
|
|
}
|
2017-08-07 22:31:12 +00:00
|
|
|
}
|
2017-08-16 02:23:42 +00:00
|
|
|
} finally {
|
|
|
|
handle.finish();
|
2017-08-07 22:31:12 +00:00
|
|
|
}
|
2017-01-05 20:42:28 +00:00
|
|
|
|
2018-08-16 16:47:43 +00:00
|
|
|
if (NotificationChannels.supported()) {
|
|
|
|
try (RecipientDatabase.RecipientReader recipients = DatabaseFactory.getRecipientDatabase(context).getRecipientsWithNotificationChannels()) {
|
|
|
|
Recipient recipient;
|
|
|
|
while ((recipient = recipients.getNext()) != null) {
|
|
|
|
NotificationChannels.updateContactChannelName(context, recipient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-09 22:51:53 +00:00
|
|
|
} catch (RemoteException | OperationApplicationException e) {
|
2019-02-01 20:47:34 +00:00
|
|
|
Log.w(TAG, "Failed to update contacts.", e);
|
2015-11-09 22:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void notifyNewUsers(@NonNull Context context,
|
2017-08-02 19:51:46 +00:00
|
|
|
@NonNull List<Address> newUsers)
|
2015-11-09 22:51:53 +00:00
|
|
|
{
|
2015-12-19 12:07:44 +00:00
|
|
|
if (!TextSecurePreferences.isNewContactsNotificationEnabled(context)) return;
|
|
|
|
|
2017-08-02 19:51:46 +00:00
|
|
|
for (Address newUser: newUsers) {
|
2018-01-25 03:17:44 +00:00
|
|
|
if (!SessionUtil.hasSession(context, newUser) && !Util.isOwnNumber(context, newUser)) {
|
2017-01-22 21:52:36 +00:00
|
|
|
IncomingJoinedMessage message = new IncomingJoinedMessage(newUser);
|
|
|
|
Optional<InsertResult> insertResult = DatabaseFactory.getSmsDatabase(context).insertMessageInbox(message);
|
|
|
|
|
|
|
|
if (insertResult.isPresent()) {
|
|
|
|
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
|
|
|
|
if (hour >= 9 && hour < 23) {
|
2018-01-25 03:17:44 +00:00
|
|
|
MessageNotifier.updateNotification(context, insertResult.get().getThreadId(), true);
|
2017-01-22 21:52:36 +00:00
|
|
|
} else {
|
2018-01-25 03:17:44 +00:00
|
|
|
MessageNotifier.updateNotification(context, insertResult.get().getThreadId(), false);
|
2017-01-22 21:52:36 +00:00
|
|
|
}
|
2015-12-19 12:07:44 +00:00
|
|
|
}
|
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)) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Created new account...");
|
2015-07-14 21:31:03 +00:00
|
|
|
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
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
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)
|
|
|
|
{
|
2018-09-28 02:59:57 +00:00
|
|
|
Set<String> sanitizedNumbers = sanitizeNumbers(eligibleContactNumbers);
|
|
|
|
List<Set<String>> batches = splitIntoBatches(sanitizedNumbers, CONTACT_DISCOVERY_BATCH_SIZE);
|
|
|
|
List<Future<Set<String>>> futures = new ArrayList<>(batches.size());
|
2018-07-18 15:27:05 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-28 02:59:57 +00:00
|
|
|
private static Set<String> sanitizeNumbers(@NonNull Set<String> numbers) {
|
2018-10-02 16:16:37 +00:00
|
|
|
return Stream.of(numbers).filter(number -> {
|
|
|
|
try {
|
|
|
|
return number.startsWith("+") && number.length() > 1 && Long.parseLong(number.substring(1)) > 0;
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}).collect(Collectors.toSet());
|
2018-09-28 02:59:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:27:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:16:37 +00:00
|
|
|
private static Optional<Set<String>> executeAndMergeContactDiscoveryRequests(@NonNull SignalServiceAccountManager accountManager, @NonNull List<Future<Set<String>>> futures) {
|
2018-07-18 15:27:05 +00:00
|
|
|
Set<String> results = new HashSet<>();
|
2018-10-02 16:16:37 +00:00
|
|
|
try {
|
|
|
|
for (Future<Set<String>> future : futures) {
|
2018-07-18 15:27:05 +00:00
|
|
|
results.addAll(future.get());
|
2018-10-02 16:16:37 +00:00
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.w(TAG, "Contact discovery batch was interrupted.", e);
|
|
|
|
accountManager.reportContactDiscoveryServiceUnexpectedError();
|
|
|
|
return Optional.absent();
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
if (isAttestationError(e.getCause())) {
|
|
|
|
Log.w(TAG, "Failed during attestation.", e);
|
|
|
|
accountManager.reportContactDiscoveryServiceAttestationError();
|
|
|
|
return Optional.absent();
|
|
|
|
} else if (e.getCause() instanceof PushNetworkException) {
|
|
|
|
Log.w(TAG, "Failed due to poor network.", e);
|
|
|
|
return Optional.absent();
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Failed for an unknown reason.", e);
|
2018-07-18 15:27:05 +00:00
|
|
|
accountManager.reportContactDiscoveryServiceUnexpectedError();
|
2018-10-02 16:16:37 +00:00
|
|
|
return Optional.absent();
|
2018-07-18 15:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:16:37 +00:00
|
|
|
return Optional.of(results);
|
2018-07-18 15:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-26 18:45:39 +00:00
|
|
|
@SuppressWarnings("unused")
|
2017-01-05 20:42:28 +00:00
|
|
|
public boolean isFresh() {
|
|
|
|
return fresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Account getAccount() {
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|