Skip to content

Commit d59bd2d

Browse files
authored
Merge pull request #8 from logicwind/crash_hot_fix
Crash hot fix To Main
2 parents 2b4b992 + c014d0a commit d59bd2d

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

android/src/main/java/com/folddetection/FoldDetectionModule.kt

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,52 @@ import androidx.window.java.layout.WindowInfoTrackerCallbackAdapter
1212
import androidx.window.layout.FoldingFeature
1313
import androidx.window.layout.WindowInfoTracker
1414
import androidx.window.layout.WindowLayoutInfo
15-
import com.facebook.react.bridge.Promise
1615
import 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
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@logicwind/react-native-fold-detection",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "The purpose of the package is to provide details regarding the Android folding capability.",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/context/FoldingFeatureContext.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,15 @@ const useProvideFunc = (): FoldingFeatureContextProps => {
117117
}
118118
);
119119

120+
const errorSubscription = eventEmitter.addListener('onError', (event) => {
121+
if (event?.error) {
122+
console.log('FoldingFeature', event.error);
123+
}
124+
});
125+
120126
return () => {
121127
layoutSubscription.remove();
128+
errorSubscription.remove();
122129
};
123130
}, []);
124131

0 commit comments

Comments
 (0)