Skip to content

Commit 11df8e6

Browse files
authored
fix(android): increase tolerance to tilts and improve landscape checks (#78)
1 parent 7397a64 commit 11df8e6

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

android/src/main/java/com/orientationdirector/implementation/OrientationSensorsEventListener.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class OrientationSensorsEventListener(
2020
private var mMagneticFieldSensor: Sensor? =
2121
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
2222

23-
private var hasRotationSensor: Boolean = mRotationSensor != null
23+
private var hasRotationSensor: Boolean =
24+
mRotationSensor != null
2425
private var hasAccelerometerAndMagneticFieldSensors: Boolean =
2526
mAccelerometerSensor != null && mMagneticFieldSensor != null
2627

android/src/main/java/com/orientationdirector/implementation/Utils.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Utils(private val context: ReactContext) {
2626
val rollDegrees = Math.toDegrees(rollRadians.toDouble()).toFloat()
2727

2828
// This is needed to account for inaccuracy due to subtle movements such as tilting
29-
val tolerance = 20f
29+
val pitchTolerance = 15f
30+
val rollTolerance = 20f
3031

3132
//////////////////////////////////////
3233
// These limits are set based on SensorManager.getOrientation reference
@@ -38,12 +39,14 @@ class Utils(private val context: ReactContext) {
3839
//
3940
//////////////////////////////////////
4041

42+
val isPitchInLandscapeModeRange = checkIfPitchIsInLandscapeModeRange(pitchDegrees, pitchTolerance)
43+
4144
return when {
4245
rollDegrees.equals(-0f) && (pitchDegrees.equals(0f) || pitchDegrees.equals(-0f)) -> Orientation.FACE_UP
4346
rollDegrees.equals(-180f) && (pitchDegrees.equals(0f) || pitchDegrees.equals(-0f)) -> Orientation.FACE_DOWN
44-
rollDegrees in tolerance..landscapeRightLimit - tolerance -> Orientation.LANDSCAPE_RIGHT
45-
rollDegrees in landscapeLeftLimit + tolerance..-tolerance -> Orientation.LANDSCAPE_LEFT
46-
pitchDegrees in portraitLimit..-0f -> Orientation.PORTRAIT
47+
rollDegrees in rollTolerance..landscapeRightLimit - rollTolerance && isPitchInLandscapeModeRange -> Orientation.LANDSCAPE_RIGHT
48+
rollDegrees in landscapeLeftLimit + rollTolerance..-rollTolerance && isPitchInLandscapeModeRange -> Orientation.LANDSCAPE_LEFT
49+
pitchDegrees in portraitLimit..pitchTolerance -> Orientation.PORTRAIT
4750
else -> Orientation.PORTRAIT_UPSIDE_DOWN
4851
}
4952
}
@@ -94,4 +97,8 @@ class Utils(private val context: ReactContext) {
9497

9598
return context.currentActivity!!.requestedOrientation;
9699
}
100+
101+
private fun checkIfPitchIsInLandscapeModeRange(pitchDegrees: Float, tolerance: Float): Boolean {
102+
return pitchDegrees > -tolerance && pitchDegrees < tolerance
103+
}
97104
}

0 commit comments

Comments
 (0)