mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-23 18:15:22 +00:00
Consider everything <= len(4) a short code, except short countries
// FREEBIE
This commit is contained in:
parent
2a753c7795
commit
69f5aacd25
@ -33,6 +33,7 @@ import com.google.i18n.phonenumbers.Phonenumber;
|
||||
import com.google.i18n.phonenumbers.ShortNumberInfo;
|
||||
|
||||
import org.thoughtcrime.securesms.util.GroupUtil;
|
||||
import org.thoughtcrime.securesms.util.ShortCodeUtil;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.thoughtcrime.securesms.util.VisibleForTesting;
|
||||
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
||||
@ -151,7 +152,7 @@ public class CanonicalAddressDatabase {
|
||||
if (isNumberAddress(address) && TextSecurePreferences.isPushRegistered(context)) {
|
||||
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
||||
|
||||
if (!isShortCode(localNumber, address)) {
|
||||
if (!ShortCodeUtil.isShortCode(localNumber, address)) {
|
||||
address = PhoneNumberFormatter.formatNumber(address, localNumber);
|
||||
}
|
||||
}
|
||||
@ -237,20 +238,6 @@ 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);
|
||||
Phonenumber.PhoneNumber shortCode = util.parse(number, localCountryCode);
|
||||
|
||||
return ShortNumberInfo.getInstance().isPossibleShortNumberForRegion(shortCode, 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) {
|
||||
|
43
src/org/thoughtcrime/securesms/util/ShortCodeUtil.java
Normal file
43
src/org/thoughtcrime/securesms/util/ShortCodeUtil.java
Normal file
@ -0,0 +1,43 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
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 java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class ShortCodeUtil {
|
||||
|
||||
private static final String TAG = ShortCodeUtil.class.getSimpleName();
|
||||
|
||||
private static final Set<String> SHORT_COUNTRIES = new HashSet<String>() {{
|
||||
add("NU");
|
||||
add("TK");
|
||||
add("NC");
|
||||
add("AC");
|
||||
}};
|
||||
|
||||
public static 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);
|
||||
|
||||
if (number.replaceAll("[^0-9+]", "").length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) {
|
||||
return true;
|
||||
} else {
|
||||
Phonenumber.PhoneNumber shortCode = util.parse(number, localCountryCode);
|
||||
return ShortNumberInfo.getInstance().isPossibleShortNumberForRegion(shortCode, localCountryCode);
|
||||
}
|
||||
} catch (NumberParseException e) {
|
||||
Log.w(TAG, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
public class ShortCodeUtilTest {
|
||||
|
||||
@Test
|
||||
public void testShortCodes() throws Exception {
|
||||
assertTrue(ShortCodeUtil.isShortCode("+14152222222", "40404"));
|
||||
assertTrue(ShortCodeUtil.isShortCode("+14152222222", "431"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+14152222222", "4157778888"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+14152222222", "+14157778888"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+14152222222", "415-777-8888"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+14152222222", "(415) 777-8888"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+14152222222", "8882222"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+14152222222", "888-2222"));
|
||||
|
||||
assertTrue(ShortCodeUtil.isShortCode("+491723742522", "670"));
|
||||
assertTrue(ShortCodeUtil.isShortCode("+491723742522", "115"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+491723742522", "089-12345678"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+491723742522", "089/12345678"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+491723742522", "12345678"));
|
||||
|
||||
assertTrue(ShortCodeUtil.isShortCode("+298123456", "4040"));
|
||||
assertTrue(ShortCodeUtil.isShortCode("+298123456", "6701"));
|
||||
assertTrue(ShortCodeUtil.isShortCode("+298123456", "433"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+298123456", "123456"));
|
||||
|
||||
assertTrue(ShortCodeUtil.isShortCode("+61414915066", "19808948"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+61414915066", "119808948"));
|
||||
|
||||
assertTrue(ShortCodeUtil.isShortCode("+79166503388", "8080"));
|
||||
assertTrue(ShortCodeUtil.isShortCode("+79166503388", "6701"));
|
||||
assertTrue(ShortCodeUtil.isShortCode("+79166503388", "431"));
|
||||
assertFalse(ShortCodeUtil.isShortCode("+79166503388", "111-22-33"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user