From 029f1cf3bed1663df4182886c8e6660a511ce091 Mon Sep 17 00:00:00 2001 From: Jiayuan Huang Date: Mon, 16 Oct 2023 16:35:38 +1100 Subject: [PATCH 1/8] fix Bug index out bound --- .../java/fr/free/nrw/commons/upload/LanguagesAdapter.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt index a2b673f46c..7b8663776a 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt @@ -25,6 +25,9 @@ class LanguagesAdapter constructor( context: Context, private val selectedLanguages: HashMap<*, String> ) : ArrayAdapter(context, R.layout.row_item_languages_spinner) { + companion object { + const val DEFAULT_INDEX = 0 + } private var languageNamesList: List private var languageCodesList: List @@ -86,7 +89,8 @@ class LanguagesAdapter constructor( } 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 { From 87741e6f1254d2036dc6f90a19e50ab4592f1679 Mon Sep 17 00:00:00 2001 From: Jiayuan Huang <1162706031@qq.com> Date: Mon, 16 Oct 2023 18:12:30 +1100 Subject: [PATCH 2/8] fix Bug index out bound --- app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt index 7b8663776a..632eafbb36 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt @@ -89,6 +89,7 @@ class LanguagesAdapter constructor( } fun getIndexOfUserDefaultLocale(context: Context): Int { + val userLanguageCode = context.locale?.language ?: return DEFAULT_INDEX return language.codes.indexOf(userLanguageCode).takeIf { it >= 0 } ?: DEFAULT_INDEX } From 6044dc301fd1b24b5113b70b8fc843f3f736d238 Mon Sep 17 00:00:00 2001 From: Jiayuan Huang <1162706031@qq.com> Date: Mon, 16 Oct 2023 18:20:45 +1100 Subject: [PATCH 3/8] fix Bug index out bound --- .../java/fr/free/nrw/commons/upload/LanguagesAdapter.kt | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt index 632eafbb36..9abd9287b3 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt @@ -25,9 +25,7 @@ class LanguagesAdapter constructor( context: Context, private val selectedLanguages: HashMap<*, String> ) : ArrayAdapter(context, R.layout.row_item_languages_spinner) { - companion object { - const val DEFAULT_INDEX = 0 - } + private var languageNamesList: List private var languageCodesList: List @@ -89,11 +87,8 @@ class LanguagesAdapter constructor( } fun getIndexOfUserDefaultLocale(context: Context): Int { - - val userLanguageCode = context.locale?.language ?: return DEFAULT_INDEX - return language.codes.indexOf(userLanguageCode).takeIf { it >= 0 } ?: DEFAULT_INDEX + return language.codes.indexOf(context.locale!!.language) } - fun getIndexOfLanguageCode(languageCode: String): Int { return languageCodesList.indexOf(languageCode) } From 25a43e28158e0f02c66bf1024901eee79d21124c Mon Sep 17 00:00:00 2001 From: Jiayuan Huang <1162706031@qq.com> Date: Mon, 16 Oct 2023 18:21:27 +1100 Subject: [PATCH 4/8] fix Bug index out bound --- .../java/fr/free/nrw/commons/upload/LanguagesAdapter.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt index 9abd9287b3..632eafbb36 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt @@ -25,7 +25,9 @@ class LanguagesAdapter constructor( context: Context, private val selectedLanguages: HashMap<*, String> ) : ArrayAdapter(context, R.layout.row_item_languages_spinner) { - + companion object { + const val DEFAULT_INDEX = 0 + } private var languageNamesList: List private var languageCodesList: List @@ -87,8 +89,11 @@ class LanguagesAdapter constructor( } 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) } From 0d59f8a22c881108a1a1c55d419c78f9cf780465 Mon Sep 17 00:00:00 2001 From: Jiayuan Huang <1162706031@qq.com> Date: Mon, 16 Oct 2023 22:17:59 +1100 Subject: [PATCH 5/8] add some comments for that --- .../nrw/commons/upload/LanguagesAdapter.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt index 632eafbb36..c0db11bc89 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt @@ -25,7 +25,14 @@ class LanguagesAdapter constructor( context: Context, private val selectedLanguages: HashMap<*, String> ) : ArrayAdapter(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. Fix bug issue 5338 + */ const val DEFAULT_INDEX = 0 } @@ -88,6 +95,18 @@ 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. + * Fix bug issue 5338 + * + * @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. + */ fun getIndexOfUserDefaultLocale(context: Context): Int { val userLanguageCode = context.locale?.language ?: return DEFAULT_INDEX @@ -95,6 +114,7 @@ class LanguagesAdapter constructor( } fun getIndexOfLanguageCode(languageCode: String): Int { + return languageCodesList.indexOf(languageCode) } From 9e7c735c4d3229c21cbfb9fcd6789b6c54bfebd7 Mon Sep 17 00:00:00 2001 From: Jiayuan Huang <1162706031@qq.com> Date: Mon, 16 Oct 2023 22:23:42 +1100 Subject: [PATCH 6/8] add some comments for that --- .../java/fr/free/nrw/commons/upload/LanguagesAdapter.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt index c0db11bc89..7abf0c75d3 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt @@ -101,11 +101,18 @@ class LanguagesAdapter constructor( * 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. - * Fix bug issue 5338 + * * * @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 { From ab503b4d1cd1a4f5929db3ff6e1f31aada1157a7 Mon Sep 17 00:00:00 2001 From: Jiayuan Huang <1162706031@qq.com> Date: Tue, 17 Oct 2023 00:25:22 +1100 Subject: [PATCH 7/8] add some comments for that --- .../main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt index 7abf0c75d3..9ae54c5b7a 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt @@ -31,7 +31,7 @@ class LanguagesAdapter constructor( * 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. Fix bug issue 5338 + * functionalities of the application. Fixs bug issue 5338 */ const val DEFAULT_INDEX = 0 } From 03aa14e8de40b0c06e5341d53c994de83a31aef3 Mon Sep 17 00:00:00 2001 From: Jiayuan Huang <1162706031@qq.com> Date: Tue, 17 Oct 2023 00:25:51 +1100 Subject: [PATCH 8/8] add some comments for that --- .../main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt index 9ae54c5b7a..85f71a3ffb 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt @@ -31,7 +31,7 @@ class LanguagesAdapter constructor( * 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. Fixs bug issue 5338 + * functionalities of the application. Fixes bug issue 5338 */ const val DEFAULT_INDEX = 0 }