Cache contact names

This commit is contained in:
ByteHamster 2018-07-09 21:42:27 +02:00 committed by cketti
parent b3c0021edb
commit 4ecb0d14e4
3 changed files with 22 additions and 1 deletions

View file

@ -12,6 +12,8 @@ import android.provider.ContactsContract.CommonDataKinds.Photo;
import com.fsck.k9.mail.Address;
import java.util.HashMap;
/**
* Helper class to access the contacts stored on the device.
*/
@ -64,6 +66,7 @@ public class Contacts {
protected Context mContext;
protected ContentResolver mContentResolver;
private static HashMap<String, String> nameCache = new HashMap<>();
/**
@ -103,6 +106,7 @@ public class Contacts {
}
mContext.startActivity(contactIntent);
clearCache();
}
/**
@ -117,6 +121,7 @@ public class Contacts {
addIntent.putExtra(ContactsContract.Intents.Insert.PHONE, Uri.decode(phoneNumber));
addIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(addIntent);
clearCache();
}
/**
@ -171,6 +176,8 @@ public class Contacts {
public String getNameForAddress(String address) {
if (address == null) {
return null;
} else if (nameCache.containsKey(address)) {
return nameCache.get(address);
}
final Cursor c = getContactByAddress(address);
@ -184,6 +191,7 @@ public class Contacts {
c.close();
}
nameCache.put(address, name);
return name;
}
@ -273,4 +281,11 @@ public class Contacts {
SORT_ORDER);
}
/**
* Clears the cache for names and photo uris
*/
public static void clearCache() {
nameCache.clear();
}
}

View file

@ -116,7 +116,6 @@ public class MessageHelper {
return address.getAddress();
} else if (contacts != null) {
final String name = contacts.getNameForAddress(address.getAddress());
// TODO: The results should probably be cached for performance reasons.
if (name != null) {
if (changeContactNameColor) {
final SpannableString coloredName = new SpannableString(name);

View file

@ -47,6 +47,7 @@ import com.fsck.k9.K9;
import com.fsck.k9.K9.SplitViewMode;
import com.fsck.k9.Preferences;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.helper.Contacts;
import com.fsck.k9.ui.R;
import com.fsck.k9.activity.compose.MessageActions;
import com.fsck.k9.activity.misc.SwipeGestureDetector.OnSwipeGestureListener;
@ -543,6 +544,12 @@ public class MessageList extends K9Activity implements MessageListFragmentListen
StorageManager.getInstance(getApplication()).addListener(mStorageListener);
}
@Override
protected void onStart() {
super.onStart();
Contacts.clearCache();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);