Small code cleanup
This commit is contained in:
parent
9abc815d8b
commit
335ebe85db
3 changed files with 43 additions and 56 deletions
|
@ -85,6 +85,22 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
|
|||
private static final int CRYPTO_PROVIDER_STATUS_UNTRUSTED = 1;
|
||||
private static final int CRYPTO_PROVIDER_STATUS_TRUSTED = 2;
|
||||
|
||||
private static final Comparator<Recipient> RECIPIENT_COMPARATOR = new Comparator<Recipient>() {
|
||||
@Override
|
||||
public int compare(Recipient lhs, Recipient rhs) {
|
||||
int timesContactedDiff = rhs.timesContacted - lhs.timesContacted;
|
||||
if (timesContactedDiff != 0) {
|
||||
return timesContactedDiff;
|
||||
}
|
||||
|
||||
if (lhs.sortKey == null || rhs.sortKey == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return lhs.sortKey.compareTo(rhs.sortKey);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
private final String query;
|
||||
private final Address[] addresses;
|
||||
|
@ -266,22 +282,7 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
|
|||
foundValidCursor |= fillContactDataFromNameAndEmail(query, recipients, recipientMap);
|
||||
|
||||
if (foundValidCursor) {
|
||||
//nicknames should be sorted as the others - by timesContacted,keyPrimary
|
||||
Collections.sort(recipients, new Comparator<Recipient>() {
|
||||
@Override
|
||||
public int compare(Recipient o1, Recipient o2) {
|
||||
|
||||
int x = o2.getTimesContacted();
|
||||
int y = o1.getTimesContacted();
|
||||
|
||||
int compTimesContacted = (x < y) ? -1 : ((x == y) ? 0 : 1);
|
||||
if (compTimesContacted != 0) {
|
||||
return compTimesContacted;
|
||||
}
|
||||
return o2.getKeyPrimary().compareTo(o1.getKeyPrimary());
|
||||
}
|
||||
});
|
||||
|
||||
Collections.sort(recipients, RECIPIENT_COMPARATOR);
|
||||
registerContentObserver();
|
||||
}
|
||||
|
||||
|
@ -361,8 +362,6 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
|
|||
String name = prefilledName != null ? prefilledName : cursor.getString(INDEX_NAME);
|
||||
|
||||
String email = cursor.getString(INDEX_EMAIL);
|
||||
long contactId = cursor.getLong(INDEX_CONTACT_ID);
|
||||
String lookupKey = cursor.getString(INDEX_LOOKUP_KEY);
|
||||
|
||||
// already exists? just skip then
|
||||
if (email == null || recipientMap.containsKey(email)) {
|
||||
|
@ -370,6 +369,11 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
|
|||
continue;
|
||||
}
|
||||
|
||||
long contactId = cursor.getLong(INDEX_CONTACT_ID);
|
||||
String lookupKey = cursor.getString(INDEX_LOOKUP_KEY);
|
||||
int timesContacted = cursor.getInt(INDEX_TIMES_CONTACTED);
|
||||
String sortKey = cursor.getString(INDEX_KEY_PRIMARY);
|
||||
|
||||
int addressType = cursor.getInt(INDEX_EMAIL_TYPE);
|
||||
String addressLabel = null;
|
||||
switch (addressType) {
|
||||
|
@ -395,10 +399,9 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
|
|||
break;
|
||||
}
|
||||
}
|
||||
Recipient recipient = new Recipient(name, email, addressLabel, contactId, lookupKey);
|
||||
recipient.setTimesContacted(cursor.getInt(INDEX_TIMES_CONTACTED));
|
||||
recipient.setKeyPrimary(cursor.getString(INDEX_KEY_PRIMARY));
|
||||
|
||||
Recipient recipient = new Recipient(name, email, addressLabel, contactId, lookupKey,
|
||||
timesContacted, sortKey);
|
||||
|
||||
if (recipient.isValidEmailAddress()) {
|
||||
|
||||
|
|
|
@ -583,8 +583,8 @@ public class RecipientSelectView extends TokenCompleteTextView<Recipient> implem
|
|||
public Address address;
|
||||
|
||||
public String addressLabel;
|
||||
private int timesContacted;
|
||||
private String keyPrimary;
|
||||
public final int timesContacted;
|
||||
public final String sortKey;
|
||||
|
||||
@Nullable // null if the contact has no photo. transient because we serialize this manually, see below.
|
||||
public transient Uri photoThumbnailUri;
|
||||
|
@ -597,14 +597,23 @@ public class RecipientSelectView extends TokenCompleteTextView<Recipient> implem
|
|||
this.contactId = null;
|
||||
this.cryptoStatus = RecipientCryptoStatus.UNDEFINED;
|
||||
this.contactLookupKey = null;
|
||||
timesContacted = 0;
|
||||
sortKey = null;
|
||||
}
|
||||
|
||||
public Recipient(String name, String email, String addressLabel, long contactId, String lookupKey) {
|
||||
this(name, email, addressLabel, contactId, lookupKey, 0, null);
|
||||
}
|
||||
|
||||
public Recipient(String name, String email, String addressLabel, long contactId, String lookupKey,
|
||||
int timesContacted, String sortKey) {
|
||||
this.address = new Address(email, name);
|
||||
this.contactId = contactId;
|
||||
this.addressLabel = addressLabel;
|
||||
this.cryptoStatus = RecipientCryptoStatus.UNDEFINED;
|
||||
this.contactLookupKey = lookupKey;
|
||||
this.timesContacted = timesContacted;
|
||||
this.sortKey = sortKey;
|
||||
}
|
||||
|
||||
public String getDisplayNameOrAddress() {
|
||||
|
@ -697,21 +706,5 @@ public class RecipientSelectView extends TokenCompleteTextView<Recipient> implem
|
|||
photoThumbnailUri = Uri.parse(uriString);
|
||||
}
|
||||
}
|
||||
|
||||
public void setTimesContacted(int timesContacted) {
|
||||
this.timesContacted = timesContacted;
|
||||
}
|
||||
|
||||
public int getTimesContacted() {
|
||||
return timesContacted;
|
||||
}
|
||||
|
||||
public void setKeyPrimary(String keyPrimary) {
|
||||
this.keyPrimary = keyPrimary;
|
||||
}
|
||||
|
||||
public String getKeyPrimary() {
|
||||
return keyPrimary;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,10 @@ public class RecipientLoaderTest {
|
|||
ContactsContract.CommonDataKinds.Email.TIMES_CONTACTED,
|
||||
ContactsContract.Contacts.SORT_KEY_PRIMARY
|
||||
};
|
||||
static final String[] PROJECTION_NICKNAME = {
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
ContactsContract.CommonDataKinds.Nickname.NAME
|
||||
};
|
||||
static final String[] PROJECTION_CRYPTO_ADDRESSES = { "address", "uid_address" };
|
||||
static final String[] PROJECTION_CRYPTO_STATUS = { "address", "uid_key_status", "autocrypt_key_status" };
|
||||
static final Address CONTACT_ADDRESS_1 = Address.parse("Contact Name <address@example.org>")[0];
|
||||
|
@ -61,12 +65,6 @@ public class RecipientLoaderTest {
|
|||
static final String QUERYSTRING = "querystring";
|
||||
|
||||
|
||||
private static final String[] PROJECTION_NICKNAME = {
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
ContactsContract.CommonDataKinds.Nickname.NAME
|
||||
};
|
||||
|
||||
|
||||
Context context;
|
||||
ContentResolver contentResolver;
|
||||
|
||||
|
@ -188,7 +186,7 @@ public class RecipientLoaderTest {
|
|||
any(String.class))).thenReturn(cursor);
|
||||
}
|
||||
|
||||
private void setupNicknameContactProvider(String queriedAddress, String[]... contactsWithNickname) {
|
||||
private void setupNicknameContactProvider(String[]... contactsWithNickname) {
|
||||
MatrixCursor cursor = new MatrixCursor(PROJECTION_NICKNAME);
|
||||
for (String[] contact : contactsWithNickname) {
|
||||
cursor.addRow(contact);
|
||||
|
@ -199,11 +197,8 @@ public class RecipientLoaderTest {
|
|||
any(String.class),
|
||||
any(String[].class),
|
||||
any(String.class))).thenReturn(cursor);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void setupContactProviderForId(String id, String[]... contacts) {
|
||||
MatrixCursor cursor = new MatrixCursor(PROJECTION);
|
||||
for (String[] contact : contacts) {
|
||||
|
@ -239,15 +234,11 @@ public class RecipientLoaderTest {
|
|||
assertEquals(0, recipients.size());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Nickname should be sorted as querying others (more times contacted first)
|
||||
*/
|
||||
@Test
|
||||
public void queryContactProvider_sortByContactedForNickname() throws Exception {
|
||||
public void queryContactProvider_sortByTimesContactedForNickname() throws Exception {
|
||||
RecipientLoader recipientLoader = new RecipientLoader(context, null, QUERYSTRING);
|
||||
setupContactProvider("%" + QUERYSTRING + "%", CONTACT_1);
|
||||
setupNicknameContactProvider("%" + QUERYSTRING + "%", NICKNAME_NOT_CONTACTED);
|
||||
setupNicknameContactProvider(NICKNAME_NOT_CONTACTED);
|
||||
setupContactProviderForId(NICKNAME_NOT_CONTACTED[0], CONTACT_WITH_NICKNAME_NOT_CONTACTED);
|
||||
|
||||
List<Recipient> recipients = recipientLoader.loadInBackground();
|
||||
|
@ -256,4 +247,4 @@ public class RecipientLoaderTest {
|
|||
assertEquals("bob@host.com", recipients.get(0).address.getAddress());
|
||||
assertEquals("eve_notContacted@host.com", recipients.get(1).address.getAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue