react-native-orientation-director 2.5.1 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/README.md +22 -12
  2. package/android/src/main/java/com/orientationdirector/implementation/OrientationDirectorModuleImpl.kt +24 -35
  3. package/android/src/main/java/com/orientationdirector/implementation/OrientationSensorsEventListener.kt +69 -19
  4. package/android/src/main/java/com/orientationdirector/implementation/Utils.kt +36 -28
  5. package/lib/commonjs/EventEmitter.js +48 -0
  6. package/lib/commonjs/NativeOrientationDirector.js +8 -0
  7. package/lib/commonjs/RNOrientationDirector.js +126 -0
  8. package/lib/commonjs/hooks/useDeviceOrientation.hook.js +37 -0
  9. package/lib/commonjs/hooks/useInterfaceOrientation.hook.js +37 -0
  10. package/lib/commonjs/hooks/useIsInterfaceOrientationLocked.hook.js +27 -0
  11. package/lib/commonjs/index.js +51 -0
  12. package/lib/commonjs/module.js +22 -0
  13. package/lib/commonjs/package.json +1 -0
  14. package/lib/commonjs/types/AutoRotation.enum.js +12 -0
  15. package/lib/commonjs/types/Event.enum.js +13 -0
  16. package/lib/commonjs/types/HumanReadableAutoRotationsResource.type.js +5 -0
  17. package/lib/commonjs/types/HumanReadableOrientationsResource.type.js +5 -0
  18. package/lib/commonjs/types/LockableOrientation.type.js +5 -0
  19. package/lib/commonjs/types/LockedEvent.interface.js +1 -0
  20. package/lib/commonjs/types/Orientation.enum.js +17 -0
  21. package/lib/commonjs/types/OrientationEvent.interface.js +5 -0
  22. package/lib/commonjs/types/OrientationType.enum.js +11 -0
  23. package/lib/typescript/commonjs/example/src/App.d.ts +2 -0
  24. package/lib/typescript/commonjs/example/src/App.d.ts.map +1 -0
  25. package/lib/typescript/commonjs/example/src/AppNavigationContainer.d.ts +3 -0
  26. package/lib/typescript/commonjs/example/src/AppNavigationContainer.d.ts.map +1 -0
  27. package/lib/typescript/commonjs/example/src/screens/Explore.d.ts +3 -0
  28. package/lib/typescript/commonjs/example/src/screens/Explore.d.ts.map +1 -0
  29. package/lib/typescript/commonjs/example/src/screens/Home.d.ts +3 -0
  30. package/lib/typescript/commonjs/example/src/screens/Home.d.ts.map +1 -0
  31. package/lib/typescript/commonjs/example/src/screens/InnerExplore.d.ts +3 -0
  32. package/lib/typescript/commonjs/example/src/screens/InnerExplore.d.ts.map +1 -0
  33. package/lib/typescript/commonjs/example/src/screens/styles.d.ts +51 -0
  34. package/lib/typescript/commonjs/example/src/screens/styles.d.ts.map +1 -0
  35. package/lib/typescript/commonjs/package.json +1 -0
  36. package/lib/typescript/commonjs/src/EventEmitter.d.ts.map +1 -0
  37. package/lib/typescript/commonjs/src/NativeOrientationDirector.d.ts.map +1 -0
  38. package/lib/typescript/commonjs/src/RNOrientationDirector.d.ts.map +1 -0
  39. package/lib/typescript/commonjs/src/hooks/useDeviceOrientation.hook.d.ts.map +1 -0
  40. package/lib/typescript/commonjs/src/hooks/useInterfaceOrientation.hook.d.ts.map +1 -0
  41. package/lib/typescript/commonjs/src/hooks/useIsInterfaceOrientationLocked.hook.d.ts.map +1 -0
  42. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  43. package/lib/typescript/commonjs/src/module.d.ts.map +1 -0
  44. package/lib/typescript/commonjs/src/types/AutoRotation.enum.d.ts.map +1 -0
  45. package/lib/typescript/commonjs/src/types/Event.enum.d.ts.map +1 -0
  46. package/lib/typescript/commonjs/src/types/HumanReadableAutoRotationsResource.type.d.ts.map +1 -0
  47. package/lib/typescript/commonjs/src/types/HumanReadableOrientationsResource.type.d.ts.map +1 -0
  48. package/lib/typescript/commonjs/src/types/LockableOrientation.type.d.ts.map +1 -0
  49. package/lib/typescript/commonjs/src/types/LockedEvent.interface.d.ts.map +1 -0
  50. package/lib/typescript/commonjs/src/types/Orientation.enum.d.ts.map +1 -0
  51. package/lib/typescript/commonjs/src/types/OrientationEvent.interface.d.ts.map +1 -0
  52. package/lib/typescript/commonjs/src/types/OrientationType.enum.d.ts.map +1 -0
  53. package/lib/typescript/module/example/src/App.d.ts +2 -0
  54. package/lib/typescript/module/example/src/App.d.ts.map +1 -0
  55. package/lib/typescript/module/example/src/AppNavigationContainer.d.ts +3 -0
  56. package/lib/typescript/module/example/src/AppNavigationContainer.d.ts.map +1 -0
  57. package/lib/typescript/module/example/src/screens/Explore.d.ts +3 -0
  58. package/lib/typescript/module/example/src/screens/Explore.d.ts.map +1 -0
  59. package/lib/typescript/module/example/src/screens/Home.d.ts +3 -0
  60. package/lib/typescript/module/example/src/screens/Home.d.ts.map +1 -0
  61. package/lib/typescript/module/example/src/screens/InnerExplore.d.ts +3 -0
  62. package/lib/typescript/module/example/src/screens/InnerExplore.d.ts.map +1 -0
  63. package/lib/typescript/module/example/src/screens/styles.d.ts +51 -0
  64. package/lib/typescript/module/example/src/screens/styles.d.ts.map +1 -0
  65. package/lib/typescript/module/src/EventEmitter.d.ts +11 -0
  66. package/lib/typescript/module/src/EventEmitter.d.ts.map +1 -0
  67. package/lib/typescript/module/src/NativeOrientationDirector.d.ts +17 -0
  68. package/lib/typescript/module/src/NativeOrientationDirector.d.ts.map +1 -0
  69. package/lib/typescript/module/src/RNOrientationDirector.d.ts +62 -0
  70. package/lib/typescript/module/src/RNOrientationDirector.d.ts.map +1 -0
  71. package/lib/typescript/module/src/hooks/useDeviceOrientation.hook.d.ts +8 -0
  72. package/lib/typescript/module/src/hooks/useDeviceOrientation.hook.d.ts.map +1 -0
  73. package/lib/typescript/module/src/hooks/useInterfaceOrientation.hook.d.ts +8 -0
  74. package/lib/typescript/module/src/hooks/useInterfaceOrientation.hook.d.ts.map +1 -0
  75. package/lib/typescript/module/src/hooks/useIsInterfaceOrientationLocked.hook.d.ts +7 -0
  76. package/lib/typescript/module/src/hooks/useIsInterfaceOrientationLocked.hook.d.ts.map +1 -0
  77. package/lib/typescript/module/src/index.d.ts +12 -0
  78. package/lib/typescript/module/src/index.d.ts.map +1 -0
  79. package/lib/typescript/module/src/module.d.ts +6 -0
  80. package/lib/typescript/module/src/module.d.ts.map +1 -0
  81. package/lib/typescript/module/src/types/AutoRotation.enum.d.ts +6 -0
  82. package/lib/typescript/module/src/types/AutoRotation.enum.d.ts.map +1 -0
  83. package/lib/typescript/module/src/types/Event.enum.d.ts +7 -0
  84. package/lib/typescript/module/src/types/Event.enum.d.ts.map +1 -0
  85. package/lib/typescript/module/src/types/HumanReadableAutoRotationsResource.type.d.ts +3 -0
  86. package/lib/typescript/module/src/types/HumanReadableAutoRotationsResource.type.d.ts.map +1 -0
  87. package/lib/typescript/module/src/types/HumanReadableOrientationsResource.type.d.ts +3 -0
  88. package/lib/typescript/module/src/types/HumanReadableOrientationsResource.type.d.ts.map +1 -0
  89. package/lib/typescript/module/src/types/LockableOrientation.type.d.ts +3 -0
  90. package/lib/typescript/module/src/types/LockableOrientation.type.d.ts.map +1 -0
  91. package/lib/typescript/module/src/types/LockedEvent.interface.d.ts +4 -0
  92. package/lib/typescript/module/src/types/LockedEvent.interface.d.ts.map +1 -0
  93. package/lib/typescript/module/src/types/Orientation.enum.d.ts +11 -0
  94. package/lib/typescript/module/src/types/Orientation.enum.d.ts.map +1 -0
  95. package/lib/typescript/module/src/types/OrientationEvent.interface.d.ts +5 -0
  96. package/lib/typescript/module/src/types/OrientationEvent.interface.d.ts.map +1 -0
  97. package/lib/typescript/module/src/types/OrientationType.enum.d.ts +5 -0
  98. package/lib/typescript/module/src/types/OrientationType.enum.d.ts.map +1 -0
  99. package/package.json +32 -13
  100. package/plugin/build/withRNOrientationAppDelegate.d.ts +1 -1
  101. package/plugin/build/withRNOrientationAppDelegate.js +20 -6
  102. package/lib/typescript/src/EventEmitter.d.ts.map +0 -1
  103. package/lib/typescript/src/NativeOrientationDirector.d.ts.map +0 -1
  104. package/lib/typescript/src/RNOrientationDirector.d.ts.map +0 -1
  105. package/lib/typescript/src/hooks/useDeviceOrientation.hook.d.ts.map +0 -1
  106. package/lib/typescript/src/hooks/useInterfaceOrientation.hook.d.ts.map +0 -1
  107. package/lib/typescript/src/hooks/useIsInterfaceOrientationLocked.hook.d.ts.map +0 -1
  108. package/lib/typescript/src/index.d.ts.map +0 -1
  109. package/lib/typescript/src/module.d.ts.map +0 -1
  110. package/lib/typescript/src/types/AutoRotation.enum.d.ts.map +0 -1
  111. package/lib/typescript/src/types/Event.enum.d.ts.map +0 -1
  112. package/lib/typescript/src/types/HumanReadableAutoRotationsResource.type.d.ts.map +0 -1
  113. package/lib/typescript/src/types/HumanReadableOrientationsResource.type.d.ts.map +0 -1
  114. package/lib/typescript/src/types/LockableOrientation.type.d.ts.map +0 -1
  115. package/lib/typescript/src/types/LockedEvent.interface.d.ts.map +0 -1
  116. package/lib/typescript/src/types/Orientation.enum.d.ts.map +0 -1
  117. package/lib/typescript/src/types/OrientationEvent.interface.d.ts.map +0 -1
  118. package/lib/typescript/src/types/OrientationType.enum.d.ts.map +0 -1
  119. /package/lib/typescript/{src → commonjs/src}/EventEmitter.d.ts +0 -0
  120. /package/lib/typescript/{src → commonjs/src}/NativeOrientationDirector.d.ts +0 -0
  121. /package/lib/typescript/{src → commonjs/src}/RNOrientationDirector.d.ts +0 -0
  122. /package/lib/typescript/{src → commonjs/src}/hooks/useDeviceOrientation.hook.d.ts +0 -0
  123. /package/lib/typescript/{src → commonjs/src}/hooks/useInterfaceOrientation.hook.d.ts +0 -0
  124. /package/lib/typescript/{src → commonjs/src}/hooks/useIsInterfaceOrientationLocked.hook.d.ts +0 -0
  125. /package/lib/typescript/{src → commonjs/src}/index.d.ts +0 -0
  126. /package/lib/typescript/{src → commonjs/src}/module.d.ts +0 -0
  127. /package/lib/typescript/{src → commonjs/src}/types/AutoRotation.enum.d.ts +0 -0
  128. /package/lib/typescript/{src → commonjs/src}/types/Event.enum.d.ts +0 -0
  129. /package/lib/typescript/{src → commonjs/src}/types/HumanReadableAutoRotationsResource.type.d.ts +0 -0
  130. /package/lib/typescript/{src → commonjs/src}/types/HumanReadableOrientationsResource.type.d.ts +0 -0
  131. /package/lib/typescript/{src → commonjs/src}/types/LockableOrientation.type.d.ts +0 -0
  132. /package/lib/typescript/{src → commonjs/src}/types/LockedEvent.interface.d.ts +0 -0
  133. /package/lib/typescript/{src → commonjs/src}/types/Orientation.enum.d.ts +0 -0
  134. /package/lib/typescript/{src → commonjs/src}/types/OrientationEvent.interface.d.ts +0 -0
  135. /package/lib/typescript/{src → commonjs/src}/types/OrientationType.enum.d.ts +0 -0
  136. /package/lib/typescript/{package.json → module/package.json} +0 -0
