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 all commits
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 mState;
private int mHeight = 0;
private int mActiveTransitionCounter = 0;
private static final int CONTENT_TYPE_MASK = WindowInsetsCompat.Type.ime();
private static final int SYSTEM_BAR_TYPE_MASK = WindowInsetsCompat.Type.systemBars();

Expand All @@ -20,15 +23,34 @@ 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 = contentBottomInset - systemBarBottomInset;
mHeight = (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 && mState == KeyboardState.OPEN) {
/*
When the keyboard opening animation is canceled, 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 the listeners on the following frame.
*/
return;
}
mHeight = keyboardHeight;
}

public void onAnimationStart() {
mState = mHeight == 0 ? KeyboardState.OPENING : KeyboardState.CLOSING;
if (mActiveTransitionCounter > 0) {
mState = mState == KeyboardState.OPENING ? KeyboardState.CLOSING : KeyboardState.OPENING;
} else {
mState = mHeight == 0 ? KeyboardState.OPENING : KeyboardState.CLOSING;
}
mActiveTransitionCounter++;
}

public void onAnimationEnd() {
mState = mHeight == 0 ? KeyboardState.CLOSED : KeyboardState.OPEN;
mActiveTransitionCounter--;
if (mActiveTransitionCounter == 0) {
mState = mHeight == 0 ? KeyboardState.CLOSED : KeyboardState.OPEN;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class KeyboardAnimationCallback extends WindowInsetsAnimationCompat.Callback {
private final Keyboard mKeyboard;
private final NotifyAboutKeyboardChangeFunction mNotifyAboutKeyboardChange;
private static final int CONTENT_TYPE_MASK = WindowInsetsCompat.Type.ime();

public KeyboardAnimationCallback(
Keyboard keyboard, NotifyAboutKeyboardChangeFunction notifyAboutKeyboardChange) {
Expand All @@ -21,6 +22,9 @@ public KeyboardAnimationCallback(
public WindowInsetsAnimationCompat.BoundsCompat onStart(
@NonNull WindowInsetsAnimationCompat animation,
@NonNull WindowInsetsAnimationCompat.BoundsCompat bounds) {
if (!isKeyboardAnimation(animation)) {
return bounds;
}
mKeyboard.onAnimationStart();
mNotifyAboutKeyboardChange.call();
return super.onStart(animation, bounds);
Expand All @@ -31,14 +35,29 @@ public WindowInsetsAnimationCompat.BoundsCompat onStart(
public WindowInsetsCompat onProgress(
@NonNull WindowInsetsCompat insets,
@NonNull List<WindowInsetsAnimationCompat> runningAnimations) {
mKeyboard.updateHeight(insets);
mNotifyAboutKeyboardChange.call();
boolean isAnyKeyboardAnimationRunning = false;
for (WindowInsetsAnimationCompat animation : runningAnimations) {
if (isKeyboardAnimation(animation)) {
isAnyKeyboardAnimationRunning = true;
break;
}
}
if (isAnyKeyboardAnimationRunning) {
mKeyboard.updateHeight(insets);
mNotifyAboutKeyboardChange.call();
}
return insets;
}

@Override
public void onEnd(@NonNull WindowInsetsAnimationCompat animation) {
mKeyboard.onAnimationEnd();
mNotifyAboutKeyboardChange.call();
if (isKeyboardAnimation(animation)) {
mKeyboard.onAnimationEnd();
mNotifyAboutKeyboardChange.call();
}
}

private static boolean isKeyboardAnimation(@NonNull WindowInsetsAnimationCompat animation) {
return (animation.getTypeMask() & CONTENT_TYPE_MASK) != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public int subscribeForKeyboardUpdates(
if (mListeners.isEmpty()) {
KeyboardAnimationCallback keyboardAnimationCallback =
new KeyboardAnimationCallback(mKeyboard, this::notifyAboutKeyboardChange);
mWindowsInsetsManager.startObservingChanges(
keyboardAnimationCallback, isStatusBarTranslucent);
mWindowsInsetsManager.startObservingChanges(keyboardAnimationCallback, isStatusBarTranslucent);
}
mListeners.put(listenerId, callback);
return listenerId;
Expand Down
Loading