Skip to content

Commit 2758488

Browse files
authored
Fix keyboard height using stale orientation on init (#744)
1 parent 68777c4 commit 2758488

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

app/src/main/java/com/urik/keyboard/service/PostureDetector.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.urik.keyboard.service
2+
3+
import android.content.res.Configuration
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.SupervisorJob
7+
import org.junit.Assert.assertEquals
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import org.robolectric.RobolectricTestRunner
11+
import org.robolectric.RuntimeEnvironment
12+
13+
@RunWith(RobolectricTestRunner::class)
14+
class PostureDetectorTest {
15+
@Test
16+
fun `attachToWindow syncs PostureInfo orientation from window context`() {
17+
val app = RuntimeEnvironment.getApplication()
18+
val scope = CoroutineScope(Dispatchers.Unconfined + SupervisorJob())
19+
20+
val landscapeConfig = Configuration(app.resources.configuration).apply {
21+
orientation = Configuration.ORIENTATION_LANDSCAPE
22+
}
23+
val serviceContext = app.createConfigurationContext(landscapeConfig)
24+
25+
val detector = PostureDetector(serviceContext, scope)
26+
detector.start()
27+
28+
assertEquals(
29+
"Initial PostureInfo should reflect the (stale) service context orientation",
30+
Configuration.ORIENTATION_LANDSCAPE,
31+
detector.postureInfo.value.orientation
32+
)
33+
34+
val portraitConfig = Configuration(app.resources.configuration).apply {
35+
orientation = Configuration.ORIENTATION_PORTRAIT
36+
}
37+
val windowContext = app.createConfigurationContext(portraitConfig)
38+
39+
detector.attachToWindow(windowContext)
40+
41+
assertEquals(
42+
"PostureInfo must update to window context orientation after attachToWindow",
43+
Configuration.ORIENTATION_PORTRAIT,
44+
detector.postureInfo.value.orientation
45+
)
46+
}
47+
}

0 commit comments

Comments
 (0)