11package com.neoorientation
22
3+ import android.annotation.SuppressLint
34import android.content.BroadcastReceiver
45import android.content.Context
56import android.content.Intent
67import android.content.IntentFilter
78import android.content.pm.ActivityInfo
89import android.hardware.SensorManager
9- import android.os.Build
1010import android.provider.Settings
1111import android.view.OrientationEventListener
1212import android.view.Surface
@@ -20,9 +20,8 @@ import com.facebook.react.bridge.ReactMethod
2020import com.facebook.react.common.ReactConstants
2121import com.facebook.react.modules.core.DeviceEventManagerModule
2222
23- class NeoOrientationModule (reactContext : ReactApplicationContext ) :
23+ class NeoOrientationModule (private val reactContext : ReactApplicationContext ) :
2424 ReactContextBaseJavaModule (reactContext), NeoOrientationListeners {
25- private val reactContext: ReactApplicationContext
2625 private val mReceiver: BroadcastReceiver
2726 private val mOrientationListener: OrientationEventListener
2827 private var isLocking = false
@@ -31,7 +30,6 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
3130 private var lastDeviceOrientation = " "
3231
3332 init {
34- this .reactContext = reactContext
3533 mOrientationListener =
3634 object : OrientationEventListener (reactContext, SensorManager .SENSOR_DELAY_UI ) {
3735 override fun onOrientationChanged (p0 : Int ) {
@@ -54,8 +52,7 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
5452 if (reactContext.hasActiveReactInstance()) {
5553 reactContext.getJSModule(
5654 DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java
57- )
58- .emit(" deviceOrientationDidChange" , params)
55+ ).emit(" deviceOrientationDidChange" , params)
5956 }
6057 }
6158 val orientation: String = currentOrientation
@@ -64,14 +61,11 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
6461 val params = Arguments .createMap()
6562 params.putString(" orientation" , orientation)
6663 if (reactContext.hasActiveReactInstance()) {
67- reactContext
68- .getJSModule(
69- DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java
70- )
71- .emit(" orientationDidChange" , params)
64+ reactContext.getJSModule(
65+ DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java
66+ ).emit(" orientationDidChange" , params)
7267 }
7368 }
74- return
7569 }
7670 }
7771 if (mOrientationListener.canDetectOrientation()) {
@@ -86,11 +80,9 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
8680 val params = Arguments .createMap()
8781 params.putString(" orientation" , orientation)
8882 if (reactContext.hasActiveReactInstance()) {
89- reactContext
90- .getJSModule(
91- DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java
92- )
93- .emit(" orientationDidChange" , params)
83+ reactContext.getJSModule(
84+ DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java
85+ ).emit(" orientationDidChange" , params)
9486 }
9587 }
9688 }
@@ -99,15 +91,14 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
9991
10092 private val currentOrientation: String
10193 get() {
102- val display =
103- (reactApplicationContext.getSystemService( Context . WINDOW_SERVICE ) as WindowManager ).defaultDisplay
104- when (display.rotation) {
105- Surface .ROTATION_0 -> return " portrait "
106- Surface .ROTATION_90 -> return " landscapeLeft "
107- Surface .ROTATION_180 -> return " portraitUpsideDown "
108- Surface . ROTATION_270 -> return " landscapeRight "
94+ val display = (reactApplicationContext.getSystemService( Context . WINDOW_SERVICE ) as WindowManager ).defaultDisplay
95+ return when (display.rotation) {
96+ Surface . ROTATION_0 -> " portrait "
97+ Surface .ROTATION_90 -> " landscapeLeft "
98+ Surface .ROTATION_180 -> " portraitUpsideDown "
99+ Surface .ROTATION_270 -> " landscapeRight "
100+ else -> " unknown "
109101 }
110- return " unknown"
111102 }
112103
113104 override fun getName (): String {
@@ -141,8 +132,7 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
141132 val lockParams = Arguments .createMap()
142133 lockParams.putString(" orientation" , lastOrientation)
143134 if (reactContext.hasActiveReactInstance()) {
144- reactContext
145- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
135+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
146136 .emit(" lockDidChange" , lockParams)
147137 }
148138 }
@@ -157,16 +147,14 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
157147 val params = Arguments .createMap()
158148 params.putString(" orientation" , lastOrientation)
159149 if (reactContext.hasActiveReactInstance()) {
160- reactContext
161- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
150+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
162151 .emit(" orientationDidChange" , params)
163152 }
164153
165154 val lockParams = Arguments .createMap()
166155 lockParams.putString(" orientation" , lastOrientation)
167156 if (reactContext.hasActiveReactInstance()) {
168- reactContext
169- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
157+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
170158 .emit(" lockDidChange" , lockParams)
171159 }
172160 }
@@ -182,17 +170,15 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
182170 val params = Arguments .createMap()
183171 params.putString(" orientation" , lastOrientation)
184172 if (reactContext.hasActiveReactInstance()) {
185- reactContext
186- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
173+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
187174 .emit(" orientationDidChange" , params)
188175 }
189176
190177 // send a locked event
191178 val lockParams = Arguments .createMap()
192179 lockParams.putString(" orientation" , lastOrientation)
193180 if (reactContext.hasActiveReactInstance()) {
194- reactContext
195- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
181+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
196182 .emit(" lockDidChange" , lockParams)
197183 }
198184 }
@@ -208,16 +194,14 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
208194 val params = Arguments .createMap()
209195 params.putString(" orientation" , lastOrientation)
210196 if (reactContext.hasActiveReactInstance()) {
211- reactContext
212- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
197+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
213198 .emit(" orientationDidChange" , params)
214199 }
215200
216201 val lockParams = Arguments .createMap()
217202 lockParams.putString(" orientation" , lastOrientation)
218203 if (reactContext.hasActiveReactInstance()) {
219- reactContext
220- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
204+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
221205 .emit(" lockDidChange" , lockParams)
222206 }
223207 }
@@ -232,16 +216,14 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
232216 val params = Arguments .createMap()
233217 params.putString(" orientation" , lastOrientation)
234218 if (reactContext.hasActiveReactInstance()) {
235- reactContext
236- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
219+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
237220 .emit(" orientationDidChange" , params)
238221 }
239222
240223 val lockParams = Arguments .createMap()
241224 lockParams.putString(" orientation" , lastOrientation)
242225 if (reactContext.hasActiveReactInstance()) {
243- reactContext
244- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
226+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
245227 .emit(" lockDidChange" , lockParams)
246228 }
247229 }
@@ -256,16 +238,14 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
256238 val params = Arguments .createMap()
257239 params.putString(" orientation" , lastOrientation)
258240 if (reactContext.hasActiveReactInstance()) {
259- reactContext
260- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
241+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
261242 .emit(" orientationDidChange" , params)
262243 }
263244
264245 val lockParams = Arguments .createMap()
265246 lockParams.putString(" orientation" , " unknown" )
266247 if (reactContext.hasActiveReactInstance()) {
267- reactContext
268- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
248+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
269249 .emit(" lockDidChange" , lockParams)
270250 }
271251 }
@@ -289,7 +269,11 @@ class NeoOrientationModule(reactContext: ReactApplicationContext) :
289269
290270 override fun start () {
291271 mOrientationListener.enable()
292- reactContext.registerReceiver(mReceiver, IntentFilter (" onConfigurationChanged" ))
272+ val intentFilter = IntentFilter (" onConfigurationChanged" ).apply {
273+ // addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)
274+ }
275+
276+ reactContext.registerReceiver(mReceiver, intentFilter, Context .RECEIVER_NOT_EXPORTED )
293277 isConfigurationChangeReceiverRegistered = true
294278 }
295279
0 commit comments