From a3630393bf6a418db436998cb9aa4e3915d8ae60 Mon Sep 17 00:00:00 2001 From: bbrockbernd <47760450+bbrockbernd@users.noreply.github.com> Date: Sat, 20 Apr 2024 20:09:17 +0200 Subject: [PATCH] Fix runBlocking in coroutines (#4340) * Fixed runBlocking call in suspend function DefaultSliderControl.performAction * Fixed runBlocking in LanguagesManager.getCurrentLang. This function is always called from a coroutine. * Fixed runBlocking in LanguagesManager.saveLang. This function is always called from a coroutine. * Fixed runBlocking in LocationSensorManager.sendLocationUpdate. This function is always called from a coroutine. * Fixed runBlocking for ThemesManager.saveTheme and ThemesManager.getCurrentTheme * Fixed ktlint --- .../android/sensors/LocationSensorManager.kt | 58 +++++++++---------- .../android/controls/DefaultSliderControl.kt | 19 +++--- .../settings/language/LanguagesManager.kt | 8 +-- .../companion/android/themes/ThemesManager.kt | 10 ++-- .../companion/android/util/ChangeLog.kt | 3 +- 5 files changed, 47 insertions(+), 51 deletions(-) diff --git a/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt b/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt index d4e41f3f28f..e6b4f1f258f 100644 --- a/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt +++ b/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt @@ -829,7 +829,7 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager { } } - private fun sendLocationUpdate(location: Location, serverId: Int, trigger: LocationUpdateTrigger?) { + private suspend fun sendLocationUpdate(location: Location, serverId: Int, trigger: LocationUpdateTrigger?) { Log.d( TAG, "Last Location: " + @@ -844,35 +844,33 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager { val updateLocation: UpdateLocation val updateLocationAs: String val updateLocationString: String - runBlocking { - updateLocationAs = getSendLocationAsSetting(serverId) - if (updateLocationAs == SEND_LOCATION_AS_ZONE_ONLY) { - val zones = getZones(serverId) - val locationZone = zones - .filter { !it.attributes.passive && it.containsWithAccuracy(location) } - .minByOrNull { it.attributes.radius } - updateLocation = UpdateLocation( - gps = null, - gpsAccuracy = null, - locationName = locationZone?.entityId?.split(".")?.get(1) ?: ZONE_NAME_NOT_HOME, - speed = null, - altitude = null, - course = null, - verticalAccuracy = null - ) - updateLocationString = updateLocation.locationName!! - } else { - updateLocation = UpdateLocation( - gps = arrayOf(location.latitude, location.longitude), - gpsAccuracy = accuracy, - locationName = null, - speed = location.speed.toInt(), - altitude = location.altitude.toInt(), - course = location.bearing.toInt(), - verticalAccuracy = if (Build.VERSION.SDK_INT >= 26) location.verticalAccuracyMeters.toInt() else 0 - ) - updateLocationString = updateLocation.gps.contentToString() - } + updateLocationAs = getSendLocationAsSetting(serverId) + if (updateLocationAs == SEND_LOCATION_AS_ZONE_ONLY) { + val zones = getZones(serverId) + val locationZone = zones + .filter { !it.attributes.passive && it.containsWithAccuracy(location) } + .minByOrNull { it.attributes.radius } + updateLocation = UpdateLocation( + gps = null, + gpsAccuracy = null, + locationName = locationZone?.entityId?.split(".")?.get(1) ?: ZONE_NAME_NOT_HOME, + speed = null, + altitude = null, + course = null, + verticalAccuracy = null + ) + updateLocationString = updateLocation.locationName!! + } else { + updateLocation = UpdateLocation( + gps = arrayOf(location.latitude, location.longitude), + gpsAccuracy = accuracy, + locationName = null, + speed = location.speed.toInt(), + altitude = location.altitude.toInt(), + course = location.bearing.toInt(), + verticalAccuracy = if (Build.VERSION.SDK_INT >= 26) location.verticalAccuracyMeters.toInt() else 0 + ) + updateLocationString = updateLocation.gps.contentToString() } val now = System.currentTimeMillis() diff --git a/app/src/main/java/io/homeassistant/companion/android/controls/DefaultSliderControl.kt b/app/src/main/java/io/homeassistant/companion/android/controls/DefaultSliderControl.kt index bf8e04df030..c3d263b5058 100644 --- a/app/src/main/java/io/homeassistant/companion/android/controls/DefaultSliderControl.kt +++ b/app/src/main/java/io/homeassistant/companion/android/controls/DefaultSliderControl.kt @@ -11,7 +11,6 @@ import androidx.annotation.RequiresApi import io.homeassistant.companion.android.common.R as commonR import io.homeassistant.companion.android.common.data.integration.Entity import io.homeassistant.companion.android.common.data.integration.IntegrationRepository -import kotlinx.coroutines.runBlocking @RequiresApi(Build.VERSION_CODES.R) object DefaultSliderControl : HaControl { @@ -45,16 +44,14 @@ object DefaultSliderControl : HaControl { integrationRepository: IntegrationRepository, action: ControlAction ): Boolean { - return runBlocking { - integrationRepository.callService( - action.templateId.split(".")[0], - "set_value", - hashMapOf( - "entity_id" to action.templateId, - "value" to (action as? FloatAction)?.newValue.toString() - ) + integrationRepository.callService( + action.templateId.split(".")[0], + "set_value", + hashMapOf( + "entity_id" to action.templateId, + "value" to (action as? FloatAction)?.newValue.toString() ) - return@runBlocking true - } + ) + return true } } diff --git a/app/src/main/java/io/homeassistant/companion/android/settings/language/LanguagesManager.kt b/app/src/main/java/io/homeassistant/companion/android/settings/language/LanguagesManager.kt index ab8420126bf..5b732e6c609 100644 --- a/app/src/main/java/io/homeassistant/companion/android/settings/language/LanguagesManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/settings/language/LanguagesManager.kt @@ -21,8 +21,8 @@ class LanguagesManager @Inject constructor( private const val SYSTEM_MANAGES_LOCALE = "system_managed" } - fun getCurrentLang(): String { - return runBlocking { + suspend fun getCurrentLang(): String { + return run { val lang = prefs.getCurrentLang() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { migrateLangSetting() @@ -38,8 +38,8 @@ class LanguagesManager @Inject constructor( } } - fun saveLang(lang: String?) { - return runBlocking { + suspend fun saveLang(lang: String?) { + return run { if (!lang.isNullOrEmpty()) { val currentLang = getCurrentLang() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { diff --git a/app/src/main/java/io/homeassistant/companion/android/themes/ThemesManager.kt b/app/src/main/java/io/homeassistant/companion/android/themes/ThemesManager.kt index e616aeb058e..f072dec507d 100644 --- a/app/src/main/java/io/homeassistant/companion/android/themes/ThemesManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/themes/ThemesManager.kt @@ -15,8 +15,8 @@ class ThemesManager @Inject constructor( private val themesUseCase: PrefsRepository ) { - fun getCurrentTheme(): String { - return runBlocking { + suspend fun getCurrentTheme(): String { + return run { val theme = themesUseCase.getCurrentTheme() if (theme.isNullOrEmpty()) { val toSetTheme = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { @@ -32,8 +32,8 @@ class ThemesManager @Inject constructor( } } - fun saveTheme(theme: String?) { - return runBlocking { + suspend fun saveTheme(theme: String?) { + return run { if (!theme.isNullOrEmpty()) { val currentTheme = getCurrentTheme() if (currentTheme != theme) { @@ -46,7 +46,7 @@ class ThemesManager @Inject constructor( @Suppress("DEPRECATION") fun setThemeForWebView(context: Context, webSettings: WebSettings) { - val theme = getCurrentTheme() + val theme = runBlocking { getCurrentTheme() } setNightModeBasedOnTheme(theme) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU && diff --git a/app/src/main/java/io/homeassistant/companion/android/util/ChangeLog.kt b/app/src/main/java/io/homeassistant/companion/android/util/ChangeLog.kt index 8fa55731d5b..ec19a6658ad 100755 --- a/app/src/main/java/io/homeassistant/companion/android/util/ChangeLog.kt +++ b/app/src/main/java/io/homeassistant/companion/android/util/ChangeLog.kt @@ -7,6 +7,7 @@ import info.hannes.changelog.ChangeLog import io.homeassistant.companion.android.R import io.homeassistant.companion.android.themes.ThemesManager import javax.inject.Inject +import kotlinx.coroutines.runBlocking class ChangeLog @Inject constructor() { @@ -14,7 +15,7 @@ class ChangeLog @Inject constructor() { lateinit var themesManager: ThemesManager fun showChangeLog(context: Context, forceShow: Boolean) { - val isDarkTheme = when (themesManager.getCurrentTheme()) { + val isDarkTheme = when (runBlocking { themesManager.getCurrentTheme() }) { "android", "system" -> { val nightModeFlags = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK