Prevent dropping the TYPE_ACCELEROMETER and TYPE_MAGNETIC_FIELD sensor events, otherwise, only one sensor value is updated in each heading update.

Especially, there may be more TYPE_ACCELEROMETER events in some devices, such that TYPE_MAGNETIC_FIELD events could be dropped easily.
Also, it took longer time to reflect the latest heading if there are few events, due to the lowPassFilter.
This commit is contained in:
Tsoi Yat Shing
2020-04-16 14:14:33 +08:00
committed by Łukasz Paczos
parent 7367a49de0
commit 928632ad43

View File

@@ -104,11 +104,6 @@ class LocationComponentCompassEngine implements CompassEngine, SensorEventListen
@Override
public void onSensorChanged(@NonNull SensorEvent event) {
// check when the last time the compass was updated, return if too soon.
long currentTime = SystemClock.elapsedRealtime();
if (currentTime < compassUpdateNextTimestamp) {
return;
}
if (lastAccuracySensorStatus == SensorManager.SENSOR_STATUS_UNRELIABLE) {
Logger.d(TAG, "Compass sensor is unreliable, device calibration is needed.");
return;
@@ -123,9 +118,6 @@ class LocationComponentCompassEngine implements CompassEngine, SensorEventListen
magneticValues = lowPassFilter(getRotationVectorFromSensorEvent(event), magneticValues);
updateOrientation();
}
// Update the compassUpdateNextTimestamp
compassUpdateNextTimestamp = currentTime + LocationComponentConstants.COMPASS_UPDATE_RATE_MS;
}
@Override
@@ -140,6 +132,12 @@ class LocationComponentCompassEngine implements CompassEngine, SensorEventListen
@SuppressWarnings("SuspiciousNameCombination")
private void updateOrientation() {
// check when the last time the compass was updated, return if too soon.
long currentTime = SystemClock.elapsedRealtime();
if (currentTime < compassUpdateNextTimestamp) {
return;
}
if (rotationVectorValue != null) {
SensorManager.getRotationMatrixFromVector(rotationMatrix, rotationVectorValue);
} else {
@@ -182,6 +180,9 @@ class LocationComponentCompassEngine implements CompassEngine, SensorEventListen
// The x-axis is all we care about here.
notifyCompassChangeListeners((float) Math.toDegrees(orientation[0]));
// Update the compassUpdateNextTimestamp
compassUpdateNextTimestamp = currentTime + LocationComponentConstants.COMPASS_UPDATE_RATE_MS;
}
private void notifyCompassChangeListeners(float heading) {