diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index 79c0c03d8a..fd5d46d42a 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -552,6 +552,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB draft.ui_hide = false; draft.ui_found = false; draft.ui_ignored = false; + draft.getAvatar(context, true); draft.id = db.message().insertMessage(draft); draft.write(context, body); } @@ -828,6 +829,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB draft.ui_hide = false; draft.ui_found = false; draft.ui_ignored = false; + draft.getAvatar(context, true); draft.id = db.message().insertMessage(draft); draft.write(context, body); diff --git a/app/src/main/java/eu/faircode/email/EntityMessage.java b/app/src/main/java/eu/faircode/email/EntityMessage.java index 0e56c0456e..96d7f3fdd2 100644 --- a/app/src/main/java/eu/faircode/email/EntityMessage.java +++ b/app/src/main/java/eu/faircode/email/EntityMessage.java @@ -19,8 +19,14 @@ package eu.faircode.email; Copyright 2018 by Marcel Bokhorst (M66B) */ +import android.Manifest; +import android.content.ContentResolver; import android.content.Context; +import android.content.pm.PackageManager; +import android.database.Cursor; +import android.provider.ContactsContract; import android.text.Html; +import android.text.TextUtils; import android.util.Log; import java.io.BufferedReader; @@ -34,8 +40,10 @@ import java.util.Date; import java.util.Random; import javax.mail.Address; +import javax.mail.internet.InternetAddress; import androidx.annotation.NonNull; +import androidx.core.content.ContextCompat; import androidx.room.Entity; import androidx.room.ForeignKey; 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 { EntityMessage message = DB.getInstance(context).message().getMessage(id); return String.format("
%s %s:
%s", diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 3d27bbfcce..888444b008 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1211,6 +1211,7 @@ public class FragmentCompose extends FragmentEx { result.draft.ui_hide = false; result.draft.ui_found = false; result.draft.ui_ignored = false; + result.draft.getAvatar(context, true); result.draft.id = db.message().insertMessage(result.draft); result.draft.write(context, body == null ? "" : body); diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index 9836daf2b0..ba99d7c7e8 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -67,6 +67,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; @@ -761,7 +762,10 @@ public class FragmentSetup extends FragmentEx { @Override 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); } diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index dd119ef097..95b873ca13 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -19,14 +19,12 @@ package eu.faircode.email; Copyright 2018 by Marcel Bokhorst (M66B) */ -import android.Manifest; import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Person; import android.content.BroadcastReceiver; -import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -1954,49 +1952,7 @@ public class ServiceSynchronize extends LifecycleService { message.ui_hide = false; message.ui_found = found; message.ui_ignored = false; - - 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.getAvatar(context, folder.isOutgoing()); message.id = db.message().insertMessage(message); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3e73899b90..fe5c43c391 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -75,6 +75,7 @@