package/README.md CHANGED
@@ -78,23 +78,33 @@ This library uses a custom broadcast receiver to handle the manual orientation c
78
78
  autorotation feature and the system prompts the user to rotate the device, the library will listen to the broadcast
79
79
  sent by the MainActivity and update the interface orientation accordingly.
80
80
 
81
- To allow the library to listen to the broadcast, you need to override the `onConfigurationChanged` method in your
82
- MainActivity file, as shown below:
81
+ To allow the library to listen to the broadcast, you need to override the `onConfigurationChanged` method in MainActivity.kt as shown below:
83
82
 
84
83
  ```kotlin
85
- override fun onConfigurationChanged(newConfig: Configuration) {
86
- super.onConfigurationChanged(newConfig)
84
+ // ...
87
85
 
88
- val orientationDirectorCustomAction =
89
- "${packageName}.${ConfigurationChangedBroadcastReceiver.CUSTOM_INTENT_ACTION}"
86
+ import android.content.Intent
87
+ import android.content.res.Configuration
88
+ import com.orientationdirector.implementation.ConfigurationChangedBroadcastReceiver
90
89
 
91
- val intent =
92
- Intent(orientationDirectorCustomAction).apply {
93
- putExtra("newConfig", newConfig)
94
- setPackage(packageName)
95
- }
90
+ class MainActivity : ReactActivity() {
96
91
 
97
- this.sendBroadcast(intent)
92
+ // ...
93
+
94
+ override fun onConfigurationChanged(newConfig: Configuration) {
95
+ super.onConfigurationChanged(newConfig)
96
+
97
+ val orientationDirectorCustomAction =
98
+ "${packageName}.${ConfigurationChangedBroadcastReceiver.CUSTOM_INTENT_ACTION}"
99
+
100
+ val intent =
101
+ Intent(orientationDirectorCustomAction).apply {
102
+ putExtra("newConfig", newConfig)
103
+ setPackage(packageName)
104
+ }
105
+
106
+ this.sendBroadcast(intent)
107
+ }
98
108
  }
99
109
  ```
