From b20d9e010b96efa1ea8a30482871e92c102a4bd7 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Fri, 1 Mar 2024 12:17:10 +0100 Subject: [PATCH] [useAnimatedKeyboard][Android] Fix navigation bar with auto hide (#5742) ## Summary Split of https://github.com/software-mansion/react-native-reanimated/pull/5666 It ensures proper respect for auto-hiding navigation bars on Android devices. --- .../java/com/swmansion/reanimated/keyboard/Keyboard.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/swmansion/reanimated/keyboard/Keyboard.java b/android/src/main/java/com/swmansion/reanimated/keyboard/Keyboard.java index c3cbe207fae..0f8ae78071e 100644 --- a/android/src/main/java/com/swmansion/reanimated/keyboard/Keyboard.java +++ b/android/src/main/java/com/swmansion/reanimated/keyboard/Keyboard.java @@ -1,5 +1,7 @@ package com.swmansion.reanimated.keyboard; +import android.view.KeyCharacterMap; +import android.view.KeyEvent; import androidx.core.view.WindowInsetsCompat; import com.facebook.react.uimanager.PixelUtil; @@ -21,8 +23,11 @@ public int getHeight() { public void updateHeight(WindowInsetsCompat insets) { int contentBottomInset = insets.getInsets(CONTENT_TYPE_MASK).bottom; int systemBarBottomInset = insets.getInsets(SYSTEM_BAR_TYPE_MASK).bottom; - int keyboardHeightDip = Math.max(0, contentBottomInset - systemBarBottomInset); - if (keyboardHeightDip == 0 && mState == KeyboardState.OPEN) { + boolean hasNavigationBar = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME); + int keyboardHeightDip = + hasNavigationBar ? contentBottomInset - systemBarBottomInset : contentBottomInset; + int keyboardHeight = (int) PixelUtil.toDIPFromPixel(Math.max(0, keyboardHeightDip)); + if (keyboardHeight == 0 && mState == KeyboardState.OPEN) { /* When the keyboard is being canceling, for one frame the insets show a keyboard height of 0, causing a jump of the keyboard. We can avoid it by ignoring that frame and calling