Repository: thundernest/k-9
Issue: Inconsistent recipient names when composing a message

### Checklist

- [X] I have used the search function to see if someone else has already submitted the same bug report.
- [X] I will describe the problem with as much detail as possible.

### App version

6.400

### Where did you get the app from?

F-Droid

### Android version

13

### Device model

_No response_

### Steps to reproduce

1. Have a contact with the nickname different from the name.
2. Create a new message in K9
3. Start typing in "To"
4. If you type the nick name and use the autocomplete/sugestion, the e-mail is in the form of `"Nickname" <email@example.com>`
5. If you type the name and use the autocomplete/sugestion, the e-mail is in the form of `"Firstname Lastname" <email@example.com>`

### Expected behavior

No matter how I find the contact, the recipient address is always in the same form `"Firstname Lastname" <email@example.com>`.

### Actual behavior

Depending on which field of the contact was matched the name changes.

### Logs

_Not relevant_

Changed files:
app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java
app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java
index e5fd41de2..e3931375f 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java
@@ -72,11 +72,9 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
 
     private static final String[] PROJECTION_NICKNAME = {
             ContactsContract.Data.CONTACT_ID,
-            ContactsContract.CommonDataKinds.Nickname.NAME
     };
 
     private static final int INDEX_CONTACT_ID_FOR_NICKNAME = 0;
-    private static final int INDEX_NICKNAME = 1;
 
     private static final String[] PROJECTION_CRYPTO_ADDRESSES = {
             "address",
@@ -377,8 +375,7 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
                 Cursor cursor = contentResolver
                         .query(queryUri, PROJECTION, selection, new String[] { id }, SORT_ORDER);
 
-                String contactNickname = nicknameCursor.getString(INDEX_NICKNAME);
-                fillContactDataFromCursor(cursor, recipients, recipientMap, contactNickname, null);
+                fillContactDataFromCursor(cursor, recipients, recipientMap, null);
 
                 hasContact = true;
             }
@@ -403,7 +400,7 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
             return recipients;
         }
 
-        fillContactDataFromCursor(cursor, recipients, new HashMap<>(), null, maxRecipients);
+        fillContactDataFromCursor(cursor, recipients, new HashMap<>(), maxRecipients);
 
         return recipients;
     }
@@ -437,15 +434,14 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
 
     private void fillContactDataFromCursor(Cursor cursor, List<Recipient> recipients,
             Map<String, Recipient> recipientMap) {
-        fillContactDataFromCursor(cursor, recipients, recipientMap, null, null);
+        fillContactDataFromCursor(cursor, recipients, recipientMap, null);
     }
 
     private void fillContactDataFromCursor(Cursor cursor, List<Recipient> recipients,
-            Map<String, Recipient> recipientMap, @Nullable String prefilledName, @Nullable Integer maxRecipients) {
+            Map<String, Recipient> recipientMap, @Nullable Integer maxRecipients) {
 
         while (cursor.moveToNext() && (maxRecipients == null || recipients.size() < maxRecipients)) {
-            String name = prefilledName != null ? prefilledName : cursor.getString(INDEX_NAME);
-
+            String name = cursor.getString(INDEX_NAME);
             String email = cursor.getString(INDEX_EMAIL);
 
             // already exists? just skip then
diff --git a/app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java b/app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java
index 6d9894552..c16e444c8 100644
--- a/app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java
+++ b/app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java
@@ -49,8 +49,7 @@ public class RecipientLoaderTest extends RobolectricTest {
             ContactsContract.Contacts.STARRED
     };
     static final String[] PROJECTION_NICKNAME = {
-            ContactsContract.Data.CONTACT_ID,
-            ContactsContract.CommonDataKinds.Nickname.NAME
+            ContactsContract.Data.CONTACT_ID
     };
     static final String[] PROJECTION_CRYPTO_ADDRESSES = { "address", "uid_address" };
     static final String[] PROJECTION_CRYPTO_STATUS = { "address", "uid_key_status", "autocrypt_key_status" };
@@ -66,7 +65,7 @@ public class RecipientLoaderTest extends RobolectricTest {
     static final String[] CONTACT_WITH_NICKNAME_NOT_CONTACTED =
             new String[] { "0", "Eve_notContacted", "eve_notContacted", "eve_notContacted@host.com", TYPE, null, "2",
                     null, "0", "Eve", "0" };
-    static final String[] NICKNAME_NOT_CONTACTED = new String[] { "2", "Eves_Nickname_Bob" };
+    static final String[] NICKNAME_NOT_CONTACTED = new String[] { "2" };
 
     static final String QUERYSTRING = "querystring";