Don't E164 shortcodes

Fixes #4478
// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-11-13 10:49:11 -08:00
parent 0c75e652eb
commit 21f04605c6

View File

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