From 3af200791c8bf683966a5d8786857ccaa7d3390d Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 26 Feb 2020 10:09:33 +0100 Subject: [PATCH] Show contact photos in contact selector --- .../faircode/email/AdapterIdentitySelect.java | 4 +- .../java/eu/faircode/email/DaoContact.java | 2 +- .../eu/faircode/email/FragmentCompose.java | 41 +++++++++++++---- app/src/main/res/layout/spinner_contact.xml | 46 +++++++++++++++++++ ...spinner_item2.xml => spinner_identity.xml} | 0 ...down.xml => spinner_identity_dropdown.xml} | 0 6 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 app/src/main/res/layout/spinner_contact.xml rename app/src/main/res/layout/{spinner_item2.xml => spinner_identity.xml} (100%) rename app/src/main/res/layout/{spinner_item2_dropdown.xml => spinner_identity_dropdown.xml} (100%) diff --git a/app/src/main/java/eu/faircode/email/AdapterIdentitySelect.java b/app/src/main/java/eu/faircode/email/AdapterIdentitySelect.java index e7b9ce8a2e..72f18f5aea 100644 --- a/app/src/main/java/eu/faircode/email/AdapterIdentitySelect.java +++ b/app/src/main/java/eu/faircode/email/AdapterIdentitySelect.java @@ -26,12 +26,12 @@ public class AdapterIdentitySelect extends ArrayAdapter { @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { - return getLayout(position, convertView, parent, R.layout.spinner_item2); + return getLayout(position, convertView, parent, R.layout.spinner_identity); } @Override public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) { - return getLayout(position, convertView, parent, R.layout.spinner_item2_dropdown); + return getLayout(position, convertView, parent, R.layout.spinner_identity_dropdown); } private View getLayout(int position, View convertView, ViewGroup parent, int resid) { diff --git a/app/src/main/java/eu/faircode/email/DaoContact.java b/app/src/main/java/eu/faircode/email/DaoContact.java index f8177ecf0c..72db9b1b7e 100644 --- a/app/src/main/java/eu/faircode/email/DaoContact.java +++ b/app/src/main/java/eu/faircode/email/DaoContact.java @@ -58,7 +58,7 @@ public interface DaoContact { " AND email = :email COLLATE NOCASE") EntityContact getContact(long account, int type, String email); - @Query("SELECT id AS _id, name, email, name || ' *' AS display" + + @Query("SELECT id AS _id, name, email, NULL AS photo, 1 AS local" + " FROM contact" + " WHERE (:account IS NULL OR account = :account)" + " AND (:type IS NULL OR type = :type)" + diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index b2d084be22..811cb443fc 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -81,6 +81,7 @@ import android.widget.CompoundButton; import android.widget.EditText; import android.widget.FilterQueryProvider; import android.widget.ImageButton; +import android.widget.ImageView; import android.widget.MultiAutoCompleteTextView; import android.widget.Spinner; import android.widget.TextView; @@ -670,12 +671,32 @@ public class FragmentCompose extends FragmentBase { SimpleCursorAdapter cadapter = new SimpleCursorAdapter( getContext(), - R.layout.spinner_item2_dropdown, + R.layout.spinner_contact, null, - new String[]{"display", "email"}, - new int[]{android.R.id.text1, android.R.id.text2}, + new String[]{"name", "email", "photo"}, + new int[]{R.id.tvName, R.id.tvEmail, R.id.ivPhoto}, 0); + cadapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { + @Override + public boolean setViewValue(View view, Cursor cursor, int columnIndex) { + if (view.getId() == R.id.ivPhoto) { + ImageView photo = (ImageView) view; + if (cursor.getInt(cursor.getColumnIndex("local")) == 1) + photo.setImageDrawable(null); + else { + String uri = cursor.getString(columnIndex); + if (uri == null) + photo.setImageResource(R.drawable.baseline_person_24); + else + photo.setImageURI(Uri.parse(uri)); + } + return true; + } + return false; + } + }); + cadapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { public CharSequence convertToString(Cursor cursor) { int colName = cursor.getColumnIndex("name"); @@ -705,7 +726,7 @@ public class FragmentCompose extends FragmentBase { String wildcard = "%" + typed + "%"; List cursors = new ArrayList<>(); - MatrixCursor provided = new MatrixCursor(new String[]{"_id", "name", "email", "display"}); + MatrixCursor provided = new MatrixCursor(new String[]{"_id", "name", "email", "photo", "local"}); boolean contacts = Helper.hasPermission(getContext(), Manifest.permission.READ_CONTACTS); if (contacts) { Cursor cursor = resolver.query( @@ -713,7 +734,8 @@ public class FragmentCompose extends FragmentBase { new String[]{ ContactsContract.CommonDataKinds.Email.CONTACT_ID, ContactsContract.Contacts.DISPLAY_NAME, - ContactsContract.CommonDataKinds.Email.DATA + ContactsContract.CommonDataKinds.Email.DATA, + ContactsContract.Contacts.PHOTO_THUMBNAIL_URI }, ContactsContract.CommonDataKinds.Email.DATA + " <> ''" + " AND (" + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?" + @@ -725,10 +747,11 @@ public class FragmentCompose extends FragmentBase { while (cursor != null && cursor.moveToNext()) provided.newRow() - .add(cursor.getLong(0)) - .add(cursor.getString(1)) - .add(cursor.getString(2)) - .add(cursor.getString(1)); + .add(cursor.getLong(0)) // id + .add(cursor.getString(1)) // name + .add(cursor.getString(2)) // email + .add(cursor.getString(3)) // photo + .add(0); // local } cursors.add(provided); diff --git a/app/src/main/res/layout/spinner_contact.xml b/app/src/main/res/layout/spinner_contact.xml new file mode 100644 index 0000000000..c4f7254687 --- /dev/null +++ b/app/src/main/res/layout/spinner_contact.xml @@ -0,0 +1,46 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/spinner_item2.xml b/app/src/main/res/layout/spinner_identity.xml similarity index 100% rename from app/src/main/res/layout/spinner_item2.xml rename to app/src/main/res/layout/spinner_identity.xml diff --git a/app/src/main/res/layout/spinner_item2_dropdown.xml b/app/src/main/res/layout/spinner_identity_dropdown.xml similarity index 100% rename from app/src/main/res/layout/spinner_item2_dropdown.xml rename to app/src/main/res/layout/spinner_identity_dropdown.xml