From fc6d5382079c5d93560c53add0a4d152cff2b6ff Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Thu, 24 May 2018 16:39:12 -0700 Subject: [PATCH 01/11] Adds ability to limit country selection in phone authentication --- .../java/com/firebase/ui/auth/AuthUI.java | 134 ++++- .../auth/data/client/CountryListLoadTask.java | 540 ++++++++++-------- .../ui/auth/ui/phone/CountryListSpinner.java | 17 +- .../ui/phone/VerifyPhoneNumberFragment.java | 11 + .../firebase/ui/auth/util/ExtraConstants.java | 3 + .../ui/phone/CountryListLoadTaskTests.java | 56 +- 6 files changed, 495 insertions(+), 266 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java index f99d0c721..eec85178c 100644 --- a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java +++ b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java @@ -77,6 +77,7 @@ import java.util.HashSet; import java.util.IdentityHashMap; import java.util.List; +import java.util.Locale; import java.util.Set; /** @@ -214,9 +215,9 @@ public static int getDefaultTheme() { * Signs the user in without any UI if possible. If this operation fails, you can safely start a * UI-based sign-in flow knowing it is required. * - * @param context requesting the user be signed in - * @param configs to use for silent sign in. Only Google and email are currently - * supported, the rest will be ignored. + * @param context requesting the user be signed in + * @param configs to use for silent sign in. Only Google and email are currently supported, the + * rest will be ignored. * @return a task which indicates whether or not the user was successfully signed in. */ @NonNull @@ -643,10 +644,123 @@ public PhoneBuilder setDefaultCountryIso(@NonNull String iso) { throw new IllegalStateException("Invalid country iso: " + iso); } - getParams().putString(ExtraConstants.COUNTRY_ISO, iso); + getParams().putString(ExtraConstants.COUNTRY_ISO, + iso.toUpperCase(Locale.getDefault())); + + return this; + } + + + /** + * Sets the country codes available in the country code selector for phone + * authentication. This is not to be called with + * {@link #setBlacklistedPhoneAuthCountryCodes(List)}. + * If both are called, an exception will be thrown. + *

+ * For a list of country codes, see Alpha-2 codes here: + * http://www.nationsonline.org/oneworld/country_code_list.htm + * + * @param whitelistedPhoneAuthCountryCodes a case insensitive list of country codes to + * be whitelisted + */ + public PhoneBuilder setWhitelistedPhoneAuthCountryCodes( + @Nullable List whitelistedPhoneAuthCountryCodes) { + if (getParams().containsKey(ExtraConstants.BLACKLISTED_COUNTRY_CODES)) { + throw new RuntimeException( + "You can either whitelist or blacklist country codes for phone " + + "authentication."); + } + addCountryCodesToBundle(whitelistedPhoneAuthCountryCodes, + ExtraConstants.WHITELISTED_COUNTRY_CODES); return this; } + + /** + * Sets the country codes to be removed from the country code selector for phone + * authentication. This is not to be called with + * {@link #setWhitelistedPhoneAuthCountryCodes(List)}. + * If both are called, an exception will be thrown. + *

+ * For a list of country codes, see Alpha-2 codes here: + * https://en.wikipedia.org/wiki/ISO_3166-1 + * + * @param blacklistedPhoneAuthCountryCodes a case insensitive list of country codes to + * be whitelisted + */ + public PhoneBuilder setBlacklistedPhoneAuthCountryCodes( + @Nullable List blacklistedPhoneAuthCountryCodes) { + if (getParams().containsKey(ExtraConstants.WHITELISTED_COUNTRY_CODES)) { + throw new RuntimeException( + "You can either whitelist or blacklist country codes for phone " + + "authentication."); + } + + addCountryCodesToBundle(blacklistedPhoneAuthCountryCodes, + ExtraConstants.BLACKLISTED_COUNTRY_CODES); + return this; + } + + private void addCountryCodesToBundle(List countryCodes, + String countryCodeType) { + ArrayList uppercaseCodes = new ArrayList<>(); + for (String code : countryCodes) { + uppercaseCodes.add(code.toUpperCase(Locale.getDefault())); + } + + getParams().putStringArrayList(countryCodeType, uppercaseCodes); + } + + @Override + public IdpConfig build() { + validateInputs(); + return super.build(); + } + + private void validateInputs() { + List whitelistedCountryCodes = getParams().getStringArrayList( + ExtraConstants.WHITELISTED_COUNTRY_CODES); + List blacklistedCountryCodes = getParams().getStringArrayList( + ExtraConstants.BLACKLISTED_COUNTRY_CODES); + + if (whitelistedCountryCodes != null && + blacklistedCountryCodes != null) { + throw new RuntimeException( + "You can either whitelist or blacklist country codes for phone " + + "authentication."); + } else if (whitelistedCountryCodes != null) { + validateCountryCodes(whitelistedCountryCodes); + validateDefaultCountryCode(whitelistedCountryCodes, true); + + } else if (blacklistedCountryCodes != null) { + validateCountryCodes(blacklistedCountryCodes); + validateDefaultCountryCode(blacklistedCountryCodes, false); + } + } + + private void validateCountryCodes(List codes) { + if (codes == null) { + return; + } + for (String code : codes) { + if (!PhoneNumberUtils.isValidIso(code)) { + throw new RuntimeException("Invalid country iso code provided."); + } + } + } + + private void validateDefaultCountryCode(List codes, + boolean whitelisted) { + if (getParams().containsKey(ExtraConstants.COUNTRY_ISO) && codes != null) { + String defaultCode = getParams().getString(ExtraConstants.COUNTRY_ISO); + if ((codes.contains(defaultCode) && !whitelisted) + || (!codes.contains(defaultCode) && whitelisted)) { + throw new RuntimeException("Invalid default country iso. Make sure it " + + "is either part of the whitelisted country codes or that you " + + "haven't blacklisted it."); + } + } + } } /** @@ -679,8 +793,8 @@ public GoogleBuilder setScopes(@NonNull List scopes) { } /** - * Set the {@link GoogleSignInOptions} to be used for Google sign-in. Standard options - * like requesting the user's email will automatically be added. + * Set the {@link GoogleSignInOptions} to be used for Google sign-in. Standard + * options like requesting the user's email will automatically be added. * * @param options sign-in options */ @@ -784,8 +898,8 @@ private abstract class AuthIntentBuilder { boolean mEnableHints = true; /** - * Specifies the theme to use for the application flow. If no theme is specified, a default - * theme will be used. + * Specifies the theme to use for the application flow. If no theme is specified, a + * default theme will be used. */ @NonNull public T setTheme(@StyleRes int theme) { @@ -825,8 +939,8 @@ public T setPrivacyPolicyUrl(@Nullable String privacyPolicyUrl) { } /** - * Specified the set of supported authentication providers. At least one provider must be - * specified. There may only be one instance of each provider. + * Specified the set of supported authentication providers. At least one provider must + * be specified. There may only be one instance of each provider. *

*

If no providers are explicitly specified by calling this method, then the email * provider is the default supported provider. diff --git a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java index d2c20589f..a3310afad 100644 --- a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java +++ b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java @@ -25,8 +25,12 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Set; // TODO We need to move away from ListView and AsyncTask in the future and use (say) // RecyclerView and Task/ThreadPoolExecutor . @@ -38,271 +42,307 @@ public interface Listener { void onLoadComplete(List result); } - private static final int MAX_COUNTRIES = 291; - private final Listener mListener; - public CountryListLoadTask(Listener listener) { + List whitelistedCountryCodes; + List blacklistedCountryCodes; + + public CountryListLoadTask(Listener listener, + List whitelistedCountryCodes, + List blacklistedCountryCodes) { mListener = listener; + this.whitelistedCountryCodes = whitelistedCountryCodes; + this.blacklistedCountryCodes = blacklistedCountryCodes; } @Override protected List doInBackground(Void... params) { - final List countryInfoList = new ArrayList<>(MAX_COUNTRIES); - countryInfoList.add(new CountryInfo(new Locale("", "AF"), 93)); - countryInfoList.add(new CountryInfo(new Locale("", "AX"), 358)); - countryInfoList.add(new CountryInfo(new Locale("", "AL"), 355)); - countryInfoList.add(new CountryInfo(new Locale("", "DZ"), 213)); - countryInfoList.add(new CountryInfo(new Locale("", "AS"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "AD"), 376)); - countryInfoList.add(new CountryInfo(new Locale("", "AO"), 244)); - countryInfoList.add(new CountryInfo(new Locale("", "AI"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "AG"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "AR"), 54)); - countryInfoList.add(new CountryInfo(new Locale("", "AM"), 374)); - countryInfoList.add(new CountryInfo(new Locale("", "AW"), 297)); - countryInfoList.add(new CountryInfo(new Locale("", "AC"), 247)); - countryInfoList.add(new CountryInfo(new Locale("", "AU"), 61)); - countryInfoList.add(new CountryInfo(new Locale("", "AT"), 43)); - countryInfoList.add(new CountryInfo(new Locale("", "AZ"), 994)); - countryInfoList.add(new CountryInfo(new Locale("", "BS"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "BH"), 973)); - countryInfoList.add(new CountryInfo(new Locale("", "BD"), 880)); - countryInfoList.add(new CountryInfo(new Locale("", "BB"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "BY"), 375)); - countryInfoList.add(new CountryInfo(new Locale("", "BE"), 32)); - countryInfoList.add(new CountryInfo(new Locale("", "BZ"), 501)); - countryInfoList.add(new CountryInfo(new Locale("", "BJ"), 229)); - countryInfoList.add(new CountryInfo(new Locale("", "BM"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "BT"), 975)); - countryInfoList.add(new CountryInfo(new Locale("", "BO"), 591)); - countryInfoList.add(new CountryInfo(new Locale("", "BA"), 387)); - countryInfoList.add(new CountryInfo(new Locale("", "BW"), 267)); - countryInfoList.add(new CountryInfo(new Locale("", "BR"), 55)); - countryInfoList.add(new CountryInfo(new Locale("", "IO"), 246)); - countryInfoList.add(new CountryInfo(new Locale("", "VG"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "BN"), 673)); - countryInfoList.add(new CountryInfo(new Locale("", "BG"), 359)); - countryInfoList.add(new CountryInfo(new Locale("", "BF"), 226)); - countryInfoList.add(new CountryInfo(new Locale("", "BI"), 257)); - countryInfoList.add(new CountryInfo(new Locale("", "KH"), 855)); - countryInfoList.add(new CountryInfo(new Locale("", "CM"), 237)); - countryInfoList.add(new CountryInfo(new Locale("", "CA"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "CV"), 238)); - countryInfoList.add(new CountryInfo(new Locale("", "BQ"), 599)); - countryInfoList.add(new CountryInfo(new Locale("", "KY"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "CF"), 236)); - countryInfoList.add(new CountryInfo(new Locale("", "TD"), 235)); - countryInfoList.add(new CountryInfo(new Locale("", "CL"), 56)); - countryInfoList.add(new CountryInfo(new Locale("", "CN"), 86)); - countryInfoList.add(new CountryInfo(new Locale("", "CX"), 61)); - countryInfoList.add(new CountryInfo(new Locale("", "CC"), 61)); - countryInfoList.add(new CountryInfo(new Locale("", "CO"), 57)); - countryInfoList.add(new CountryInfo(new Locale("", "KM"), 269)); - countryInfoList.add(new CountryInfo(new Locale("", "CD"), 243)); - countryInfoList.add(new CountryInfo(new Locale("", "CG"), 242)); - countryInfoList.add(new CountryInfo(new Locale("", "CK"), 682)); - countryInfoList.add(new CountryInfo(new Locale("", "CR"), 506)); - countryInfoList.add(new CountryInfo(new Locale("", "CI"), 225)); - countryInfoList.add(new CountryInfo(new Locale("", "HR"), 385)); - countryInfoList.add(new CountryInfo(new Locale("", "CU"), 53)); - countryInfoList.add(new CountryInfo(new Locale("", "CW"), 599)); - countryInfoList.add(new CountryInfo(new Locale("", "CY"), 357)); - countryInfoList.add(new CountryInfo(new Locale("", "CZ"), 420)); - countryInfoList.add(new CountryInfo(new Locale("", "DK"), 45)); - countryInfoList.add(new CountryInfo(new Locale("", "DJ"), 253)); - countryInfoList.add(new CountryInfo(new Locale("", "DM"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "DO"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "TL"), 670)); - countryInfoList.add(new CountryInfo(new Locale("", "EC"), 593)); - countryInfoList.add(new CountryInfo(new Locale("", "EG"), 20)); - countryInfoList.add(new CountryInfo(new Locale("", "SV"), 503)); - countryInfoList.add(new CountryInfo(new Locale("", "GQ"), 240)); - countryInfoList.add(new CountryInfo(new Locale("", "ER"), 291)); - countryInfoList.add(new CountryInfo(new Locale("", "EE"), 372)); - countryInfoList.add(new CountryInfo(new Locale("", "ET"), 251)); - countryInfoList.add(new CountryInfo(new Locale("", "FK"), 500)); - countryInfoList.add(new CountryInfo(new Locale("", "FO"), 298)); - countryInfoList.add(new CountryInfo(new Locale("", "FJ"), 679)); - countryInfoList.add(new CountryInfo(new Locale("", "FI"), 358)); - countryInfoList.add(new CountryInfo(new Locale("", "FR"), 33)); - countryInfoList.add(new CountryInfo(new Locale("", "GF"), 594)); - countryInfoList.add(new CountryInfo(new Locale("", "PF"), 689)); - countryInfoList.add(new CountryInfo(new Locale("", "GA"), 241)); - countryInfoList.add(new CountryInfo(new Locale("", "GM"), 220)); - countryInfoList.add(new CountryInfo(new Locale("", "GE"), 995)); - countryInfoList.add(new CountryInfo(new Locale("", "DE"), 49)); - countryInfoList.add(new CountryInfo(new Locale("", "GH"), 233)); - countryInfoList.add(new CountryInfo(new Locale("", "GI"), 350)); - countryInfoList.add(new CountryInfo(new Locale("", "GR"), 30)); - countryInfoList.add(new CountryInfo(new Locale("", "GL"), 299)); - countryInfoList.add(new CountryInfo(new Locale("", "GD"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "GP"), 590)); - countryInfoList.add(new CountryInfo(new Locale("", "GU"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "GT"), 502)); - countryInfoList.add(new CountryInfo(new Locale("", "GG"), 44)); - countryInfoList.add(new CountryInfo(new Locale("", "GN"), 224)); - countryInfoList.add(new CountryInfo(new Locale("", "GW"), 245)); - countryInfoList.add(new CountryInfo(new Locale("", "GY"), 592)); - countryInfoList.add(new CountryInfo(new Locale("", "HT"), 509)); - countryInfoList.add(new CountryInfo(new Locale("", "HM"), 672)); - countryInfoList.add(new CountryInfo(new Locale("", "HN"), 504)); - countryInfoList.add(new CountryInfo(new Locale("", "HK"), 852)); - countryInfoList.add(new CountryInfo(new Locale("", "HU"), 36)); - countryInfoList.add(new CountryInfo(new Locale("", "IS"), 354)); - countryInfoList.add(new CountryInfo(new Locale("", "IN"), 91)); - countryInfoList.add(new CountryInfo(new Locale("", "ID"), 62)); - countryInfoList.add(new CountryInfo(new Locale("", "IR"), 98)); - countryInfoList.add(new CountryInfo(new Locale("", "IQ"), 964)); - countryInfoList.add(new CountryInfo(new Locale("", "IE"), 353)); - countryInfoList.add(new CountryInfo(new Locale("", "IM"), 44)); - countryInfoList.add(new CountryInfo(new Locale("", "IL"), 972)); - countryInfoList.add(new CountryInfo(new Locale("", "IT"), 39)); - countryInfoList.add(new CountryInfo(new Locale("", "JM"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "JP"), 81)); - countryInfoList.add(new CountryInfo(new Locale("", "JE"), 44)); - countryInfoList.add(new CountryInfo(new Locale("", "JO"), 962)); - countryInfoList.add(new CountryInfo(new Locale("", "KZ"), 7)); - countryInfoList.add(new CountryInfo(new Locale("", "KE"), 254)); - countryInfoList.add(new CountryInfo(new Locale("", "KI"), 686)); - countryInfoList.add(new CountryInfo(new Locale("", "XK"), 381)); - countryInfoList.add(new CountryInfo(new Locale("", "KW"), 965)); - countryInfoList.add(new CountryInfo(new Locale("", "KG"), 996)); - countryInfoList.add(new CountryInfo(new Locale("", "LA"), 856)); - countryInfoList.add(new CountryInfo(new Locale("", "LV"), 371)); - countryInfoList.add(new CountryInfo(new Locale("", "LB"), 961)); - countryInfoList.add(new CountryInfo(new Locale("", "LS"), 266)); - countryInfoList.add(new CountryInfo(new Locale("", "LR"), 231)); - countryInfoList.add(new CountryInfo(new Locale("", "LY"), 218)); - countryInfoList.add(new CountryInfo(new Locale("", "LI"), 423)); - countryInfoList.add(new CountryInfo(new Locale("", "LT"), 370)); - countryInfoList.add(new CountryInfo(new Locale("", "LU"), 352)); - countryInfoList.add(new CountryInfo(new Locale("", "MO"), 853)); - countryInfoList.add(new CountryInfo(new Locale("", "MK"), 389)); - countryInfoList.add(new CountryInfo(new Locale("", "MG"), 261)); - countryInfoList.add(new CountryInfo(new Locale("", "MW"), 265)); - countryInfoList.add(new CountryInfo(new Locale("", "MY"), 60)); - countryInfoList.add(new CountryInfo(new Locale("", "MV"), 960)); - countryInfoList.add(new CountryInfo(new Locale("", "ML"), 223)); - countryInfoList.add(new CountryInfo(new Locale("", "MT"), 356)); - countryInfoList.add(new CountryInfo(new Locale("", "MH"), 692)); - countryInfoList.add(new CountryInfo(new Locale("", "MQ"), 596)); - countryInfoList.add(new CountryInfo(new Locale("", "MR"), 222)); - countryInfoList.add(new CountryInfo(new Locale("", "MU"), 230)); - countryInfoList.add(new CountryInfo(new Locale("", "YT"), 262)); - countryInfoList.add(new CountryInfo(new Locale("", "MX"), 52)); - countryInfoList.add(new CountryInfo(new Locale("", "FM"), 691)); - countryInfoList.add(new CountryInfo(new Locale("", "MD"), 373)); - countryInfoList.add(new CountryInfo(new Locale("", "MC"), 377)); - countryInfoList.add(new CountryInfo(new Locale("", "MN"), 976)); - countryInfoList.add(new CountryInfo(new Locale("", "ME"), 382)); - countryInfoList.add(new CountryInfo(new Locale("", "MS"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "MA"), 212)); - countryInfoList.add(new CountryInfo(new Locale("", "MZ"), 258)); - countryInfoList.add(new CountryInfo(new Locale("", "MM"), 95)); - countryInfoList.add(new CountryInfo(new Locale("", "NA"), 264)); - countryInfoList.add(new CountryInfo(new Locale("", "NR"), 674)); - countryInfoList.add(new CountryInfo(new Locale("", "NP"), 977)); - countryInfoList.add(new CountryInfo(new Locale("", "NL"), 31)); - countryInfoList.add(new CountryInfo(new Locale("", "NC"), 687)); - countryInfoList.add(new CountryInfo(new Locale("", "NZ"), 64)); - countryInfoList.add(new CountryInfo(new Locale("", "NI"), 505)); - countryInfoList.add(new CountryInfo(new Locale("", "NE"), 227)); - countryInfoList.add(new CountryInfo(new Locale("", "NG"), 234)); - countryInfoList.add(new CountryInfo(new Locale("", "NU"), 683)); - countryInfoList.add(new CountryInfo(new Locale("", "NF"), 672)); - countryInfoList.add(new CountryInfo(new Locale("", "KP"), 850)); - countryInfoList.add(new CountryInfo(new Locale("", "MP"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "NO"), 47)); - countryInfoList.add(new CountryInfo(new Locale("", "OM"), 968)); - countryInfoList.add(new CountryInfo(new Locale("", "PK"), 92)); - countryInfoList.add(new CountryInfo(new Locale("", "PW"), 680)); - countryInfoList.add(new CountryInfo(new Locale("", "PS"), 970)); - countryInfoList.add(new CountryInfo(new Locale("", "PA"), 507)); - countryInfoList.add(new CountryInfo(new Locale("", "PG"), 675)); - countryInfoList.add(new CountryInfo(new Locale("", "PY"), 595)); - countryInfoList.add(new CountryInfo(new Locale("", "PE"), 51)); - countryInfoList.add(new CountryInfo(new Locale("", "PH"), 63)); - countryInfoList.add(new CountryInfo(new Locale("", "PL"), 48)); - countryInfoList.add(new CountryInfo(new Locale("", "PT"), 351)); - countryInfoList.add(new CountryInfo(new Locale("", "PR"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "QA"), 974)); - countryInfoList.add(new CountryInfo(new Locale("", "RE"), 262)); - countryInfoList.add(new CountryInfo(new Locale("", "RO"), 40)); - countryInfoList.add(new CountryInfo(new Locale("", "RU"), 7)); - countryInfoList.add(new CountryInfo(new Locale("", "RW"), 250)); - countryInfoList.add(new CountryInfo(new Locale("", "BL"), 590)); - countryInfoList.add(new CountryInfo(new Locale("", "SH"), 290)); - countryInfoList.add(new CountryInfo(new Locale("", "KN"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "LC"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "MF"), 590)); - countryInfoList.add(new CountryInfo(new Locale("", "PM"), 508)); - countryInfoList.add(new CountryInfo(new Locale("", "VC"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "WS"), 685)); - countryInfoList.add(new CountryInfo(new Locale("", "SM"), 378)); - countryInfoList.add(new CountryInfo(new Locale("", "ST"), 239)); - countryInfoList.add(new CountryInfo(new Locale("", "SA"), 966)); - countryInfoList.add(new CountryInfo(new Locale("", "SN"), 221)); - countryInfoList.add(new CountryInfo(new Locale("", "RS"), 381)); - countryInfoList.add(new CountryInfo(new Locale("", "SC"), 248)); - countryInfoList.add(new CountryInfo(new Locale("", "SL"), 232)); - countryInfoList.add(new CountryInfo(new Locale("", "SG"), 65)); - countryInfoList.add(new CountryInfo(new Locale("", "SX"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "SK"), 421)); - countryInfoList.add(new CountryInfo(new Locale("", "SI"), 386)); - countryInfoList.add(new CountryInfo(new Locale("", "SB"), 677)); - countryInfoList.add(new CountryInfo(new Locale("", "SO"), 252)); - countryInfoList.add(new CountryInfo(new Locale("", "ZA"), 27)); - countryInfoList.add(new CountryInfo(new Locale("", "GS"), 500)); - countryInfoList.add(new CountryInfo(new Locale("", "KR"), 82)); - countryInfoList.add(new CountryInfo(new Locale("", "SS"), 211)); - countryInfoList.add(new CountryInfo(new Locale("", "ES"), 34)); - countryInfoList.add(new CountryInfo(new Locale("", "LK"), 94)); - countryInfoList.add(new CountryInfo(new Locale("", "SD"), 249)); - countryInfoList.add(new CountryInfo(new Locale("", "SR"), 597)); - countryInfoList.add(new CountryInfo(new Locale("", "SJ"), 47)); - countryInfoList.add(new CountryInfo(new Locale("", "SZ"), 268)); - countryInfoList.add(new CountryInfo(new Locale("", "SE"), 46)); - countryInfoList.add(new CountryInfo(new Locale("", "CH"), 41)); - countryInfoList.add(new CountryInfo(new Locale("", "SY"), 963)); - countryInfoList.add(new CountryInfo(new Locale("", "TW"), 886)); - countryInfoList.add(new CountryInfo(new Locale("", "TJ"), 992)); - countryInfoList.add(new CountryInfo(new Locale("", "TZ"), 255)); - countryInfoList.add(new CountryInfo(new Locale("", "TH"), 66)); - countryInfoList.add(new CountryInfo(new Locale("", "TG"), 228)); - countryInfoList.add(new CountryInfo(new Locale("", "TK"), 690)); - countryInfoList.add(new CountryInfo(new Locale("", "TO"), 676)); - countryInfoList.add(new CountryInfo(new Locale("", "TT"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "TN"), 216)); - countryInfoList.add(new CountryInfo(new Locale("", "TR"), 90)); - countryInfoList.add(new CountryInfo(new Locale("", "TM"), 993)); - countryInfoList.add(new CountryInfo(new Locale("", "TC"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "TV"), 688)); - countryInfoList.add(new CountryInfo(new Locale("", "VI"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "UG"), 256)); - countryInfoList.add(new CountryInfo(new Locale("", "UA"), 380)); - countryInfoList.add(new CountryInfo(new Locale("", "AE"), 971)); - countryInfoList.add(new CountryInfo(new Locale("", "GB"), 44)); - countryInfoList.add(new CountryInfo(new Locale("", "US"), 1)); - countryInfoList.add(new CountryInfo(new Locale("", "UY"), 598)); - countryInfoList.add(new CountryInfo(new Locale("", "UZ"), 998)); - countryInfoList.add(new CountryInfo(new Locale("", "VU"), 678)); - countryInfoList.add(new CountryInfo(new Locale("", "VA"), 379)); - countryInfoList.add(new CountryInfo(new Locale("", "VE"), 58)); - countryInfoList.add(new CountryInfo(new Locale("", "VN"), 84)); - countryInfoList.add(new CountryInfo(new Locale("", "WF"), 681)); - countryInfoList.add(new CountryInfo(new Locale("", "EH"), 212)); - countryInfoList.add(new CountryInfo(new Locale("", "YE"), 967)); - countryInfoList.add(new CountryInfo(new Locale("", "ZM"), 260)); - countryInfoList.add(new CountryInfo(new Locale("", "ZW"), 263)); + final Map countryInfoMap = setupEntireCountrySet(); + List countryInfoList = getAvailableCountryCodes(countryInfoMap); Collections.sort(countryInfoList); return countryInfoList; } + public List getAvailableCountryCodes(Map countryInfoMap) { + if (whitelistedCountryCodes == null && blacklistedCountryCodes == null) { + whitelistedCountryCodes = new ArrayList<>(countryInfoMap.keySet()); + } + + List countryInfoList = new ArrayList<>(); + + Set excludedCountries = new HashSet<>(); + if (whitelistedCountryCodes == null) { + excludedCountries.addAll(blacklistedCountryCodes); + } else { + excludedCountries.addAll(countryInfoMap.keySet()); + excludedCountries.removeAll(whitelistedCountryCodes); + } + + for (String countryCode : countryInfoMap.keySet()) { + if (!excludedCountries.contains(countryCode)) { + countryInfoList.add(new CountryInfo(new Locale("", countryCode), + countryInfoMap.get(countryCode))); + } + } + + return countryInfoList; + } + @Override public void onPostExecute(List result) { if (mListener != null) { mListener.onLoadComplete(result); } } + + public Map setupEntireCountrySet() { + Map countryInfoMap = new HashMap<>(); + countryInfoMap.put("AF", 93); + countryInfoMap.put("AX", 358); + countryInfoMap.put("AL", 355); + countryInfoMap.put("DZ", 213); + countryInfoMap.put("AS", 1); + countryInfoMap.put("AD", 376); + countryInfoMap.put("AO", 244); + countryInfoMap.put("AI", 1); + countryInfoMap.put("AG", 1); + countryInfoMap.put("AR", 54); + countryInfoMap.put("AM", 374); + countryInfoMap.put("AW", 297); + countryInfoMap.put("AC", 247); + countryInfoMap.put("AU", 61); + countryInfoMap.put("AT", 43); + countryInfoMap.put("AZ", 994); + countryInfoMap.put("BS", 1); + countryInfoMap.put("BH", 973); + countryInfoMap.put("BD", 880); + countryInfoMap.put("BB", 1); + countryInfoMap.put("BY", 375); + countryInfoMap.put("BE", 32); + countryInfoMap.put("BZ", 501); + countryInfoMap.put("BJ", 229); + countryInfoMap.put("BM", 1); + countryInfoMap.put("BT", 975); + countryInfoMap.put("BO", 591); + countryInfoMap.put("BA", 387); + countryInfoMap.put("BW", 267); + countryInfoMap.put("BR", 55); + countryInfoMap.put("IO", 246); + countryInfoMap.put("VG", 1); + countryInfoMap.put("BN", 673); + countryInfoMap.put("BG", 359); + countryInfoMap.put("BF", 226); + countryInfoMap.put("BI", 257); + countryInfoMap.put("KH", 855); + countryInfoMap.put("CM", 237); + countryInfoMap.put("CA", 1); + countryInfoMap.put("CV", 238); + countryInfoMap.put("BQ", 599); + countryInfoMap.put("KY", 1); + countryInfoMap.put("CF", 236); + countryInfoMap.put("TD", 235); + countryInfoMap.put("CL", 56); + countryInfoMap.put("CN", 86); + countryInfoMap.put("CX", 61); + countryInfoMap.put("CC", 61); + countryInfoMap.put("CO", 57); + countryInfoMap.put("KM", 269); + countryInfoMap.put("CD", 243); + countryInfoMap.put("CG", 242); + countryInfoMap.put("CK", 682); + countryInfoMap.put("CR", 506); + countryInfoMap.put("CI", 225); + countryInfoMap.put("HR", 385); + countryInfoMap.put("CU", 53); + countryInfoMap.put("CW", 599); + countryInfoMap.put("CY", 357); + countryInfoMap.put("CZ", 420); + countryInfoMap.put("DK", 45); + countryInfoMap.put("DJ", 253); + countryInfoMap.put("DM", 1); + countryInfoMap.put("DO", 1); + countryInfoMap.put("TL", 670); + countryInfoMap.put("EC", 593); + countryInfoMap.put("EG", 20); + countryInfoMap.put("SV", 503); + countryInfoMap.put("GQ", 240); + countryInfoMap.put("ER", 291); + countryInfoMap.put("EE", 372); + countryInfoMap.put("ET", 251); + countryInfoMap.put("FK", 500); + countryInfoMap.put("FO", 298); + countryInfoMap.put("FJ", 679); + countryInfoMap.put("FI", 358); + countryInfoMap.put("FR", 33); + countryInfoMap.put("GF", 594); + countryInfoMap.put("PF", 689); + countryInfoMap.put("GA", 241); + countryInfoMap.put("GM", 220); + countryInfoMap.put("GE", 995); + countryInfoMap.put("DE", 49); + countryInfoMap.put("GH", 233); + countryInfoMap.put("GI", 350); + countryInfoMap.put("GR", 30); + countryInfoMap.put("GL", 299); + countryInfoMap.put("GD", 1); + countryInfoMap.put("GP", 590); + countryInfoMap.put("GU", 1); + countryInfoMap.put("GT", 502); + countryInfoMap.put("GG", 44); + countryInfoMap.put("GN", 224); + countryInfoMap.put("GW", 245); + countryInfoMap.put("GY", 592); + countryInfoMap.put("HT", 509); + countryInfoMap.put("HM", 672); + countryInfoMap.put("HN", 504); + countryInfoMap.put("HK", 852); + countryInfoMap.put("HU", 36); + countryInfoMap.put("IS", 354); + countryInfoMap.put("IN", 91); + countryInfoMap.put("ID", 62); + countryInfoMap.put("IR", 98); + countryInfoMap.put("IQ", 964); + countryInfoMap.put("IE", 353); + countryInfoMap.put("IM", 44); + countryInfoMap.put("IL", 972); + countryInfoMap.put("IT", 39); + countryInfoMap.put("JM", 1); + countryInfoMap.put("JP", 81); + countryInfoMap.put("JE", 44); + countryInfoMap.put("JO", 962); + countryInfoMap.put("KZ", 7); + countryInfoMap.put("KE", 254); + countryInfoMap.put("KI", 686); + countryInfoMap.put("XK", 381); + countryInfoMap.put("KW", 965); + countryInfoMap.put("KG", 996); + countryInfoMap.put("LA", 856); + countryInfoMap.put("LV", 371); + countryInfoMap.put("LB", 961); + countryInfoMap.put("LS", 266); + countryInfoMap.put("LR", 231); + countryInfoMap.put("LY", 218); + countryInfoMap.put("LI", 423); + countryInfoMap.put("LT", 370); + countryInfoMap.put("LU", 352); + countryInfoMap.put("MO", 853); + countryInfoMap.put("MK", 389); + countryInfoMap.put("MG", 261); + countryInfoMap.put("MW", 265); + countryInfoMap.put("MY", 60); + countryInfoMap.put("MV", 960); + countryInfoMap.put("ML", 223); + countryInfoMap.put("MT", 356); + countryInfoMap.put("MH", 692); + countryInfoMap.put("MQ", 596); + countryInfoMap.put("MR", 222); + countryInfoMap.put("MU", 230); + countryInfoMap.put("YT", 262); + countryInfoMap.put("MX", 52); + countryInfoMap.put("FM", 691); + countryInfoMap.put("MD", 373); + countryInfoMap.put("MC", 377); + countryInfoMap.put("MN", 976); + countryInfoMap.put("ME", 382); + countryInfoMap.put("MS", 1); + countryInfoMap.put("MA", 212); + countryInfoMap.put("MZ", 258); + countryInfoMap.put("MM", 95); + countryInfoMap.put("NA", 264); + countryInfoMap.put("NR", 674); + countryInfoMap.put("NP", 977); + countryInfoMap.put("NL", 31); + countryInfoMap.put("NC", 687); + countryInfoMap.put("NZ", 64); + countryInfoMap.put("NI", 505); + countryInfoMap.put("NE", 227); + countryInfoMap.put("NG", 234); + countryInfoMap.put("NU", 683); + countryInfoMap.put("NF", 672); + countryInfoMap.put("KP", 850); + countryInfoMap.put("MP", 1); + countryInfoMap.put("NO", 47); + countryInfoMap.put("OM", 968); + countryInfoMap.put("PK", 92); + countryInfoMap.put("PW", 680); + countryInfoMap.put("PS", 970); + countryInfoMap.put("PA", 507); + countryInfoMap.put("PG", 675); + countryInfoMap.put("PY", 595); + countryInfoMap.put("PE", 51); + countryInfoMap.put("PH", 63); + countryInfoMap.put("PL", 48); + countryInfoMap.put("PT", 351); + countryInfoMap.put("PR", 1); + countryInfoMap.put("QA", 974); + countryInfoMap.put("RE", 262); + countryInfoMap.put("RO", 40); + countryInfoMap.put("RU", 7); + countryInfoMap.put("RW", 250); + countryInfoMap.put("BL", 590); + countryInfoMap.put("SH", 290); + countryInfoMap.put("KN", 1); + countryInfoMap.put("LC", 1); + countryInfoMap.put("MF", 590); + countryInfoMap.put("PM", 508); + countryInfoMap.put("VC", 1); + countryInfoMap.put("WS", 685); + countryInfoMap.put("SM", 378); + countryInfoMap.put("ST", 239); + countryInfoMap.put("SA", 966); + countryInfoMap.put("SN", 221); + countryInfoMap.put("RS", 381); + countryInfoMap.put("SC", 248); + countryInfoMap.put("SL", 232); + countryInfoMap.put("SG", 65); + countryInfoMap.put("SX", 1); + countryInfoMap.put("SK", 421); + countryInfoMap.put("SI", 386); + countryInfoMap.put("SB", 677); + countryInfoMap.put("SO", 252); + countryInfoMap.put("ZA", 27); + countryInfoMap.put("GS", 500); + countryInfoMap.put("KR", 82); + countryInfoMap.put("SS", 211); + countryInfoMap.put("ES", 34); + countryInfoMap.put("LK", 94); + countryInfoMap.put("SD", 249); + countryInfoMap.put("SR", 597); + countryInfoMap.put("SJ", 47); + countryInfoMap.put("SZ", 268); + countryInfoMap.put("SE", 46); + countryInfoMap.put("CH", 41); + countryInfoMap.put("SY", 963); + countryInfoMap.put("TW", 886); + countryInfoMap.put("TJ", 992); + countryInfoMap.put("TZ", 255); + countryInfoMap.put("TH", 66); + countryInfoMap.put("TG", 228); + countryInfoMap.put("TK", 690); + countryInfoMap.put("TO", 676); + countryInfoMap.put("TT", 1); + countryInfoMap.put("TN", 216); + countryInfoMap.put("TR", 90); + countryInfoMap.put("TM", 993); + countryInfoMap.put("TC", 1); + countryInfoMap.put("TV", 688); + countryInfoMap.put("VI", 1); + countryInfoMap.put("UG", 256); + countryInfoMap.put("UA", 380); + countryInfoMap.put("AE", 971); + countryInfoMap.put("GB", 44); + countryInfoMap.put("US", 1); + countryInfoMap.put("UY", 598); + countryInfoMap.put("UZ", 998); + countryInfoMap.put("VU", 678); + countryInfoMap.put("VA", 379); + countryInfoMap.put("VE", 58); + countryInfoMap.put("VN", 84); + countryInfoMap.put("WF", 681); + countryInfoMap.put("EH", 212); + countryInfoMap.put("YE", 967); + countryInfoMap.put("ZM", 260); + countryInfoMap.put("ZW", 263); + return countryInfoMap; + } } diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java index 5d80c5de1..9ca997c14 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java +++ b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java @@ -50,6 +50,9 @@ public final class CountryListSpinner extends AppCompatEditText implements private String mSelectedCountryName; private CountryInfo mSelectedCountryInfo; + private List whitelistedCountryCodes; + private List blacklistedCountryCodes; + public CountryListSpinner(Context context) { this(context, null, android.R.attr.spinnerStyle); } @@ -116,7 +119,7 @@ public void setSelectedForCountry(final Locale locale, String countryCode) { } public CountryInfo getSelectedCountryInfo() { - return mSelectedCountryInfo; + return mSelectedCountryInfo; } @Override @@ -145,7 +148,9 @@ public void onClick(View view) { } private void loadCountryList() { - new CountryListLoadTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + new CountryListLoadTask(this, + whitelistedCountryCodes, + blacklistedCountryCodes).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } private void executeUserClickListener(View view) { @@ -154,6 +159,14 @@ private void executeUserClickListener(View view) { } } + public void setWhitelistedCountryCodes(List whitelistedCountryCodes) { + this.whitelistedCountryCodes = whitelistedCountryCodes; + } + + public void setBlacklistedCountryCodes(List blacklistedCountryCodes) { + this.blacklistedCountryCodes = blacklistedCountryCodes; + } + @Override public void onLoadComplete(List result) { mCountryListAdapter.setData(result); diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java b/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java index 721632025..1e5f1c126 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java +++ b/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java @@ -48,6 +48,7 @@ import com.google.android.gms.auth.api.credentials.CredentialPickerConfig; import com.google.android.gms.auth.api.credentials.HintRequest; +import java.util.List; import java.util.Locale; /** @@ -134,6 +135,8 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { // It is assumed that the phone number that are being wired in via Credential Selector // are e164 since we store it. Bundle params = getArguments().getBundle(ExtraConstants.PARAMS); + List whitelistedCountryCodes = null; + List blacklistedCountryCodes = null; String phone = null; String countryIso = null; String nationalNumber = null; @@ -219,6 +222,14 @@ private String getPseudoValidPhoneNumber() { } private void setupCountrySpinner() { + Bundle params = getArguments().getBundle(ExtraConstants.PARAMS); + if (params != null) { + mCountryListSpinner.setWhitelistedCountryCodes( + params.getStringArrayList(ExtraConstants.WHITELISTED_COUNTRY_CODES)); + mCountryListSpinner.setBlacklistedCountryCodes( + params.getStringArrayList(ExtraConstants.BLACKLISTED_COUNTRY_CODES)); + } + //clear error when spinner is clicked on mCountryListSpinner.setOnClickListener(new View.OnClickListener() { @Override diff --git a/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java b/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java index 7e2035a0f..7dec6571d 100644 --- a/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java +++ b/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java @@ -37,6 +37,9 @@ public final class ExtraConstants { public static final String COUNTRY_ISO = "extra_country_iso"; public static final String NATIONAL_NUMBER = "extra_national_number"; + public static final String WHITELISTED_COUNTRY_CODES = "whitelisted_country_codes"; + public static final String BLACKLISTED_COUNTRY_CODES = "blacklisted_country_codes"; + private ExtraConstants() { throw new AssertionError("No instance for you!"); } diff --git a/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java b/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java index d189f6b30..51c86f724 100644 --- a/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java +++ b/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java @@ -295,13 +295,13 @@ public class CountryListLoadTaskTests { @Before public void setUp() { - // Create task and mock dependencies mListener = mock(CountryListLoadTask.Listener.class); - mTask = new CountryListLoadTask(mListener); } @Test - public void testExecute() { + public void testExecute_withoutUserInput_expectAllCountriesAdded() { + mTask = new CountryListLoadTask(mListener, null, null); + mTask.execute(); try { @@ -315,9 +315,56 @@ public void testExecute() { } } + + @Test + public void testExecute_withUserWhitelistInput_expectUserCountriesAddedOnly() { + List whitelistedCountryCodes = new ArrayList<>(); + whitelistedCountryCodes.add("US"); + + List expectedOutput = new ArrayList<>(); + expectedOutput.add(new CountryInfo(new Locale("", "US"), 1)); + + mTask = new CountryListLoadTask(mListener, whitelistedCountryCodes, null); + + mTask.execute(); + + try { + final List result = mTask.get(); + Collections.sort(expectedOutput); + assertEquals(expectedOutput, result); + } catch (InterruptedException e) { + fail("Should not throw InterruptedException"); + } catch (ExecutionException e) { + fail("Should not throw ExecutionException"); + } + } + + @Test + public void testExecute_withUserBlacklistInput_expectUserCountriesAddedOnly() { + List blacklistedCountryCodes = new ArrayList<>(); + blacklistedCountryCodes.add("US"); + + List expectedOutput = new ArrayList<>(COUNTRY_LIST); + expectedOutput.remove(new CountryInfo(new Locale("", "US"), 1)); + + mTask = new CountryListLoadTask(mListener, null, blacklistedCountryCodes); + + mTask.execute(); + + try { + final List result = mTask.get(); + Collections.sort(expectedOutput); + assertEquals(expectedOutput, result); + } catch (InterruptedException e) { + fail("Should not throw InterruptedException"); + } catch (ExecutionException e) { + fail("Should not throw ExecutionException"); + } + } + @Test public void testOnPostExecute_nullListener() { - mTask = new CountryListLoadTask(null); + mTask = new CountryListLoadTask(null, null, null); try { mTask.onPostExecute(COUNTRY_LIST); } catch (NullPointerException ex) { @@ -327,6 +374,7 @@ public void testOnPostExecute_nullListener() { @Test public void testOnPostExecute() { + mTask = new CountryListLoadTask(mListener, null, null); mTask.onPostExecute(COUNTRY_LIST); verify(mListener).onLoadComplete(COUNTRY_LIST); } From 38d3ab0c0a46a1a7716563b8254a22b57f1e0864 Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Thu, 24 May 2018 17:49:04 -0700 Subject: [PATCH 02/11] Small fix --- auth/src/main/java/com/firebase/ui/auth/AuthUI.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java index eec85178c..221654d55 100644 --- a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java +++ b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java @@ -664,7 +664,7 @@ public PhoneBuilder setDefaultCountryIso(@NonNull String iso) { * be whitelisted */ public PhoneBuilder setWhitelistedPhoneAuthCountryCodes( - @Nullable List whitelistedPhoneAuthCountryCodes) { + @NonNull List whitelistedPhoneAuthCountryCodes) { if (getParams().containsKey(ExtraConstants.BLACKLISTED_COUNTRY_CODES)) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + @@ -689,7 +689,7 @@ public PhoneBuilder setWhitelistedPhoneAuthCountryCodes( * be whitelisted */ public PhoneBuilder setBlacklistedPhoneAuthCountryCodes( - @Nullable List blacklistedPhoneAuthCountryCodes) { + @NonNull List blacklistedPhoneAuthCountryCodes) { if (getParams().containsKey(ExtraConstants.WHITELISTED_COUNTRY_CODES)) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + From 06fba95d905d37b593a9d3f3f09a6e3b579d3028 Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Fri, 25 May 2018 09:59:35 -0700 Subject: [PATCH 03/11] Removes redundant country code map --- .../auth/data/client/CountryListLoadTask.java | 259 +----------------- .../ui/auth/util/data/PhoneNumberUtils.java | 4 + 2 files changed, 8 insertions(+), 255 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java index a3310afad..0fb8107e7 100644 --- a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java +++ b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java @@ -22,10 +22,10 @@ import android.support.annotation.RestrictTo; import com.firebase.ui.auth.data.model.CountryInfo; +import com.firebase.ui.auth.util.data.PhoneNumberUtils; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -57,13 +57,13 @@ public CountryListLoadTask(Listener listener, @Override protected List doInBackground(Void... params) { - final Map countryInfoMap = setupEntireCountrySet(); - List countryInfoList = getAvailableCountryCodes(countryInfoMap); + List countryInfoList = getAvailableCountryCodes(); Collections.sort(countryInfoList); return countryInfoList; } - public List getAvailableCountryCodes(Map countryInfoMap) { + public List getAvailableCountryCodes() { + Map countryInfoMap = PhoneNumberUtils.getCountryCodeMap(); if (whitelistedCountryCodes == null && blacklistedCountryCodes == null) { whitelistedCountryCodes = new ArrayList<>(countryInfoMap.keySet()); } @@ -94,255 +94,4 @@ public void onPostExecute(List result) { mListener.onLoadComplete(result); } } - - public Map setupEntireCountrySet() { - Map countryInfoMap = new HashMap<>(); - countryInfoMap.put("AF", 93); - countryInfoMap.put("AX", 358); - countryInfoMap.put("AL", 355); - countryInfoMap.put("DZ", 213); - countryInfoMap.put("AS", 1); - countryInfoMap.put("AD", 376); - countryInfoMap.put("AO", 244); - countryInfoMap.put("AI", 1); - countryInfoMap.put("AG", 1); - countryInfoMap.put("AR", 54); - countryInfoMap.put("AM", 374); - countryInfoMap.put("AW", 297); - countryInfoMap.put("AC", 247); - countryInfoMap.put("AU", 61); - countryInfoMap.put("AT", 43); - countryInfoMap.put("AZ", 994); - countryInfoMap.put("BS", 1); - countryInfoMap.put("BH", 973); - countryInfoMap.put("BD", 880); - countryInfoMap.put("BB", 1); - countryInfoMap.put("BY", 375); - countryInfoMap.put("BE", 32); - countryInfoMap.put("BZ", 501); - countryInfoMap.put("BJ", 229); - countryInfoMap.put("BM", 1); - countryInfoMap.put("BT", 975); - countryInfoMap.put("BO", 591); - countryInfoMap.put("BA", 387); - countryInfoMap.put("BW", 267); - countryInfoMap.put("BR", 55); - countryInfoMap.put("IO", 246); - countryInfoMap.put("VG", 1); - countryInfoMap.put("BN", 673); - countryInfoMap.put("BG", 359); - countryInfoMap.put("BF", 226); - countryInfoMap.put("BI", 257); - countryInfoMap.put("KH", 855); - countryInfoMap.put("CM", 237); - countryInfoMap.put("CA", 1); - countryInfoMap.put("CV", 238); - countryInfoMap.put("BQ", 599); - countryInfoMap.put("KY", 1); - countryInfoMap.put("CF", 236); - countryInfoMap.put("TD", 235); - countryInfoMap.put("CL", 56); - countryInfoMap.put("CN", 86); - countryInfoMap.put("CX", 61); - countryInfoMap.put("CC", 61); - countryInfoMap.put("CO", 57); - countryInfoMap.put("KM", 269); - countryInfoMap.put("CD", 243); - countryInfoMap.put("CG", 242); - countryInfoMap.put("CK", 682); - countryInfoMap.put("CR", 506); - countryInfoMap.put("CI", 225); - countryInfoMap.put("HR", 385); - countryInfoMap.put("CU", 53); - countryInfoMap.put("CW", 599); - countryInfoMap.put("CY", 357); - countryInfoMap.put("CZ", 420); - countryInfoMap.put("DK", 45); - countryInfoMap.put("DJ", 253); - countryInfoMap.put("DM", 1); - countryInfoMap.put("DO", 1); - countryInfoMap.put("TL", 670); - countryInfoMap.put("EC", 593); - countryInfoMap.put("EG", 20); - countryInfoMap.put("SV", 503); - countryInfoMap.put("GQ", 240); - countryInfoMap.put("ER", 291); - countryInfoMap.put("EE", 372); - countryInfoMap.put("ET", 251); - countryInfoMap.put("FK", 500); - countryInfoMap.put("FO", 298); - countryInfoMap.put("FJ", 679); - countryInfoMap.put("FI", 358); - countryInfoMap.put("FR", 33); - countryInfoMap.put("GF", 594); - countryInfoMap.put("PF", 689); - countryInfoMap.put("GA", 241); - countryInfoMap.put("GM", 220); - countryInfoMap.put("GE", 995); - countryInfoMap.put("DE", 49); - countryInfoMap.put("GH", 233); - countryInfoMap.put("GI", 350); - countryInfoMap.put("GR", 30); - countryInfoMap.put("GL", 299); - countryInfoMap.put("GD", 1); - countryInfoMap.put("GP", 590); - countryInfoMap.put("GU", 1); - countryInfoMap.put("GT", 502); - countryInfoMap.put("GG", 44); - countryInfoMap.put("GN", 224); - countryInfoMap.put("GW", 245); - countryInfoMap.put("GY", 592); - countryInfoMap.put("HT", 509); - countryInfoMap.put("HM", 672); - countryInfoMap.put("HN", 504); - countryInfoMap.put("HK", 852); - countryInfoMap.put("HU", 36); - countryInfoMap.put("IS", 354); - countryInfoMap.put("IN", 91); - countryInfoMap.put("ID", 62); - countryInfoMap.put("IR", 98); - countryInfoMap.put("IQ", 964); - countryInfoMap.put("IE", 353); - countryInfoMap.put("IM", 44); - countryInfoMap.put("IL", 972); - countryInfoMap.put("IT", 39); - countryInfoMap.put("JM", 1); - countryInfoMap.put("JP", 81); - countryInfoMap.put("JE", 44); - countryInfoMap.put("JO", 962); - countryInfoMap.put("KZ", 7); - countryInfoMap.put("KE", 254); - countryInfoMap.put("KI", 686); - countryInfoMap.put("XK", 381); - countryInfoMap.put("KW", 965); - countryInfoMap.put("KG", 996); - countryInfoMap.put("LA", 856); - countryInfoMap.put("LV", 371); - countryInfoMap.put("LB", 961); - countryInfoMap.put("LS", 266); - countryInfoMap.put("LR", 231); - countryInfoMap.put("LY", 218); - countryInfoMap.put("LI", 423); - countryInfoMap.put("LT", 370); - countryInfoMap.put("LU", 352); - countryInfoMap.put("MO", 853); - countryInfoMap.put("MK", 389); - countryInfoMap.put("MG", 261); - countryInfoMap.put("MW", 265); - countryInfoMap.put("MY", 60); - countryInfoMap.put("MV", 960); - countryInfoMap.put("ML", 223); - countryInfoMap.put("MT", 356); - countryInfoMap.put("MH", 692); - countryInfoMap.put("MQ", 596); - countryInfoMap.put("MR", 222); - countryInfoMap.put("MU", 230); - countryInfoMap.put("YT", 262); - countryInfoMap.put("MX", 52); - countryInfoMap.put("FM", 691); - countryInfoMap.put("MD", 373); - countryInfoMap.put("MC", 377); - countryInfoMap.put("MN", 976); - countryInfoMap.put("ME", 382); - countryInfoMap.put("MS", 1); - countryInfoMap.put("MA", 212); - countryInfoMap.put("MZ", 258); - countryInfoMap.put("MM", 95); - countryInfoMap.put("NA", 264); - countryInfoMap.put("NR", 674); - countryInfoMap.put("NP", 977); - countryInfoMap.put("NL", 31); - countryInfoMap.put("NC", 687); - countryInfoMap.put("NZ", 64); - countryInfoMap.put("NI", 505); - countryInfoMap.put("NE", 227); - countryInfoMap.put("NG", 234); - countryInfoMap.put("NU", 683); - countryInfoMap.put("NF", 672); - countryInfoMap.put("KP", 850); - countryInfoMap.put("MP", 1); - countryInfoMap.put("NO", 47); - countryInfoMap.put("OM", 968); - countryInfoMap.put("PK", 92); - countryInfoMap.put("PW", 680); - countryInfoMap.put("PS", 970); - countryInfoMap.put("PA", 507); - countryInfoMap.put("PG", 675); - countryInfoMap.put("PY", 595); - countryInfoMap.put("PE", 51); - countryInfoMap.put("PH", 63); - countryInfoMap.put("PL", 48); - countryInfoMap.put("PT", 351); - countryInfoMap.put("PR", 1); - countryInfoMap.put("QA", 974); - countryInfoMap.put("RE", 262); - countryInfoMap.put("RO", 40); - countryInfoMap.put("RU", 7); - countryInfoMap.put("RW", 250); - countryInfoMap.put("BL", 590); - countryInfoMap.put("SH", 290); - countryInfoMap.put("KN", 1); - countryInfoMap.put("LC", 1); - countryInfoMap.put("MF", 590); - countryInfoMap.put("PM", 508); - countryInfoMap.put("VC", 1); - countryInfoMap.put("WS", 685); - countryInfoMap.put("SM", 378); - countryInfoMap.put("ST", 239); - countryInfoMap.put("SA", 966); - countryInfoMap.put("SN", 221); - countryInfoMap.put("RS", 381); - countryInfoMap.put("SC", 248); - countryInfoMap.put("SL", 232); - countryInfoMap.put("SG", 65); - countryInfoMap.put("SX", 1); - countryInfoMap.put("SK", 421); - countryInfoMap.put("SI", 386); - countryInfoMap.put("SB", 677); - countryInfoMap.put("SO", 252); - countryInfoMap.put("ZA", 27); - countryInfoMap.put("GS", 500); - countryInfoMap.put("KR", 82); - countryInfoMap.put("SS", 211); - countryInfoMap.put("ES", 34); - countryInfoMap.put("LK", 94); - countryInfoMap.put("SD", 249); - countryInfoMap.put("SR", 597); - countryInfoMap.put("SJ", 47); - countryInfoMap.put("SZ", 268); - countryInfoMap.put("SE", 46); - countryInfoMap.put("CH", 41); - countryInfoMap.put("SY", 963); - countryInfoMap.put("TW", 886); - countryInfoMap.put("TJ", 992); - countryInfoMap.put("TZ", 255); - countryInfoMap.put("TH", 66); - countryInfoMap.put("TG", 228); - countryInfoMap.put("TK", 690); - countryInfoMap.put("TO", 676); - countryInfoMap.put("TT", 1); - countryInfoMap.put("TN", 216); - countryInfoMap.put("TR", 90); - countryInfoMap.put("TM", 993); - countryInfoMap.put("TC", 1); - countryInfoMap.put("TV", 688); - countryInfoMap.put("VI", 1); - countryInfoMap.put("UG", 256); - countryInfoMap.put("UA", 380); - countryInfoMap.put("AE", 971); - countryInfoMap.put("GB", 44); - countryInfoMap.put("US", 1); - countryInfoMap.put("UY", 598); - countryInfoMap.put("UZ", 998); - countryInfoMap.put("VU", 678); - countryInfoMap.put("VA", 379); - countryInfoMap.put("VE", 58); - countryInfoMap.put("VN", 84); - countryInfoMap.put("WF", 681); - countryInfoMap.put("EH", 212); - countryInfoMap.put("YE", 967); - countryInfoMap.put("ZM", 260); - countryInfoMap.put("ZW", 263); - return countryInfoMap; - } } diff --git a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java index 704bb3060..07d3873d9 100644 --- a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java +++ b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java @@ -155,6 +155,10 @@ public static Integer getCountryCode(String countryIso) { ? null : COUNTRY_TO_ISO_CODES.get(countryIso.toUpperCase(Locale.getDefault())); } + public static Map getCountryCodeMap() { + return COUNTRY_TO_ISO_CODES; + } + private static String getCountryIsoForCountryCode(String countryCode) { List countries = COUNTRY_TO_REGION_CODES.get(Integer.parseInt(countryCode)); if (countries != null) { From e68c2b880f7b1b5c81ca88c1de352aeb2c1a9f7d Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Fri, 25 May 2018 10:23:23 -0700 Subject: [PATCH 04/11] Small change --- .../com/firebase/ui/auth/data/client/CountryListLoadTask.java | 2 +- .../java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java index 0fb8107e7..971491f76 100644 --- a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java +++ b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java @@ -63,7 +63,7 @@ protected List doInBackground(Void... params) { } public List getAvailableCountryCodes() { - Map countryInfoMap = PhoneNumberUtils.getCountryCodeMap(); + Map countryInfoMap = PhoneNumberUtils.getImmutableCountryCodeMap(); if (whitelistedCountryCodes == null && blacklistedCountryCodes == null) { whitelistedCountryCodes = new ArrayList<>(countryInfoMap.keySet()); } diff --git a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java index 07d3873d9..08dc83617 100644 --- a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java +++ b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java @@ -155,7 +155,7 @@ public static Integer getCountryCode(String countryIso) { ? null : COUNTRY_TO_ISO_CODES.get(countryIso.toUpperCase(Locale.getDefault())); } - public static Map getCountryCodeMap() { + public static Map getImmutableCountryCodeMap() { return COUNTRY_TO_ISO_CODES; } From f34fce4a0c52d15ebbdc6fd10149f1badf78a9af Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Fri, 25 May 2018 12:22:30 -0700 Subject: [PATCH 05/11] Renames CountryCodes to CountryIsos --- .../java/com/firebase/ui/auth/AuthUI.java | 62 +++++++++---------- .../auth/data/client/CountryListLoadTask.java | 36 +++++------ .../ui/auth/ui/phone/CountryListSpinner.java | 16 ++--- .../ui/phone/VerifyPhoneNumberFragment.java | 11 ++-- .../firebase/ui/auth/util/ExtraConstants.java | 4 +- .../ui/auth/util/data/PhoneNumberUtils.java | 2 +- .../ui/phone/CountryListLoadTaskTests.java | 12 ++-- 7 files changed, 70 insertions(+), 73 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java index 221654d55..e8d6881d6 100644 --- a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java +++ b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java @@ -654,61 +654,61 @@ public PhoneBuilder setDefaultCountryIso(@NonNull String iso) { /** * Sets the country codes available in the country code selector for phone * authentication. This is not to be called with - * {@link #setBlacklistedPhoneAuthCountryCodes(List)}. + * {@link #setBlacklistedPhoneAuthCountryIsos(List)}. * If both are called, an exception will be thrown. *

* For a list of country codes, see Alpha-2 codes here: * http://www.nationsonline.org/oneworld/country_code_list.htm * - * @param whitelistedPhoneAuthCountryCodes a case insensitive list of country codes to + * @param whitelistedPhoneAuthCountryIsos a case insensitive list of country codes to * be whitelisted */ - public PhoneBuilder setWhitelistedPhoneAuthCountryCodes( - @NonNull List whitelistedPhoneAuthCountryCodes) { - if (getParams().containsKey(ExtraConstants.BLACKLISTED_COUNTRY_CODES)) { + public PhoneBuilder setWhitelistedPhoneAuthCountryIsos( + @NonNull List whitelistedPhoneAuthCountryIsos) { + if (getParams().containsKey(ExtraConstants.BLACKLISTED_COUNTRY_ISOS)) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + "authentication."); } - addCountryCodesToBundle(whitelistedPhoneAuthCountryCodes, - ExtraConstants.WHITELISTED_COUNTRY_CODES); + addCountryIsosToBundle(whitelistedPhoneAuthCountryIsos, + ExtraConstants.WHITELISTED_COUNTRY_ISOS); return this; } /** * Sets the country codes to be removed from the country code selector for phone * authentication. This is not to be called with - * {@link #setWhitelistedPhoneAuthCountryCodes(List)}. + * {@link #setWhitelistedPhoneAuthCountryIsos(List)}. * If both are called, an exception will be thrown. *

* For a list of country codes, see Alpha-2 codes here: * https://en.wikipedia.org/wiki/ISO_3166-1 * - * @param blacklistedPhoneAuthCountryCodes a case insensitive list of country codes to + * @param blacklistedPhoneAuthCountryIsos a case insensitive list of country codes to * be whitelisted */ - public PhoneBuilder setBlacklistedPhoneAuthCountryCodes( - @NonNull List blacklistedPhoneAuthCountryCodes) { - if (getParams().containsKey(ExtraConstants.WHITELISTED_COUNTRY_CODES)) { + public PhoneBuilder setBlacklistedPhoneAuthCountryIsos( + @NonNull List blacklistedPhoneAuthCountryIsos) { + if (getParams().containsKey(ExtraConstants.WHITELISTED_COUNTRY_ISOS)) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + "authentication."); } - addCountryCodesToBundle(blacklistedPhoneAuthCountryCodes, - ExtraConstants.BLACKLISTED_COUNTRY_CODES); + addCountryIsosToBundle(blacklistedPhoneAuthCountryIsos, + ExtraConstants.BLACKLISTED_COUNTRY_ISOS); return this; } - private void addCountryCodesToBundle(List countryCodes, - String countryCodeType) { + private void addCountryIsosToBundle(List CountryIsos, + String CountryIsoType) { ArrayList uppercaseCodes = new ArrayList<>(); - for (String code : countryCodes) { + for (String code : CountryIsos) { uppercaseCodes.add(code.toUpperCase(Locale.getDefault())); } - getParams().putStringArrayList(countryCodeType, uppercaseCodes); + getParams().putStringArrayList(CountryIsoType, uppercaseCodes); } @Override @@ -718,27 +718,27 @@ public IdpConfig build() { } private void validateInputs() { - List whitelistedCountryCodes = getParams().getStringArrayList( - ExtraConstants.WHITELISTED_COUNTRY_CODES); - List blacklistedCountryCodes = getParams().getStringArrayList( - ExtraConstants.BLACKLISTED_COUNTRY_CODES); + List whitelistedCountryIsos = getParams().getStringArrayList( + ExtraConstants.WHITELISTED_COUNTRY_ISOS); + List blacklistedCountryIsos = getParams().getStringArrayList( + ExtraConstants.BLACKLISTED_COUNTRY_ISOS); - if (whitelistedCountryCodes != null && - blacklistedCountryCodes != null) { + if (whitelistedCountryIsos != null && + blacklistedCountryIsos != null) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + "authentication."); - } else if (whitelistedCountryCodes != null) { - validateCountryCodes(whitelistedCountryCodes); - validateDefaultCountryCode(whitelistedCountryCodes, true); + } else if (whitelistedCountryIsos != null) { + validateCountryIsos(whitelistedCountryIsos); + validateDefaultCountryCode(whitelistedCountryIsos, true); - } else if (blacklistedCountryCodes != null) { - validateCountryCodes(blacklistedCountryCodes); - validateDefaultCountryCode(blacklistedCountryCodes, false); + } else if (blacklistedCountryIsos != null) { + validateCountryIsos(blacklistedCountryIsos); + validateDefaultCountryCode(blacklistedCountryIsos, false); } } - private void validateCountryCodes(List codes) { + private void validateCountryIsos(List codes) { if (codes == null) { return; } diff --git a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java index 971491f76..c2cad7df6 100644 --- a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java +++ b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java @@ -44,44 +44,44 @@ public interface Listener { private final Listener mListener; - List whitelistedCountryCodes; - List blacklistedCountryCodes; + List whitelistedCountryIsos; + List blacklistedCountryIsos; public CountryListLoadTask(Listener listener, - List whitelistedCountryCodes, - List blacklistedCountryCodes) { + List whitelistedCountryIsos, + List blacklistedCountryIsos) { mListener = listener; - this.whitelistedCountryCodes = whitelistedCountryCodes; - this.blacklistedCountryCodes = blacklistedCountryCodes; + this.whitelistedCountryIsos = whitelistedCountryIsos; + this.blacklistedCountryIsos = blacklistedCountryIsos; } @Override protected List doInBackground(Void... params) { - List countryInfoList = getAvailableCountryCodes(); + List countryInfoList = getAvailableCountryIsos(); Collections.sort(countryInfoList); return countryInfoList; } - public List getAvailableCountryCodes() { - Map countryInfoMap = PhoneNumberUtils.getImmutableCountryCodeMap(); - if (whitelistedCountryCodes == null && blacklistedCountryCodes == null) { - whitelistedCountryCodes = new ArrayList<>(countryInfoMap.keySet()); + public List getAvailableCountryIsos() { + Map countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap(); + if (whitelistedCountryIsos == null && blacklistedCountryIsos == null) { + whitelistedCountryIsos = new ArrayList<>(countryInfoMap.keySet()); } List countryInfoList = new ArrayList<>(); Set excludedCountries = new HashSet<>(); - if (whitelistedCountryCodes == null) { - excludedCountries.addAll(blacklistedCountryCodes); + if (whitelistedCountryIsos == null) { + excludedCountries.addAll(blacklistedCountryIsos); } else { excludedCountries.addAll(countryInfoMap.keySet()); - excludedCountries.removeAll(whitelistedCountryCodes); + excludedCountries.removeAll(whitelistedCountryIsos); } - for (String countryCode : countryInfoMap.keySet()) { - if (!excludedCountries.contains(countryCode)) { - countryInfoList.add(new CountryInfo(new Locale("", countryCode), - countryInfoMap.get(countryCode))); + for (String countryIso : countryInfoMap.keySet()) { + if (!excludedCountries.contains(countryIso)) { + countryInfoList.add(new CountryInfo(new Locale("", countryIso), + countryInfoMap.get(countryIso))); } } diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java index 9ca997c14..bcea521f1 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java +++ b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java @@ -50,8 +50,8 @@ public final class CountryListSpinner extends AppCompatEditText implements private String mSelectedCountryName; private CountryInfo mSelectedCountryInfo; - private List whitelistedCountryCodes; - private List blacklistedCountryCodes; + private List whitelistedCountryIsos; + private List blacklistedCountryIsos; public CountryListSpinner(Context context) { this(context, null, android.R.attr.spinnerStyle); @@ -149,8 +149,8 @@ public void onClick(View view) { private void loadCountryList() { new CountryListLoadTask(this, - whitelistedCountryCodes, - blacklistedCountryCodes).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + whitelistedCountryIsos, + blacklistedCountryIsos).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } private void executeUserClickListener(View view) { @@ -159,12 +159,12 @@ private void executeUserClickListener(View view) { } } - public void setWhitelistedCountryCodes(List whitelistedCountryCodes) { - this.whitelistedCountryCodes = whitelistedCountryCodes; + public void setWhitelistedCountryIsos(List whitelistedCountryIsos) { + this.whitelistedCountryIsos = whitelistedCountryIsos; } - public void setBlacklistedCountryCodes(List blacklistedCountryCodes) { - this.blacklistedCountryCodes = blacklistedCountryCodes; + public void setBlacklistedCountryIsos(List blacklistedCountryIsos) { + this.blacklistedCountryIsos = blacklistedCountryIsos; } @Override diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java b/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java index 1e5f1c126..7318f1ce7 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java +++ b/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java @@ -48,7 +48,6 @@ import com.google.android.gms.auth.api.credentials.CredentialPickerConfig; import com.google.android.gms.auth.api.credentials.HintRequest; -import java.util.List; import java.util.Locale; /** @@ -135,8 +134,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { // It is assumed that the phone number that are being wired in via Credential Selector // are e164 since we store it. Bundle params = getArguments().getBundle(ExtraConstants.PARAMS); - List whitelistedCountryCodes = null; - List blacklistedCountryCodes = null; String phone = null; String countryIso = null; String nationalNumber = null; @@ -224,10 +221,10 @@ private String getPseudoValidPhoneNumber() { private void setupCountrySpinner() { Bundle params = getArguments().getBundle(ExtraConstants.PARAMS); if (params != null) { - mCountryListSpinner.setWhitelistedCountryCodes( - params.getStringArrayList(ExtraConstants.WHITELISTED_COUNTRY_CODES)); - mCountryListSpinner.setBlacklistedCountryCodes( - params.getStringArrayList(ExtraConstants.BLACKLISTED_COUNTRY_CODES)); + mCountryListSpinner.setWhitelistedCountryIsos( + params.getStringArrayList(ExtraConstants.WHITELISTED_COUNTRY_ISOS)); + mCountryListSpinner.setBlacklistedCountryIsos( + params.getStringArrayList(ExtraConstants.BLACKLISTED_COUNTRY_ISOS)); } //clear error when spinner is clicked on diff --git a/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java b/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java index 7dec6571d..15980854e 100644 --- a/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java +++ b/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java @@ -37,8 +37,8 @@ public final class ExtraConstants { public static final String COUNTRY_ISO = "extra_country_iso"; public static final String NATIONAL_NUMBER = "extra_national_number"; - public static final String WHITELISTED_COUNTRY_CODES = "whitelisted_country_codes"; - public static final String BLACKLISTED_COUNTRY_CODES = "blacklisted_country_codes"; + public static final String WHITELISTED_COUNTRY_ISOS = "whitelisted_country_isos"; + public static final String BLACKLISTED_COUNTRY_ISOS = "blacklisted_country_isos"; private ExtraConstants() { throw new AssertionError("No instance for you!"); diff --git a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java index 08dc83617..6f18348fa 100644 --- a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java +++ b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java @@ -155,7 +155,7 @@ public static Integer getCountryCode(String countryIso) { ? null : COUNTRY_TO_ISO_CODES.get(countryIso.toUpperCase(Locale.getDefault())); } - public static Map getImmutableCountryCodeMap() { + public static Map getImmutableCountryIsoMap() { return COUNTRY_TO_ISO_CODES; } diff --git a/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java b/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java index 51c86f724..ea39d8ca7 100644 --- a/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java +++ b/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java @@ -318,13 +318,13 @@ public void testExecute_withoutUserInput_expectAllCountriesAdded() { @Test public void testExecute_withUserWhitelistInput_expectUserCountriesAddedOnly() { - List whitelistedCountryCodes = new ArrayList<>(); - whitelistedCountryCodes.add("US"); + List whitelistedCountryIsos = new ArrayList<>(); + whitelistedCountryIsos.add("US"); List expectedOutput = new ArrayList<>(); expectedOutput.add(new CountryInfo(new Locale("", "US"), 1)); - mTask = new CountryListLoadTask(mListener, whitelistedCountryCodes, null); + mTask = new CountryListLoadTask(mListener, whitelistedCountryIsos, null); mTask.execute(); @@ -341,13 +341,13 @@ public void testExecute_withUserWhitelistInput_expectUserCountriesAddedOnly() { @Test public void testExecute_withUserBlacklistInput_expectUserCountriesAddedOnly() { - List blacklistedCountryCodes = new ArrayList<>(); - blacklistedCountryCodes.add("US"); + List blacklistedCountryIsos = new ArrayList<>(); + blacklistedCountryIsos.add("US"); List expectedOutput = new ArrayList<>(COUNTRY_LIST); expectedOutput.remove(new CountryInfo(new Locale("", "US"), 1)); - mTask = new CountryListLoadTask(mListener, null, blacklistedCountryCodes); + mTask = new CountryListLoadTask(mListener, null, blacklistedCountryIsos); mTask.execute(); From be019cc8098da00195b2a36482fe8219b73e2713 Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Fri, 25 May 2018 12:40:19 -0700 Subject: [PATCH 06/11] Adds code review suggestions --- .../auth/data/client/CountryListLoadTask.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java index c2cad7df6..bebb95526 100644 --- a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java +++ b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java @@ -44,6 +44,7 @@ public interface Listener { private final Listener mListener; + Map countryInfoMap; List whitelistedCountryIsos; List blacklistedCountryIsos; @@ -51,8 +52,14 @@ public CountryListLoadTask(Listener listener, List whitelistedCountryIsos, List blacklistedCountryIsos) { mListener = listener; - this.whitelistedCountryIsos = whitelistedCountryIsos; - this.blacklistedCountryIsos = blacklistedCountryIsos; + countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap(); + + if (whitelistedCountryIsos == null && blacklistedCountryIsos == null) { + this.whitelistedCountryIsos = new ArrayList<>(countryInfoMap.keySet()); + } else { + this.whitelistedCountryIsos = whitelistedCountryIsos; + this.blacklistedCountryIsos = blacklistedCountryIsos; + } } @Override @@ -63,21 +70,23 @@ protected List doInBackground(Void... params) { } public List getAvailableCountryIsos() { - Map countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap(); - if (whitelistedCountryIsos == null && blacklistedCountryIsos == null) { - whitelistedCountryIsos = new ArrayList<>(countryInfoMap.keySet()); - } - List countryInfoList = new ArrayList<>(); + // At this point either whitelistedCountryIsos or blacklistedCountryIsos is null. + // We assume no countries are to be excluded. Here, we correct this assumption based on the + // contents of either lists. Set excludedCountries = new HashSet<>(); if (whitelistedCountryIsos == null) { + // Exclude all countries in the blacklistedCountryIsos list. excludedCountries.addAll(blacklistedCountryIsos); } else { + // Exclude all countries that are not present in the whitelistedCountryIsos list. excludedCountries.addAll(countryInfoMap.keySet()); excludedCountries.removeAll(whitelistedCountryIsos); } + // Once we know which countries need to be excluded, we loop through the country isos, + // skipping those that have been excluded. for (String countryIso : countryInfoMap.keySet()) { if (!excludedCountries.contains(countryIso)) { countryInfoList.add(new CountryInfo(new Locale("", countryIso), From 75a6e39235901688a4fe81d4a3c929844edb1323 Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Fri, 25 May 2018 15:53:08 -0700 Subject: [PATCH 07/11] Adds ability to input country code as well as iso --- .../java/com/firebase/ui/auth/AuthUI.java | 131 ++++++++++-------- .../auth/data/client/CountryListLoadTask.java | 26 +++- .../ui/auth/ui/phone/CountryListSpinner.java | 16 +-- .../ui/phone/VerifyPhoneNumberFragment.java | 8 +- .../firebase/ui/auth/util/ExtraConstants.java | 4 +- .../ui/auth/util/data/PhoneNumberUtils.java | 7 +- .../ui/phone/CountryListLoadTaskTests.java | 57 +++++++- 7 files changed, 172 insertions(+), 77 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java index e8d6881d6..b8eabce85 100644 --- a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java +++ b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java @@ -653,56 +653,70 @@ public PhoneBuilder setDefaultCountryIso(@NonNull String iso) { /** * Sets the country codes available in the country code selector for phone - * authentication. This is not to be called with - * {@link #setBlacklistedPhoneAuthCountryIsos(List)}. + * authentication. Takes as input a List of both country isos and codes. + * This is not to be called with + * {@link #setBlacklistedPhoneAuthCountries(List)}. * If both are called, an exception will be thrown. *

- * For a list of country codes, see Alpha-2 codes here: - * http://www.nationsonline.org/oneworld/country_code_list.htm + * Inputting an e-164 country code (e.g. '+1') will include all countries with + * +1 as its code. + * Example input: {'+52', 'us'} + * For a list of country iso or codes, see Alpha-2 isos here: + * https://en.wikipedia.org/wiki/ISO_3166-1 + * and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes * - * @param whitelistedPhoneAuthCountryIsos a case insensitive list of country codes to - * be whitelisted + * @param whitelistedPhoneAuthCountries a case insensitive list of country codes to be + * whitelisted */ - public PhoneBuilder setWhitelistedPhoneAuthCountryIsos( - @NonNull List whitelistedPhoneAuthCountryIsos) { - if (getParams().containsKey(ExtraConstants.BLACKLISTED_COUNTRY_ISOS)) { + public PhoneBuilder setWhitelistedPhoneAuthCountries( + @NonNull List whitelistedPhoneAuthCountries) { + if (getParams().containsKey(ExtraConstants.BLACKLISTED_COUNTRIES)) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + "authentication."); } - addCountryIsosToBundle(whitelistedPhoneAuthCountryIsos, - ExtraConstants.WHITELISTED_COUNTRY_ISOS); + addCountryIsosToBundle(whitelistedPhoneAuthCountries, + ExtraConstants.WHITELISTED_COUNTRIES); return this; } /** - * Sets the country codes to be removed from the country code selector for phone - * authentication. This is not to be called with - * {@link #setWhitelistedPhoneAuthCountryIsos(List)}. + * Sets the countries to be removed from the country code selector for phone + * authentication. Takes as input a List of both country isos and codes. + * This is not to be called with + * {@link #setWhitelistedPhoneAuthCountries(List)}. * If both are called, an exception will be thrown. *

- * For a list of country codes, see Alpha-2 codes here: + * Inputting an e-164 country code (e.g. '+1') will include all countries with + * +1 as its code. + * Example input: {'+52', 'us'} + * For a list of country iso or codes, see Alpha-2 codes here: * https://en.wikipedia.org/wiki/ISO_3166-1 - * - * @param blacklistedPhoneAuthCountryIsos a case insensitive list of country codes to - * be whitelisted + * and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes + * @param blacklistedPhoneAuthCountries a case insensitive list of country codes to be + * whitelisted */ - public PhoneBuilder setBlacklistedPhoneAuthCountryIsos( - @NonNull List blacklistedPhoneAuthCountryIsos) { - if (getParams().containsKey(ExtraConstants.WHITELISTED_COUNTRY_ISOS)) { + public PhoneBuilder setBlacklistedPhoneAuthCountries( + @NonNull List blacklistedPhoneAuthCountries) { + if (getParams().containsKey(ExtraConstants.WHITELISTED_COUNTRIES)) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + "authentication."); } - addCountryIsosToBundle(blacklistedPhoneAuthCountryIsos, - ExtraConstants.BLACKLISTED_COUNTRY_ISOS); + addCountryIsosToBundle(blacklistedPhoneAuthCountries, + ExtraConstants.BLACKLISTED_COUNTRIES); return this; } - private void addCountryIsosToBundle(List CountryIsos, - String CountryIsoType) { + @Override + public IdpConfig build() { + validateInputs(); + return super.build(); + } + + private void addCountryIsosToBundle(List CountryIsos, String CountryIsoType) { ArrayList uppercaseCodes = new ArrayList<>(); for (String code : CountryIsos) { uppercaseCodes.add(code.toUpperCase(Locale.getDefault())); @@ -711,56 +725,63 @@ private void addCountryIsosToBundle(List CountryIsos, getParams().putStringArrayList(CountryIsoType, uppercaseCodes); } - @Override - public IdpConfig build() { - validateInputs(); - return super.build(); - } - private void validateInputs() { - List whitelistedCountryIsos = getParams().getStringArrayList( - ExtraConstants.WHITELISTED_COUNTRY_ISOS); - List blacklistedCountryIsos = getParams().getStringArrayList( - ExtraConstants.BLACKLISTED_COUNTRY_ISOS); + List whitelistedCountries = getParams().getStringArrayList( + ExtraConstants.WHITELISTED_COUNTRIES); + List blacklistedCountries = getParams().getStringArrayList( + ExtraConstants.BLACKLISTED_COUNTRIES); - if (whitelistedCountryIsos != null && - blacklistedCountryIsos != null) { + if (whitelistedCountries != null && + blacklistedCountries != null) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + "authentication."); - } else if (whitelistedCountryIsos != null) { - validateCountryIsos(whitelistedCountryIsos); - validateDefaultCountryCode(whitelistedCountryIsos, true); + } else if (whitelistedCountries != null) { + validateCountries(whitelistedCountries); + validateDefaultCountryIso(whitelistedCountries, true); - } else if (blacklistedCountryIsos != null) { - validateCountryIsos(blacklistedCountryIsos); - validateDefaultCountryCode(blacklistedCountryIsos, false); + } else if (blacklistedCountries != null) { + validateCountries(blacklistedCountries); + validateDefaultCountryIso(blacklistedCountries, false); } } - private void validateCountryIsos(List codes) { - if (codes == null) { - return; - } + private void validateCountries(List codes) { for (String code : codes) { - if (!PhoneNumberUtils.isValidIso(code)) { - throw new RuntimeException("Invalid country iso code provided."); + if (!PhoneNumberUtils.isValidIso(code) && !PhoneNumberUtils.isValid(code)) { + throw new RuntimeException("Invalid input: You must provide a valid " + + "country iso (alpha-2) or code (e-164). e.g. 'us' or '+1'."); } } } - private void validateDefaultCountryCode(List codes, - boolean whitelisted) { + private void validateDefaultCountryIso(List codes, boolean whitelisted) { if (getParams().containsKey(ExtraConstants.COUNTRY_ISO) && codes != null) { - String defaultCode = getParams().getString(ExtraConstants.COUNTRY_ISO); - if ((codes.contains(defaultCode) && !whitelisted) - || (!codes.contains(defaultCode) && whitelisted)) { + String defaultIso = getParams().getString(ExtraConstants.COUNTRY_ISO); + boolean containsIso = containsCountryIso(codes, defaultIso); + if (!containsIso && whitelisted || containsIso && !whitelisted) { throw new RuntimeException("Invalid default country iso. Make sure it " + - "is either part of the whitelisted country codes or that you " + + "is either part of the whitelisted list or that you " + "haven't blacklisted it."); } } } + + private boolean containsCountryIso(List codes, String iso) { + for (String code : codes) { + if (PhoneNumberUtils.isValidIso(code)) { + if (code.equals(iso)) { + return true; + } + } else { + List isos = PhoneNumberUtils.getCountryIsosFromCountryCode(code); + if (isos.contains(iso)) { + return true; + } + } + } + return false; + } } /** diff --git a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java index bebb95526..f872ca044 100644 --- a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java +++ b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java @@ -19,6 +19,7 @@ package com.firebase.ui.auth.data.client; import android.os.AsyncTask; +import android.support.annotation.NonNull; import android.support.annotation.RestrictTo; import com.firebase.ui.auth.data.model.CountryInfo; @@ -49,17 +50,30 @@ public interface Listener { List blacklistedCountryIsos; public CountryListLoadTask(Listener listener, - List whitelistedCountryIsos, - List blacklistedCountryIsos) { + List whitelistedCountries, + List blacklistedCountries) { mListener = listener; countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap(); - if (whitelistedCountryIsos == null && blacklistedCountryIsos == null) { - this.whitelistedCountryIsos = new ArrayList<>(countryInfoMap.keySet()); + if (whitelistedCountries != null) { + this.whitelistedCountryIsos = convertCodesToIsos(whitelistedCountries); + } else if (blacklistedCountries != null) { + this.blacklistedCountryIsos = convertCodesToIsos(blacklistedCountries); } else { - this.whitelistedCountryIsos = whitelistedCountryIsos; - this.blacklistedCountryIsos = blacklistedCountryIsos; + this.whitelistedCountryIsos = new ArrayList<>(countryInfoMap.keySet()); + } + } + + private List convertCodesToIsos(@NonNull List codes) { + List isos = new ArrayList<>(); + for (String code : codes) { + if (PhoneNumberUtils.isValid(code)) { + isos.addAll(PhoneNumberUtils.getCountryIsosFromCountryCode(code)); + } else { + isos.add(code); + } } + return isos; } @Override diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java index bcea521f1..9ae55b75c 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java +++ b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java @@ -50,8 +50,8 @@ public final class CountryListSpinner extends AppCompatEditText implements private String mSelectedCountryName; private CountryInfo mSelectedCountryInfo; - private List whitelistedCountryIsos; - private List blacklistedCountryIsos; + private List whitelistedCountries; + private List blacklistedCountries; public CountryListSpinner(Context context) { this(context, null, android.R.attr.spinnerStyle); @@ -149,8 +149,8 @@ public void onClick(View view) { private void loadCountryList() { new CountryListLoadTask(this, - whitelistedCountryIsos, - blacklistedCountryIsos).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + whitelistedCountries, + blacklistedCountries).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } private void executeUserClickListener(View view) { @@ -159,12 +159,12 @@ private void executeUserClickListener(View view) { } } - public void setWhitelistedCountryIsos(List whitelistedCountryIsos) { - this.whitelistedCountryIsos = whitelistedCountryIsos; + public void setWhitelistedCountries(List whitelistedCountries) { + this.whitelistedCountries = whitelistedCountries; } - public void setBlacklistedCountryIsos(List blacklistedCountryIsos) { - this.blacklistedCountryIsos = blacklistedCountryIsos; + public void setBlacklistedCountries(List blacklistedCountries) { + this.blacklistedCountries = blacklistedCountries; } @Override diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java b/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java index 7318f1ce7..c59734443 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java +++ b/auth/src/main/java/com/firebase/ui/auth/ui/phone/VerifyPhoneNumberFragment.java @@ -221,10 +221,10 @@ private String getPseudoValidPhoneNumber() { private void setupCountrySpinner() { Bundle params = getArguments().getBundle(ExtraConstants.PARAMS); if (params != null) { - mCountryListSpinner.setWhitelistedCountryIsos( - params.getStringArrayList(ExtraConstants.WHITELISTED_COUNTRY_ISOS)); - mCountryListSpinner.setBlacklistedCountryIsos( - params.getStringArrayList(ExtraConstants.BLACKLISTED_COUNTRY_ISOS)); + mCountryListSpinner.setWhitelistedCountries( + params.getStringArrayList(ExtraConstants.WHITELISTED_COUNTRIES)); + mCountryListSpinner.setBlacklistedCountries( + params.getStringArrayList(ExtraConstants.BLACKLISTED_COUNTRIES)); } //clear error when spinner is clicked on diff --git a/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java b/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java index 15980854e..a3f8ab7e5 100644 --- a/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java +++ b/auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java @@ -37,8 +37,8 @@ public final class ExtraConstants { public static final String COUNTRY_ISO = "extra_country_iso"; public static final String NATIONAL_NUMBER = "extra_national_number"; - public static final String WHITELISTED_COUNTRY_ISOS = "whitelisted_country_isos"; - public static final String BLACKLISTED_COUNTRY_ISOS = "blacklisted_country_isos"; + public static final String WHITELISTED_COUNTRIES = "whitelisted_countries"; + public static final String BLACKLISTED_COUNTRIES = "blacklisted_countries"; private ExtraConstants() { throw new AssertionError("No instance for you!"); diff --git a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java index 6f18348fa..291cbd2de 100644 --- a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java +++ b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java @@ -167,6 +167,12 @@ private static String getCountryIsoForCountryCode(String countryCode) { return DEFAULT_LOCALE.getCountry(); } + @Nullable + public static List getCountryIsosFromCountryCode(String countryCode) { + return !isValid(countryCode) ? null : + COUNTRY_TO_REGION_CODES.get(Integer.parseInt(countryCode.substring(1))); + } + /** * Country code extracted using shortest matching prefix like libPhoneNumber. See: * https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com @@ -185,7 +191,6 @@ private static String getCountryCodeForPhoneNumber(String normalizedPhoneNumber) return potentialCountryCode; } } - return null; } diff --git a/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java b/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java index ea39d8ca7..480e04fee 100644 --- a/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java +++ b/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java @@ -20,6 +20,7 @@ import com.firebase.ui.auth.data.client.CountryListLoadTask; import com.firebase.ui.auth.data.model.CountryInfo; +import com.firebase.ui.auth.util.data.PhoneNumberUtils; import org.junit.Before; import org.junit.Test; @@ -340,7 +341,7 @@ public void testExecute_withUserWhitelistInput_expectUserCountriesAddedOnly() { } @Test - public void testExecute_withUserBlacklistInput_expectUserCountriesAddedOnly() { + public void testExecute_withUserBlacklistInput_expectUserCountriesRemoved() { List blacklistedCountryIsos = new ArrayList<>(); blacklistedCountryIsos.add("US"); @@ -362,6 +363,60 @@ public void testExecute_withUserBlacklistInput_expectUserCountriesAddedOnly() { } } + @Test + public void testExecute_withCodeWhitelistInput_expectUserCodeCountriesAddedOnly() { + List whitelistedCountries = new ArrayList<>(); + whitelistedCountries.add("+1"); + + List expectedOutput = new ArrayList<>(); + for (String iso : PhoneNumberUtils.getCountryIsosFromCountryCode("+1")) { + expectedOutput.add(new CountryInfo(new Locale("", iso), 1)); + } + + mTask = new CountryListLoadTask(mListener, whitelistedCountries, null); + + mTask.execute(); + + try { + final List result = mTask.get(); + Collections.sort(expectedOutput); + assertEquals(expectedOutput, result); + } catch (InterruptedException e) { + fail("Should not throw InterruptedException"); + } catch (ExecutionException e) { + fail("Should not throw ExecutionException"); + } + } + + @Test + public void testExecute_withCodeBlacklistInput_expectUserCodeCountriesRemoved() { + List blacklistedCountries = new ArrayList<>(); + blacklistedCountries.add("+1"); + + List excludedCountries = new ArrayList<>(); + for (String iso : PhoneNumberUtils.getCountryIsosFromCountryCode("+1")) { + excludedCountries.add(new CountryInfo(new Locale("", iso), 1)); + } + + List expectedOutput = new ArrayList<>(COUNTRY_LIST); + expectedOutput.removeAll(excludedCountries); + + mTask = new CountryListLoadTask(mListener, null, blacklistedCountries); + + mTask.execute(); + + try { + final List result = mTask.get(); + Collections.sort(expectedOutput); + assertEquals(expectedOutput, result); + } catch (InterruptedException e) { + fail("Should not throw InterruptedException"); + } catch (ExecutionException e) { + fail("Should not throw ExecutionException"); + } + } + + @Test public void testOnPostExecute_nullListener() { mTask = new CountryListLoadTask(null, null, null); From 6d2011e499ddc10eee08c2cc9605189d6696e7f2 Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Fri, 25 May 2018 20:02:34 -0700 Subject: [PATCH 08/11] Renames new methods --- .../java/com/firebase/ui/auth/AuthUI.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java index b8eabce85..0d84db783 100644 --- a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java +++ b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java @@ -655,7 +655,7 @@ public PhoneBuilder setDefaultCountryIso(@NonNull String iso) { * Sets the country codes available in the country code selector for phone * authentication. Takes as input a List of both country isos and codes. * This is not to be called with - * {@link #setBlacklistedPhoneAuthCountries(List)}. + * {@link #setBlacklistedCountries(List)}. * If both are called, an exception will be thrown. *

* Inputting an e-164 country code (e.g. '+1') will include all countries with @@ -665,18 +665,18 @@ public PhoneBuilder setDefaultCountryIso(@NonNull String iso) { * https://en.wikipedia.org/wiki/ISO_3166-1 * and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes * - * @param whitelistedPhoneAuthCountries a case insensitive list of country codes to be - * whitelisted + * @param whitelistedCountries a case insensitive list of country codes and/or + * isos to be whitelisted */ - public PhoneBuilder setWhitelistedPhoneAuthCountries( - @NonNull List whitelistedPhoneAuthCountries) { + public PhoneBuilder setWhitelistedCountries( + @NonNull List whitelistedCountries) { if (getParams().containsKey(ExtraConstants.BLACKLISTED_COUNTRIES)) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + "authentication."); } - addCountryIsosToBundle(whitelistedPhoneAuthCountries, + addCountryIsosToBundle(whitelistedCountries, ExtraConstants.WHITELISTED_COUNTRIES); return this; } @@ -685,7 +685,7 @@ public PhoneBuilder setWhitelistedPhoneAuthCountries( * Sets the countries to be removed from the country code selector for phone * authentication. Takes as input a List of both country isos and codes. * This is not to be called with - * {@link #setWhitelistedPhoneAuthCountries(List)}. + * {@link #setWhitelistedCountries(List)}. * If both are called, an exception will be thrown. *

* Inputting an e-164 country code (e.g. '+1') will include all countries with @@ -694,18 +694,18 @@ public PhoneBuilder setWhitelistedPhoneAuthCountries( * For a list of country iso or codes, see Alpha-2 codes here: * https://en.wikipedia.org/wiki/ISO_3166-1 * and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes - * @param blacklistedPhoneAuthCountries a case insensitive list of country codes to be - * whitelisted + * @param blacklistedCountries a case insensitive list of country codes and/or isos + * to be blacklisted */ - public PhoneBuilder setBlacklistedPhoneAuthCountries( - @NonNull List blacklistedPhoneAuthCountries) { + public PhoneBuilder setBlacklistedCountries( + @NonNull List blacklistedCountries) { if (getParams().containsKey(ExtraConstants.WHITELISTED_COUNTRIES)) { throw new RuntimeException( "You can either whitelist or blacklist country codes for phone " + "authentication."); } - addCountryIsosToBundle(blacklistedPhoneAuthCountries, + addCountryIsosToBundle(blacklistedCountries, ExtraConstants.BLACKLISTED_COUNTRIES); return this; } From 545a90ab2be293e2ffca7510e4fe4ee3bd8968b1 Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Sat, 26 May 2018 21:27:19 -0700 Subject: [PATCH 09/11] Small fix --- auth/src/main/java/com/firebase/ui/auth/AuthUI.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java index 0d84db783..6e283d69e 100644 --- a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java +++ b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java @@ -675,9 +675,7 @@ public PhoneBuilder setWhitelistedCountries( "You can either whitelist or blacklist country codes for phone " + "authentication."); } - - addCountryIsosToBundle(whitelistedCountries, - ExtraConstants.WHITELISTED_COUNTRIES); + addCountriesToBundle(whitelistedCountries, ExtraConstants.WHITELISTED_COUNTRIES); return this; } @@ -704,9 +702,7 @@ public PhoneBuilder setBlacklistedCountries( "You can either whitelist or blacklist country codes for phone " + "authentication."); } - - addCountryIsosToBundle(blacklistedCountries, - ExtraConstants.BLACKLISTED_COUNTRIES); + addCountriesToBundle(blacklistedCountries, ExtraConstants.BLACKLISTED_COUNTRIES); return this; } @@ -716,7 +712,7 @@ public IdpConfig build() { return super.build(); } - private void addCountryIsosToBundle(List CountryIsos, String CountryIsoType) { + private void addCountriesToBundle(List CountryIsos, String CountryIsoType) { ArrayList uppercaseCodes = new ArrayList<>(); for (String code : CountryIsos) { uppercaseCodes.add(code.toUpperCase(Locale.getDefault())); From d41c901839d7fad610ac2d9b9086461980b7e0f5 Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Tue, 29 May 2018 16:40:01 -0700 Subject: [PATCH 10/11] Applies review suggestions --- .../auth/data/client/CountryListLoadTask.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java index f872ca044..c36305161 100644 --- a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java +++ b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java @@ -20,6 +20,7 @@ import android.os.AsyncTask; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.annotation.RestrictTo; import com.firebase.ui.auth.data.model.CountryInfo; @@ -45,27 +46,24 @@ public interface Listener { private final Listener mListener; - Map countryInfoMap; - List whitelistedCountryIsos; - List blacklistedCountryIsos; + + Set whitelistedCountryIsos; + Set blacklistedCountryIsos; public CountryListLoadTask(Listener listener, - List whitelistedCountries, - List blacklistedCountries) { + @Nullable List whitelistedCountries, + @Nullable List blacklistedCountries) { mListener = listener; - countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap(); if (whitelistedCountries != null) { - this.whitelistedCountryIsos = convertCodesToIsos(whitelistedCountries); + this.whitelistedCountryIsos = convertCodesToIsos(new HashSet<>(whitelistedCountries)); } else if (blacklistedCountries != null) { - this.blacklistedCountryIsos = convertCodesToIsos(blacklistedCountries); - } else { - this.whitelistedCountryIsos = new ArrayList<>(countryInfoMap.keySet()); + this.blacklistedCountryIsos = convertCodesToIsos(new HashSet<>(blacklistedCountries)); } } - private List convertCodesToIsos(@NonNull List codes) { - List isos = new ArrayList<>(); + private Set convertCodesToIsos(@NonNull Set codes) { + Set isos = new HashSet<>(); for (String code : codes) { if (PhoneNumberUtils.isValid(code)) { isos.addAll(PhoneNumberUtils.getCountryIsosFromCountryCode(code)); @@ -78,12 +76,19 @@ private List convertCodesToIsos(@NonNull List codes) { @Override protected List doInBackground(Void... params) { - List countryInfoList = getAvailableCountryIsos(); + Map countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap(); + List countryInfoList = getAvailableCountryIsos(countryInfoMap); Collections.sort(countryInfoList); return countryInfoList; } - public List getAvailableCountryIsos() { + public List getAvailableCountryIsos(Map countryInfoMap) { + // We consider all countries to be whitelisted if there are no whitelisted + // or blacklisted countries given as input. + if(whitelistedCountryIsos == null && blacklistedCountryIsos == null) { + this.whitelistedCountryIsos = new HashSet<>(countryInfoMap.keySet()); + } + List countryInfoList = new ArrayList<>(); // At this point either whitelistedCountryIsos or blacklistedCountryIsos is null. From 9051b55c8cf7b530931f8992c668bef79ff788b2 Mon Sep 17 00:00:00 2001 From: Leonardo Siracusa Date: Tue, 29 May 2018 16:56:00 -0700 Subject: [PATCH 11/11] Country code to iso map is initialized only when needed --- .../ui/auth/data/client/CountryListLoadTask.java | 2 +- .../ui/auth/util/data/PhoneNumberUtils.java | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java index c36305161..119d35127 100644 --- a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java +++ b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java @@ -85,7 +85,7 @@ protected List doInBackground(Void... params) { public List getAvailableCountryIsos(Map countryInfoMap) { // We consider all countries to be whitelisted if there are no whitelisted // or blacklisted countries given as input. - if(whitelistedCountryIsos == null && blacklistedCountryIsos == null) { + if (whitelistedCountryIsos == null && blacklistedCountryIsos == null) { this.whitelistedCountryIsos = new HashSet<>(countryInfoMap.keySet()); } diff --git a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java index 291cbd2de..53f84e8a4 100644 --- a/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java +++ b/auth/src/main/java/com/firebase/ui/auth/util/data/PhoneNumberUtils.java @@ -51,8 +51,8 @@ public final class PhoneNumberUtils { private static final SparseArray> COUNTRY_TO_REGION_CODES = createCountryCodeToRegionCodeMap(); - private static final Map COUNTRY_TO_ISO_CODES = - Collections.unmodifiableMap(createCountryCodeByIsoMap()); + + private static Map COUNTRY_TO_ISO_CODES; /** * This method works as follow:

  1. When the android version is LOLLIPOP or greater, the @@ -151,11 +151,17 @@ public static PhoneNumber getPhoneNumber( @Nullable public static Integer getCountryCode(String countryIso) { + if (COUNTRY_TO_ISO_CODES == null) { + initCountryCodeByIsoMap(); + } return countryIso == null ? null : COUNTRY_TO_ISO_CODES.get(countryIso.toUpperCase(Locale.getDefault())); } public static Map getImmutableCountryIsoMap() { + if (COUNTRY_TO_ISO_CODES == null) { + initCountryCodeByIsoMap(); + } return COUNTRY_TO_ISO_CODES; } @@ -442,7 +448,7 @@ private static SparseArray> createCountryCodeToRegionCodeMap() { return map; } - private static Map createCountryCodeByIsoMap() { + private static void initCountryCodeByIsoMap() { Map map = new HashMap<>(MAX_COUNTRIES); for (int i = 0; i < COUNTRY_TO_REGION_CODES.size(); i++) { @@ -466,6 +472,6 @@ private static Map createCountryCodeByIsoMap() { map.put("GS", 500); map.put("XK", 381); - return map; + COUNTRY_TO_ISO_CODES = Collections.unmodifiableMap(map); } }