From e7937fd42464ca045376c0d68cf981c369502656 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Mon, 16 Oct 2017 13:11:59 -0700 Subject: [PATCH] Cache external address formatter for better performance // FREEBIE --- .../securesms/database/Address.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/Address.java b/src/org/thoughtcrime/securesms/database/Address.java index a14ea3a6b8..56c6108282 100644 --- a/src/org/thoughtcrime/securesms/database/Address.java +++ b/src/org/thoughtcrime/securesms/database/Address.java @@ -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
{ @@ -44,6 +46,8 @@ public class Address implements Parcelable, Comparable
{ private static final String TAG = Address.class.getSimpleName(); + private static final AtomicReference> cachedFormatter = new AtomicReference<>(); + private final String address; private Address(@NonNull String address) { @@ -60,17 +64,7 @@ public class Address implements Parcelable, Comparable
{ } 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
fromSerializedList(@NonNull String serialized, char delimiter) { @@ -96,6 +90,23 @@ public class Address implements Parcelable, Comparable
{ return Util.join(escapedAddresses, delimiter + ""); } + private static @NonNull ExternalAddressFormatter getExternalAddressFormatter(Context context) { + String localNumber = TextSecurePreferences.getLocalNumber(context); + + if (!TextUtils.isEmpty(localNumber)) { + Pair 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
{ } @VisibleForTesting - static class ExternalAddressFormatter { + public static class ExternalAddressFormatter { private static final String TAG = ExternalAddressFormatter.class.getSimpleName();