Skip to content

Commit 0950a3e

Browse files
Dianne HackbornAndroid Code Review
authored andcommitted
Merge "Added check to make orientation calculations more robust"
2 parents a220a29 + 494565a commit 0950a3e

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)