Skip to content

Commit 4be988e

Browse files
authored
[MOBILE-3656] Fix adjustResize when ignoring safe areas (#1243)
* Fix adjustResize when ignoring safe areas * spots
1 parent fa09d3f commit 4be988e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

urbanairship-layout/src/main/java/com/urbanairship/android/layout/ui/ModalActivity.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.urbanairship.android.layout.property.ModalPlacement
2828
import com.urbanairship.android.layout.property.Orientation
2929
import com.urbanairship.android.layout.reporting.DisplayTimer
3030
import com.urbanairship.android.layout.reporting.LayoutData
31+
import com.urbanairship.android.layout.util.FullScreenAdjustResizeWorkaround.Companion.applyAdjustResizeWorkaround
3132
import com.urbanairship.android.layout.util.parcelableExtra
3233
import com.urbanairship.android.layout.view.ModalView
3334
import kotlinx.coroutines.flow.Flow
@@ -115,6 +116,13 @@ public class ModalActivity : AppCompatActivity() {
115116
}
116117

117118
setContentView(view)
119+
120+
if (placement.shouldIgnoreSafeArea()) {
121+
// Apply workaround for adjustResize not working with fullscreen activities,
122+
// so that the keyboard does not cover the modal when we're ignoring safe areas.
123+
// ref: https://issuetracker.google.com/issues/36911528
124+
applyAdjustResizeWorkaround()
125+
}
118126
} catch (e: LoadException) {
119127
Logger.error(e, "Failed to load model!")
120128
finish()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.urbanairship.android.layout.util
2+
3+
import android.app.Activity
4+
import android.graphics.Rect
5+
import android.view.View
6+
import android.widget.FrameLayout
7+
8+
/**
9+
* Allows a view to be resized when the keyboard is shown, even when the activity is in fullscreen.
10+
*
11+
* Workaround for this "won't fix" bug: https://issuetracker.google.com/issues/36911528
12+
*/
13+
internal class FullScreenAdjustResizeWorkaround private constructor(activity: Activity) {
14+
15+
private val child: View
16+
private var previousUsableHeight: Int = 0
17+
private val layoutParams: FrameLayout.LayoutParams
18+
19+
init {
20+
val content = activity.findViewById<FrameLayout>(android.R.id.content)
21+
child = content.getChildAt(0)
22+
child.viewTreeObserver.addOnGlobalLayoutListener {
23+
adjustResizeIfNeeded()
24+
}
25+
layoutParams = child.layoutParams as FrameLayout.LayoutParams
26+
}
27+
28+
private fun adjustResizeIfNeeded() {
29+
val usableHeight = getUsableHeight()
30+
if (usableHeight != previousUsableHeight) {
31+
val usableHeightMinusKeyboard = child.rootView.height
32+
val heightDelta = usableHeightMinusKeyboard - usableHeight
33+
if (heightDelta > (usableHeightMinusKeyboard / 4)) {
34+
// keyboard probably just became visible
35+
layoutParams.height = usableHeightMinusKeyboard - heightDelta
36+
} else {
37+
// keyboard probably just dismissed
38+
layoutParams.height = usableHeightMinusKeyboard
39+
}
40+
child.requestLayout()
41+
previousUsableHeight = usableHeight
42+
}
43+
}
44+
45+
private fun getUsableHeight(): Int {
46+
val rect = Rect().apply { child.getWindowVisibleDisplayFrame(this) }
47+
return rect.bottom - rect.top
48+
}
49+
50+
internal companion object {
51+
fun Activity.applyAdjustResizeWorkaround() {
52+
FullScreenAdjustResizeWorkaround(this)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)