diff --git a/android/java/org/chromium/chrome/browser/rewards/onboarding/CountrySelectionSpinnerAdapter.java b/android/java/org/chromium/chrome/browser/rewards/onboarding/CountrySelectionSpinnerAdapter.java index 487f9edcbab3..e87a35ff6604 100644 --- a/android/java/org/chromium/chrome/browser/rewards/onboarding/CountrySelectionSpinnerAdapter.java +++ b/android/java/org/chromium/chrome/browser/rewards/onboarding/CountrySelectionSpinnerAdapter.java @@ -9,62 +9,32 @@ import android.content.Context; import android.graphics.Typeface; -import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - import org.chromium.chrome.browser.BraveRewardsNativeWorker; - -import java.util.ArrayList; import java.util.Locale; public class CountrySelectionSpinnerAdapter extends ArrayAdapter { - public CountrySelectionSpinnerAdapter(Context context, ArrayList countryList) { - super(context, 0, countryList); + public CountrySelectionSpinnerAdapter(Context context, String[] countries) { + super(context, android.R.layout.simple_spinner_item, countries); } - @NonNull @Override - public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { - if (convertView == null) { - convertView = LayoutInflater.from(getContext()) - .inflate(android.R.layout.simple_spinner_item, parent, false); - } - return createView(position, convertView, parent, false); - } - - @Override - public View getDropDownView( - int position, @Nullable View convertView, @NonNull ViewGroup parent) { - if (convertView == null) { - convertView = - LayoutInflater.from(getContext()) - .inflate(android.R.layout.simple_spinner_dropdown_item, parent, false); - } - return createView(position, convertView, parent, true); - } - - private View createView(int position, View convertView, ViewGroup parent, boolean isDropdown) { - TextView textViewName = convertView.findViewById(android.R.id.text1); + public View getDropDownView(int position, View convertView, ViewGroup parent) { + View view = super.getDropDownView(position, null, parent); + TextView textViewName = view.findViewById(android.R.id.text1); + + String defaultCountry = BraveRewardsNativeWorker.getInstance().getCountryCode() != null + ? new Locale("", BraveRewardsNativeWorker.getInstance().getCountryCode()) + .getDisplayCountry() + : null; String currentItem = getItem(position); - if (isDropdown) { - String defaultCountry = BraveRewardsNativeWorker.getInstance().getCountryCode() != null - ? new Locale("", BraveRewardsNativeWorker.getInstance().getCountryCode()) - .getDisplayCountry() - : null; - if (defaultCountry != null && currentItem.equals(defaultCountry)) { - textViewName.setTypeface(textViewName.getTypeface(), Typeface.BOLD); - } + if (defaultCountry != null && currentItem.equals(defaultCountry)) { + textViewName.setTypeface(textViewName.getTypeface(), Typeface.BOLD); } - if (currentItem != null) { - textViewName.setText(currentItem); - } - return convertView; + return view; } } diff --git a/android/java/org/chromium/chrome/browser/rewards/onboarding/RewardsOnboarding.java b/android/java/org/chromium/chrome/browser/rewards/onboarding/RewardsOnboarding.java index 086ee8af129e..c6c1cbc6f1d8 100644 --- a/android/java/org/chromium/chrome/browser/rewards/onboarding/RewardsOnboarding.java +++ b/android/java/org/chromium/chrome/browser/rewards/onboarding/RewardsOnboarding.java @@ -7,12 +7,14 @@ package org.chromium.chrome.browser.rewards.onboarding; +import android.annotation.SuppressLint; import android.content.Context; import android.os.Build; import android.text.SpannableString; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.view.LayoutInflater; +import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; @@ -38,7 +40,9 @@ import org.chromium.ui.permissions.PermissionConstants; import org.chromium.ui.text.NoUnderlineClickableSpan; +import java.text.Collator; import java.util.ArrayList; +import java.util.Collections; import java.util.Locale; import java.util.TreeMap; @@ -171,30 +175,39 @@ public void onGetAvailableCountries(String[] countries) { updateCountryList(countries); } + @SuppressLint("ClickableViewAccessibility") private void updateCountryList(String[] countries) { shouldShowContinueProgress(false); mContinueButton.setText(mActivity.getResources().getString(R.string.continue_text)); + String defaultCountry = mBraveRewardsNativeWorker.getCountryCode() != null + ? new Locale("", mBraveRewardsNativeWorker.getCountryCode()).getDisplayCountry() + : null; + TreeMap sortedCountryMap = new TreeMap(); + ArrayList list = new ArrayList<>(); for (String countryCode : countries) { sortedCountryMap.put(new Locale("", countryCode).getDisplayCountry(), countryCode); + list.add(new Locale("", countryCode).getDisplayCountry()); } - - ArrayList countryList = new ArrayList(); + Collections.sort(list, Collator.getInstance()); + ArrayList countryList = new ArrayList<>(); countryList.add(mActivity.getResources().getString(R.string.select_your_country_title)); - countryList.addAll(sortedCountryMap.keySet()); - String defaultCountry = mBraveRewardsNativeWorker.getCountryCode() != null - ? new Locale("", mBraveRewardsNativeWorker.getCountryCode()).getDisplayCountry() - : null; - if (defaultCountry != null && countryList.contains(defaultCountry)) { - countryList.remove(defaultCountry); - countryList.add(1, defaultCountry); - } + countryList.addAll(list); String[] countryArray = countryList.toArray(new String[countryList.size()]); CountrySelectionSpinnerAdapter countrySelectionSpinnerAdapter = - new CountrySelectionSpinnerAdapter(mActivity, countryList); + new CountrySelectionSpinnerAdapter(mActivity, countryArray); + countrySelectionSpinnerAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); mCountrySpinner.setAdapter(countrySelectionSpinnerAdapter); + mCountrySpinner.setOnTouchListener((view, event) -> { + if (event.getAction() == MotionEvent.ACTION_UP) { + mCountrySpinner.setSelection( + countrySelectionSpinnerAdapter.getPosition(defaultCountry)); + } + return false; + }); mCountrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int pos, long id) {