Fix for getting the device number with national prefix

Closes #2312
This commit is contained in:
McLoo
2015-01-08 23:56:04 +01:00
committed by Moxie Marlinspike
parent 3b80e35547
commit c7dd956f45
3 changed files with 59 additions and 25 deletions

View File

@@ -38,6 +38,8 @@ import android.text.style.StyleSpan;
import android.util.Log;
import android.widget.EditText;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.mms.OutgoingLegacyMmsConnection;
import org.whispersystems.textsecure.api.util.InvalidNumberException;
@@ -50,7 +52,6 @@ import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -217,19 +218,22 @@ public class Util {
return total;
}
public static String getDeviceE164Number(Context context) {
String localNumber = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE))
.getLine1Number();
public static @Nullable String getDeviceE164Number(Context context) {
final String localNumber = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
final String countryIso = getSimCountryIso(context);
final Integer countryCode = PhoneNumberUtil.getInstance().getCountryCodeForRegion(countryIso);
if (!TextUtils.isEmpty(localNumber) && !localNumber.startsWith("+"))
{
if (localNumber.length() == 10) localNumber = "+1" + localNumber;
else localNumber = "+" + localNumber;
if (TextUtils.isEmpty(localNumber)) return null;
return localNumber;
}
if (localNumber.startsWith("+")) return localNumber;
else if (!TextUtils.isEmpty(countryIso)) return PhoneNumberFormatter.formatE164(String.valueOf(countryCode), localNumber);
else if (localNumber.length() == 10) return "+1" + localNumber;
else return "+" + localNumber;
}
return null;
public static @Nullable String getSimCountryIso(Context context) {
String simCountryIso = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getSimCountryIso();
return simCountryIso != null ? simCountryIso.toUpperCase() : null;
}
public static <T> List<List<T>> partition(List<T> list, int partitionSize) {