Skip to content

Commit dbec7c0

Browse files
authored
Fix NPE in CountryListSpinner (#1756)
1 parent cd089cd commit dbec7c0

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public final class CountryListSpinner extends AppCompatEditText implements View.
5555
private String mSelectedCountryName;
5656
private CountryInfo mSelectedCountryInfo;
5757

58-
private Set<String> mWhitelistedCountryIsos;
59-
private Set<String> mBlacklistedCountryIsos;
58+
private Set<String> mWhitelistedCountryIsos = new HashSet<>();
59+
private Set<String> mBlacklistedCountryIsos = new HashSet<>();
6060

6161
public CountryListSpinner(Context context) {
6262
this(context, null, android.R.attr.spinnerStyle);
@@ -86,11 +86,11 @@ public void init(Bundle params) {
8686

8787
private List<CountryInfo> getCountriesToDisplayInSpinner(Bundle params) {
8888
initCountrySpinnerIsosFromParams(params);
89-
9089
Map<String, Integer> countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap();
90+
9191
// We consider all countries to be whitelisted if there are no whitelisted
9292
// or blacklisted countries given as input.
93-
if (mWhitelistedCountryIsos == null && mBlacklistedCountryIsos == null) {
93+
if (mWhitelistedCountryIsos.isEmpty() && mBlacklistedCountryIsos.isEmpty()) {
9494
this.mWhitelistedCountryIsos = new HashSet<>(countryInfoMap.keySet());
9595
}
9696

@@ -100,7 +100,7 @@ private List<CountryInfo> getCountriesToDisplayInSpinner(Bundle params) {
100100
// We assume no countries are to be excluded. Here, we correct this assumption based on the
101101
// contents of either lists.
102102
Set<String> excludedCountries = new HashSet<>();
103-
if (mWhitelistedCountryIsos == null) {
103+
if (!mBlacklistedCountryIsos.isEmpty()) {
104104
// Exclude all countries in the mBlacklistedCountryIsos list.
105105
excludedCountries.addAll(mBlacklistedCountryIsos);
106106
} else {
@@ -129,7 +129,9 @@ private void initCountrySpinnerIsosFromParams(@NonNull Bundle params) {
129129

130130
if (whitelistedCountries != null) {
131131
mWhitelistedCountryIsos = convertCodesToIsos(whitelistedCountries);
132-
} else if (blacklistedCountries != null) {
132+
}
133+
134+
if (blacklistedCountries != null) {
133135
mBlacklistedCountryIsos = convertCodesToIsos(blacklistedCountries);
134136
}
135137
}
@@ -164,9 +166,16 @@ private void setDefaultCountryForSpinner(List<CountryInfo> countries) {
164166

165167
public boolean isValidIso(String iso) {
166168
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;
170179
}
171180

172181
@Override

0 commit comments

Comments
 (0)