|
| 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