Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[useAnimatedKeyboard][Android] Fix some keyboard issues #5666

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.swmansion.reanimated.keyboard;

import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import androidx.core.view.WindowInsetsCompat;
import com.facebook.react.uimanager.PixelUtil;

public class Keyboard {
private KeyboardState state;
private int height = 0;
private int activeTransitionCounter = 0;
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
private final int contentTypeMask = WindowInsetsCompat.Type.ime();
private final int systemBarTypeMask = WindowInsetsCompat.Type.systemBars();

Expand All @@ -20,15 +23,30 @@ public int getHeight() {
public void updateHeight(WindowInsetsCompat insets) {
int contentBottomInset = insets.getInsets(contentTypeMask).bottom;
int systemBarBottomInset = insets.getInsets(systemBarTypeMask).bottom;
int keyboardHeightDip = contentBottomInset - systemBarBottomInset;
height = (int) PixelUtil.toDIPFromPixel(Math.max(0, keyboardHeightDip));
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 && state == KeyboardState.OPEN) {
return;
}
height = keyboardHeight;
}

public void onAnimationStart() {
state = height == 0 ? KeyboardState.OPENING : KeyboardState.CLOSING;
if (activeTransitionCounter > 0) {
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
boolean isOpening = state == KeyboardState.OPENING;
state = isOpening ? KeyboardState.CLOSING : KeyboardState.OPENING;
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
} else {
state = height == 0 ? KeyboardState.OPENING : KeyboardState.CLOSING;
}
activeTransitionCounter++;
}

public void onAnimationEnd() {
state = height == 0 ? KeyboardState.CLOSED : KeyboardState.OPEN;
activeTransitionCounter--;
if (activeTransitionCounter == 0) {
state = height == 0 ? KeyboardState.CLOSED : KeyboardState.OPEN;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public KeyboardAnimationCallback(
public WindowInsetsAnimationCompat.BoundsCompat onStart(
@NonNull WindowInsetsAnimationCompat animation,
@NonNull WindowInsetsAnimationCompat.BoundsCompat bounds) {
if (!isKeyboardAnimation(animation)) {
return bounds;
}
keyboard.onAnimationStart();
notifyAboutKeyboardChange.call();
return super.onStart(animation, bounds);
Expand All @@ -31,14 +34,31 @@ public WindowInsetsAnimationCompat.BoundsCompat onStart(
public WindowInsetsCompat onProgress(
@NonNull WindowInsetsCompat insets,
@NonNull List<WindowInsetsAnimationCompat> runningAnimations) {
boolean isAnyKeyboardAnimation = false;
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
for (WindowInsetsAnimationCompat animation : runningAnimations) {
if (isKeyboardAnimation(animation)) {
isAnyKeyboardAnimation = true;
break;
}
}
if (!isAnyKeyboardAnimation) {
return insets;
}
keyboard.updateHeight(insets);
notifyAboutKeyboardChange.call();
return insets;
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void onEnd(@NonNull WindowInsetsAnimationCompat animation) {
if (!isKeyboardAnimation(animation)) {
return;
}
keyboard.onAnimationEnd();
notifyAboutKeyboardChange.call();
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean isKeyboardAnimation(@NonNull WindowInsetsAnimationCompat animation) {
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
return (animation.getTypeMask() & WindowInsetsCompat.Type.ime()) != 0;
}
}
Loading