mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-09 07:36:50 +00:00
Consider everything <= len(4) a short code, except short countries
// FREEBIE
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user