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:
Daniel Shokouhi 2020-08-27 10:15:58 -07:00 committed by GitHub
parent 5ee23a764e
commit e6f2e589d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View file

@ -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 ->
var address: Address? = null
try {
if (location == null) { if (location == null) {
Log.e(TAG, "Somehow location is null even though it was successful") Log.e(TAG, "Somehow location is null even though it was successful")
return@addOnSuccessListener return@addOnSuccessListener
} }
var address: Address? = null
if (location.accuracy <= LocationSensorManager.MINIMUM_ACCURACY) if (location.accuracy <= LocationSensorManager.MINIMUM_ACCURACY)
address = Geocoder(context) address = Geocoder(context)
.getFromLocation(location.latitude, location.longitude, 1) .getFromLocation(location.latitude, location.longitude, 1)
.firstOrNull() .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,

View file

@ -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(),