ImportKeys: Use cache for Highlighter
This commit is contained in:
@@ -9,6 +9,7 @@ import android.widget.TextView;
|
|||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.ui.util.Highlighter;
|
import org.sufficientlysecure.keychain.ui.util.Highlighter;
|
||||||
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
||||||
|
import org.sufficientlysecure.keychain.util.LruCache;
|
||||||
|
|
||||||
public class ImportKeysAdapterBinding {
|
public class ImportKeysAdapterBinding {
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ public class ImportKeysAdapterBinding {
|
|||||||
if (secret) {
|
if (secret) {
|
||||||
userId = resources.getString(R.string.secret_key) + " " + userId;
|
userId = resources.getString(R.string.secret_key) + " " + userId;
|
||||||
} else {
|
} else {
|
||||||
Highlighter highlighter = new Highlighter(context, query);
|
Highlighter highlighter = getHighlighter(context, query);
|
||||||
userId = highlighter.highlight(userId);
|
userId = highlighter.highlight(userId);
|
||||||
}
|
}
|
||||||
textView.setText(userId);
|
textView.setText(userId);
|
||||||
@@ -46,7 +47,7 @@ public class ImportKeysAdapterBinding {
|
|||||||
if (userEmail == null)
|
if (userEmail == null)
|
||||||
userEmail = "";
|
userEmail = "";
|
||||||
|
|
||||||
Highlighter highlighter = new Highlighter(context, query);
|
Highlighter highlighter = getHighlighter(context, query);
|
||||||
textView.setText(highlighter.highlight(userEmail));
|
textView.setText(highlighter.highlight(userEmail));
|
||||||
|
|
||||||
if (revokedOrExpired) {
|
if (revokedOrExpired) {
|
||||||
@@ -69,4 +70,16 @@ public class ImportKeysAdapterBinding {
|
|||||||
textView.setText(KeyFormattingUtils.beautifyKeyIdWithPrefix(keyId));
|
textView.setText(KeyFormattingUtils.beautifyKeyIdWithPrefix(keyId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static LruCache<String, Highlighter> highlighterCache = new LruCache<>(1);
|
||||||
|
|
||||||
|
private static Highlighter getHighlighter(Context context, String query) {
|
||||||
|
Highlighter highlighter = highlighterCache.get(query);
|
||||||
|
if (highlighter == null) {
|
||||||
|
highlighter = new Highlighter(context, query);
|
||||||
|
highlighterCache.put(query, highlighter);
|
||||||
|
}
|
||||||
|
|
||||||
|
return highlighter;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package org.sufficientlysecure.keychain.util;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
// Source: http://stackoverflow.com/a/1953516
|
||||||
|
public class LruCache<A, B> extends LinkedHashMap<A, B> {
|
||||||
|
|
||||||
|
private final int maxEntries;
|
||||||
|
|
||||||
|
public LruCache(final int maxEntries) {
|
||||||
|
super(maxEntries + 1, 1.0f, true);
|
||||||
|
this.maxEntries = maxEntries;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean removeEldestEntry(final Map.Entry<A, B> eldest) {
|
||||||
|
return super.size() > maxEntries;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user