Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2bc155de authored by Ludovic Barman's avatar Ludovic Barman
Browse files

LocationFudger: Avoid division by zero

Following internal discussions, this CL removes a rarely-occuring bug.

At latitudes -+90°, cos() is 0, leading to a division by zero.
This exception happens only if the device location (plus offset) is exactly at the North or South pole, so I assume it never happened in practice.

Change-Id: I6e91791e1b7e87ed88e48cff13a0af9b3a680908
Test: atest FrameworksMockingServicesTests:LocationFudgerTest
Bug: 388189593
Flag: EXEMPT Bug Fix
parent 086613e0
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -239,6 +239,15 @@ public class LocationFudger {

    // requires latitude since longitudinal distances change with distance from equator.
    private static double metersToDegreesLongitude(double distance, double lat) {
        return distance / APPROXIMATE_METERS_PER_DEGREE_AT_EQUATOR / Math.cos(Math.toRadians(lat));
        // Needed to convert from longitude distance to longitude degree.
        // X meters near the poles is more degrees than at the equator.
        double cosLat = Math.cos(Math.toRadians(lat));
        // If we are right on top of the pole, the degree is always 0.
        // We return a very small value instead to avoid divide by zero errors
        // later on.
        if (cosLat == 0.0) {
            return 0.0001;
        }
        return distance / APPROXIMATE_METERS_PER_DEGREE_AT_EQUATOR / cosLat;
    }
}