From a4eec5d07cbb88b3eeaf4762a57960a65297a139 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 13 Sep 2018 13:59:09 +0000 Subject: [PATCH] Allow adding/changing contacts Fixes #103 --- .../eu/faircode/email/FragmentMessage.java | 63 +++++++++++++++++++ .../drawable/baseline_import_contacts_24.xml | 10 +++ app/src/main/res/layout/fragment_message.xml | 15 ++++- 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/drawable/baseline_import_contacts_24.xml diff --git a/app/src/main/java/eu/faircode/email/FragmentMessage.java b/app/src/main/java/eu/faircode/email/FragmentMessage.java index 3587542a..957378b5 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessage.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessage.java @@ -24,6 +24,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Typeface; @@ -79,6 +80,9 @@ import java.util.Date; import java.util.List; import java.util.Locale; +import javax.mail.Address; +import javax.mail.internet.InternetAddress; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; @@ -98,6 +102,7 @@ public class FragmentMessage extends FragmentEx { private ImageView ivFlagged; private ImageView ivAvatar; private TextView tvFrom; + private ImageView ivContactAdd; private TextView tvTime; private TextView tvCount; private TextView tvTo; @@ -158,6 +163,7 @@ public class FragmentMessage extends FragmentEx { ivFlagged = view.findViewById(R.id.ivFlagged); ivAvatar = view.findViewById(R.id.ivAvatar); tvFrom = view.findViewById(R.id.tvFrom); + ivContactAdd = view.findViewById(R.id.ivContactAdd); tvTime = view.findViewById(R.id.tvTime); tvCount = view.findViewById(R.id.tvCount); tvTo = view.findViewById(R.id.tvTo); @@ -213,6 +219,56 @@ public class FragmentMessage extends FragmentEx { } }); + ivContactAdd.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + for (Address address : message.from) { + InternetAddress ia = (InternetAddress) address; + String name = ia.getPersonal(); + String email = ia.getAddress(); + + // https://developer.android.com/training/contacts-provider/modify-data + Intent edit = new Intent(); + if (!TextUtils.isEmpty(name)) + edit.putExtra(ContactsContract.Intents.Insert.NAME, name); + if (!TextUtils.isEmpty(email)) + edit.putExtra(ContactsContract.Intents.Insert.EMAIL, email); + + Cursor cursor = null; + try { + ContentResolver resolver = getContext().getContentResolver(); + cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, + new String[]{ + ContactsContract.CommonDataKinds.Photo.CONTACT_ID, + ContactsContract.Contacts.LOOKUP_KEY + }, + ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?", + new String[]{email}, null); + if (cursor.moveToNext()) { + int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID); + int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY); + + long contactId = cursor.getLong(colContactId); + String lookupKey = cursor.getString(colLookupKey); + + Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey); + + edit.setAction(Intent.ACTION_EDIT); + edit.setDataAndType(lookupUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE); + } else { + edit.setAction(Intent.ACTION_INSERT); + edit.setType(ContactsContract.Contacts.CONTENT_TYPE); + } + } finally { + if (cursor != null) + cursor.close(); + } + + startActivity(edit); + } + } + }); + tvBody.setMovementMethod(new LinkMovementMethod() { public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { if (event.getAction() != MotionEvent.ACTION_UP) @@ -446,6 +502,13 @@ public class FragmentMessage extends FragmentEx { ivAvatar.setImageDrawable(Drawable.createFromStream(is, "avatar")); } + if (message.from == null) { + ViewGroup.LayoutParams lp = ivContactAdd.getLayoutParams(); + lp.height = 0; + lp.width = 0; + ivContactAdd.setLayoutParams(lp); + } + pbWait.setVisibility(View.GONE); grpHeader.setVisibility(free ? View.GONE : View.VISIBLE); diff --git a/app/src/main/res/drawable/baseline_import_contacts_24.xml b/app/src/main/res/drawable/baseline_import_contacts_24.xml new file mode 100644 index 00000000..44dc310f --- /dev/null +++ b/app/src/main/res/drawable/baseline_import_contacts_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/fragment_message.xml b/app/src/main/res/layout/fragment_message.xml index 41f844f2..33ab0f9f 100644 --- a/app/src/main/res/layout/fragment_message.xml +++ b/app/src/main/res/layout/fragment_message.xml @@ -46,10 +46,21 @@ android:text="From" android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textIsSelectable="true" - app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintEnd_toStartOf="@+id/ivContactAdd" app:layout_constraintStart_toEndOf="@id/ivAvatar" app:layout_constraintTop_toTopOf="parent" /> + + + app:constraint_referenced_ids="ivFlagged,ivAvatar,tvFrom,ivContactAdd,tvToTitle,tvTo,tvSize,tvTime,tvSubject" />