Get avatar for drafts

pull/146/head
M66B 6 years ago
parent cce4bd8c60
commit e074625834

@ -552,6 +552,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
draft.ui_hide = false; draft.ui_hide = false;
draft.ui_found = false; draft.ui_found = false;
draft.ui_ignored = false; draft.ui_ignored = false;
draft.getAvatar(context, true);
draft.id = db.message().insertMessage(draft); draft.id = db.message().insertMessage(draft);
draft.write(context, body); draft.write(context, body);
} }
@ -828,6 +829,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
draft.ui_hide = false; draft.ui_hide = false;
draft.ui_found = false; draft.ui_found = false;
draft.ui_ignored = false; draft.ui_ignored = false;
draft.getAvatar(context, true);
draft.id = db.message().insertMessage(draft); draft.id = db.message().insertMessage(draft);
draft.write(context, body); draft.write(context, body);

@ -19,8 +19,14 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B) Copyright 2018 by Marcel Bokhorst (M66B)
*/ */
import android.Manifest;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.text.Html; import android.text.Html;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -34,8 +40,10 @@ import java.util.Date;
import java.util.Random; import java.util.Random;
import javax.mail.Address; import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.ForeignKey; import androidx.room.ForeignKey;
import androidx.room.Index; import androidx.room.Index;
@ -182,6 +190,51 @@ public class EntityMessage implements Serializable {
} }
} }
void getAvatar(Context context, boolean outgoing) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED) {
try {
Address[] addresses = (outgoing ? this.to : this.from);
if (addresses != null)
for (int i = 0; i < addresses.length; i++) {
String email = ((InternetAddress) addresses[i]).getAddress();
Cursor cursor = null;
try {
ContentResolver resolver = context.getContentResolver();
cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME
},
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);
int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
long contactId = cursor.getLong(colContactId);
String lookupKey = cursor.getString(colLookupKey);
String displayName = cursor.getString(colDisplayName);
this.avatar = ContactsContract.Contacts.getLookupUri(contactId, lookupKey).toString();
if (!TextUtils.isEmpty(displayName))
((InternetAddress) addresses[i]).setPersonal(displayName);
}
} finally {
if (cursor != null)
cursor.close();
}
}
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
}
}
static String getQuote(Context context, long id) throws IOException { static String getQuote(Context context, long id) throws IOException {
EntityMessage message = DB.getInstance(context).message().getMessage(id); EntityMessage message = DB.getInstance(context).message().getMessage(id);
return String.format("<p>%s %s:</p><blockquote>%s</blockquote>", return String.format("<p>%s %s:</p><blockquote>%s</blockquote>",

@ -1211,6 +1211,7 @@ public class FragmentCompose extends FragmentEx {
result.draft.ui_hide = false; result.draft.ui_hide = false;
result.draft.ui_found = false; result.draft.ui_found = false;
result.draft.ui_ignored = false; result.draft.ui_ignored = false;
result.draft.getAvatar(context, true);
result.draft.id = db.message().insertMessage(result.draft); result.draft.id = db.message().insertMessage(result.draft);
result.draft.write(context, body == null ? "" : body); result.draft.write(context, body == null ? "" : body);

@ -67,6 +67,7 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.CipherInputStream; import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream; import javax.crypto.CipherOutputStream;
@ -761,7 +762,10 @@ public class FragmentSetup extends FragmentEx {
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getContext(), ex); if (ex.getCause() instanceof BadPaddingException)
Snackbar.make(view, R.string.title_setup_password_invalid, Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(getContext(), ex);
} }
}.load(this, args); }.load(this, args);
} }

@ -19,14 +19,12 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B) Copyright 2018 by Marcel Bokhorst (M66B)
*/ */
import android.Manifest;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Person; import android.app.Person;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
@ -1954,49 +1952,7 @@ public class ServiceSynchronize extends LifecycleService {
message.ui_hide = false; message.ui_hide = false;
message.ui_found = found; message.ui_found = found;
message.ui_ignored = false; message.ui_ignored = false;
message.getAvatar(context, folder.isOutgoing());
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED) {
try {
Address[] addresses = (folder.isOutgoing() ? message.to : message.from);
if (addresses != null)
for (int i = 0; i < addresses.length; i++) {
String email = ((InternetAddress) addresses[i]).getAddress();
Cursor cursor = null;
try {
ContentResolver resolver = context.getContentResolver();
cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME
},
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);
int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
long contactId = cursor.getLong(colContactId);
String lookupKey = cursor.getString(colLookupKey);
String displayName = cursor.getString(colDisplayName);
message.avatar = ContactsContract.Contacts.getLookupUri(contactId, lookupKey).toString();
if (!TextUtils.isEmpty(displayName))
((InternetAddress) addresses[i]).setPersonal(displayName);
}
} finally {
if (cursor != null)
cursor.close();
}
}
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
}
message.id = db.message().insertMessage(message); message.id = db.message().insertMessage(message);

@ -75,6 +75,7 @@
<string name="title_setup_password_repeat">Repeat password</string> <string name="title_setup_password_repeat">Repeat password</string>
<string name="title_setup_password_missing">Password missing</string> <string name="title_setup_password_missing">Password missing</string>
<string name="title_setup_password_different">Passwords different</string> <string name="title_setup_password_different">Passwords different</string>
<string name="title_setup_password_invalid">Password invalid</string>
<string name="title_setup_exported">Settings exported</string> <string name="title_setup_exported">Settings exported</string>
<string name="title_setup_imported">Settings imported</string> <string name="title_setup_imported">Settings imported</string>
<string name="title_setup_account">Manage accounts</string> <string name="title_setup_account">Manage accounts</string>

Loading…
Cancel
Save