Skip to content

Commit

Permalink
ISSUE-592: switch to the main thread to change language on 33+ (Kaspe…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikitae57 authored Nov 14, 2023
1 parent 971b8d9 commit e07ac1a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.kaspersky.kaspresso.device.languages

import android.app.Instrumentation
import android.app.LocaleManager
import android.content.Context
import android.os.Build
import android.os.Handler
import android.os.LocaleList
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.ConfigurationCompat
Expand All @@ -15,7 +17,7 @@ import java.util.Locale
*/
class LanguageImpl(
private val logger: UiTestLogger,
private val context: Context
private val instrumentation: Instrumentation,
) : Language {

override fun switchInApp(locale: Locale) {
Expand Down Expand Up @@ -44,14 +46,16 @@ class LanguageImpl(
}

private fun getCurrentLocale(): Locale? =
ConfigurationCompat.getLocales(context.resources.configuration).get(0)
ConfigurationCompat.getLocales(instrumentation.targetContext.resources.configuration).get(0)

private fun applyCurrentLocaleToContext(locale: Locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val localeManager = context.getSystemService(Context.LOCALE_SERVICE) as LocaleManager
val localeManager = instrumentation.targetContext.getSystemService(Context.LOCALE_SERVICE) as LocaleManager
localeManager.applicationLocales = LocaleList.forLanguageTags(locale.toLanguageTag())
} else {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.create(locale))
Handler(instrumentation.targetContext.mainLooper).post {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.create(locale))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ data class Kaspresso(
instrumentalDependencyProviderFactory.getComponentProvider<ExploitImpl>(instrumentation),
adbServer
)
if (!::language.isInitialized) language = LanguageImpl(libLogger, instrumentation.targetContext)
if (!::language.isInitialized) language = LanguageImpl(libLogger, instrumentation)
if (!::logcat.isInitialized) logcat = LogcatImpl(libLogger, adbServer)

if (!::flakySafetyParams.isInitialized) flakySafetyParams = FlakySafetyParams.default()
Expand Down

0 comments on commit e07ac1a

Please sign in to comment.