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) {