Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-592: switch to the main thread to change language on 33+ #596

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading