1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

Optimize code to extract the character to display in the fallback contact picture

This commit is contained in:
cketti 2013-08-21 13:05:42 +02:00
parent ce56475a4f
commit bd3bd861c5

View File

@ -4,6 +4,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -33,6 +34,12 @@ public class ContactPictureLoader {
*/ */
private static final int PICTURE_SIZE = 40; private static final int PICTURE_SIZE = 40;
/**
* Pattern to extract the letter to be displayed as fallback image.
*/
private static final Pattern EXTRACT_LETTER_PATTERN = Pattern.compile("[a-zA-Z]");
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
private Resources mResources; private Resources mResources;
private Contacts mContactsHelper; private Contacts mContactsHelper;
@ -154,11 +161,11 @@ public class ContactPictureLoader {
private char calcUnknownContactLetter(Address address) { private char calcUnknownContactLetter(Address address) {
String letter = ""; String letter = "";
Pattern p = Pattern.compile("[^a-zA-Z]*([a-zA-Z]).*");
String str = address.getPersonal() != null ? address.getPersonal() : address.getAddress(); String str = address.getPersonal() != null ? address.getPersonal() : address.getAddress();
Matcher m = p.matcher(str);
if (m.matches()) { Matcher m = EXTRACT_LETTER_PATTERN.matcher(str);
letter = m.group(1).toUpperCase(); if (m.find()) {
letter = m.group(0).toUpperCase(Locale.US);
} }
return letter.length() == 0 ? '?' : letter.charAt(0); return letter.length() == 0 ? '?' : letter.charAt(0);