@@ -658,16 +658,24 @@ public PhoneBuilder setDefaultCountryIso(@NonNull String iso) {
658
658
* https://en.wikipedia.org/wiki/ISO_3166-1
659
659
* and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes
660
660
*
661
- * @param whitelistedCountries a case insensitive list of country codes and/or
662
- * isos to be whitelisted
661
+ * @param whitelistedCountries a non empty case insensitive list of country codes
662
+ * and/or isos to be whitelisted
663
+ * @throws IllegalArgumentException if an empty whitelist is provided.
664
+ * @throws NullPointerException if a null whitelist is provided.
663
665
*/
664
666
public PhoneBuilder setWhitelistedCountries (
665
667
@ NonNull List <String > whitelistedCountries ) {
666
668
if (getParams ().containsKey (ExtraConstants .BLACKLISTED_COUNTRIES )) {
667
- throw new RuntimeException (
669
+ throw new IllegalStateException (
668
670
"You can either whitelist or blacklist country codes for phone " +
669
671
"authentication." );
670
672
}
673
+
674
+ String message = "Invalid argument: Only non-%s whitelists are valid. " +
675
+ "To specify no whitelist, do not call this method." ;
676
+ Preconditions .checkNotNull (whitelistedCountries , String .format (message , "null" ));
677
+ Preconditions .checkArgument (!whitelistedCountries .isEmpty (), String .format (message , "empty" ));
678
+
671
679
addCountriesToBundle (whitelistedCountries , ExtraConstants .WHITELISTED_COUNTRIES );
672
680
return this ;
673
681
}
@@ -685,16 +693,25 @@ public PhoneBuilder setWhitelistedCountries(
685
693
* For a list of country iso or codes, see Alpha-2 codes here:
686
694
* https://en.wikipedia.org/wiki/ISO_3166-1
687
695
* and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes
688
- * @param blacklistedCountries a case insensitive list of country codes and/or isos
689
- * to be blacklisted
696
+ *
697
+ * @param blacklistedCountries a non empty case insensitive list of country codes
698
+ * and/or isos to be blacklisted
699
+ * @throws IllegalArgumentException if an empty blacklist is provided.
700
+ * @throws NullPointerException if a null blacklist is provided.
690
701
*/
691
702
public PhoneBuilder setBlacklistedCountries (
692
703
@ NonNull List <String > blacklistedCountries ) {
693
704
if (getParams ().containsKey (ExtraConstants .WHITELISTED_COUNTRIES )) {
694
- throw new RuntimeException (
705
+ throw new IllegalStateException (
695
706
"You can either whitelist or blacklist country codes for phone " +
696
707
"authentication." );
697
708
}
709
+
710
+ String message = "Invalid argument: Only non-%s blacklists are valid. " +
711
+ "To specify no blacklist, do not call this method." ;
712
+ Preconditions .checkNotNull (blacklistedCountries , String .format (message , "null" ));
713
+ Preconditions .checkArgument (!blacklistedCountries .isEmpty (), String .format (message , "empty" ));
714
+
698
715
addCountriesToBundle (blacklistedCountries , ExtraConstants .BLACKLISTED_COUNTRIES );
699
716
return this ;
700
717
}
@@ -722,41 +739,70 @@ private void validateInputs() {
722
739
723
740
if (whitelistedCountries != null &&
724
741
blacklistedCountries != null ) {
725
- throw new RuntimeException (
742
+ throw new IllegalStateException (
726
743
"You can either whitelist or blacklist country codes for phone " +
727
744
"authentication." );
728
745
} else if (whitelistedCountries != null ) {
729
- validateCountries (whitelistedCountries );
730
- validateDefaultCountryIso (whitelistedCountries , true );
746
+ validateInputs (whitelistedCountries , true );
731
747
732
748
} else if (blacklistedCountries != null ) {
733
- validateCountries (blacklistedCountries );
734
- validateDefaultCountryIso (blacklistedCountries , false );
749
+ validateInputs (blacklistedCountries , false );
735
750
}
736
751
}
737
752
738
- private void validateCountries (List <String > codes ) {
753
+ private void validateInputs (List <String > countries , boolean whitelisted ) {
754
+ validateCountryInput (countries );
755
+ validateDefaultCountryInput (countries , whitelisted );
756
+ }
757
+
758
+ private void validateCountryInput (List <String > codes ) {
739
759
for (String code : codes ) {
740
760
if (!PhoneNumberUtils .isValidIso (code ) && !PhoneNumberUtils .isValid (code )) {
741
- throw new RuntimeException ("Invalid input: You must provide a valid " +
742
- "country iso (alpha-2) or code (e-164). e.g. 'us' or '+1'." );
761
+ throw new IllegalArgumentException ("Invalid input: You must provide a " +
762
+ "valid country iso (alpha-2) or code (e-164). e.g. 'us' or '+1'." );
743
763
}
744
764
}
745
765
}
746
766
747
- private void validateDefaultCountryIso (List <String > codes , boolean whitelisted ) {
748
- if (getParams ().containsKey (ExtraConstants .COUNTRY_ISO ) && codes != null ) {
749
- String defaultIso = getParams ().getString (ExtraConstants .COUNTRY_ISO );
750
- boolean containsIso = containsCountryIso (codes , defaultIso );
751
- if (!containsIso && whitelisted || containsIso && !whitelisted ) {
752
- throw new RuntimeException ("Invalid default country iso. Make sure it " +
753
- "is either part of the whitelisted list or that you " +
754
- "haven't blacklisted it." );
767
+ private void validateDefaultCountryInput (List <String > codes , boolean whitelisted ) {
768
+ // A default iso/code can be set via #setDefaultCountryIso() or #setDefaultNumber()
769
+ if (getParams ().containsKey (ExtraConstants .COUNTRY_ISO ) ||
770
+ getParams ().containsKey (ExtraConstants .PHONE )) {
771
+
772
+ if (!validateDefaultCountryIso (codes , whitelisted )
773
+ || !validateDefaultPhoneIsos (codes , whitelisted )) {
774
+ throw new IllegalArgumentException ("Invalid default country iso. Make " +
775
+ "sure it is either part of the whitelisted list or that you "
776
+ + "haven't blacklisted it." );
755
777
}
756
778
}
779
+
780
+ }
781
+
782
+ private boolean validateDefaultCountryIso (List <String > codes , boolean whitelisted ) {
783
+ String defaultIso = getDefaultIso ();
784
+ return isValidDefaultIso (codes , defaultIso , whitelisted );
785
+ }
786
+
787
+ private boolean validateDefaultPhoneIsos (List <String > codes , boolean whitelisted ) {
788
+ List <String > phoneIsos = getPhoneIsosFromCode ();
789
+ for (String iso : phoneIsos ) {
790
+ if (isValidDefaultIso (codes , iso , whitelisted )) {
791
+ return true ;
792
+ }
793
+ }
794
+ return phoneIsos .isEmpty ();
795
+ }
796
+
797
+ private boolean isValidDefaultIso (List <String > codes , String iso , boolean whitelisted ) {
798
+ if (iso == null ) return true ;
799
+ boolean containsIso = containsCountryIso (codes , iso );
800
+ return containsIso && whitelisted || !containsIso && !whitelisted ;
801
+
757
802
}
758
803
759
804
private boolean containsCountryIso (List <String > codes , String iso ) {
805
+ iso = iso .toUpperCase (Locale .getDefault ());
760
806
for (String code : codes ) {
761
807
if (PhoneNumberUtils .isValidIso (code )) {
762
808
if (code .equals (iso )) {
@@ -771,6 +817,26 @@ private boolean containsCountryIso(List<String> codes, String iso) {
771
817
}
772
818
return false ;
773
819
}
820
+
821
+ private List <String > getPhoneIsosFromCode () {
822
+ List <String > isos = new ArrayList <>();
823
+ String phone = getParams ().getString (ExtraConstants .PHONE );
824
+ if (phone != null && phone .startsWith ("+" )) {
825
+ String countryCode = "+" + PhoneNumberUtils .getPhoneNumber (phone )
826
+ .getCountryCode ();
827
+ List <String > isosToAdd = PhoneNumberUtils .
828
+ getCountryIsosFromCountryCode (countryCode );
829
+ if (isosToAdd != null ) {
830
+ isos .addAll (isosToAdd );
831
+ }
832
+ }
833
+ return isos ;
834
+ }
835
+
836
+ private String getDefaultIso () {
837
+ return getParams ().containsKey (ExtraConstants .COUNTRY_ISO ) ?
838
+ getParams ().getString (ExtraConstants .COUNTRY_ISO ) : null ;
839
+ }
774
840
}
775
841
776
842
/**
@@ -933,8 +999,8 @@ public T setLogo(@DrawableRes int logo) {
933
999
/**
934
1000
* Specifies the terms-of-service URL for the application.
935
1001
*
936
- * @deprecated Please use {@link #setTosAndPrivacyPolicyUrls(String, String)}
937
- * For the Tos link to be displayed a Privacy Policy url must also be provided.
1002
+ * @deprecated Please use {@link #setTosAndPrivacyPolicyUrls(String, String)} For the Tos
1003
+ * link to be displayed a Privacy Policy url must also be provided.
938
1004
*/
939
1005
@ NonNull
940
1006
@ Deprecated
@@ -946,8 +1012,8 @@ public T setTosUrl(@Nullable String tosUrl) {
946
1012
/**
947
1013
* Specifies the privacy policy URL for the application.
948
1014
*
949
- * @deprecated Please use {@link #setTosAndPrivacyPolicyUrls(String, String)}
950
- * For the Privacy Policy link to be displayed a Tos url must also be provided.
1015
+ * @deprecated Please use {@link #setTosAndPrivacyPolicyUrls(String, String)} For the
1016
+ * Privacy Policy link to be displayed a Tos url must also be provided.
951
1017
*/
952
1018
@ NonNull
953
1019
@ Deprecated
0 commit comments