Skip to content

Commit 494565a

Browse files
Niclas KellgrenJohan Redestig
authored andcommitted
Added check to make orientation calculations more robust
Added check avoid a division by zero resulting in NaN which in turn makes checkFullyTilted to ignore high tilt angles from then on. If (x, y, z) == (0, 0, 0) then there is no tilt or rotation and this vector must be ignored. This check is extended to ignore all small acceleration values where noise can be of big influence. Low or zero readings can happen when space travelling free falling, but more commonly when shaking or getting bad readings from the sensor. The accelerometer is turned off when not used and polling it too soon after it is turned on may result in (0, 0, 0). Change-Id: I19aec653abb8ab6f7126778035c8c96449f1326f
1 parent cb84275 commit 494565a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

core/java/android/view/WindowOrientationListener.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ static class SensorEventListenerImpl implements SensorEventListener {
234234
// high time constant.
235235
private static final float MAX_DEVIATION_FROM_GRAVITY = 1.5f;
236236

237+
// Minimum acceleration considered, in m/s^2. Below this threshold sensor noise will have
238+
// significant impact on the calculations and in case of the vector (0, 0, 0) there is no
239+
// defined rotation or tilt at all. Low or zero readings can happen when space travelling
240+
// or free falling, but more commonly when shaking or getting bad readings from the sensor.
241+
// The accelerometer is turned off when not used and polling it too soon after it is
242+
// turned on may result in (0, 0, 0).
243+
private static final float MIN_ABS_ACCELERATION = 1.5f;
244+
237245
// Actual sampling period corresponding to SensorManager.SENSOR_DELAY_NORMAL. There's no
238246
// way to get this information from SensorManager.
239247
// Note the actual period is generally 3-30ms larger than this depending on the device, but
@@ -347,6 +355,9 @@ public void onSensorChanged(SensorEvent event) {
347355
float deviation = Math.abs(magnitude - SensorManager.STANDARD_GRAVITY);
348356

349357
handleAccelerationDistrust(deviation);
358+
if (magnitude < MIN_ABS_ACCELERATION) {
359+
return; // Ignore tilt and orientation when (0, 0, 0) or low reading
360+
}
350361

351362
// only filter tilt when we're accelerating
352363
float alpha = 1;

0 commit comments

Comments
 (0)