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;
|
|
|
|
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;
|
|
|
|
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;
|
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;
|
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-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));
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:54:18 +00:00
|
|
|
if (notifyOfNewUsers) notifyNewUsers(context, masterSecret, newlyActiveUsers);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
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))) {
|
2017-11-20 22:48:39 +00:00
|
|
|
return new LinkedList<>();
|
2017-08-02 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
if (!Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) {
|
|
|
|
return new LinkedList<>();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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-11-26 18:45:39 +00:00
|
|
|
List<Address> activeAddresses = new LinkedList<>();
|
|
|
|
List<Address> inactiveAddresses = new LinkedList<>();
|
2017-08-22 17:44:04 +00:00
|
|
|
|
|
|
|
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-11-26 18:45:39 +00:00
|
|
|
activeAddresses.add(Address.fromSerialized(activeToken.getNumber()));
|
2017-08-22 17:44:04 +00:00
|
|
|
inactiveContactNumbers.remove(activeToken.getNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String inactiveContactNumber : inactiveContactNumbers) {
|
2017-11-26 18:45:39 +00:00
|
|
|
inactiveAddresses.add(Address.fromSerialized(inactiveContactNumber));
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 22:48:39 +00:00
|
|
|
Set<Address> currentActiveAddresses = new HashSet<>(recipientDatabase.getRegistered());
|
2017-12-07 19:53:17 +00:00
|
|
|
Set<Address> contactAddresses = new HashSet<>(recipientDatabase.getSystemContacts());
|
2017-11-26 18:45:39 +00:00
|
|
|
List<Address> newlyActiveAddresses = Stream.of(activeAddresses)
|
2017-11-20 22:48:39 +00:00
|
|
|
.filter(address -> !currentActiveAddresses.contains(address))
|
2017-12-07 19:53:17 +00:00
|
|
|
.filter(contactAddresses::contains)
|
2017-11-20 22:48:39 +00:00
|
|
|
.toList();
|
|
|
|
|
2017-11-26 18:45:39 +00:00
|
|
|
recipientDatabase.setRegistered(activeAddresses, inactiveAddresses);
|
|
|
|
updateContactsDatabase(context, activeAddresses, true);
|
2017-11-20 22:48:39 +00:00
|
|
|
|
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-12-07 19:53:17 +00:00
|
|
|
boolean systemContact = recipient.isSystemContact();
|
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-25 06:00:30 +00:00
|
|
|
if (Permissions.hasAll(context, Manifest.permission.WRITE_CONTACTS)) {
|
|
|
|
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-12-07 19:53:17 +00:00
|
|
|
if (!activeUser && systemContact) {
|
2017-11-20 22:48:39 +00:00
|
|
|
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-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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|