Chip generated avatars

pull/194/merge
M66B 3 years ago
parent 239d8f8868
commit 4480e2ba65

@ -22,8 +22,12 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.provider.ContactsContract; import android.provider.ContactsContract;
@ -194,8 +198,12 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
public void run() { public void run() {
try { try {
final Context context = getContext(); final Context context = getContext();
final Resources res = getResources();
final Editable edit = getText(); final Editable edit = getText();
final int dp24 = Helper.dp2pixels(context, 24);
final boolean send_chips = prefs.getBoolean("send_chips", true); final boolean send_chips = prefs.getBoolean("send_chips", true);
boolean generated = prefs.getBoolean("generated_icons", true);
boolean identicons = prefs.getBoolean("identicons", false);
final boolean focus = hasFocus(); final boolean focus = hasFocus();
final int selStart = getSelectionStart(); final int selStart = getSelectionStart();
@ -252,18 +260,27 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
else if (kar != ',') else if (kar != ',')
edit.insert(++i, ","); edit.insert(++i, ",");
Drawable avatar = null; String e = parsed[0].getAddress();
String p = parsed[0].getPersonal();
Bitmap bm = null;
Uri lookupUri = ContactInfo.getLookupUri(parsed); Uri lookupUri = ContactInfo.getLookupUri(parsed);
if (lookupUri != null) if (lookupUri != null)
try (InputStream is = ContactsContract.Contacts.openContactPhotoInputStream( try (InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
context.getContentResolver(), lookupUri, false)) { context.getContentResolver(), lookupUri, false)) {
avatar = Drawable.createFromStream(is, email); bm = BitmapFactory.decodeStream(is);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }
String e = parsed[0].getAddress(); if (bm == null && generated)
String p = parsed[0].getPersonal(); if (identicons)
bm = ImageHelper.generateIdenticon(e, dp24, 5, context);
else
bm = ImageHelper.generateLetterIcon(e, p, dp24, context);
Drawable avatar = (bm == null ? null : new BitmapDrawable(res, bm));
String text = (TextUtils.isEmpty(p) ? e : p); String text = (TextUtils.isEmpty(p) ? e : p);
// https://github.com/material-components/material-components-android/blob/master/docs/components/Chip.md // https://github.com/material-components/material-components-android/blob/master/docs/components/Chip.md

Loading…
Cancel
Save