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-07 21:24:53 +00:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
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-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
|
|
|
|
2017-08-07 21:33:21 +00:00
|
|
|
public enum Capability {
|
|
|
|
UNKNOWN, SUPPORTED, UNSUPPORTED
|
2015-09-22 00:41:27 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 08:30:29 +00:00
|
|
|
private static final String TAG = DirectoryHelper.class.getSimpleName();
|
2014-02-07 02:06:23 +00:00
|
|
|
|
2015-10-27 19:18:02 +00:00
|
|
|
public static void refreshDirectory(@NonNull Context context, @Nullable MasterSecret masterSecret)
|
|
|
|
throws IOException
|
|
|
|
{
|
2017-08-02 19:51:46 +00:00
|
|
|
if (TextUtils.isEmpty(TextSecurePreferences.getLocalNumber(context))) return;
|
|
|
|
|
|
|
|
RefreshResult result = refreshDirectory(context, AccountManagerFactory.createManager(context));
|
2015-10-27 19:18:02 +00:00
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
if (!result.getNewUsers().isEmpty() && TextSecurePreferences.isMultiDevice(context)) {
|
2015-10-27 19:18:02 +00:00
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
if (!result.isFresh()) {
|
|
|
|
notifyNewUsers(context, masterSecret, result.getNewUsers());
|
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 19:51:46 +00:00
|
|
|
public static @NonNull RefreshResult 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-08-07 21:24:53 +00:00
|
|
|
return new RefreshResult(new LinkedList<>(), false);
|
2017-08-02 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
RecipientPreferenceDatabase recipientPreferenceDatabase = DatabaseFactory.getRecipientPreferenceDatabase(context);
|
|
|
|
Set<Address> eligibleContactNumbers = recipientPreferenceDatabase.getAllRecipients();
|
|
|
|
eligibleContactNumbers.addAll(ContactAccessor.getInstance().getAllContactsWithNumbers(context));
|
2017-08-02 19:51:46 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
Set<String> serializedAddress = Stream.of(eligibleContactNumbers).map(Address::serialize).collect(Collectors.toSet());
|
|
|
|
List<ContactTokenDetails> activeTokens = accountManager.getContacts(serializedAddress);
|
2014-02-07 02:06:23 +00:00
|
|
|
|
|
|
|
if (activeTokens != null) {
|
2017-08-07 21:24:53 +00:00
|
|
|
List<Address> activeAddresses = new LinkedList<>();
|
|
|
|
Set<Address> inactiveAddresses = new HashSet<>(eligibleContactNumbers);
|
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
for (ContactTokenDetails activeToken : activeTokens) {
|
2017-08-07 21:24:53 +00:00
|
|
|
Address activeAddress = Address.fromSerialized(activeToken.getNumber());
|
|
|
|
activeAddresses.add(activeAddress);
|
|
|
|
inactiveAddresses.remove(activeAddress);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
recipientPreferenceDatabase.setRegistered(activeAddresses, new LinkedList<>(inactiveAddresses));
|
2017-08-07 22:31:12 +00:00
|
|
|
return updateContactsDatabase(context, activeAddresses, true);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
2015-10-27 19:18:02 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
return new RefreshResult(new LinkedList<>(), false);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
2014-02-26 08:30:29 +00:00
|
|
|
|
2017-08-07 21:33:21 +00:00
|
|
|
public static Capability refreshDirectoryFor(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
@NonNull Recipient recipient)
|
2015-09-22 00:41:27 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
2017-08-07 21:24:53 +00:00
|
|
|
RecipientPreferenceDatabase recipientDatabase = DatabaseFactory.getRecipientPreferenceDatabase(context);
|
|
|
|
SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
|
|
|
|
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-07 21:24:53 +00:00
|
|
|
recipientDatabase.setRegistered(Util.asList(recipient.getAddress()), new LinkedList<>());
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2017-08-07 22:31:12 +00:00
|
|
|
RefreshResult result = updateContactsDatabase(context, Util.asList(recipient.getAddress()), false);
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (!result.getNewUsers().isEmpty() && TextSecurePreferences.isMultiDevice(context)) {
|
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context));
|
|
|
|
}
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (!result.isFresh()) {
|
|
|
|
notifyNewUsers(context, masterSecret, result.getNewUsers());
|
2015-09-22 00:41:27 +00:00
|
|
|
}
|
2017-07-26 16:59:15 +00:00
|
|
|
|
2017-08-07 21:33:21 +00:00
|
|
|
return Capability.SUPPORTED;
|
2017-07-26 16:59:15 +00:00
|
|
|
} else {
|
2017-08-07 21:24:53 +00:00
|
|
|
recipientDatabase.setRegistered(new LinkedList<>(), Util.asList(recipient.getAddress()));
|
2017-08-07 21:33:21 +00:00
|
|
|
return Capability.UNSUPPORTED;
|
2015-09-22 00:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 21:33:21 +00:00
|
|
|
public static @NonNull Capability getUserCapabilities(@NonNull Context context, @Nullable Recipient recipient) {
|
2017-08-07 21:24:53 +00:00
|
|
|
if (recipient == null) {
|
2017-08-07 21:33:21 +00:00
|
|
|
return Capability.UNSUPPORTED;
|
2017-08-07 21:24:53 +00:00
|
|
|
}
|
2014-04-03 17:47:51 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
2017-08-07 21:33:21 +00:00
|
|
|
return Capability.UNSUPPORTED;
|
2017-08-07 21:24:53 +00:00
|
|
|
}
|
2014-02-26 08:30:29 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
if (recipient.isMmsGroupRecipient()) {
|
2017-08-07 21:33:21 +00:00
|
|
|
return Capability.UNSUPPORTED;
|
2017-08-07 21:24:53 +00:00
|
|
|
}
|
2014-04-03 17:47:51 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
if (recipient.isPushGroupRecipient()) {
|
2017-08-07 21:33:21 +00:00
|
|
|
return Capability.SUPPORTED;
|
2017-08-07 21:24:53 +00:00
|
|
|
}
|
2014-04-03 17:47:51 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
final RecipientPreferenceDatabase recipientDatabase = DatabaseFactory.getRecipientPreferenceDatabase(context);
|
|
|
|
final Optional<RecipientsPreferences> recipientPreferences = recipientDatabase.getRecipientsPreferences(recipient.getAddress());
|
2015-09-29 21:26:37 +00:00
|
|
|
|
2017-08-07 21:33:21 +00:00
|
|
|
if (recipientPreferences.isPresent() && recipientPreferences.get().isRegistered()) return Capability.SUPPORTED;
|
|
|
|
else if (recipientPreferences.isPresent()) return Capability.UNSUPPORTED;
|
|
|
|
else return Capability.UNKNOWN;
|
2014-02-26 08:30:29 +00:00
|
|
|
}
|
2014-03-18 06:25:09 +00:00
|
|
|
|
2017-08-07 22:31:12 +00:00
|
|
|
private static @NonNull RefreshResult 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-08-02 19:51:46 +00:00
|
|
|
List<Address> newUsers = DatabaseFactory.getContactsDatabase(context)
|
2017-08-07 22:31:12 +00:00
|
|
|
.setRegisteredUsers(account.get().getAccount(), activeAddresses, removeMissing);
|
|
|
|
|
2017-08-16 02:23:42 +00:00
|
|
|
Cursor cursor = ContactAccessor.getInstance().getAllSystemContacts(context);
|
|
|
|
RecipientPreferenceDatabase.BulkOperationsHandle handle = DatabaseFactory.getRecipientPreferenceDatabase(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)) {
|
|
|
|
Address address = Address.fromExternal(context, number);
|
|
|
|
String displayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
|
2017-08-07 22:31:12 +00:00
|
|
|
|
2017-08-16 02:23:42 +00:00
|
|
|
handle.setDisplayName(address, displayName);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
return new RefreshResult(newUsers, account.get().isFresh());
|
2015-11-09 22:51:53 +00:00
|
|
|
} catch (RemoteException | OperationApplicationException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:51:46 +00:00
|
|
|
return new RefreshResult(new LinkedList<Address>(), false);
|
2015-11-09 22:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class RefreshResult {
|
|
|
|
|
2017-08-02 19:51:46 +00:00
|
|
|
private final List<Address> newUsers;
|
|
|
|
private final boolean fresh;
|
2017-01-05 20:42:28 +00:00
|
|
|
|
2017-08-02 19:51:46 +00:00
|
|
|
private RefreshResult(List<Address> newUsers, boolean fresh) {
|
2017-01-05 20:42:28 +00:00
|
|
|
this.newUsers = newUsers;
|
|
|
|
this.fresh = fresh;
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:51:46 +00:00
|
|
|
public List<Address> getNewUsers() {
|
2017-01-05 20:42:28 +00:00
|
|
|
return newUsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFresh() {
|
|
|
|
return fresh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|