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

Commit

Permalink
Adds SDK version check to getElapsedRealtimeNanos (API 17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgreb committed Mar 10, 2015
1 parent 0d109df commit b9440a7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -293,17 +292,26 @@ 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);
locationB.setElapsedRealtimeNanos(timeInNanos - RECENT_UPDATE_THRESHOLD_IN_NANOS - 1);
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");
Expand Down
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);
}
}

0 comments on commit b9440a7

Please sign in to comment.