Skip to content

Commit

Permalink
Fix eof for setLocale (#2180)
Browse files Browse the repository at this point in the history
* Fix eof when setting device locale. Exception was not being propagated which probably was keeping connections hanging until socket timeout.

* Return different error code when exception is thrown at validating locale

* added logs

* improve logs

* improve log and result code
  • Loading branch information
lucaswiechmann authored Dec 10, 2024
1 parent fa9588d commit 10019b5
Showing 1 changed file with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import android.util.Log
import dev.mobile.maestro.handlers.LocaleSettingHandler
import org.apache.commons.lang3.LocaleUtils
import java.util.*
import java.lang.Exception

class LocaleSettingReceiver : BroadcastReceiver(), HasAction {

override fun onReceive(context: Context, intent: Intent) {
var language = intent.getStringExtra(LANG)
var country = intent.getStringExtra(COUNTRY)

if (language == null || country == null) {
Log.w(TAG, "It is required to provide both language and country, for example: " +
"am broadcast -a dev.mobile.maestro --es lang ja --es country JP")
Log.w(
TAG, "It is required to provide both language and country, for example: " +
"am broadcast -a dev.mobile.maestro --es lang ja --es country JP"
)
Log.i(TAG, "Set en-US by default.")
language = "en"
country = "US"
Expand All @@ -25,39 +29,48 @@ class LocaleSettingReceiver : BroadcastReceiver(), HasAction {

Log.i(TAG, "Obtained locale: $locale")

val script = intent.getStringExtra(SCRIPT)
if (script != null) {
Locale.Builder().setLocale(locale).setScript(script).build().also { locale = it }
}
try {
Log.i(TAG, "getting string extra for device locale")
val script = intent.getStringExtra(SCRIPT)
if (script != null) {
Log.i(TAG, "setting script with device locale")
Locale.Builder().setLocale(locale).setScript(script).build().also { locale = it }
Log.i(TAG, "script set for device locale")
}

if (!LocaleUtils.isAvailableLocale(locale)) {
val approximateMatchesLc = matchLocales(language, country)
if (!LocaleUtils.isAvailableLocale(locale)) {
val approximateMatchesLc = matchLocales(language, country)

if (approximateMatchesLc.isNotEmpty() && script.isNullOrBlank()) {
Log.i(
TAG,
"The locale $locale is not known. Selecting the closest known one ${approximateMatchesLc[0]} instead"
)
locale = approximateMatchesLc[0]
} else {
val approximateMatchesL = matchLocales(language)
if (approximateMatchesL.isEmpty()) {
Log.e(
if (approximateMatchesLc.isNotEmpty() && script.isNullOrBlank()) {
Log.i(
TAG,
"The locale $locale is not known. Only the following locales are available: ${LocaleUtils.availableLocaleList()}"
"The locale $locale is not known. Selecting the closest known one ${approximateMatchesLc[0]} instead"
)
locale = approximateMatchesLc[0]
} else {
Log.e(
TAG,
"The locale $locale is not known. " +
"The following locales are available for the $language language: $approximateMatchesL" +
"The following locales are available altogether: ${LocaleUtils.availableLocaleList()}"
)
val approximateMatchesL = matchLocales(language)
if (approximateMatchesL.isEmpty()) {
Log.e(
TAG,
"The locale $locale is not known. Only the following locales are available: ${LocaleUtils.availableLocaleList()}"
)
} else {
Log.e(
TAG,
"The locale $locale is not known. " +
"The following locales are available for the $language language: $approximateMatchesL" +
"The following locales are available altogether: ${LocaleUtils.availableLocaleList()}"
)
}
resultCode = RESULT_LOCALE_NOT_VALID
resultData = "Failed to set locale $locale, the locale is not valid"
return
}
resultCode = RESULT_LOCALE_NOT_VALID
resultData = "Failed to set locale $locale, the locale is not valid"
return
}
} catch (e: Exception) {
Log.e(TAG, "Failed to validate device locale", e)
resultCode = RESULT_LOCALE_VALIDATION_FAILED
resultData = "Failed to set locale $locale: ${e.message}"
}

try {
Expand Down Expand Up @@ -108,5 +121,6 @@ class LocaleSettingReceiver : BroadcastReceiver(), HasAction {
private const val RESULT_SUCCESS = 0
private const val RESULT_LOCALE_NOT_VALID = 1
private const val RESULT_UPDATE_CONFIGURATION_FAILED = 2
private const val RESULT_LOCALE_VALIDATION_FAILED = 3
}
}

0 comments on commit 10019b5

Please sign in to comment.