@@ -12,25 +12,52 @@ import androidx.window.java.layout.WindowInfoTrackerCallbackAdapter
1212import androidx.window.layout.FoldingFeature
1313import androidx.window.layout.WindowInfoTracker
1414import androidx.window.layout.WindowLayoutInfo
15- import com.facebook.react.bridge.Promise
1615import java.util.concurrent.Executors
1716
18- class FoldDetectionModule (reactContext : ReactApplicationContext ) : ReactContextBaseJavaModule(reactContext) {
19- private var windowInfoTracker: WindowInfoTrackerCallbackAdapter = WindowInfoTrackerCallbackAdapter (WindowInfoTracker .getOrCreate(reactContext))
17+ class FoldDetectionModule (reactContext : ReactApplicationContext ) :
18+ ReactContextBaseJavaModule (reactContext) {
19+ private var windowInfoTracker: WindowInfoTrackerCallbackAdapter ? = null
2020 private val layoutStateChangeCallback = LayoutStateChangeCallback ()
2121
22+ init {
23+ val packageManager = reactContext.packageManager
24+ if (packageManager.hasSystemFeature(PackageManager .FEATURE_SENSOR_HINGE_ANGLE )) {
25+ windowInfoTracker =
26+ WindowInfoTrackerCallbackAdapter (WindowInfoTracker .getOrCreate(reactContext))
27+ }
28+ }
29+
2230 override fun getName (): String {
2331 return " FoldingFeature"
2432 }
2533
2634 @ReactMethod
2735 fun startListening () {
28- windowInfoTracker.addWindowLayoutInfoListener(currentActivity!! , Executors .newSingleThreadExecutor(), layoutStateChangeCallback)
36+ val activity = currentActivity
37+ try {
38+ if (activity != null && windowInfoTracker != null ) {
39+ windowInfoTracker!! .addWindowLayoutInfoListener(
40+ activity,
41+ Executors .newSingleThreadExecutor(),
42+ layoutStateChangeCallback
43+ )
44+ } else {
45+ sendErrorEvent(" Activity is null or device does not support fold feature in startListening" )
46+ }
47+ } catch (e: Exception ) {
48+ sendErrorEvent(" Error On startListening" )
49+ }
2950 }
3051
3152 @ReactMethod
3253 fun stopListening () {
33- windowInfoTracker.removeWindowLayoutInfoListener(layoutStateChangeCallback)
54+ try {
55+ if (windowInfoTracker != null ) {
56+ windowInfoTracker!! .removeWindowLayoutInfoListener(layoutStateChangeCallback)
57+ }
58+ } catch (e: Exception ) {
59+ sendErrorEvent(" Error On stopListening" )
60+ }
3461 }
3562
3663 inner class LayoutStateChangeCallback : Consumer <WindowLayoutInfo > {
@@ -40,7 +67,8 @@ class FoldDetectionModule(reactContext: ReactApplicationContext) : ReactContextB
4067 try {
4168 val displayFeaturesList = newLayoutInfo.displayFeatures
4269 val packageManager = reactApplicationContext.packageManager
43- val featureSupported = packageManager.hasSystemFeature(PackageManager .FEATURE_SENSOR_HINGE_ANGLE )
70+ val featureSupported =
71+ packageManager.hasSystemFeature(PackageManager .FEATURE_SENSOR_HINGE_ANGLE )
4472
4573 if (displayFeaturesList.isNotEmpty()) {
4674 val feature = displayFeaturesList[0 ] // Assuming there's only one feature
@@ -53,7 +81,7 @@ class FoldDetectionModule(reactContext: ReactApplicationContext) : ReactContextB
5381 featureObject.putString(" orientation" , foldingFeature.orientation.toString())
5482 featureObject.putBoolean(" isSeparating" , foldingFeature.isSeparating)
5583 featureObject.putString(" occlusionType" , foldingFeature.occlusionType.toString())
56- featureObject.putBoolean(" isFoldSupported" ,featureSupported)
84+ featureObject.putBoolean(" isFoldSupported" , featureSupported)
5785
5886 // Parse and include detailed bounds information
5987 val bounds = parseBoundsString(foldingFeature.bounds.toString())
@@ -89,9 +117,20 @@ class FoldDetectionModule(reactContext: ReactApplicationContext) : ReactContextB
89117 return bounds
90118 }
91119 }
92- private fun sendEvent (reactContext : ReactApplicationContext , eventName : String , params : WritableMap ) {
120+
121+ private fun sendEvent (
122+ reactContext : ReactApplicationContext ,
123+ eventName : String ,
124+ params : WritableMap
125+ ) {
93126 reactContext
94127 .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
95128 .emit(eventName, params)
96129 }
130+
131+ private fun sendErrorEvent (errorMessage : String ) {
132+ val event: WritableMap = Arguments .createMap()
133+ event.putString(" error" , errorMessage)
134+ sendEvent(reactApplicationContext, " onError" , event)
135+ }
97136}
0 commit comments