Skip to content

Fix NPE in CountryListSpinner #1756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public final class CountryListSpinner extends AppCompatEditText implements View.
private String mSelectedCountryName;
private CountryInfo mSelectedCountryInfo;

private Set<String> mWhitelistedCountryIsos;
private Set<String> mBlacklistedCountryIsos;
private Set<String> mWhitelistedCountryIsos = new HashSet<>();
private Set<String> mBlacklistedCountryIsos = new HashSet<>();

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

private List<CountryInfo> getCountriesToDisplayInSpinner(Bundle params) {
initCountrySpinnerIsosFromParams(params);

Map<String, Integer> countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap();

// We consider all countries to be whitelisted if there are no whitelisted
// or blacklisted countries given as input.
if (mWhitelistedCountryIsos == null && mBlacklistedCountryIsos == null) {
if (mWhitelistedCountryIsos.isEmpty() && mBlacklistedCountryIsos.isEmpty()) {
this.mWhitelistedCountryIsos = new HashSet<>(countryInfoMap.keySet());
}

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

if (whitelistedCountries != null) {
mWhitelistedCountryIsos = convertCodesToIsos(whitelistedCountries);
} else if (blacklistedCountries != null) {
}

if (blacklistedCountries != null) {
mBlacklistedCountryIsos = convertCodesToIsos(blacklistedCountries);
}
}
Expand Down Expand Up @@ -164,9 +166,16 @@ private void setDefaultCountryForSpinner(List<CountryInfo> countries) {

public boolean isValidIso(String iso) {
iso = iso.toUpperCase(Locale.getDefault());
return ((mWhitelistedCountryIsos == null && mBlacklistedCountryIsos == null)
|| (mWhitelistedCountryIsos != null && mWhitelistedCountryIsos.contains(iso))
|| (mBlacklistedCountryIsos != null && !mBlacklistedCountryIsos.contains(iso)));
boolean valid = true;
if (!mWhitelistedCountryIsos.isEmpty()) {
valid = valid && mWhitelistedCountryIsos.contains(iso);
}

if (!mBlacklistedCountryIsos.isEmpty()) {
valid = valid && !mBlacklistedCountryIsos.contains(iso);
}

return valid;
}

@Override
Expand Down