mirror of
https://github.com/home-assistant/android
synced 2024-09-19 08:01:31 +00:00
Fix geocoded crash, report expected minimum accuracy for location (#832)
* Fix geocoded crash, report minimum accuracy for location * Lint * Always log exceptions! Co-authored-by: Justin Bassett <bassett.justint@gmail.com>
This commit is contained in:
parent
5ee23a764e
commit
e6f2e589d2
|
@ -7,6 +7,7 @@ import android.location.Geocoder
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.google.android.gms.location.LocationServices
|
import com.google.android.gms.location.LocationServices
|
||||||
|
import java.lang.Exception
|
||||||
|
|
||||||
class GeocodeSensorManager : SensorManager {
|
class GeocodeSensorManager : SensorManager {
|
||||||
|
|
||||||
|
@ -46,17 +47,20 @@ class GeocodeSensorManager : SensorManager {
|
||||||
return
|
return
|
||||||
val locApi = LocationServices.getFusedLocationProviderClient(context)
|
val locApi = LocationServices.getFusedLocationProviderClient(context)
|
||||||
locApi.lastLocation.addOnSuccessListener { location ->
|
locApi.lastLocation.addOnSuccessListener { location ->
|
||||||
if (location == null) {
|
|
||||||
Log.e(TAG, "Somehow location is null even though it was successful")
|
|
||||||
return@addOnSuccessListener
|
|
||||||
}
|
|
||||||
|
|
||||||
var address: Address? = null
|
var address: Address? = null
|
||||||
if (location.accuracy <= LocationSensorManager.MINIMUM_ACCURACY)
|
try {
|
||||||
address = Geocoder(context)
|
if (location == null) {
|
||||||
.getFromLocation(location.latitude, location.longitude, 1)
|
Log.e(TAG, "Somehow location is null even though it was successful")
|
||||||
.firstOrNull()
|
return@addOnSuccessListener
|
||||||
|
}
|
||||||
|
|
||||||
|
if (location.accuracy <= LocationSensorManager.MINIMUM_ACCURACY)
|
||||||
|
address = Geocoder(context)
|
||||||
|
.getFromLocation(location.latitude, location.longitude, 1)
|
||||||
|
.firstOrNull()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Failed to get geocoded location", e)
|
||||||
|
}
|
||||||
val attributes = address?.let {
|
val attributes = address?.let {
|
||||||
mapOf(
|
mapOf(
|
||||||
"Administrative Area" to it.adminArea,
|
"Administrative Area" to it.adminArea,
|
||||||
|
|
|
@ -191,10 +191,14 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
|
||||||
"\nAccuracy: ${location.accuracy}" +
|
"\nAccuracy: ${location.accuracy}" +
|
||||||
"\nBearing: ${location.bearing}"
|
"\nBearing: ${location.bearing}"
|
||||||
)
|
)
|
||||||
|
var accuracy = 0
|
||||||
|
if (location.accuracy.toInt() >= 0) {
|
||||||
|
accuracy = location.accuracy.toInt()
|
||||||
|
}
|
||||||
val updateLocation = UpdateLocation(
|
val updateLocation = UpdateLocation(
|
||||||
"",
|
"",
|
||||||
arrayOf(location.latitude, location.longitude),
|
arrayOf(location.latitude, location.longitude),
|
||||||
location.accuracy.toInt(),
|
accuracy,
|
||||||
location.speed.toInt(),
|
location.speed.toInt(),
|
||||||
location.altitude.toInt(),
|
location.altitude.toInt(),
|
||||||
location.bearing.toInt(),
|
location.bearing.toInt(),
|
||||||
|
|
Loading…
Reference in a new issue