@@ -20,38 +20,47 @@ class Utils(private val context: ReactContext) {
2020 }
2121
2222 fun convertToDeviceOrientationFrom (orientationAngles : FloatArray ): Orientation {
23- val (_, pitchRadians, rollRadians) = orientationAngles;
24-
25- val pitchDegrees = Math .toDegrees(pitchRadians.toDouble()).toFloat()
26- val rollDegrees = Math .toDegrees(rollRadians.toDouble()).toFloat()
27-
28- // This is needed to account for inaccuracy due to subtle movements such as tilting
29- val pitchToleranceDefault = 5f
30- val rollTolerance = 0f
31- val toleranceForFaceUpOrDown = 5f ;
32-
33- // ////////////////////////////////////
34- // These limits are set based on SensorManager.getOrientation reference
35- // https://developer.android.com/develop/sensors-and-location/sensors/sensors_position#sensors-pos-orient
36- //
37- val portraitLimit = - 90f
38- val landscapeRightLimit = 180f
39- val landscapeLeftLimit = - 180f
40- //
41- // ////////////////////////////////////
42-
43- val isPitchInLandscapeModeRange =
44- checkIfValueIsBetweenTolerance(pitchDegrees, pitchToleranceDefault)
45- val isPitchCloseToFaceUpOrDown =
46- checkIfValueIsBetweenTolerance(pitchDegrees, toleranceForFaceUpOrDown)
23+ if (orientationAngles.size < 3 ) {
24+ return Orientation .PORTRAIT
25+ }
26+
27+ val (_, pitchRadians, rollRadians) = orientationAngles
28+
29+ val pitch = Math .toDegrees(pitchRadians.toDouble()).toFloat()
30+ val roll = Math .toDegrees(rollRadians.toDouble()).toFloat()
31+
32+ val faceUpDownPitchTolerance = 30f
33+
34+ fun isValueCloseTo (value : Float , target : Float , tolerance : Float ): Boolean {
35+ return value in (target - tolerance).. (target + tolerance)
36+ }
4737
4838 return when {
49- checkIfRollIsCloseToFaceUp(rollDegrees) && isPitchCloseToFaceUpOrDown -> Orientation .FACE_UP
50- checkIfRollIsCloseToFaceDown(rollDegrees) && isPitchCloseToFaceUpOrDown -> Orientation .FACE_DOWN
51- rollDegrees in rollTolerance.. landscapeRightLimit - rollTolerance && isPitchInLandscapeModeRange -> Orientation .LANDSCAPE_RIGHT
52- rollDegrees in landscapeLeftLimit + rollTolerance.. - rollTolerance && isPitchInLandscapeModeRange -> Orientation .LANDSCAPE_LEFT
53- pitchDegrees in portraitLimit.. pitchToleranceDefault -> Orientation .PORTRAIT
54- else -> Orientation .PORTRAIT_UPSIDE_DOWN
39+ // Face up: device is lying flat with screen up
40+ isValueCloseTo(pitch, 0f , faceUpDownPitchTolerance) &&
41+ isValueCloseTo(roll, 0f , faceUpDownPitchTolerance) -> Orientation .FACE_UP
42+
43+ // Face down: device is lying flat with screen down
44+ isValueCloseTo(pitch, 0f , faceUpDownPitchTolerance) &&
45+ (isValueCloseTo(roll, 180f , faceUpDownPitchTolerance) || isValueCloseTo(
46+ roll,
47+ - 180f ,
48+ faceUpDownPitchTolerance
49+ )) -> Orientation .FACE_DOWN
50+
51+ // Portrait
52+ isValueCloseTo(pitch, - 90f , 45f ) -> Orientation .PORTRAIT
53+
54+ // Portrait upside down
55+ isValueCloseTo(pitch, 90f , 45f ) -> Orientation .PORTRAIT_UPSIDE_DOWN
56+
57+ // Landscape left
58+ isValueCloseTo(roll, - 90f , 45f ) -> Orientation .LANDSCAPE_LEFT
59+
60+ // Landscape right
61+ isValueCloseTo(roll, 90f , 45f ) -> Orientation .LANDSCAPE_RIGHT
62+
63+ else -> Orientation .PORTRAIT
5564 }
5665 }
5766
@@ -93,32 +102,4 @@ class Utils(private val context: ReactContext) {
93102 else -> Orientation .UNKNOWN
94103 }
95104 }
96-
97- fun getRequestedOrientation (): Int {
98- if (context.currentActivity?.requestedOrientation == null ) {
99- return ActivityInfo .SCREEN_ORIENTATION_UNSPECIFIED ;
100- }
101-
102- return context.currentActivity!! .requestedOrientation;
103- }
104-
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
123- }
124105}
0 commit comments