Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] normalize previous rotation values of the location animator
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed May 10, 2019
1 parent b1c1471 commit 9fb41a7
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ private void updateLayerAnimators(LatLng previousLatLng, LatLng targetLatLng,
float previousBearing, float targetBearing) {
createNewLatLngAnimator(ANIMATOR_LAYER_LATLNG, previousLatLng, targetLatLng);

// Because Location bearing values are normalized to [0, 360]
// we need to do the same for the previous bearing value to determine the shortest path
previousBearing = Utils.normalize(previousBearing);
float normalizedLayerBearing = Utils.shortestRotation(targetBearing, previousBearing);
createNewFloatAnimator(ANIMATOR_LAYER_GPS_BEARING, previousBearing, normalizedLayerBearing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public static float shortestRotation(float heading, float previousHeading) {
return heading;
}

/**
* Normalizes an angle to be in the [0, 360] range.
*
* @param angle the provided angle
* @return the normalized angle
*/
public static float normalize(float angle) {
return (angle % 360 + 360) % 360;
}

static Bitmap generateShadow(Drawable drawable, float elevation) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,108 @@ class LocationAnimatorCoordinatorTest {
assertEquals(location.bearing, layerBearingTarget)
}

@Test
fun feedNewLocation_animatorValue_correctRotation_1() {
val location = Location("")
location.latitude = 51.0
location.longitude = 17.0
location.bearing = 0f

val animator = mockk<MapboxFloatAnimator>(relaxed = true)
every { animator.animatedValue } returns 270f
locationAnimatorCoordinator.animatorArray.put(ANIMATOR_LAYER_GPS_BEARING, animator)

locationAnimatorCoordinator.feedNewLocation(location, cameraPosition, false)

val layerBearingTarget = locationAnimatorCoordinator.animatorArray[ANIMATOR_LAYER_GPS_BEARING]?.target as Float
assertEquals(360f, layerBearingTarget)
}

@Test
fun feedNewLocation_animatorValue_correctRotation_2() {
val location = Location("")
location.latitude = 51.0
location.longitude = 17.0
location.bearing = 90f

val animator = mockk<MapboxFloatAnimator>(relaxed = true)
every { animator.animatedValue } returns 280f
locationAnimatorCoordinator.animatorArray.put(ANIMATOR_LAYER_GPS_BEARING, animator)

locationAnimatorCoordinator.feedNewLocation(location, cameraPosition, false)

val layerBearingTarget = locationAnimatorCoordinator.animatorArray[ANIMATOR_LAYER_GPS_BEARING]?.target as Float
assertEquals(450f, layerBearingTarget)
}

@Test
fun feedNewLocation_animatorValue_correctRotation_3() {
val location = Location("")
location.latitude = 51.0
location.longitude = 17.0
location.bearing = 300f

val animator = mockk<MapboxFloatAnimator>(relaxed = true)
every { animator.animatedValue } returns 450f
locationAnimatorCoordinator.animatorArray.put(ANIMATOR_LAYER_GPS_BEARING, animator)

locationAnimatorCoordinator.feedNewLocation(location, cameraPosition, false)

val layerBearingTarget = locationAnimatorCoordinator.animatorArray[ANIMATOR_LAYER_GPS_BEARING]?.target as Float
assertEquals(-60f, layerBearingTarget)
}

@Test
fun feedNewLocation_animatorValue_correctRotation_4() {
val location = Location("")
location.latitude = 51.0
location.longitude = 17.0
location.bearing = 350f

val animator = mockk<MapboxFloatAnimator>(relaxed = true)
every { animator.animatedValue } returns 10f
locationAnimatorCoordinator.animatorArray.put(ANIMATOR_LAYER_GPS_BEARING, animator)

locationAnimatorCoordinator.feedNewLocation(location, cameraPosition, false)

val layerBearingTarget = locationAnimatorCoordinator.animatorArray[ANIMATOR_LAYER_GPS_BEARING]?.target as Float
assertEquals(-10f, layerBearingTarget)
}

@Test
fun feedNewLocation_animatorValue_correctRotation_5() {
val location = Location("")
location.latitude = 51.0
location.longitude = 17.0
location.bearing = 90f

val animator = mockk<MapboxFloatAnimator>(relaxed = true)
every { animator.animatedValue } returns -280f
locationAnimatorCoordinator.animatorArray.put(ANIMATOR_LAYER_GPS_BEARING, animator)

locationAnimatorCoordinator.feedNewLocation(location, cameraPosition, false)

val layerBearingTarget = locationAnimatorCoordinator.animatorArray[ANIMATOR_LAYER_GPS_BEARING]?.target as Float
assertEquals(90f, layerBearingTarget)
}

@Test
fun feedNewLocation_animatorValue_correctRotation_6() {
val location = Location("")
location.latitude = 51.0
location.longitude = 17.0
location.bearing = 270f

val animator = mockk<MapboxFloatAnimator>(relaxed = true)
every { animator.animatedValue } returns -350f
locationAnimatorCoordinator.animatorArray.put(ANIMATOR_LAYER_GPS_BEARING, animator)

locationAnimatorCoordinator.feedNewLocation(location, cameraPosition, false)

val layerBearingTarget = locationAnimatorCoordinator.animatorArray[ANIMATOR_LAYER_GPS_BEARING]?.target as Float
assertEquals(-90f, layerBearingTarget)
}

@Test
fun feedNewLocation_isNorth_animatorsAreCreated() {
val location = Location("")
Expand Down

0 comments on commit 9fb41a7

Please sign in to comment.