@@ -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