session-android/src/org/thoughtcrime/securesms/util/ShortCodeUtil.java
Andy Staudacher 61a7062030 Remove obsolete workaround for DE/FI/SK shortcodes
libphonenumber supports these since December 2015 / January 2016.
Most of these changes went into libphonenumber-7.2.2 (Dec 15, 2015),
the last changes related to this into libphonenumber-7.2.4 (Jan 28, 2016).
This was about adding EU-wide helplines 116xxx (http://ec.europa.eu/digital-agenda/en/116-helplines).
See: https://github.com/googlei18n/libphonenumber/blob/master/java/release_notes.txt

Closes #6130
2018-03-07 09:59:43 -08:00

46 lines
1.5 KiB
Java

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);
String bareNumber = number.replaceAll("[^0-9+]", "");
// libphonenumber seems incorrect for Russia and a few other countries with 4 digit short codes.
if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) {
return true;
}
Phonenumber.PhoneNumber shortCode = util.parse(number, localCountryCode);
return ShortNumberInfo.getInstance().isPossibleShortNumberForRegion(shortCode, localCountryCode);
} catch (NumberParseException e) {
Log.w(TAG, e);
return false;
}
}
}