From c7dd956f456841cbb51c8e5e29940fb09759d628 Mon Sep 17 00:00:00 2001 From: McLoo Date: Thu, 8 Jan 2015 23:56:04 +0100 Subject: [PATCH] Fix for getting the device number with national prefix Closes #2312 --- .../securesms/RegistrationActivity.java | 10 ++-- src/org/thoughtcrime/securesms/util/Util.java | 26 +++++----- .../util/PhoneNumberFormatterTest.java | 48 +++++++++++++++---- 3 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/org/thoughtcrime/securesms/RegistrationActivity.java b/src/org/thoughtcrime/securesms/RegistrationActivity.java index bbd1baef68..61b94f92f6 100644 --- a/src/org/thoughtcrime/securesms/RegistrationActivity.java +++ b/src/org/thoughtcrime/securesms/RegistrationActivity.java @@ -1,12 +1,10 @@ package org.thoughtcrime.securesms; -import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AlertDialog; -import android.telephony.TelephonyManager; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; @@ -133,14 +131,14 @@ public class RegistrationActivity extends BaseActionBarActivity { Phonenumber.PhoneNumber localNumberObject = numberUtil.parse(localNumber, null); if (localNumberObject != null) { - this.countryCode.setText(localNumberObject.getCountryCode()+""); - this.number.setText(localNumberObject.getNationalNumber()+""); + this.countryCode.setText(String.valueOf(localNumberObject.getCountryCode())); + this.number.setText(String.valueOf(localNumberObject.getNationalNumber())); } } else { - String simCountryIso = ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getSimCountryIso(); + String simCountryIso = Util.getSimCountryIso(this); if (!TextUtils.isEmpty(simCountryIso)) { - this.countryCode.setText(numberUtil.getCountryCodeForRegion(simCountryIso.toUpperCase())+""); + this.countryCode.setText(numberUtil.getCountryCodeForRegion(simCountryIso)+""); } } } catch (NumberParseException npe) { diff --git a/src/org/thoughtcrime/securesms/util/Util.java b/src/org/thoughtcrime/securesms/util/Util.java index 77ec363411..c25ffcab40 100644 --- a/src/org/thoughtcrime/securesms/util/Util.java +++ b/src/org/thoughtcrime/securesms/util/Util.java @@ -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 List> partition(List list, int partitionSize) { diff --git a/test/unitTest/java/org/thoughtcrime/securesms/util/PhoneNumberFormatterTest.java b/test/unitTest/java/org/thoughtcrime/securesms/util/PhoneNumberFormatterTest.java index e8c6b8c2bb..275fbb8279 100644 --- a/test/unitTest/java/org/thoughtcrime/securesms/util/PhoneNumberFormatterTest.java +++ b/test/unitTest/java/org/thoughtcrime/securesms/util/PhoneNumberFormatterTest.java @@ -8,21 +8,53 @@ import org.whispersystems.textsecure.api.util.PhoneNumberFormatter; import static org.assertj.core.api.Assertions.assertThat; -public class PhoneNumberFormatterTest { - private static final String LOCAL_NUMBER = "+15555555555"; +public class PhoneNumberFormatterTest extends TextSecureTestCase { + private static final String LOCAL_NUMBER_US = "+15555555555"; + private static final String NUMBER_CH = "+41446681800"; + private static final String NUMBER_UK = "+442079460018"; + private static final String NUMBER_DE = "+4930123456"; + private static final String NUMBER_MOBILE_DE = "+49171123456"; + private static final String COUNTRY_CODE_CH = "41"; + private static final String COUNTRY_CODE_UK = "44"; + private static final String COUNTRY_CODE_DE = "49"; - @Test public void testFormatNumberE164() throws Exception, InvalidNumberException { - assertThat(PhoneNumberFormatter.formatNumber("(555) 555-5555", LOCAL_NUMBER)).isEqualTo(LOCAL_NUMBER); - assertThat(PhoneNumberFormatter.formatNumber("555-5555", LOCAL_NUMBER)).isEqualTo(LOCAL_NUMBER); - assertThat(PhoneNumberFormatter.formatNumber("(123) 555-5555", LOCAL_NUMBER)).isNotEqualTo(LOCAL_NUMBER); + @Test + public void testFormatNumber() throws Exception, InvalidNumberException { + assertThat(PhoneNumberFormatter.formatNumber("(555) 555-5555", LOCAL_NUMBER_US)).isEqualTo(LOCAL_NUMBER_US); + assertThat(PhoneNumberFormatter.formatNumber("555-5555", LOCAL_NUMBER_US)).isEqualTo(LOCAL_NUMBER_US); + assertThat(PhoneNumberFormatter.formatNumber("(123) 555-5555", LOCAL_NUMBER_US)).isNotEqualTo(LOCAL_NUMBER_US); } - @Test public void testFormatNumberEmail() throws Exception { + @Test + public void testFormatNumberEmail() throws Exception { try { - PhoneNumberFormatter.formatNumber("person@domain.com", LOCAL_NUMBER); + PhoneNumberFormatter.formatNumber("person@domain.com", LOCAL_NUMBER_US); throw new AssertionFailedError("should have thrown on email"); } catch (InvalidNumberException ine) { // success } } + + @Test + public void testFormatNumberE164() throws Exception, InvalidNumberException { + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "(020) 7946 0018").equals(NUMBER_UK)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "044 20 7946 0018").equals(NUMBER_UK)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "+442079460018").equals(NUMBER_UK)); + + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_CH, "+41 44 668 18 00").equals(NUMBER_CH)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_CH, "+41 (044) 6681800").equals(NUMBER_CH)); + + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049 030 123456").equals(NUMBER_DE)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049 (0)30123456").equals(NUMBER_DE)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049((0)30)123456").equals(NUMBER_DE)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "+49 (0) 30 1 2 3 45 6 ").equals(NUMBER_DE)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "030 123456").equals(NUMBER_DE)); + + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0171123456").equals(NUMBER_MOBILE_DE)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0171/123456").equals(NUMBER_MOBILE_DE)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "+490171/123456").equals(NUMBER_MOBILE_DE)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "00490171/123456").equals(NUMBER_MOBILE_DE)); + assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049171/123456").equals(NUMBER_MOBILE_DE)); + } + }