Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class OrientationSensorsEventListener(
private var mMagneticFieldSensor: Sensor? =
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)

private var hasRotationSensor: Boolean = mRotationSensor != null
private var hasRotationSensor: Boolean =
mRotationSensor != null
private var hasAccelerometerAndMagneticFieldSensors: Boolean =
mAccelerometerSensor != null && mMagneticFieldSensor != null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Utils(private val context: ReactContext) {
val rollDegrees = Math.toDegrees(rollRadians.toDouble()).toFloat()

// This is needed to account for inaccuracy due to subtle movements such as tilting
val tolerance = 20f
val pitchTolerance = 15f
val rollTolerance = 20f

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

val isPitchInLandscapeModeRange = checkIfPitchIsInLandscapeModeRange(pitchDegrees, pitchTolerance)

return when {
rollDegrees.equals(-0f) && (pitchDegrees.equals(0f) || pitchDegrees.equals(-0f)) -> Orientation.FACE_UP
rollDegrees.equals(-180f) && (pitchDegrees.equals(0f) || pitchDegrees.equals(-0f)) -> Orientation.FACE_DOWN
rollDegrees in tolerance..landscapeRightLimit - tolerance -> Orientation.LANDSCAPE_RIGHT
rollDegrees in landscapeLeftLimit + tolerance..-tolerance -> Orientation.LANDSCAPE_LEFT
pitchDegrees in portraitLimit..-0f -> Orientation.PORTRAIT
rollDegrees in rollTolerance..landscapeRightLimit - rollTolerance && isPitchInLandscapeModeRange -> Orientation.LANDSCAPE_RIGHT
rollDegrees in landscapeLeftLimit + rollTolerance..-rollTolerance && isPitchInLandscapeModeRange -> Orientation.LANDSCAPE_LEFT
pitchDegrees in portraitLimit..pitchTolerance -> Orientation.PORTRAIT
else -> Orientation.PORTRAIT_UPSIDE_DOWN
}
}
Expand Down Expand Up @@ -94,4 +97,8 @@ class Utils(private val context: ReactContext) {

return context.currentActivity!!.requestedOrientation;
}

private fun checkIfPitchIsInLandscapeModeRange(pitchDegrees: Float, tolerance: Float): Boolean {
return pitchDegrees > -tolerance && pitchDegrees < tolerance
}
}