@@ -55,8 +55,8 @@ public final class CountryListSpinner extends AppCompatEditText implements View.
55
55
private String mSelectedCountryName ;
56
56
private CountryInfo mSelectedCountryInfo ;
57
57
58
- private Set <String > mWhitelistedCountryIsos ;
59
- private Set <String > mBlacklistedCountryIsos ;
58
+ private Set <String > mWhitelistedCountryIsos = new HashSet <>() ;
59
+ private Set <String > mBlacklistedCountryIsos = new HashSet <>() ;
60
60
61
61
public CountryListSpinner (Context context ) {
62
62
this (context , null , android .R .attr .spinnerStyle );
@@ -86,11 +86,11 @@ public void init(Bundle params) {
86
86
87
87
private List <CountryInfo > getCountriesToDisplayInSpinner (Bundle params ) {
88
88
initCountrySpinnerIsosFromParams (params );
89
-
90
89
Map <String , Integer > countryInfoMap = PhoneNumberUtils .getImmutableCountryIsoMap ();
90
+
91
91
// We consider all countries to be whitelisted if there are no whitelisted
92
92
// or blacklisted countries given as input.
93
- if (mWhitelistedCountryIsos == null && mBlacklistedCountryIsos == null ) {
93
+ if (mWhitelistedCountryIsos . isEmpty () && mBlacklistedCountryIsos . isEmpty () ) {
94
94
this .mWhitelistedCountryIsos = new HashSet <>(countryInfoMap .keySet ());
95
95
}
96
96
@@ -100,7 +100,7 @@ private List<CountryInfo> getCountriesToDisplayInSpinner(Bundle params) {
100
100
// We assume no countries are to be excluded. Here, we correct this assumption based on the
101
101
// contents of either lists.
102
102
Set <String > excludedCountries = new HashSet <>();
103
- if (mWhitelistedCountryIsos == null ) {
103
+ if (! mBlacklistedCountryIsos . isEmpty () ) {
104
104
// Exclude all countries in the mBlacklistedCountryIsos list.
105
105
excludedCountries .addAll (mBlacklistedCountryIsos );
106
106
} else {
@@ -129,7 +129,9 @@ private void initCountrySpinnerIsosFromParams(@NonNull Bundle params) {
129
129
130
130
if (whitelistedCountries != null ) {
131
131
mWhitelistedCountryIsos = convertCodesToIsos (whitelistedCountries );
132
- } else if (blacklistedCountries != null ) {
132
+ }
133
+
134
+ if (blacklistedCountries != null ) {
133
135
mBlacklistedCountryIsos = convertCodesToIsos (blacklistedCountries );
134
136
}
135
137
}
@@ -164,9 +166,16 @@ private void setDefaultCountryForSpinner(List<CountryInfo> countries) {
164
166
165
167
public boolean isValidIso (String iso ) {
166
168
iso = iso .toUpperCase (Locale .getDefault ());
167
- return ((mWhitelistedCountryIsos == null && mBlacklistedCountryIsos == null )
168
- || (mWhitelistedCountryIsos != null && mWhitelistedCountryIsos .contains (iso ))
169
- || (mBlacklistedCountryIsos != null && !mBlacklistedCountryIsos .contains (iso )));
169
+ boolean valid = true ;
170
+ if (!mWhitelistedCountryIsos .isEmpty ()) {
171
+ valid = valid && mWhitelistedCountryIsos .contains (iso );
172
+ }
173
+
174
+ if (!mBlacklistedCountryIsos .isEmpty ()) {
175
+ valid = valid && !mBlacklistedCountryIsos .contains (iso );
176
+ }
177
+
178
+ return valid ;
170
179
}
171
180
172
181
@ Override
0 commit comments