From b9440a7c917ae080e3a37decee6920c9b62a908b Mon Sep 17 00:00:00 2001 From: Chuck Greb Date: Tue, 10 Mar 2015 15:45:14 -0400 Subject: [PATCH] Adds SDK version check to getElapsedRealtimeNanos (API 17) --- .../android/lost/internal/FusionEngine.java | 2 +- .../android/lost/internal/SystemClock.java | 13 ++++++++ .../lost/internal/FusionEngineTest.java | 22 +++++++++----- .../lost/internal/SystemClockTest.java | 30 +++++++++++++++++++ 4 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 lost/src/test/java/com/mapzen/android/lost/internal/SystemClockTest.java diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/FusionEngine.java b/lost/src/main/java/com/mapzen/android/lost/internal/FusionEngine.java index 0663b4a..556ce4a 100644 --- a/lost/src/main/java/com/mapzen/android/lost/internal/FusionEngine.java +++ b/lost/src/main/java/com/mapzen/android/lost/internal/FusionEngine.java @@ -160,7 +160,7 @@ public static boolean isBetterThan(Location locationA, Location locationB) { return true; } - if (locationA.getElapsedRealtimeNanos() > locationB.getElapsedRealtimeNanos() + + if (SystemClock.getTimeInNanos(locationA) > SystemClock.getTimeInNanos(locationB) + RECENT_UPDATE_THRESHOLD_IN_NANOS) { return true; } diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/SystemClock.java b/lost/src/main/java/com/mapzen/android/lost/internal/SystemClock.java index 790d565..6296a98 100644 --- a/lost/src/main/java/com/mapzen/android/lost/internal/SystemClock.java +++ b/lost/src/main/java/com/mapzen/android/lost/internal/SystemClock.java @@ -1,8 +1,21 @@ package com.mapzen.android.lost.internal; +import android.location.Location; +import android.os.Build; + public class SystemClock implements Clock { + public static final long MS_TO_NS = 1000000; + @Override public long getCurrentTimeInMillis() { return System.currentTimeMillis(); } + + public static long getTimeInNanos(Location location) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + return location.getElapsedRealtimeNanos(); + } + + return location.getTime() * MS_TO_NS; + } } diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/FusionEngineTest.java b/lost/src/test/java/com/mapzen/android/lost/internal/FusionEngineTest.java index 7fccd20..d4aed89 100644 --- a/lost/src/test/java/com/mapzen/android/lost/internal/FusionEngineTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/internal/FusionEngineTest.java @@ -9,23 +9,22 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowLocationManager; -import android.annotation.TargetApi; import android.content.Context; import android.location.Location; import android.location.LocationManager; -import android.os.Build; import java.util.Collection; import static android.location.LocationManager.GPS_PROVIDER; import static android.location.LocationManager.NETWORK_PROVIDER; import static android.location.LocationManager.PASSIVE_PROVIDER; -import static com.mapzen.android.lost.internal.FusionEngine.RECENT_UPDATE_THRESHOLD_IN_MILLIS; import static com.mapzen.android.lost.api.LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY; import static com.mapzen.android.lost.api.LocationRequest.PRIORITY_HIGH_ACCURACY; import static com.mapzen.android.lost.api.LocationRequest.PRIORITY_LOW_POWER; import static com.mapzen.android.lost.api.LocationRequest.PRIORITY_NO_POWER; +import static com.mapzen.android.lost.internal.FusionEngine.RECENT_UPDATE_THRESHOLD_IN_MILLIS; import static com.mapzen.android.lost.internal.FusionEngine.RECENT_UPDATE_THRESHOLD_IN_NANOS; +import static com.mapzen.android.lost.internal.SystemClock.MS_TO_NS; import static org.fest.assertions.api.Assertions.assertThat; import static org.robolectric.Robolectric.application; import static org.robolectric.Robolectric.shadowOf; @@ -293,10 +292,9 @@ public void isBetterThan_shouldReturnTrueIfLocationBIsNull() throws Exception { assertThat(FusionEngine.isBetterThan(locationA, locationB)).isTrue(); } - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) - @Test - public void isBetterThan_shouldReturnTrueIfLocationBIsStale() throws Exception { - final long timeInNanos = System.currentTimeMillis() * 1000000; + @Test @Config(emulateSdk = 17, reportSdk = 17) + public void isBetterThan_shouldReturnTrueIfLocationBIsStale_Api17() throws Exception { + final long timeInNanos = System.currentTimeMillis() * MS_TO_NS; Location locationA = new Location("test"); Location locationB = new Location("test"); locationA.setElapsedRealtimeNanos(timeInNanos); @@ -304,6 +302,16 @@ public void isBetterThan_shouldReturnTrueIfLocationBIsStale() throws Exception { assertThat(FusionEngine.isBetterThan(locationA, locationB)).isTrue(); } + @Test @Config(emulateSdk = 16, reportSdk = 16) + public void isBetterThan_shouldReturnTrueIfLocationBIsStale_Api16() throws Exception { + final long timeInMillis = System.currentTimeMillis(); + Location locationA = new Location("test"); + Location locationB = new Location("test"); + locationA.setTime(timeInMillis); + locationB.setTime(timeInMillis - RECENT_UPDATE_THRESHOLD_IN_MILLIS - 1); + assertThat(FusionEngine.isBetterThan(locationA, locationB)).isTrue(); + } + @Test public void isBetterThan_shouldReturnFalseIfLocationAHasNoAccuracy() throws Exception { Location locationA = new Location("test"); diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/SystemClockTest.java b/lost/src/test/java/com/mapzen/android/lost/internal/SystemClockTest.java new file mode 100644 index 0000000..755299a --- /dev/null +++ b/lost/src/test/java/com/mapzen/android/lost/internal/SystemClockTest.java @@ -0,0 +1,30 @@ +package com.mapzen.android.lost.internal; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import android.location.Location; + +import static com.mapzen.android.lost.internal.SystemClock.MS_TO_NS; +import static org.fest.assertions.api.Assertions.assertThat; + +@RunWith(RobolectricTestRunner.class) +public class SystemClockTest { + @Test @Config(emulateSdk = 17, reportSdk = 17) + public void getTimeInNanos_shouldReturnElapsedRealtimeNanosForSdk17AndUp() throws Exception { + final long nanos = 1000000; + final Location location = new Location("mock"); + location.setElapsedRealtimeNanos(nanos); + assertThat(SystemClock.getTimeInNanos(location)).isEqualTo(nanos); + } + + @Test @Config(emulateSdk = 16, reportSdk = 16) + public void getTimeInNanos_shouldUseUtcTimeInMillisForSdk16AndLower() throws Exception { + final long millis = 1000; + final Location location = new Location("mock"); + location.setTime(millis); + assertThat(SystemClock.getTimeInNanos(location)).isEqualTo(millis * MS_TO_NS); + } +}