Skip to content

Commit ab28c5f

Browse files
committed
Improve GPS location fallbacks and error handling
1 parent fd2bb49 commit ab28c5f

File tree

1 file changed

+18
-19
lines changed
  • location/src/main/java/com/kylecorry/andromeda/location

1 file changed

+18
-19
lines changed

location/src/main/java/com/kylecorry/andromeda/location/GPS.kt

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.location.LocationManager
77
import android.os.Handler
88
import android.os.Looper
99
import androidx.core.content.getSystemService
10+
import androidx.core.location.LocationCompat
1011
import com.kylecorry.andromeda.core.sensors.AbstractSensor
1112
import com.kylecorry.andromeda.core.sensors.Quality
1213
import com.kylecorry.andromeda.core.tryOrDefault
@@ -111,12 +112,12 @@ class GPS(
111112

112113
// Can only get NMEA with fine location permission
113114
if (Permissions.canGetFineLocation(context)) {
114-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
115-
nmeaListener?.let {
116-
locationManager?.addNmeaListener(it, Handler(Looper.getMainLooper()))
117-
}
118-
} else {
119-
tryOrNothing {
115+
tryOrNothing {
116+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
117+
nmeaListener?.let {
118+
locationManager?.addNmeaListener(it, Handler(Looper.getMainLooper()))
119+
}
120+
} else {
120121
@Suppress("DEPRECATION")
121122
locationManager?.addNmeaListener(legacyNmeaListener)
122123
}
@@ -150,7 +151,8 @@ class GPS(
150151
_location = Coordinate(location.latitude, location.longitude)
151152
_time = Instant.ofEpochMilli(location.time)
152153
_satellites =
153-
if (location.extras?.containsKey("satellites") == true) (location.extras?.getInt("satellites") ?: 0) else 0
154+
if (location.extras?.containsKey("satellites") == true) (location.extras?.getInt("satellites")
155+
?: 0) else 0
154156
_altitude = if (location.hasAltitude()) location.altitude.toFloat() else 0f
155157
val accuracy = if (location.hasAccuracy()) location.accuracy else null
156158
_quality = when {
@@ -160,20 +162,17 @@ class GPS(
160162
else -> Quality.Unknown
161163
}
162164
_horizontalAccuracy = accuracy ?: 0f
163-
_verticalAccuracy =
164-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && location.hasVerticalAccuracy()) {
165-
location.verticalAccuracyMeters
166-
} else {
167-
null
168-
}
165+
_verticalAccuracy = if (LocationCompat.hasVerticalAccuracy(location)) {
166+
LocationCompat.getVerticalAccuracyMeters(location)
167+
} else {
168+
null
169+
}
169170
// TODO: Add speed accuracy to IGPS
170171
_speed = if (location.hasSpeed()) {
171-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && location.hasSpeedAccuracy()) {
172-
if (location.speed < location.speedAccuracyMetersPerSecond * 0.68) {
173-
0f
174-
} else {
175-
location.speed
176-
}
172+
if (LocationCompat.hasSpeedAccuracy(location) &&
173+
location.speed < LocationCompat.getSpeedAccuracyMetersPerSecond(location) * 0.68
174+
) {
175+
0f
177176
} else {
178177
location.speed
179178
}

0 commit comments

Comments
 (0)