1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-25 01:06:04 +00:00

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

View file

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M21,5c-1.11,-0.35 -2.33,-0.5 -3.5,-0.5 -1.95,0 -4.05,0.4 -5.5,1.5 -1.45,-1.1 -3.55,-1.5 -5.5,-1.5S2.45,4.9 1,6v14.65c0,0.25 0.25,0.5 0.5,0.5 0.1,0 0.15,-0.05 0.25,-0.05C3.1,20.45 5.05,20 6.5,20c1.95,0 4.05,0.4 5.5,1.5 1.35,-0.85 3.8,-1.5 5.5,-1.5 1.65,0 3.35,0.3 4.75,1.05 0.1,0.05 0.15,0.05 0.25,0.05 0.25,0 0.5,-0.25 0.5,-0.5L23,6c-0.6,-0.45 -1.25,-0.75 -2,-1zM21,18.5c-1.1,-0.35 -2.3,-0.5 -3.5,-0.5 -1.7,0 -4.15,0.65 -5.5,1.5L12,8c1.35,-0.85 3.8,-1.5 5.5,-1.5 1.2,0 2.4,0.15 3.5,0.5v11.5z"/>
</vector>

View file

@ -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" />
<ImageView
android:id="@+id/ivContactAdd"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
android:src="@drawable/baseline_import_contacts_24"
app:layout_constraintBottom_toBottomOf="@id/spFrom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tvFrom" />
<TextView
android:id="@+id/tvTime"
android:layout_width="0dp"
@ -390,7 +401,7 @@
android:id="@+id/grpHeader"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="ivFlagged,ivAvatar,tvFrom,tvToTitle,tvTo,tvSize,tvTime,tvSubject" />
app:constraint_referenced_ids="ivFlagged,ivAvatar,tvFrom,ivContactAdd,tvToTitle,tvTo,tvSize,tvTime,tvSubject" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpThread"