@@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
1212import kotlinx.coroutines.flow.StateFlow
1313import kotlinx.coroutines.flow.asStateFlow
1414import kotlinx.coroutines.launch
15+ import timber.log.Timber
1516import javax.inject.Inject
1617import 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