|
27 | 27 | import android.text.TextUtils; |
28 | 28 | import android.util.Log; |
29 | 29 |
|
| 30 | +import com.google.i18n.phonenumbers.NumberParseException; |
| 31 | +import com.google.i18n.phonenumbers.PhoneNumberUtil; |
| 32 | +import com.google.i18n.phonenumbers.Phonenumber; |
| 33 | +import com.google.i18n.phonenumbers.ShortNumberInfo; |
| 34 | + |
30 | 35 | import org.thoughtcrime.securesms.util.GroupUtil; |
31 | 36 | import org.thoughtcrime.securesms.util.TextSecurePreferences; |
32 | 37 | import org.thoughtcrime.securesms.util.VisibleForTesting; |
@@ -144,7 +149,11 @@ public long getCanonicalAddressId(@NonNull String address) { |
144 | 149 | long canonicalAddressId; |
145 | 150 |
|
146 | 151 | if (isNumberAddress(address) && TextSecurePreferences.isPushRegistered(context)) { |
147 | | - address = PhoneNumberFormatter.formatNumber(address, TextSecurePreferences.getLocalNumber(context)); |
| 152 | + String localNumber = TextSecurePreferences.getLocalNumber(context); |
| 153 | + |
| 154 | + if (!isShortCode(localNumber, address)) { |
| 155 | + address = PhoneNumberFormatter.formatNumber(address, localNumber); |
| 156 | + } |
148 | 157 | } |
149 | 158 |
|
150 | 159 | if ((canonicalAddressId = getCanonicalAddressFromCache(address)) != -1) { |
@@ -228,6 +237,19 @@ static boolean isNumberAddress(@NonNull String number) { |
228 | 237 | return PhoneNumberUtils.isWellFormedSmsAddress(number); |
229 | 238 | } |
230 | 239 |
|
| 240 | + private boolean isShortCode(@NonNull String localNumber, @NonNull String number) { |
| 241 | + try { |
| 242 | + PhoneNumberUtil util = PhoneNumberUtil.getInstance(); |
| 243 | + Phonenumber.PhoneNumber localNumberObject = util.parse(localNumber, null); |
| 244 | + String localCountryCode = util.getRegionCodeForNumber(localNumberObject); |
| 245 | + |
| 246 | + return ShortNumberInfo.getInstance().isValidShortNumberForRegion(number, localCountryCode); |
| 247 | + } catch (NumberParseException e) { |
| 248 | + Log.w(TAG, e); |
| 249 | + return false; |
| 250 | + } |
| 251 | + } |
| 252 | + |
231 | 253 | private static class DatabaseHelper extends SQLiteOpenHelper { |
232 | 254 |
|
233 | 255 | public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { |
|
0 commit comments