SensorData: use Instant instead of long.

This commit is contained in:
Dennis Guse
2021-02-07 23:16:12 +01:00
parent 854c8727e5
commit cd4e0abd6f
2 changed files with 10 additions and 10 deletions
@@ -4,6 +4,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.time.Instant;
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
public class SensorData {
@@ -11,18 +13,18 @@ public class SensorData {
private final String sensorAddress;
private final String sensorName;
private final long timestamp_ms;
private final Instant timestamp_ms;
SensorData(String sensorAddress) {
this(sensorAddress, null, System.currentTimeMillis());
this(sensorAddress, null);
}
SensorData(String sensorAddress, String sensorName) {
this(sensorAddress, sensorName, System.currentTimeMillis());
this(sensorAddress, sensorName, Instant.now());
}
@VisibleForTesting
SensorData(String sensorAddress, String sensorName, long timestamp_ms) {
SensorData(String sensorAddress, String sensorName, Instant timestamp_ms) {
this.sensorAddress = sensorAddress;
this.sensorName = sensorName;
this.timestamp_ms = timestamp_ms;
@@ -42,10 +44,7 @@ public class SensorData {
return sensorName != null ? sensorName : sensorAddress;
}
/**
* Is the data recent considering the current time.
*/
public boolean isRecent() {
return timestamp_ms + BluetoothRemoteSensorManager.MAX_SENSOR_DATE_SET_AGE_MS > System.currentTimeMillis();
return Instant.now().isBefore(timestamp_ms.plus(BluetoothRemoteSensorManager.MAX_SENSOR_DATE_SET_AGE_MS));
}
}
@@ -22,13 +22,14 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import java.time.Duration;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.content.sensor.SensorData;
import de.dennisguse.opentracks.content.sensor.SensorDataCycling;
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
import de.dennisguse.opentracks.util.BluetoothUtils;
import de.dennisguse.opentracks.util.PreferencesUtils;
import de.dennisguse.opentracks.util.UnitConversions;
/**
* Bluetooth LE sensor manager: manages connections to Bluetooth LE sensors.
@@ -48,7 +49,7 @@ public class BluetoothRemoteSensorManager implements BluetoothConnectionManager.
private static final String TAG = BluetoothRemoteSensorManager.class.getSimpleName();
public static final long MAX_SENSOR_DATE_SET_AGE_MS = 5 * UnitConversions.S_TO_MS;
public static final Duration MAX_SENSOR_DATE_SET_AGE_MS = Duration.ofSeconds(5);
private final BluetoothAdapter bluetoothAdapter;
private final Context context;