Skip to content

Commit f9bcfee

Browse files
committed
Added exception handling.
1 parent b141563 commit f9bcfee

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ import androidx.window.layout.WindowInfoTracker
1414
import androidx.window.layout.WindowLayoutInfo
1515
import java.util.concurrent.Executors
1616

17-
class FoldDetectionModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
17+
class FoldDetectionModule(reactContext: ReactApplicationContext) :
18+
ReactContextBaseJavaModule(reactContext) {
1819
private var windowInfoTracker: WindowInfoTrackerCallbackAdapter? = null
1920
private val layoutStateChangeCallback = LayoutStateChangeCallback()
2021

2122
init {
2223
val packageManager = reactContext.packageManager
2324
if (packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_HINGE_ANGLE)) {
24-
windowInfoTracker = WindowInfoTrackerCallbackAdapter(WindowInfoTracker.getOrCreate(reactContext))
25+
windowInfoTracker =
26+
WindowInfoTrackerCallbackAdapter(WindowInfoTracker.getOrCreate(reactContext))
2527
}
2628
}
2729

@@ -32,17 +34,29 @@ class FoldDetectionModule(reactContext: ReactApplicationContext) : ReactContextB
3234
@ReactMethod
3335
fun startListening() {
3436
val activity = currentActivity
35-
if (activity != null && windowInfoTracker != null) {
36-
windowInfoTracker!!.addWindowLayoutInfoListener(activity, Executors.newSingleThreadExecutor(), layoutStateChangeCallback)
37-
} else {
38-
sendErrorEvent("Activity is null or device does not support fold feature in startListening")
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")
3949
}
4050
}
4151

4252
@ReactMethod
4353
fun stopListening() {
44-
if (windowInfoTracker != null) {
45-
windowInfoTracker!!.removeWindowLayoutInfoListener(layoutStateChangeCallback)
54+
try {
55+
if (windowInfoTracker != null) {
56+
windowInfoTracker!!.removeWindowLayoutInfoListener(layoutStateChangeCallback)
57+
}
58+
} catch (e: Exception) {
59+
sendErrorEvent("Error On stopListening")
4660
}
4761
}
4862

@@ -53,7 +67,8 @@ class FoldDetectionModule(reactContext: ReactApplicationContext) : ReactContextB
5367
try {
5468
val displayFeaturesList = newLayoutInfo.displayFeatures
5569
val packageManager = reactApplicationContext.packageManager
56-
val featureSupported = packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_HINGE_ANGLE)
70+
val featureSupported =
71+
packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_HINGE_ANGLE)
5772

5873
if (displayFeaturesList.isNotEmpty()) {
5974
val feature = displayFeaturesList[0] // Assuming there's only one feature
@@ -102,7 +117,12 @@ class FoldDetectionModule(reactContext: ReactApplicationContext) : ReactContextB
102117
return bounds
103118
}
104119
}
105-
private fun sendEvent(reactContext: ReactApplicationContext, eventName: String, params: WritableMap) {
120+
121+
private fun sendEvent(
122+
reactContext: ReactApplicationContext,
123+
eventName: String,
124+
params: WritableMap
125+
) {
106126
reactContext
107127
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
108128
.emit(eventName, params)

0 commit comments

Comments
 (0)