Skip to content

Commit a4d02f2

Browse files
committed
Disallow compass azimuth outside range but allow finite angles
1 parent cca12d6 commit a4d02f2

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

data/src/main/java/io/trewartha/positional/data/compass/AospCompassAzimuthRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ class AospCompassAzimuthRepository @Inject constructor(
6565
accelerometerAccuracy,
6666
magnetometerAccuracy
6767
) { rotation, accelerometerAccuracy, magnetometerAccuracy ->
68-
SensorManager.getOrientation(rotation, orientation)
6968
try {
69+
SensorManager.getOrientation(rotation, orientation)
7070
val angle = Angle.Degrees(
7171
(Math.toDegrees(orientation[0].toDouble())
7272
.toFloat() + DEGREES_360) % DEGREES_360
7373
)
7474
CompassAzimuth(angle, accelerometerAccuracy, magnetometerAccuracy)
75-
} catch (_: IllegalArgumentException) {
75+
} catch (_: Exception) {
7676
null
7777
}
7878
}.filterNotNull()

data/src/main/java/io/trewartha/positional/data/compass/CompassAzimuth.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import io.trewartha.positional.data.measurement.Angle
44

55
/**
66
* Compass azimuth angle and accuracies associated with it
7+
*
8+
* @throws IllegalArgumentException if [angle] is outside of the range 0..<360
79
*/
8-
data class CompassAzimuth(
10+
data class CompassAzimuth @Throws(IllegalArgumentException::class) constructor(
911

1012
/**
1113
* Angle from north. A positive angle indicates the user is facing east of north and a negative
@@ -22,4 +24,13 @@ data class CompassAzimuth(
2224
* Accuracy of the magnetometer or `null` if unavailable
2325
*/
2426
val magnetometerAccuracy: CompassAccuracy?
25-
)
27+
) {
28+
29+
init {
30+
require(angle.inDegrees().value in VALID_RANGE) { "$angle is outside of range 0..<360" }
31+
}
32+
}
33+
34+
private const val DEGREES_0 = 0f
35+
private const val DEGREES_360 = 360f
36+
private val VALID_RANGE = DEGREES_0..<DEGREES_360

data/src/main/java/io/trewartha/positional/data/location/Conversions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.trewartha.positional.data.measurement.Distance
99
import io.trewartha.positional.data.measurement.Speed
1010
import kotlinx.datetime.Clock
1111
import kotlinx.datetime.Instant
12+
import timber.log.Timber
1213

1314
fun AndroidLocation.toLocation(): Location = Location(
1415
coordinates = Coordinates(latitude, longitude),
@@ -72,7 +73,8 @@ internal val AndroidLocation.magneticDeclination: Angle?
7273
val alt = altitude.toFloat().takeIf { hasAltitude() && it.isFinite() } ?: 0f
7374
val millis = timestamp.toEpochMilliseconds()
7475
Angle.Degrees(GeomagneticField(lat, lon, alt, millis).declination)
75-
} catch (_: IllegalArgumentException) {
76+
} catch (exception: IllegalArgumentException) {
77+
Timber.w(exception, "Unable to calculate magnetic declination")
7678
null
7779
}
7880

data/src/main/java/io/trewartha/positional/data/measurement/Angle.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@ sealed interface Angle {
1212
) : Angle {
1313

1414
init {
15-
require(value.isFinite()) { "Value must be finite" }
16-
require(value in VALID_RANGE) { "Value is outside of range 0..<360" }
15+
require(value.isFinite()) { "Value '$value' is not finite" }
1716
}
1817

1918
override fun inDegrees(): Degrees = this
2019

2120
override operator fun plus(other: Angle): Angle =
22-
Degrees((value + other.inDegrees().value) % DEGREES_360)
21+
Degrees((value + other.inDegrees().value + DEGREES_360) % DEGREES_360)
2322

2423
override fun toString(): String = "${value}°"
2524
}
2625
}
2726

28-
private const val DEGREES_0 = 0f
2927
private const val DEGREES_360 = 360f
30-
private val VALID_RANGE = DEGREES_0..<DEGREES_360

data/src/test/java/io/trewartha/positional/data/measurement/AngleTest.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@ import kotlin.test.assertFailsWith
66

77
class AngleTest {
88

9-
@Test
10-
fun testIllegalArgumentExceptionThrownWhenValueIsLessThanZero() {
11-
assertFailsWith<IllegalArgumentException> { Angle.Degrees(-0.0001f) }
12-
}
13-
14-
@Test
15-
fun testIllegalArgumentExceptionThrownWhenValueIsEqualTo360() {
16-
assertFailsWith<IllegalArgumentException> { Angle.Degrees(360f) }
17-
}
18-
19-
@Test
20-
fun testIllegalArgumentExceptionThrownWhenValueIsGreaterThan360() {
21-
assertFailsWith<IllegalArgumentException> { Angle.Degrees(360.0001f) }
22-
}
23-
249
@Test
2510
fun testIllegalArgumentExceptionThrownWhenValueIsInfinite() {
2611
assertFailsWith<IllegalArgumentException> { Angle.Degrees(Float.POSITIVE_INFINITY) }

0 commit comments

Comments
 (0)