mirror of https://github.com/M66B/FairEmail.git
Generated icons improvements
This commit is contained in:
parent
d6d9c0adb4
commit
a7cd5e7721
|
@ -166,9 +166,11 @@ public class ContactInfo {
|
|||
boolean identicons = prefs.getBoolean("identicons", false);
|
||||
if (identicons) {
|
||||
identicon = true;
|
||||
info.bitmap = ImageHelper.generateIdenticon(key, dp, 5, context);
|
||||
info.bitmap = ImageHelper.generateIdenticon(
|
||||
address.getAddress(), dp, 5, context);
|
||||
} else
|
||||
info.bitmap = ImageHelper.generateLetterIcon(key, dp, context);
|
||||
info.bitmap = ImageHelper.generateLetterIcon(
|
||||
address.getAddress(), address.getPersonal(), dp, context);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -182,7 +184,6 @@ public class ContactInfo {
|
|||
if (!info.known) {
|
||||
DB db = DB.getInstance(context);
|
||||
EntityContact contact = db.contact().getContact(account, EntityContact.TYPE_TO, info.email);
|
||||
|
||||
info.known = (contact != null);
|
||||
}
|
||||
|
||||
|
|
|
@ -215,6 +215,8 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
|
|||
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
||||
prefs.edit().putBoolean("generated_icons", checked).apply();
|
||||
swIdenticons.setEnabled(checked);
|
||||
sbSaturation.setEnabled(swGeneratedIcons.isChecked());
|
||||
sbBrightness.setEnabled(swGeneratedIcons.isChecked());
|
||||
ContactInfo.clearCache();
|
||||
}
|
||||
});
|
||||
|
@ -481,7 +483,9 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
|
|||
swIdenticons.setEnabled(swGeneratedIcons.isChecked());
|
||||
swCircular.setChecked(prefs.getBoolean("circular", true));
|
||||
sbSaturation.setProgress(prefs.getInt("saturation", 100));
|
||||
sbSaturation.setEnabled(swGeneratedIcons.isChecked());
|
||||
sbBrightness.setProgress(prefs.getInt("brightness", 100));
|
||||
sbBrightness.setEnabled(swGeneratedIcons.isChecked());
|
||||
swNameEmail.setChecked(prefs.getBoolean("name_email", false));
|
||||
swDistinguishContacts.setChecked(prefs.getBoolean("distinguish_contacts", false));
|
||||
swAuthentication.setChecked(prefs.getBoolean("authentication", true));
|
||||
|
@ -517,14 +521,24 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
|
|||
private void updateColor() {
|
||||
Context context = getContext();
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
boolean identicons = prefs.getBoolean("identicons", false);
|
||||
boolean circular = prefs.getBoolean("circular", true);
|
||||
|
||||
int size = Helper.dp2pixels(context, 36);
|
||||
Integer radius = (circular ? null : Helper.dp2pixels(context, 3));
|
||||
byte[] hash = ImageHelper.getHash("test@example.com");
|
||||
Integer radius = (circular && !identicons ? null : Helper.dp2pixels(context, 3));
|
||||
|
||||
Bitmap red = ImageHelper.generateLetterIcon(0f, "A", size, context);
|
||||
Bitmap green = ImageHelper.generateLetterIcon(120f, "B", size, context);
|
||||
Bitmap blue = ImageHelper.generateLetterIcon(240f, "C", size, context);
|
||||
Bitmap red = identicons
|
||||
? ImageHelper.generateIdenticon(hash, 0f, size, 5, context)
|
||||
: ImageHelper.generateLetterIcon("A", 0f, size, context);
|
||||
|
||||
Bitmap green = identicons
|
||||
? ImageHelper.generateIdenticon(hash, 120f, size, 5, context)
|
||||
: ImageHelper.generateLetterIcon("B", 120f, size, context);
|
||||
|
||||
Bitmap blue = identicons
|
||||
? ImageHelper.generateIdenticon(hash, 240f, size, 5, context)
|
||||
: ImageHelper.generateLetterIcon("C", 240f, size, context);
|
||||
|
||||
red = ImageHelper.makeCircular(red, radius);
|
||||
green = ImageHelper.makeCircular(green, radius);
|
||||
|
|
|
@ -68,18 +68,19 @@ class ImageHelper {
|
|||
|
||||
static Bitmap generateIdenticon(@NonNull String email, int size, int pixels, Context context) {
|
||||
byte[] hash = getHash(email);
|
||||
float h = Math.abs(email.hashCode()) % 360;
|
||||
return generateIdenticon(hash, h, size, pixels, context);
|
||||
}
|
||||
|
||||
static Bitmap generateIdenticon(byte[] hash, float h, int size, int pixels, Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
int saturation = prefs.getInt("saturation", 100);
|
||||
int brightness = prefs.getInt("brightness", 100);
|
||||
int s = prefs.getInt("saturation", 100);
|
||||
int v = prefs.getInt("brightness", 100);
|
||||
|
||||
int color = Color.HSVToColor(new float[]{
|
||||
Math.abs(email.hashCode()) % 360,
|
||||
saturation / 100f,
|
||||
brightness / 100f});
|
||||
int bg = Color.HSVToColor(new float[]{h, s / 100f, v / 100f});
|
||||
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(color);
|
||||
paint.setColor(bg);
|
||||
|
||||
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
|
@ -100,12 +101,15 @@ class ImageHelper {
|
|||
return bitmap;
|
||||
}
|
||||
|
||||
static Bitmap generateLetterIcon(@NonNull String email, int size, Context context) {
|
||||
static Bitmap generateLetterIcon(@NonNull String email, String name, int size, Context context) {
|
||||
if (TextUtils.isEmpty(name))
|
||||
name = email;
|
||||
|
||||
String letter = null;
|
||||
for (int i = 0; i < email.length(); i++) {
|
||||
char kar = email.charAt(i);
|
||||
for (int i = 0; i < name.length(); i++) {
|
||||
char kar = name.charAt(i);
|
||||
if (Character.isAlphabetic(kar)) {
|
||||
letter = email.substring(i, i + 1).toUpperCase();
|
||||
letter = name.substring(i, i + 1).toUpperCase();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -113,25 +117,23 @@ class ImageHelper {
|
|||
return null;
|
||||
|
||||
float h = Math.abs(email.hashCode()) % 360f;
|
||||
return generateLetterIcon(h, letter, size, context);
|
||||
return generateLetterIcon(letter, h, size, context);
|
||||
}
|
||||
|
||||
static Bitmap generateLetterIcon(float h, String letter, int size, Context context) {
|
||||
static Bitmap generateLetterIcon(String letter, float h, int size, Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
float s = prefs.getInt("saturation", 100) / 100f;
|
||||
float v = prefs.getInt("brightness", 100) / 100f;
|
||||
|
||||
int bg = Color.HSVToColor(new float[]{h, s, v});
|
||||
|
||||
double lum = ColorUtils.calculateLuminance(bg);
|
||||
int fg = Color.HSVToColor(new float[]{0, 0, lum < 0.5 ? v : 0});
|
||||
|
||||
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawColor(bg);
|
||||
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(fg);
|
||||
paint.setColor(lum < 0.5 ? Color.WHITE : Color.BLACK);
|
||||
paint.setTextSize(size / 2f);
|
||||
paint.setTypeface(Typeface.DEFAULT_BOLD);
|
||||
|
||||
|
@ -142,7 +144,7 @@ class ImageHelper {
|
|||
return bitmap;
|
||||
}
|
||||
|
||||
private static byte[] getHash(String email) {
|
||||
static byte[] getHash(String email) {
|
||||
try {
|
||||
return MessageDigest.getInstance("MD5").digest(email.getBytes());
|
||||
} catch (NoSuchAlgorithmException ignored) {
|
||||
|
|
|
@ -221,6 +221,7 @@
|
|||
android:id="@+id/tvSaturation"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:layout_marginEnd="48dp"
|
||||
android:text="@string/title_advanced_color_saturation"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
|
|
Loading…
Reference in New Issue