Cache external address formatter for better performance

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2017-10-16 13:11:59 -07:00
parent b80408bcb4
commit e7937fd424

View File

@ -9,6 +9,7 @@ import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
@ -26,6 +27,7 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
public class Address implements Parcelable, Comparable<Address> {
@ -44,6 +46,8 @@ public class Address implements Parcelable, Comparable<Address> {
private static final String TAG = Address.class.getSimpleName();
private static final AtomicReference<Pair<String, ExternalAddressFormatter>> cachedFormatter = new AtomicReference<>();
private final String address;
private Address(@NonNull String address) {
@ -60,17 +64,7 @@ public class Address implements Parcelable, Comparable<Address> {
}
public static Address fromExternal(@NonNull Context context, @Nullable String external) {
String localNumber = TextSecurePreferences.getLocalNumber(context);
ExternalAddressFormatter formatter;
if (!TextUtils.isEmpty(localNumber)) {
formatter = new ExternalAddressFormatter(localNumber);
} else {
formatter = new ExternalAddressFormatter(Util.getSimCountryIso(context).or("US"), true);
}
return new Address(formatter.format(external));
return new Address(getExternalAddressFormatter(context).format(external));
}
public static @NonNull List<Address> fromSerializedList(@NonNull String serialized, char delimiter) {
@ -96,6 +90,23 @@ public class Address implements Parcelable, Comparable<Address> {
return Util.join(escapedAddresses, delimiter + "");
}
private static @NonNull ExternalAddressFormatter getExternalAddressFormatter(Context context) {
String localNumber = TextSecurePreferences.getLocalNumber(context);
if (!TextUtils.isEmpty(localNumber)) {
Pair<String, ExternalAddressFormatter> cached = cachedFormatter.get();
if (cached != null && cached.first.equals(localNumber)) return cached.second;
ExternalAddressFormatter formatter = new ExternalAddressFormatter(localNumber);
cachedFormatter.set(new Pair<>(localNumber, formatter));
return formatter;
} else {
return new ExternalAddressFormatter(Util.getSimCountryIso(context).or("US"), true);
}
}
public boolean isGroup() {
return GroupUtil.isEncodedGroup(address);
}
@ -164,7 +175,7 @@ public class Address implements Parcelable, Comparable<Address> {
}
@VisibleForTesting
static class ExternalAddressFormatter {
public static class ExternalAddressFormatter {
private static final String TAG = ExternalAddressFormatter.class.getSimpleName();