100
110
 
@@ -33,7 +33,7 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
33
33
  mAutoRotationObserver.enable()
34
34
 
35
35
  mBroadcastReceiver.setOnReceiveCallback {
36
- adaptInterfaceTo(lastDeviceOrientation, false)
36
+ checkInterfaceOrientation(false)
37
37
  }
38
38
 
39
39
  context.addLifecycleEventListener(mLifecycleListener)
@@ -113,7 +113,7 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
113
113
  context.currentActivity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
114
114
 
115
115
  updateIsLockedTo(false)
116
- adaptInterfaceTo(lastDeviceOrientation)
116
+ checkInterfaceOrientation()
117
117
  }
118
118
 
119
119
  fun resetSupportedInterfaceOrientations() {
@@ -161,7 +161,7 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
161
161
  mEventManager.sendDeviceOrientationDidChange(deviceOrientation.ordinal)
162
162
  lastDeviceOrientation = deviceOrientation
163
163
 
164
- adaptInterfaceTo(deviceOrientation)
164
+ checkInterfaceOrientation()
165
165
 
166
166
  if (!didComputeInitialDeviceOrientation) {
167
167
  didComputeInitialDeviceOrientation = true
@@ -169,50 +169,35 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
169
169
  }
170
170
  }
171
171
 
