diff --git a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java index ec85c1105..0fd471447 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java @@ -85,6 +85,22 @@ public class RecipientLoader extends AsyncTaskLoader> { private static final int CRYPTO_PROVIDER_STATUS_UNTRUSTED = 1; private static final int CRYPTO_PROVIDER_STATUS_TRUSTED = 2; + private static final Comparator RECIPIENT_COMPARATOR = new Comparator() { + @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> { foundValidCursor |= fillContactDataFromNameAndEmail(query, recipients, recipientMap); if (foundValidCursor) { - //nicknames should be sorted as the others - by timesContacted,keyPrimary - Collections.sort(recipients, new Comparator() { - @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> { 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> { 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> { 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()) { diff --git a/k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java b/k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java index c018307ab..57033b8eb 100644 --- a/k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java +++ b/k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java @@ -583,8 +583,8 @@ public class RecipientSelectView extends TokenCompleteTextView 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 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 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; - } } } diff --git a/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java b/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java index eb79ab234..5cef41381 100644 --- a/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java +++ b/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java @@ -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 ")[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 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()); } -} \ No newline at end of file +}