Skip to content

Commit

Permalink
5338 bug fix (#5340)
Browse files Browse the repository at this point in the history
* fix Bug index out bound

* fix Bug index out bound

* fix Bug index out bound

* fix Bug index out bound

* add some comments for that

* add some comments for that

* add some comments for that

* add some comments for that
  • Loading branch information
1162706031 authored Oct 17, 2023
1 parent f06deda commit f130851
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class LanguagesAdapter constructor(
private val selectedLanguages: HashMap<*, String>
) : ArrayAdapter<String?>(context, R.layout.row_item_languages_spinner) {

companion object {
/**
* Represents the default index for the language list. By default, this index corresponds to the
* English language. This serves as a fallback when the user's system language is not present in
* the language_list.xml. Though this default can be changed by the user, it does not affect other
* functionalities of the application. Fixes bug issue 5338
*/
const val DEFAULT_INDEX = 0
}

private var languageNamesList: List<String>
private var languageCodesList: List<String>

Expand Down Expand Up @@ -85,11 +95,33 @@ class LanguagesAdapter constructor(
return languageNamesList[position]
}

/**
* Retrieves the index of the user's default locale from the list of available languages.
*
* This function checks the user's system language and finds its index within the application's
* list of supported languages. If the system language is not supported, or any error occurs,
* it falls back to the default language index, typically representing English.
*
*
* @param context The context used to get the user's system locale.
* @return The index of the user's default language in the supported language list,
* or the default index if the language is not found.
* Note: This function was implemented to address a bug where unsupported system languages
* resulted in an incorrect language selection. Directly returning the result of `indexOf`
* without checking its validity could result in returning an index of -1, leading to ArrayIndex
* OutOfBoundsException.
* [See bug issue 5338]
* It's essential to ensure that the returned index is valid or fall back to a default index.
* Future contributors are advised not to simplify this function without addressing this concern.
*/
fun getIndexOfUserDefaultLocale(context: Context): Int {
return language.codes.indexOf(context.locale!!.language)

val userLanguageCode = context.locale?.language ?: return DEFAULT_INDEX
return language.codes.indexOf(userLanguageCode).takeIf { it >= 0 } ?: DEFAULT_INDEX
}

fun getIndexOfLanguageCode(languageCode: String): Int {

return languageCodesList.indexOf(languageCode)
}

Expand Down

0 comments on commit f130851

Please sign in to comment.