This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds SDK version check to getElapsedRealtimeNanos (API 17)
- Loading branch information
Showing
4 changed files
with
59 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lost/src/main/java/com/mapzen/android/lost/internal/SystemClock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
lost/src/test/java/com/mapzen/android/lost/internal/SystemClockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |