From 21f04605c68a2e157cfe0da4e5a480635842f5ec Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Fri, 13 Nov 2015 10:49:11 -0800 Subject: [PATCH] Don't E164 shortcodes Fixes #4478 // FREEBIE --- .../database/CanonicalAddressDatabase.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/database/CanonicalAddressDatabase.java b/src/org/thoughtcrime/securesms/database/CanonicalAddressDatabase.java index fc586eeb9b..1f0d128597 100644 --- a/src/org/thoughtcrime/securesms/database/CanonicalAddressDatabase.java +++ b/src/org/thoughtcrime/securesms/database/CanonicalAddressDatabase.java @@ -27,6 +27,11 @@ import android.telephony.PhoneNumberUtils; import android.text.TextUtils; import android.util.Log; +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.Phonenumber; +import com.google.i18n.phonenumbers.ShortNumberInfo; + import org.thoughtcrime.securesms.util.GroupUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.VisibleForTesting; @@ -144,7 +149,11 @@ public class CanonicalAddressDatabase { long canonicalAddressId; if (isNumberAddress(address) && TextSecurePreferences.isPushRegistered(context)) { - address = PhoneNumberFormatter.formatNumber(address, TextSecurePreferences.getLocalNumber(context)); + String localNumber = TextSecurePreferences.getLocalNumber(context); + + if (!isShortCode(localNumber, address)) { + address = PhoneNumberFormatter.formatNumber(address, localNumber); + } } if ((canonicalAddressId = getCanonicalAddressFromCache(address)) != -1) { @@ -228,6 +237,19 @@ public class CanonicalAddressDatabase { return PhoneNumberUtils.isWellFormedSmsAddress(number); } + private boolean isShortCode(@NonNull String localNumber, @NonNull String number) { + try { + PhoneNumberUtil util = PhoneNumberUtil.getInstance(); + Phonenumber.PhoneNumber localNumberObject = util.parse(localNumber, null); + String localCountryCode = util.getRegionCodeForNumber(localNumberObject); + + return ShortNumberInfo.getInstance().isValidShortNumberForRegion(number, localCountryCode); + } catch (NumberParseException e) { + Log.w(TAG, e); + return false; + } + } + private static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {