@@ -49,6 +49,7 @@ class PostureDetector(private val context: Context, private val scope: Coroutine
4949
5050 fun attachToWindow (windowCtx : Context ) {
5151 windowContext = windowCtx
52+ _postureInfo .value = getCurrentPostureInfo()
5253 try {
5354 windowInfoTracker = WindowInfoTracker .getOrCreate(windowCtx)
5455 fallbackJob?.cancel()
@@ -109,7 +110,8 @@ class PostureDetector(private val context: Context, private val scope: Coroutine
109110 }
110111
111112 private fun updatePostureFromLayoutInfo (layoutInfo : androidx.window.layout.WindowLayoutInfo ) {
112- val displayMetrics = context.resources.displayMetrics
113+ val effectiveContext = windowContext ? : context
114+ val displayMetrics = effectiveContext.resources.displayMetrics
113115 val widthPx = displayMetrics.widthPixels
114116 val heightPx = displayMetrics.heightPixels
115117 val density = displayMetrics.density
@@ -152,12 +154,13 @@ class PostureDetector(private val context: Context, private val scope: Coroutine
152154 screenWidthPx = widthPx,
153155 screenHeightPx = heightPx,
154156 isTablet = isTablet,
155- orientation = context .resources.configuration.orientation
157+ orientation = effectiveContext .resources.configuration.orientation
156158 )
157159 }
158160
159161 private fun getCurrentPostureInfo (): PostureInfo {
160- val displayMetrics = context.resources.displayMetrics
162+ val effectiveContext = windowContext ? : context
163+ val displayMetrics = effectiveContext.resources.displayMetrics
161164 val widthPx = displayMetrics.widthPixels
162165 val heightPx = displayMetrics.heightPixels
163166 val density = displayMetrics.density
@@ -182,7 +185,7 @@ class PostureDetector(private val context: Context, private val scope: Coroutine
182185 screenWidthPx = widthPx,
183186 screenHeightPx = heightPx,
184187 isTablet = isTablet,
185- orientation = context .resources.configuration.orientation
188+ orientation = effectiveContext .resources.configuration.orientation
186189 )
187190 }
188191
0 commit comments