Migrate contact interactions to SystemContactsRepository.

This commit is contained in:
Greyson Parrelli
2022-03-23 09:50:47 -04:00
parent db309b7930
commit c2627dda8d
8 changed files with 597 additions and 662 deletions

View File

@@ -77,9 +77,7 @@ public class ContactShareEditActivity extends PassphraseRequiredActivity impleme
ContactShareEditAdapter contactAdapter = new ContactShareEditAdapter(GlideApp.with(this), dynamicLanguage.getCurrentLocale(), this);
contactList.setAdapter(contactAdapter);
SharedContactRepository contactRepository = new SharedContactRepository(this,
AsyncTask.THREAD_POOL_EXECUTOR,
SignalDatabase.contacts());
SharedContactRepository contactRepository = new SharedContactRepository(this, AsyncTask.THREAD_POOL_EXECUTOR);
viewModel = ViewModelProviders.of(this, new Factory(contactUris, contactRepository)).get(ContactShareEditViewModel.class);
viewModel.getContacts().observe(this, contacts -> {

View File

@@ -113,7 +113,7 @@ public final class ContactUtil {
}
}
public static @NonNull String getNormalizedPhoneNumber(@NonNull Context context, @NonNull String number) {
public static @NonNull String getNormalizedPhoneNumber(@NonNull Context context, @Nullable String number) {
return PhoneNumberFormatter.get(context).format(number);
}

View File

@@ -1,7 +1,6 @@
package org.thoughtcrime.securesms.contactshare;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
@@ -11,8 +10,10 @@ import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.contacts.ContactsDatabase;
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
import org.thoughtcrime.securesms.contacts.sync.SystemContactsRepository;
import org.thoughtcrime.securesms.contacts.sync.SystemContactsRepository.NameDetails;
import org.thoughtcrime.securesms.contacts.sync.SystemContactsRepository.PhoneDetails;
import org.thoughtcrime.securesms.contactshare.Contact.Email;
import org.thoughtcrime.securesms.contactshare.Contact.Name;
import org.thoughtcrime.securesms.contactshare.Contact.Phone;
@@ -26,10 +27,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import ezvcard.Ezvcard;
import ezvcard.VCard;
@@ -40,17 +42,12 @@ public class SharedContactRepository {
private static final String TAG = Log.tag(SharedContactRepository.class);
private final Context context;
private final Executor executor;
private final ContactsDatabase contactsDatabase;
private final Context context;
private final Executor executor;
SharedContactRepository(@NonNull Context context,
@NonNull Executor executor,
@NonNull ContactsDatabase contactsDatabase)
{
this.context = context.getApplicationContext();
this.executor = executor;
this.contactsDatabase = contactsDatabase;
SharedContactRepository(@NonNull Context context, @NonNull Executor executor) {
this.context = context.getApplicationContext();
this.executor = executor;
}
void getContacts(@NonNull List<Uri> contactUris, @NonNull ValueCallback<List<Contact>> callback) {
@@ -108,23 +105,16 @@ public class SharedContactRepository {
@WorkerThread
private @Nullable Name getName(long contactId) {
try (Cursor cursor = contactsDatabase.getNameDetails(contactId)) {
if (cursor != null && cursor.moveToFirst()) {
String cursorDisplayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
String cursorGivenName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String cursorFamilyName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String cursorPrefix = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
String cursorSuffix = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));
String cursorMiddleName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
NameDetails nameDetails = SystemContactsRepository.getNameDetails(context, contactId);
Name name = new Name(cursorDisplayName, cursorGivenName, cursorFamilyName, cursorPrefix, cursorSuffix, cursorMiddleName);
if (!name.isEmpty()) {
return name;
}
if (nameDetails != null) {
Name name = new Name(nameDetails.getDisplayName(), nameDetails.getGivenName(), nameDetails.getFamilyName(), nameDetails.getPrefix(), nameDetails.getSuffix(), nameDetails.getMiddleName());
if (!name.isEmpty()) {
return name;
}
}
String org = contactsDatabase.getOrganizationName(contactId);
String org = SystemContactsRepository.getOrganizationName(context, contactId);
if (!TextUtils.isEmpty(org)) {
return new Name(org, org, null, null, null, null);
}
@@ -134,20 +124,16 @@ public class SharedContactRepository {
@WorkerThread
private @NonNull List<Phone> getPhoneNumbers(long contactId) {
Map<String, Phone> numberMap = new HashMap<>();
try (Cursor cursor = contactsDatabase.getPhoneDetails(contactId)) {
while (cursor != null && cursor.moveToNext()) {
String cursorNumber = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
int cursorType = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));
String cursorLabel = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.LABEL));
Map<String, Phone> numberMap = new HashMap<>();
List<PhoneDetails> phoneDetails = SystemContactsRepository.getPhoneDetails(context, contactId);
String number = ContactUtil.getNormalizedPhoneNumber(context, cursorNumber);
Phone existing = numberMap.get(number);
Phone candidate = new Phone(number, VCardUtil.phoneTypeFromContactType(cursorType), cursorLabel);
for (PhoneDetails phone : phoneDetails) {
String number = ContactUtil.getNormalizedPhoneNumber(context, phone.getNumber());
Phone existing = numberMap.get(number);
Phone candidate = new Phone(number, VCardUtil.phoneTypeFromContactType(phone.getType()), phone.getLabel());
if (existing == null || (existing.getType() == Phone.Type.CUSTOM && existing.getLabel() == null)) {
numberMap.put(number, candidate);
}
if (existing == null || (existing.getType() == Phone.Type.CUSTOM && existing.getLabel() == null)) {
numberMap.put(number, candidate);
}
}
@@ -158,50 +144,31 @@ public class SharedContactRepository {
@WorkerThread
private @NonNull List<Email> getEmails(long contactId) {
List<Email> emails = new LinkedList<>();
try (Cursor cursor = contactsDatabase.getEmailDetails(contactId)) {
while (cursor != null && cursor.moveToNext()) {
String cursorEmail = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.ADDRESS));
int cursorType = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.TYPE));
String cursorLabel = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.LABEL));
emails.add(new Email(cursorEmail, VCardUtil.emailTypeFromContactType(cursorType), cursorLabel));
}
}
return emails;
return SystemContactsRepository.getEmailDetails(context, contactId)
.stream()
.filter(Objects::nonNull)
.map(email -> new Email(Objects.requireNonNull(email.getAddress()),
VCardUtil.emailTypeFromContactType(email.getType()),
email.getLabel()))
.collect(Collectors.toList());
}
@WorkerThread
private @NonNull List<PostalAddress> getPostalAddresses(long contactId) {
List<PostalAddress> postalAddresses = new LinkedList<>();
try (Cursor cursor = contactsDatabase.getPostalAddressDetails(contactId)) {
while (cursor != null && cursor.moveToNext()) {
int cursorType = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
String cursorLabel = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.LABEL));
String cursorStreet = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String cursorPoBox = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
String cursorNeighborhood = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD));
String cursorCity = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String cursorRegion = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String cursorPostal = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String cursorCountry = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
postalAddresses.add(new PostalAddress(VCardUtil.postalAddressTypeFromContactType(cursorType),
cursorLabel,
cursorStreet,
cursorPoBox,
cursorNeighborhood,
cursorCity,
cursorRegion,
cursorPostal,
cursorCountry));
}
}
return postalAddresses;
return SystemContactsRepository.getPostalAddressDetails(context, contactId)
.stream()
.map(address -> {
return new PostalAddress(VCardUtil.postalAddressTypeFromContactType(address.getType()),
address.getLabel(),
address.getStreet(),
address.getPoBox(),
address.getNeighborhood(),
address.getCity(),
address.getRegion(),
address.getPostal(),
address.getCountry());
})
.collect(Collectors.toList());
}
@WorkerThread
@@ -223,7 +190,7 @@ public class SharedContactRepository {
@WorkerThread
private @Nullable AvatarInfo getSystemAvatarInfo(long contactId) {
Uri uri = contactsDatabase.getAvatarUri(contactId);
Uri uri = SystemContactsRepository.getAvatarUri(context, contactId);
if (uri != null) {
return new AvatarInfo(uri, false);
}