mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
compute correct Keyboard Height with Notch (#30919)
Summary: fixes https://github.com/facebook/react-native/issues/27089 fixes https://github.com/facebook/react-native/issues/30191 fixes https://github.com/facebook/react-native/issues/26296 fixes https://github.com/facebook/react-native/issues/24353 Related https://github.com/facebook/react-native/issues/30052 https://github.com/facebook/react-native/issues/28004 https://github.com/facebook/react-native/issues/26536 The keyboard height of event keyboardDidShow is computed as the difference of two variables: - The screen height excluding the Android Notch DisplayMetricsHolder.getWindowDisplayMetrics().heightPixels returns the screen height excluding the Android Notch - The Visible Area excluding the Keyboard, but including the Android Notch getWindowVisibleDisplayFrame() which returns the visible area including the Android Notch The computation of the keyboard height is wrong when the device has an Android Notch. This pr adds the Android Notch computation for API levels 28+ More info at https://github.com/facebook/react-native/issues/27089#issuecomment-775821333 ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [Android] [Fixed] - Compute Android Notch in keyboardDidShow height calculation API 28+ Pull Request resolved: https://github.com/facebook/react-native/pull/30919 Test Plan: adding a ReactRootViewTest for keyboardDidShow verifying correct functionality on API < 28 **<details><summary>TEST CASE - BEFORE FIX</summary>** <p> **WITHOUT NOTCH** - The black view on the bottom is visible - The keyboard height is 282 | **Full Screen** | **Keyboard Did Show** | |:-------------------------:|:-------------------------:| | <img src="https://user-images.githubusercontent.com/24992535/107212700-a1fd9d00-6a07-11eb-9248-26f9c4d92ae3.png" width="300" height="" /> | <img src="https://user-images.githubusercontent.com/24992535/107212590-7975a300-6a07-11eb-89f4-891a37a7c406.png" width="300" height="" /> | **WITH NOTCH** - The black view on the bottom is **not** visible. The black view is not visible because keyboardDidHide is sending the wrong keyboard height value. - The keyboard height changes to 234. The keyboard height is the same from the previous test, but the value sent from keyboardDidHide changed for the Notch. | **Full Screen** | **Keyboard Did Show** | |:-------------------------:|:-------------------------:| | <img src="https://user-images.githubusercontent.com/24992535/107212619-81cdde00-6a07-11eb-9630-7e7c8c34d798.png" width="300" height="" /> | <img src="https://user-images.githubusercontent.com/24992535/107212707-a4f88d80-6a07-11eb-9134-f077059c83a6.png" width="300" height="" /> | </p> </details> **<details><summary>TEST CASE - AFTER FIX</summary>** <p> **WITH NOTCH** - The black view on the bottom is visible - The keyboard height is 282 | **Full Screen** | **Keyboard Did Show** | |:-------------------------:|:-------------------------:| | <img src="https://user-images.githubusercontent.com/24992535/107212619-81cdde00-6a07-11eb-9630-7e7c8c34d798.png" width="300" height="" /> | <img src="https://user-images.githubusercontent.com/24992535/107349053-0d0ea880-6ac8-11eb-9695-33128080b6b8.png" width="300" height="" /> | </p> </details> Reviewed By: ShikaSD Differential Revision: D31207989 Pulled By: cortinico fbshipit-source-id: 0955a3884201122166c5c0ae2aca988a0ed4af53
This commit is contained in:
committed by
Facebook GitHub Bot
parent
ea3e244668
commit
8bef3b1f11
@@ -16,8 +16,10 @@ import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.DisplayCutout;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.Surface;
|
||||
@@ -647,6 +649,11 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
|
||||
mJSTouchDispatcher = new JSTouchDispatcher(this);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
/* package */ void simulateCheckForKeyboardForTesting() {
|
||||
getCustomGlobalLayoutListener().checkForKeyboardEvents();
|
||||
}
|
||||
|
||||
private CustomGlobalLayoutListener getCustomGlobalLayoutListener() {
|
||||
if (mCustomGlobalLayoutListener == null) {
|
||||
mCustomGlobalLayoutListener = new CustomGlobalLayoutListener();
|
||||
@@ -766,8 +773,17 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
|
||||
|
||||
private void checkForKeyboardEvents() {
|
||||
getRootView().getWindowVisibleDisplayFrame(mVisibleViewArea);
|
||||
int notchHeight = 0;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
DisplayCutout displayCutout = getRootView().getRootWindowInsets().getDisplayCutout();
|
||||
if (displayCutout != null) {
|
||||
notchHeight = displayCutout.getSafeInsetTop();
|
||||
}
|
||||
}
|
||||
final int heightDiff =
|
||||
DisplayMetricsHolder.getWindowDisplayMetrics().heightPixels - mVisibleViewArea.bottom;
|
||||
DisplayMetricsHolder.getWindowDisplayMetrics().heightPixels
|
||||
- mVisibleViewArea.bottom
|
||||
+ notchHeight;
|
||||
|
||||
boolean isKeyboardShowingOrKeyboardHeightChanged =
|
||||
mKeyboardHeight != heightDiff && heightDiff > mMinKeyboardHeightDetected;
|
||||
|
||||
Reference in New Issue
Block a user