172
- private fun adaptInterfaceTo(deviceOrientation: Orientation, checkLastAutoRotationStatus: Boolean = true) {
173
- if (checkLastAutoRotationStatus && !mAutoRotationObserver.getLastAutoRotationStatus()) {
172
+ private fun checkInterfaceOrientation(skipIfAutoRotationIsDisabled: Boolean = true) {
173
+ if (skipIfAutoRotationIsDisabled && !mAutoRotationObserver.getLastAutoRotationStatus()) {
174
174
  return
175
175
  }
176
176
 
177
- val supportsLandscape =
178
- mUtils.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
179
- if (isLocked && !supportsLandscape) {
177
+ if (isLocked) {
180
178
  return
181
179
  }
182
180
 
183
- var newInterfaceOrientation = mUtils.convertToInterfaceOrientationFrom(deviceOrientation);
184
-
185
- /**
186
- * When the device orientation is either face up or face down,
187
- * we can't match it to an interface orientation, because
188
- * it could be either portrait or any landscape.
189
- * So we read it from the system itself.
190
- */
191
- if (newInterfaceOrientation == Orientation.UNKNOWN) {
181
+ if (lastDeviceOrientation != Orientation.LANDSCAPE_RIGHT && lastDeviceOrientation != Orientation.LANDSCAPE_LEFT) {
192
182
  val rotation = mUtils.getInterfaceRotation()
193
- newInterfaceOrientation = mUtils.convertToOrientationFromScreenRotation(rotation)
194
- }
183
+ val newInterfaceOrientation = mUtils.convertToOrientationFromScreenRotation(rotation)
195
184
 
196
- /**
197
- * This differs from iOS because we can't read the actual orientation of the interface,
198
- * we read its rotation.
199
- * This means that even if the requestedOrientation of the currentActivity is locked to landscape
200
- * it reads every possible orientation and this is not what we want.
201
- * Instead, we check that its value is either LANDSCAPE_RIGHT or LANDSCAPE_LEFT, otherwise we
202
- * exit
203
- */
204
- val newInterfaceOrientationIsNotLandscape =
205
- newInterfaceOrientation != Orientation.LANDSCAPE_RIGHT
206
- && newInterfaceOrientation != Orientation.LANDSCAPE_LEFT;
207
- if (supportsLandscape && newInterfaceOrientationIsNotLandscape) {
185
+ updateLastInterfaceOrientationTo(newInterfaceOrientation)
208
186
  return
209
187
  }
210
188
 
211
- if (newInterfaceOrientation == lastInterfaceOrientation) {
212
- return
213
- }
189
+ /**
190
+ * The reason we invert the interface orientation is to match iOS behavior with
191
+ * UIInterfaceOrientation when device is in landscape mode
192
+ */
193
+ val interfaceOrientationBasedOnDeviceOne =
194
+ if (lastDeviceOrientation == Orientation.LANDSCAPE_RIGHT) {
195
+ Orientation.LANDSCAPE_LEFT
196
+ } else {
197
+ Orientation.LANDSCAPE_RIGHT
198
+ }
214
199
 
215
- updateLastInterfaceOrientationTo(newInterfaceOrientation)
200
+ updateLastInterfaceOrientationTo(interfaceOrientationBasedOnDeviceOne)
216
201
  }
217
202
 
218
203
  private fun updateIsLockedTo(value: Boolean) {
@@ -221,6 +206,10 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
221
206
  }
222
207
 
223
208
  private fun updateLastInterfaceOrientationTo(value: Orientation) {
209
+ if (value == lastInterfaceOrientation) {
210
+ return
211
+ }
212
+
224
213
  lastInterfaceOrientation = value
225
214
  mEventManager.sendInterfaceOrientationDidChange(value.ordinal)
226
215
  }
@@ -10,12 +10,20 @@ import com.facebook.react.bridge.ReactApplicationContext
10
10
  class OrientationSensorsEventListener(
11
11
  context: ReactApplicationContext,
12
12
  ) : SensorEventListener {
13
- private var mSensorManager: SensorManager = context.getSystemService(SENSOR_SERVICE) as SensorManager
13
+ private var mSensorManager: SensorManager =
14
+ context.getSystemService(SENSOR_SERVICE) as SensorManager
14
15
 
15
- private var mAccelerometerSensor: Sensor? = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
16
- private var mMagneticFieldSensor: Sensor? = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
16
+ private var mRotationSensor: Sensor? =
17
+ mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)
18
+ private var mAccelerometerSensor: Sensor? =
19
+ mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
20
+ private var mMagneticFieldSensor: Sensor? =
21
+ mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
17
22
 
18
- private var hasRequiredSensors: Boolean = mAccelerometerSensor != null && mMagneticFieldSensor != null
23
+ private var hasRotationSensor: Boolean =
24
+ mRotationSensor != null
25
+ private var hasAccelerometerAndMagneticFieldSensors: Boolean =
26
+ mAccelerometerSensor != null && mMagneticFieldSensor != null
19
27
 
20
28
  private val accelerometerReading = FloatArray(3)
21
29
  private val magnetometerReading = FloatArray(3)
@@ -32,6 +40,59 @@ class OrientationSensorsEventListener(
32
40
  return
33
41
  }
34
42
 
43
+ if (event.sensor.type == Sensor.TYPE_ROTATION_VECTOR) {
44
+ computeOrientationFromRotationSensor(event.values);
45
+ return
46
+ }
47
+
48
+ computeOrientationFromOtherSensors(event)
49
+ }
50
+
51
+ override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
52
+
53
+ fun enable() {
54
+ if (hasRotationSensor) {
55
+ mSensorManager.registerListener(
56
+ this,
57
+ mRotationSensor,
58
+ SensorManager.SENSOR_DELAY_NORMAL,
59
+ SensorManager.SENSOR_DELAY_UI
60
+ )
61
+ return
62
+ }
63
+
64
+ if (hasAccelerometerAndMagneticFieldSensors) {
65
+ mSensorManager.registerListener(
66
+ this,
67
+ mAccelerometerSensor,
68
+ SensorManager.SENSOR_DELAY_NORMAL,
69
+ SensorManager.SENSOR_DELAY_UI
70
+ )
71
+ mSensorManager.registerListener(
72
+ this,
73
+ mMagneticFieldSensor,
74
+ SensorManager.SENSOR_DELAY_NORMAL,
75
+ SensorManager.SENSOR_DELAY_UI
76
+ )
77
+ return
78
+ }
79
+ }
80
+
81
+ fun disable() {
82
+ mSensorManager.unregisterListener(this)
83
+ }
84
+
85
+ private fun computeOrientationFromRotationSensor(values: FloatArray) {
86
+ val rotationMatrix = FloatArray(9)
87
+ SensorManager.getRotationMatrixFromVector(rotationMatrix, values)
88
+
89
+ val orientationAngles = FloatArray(3)
90
+ SensorManager.getOrientation(rotationMatrix, orientationAngles)
91
+
92
+ notifyOrientationAnglesChanged(orientationAngles)
93
+ }
94
+
95
+ private fun computeOrientationFromOtherSensors(event: SensorEvent) {
35
96
  if (event.sensor.type == Sensor.TYPE_ACCELEROMETER) {
36
97
  System.arraycopy(event.values, 0, accelerometerReading, 0, accelerometerReading.size)
37
98
  }
@@ -54,6 +115,10 @@ class OrientationSensorsEventListener(
54
115
  val orientationAngles = FloatArray(3)
55
116
  SensorManager.getOrientation(rotationMatrix, orientationAngles)
56
117
 
118
+ notifyOrientationAnglesChanged(orientationAngles)
119
+ }
120
+
121
+ private fun notifyOrientationAnglesChanged(orientationAngles: FloatArray) {
57
122
  if (lastComputedOrientationAngles.contentEquals(orientationAngles)) {
58
123
  return
59
124
  }
@@ -61,19 +126,4 @@ class OrientationSensorsEventListener(
61
126
  onOrientationAnglesChangedCallback?.invoke(orientationAngles)
62
127
  lastComputedOrientationAngles = orientationAngles
63
128
  }
64
-
65
- override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
66
-
67
- fun enable() {
68
- if (!hasRequiredSensors) {
69
- return
70
- }
71
-
72
- mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL)
73
- mSensorManager.registerListener(this, mMagneticFieldSensor, SensorManager.SENSOR_DELAY_NORMAL)
74
- }
75
-
76
- fun disable() {
77
- mSensorManager.unregisterListener(this)
78
- }
79
129
  }
@@ -20,31 +20,47 @@ class Utils(private val context: ReactContext) {
20
20
  }
21
21
 
22
22
  fun convertToDeviceOrientationFrom(orientationAngles: FloatArray): Orientation {
23
- val (_, pitchRadians, rollRadians) = orientationAngles;
23
+ if (orientationAngles.size < 3) {
24
+ return Orientation.PORTRAIT
25
+ }
26
+
27
+ val (_, pitchRadians, rollRadians) = orientationAngles
24
28
 
25
- val pitchDegrees = Math.toDegrees(pitchRadians.toDouble()).toFloat()
26
- val rollDegrees = Math.toDegrees(rollRadians.toDouble()).toFloat()
29
+ val pitch = Math.toDegrees(pitchRadians.toDouble()).toFloat()
30
+ val roll = Math.toDegrees(rollRadians.toDouble()).toFloat()
27
31
 
28
- // This is needed to account for inaccuracy due to subtle movements such as tilting
29
- val tolerance = 20f
32
+ val faceUpDownPitchTolerance = 30f
30
33
 
31
- //////////////////////////////////////
32
- // These limits are set based on SensorManager.getOrientation reference
33
- // https://developer.android.com/develop/sensors-and-location/sensors/sensors_position#sensors-pos-orient
34
- //
35
- val portraitLimit = -90f
36
- val landscapeRightLimit = 180f
37
- val landscapeLeftLimit = -180f
38
- //
39
- //////////////////////////////////////
34
+ fun isValueCloseTo(value: Float, target: Float, tolerance: Float): Boolean {
35
+ return value in (target - tolerance)..(target + tolerance)
36
+ }
40
37
 
41
38
  return when {
42
- rollDegrees.equals(-0f) && (pitchDegrees.equals(0f) || pitchDegrees.equals(-0f)) -> Orientation.FACE_UP
43
- 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
- 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
48
64
  }
49
65
  }
50
66
 
@@ -86,12 +102,4 @@ class Utils(private val context: ReactContext) {
86
102
  else -> Orientation.UNKNOWN
87
103
  }
88
104
  }
89
-
90
- fun getRequestedOrientation(): Int {
91
- if (context.currentActivity?.requestedOrientation == null) {
92
- return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
93
- }
94
-
95
- return context.currentActivity!!.requestedOrientation;
96
- }
97
105
  }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _reactNative = require("react-native");
8
+ var _module = _interopRequireWildcard(require("./module.js"));
9
+ var _EventEnum = _interopRequireDefault(require("./types/Event.enum.js"));
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
12
+ class EventEmitter {
13
+ static addDeviceOrientationDidChangeListener(callback) {
14
+ let listener = _module.ModuleEventEmitter.addListener(_EventEnum.default.DeviceOrientationDidChange, callback);
15
+ if (_reactNative.Platform.OS !== 'android') {
16
+ return listener;
17
+ }
18
+ const listenerCount = _module.ModuleEventEmitter.listenerCount(_EventEnum.default.DeviceOrientationDidChange);
19
+ if (listenerCount === 1) {
20
+ _module.default.enableOrientationSensors();
21
+ }
22
+ return EventEmitter.createDeviceOrientationListenerProxy(listener);
23
+ }
24
+ static addInterfaceOrientationDidChangeListener(callback) {
25
+ return _module.ModuleEventEmitter.addListener(_EventEnum.default.InterfaceOrientationDidChange, callback);
26
+ }
27
+ static addLockDidChangeListener(callback) {
28
+ return _module.ModuleEventEmitter.addListener(_EventEnum.default.LockDidChange, callback);
29
+ }
30
+ static createDeviceOrientationListenerProxy(listener) {
31
+ const handler = {
32
+ get(target, propertyKey, receiver) {
33
+ if (propertyKey === 'remove') {
34
+ disableOrientationSensorsIfLastListener();
35
+ }
36
+ return Reflect.get(target, propertyKey, receiver);
37
+ }
38
+ };
39
+ return new Proxy(listener, handler);
40
+ function disableOrientationSensorsIfLastListener() {
41
+ const listenerCount = _module.ModuleEventEmitter.listenerCount(_EventEnum.default.DeviceOrientationDidChange);
42
+ if (listenerCount === 1) {
43
+ _module.default.disableOrientationSensors();
44
+ }
45
+ }
46
+ }
47
+ }
48
+ var _default = exports.default = EventEmitter;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _reactNative = require("react-native");
8
+ var _default = exports.default = _reactNative.TurboModuleRegistry.getEnforcing('OrientationDirector');
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _reactNative = require("react-native");
8
+ var _module = _interopRequireDefault(require("./module.js"));
9
+ var _OrientationEnum = require("./types/Orientation.enum.js");
10
+ var _AutoRotationEnum = require("./types/AutoRotation.enum.js");
11
+ var _OrientationTypeEnum = require("./types/OrientationType.enum.js");
12
+ var _EventEmitter = _interopRequireDefault(require("./EventEmitter.js"));
13
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
+ class RNOrientationDirector {
15
+ static _humanReadableOrientationsResource = {
16
+ [_OrientationEnum.Orientation.unknown]: 'Unknown',
17
+ [_OrientationEnum.Orientation.portrait]: 'Portrait',
18
+ [_OrientationEnum.Orientation.portraitUpsideDown]: 'Portrait Upside Down',
19
+ [_OrientationEnum.Orientation.landscapeLeft]: 'Landscape Left',
20
+ [_OrientationEnum.Orientation.landscapeRight]: 'Landscape Right',
21
+ [_OrientationEnum.Orientation.landscape]: 'Landscape',
22
+ [_OrientationEnum.Orientation.faceUp]: 'Face Up',
23
+ [_OrientationEnum.Orientation.faceDown]: 'Face Down'
24
+ };
25
+ static _humanReadableAutoRotationsResource = {
26
+ [_AutoRotationEnum.AutoRotation.unknown]: 'Unknown',
27
+ [_AutoRotationEnum.AutoRotation.enabled]: 'Enabled',
28
+ [_AutoRotationEnum.AutoRotation.disabled]: 'Disabled'
29
+ };
30
+ setHumanReadableOrientations(resource) {
31
+ RNOrientationDirector._humanReadableOrientationsResource = resource;
32
+ }
33
+ setHumanReadableAutoRotations(resource) {
34
+ RNOrientationDirector._humanReadableAutoRotationsResource = resource;
35
+ }
36
+ static getInterfaceOrientation() {
37
+ return _module.default.getInterfaceOrientation();
38
+ }
39
+ static getDeviceOrientation() {
40
+ return _module.default.getDeviceOrientation();
41
+ }
42
+
43
+ /**
44
+ * Please be aware that device orientation is not the
45
+ * same as interface orientation.
46
+ *
47
+ * Specifically, landscape left and right are inverted:
48
+ *
49
+ * - landscapeLeft in device orientation is landscapeRight in interface orientation
50
+ * - landscapeRight in device orientation is landscapeLeft in interface orientation
51
+ *
52
+ * This is a behavior of the native API.
53
+ *
54
+ * When you pass an orientation value, do provide orientationType
55
+ * as well if the orientation value is not an interface orientation.
56
+ * Example: when using listenForDeviceOrientationChanges.
57
+ *
58
+ * @param orientation any lockable orientation enum value
59
+ * @param orientationType any orientation type enum value
60
+ */
61
+ static lockTo(orientation, orientationType = _OrientationTypeEnum.OrientationType.interface) {
62
+ if (orientationType === _OrientationTypeEnum.OrientationType.interface) {
63
+ _module.default.lockTo(orientation);
64
+ return;
65
+ }
66
+ if (orientation === _OrientationEnum.Orientation.landscapeLeft) {
67
+ _module.default.lockTo(_OrientationEnum.Orientation.landscapeRight);
68
+ return;
69
+ }
70
+ if (orientation === _OrientationEnum.Orientation.landscapeRight) {
71
+ _module.default.lockTo(_OrientationEnum.Orientation.landscapeLeft);
72
+ return;
73
+ }
74
+ _module.default.lockTo(orientation);
75
+ }
76
+ static unlock() {
77
+ _module.default.unlock();
78
+ }
79
+ static isLocked() {
80
+ return _module.default.isLocked();
81
+ }
82
+ static isAutoRotationEnabled() {
83
+ if (_reactNative.Platform.OS !== 'android') {
84
+ return _AutoRotationEnum.AutoRotation.unknown;
85
+ }
86
+ return _module.default.isAutoRotationEnabled() ? _AutoRotationEnum.AutoRotation.enabled : _AutoRotationEnum.AutoRotation.disabled;
87
+ }
88
+ static resetSupportedInterfaceOrientations() {
89
+ _module.default.resetSupportedInterfaceOrientations();
90
+ }
91
+ static listenForDeviceOrientationChanges(callback) {
92
+ return _EventEmitter.default.addDeviceOrientationDidChangeListener(callback);
93
+ }
94
+ static listenForInterfaceOrientationChanges(callback) {
95
+ return _EventEmitter.default.addInterfaceOrientationDidChangeListener(callback);
96
+ }
97
+ static listenForLockChanges(callback) {
98
+ return _EventEmitter.default.addLockDidChangeListener(callback);
99
+ }
100
+ static convertOrientationToHumanReadableString(orientation) {
101
+ return RNOrientationDirector._humanReadableOrientationsResource[orientation];
102
+ }
103
+ static convertAutoRotationToHumanReadableString(autoRotation) {
104
+ return RNOrientationDirector._humanReadableAutoRotationsResource[autoRotation];
105
+ }
106
+
107
+ /**
108
+ * This method checks if the given orientation is lockable
109
+ * by interface perspective.
110
+ *
111
+ * All orientations are lockable except for unknown, faceUp
112
+ * and faceDown.
113
+ *
114
+ * This method is useful when you want to lock the interface
115
+ * orientation from a given device orientation.
116
+ *
117
+ * Example: with listenForDeviceOrientationChanges
118
+ *
119
+ * @param orientation any orientation enum value
120
+ * @returns true if the orientation is lockable
121
+ */
122
+ static isLockableOrientation(orientation) {
123
+ return !(orientation === _OrientationEnum.Orientation.unknown || orientation === _OrientationEnum.Orientation.faceUp || orientation === _OrientationEnum.Orientation.faceDown);
124
+ }
125
+ }
126
+ var _default = exports.default = RNOrientationDirector;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _react = _interopRequireWildcard(require("react"));
8
+ var _RNOrientationDirector = _interopRequireDefault(require("../RNOrientationDirector.js"));
9
+ var _OrientationEnum = require("../types/Orientation.enum.js");
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
12
+ /**
13
+ * Hook that returns the current device orientation.
14
+ * It listens for orientation changes and updates the state accordingly.
15
+ */
16
+ const useDeviceOrientation = () => {
17
+ const initialRender = (0, _react.useRef)(false);
18
+ const [orientation, setOrientation] = _react.default.useState(_OrientationEnum.Orientation.unknown);
19
+ _react.default.useEffect(() => {
20
+ if (initialRender.current) {
21
+ return;
22
+ }
23
+ initialRender.current = true;
24
+ _RNOrientationDirector.default.getDeviceOrientation().then(setOrientation);
25
+ }, []);
26
+ _react.default.useEffect(() => {
27
+ const onChange = event => {
28
+ setOrientation(event.orientation);
29
+ };
30
+ const subscription = _RNOrientationDirector.default.listenForDeviceOrientationChanges(onChange);
31
+ return () => {
32
+ subscription.remove();
33
+ };
34
+ }, []);
35
+ return orientation;
36
+ };
37
+ var _default = exports.default = useDeviceOrientation;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _react = _interopRequireWildcard(require("react"));
8
+ var _RNOrientationDirector = _interopRequireDefault(require("../RNOrientationDirector.js"));
9
+ var _OrientationEnum = require("../types/Orientation.enum.js");
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
12
+ /**
13
+ * Hook that returns the current interface orientation.
14
+ * It listens for orientation changes and updates the state accordingly.
15
+ */
16
+ const useInterfaceOrientation = () => {
17
+ const initialRender = (0, _react.useRef)(false);
18
+ const [orientation, setOrientation] = _react.default.useState(_OrientationEnum.Orientation.unknown);
19
+ _react.default.useEffect(() => {
20
+ if (initialRender.current) {
21
+ return;
22
+ }
23
+ initialRender.current = true;
24
+ _RNOrientationDirector.default.getInterfaceOrientation().then(setOrientation);
25
+ }, []);
26
+ _react.default.useEffect(() => {
27
+ const onChange = event => {
28
+ setOrientation(event.orientation);
29
+ };
30
+ const subscription = _RNOrientationDirector.default.listenForInterfaceOrientationChanges(onChange);
31
+ return () => {
32
+ subscription.remove();
33
+ };
34
+ }, []);
35
+ return orientation;
36
+ };
37
+ var _default = exports.default = useInterfaceOrientation;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _react = _interopRequireDefault(require("react"));
8
+ var _RNOrientationDirector = _interopRequireDefault(require("../RNOrientationDirector.js"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ /**
11
+ * Hook that returns whether the interface is locked.
12
+ * It listens for changes and updates the state accordingly.
13
+ */
14
+ const useIsInterfaceOrientationLocked = () => {
15
+ const [orientation, setOrientation] = _react.default.useState(() => _RNOrientationDirector.default.isLocked());
16
+ _react.default.useEffect(() => {
17
+ const onChange = event => {
18
+ setOrientation(event.locked);
19
+ };
20
+ const subscription = _RNOrientationDirector.default.listenForLockChanges(onChange);
21
+ return () => {
22
+ subscription.remove();
23
+ };
24
+ }, []);
25
+ return orientation;
26
+ };
27
+ var _default = exports.default = useIsInterfaceOrientationLocked;