Skip to content

Commit 9bc1d9c

Browse files
authored
fix(android): face up or down detection and improve portrait (#84)
1 parent 11df8e6 commit 9bc1d9c

File tree

1 file changed

+28
-8
lines changed
  • android/src/main/java/com/orientationdirector/implementation

1 file changed

+28
-8
lines changed

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ 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 = 15f
30-
val rollTolerance = 20f
29+
val pitchToleranceDefault = 5f
30+
val rollTolerance = 0f
31+
val toleranceForFaceUpOrDown = 5f;
3132

3233
//////////////////////////////////////
3334
// These limits are set based on SensorManager.getOrientation reference
@@ -39,14 +40,17 @@ class Utils(private val context: ReactContext) {
3940
//
4041
//////////////////////////////////////
4142

42-
val isPitchInLandscapeModeRange = checkIfPitchIsInLandscapeModeRange(pitchDegrees, pitchTolerance)
43+
val isPitchInLandscapeModeRange =
44+
checkIfValueIsBetweenTolerance(pitchDegrees, pitchToleranceDefault)
45+
val isPitchCloseToFaceUpOrDown =
46+
checkIfValueIsBetweenTolerance(pitchDegrees, toleranceForFaceUpOrDown)
4347

4448
return when {
45-
rollDegrees.equals(-0f) && (pitchDegrees.equals(0f) || pitchDegrees.equals(-0f)) -> Orientation.FACE_UP
46-
rollDegrees.equals(-180f) && (pitchDegrees.equals(0f) || pitchDegrees.equals(-0f)) -> Orientation.FACE_DOWN
49+
checkIfRollIsCloseToFaceUp(rollDegrees) && isPitchCloseToFaceUpOrDown -> Orientation.FACE_UP
50+
checkIfRollIsCloseToFaceDown(rollDegrees) && isPitchCloseToFaceUpOrDown -> Orientation.FACE_DOWN
4751
rollDegrees in rollTolerance..landscapeRightLimit - rollTolerance && isPitchInLandscapeModeRange -> Orientation.LANDSCAPE_RIGHT
4852
rollDegrees in landscapeLeftLimit + rollTolerance..-rollTolerance && isPitchInLandscapeModeRange -> Orientation.LANDSCAPE_LEFT
49-
pitchDegrees in portraitLimit..pitchTolerance -> Orientation.PORTRAIT
53+
pitchDegrees in portraitLimit..pitchToleranceDefault -> Orientation.PORTRAIT
5054
else -> Orientation.PORTRAIT_UPSIDE_DOWN
5155
}
5256
}
@@ -98,7 +102,23 @@ class Utils(private val context: ReactContext) {
98102
return context.currentActivity!!.requestedOrientation;
99103
}
100104

101-
private fun checkIfPitchIsInLandscapeModeRange(pitchDegrees: Float, tolerance: Float): Boolean {
102-
return pitchDegrees > -tolerance && pitchDegrees < tolerance
105+
private fun checkIfValueIsBetweenTolerance(value: Float, tolerance: Float): Boolean {
106+
return value > -tolerance && value < tolerance
107+
}
108+
109+
private fun checkIfRollIsCloseToFaceDown(value: Float): Boolean {
110+
val landscapeLimit = 180f
111+
val faceDownLimit = 170f
112+
113+
return value in faceDownLimit..landscapeLimit ||
114+
value in -landscapeLimit..-faceDownLimit;
115+
}
116+
117+
private fun checkIfRollIsCloseToFaceUp(value: Float): Boolean {
118+
val landscapeLimit = 0f
119+
val faceUpLimit = 10f
120+
121+
return value in landscapeLimit..faceUpLimit ||
122+
value in -faceUpLimit..-landscapeLimit
103123
}
104124
}

0 commit comments

Comments
 (0)