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;
|
2017-08-07 22:31:12 +00:00
|
|
|
import android.database.Cursor;
|
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;
|
|
|
|
import android.support.annotation.Nullable;
|
2017-08-02 19:51:46 +00:00
|
|
|
import android.text.TextUtils;
|
2014-02-26 08:30:29 +00:00
|
|
|
import android.util.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;
|
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-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;
|
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;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
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;
|
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;
|
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;
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-11-21 19:54:18 +00:00
|
|
|
public static void refreshDirectory(@NonNull Context context, @Nullable MasterSecret masterSecret, 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-20 22:48:39 +00:00
|
|
|
List<Address> newlyActiveUsers = refreshDirectory(context, AccountManagerFactory.createManager(context));
|
2015-10-27 19:18:02 +00:00
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
if (!newlyActiveUsers.isEmpty() && TextSecurePreferences.isMultiDevice(context)) {
|
2015-10-27 19:18:02 +00:00
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:54:18 +00:00
|
|
|
if (notifyOfNewUsers) notifyNewUsers(context, masterSecret, newlyActiveUsers);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
public 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))) {
|
2017-11-20 22:48:39 +00:00
|
|
|
return new LinkedList<>();
|
2017-08-02 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
|
|
|
Stream<String> eligibleRecipientDatabaseContactNumbers = Stream.of(recipientDatabase.getAllRecipients()).map(recipient -> recipient.getAddress().serialize());
|
|
|
|
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
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
List<ContactTokenDetails> activeTokens = accountManager.getContacts(eligibleContactNumbers);
|
2014-02-07 02:06:23 +00:00
|
|
|
|
|
|
|
if (activeTokens != null) {
|
2017-08-22 17:44:04 +00:00
|
|
|
List<Recipient> activeRecipients = new LinkedList<>();
|
|
|
|
List<Recipient> inactiveRecipients = new LinkedList<>();
|
|
|
|
|
|
|
|
Set<String> inactiveContactNumbers = new HashSet<>(eligibleContactNumbers);
|
2017-08-07 21:24:53 +00:00
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
for (ContactTokenDetails activeToken : activeTokens) {
|
2017-08-22 17:44:04 +00:00
|
|
|
activeRecipients.add(Recipient.from(context, Address.fromSerialized(activeToken.getNumber()), true));
|
|
|
|
inactiveContactNumbers.remove(activeToken.getNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String inactiveContactNumber : inactiveContactNumbers) {
|
|
|
|
inactiveRecipients.add(Recipient.from(context, Address.fromSerialized(inactiveContactNumber), true));
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
Set<Address> currentActiveAddresses = new HashSet<>(recipientDatabase.getRegistered());
|
|
|
|
List<Address> newlyActiveAddresses = Stream.of(activeRecipients)
|
|
|
|
.map(Recipient::getAddress)
|
|
|
|
.filter(address -> !currentActiveAddresses.contains(address))
|
|
|
|
.toList();
|
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
recipientDatabase.setRegistered(activeRecipients, inactiveRecipients);
|
2017-11-20 22:48:39 +00:00
|
|
|
updateContactsDatabase(context, Stream.of(activeRecipients).map(Recipient::getAddress).toList(), true);
|
|
|
|
|
2017-11-21 19:54:18 +00:00
|
|
|
if (TextSecurePreferences.hasSuccessfullyRetrievedDirectory(context)) {
|
|
|
|
return newlyActiveAddresses;
|
|
|
|
} else {
|
|
|
|
TextSecurePreferences.setHasSuccessfullyRetrievedDirectory(context, true);
|
|
|
|
return new LinkedList<>();
|
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
2015-10-27 19:18:02 +00:00
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
return new LinkedList<>();
|
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,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
@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);
|
2017-11-20 22:48:39 +00:00
|
|
|
boolean activeUser = recipient.resolve().getRegistered() == RegisteredState.REGISTERED;
|
2017-08-07 21:24:53 +00:00
|
|
|
String number = recipient.getAddress().serialize();
|
|
|
|
Optional<ContactTokenDetails> details = accountManager.getContact(number);
|
2015-09-22 00:41:27 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (details.isPresent()) {
|
2017-08-22 17:44:04 +00:00
|
|
|
recipientDatabase.setRegistered(recipient, RegisteredState.REGISTERED);
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
updateContactsDatabase(context, Util.asList(recipient.getAddress()), false);
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
if (!activeUser && TextSecurePreferences.isMultiDevice(context)) {
|
2017-07-26 16:59:15 +00:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
}
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
if (!activeUser) {
|
|
|
|
notifyNewUsers(context, masterSecret, Collections.singletonList(recipient.getAddress()));
|
2015-09-22 00:41:27 +00:00
|
|
|
}
|
2017-07-26 16:59:15 +00:00
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
return RegisteredState.REGISTERED;
|
2017-07-26 16:59:15 +00:00
|
|
|
} else {
|
2017-08-22 17:44:04 +00:00
|
|
|
recipientDatabase.setRegistered(recipient, RegisteredState.NOT_REGISTERED);
|
|
|
|
return RegisteredState.NOT_REGISTERED;
|
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 {
|
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-08-22 01:47:37 +00:00
|
|
|
RecipientDatabase.BulkOperationsHandle handle = DatabaseFactory.getRecipientDatabase(context).resetAllDisplayNames();
|
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-08-22 17:44:04 +00:00
|
|
|
Address address = Address.fromExternal(context, number);
|
|
|
|
Recipient recipient = Recipient.from(context, address, true);
|
|
|
|
String displayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
|
2017-08-07 22:31:12 +00:00
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
handle.setDisplayName(recipient, displayName);
|
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
|
|
|
|
2015-11-09 22:51:53 +00:00
|
|
|
} catch (RemoteException | OperationApplicationException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void notifyNewUsers(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
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) {
|
2016-02-14 13:58:39 +00:00
|
|
|
if (!SessionUtil.hasSession(context, masterSecret, 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) {
|
2017-03-09 01:38:55 +00:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret, insertResult.get().getThreadId(), true);
|
2017-01-22 21:52:36 +00:00
|
|
|
} else {
|
2017-03-09 01:38:55 +00:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret, 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)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|