Allow adding/changing contacts

Fixes #103
This commit is contained in:
M66B
2018-09-13 13:59:09 +00:00
parent 9a6e56cf16
commit a4eec5d07c
3 changed files with 86 additions and 2 deletions

View File

@@ -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);