Skip to content

Commit

Permalink
fix(keyboard): Prevent null pointer if native listener not set (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Jan 28, 2022
1 parent fd059fc commit 8b6f119
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.getcapacitor.Logger;

public class Keyboard {

Expand Down Expand Up @@ -100,12 +101,16 @@ public void onGlobalLayout() {
int heightDiff = screenHeight - resultBottom;

int pixelHeightDiff = (int) (heightDiff / density);
if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
keyboardEventListener.onKeyboardEvent(EVENT_KB_WILL_SHOW, pixelHeightDiff);
keyboardEventListener.onKeyboardEvent(EVENT_KB_DID_SHOW, pixelHeightDiff);
} else if (pixelHeightDiff != previousHeightDiff && (previousHeightDiff - pixelHeightDiff) > 100) {
keyboardEventListener.onKeyboardEvent(EVENT_KB_WILL_HIDE, 0);
keyboardEventListener.onKeyboardEvent(EVENT_KB_DID_HIDE, 0);
if (keyboardEventListener != null) {
if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
keyboardEventListener.onKeyboardEvent(EVENT_KB_WILL_SHOW, pixelHeightDiff);
keyboardEventListener.onKeyboardEvent(EVENT_KB_DID_SHOW, pixelHeightDiff);
} else if (pixelHeightDiff != previousHeightDiff && (previousHeightDiff - pixelHeightDiff) > 100) {
keyboardEventListener.onKeyboardEvent(EVENT_KB_WILL_HIDE, 0);
keyboardEventListener.onKeyboardEvent(EVENT_KB_DID_HIDE, 0);
}
} else {
Logger.warn("Native Keyboard Event Listener not found");
}
previousHeightDiff = pixelHeightDiff;
}
Expand Down

0 comments on commit 8b6f119

Please sign in to comment.