Skip to content

Commit 6f57dcb

Browse files
committed
fix: add pitch tolerance to improve portrait and upside down detection
1 parent 6720f7b commit 6f57dcb

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

  • android/src/main/java/com/orientationdirector/implementation

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ 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 pitchTolerance = 5f
2930
val tolerance = 20f
3031

3132
//////////////////////////////////////
@@ -38,14 +39,14 @@ class Utils(private val context: ReactContext) {
3839
//
3940
//////////////////////////////////////
4041

41-
val isPitchInLandscapeModeRange = checkIfPitchIsInLandscapeModeRange(pitchDegrees)
42+
val isPitchInLandscapeModeRange = checkIfPitchIsInLandscapeModeRange(pitchDegrees, pitchTolerance)
4243

4344
return when {
4445
rollDegrees.equals(-0f) && (pitchDegrees.equals(0f) || pitchDegrees.equals(-0f)) -> Orientation.FACE_UP
4546
rollDegrees.equals(-180f) && (pitchDegrees.equals(0f) || pitchDegrees.equals(-0f)) -> Orientation.FACE_DOWN
4647
rollDegrees in tolerance..landscapeRightLimit - tolerance && isPitchInLandscapeModeRange -> Orientation.LANDSCAPE_RIGHT
4748
rollDegrees in landscapeLeftLimit + tolerance..-tolerance && isPitchInLandscapeModeRange -> Orientation.LANDSCAPE_LEFT
48-
pitchDegrees in portraitLimit..-0f -> Orientation.PORTRAIT
49+
pitchDegrees in portraitLimit..pitchTolerance -> Orientation.PORTRAIT
4950
else -> Orientation.PORTRAIT_UPSIDE_DOWN
5051
}
5152
}
@@ -97,8 +98,7 @@ class Utils(private val context: ReactContext) {
9798
return context.currentActivity!!.requestedOrientation;
9899
}
99100

100-
private fun checkIfPitchIsInLandscapeModeRange(pitchDegrees: Float): Boolean {
101-
val tolerance = 5f
101+
private fun checkIfPitchIsInLandscapeModeRange(pitchDegrees: Float, tolerance: Float): Boolean {
102102
return pitchDegrees > -tolerance && pitchDegrees < tolerance
103103
}
104104
}

0 commit comments

Comments
 (0)