Skip to content

Commit 7248549

Browse files
Copilotsds100
andcommitted
Add error handling and documentation for hinge constraint
Co-authored-by: sds100 <16245954+sds100@users.noreply.github.com>
1 parent e1a315a commit 7248549

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

system/src/main/java/io/github/sds100/keymapper/system/hinge/AndroidHingeAdapter.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
1212
import kotlinx.coroutines.flow.StateFlow
1313
import kotlinx.coroutines.flow.asStateFlow
1414
import kotlinx.coroutines.launch
15+
import timber.log.Timber
1516
import javax.inject.Inject
1617
import javax.inject.Singleton
1718

@@ -27,14 +28,23 @@ class AndroidHingeAdapter @Inject constructor(
2728
/**
2829
* This should be called with an Activity context when the accessibility service
2930
* or main activity is available. The WindowInfoTracker needs an Activity.
31+
*
32+
* Note: For background services, hinge detection has limitations. The WindowManager
33+
* APIs work best with an active Activity context. Apps running in the background
34+
* may not receive real-time hinge state updates.
3035
*/
3136
fun startMonitoring(activity: Activity) {
32-
val windowInfoTracker = WindowInfoTracker.getOrCreate(activity)
33-
34-
coroutineScope.launch {
35-
windowInfoTracker.windowLayoutInfo(activity).collect { layoutInfo ->
36-
updateHingeState(layoutInfo)
37+
try {
38+
val windowInfoTracker = WindowInfoTracker.getOrCreate(activity)
39+
40+
coroutineScope.launch {
41+
windowInfoTracker.windowLayoutInfo(activity).collect { layoutInfo ->
42+
updateHingeState(layoutInfo)
43+
}
3744
}
45+
} catch (e: Exception) {
46+
Timber.e(e, "Failed to start hinge monitoring")
47+
_hingeState.value = HingeState(isAvailable = false, angle = null)
3848
}
3949
}
4050

@@ -51,7 +61,7 @@ class AndroidHingeAdapter @Inject constructor(
5161
// FoldingFeature.State has FLAT and HALF_OPENED states
5262
// We can estimate the angle based on the state
5363
val angle = when (foldingFeature.state) {
54-
FoldingFeature.State.FLAT -> 180f // Fully open
64+
FoldingFeature.State.FLAT -> 180f // Fully open/flat
5565
FoldingFeature.State.HALF_OPENED -> {
5666
// For HALF_OPENED state, we don't have the exact angle from the API
5767
// We'll use 90 degrees as a reasonable estimate for half-opened

0 commit comments

Comments
 (0)