Move away from runBlocking and fix some bugs in geofence updates and clearing notification (#3409)

This commit is contained in:
Daniel Shokouhi 2023-03-10 10:14:22 -08:00 committed by GitHub
parent 3ebae7ee08
commit 52c2f3a8c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 11 deletions

View file

@ -10,7 +10,6 @@ import android.os.Build
import android.os.Looper
import android.os.PowerManager
import android.util.Log
import android.widget.Toast
import androidx.core.content.getSystemService
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.Geofence
@ -747,17 +746,15 @@ class LocationSensorManager : LocationSensorManagerBase() {
"vertical_accuracy" to if (Build.VERSION.SDK_INT >= 26) geofencingEvent.triggeringLocation!!.verticalAccuracyMeters.toInt() else 0,
"zone" to zone
)
runBlocking {
ioScope.launch {
try {
val serverId = zone.split("_")[0].toIntOrNull() ?: return@runBlocking
val enabled = isEnabled(latestContext, backgroundLocation, serverId)
if (!enabled) return@runBlocking
val serverId = zone.split("_")[0].toIntOrNull() ?: return@launch
val enabled = isEnabled(latestContext, zoneLocation, serverId)
if (!enabled) return@launch
serverManager.integrationRepository(serverId).fireEvent(zoneStatusEvent, zoneAttr as Map<String, Any>)
Log.d(TAG, "Event sent to Home Assistant")
} catch (e: Exception) {
Log.e(TAG, "Unable to send event to Home Assistant", e)
Toast.makeText(latestContext, commonR.string.zone_event_failure, Toast.LENGTH_LONG)
.show()
}
}
}
@ -777,8 +774,10 @@ class LocationSensorManager : LocationSensorManagerBase() {
)
requestSingleAccurateLocation()
} else {
getEnabledServers(latestContext, backgroundLocation).forEach {
sendLocationUpdate(geofencingEvent.triggeringLocation!!, it, true)
getEnabledServers(latestContext, zoneLocation).forEach {
ioScope.launch {
sendLocationUpdate(geofencingEvent.triggeringLocation!!, it, true)
}
}
}

View file

@ -9,7 +9,10 @@ import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.data.servers.ServerManager
import io.homeassistant.companion.android.common.util.cancelGroupIfNeeded
import io.homeassistant.companion.android.database.notification.NotificationDao
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
@AndroidEntryPoint
@ -22,6 +25,8 @@ class NotificationDeleteReceiver : BroadcastReceiver() {
const val TAG = "NotifDeleteReceiver"
}
private val ioScope: CoroutineScope = CoroutineScope(Dispatchers.IO + Job())
@Inject
lateinit var serverManager: ServerManager
@ -40,7 +45,7 @@ class NotificationDeleteReceiver : BroadcastReceiver() {
// Then only the empty group is left and needs to be cancelled
notificationManagerCompat.cancelGroupIfNeeded(group, groupId)
runBlocking {
ioScope.launch {
try {
val databaseId = intent.getLongExtra(EXTRA_NOTIFICATION_DB, 0)
val serverId = notificationDao.get(databaseId.toInt())?.serverId ?: ServerManager.SERVER_ID_ACTIVE