mirror of
https://github.com/home-assistant/android
synced 2024-09-06 17:23:32 +00:00
Bugfix/various (#546)
* Catch geocode sensor crashes. * Remove battery from location sensor.
This commit is contained in:
parent
825dd78f1e
commit
e28d0fee62
|
@ -4,9 +4,7 @@ import android.app.PendingIntent
|
|||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.location.Location
|
||||
import android.os.BatteryManager
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import com.google.android.gms.location.Geofence
|
||||
|
@ -55,7 +53,7 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
|
|||
when (intent.action) {
|
||||
Intent.ACTION_BOOT_COMPLETED,
|
||||
ACTION_REQUEST_LOCATION_UPDATES -> setupLocationTracking(context)
|
||||
ACTION_PROCESS_LOCATION -> handleLocationUpdate(context, intent)
|
||||
ACTION_PROCESS_LOCATION -> handleLocationUpdate(intent)
|
||||
ACTION_PROCESS_GEO -> handleGeoUpdate(context, intent)
|
||||
ACTION_REQUEST_ACCURATE_LOCATION_UPDATE -> requestSingleAccurateLocation(context)
|
||||
else -> Log.w(TAG, "Unknown intent action: ${intent.action}!")
|
||||
|
@ -133,13 +131,13 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun handleLocationUpdate(context: Context, intent: Intent) {
|
||||
private fun handleLocationUpdate(intent: Intent) {
|
||||
Log.d(TAG, "Received location update.")
|
||||
LocationResult.extractResult(intent)?.lastLocation?.let {
|
||||
if (it.accuracy > MINIMUM_ACCURACY) {
|
||||
Log.w(TAG, "Location accuracy didn't meet requirements, disregarding: $it")
|
||||
} else {
|
||||
sendLocationUpdate(it, context)
|
||||
sendLocationUpdate(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -156,11 +154,11 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
|
|||
Log.w(TAG, "Geofence location accuracy didn't meet requirements, requesting new location.")
|
||||
requestSingleAccurateLocation(context)
|
||||
} else {
|
||||
sendLocationUpdate(geofencingEvent.triggeringLocation, context)
|
||||
sendLocationUpdate(geofencingEvent.triggeringLocation)
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendLocationUpdate(location: Location, context: Context) {
|
||||
private fun sendLocationUpdate(location: Location) {
|
||||
Log.d(
|
||||
TAG, "Last Location: " +
|
||||
"\nCoords:(${location.latitude}, ${location.longitude})" +
|
||||
|
@ -171,7 +169,6 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
|
|||
"",
|
||||
arrayOf(location.latitude, location.longitude),
|
||||
location.accuracy.toInt(),
|
||||
getBatteryLevel(context),
|
||||
location.speed.toInt(),
|
||||
location.altitude.toInt(),
|
||||
location.bearing.toInt(),
|
||||
|
@ -239,11 +236,11 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
|
|||
Log.d(TAG, "Got single accurate location update: ${locationResult?.lastLocation}")
|
||||
if (locationResult != null && locationResult.lastLocation.accuracy <= 1) {
|
||||
Log.d(TAG, "Location accurate enough, all done with high accuracy.")
|
||||
runBlocking { sendLocationUpdate(locationResult.lastLocation, context) }
|
||||
runBlocking { sendLocationUpdate(locationResult.lastLocation) }
|
||||
LocationServices.getFusedLocationProviderClient(context).removeLocationUpdates(this)
|
||||
} else if (numberCalls >= maxRetries) {
|
||||
Log.d(TAG, "No location was accurate enough, sending our last location anyway")
|
||||
runBlocking { sendLocationUpdate(locationResult!!.lastLocation, context) }
|
||||
runBlocking { sendLocationUpdate(locationResult!!.lastLocation) }
|
||||
} else {
|
||||
Log.w(TAG, "Location not accurate enough on retry $numberCalls of $maxRetries")
|
||||
}
|
||||
|
@ -252,18 +249,4 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
|
|||
null
|
||||
)
|
||||
}
|
||||
|
||||
private fun getBatteryLevel(context: Context): Int? {
|
||||
val batteryIntent =
|
||||
context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||
val level = batteryIntent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
|
||||
val scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
|
||||
|
||||
if (level == -1 || scale == -1) {
|
||||
Log.e(TAG, "Issue getting battery level!")
|
||||
return null
|
||||
}
|
||||
|
||||
return (level.toFloat() / scale.toFloat() * 100.0f).toInt()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,9 +25,14 @@ class AllSensorsUpdaterImpl(
|
|||
|
||||
registerSensors(sensorManagers)
|
||||
|
||||
val success = integrationUseCase.updateSensors(
|
||||
sensorManagers.flatMap { it.getSensors(appContext) }.toTypedArray()
|
||||
)
|
||||
var success = false
|
||||
try {
|
||||
success = integrationUseCase.updateSensors(
|
||||
sensorManagers.flatMap { it.getSensors(appContext) }.toTypedArray()
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Exception while updating sensors.", e)
|
||||
}
|
||||
|
||||
// We failed to update a sensor, we should register all the sensors again.
|
||||
if (!success) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package io.homeassistant.companion.android.sensors
|
|||
|
||||
import android.content.Context
|
||||
import android.location.Geocoder
|
||||
import android.util.Log
|
||||
import com.google.android.gms.location.LocationServices
|
||||
import com.google.android.gms.tasks.Tasks
|
||||
import io.homeassistant.companion.android.background.LocationBroadcastReceiver
|
||||
|
@ -9,6 +10,11 @@ import io.homeassistant.companion.android.domain.integration.Sensor
|
|||
import io.homeassistant.companion.android.domain.integration.SensorRegistration
|
||||
|
||||
class GeocodeSensorManager : SensorManager {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "GeocodeSM"
|
||||
}
|
||||
|
||||
override fun getSensorRegistrations(context: Context): List<SensorRegistration<Any>> {
|
||||
val sensor = getGeocodedLocation(context)
|
||||
if (sensor != null) {
|
||||
|
@ -37,28 +43,33 @@ class GeocodeSensorManager : SensorManager {
|
|||
if (it.accuracy > LocationBroadcastReceiver.MINIMUM_ACCURACY)
|
||||
return null
|
||||
|
||||
Geocoder(context)
|
||||
.getFromLocation(it.latitude, it.longitude, 1)
|
||||
.firstOrNull()?.let { address ->
|
||||
return Sensor(
|
||||
"geocoded_location",
|
||||
if (address.maxAddressLineIndex >= 0) address.getAddressLine(0) else "Unknown",
|
||||
"sensor",
|
||||
"mdi:map",
|
||||
mapOf(
|
||||
"Administrative Area" to address.adminArea,
|
||||
"Country" to address.countryName,
|
||||
"ISO Country Code" to address.countryCode,
|
||||
"Locality" to address.locality,
|
||||
"Location" to listOf(address.latitude, address.longitude),
|
||||
"Postal Code" to address.postalCode,
|
||||
"Sub Administrative Area" to address.subAdminArea,
|
||||
"Sub Locality" to address.subLocality,
|
||||
"Sub Thoroughfare" to address.subThoroughfare,
|
||||
"Thoroughfare" to address.thoroughfare
|
||||
try {
|
||||
Geocoder(context)
|
||||
.getFromLocation(it.latitude, it.longitude, 1)
|
||||
.firstOrNull()?.let { address ->
|
||||
return Sensor(
|
||||
"geocoded_location",
|
||||
if (address.maxAddressLineIndex >= 0) address.getAddressLine(0) else "Unknown",
|
||||
"sensor",
|
||||
"mdi:map",
|
||||
mapOf(
|
||||
"Administrative Area" to address.adminArea,
|
||||
"Country" to address.countryName,
|
||||
"ISO Country Code" to address.countryCode,
|
||||
"Locality" to address.locality,
|
||||
"Location" to listOf(address.latitude, address.longitude),
|
||||
"Postal Code" to address.postalCode,
|
||||
"Sub Administrative Area" to address.subAdminArea,
|
||||
"Sub Locality" to address.subLocality,
|
||||
"Sub Thoroughfare" to address.subThoroughfare,
|
||||
"Thoroughfare" to address.thoroughfare
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// We don't want to crash if the device cannot get a geocoded location
|
||||
Log.e(TAG, "Issue getting geocoded location ", e)
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
|
|
@ -386,7 +386,6 @@ class IntegrationRepositoryImpl @Inject constructor(
|
|||
updateLocation.locationName,
|
||||
updateLocation.gps,
|
||||
updateLocation.gpsAccuracy,
|
||||
updateLocation.battery,
|
||||
updateLocation.speed,
|
||||
updateLocation.altitude,
|
||||
updateLocation.course,
|
||||
|
|
|
@ -4,7 +4,6 @@ data class UpdateLocationRequest(
|
|||
val locationName: String,
|
||||
val gps: Array<Double>,
|
||||
val gpsAccuracy: Int,
|
||||
val battery: Int?,
|
||||
val speed: Int,
|
||||
val altitude: Int,
|
||||
val course: Int,
|
||||
|
|
|
@ -296,8 +296,7 @@ object IntegrationRepositoryImplSpec : Spek({
|
|||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
4
|
||||
)
|
||||
val integrationRequest =
|
||||
IntegrationRequest(
|
||||
|
@ -306,7 +305,6 @@ object IntegrationRepositoryImplSpec : Spek({
|
|||
location.locationName,
|
||||
location.gps,
|
||||
location.gpsAccuracy,
|
||||
location.battery,
|
||||
location.speed,
|
||||
location.altitude,
|
||||
location.course,
|
||||
|
@ -346,8 +344,7 @@ object IntegrationRepositoryImplSpec : Spek({
|
|||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
4
|
||||
)
|
||||
val integrationRequest =
|
||||
IntegrationRequest(
|
||||
|
@ -356,7 +353,6 @@ object IntegrationRepositoryImplSpec : Spek({
|
|||
location.locationName,
|
||||
location.gps,
|
||||
location.gpsAccuracy,
|
||||
location.battery,
|
||||
location.speed,
|
||||
location.altitude,
|
||||
location.course,
|
||||
|
@ -395,8 +391,7 @@ object IntegrationRepositoryImplSpec : Spek({
|
|||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
4
|
||||
)
|
||||
val integrationRequest =
|
||||
IntegrationRequest(
|
||||
|
@ -405,7 +400,6 @@ object IntegrationRepositoryImplSpec : Spek({
|
|||
location.locationName,
|
||||
location.gps,
|
||||
location.gpsAccuracy,
|
||||
location.battery,
|
||||
location.speed,
|
||||
location.altitude,
|
||||
location.course,
|
||||
|
@ -443,8 +437,7 @@ object IntegrationRepositoryImplSpec : Spek({
|
|||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
4
|
||||
)
|
||||
val integrationRequest =
|
||||
IntegrationRequest(
|
||||
|
@ -453,7 +446,6 @@ object IntegrationRepositoryImplSpec : Spek({
|
|||
location.locationName,
|
||||
location.gps,
|
||||
location.gpsAccuracy,
|
||||
location.battery,
|
||||
location.speed,
|
||||
location.altitude,
|
||||
location.course,
|
||||
|
@ -507,8 +499,7 @@ object IntegrationRepositoryImplSpec : Spek({
|
|||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
4
|
||||
)
|
||||
|
||||
lateinit var thrown: Throwable
|
||||
|
|
|
@ -66,8 +66,7 @@ object IntegrationServiceSpec : Spek({
|
|||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
4
|
||||
)
|
||||
val integrationRequest =
|
||||
IntegrationRequest(
|
||||
|
|
|
@ -4,7 +4,6 @@ data class UpdateLocation(
|
|||
val locationName: String,
|
||||
val gps: Array<Double>,
|
||||
val gpsAccuracy: Int,
|
||||
val battery: Int?,
|
||||
val speed: Int,
|
||||
val altitude: Int,
|
||||
val course: Int,
|
||||
|
|
Loading…
Reference in a new issue