diff --git a/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java b/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java index 6bc4309c59a1d..33988c83aaf0c 100644 --- a/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java +++ b/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java @@ -85,27 +85,27 @@ public void destroy() { * @return true if the key event should not be propagated to other Android components. Delayed * synthesis events will return false, so that other components may handle them. */ - public boolean onKeyEvent(@NonNull KeyEvent event) { - int action = event.getAction(); - if (action != event.ACTION_DOWN && action != event.ACTION_UP) { + public boolean onKeyEvent(@NonNull KeyEvent keyEvent) { + int action = keyEvent.getAction(); + if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_UP) { // There is theoretically a KeyEvent.ACTION_MULTIPLE, but theoretically // that isn't sent by Android anymore, so this is just for protection in // case the theory is wrong. return false; } - if (eventResponder.isHeadEvent(event)) { - // If the event is at the head of the queue of pending events we've seen, - // and has the same id, then we know that this is a re-dispatched event, and + if (eventResponder.isHeadEvent(keyEvent)) { + // If the keyEvent is at the head of the queue of pending events we've seen, + // and has the same id, then we know that this is a re-dispatched keyEvent, and // we shouldn't respond to it, but we should remove it from tracking now. eventResponder.removeHeadEvent(); return false; } - Character complexCharacter = applyCombiningCharacterToBaseCharacter(event.getUnicodeChar()); + Character complexCharacter = applyCombiningCharacterToBaseCharacter(keyEvent.getUnicodeChar()); KeyEventChannel.FlutterKeyEvent flutterEvent = - new KeyEventChannel.FlutterKeyEvent(event, complexCharacter); + new KeyEventChannel.FlutterKeyEvent(keyEvent, complexCharacter); - eventResponder.addEvent(event); + eventResponder.addEvent(keyEvent); if (action == KeyEvent.ACTION_DOWN) { keyEventChannel.keyDown(flutterEvent); } else { diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/KeyEventChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/KeyEventChannel.java index efcfb4e938b4d..dbd6bf7f9c924 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/KeyEventChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/KeyEventChannel.java @@ -117,108 +117,34 @@ public void keyDown(@NonNull FlutterKeyEvent keyEvent) { } private void encodeKeyEvent( - @NonNull FlutterKeyEvent event, @NonNull Map message) { - message.put("flags", event.flags); - message.put("plainCodePoint", event.plainCodePoint); - message.put("codePoint", event.codePoint); - message.put("keyCode", event.keyCode); - message.put("scanCode", event.scanCode); - message.put("metaState", event.metaState); - if (event.complexCharacter != null) { - message.put("character", event.complexCharacter.toString()); + @NonNull FlutterKeyEvent keyEvent, @NonNull Map message) { + message.put("flags", keyEvent.event.getFlags()); + message.put("plainCodePoint", keyEvent.event.getUnicodeChar(0x0)); + message.put("codePoint", keyEvent.event.getUnicodeChar()); + message.put("keyCode", keyEvent.event.getKeyCode()); + message.put("scanCode", keyEvent.event.getScanCode()); + message.put("metaState", keyEvent.event.getMetaState()); + if (keyEvent.complexCharacter != null) { + message.put("character", keyEvent.complexCharacter.toString()); } - message.put("source", event.source); - message.put("vendorId", event.vendorId); - message.put("productId", event.productId); - message.put("deviceId", event.deviceId); - message.put("repeatCount", event.repeatCount); + message.put("source", keyEvent.event.getSource()); + InputDevice device = InputDevice.getDevice(keyEvent.event.getDeviceId()); + int vendorId = 0; + int productId = 0; + if (device != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + vendorId = device.getVendorId(); + productId = device.getProductId(); + } + } + message.put("vendorId", vendorId); + message.put("productId", productId); + message.put("deviceId", keyEvent.event.getDeviceId()); + message.put("repeatCount", keyEvent.event.getRepeatCount()); } /** A key event as defined by Flutter. */ public static class FlutterKeyEvent { - /** - * The id for the device this event came from. - * - * @see KeyEvent.getDeviceId() - */ - public final int deviceId; - /** - * The flags for this key event. - * - * @see KeyEvent.getFlags() - */ - public final int flags; - /** - * The code point for the Unicode character produced by this event if no meta keys were pressed - * (by passing 0 to {@code KeyEvent.getUnicodeChar(int)}). - * - * @see KeyEvent.getUnicodeChar(int) - */ - public final int plainCodePoint; - /** - * The code point for the Unicode character produced by this event, taking into account the meta - * keys currently pressed. - * - * @see KeyEvent.getUnicodeChar() - */ - public final int codePoint; - /** - * The Android key code for this event. - * - * @see KeyEvent.getKeyCode() - */ - public final int keyCode; - /** - * The character produced by this event, including any combining characters pressed before it. - */ - @Nullable public final Character complexCharacter; - /** - * The Android scan code for the key pressed. - * - * @see KeyEvent.getScanCode() - */ - public final int scanCode; - /** - * The meta key state for the Android key event. - * - * @see KeyEvent.getMetaState() - */ - public final int metaState; - /** - * The source of the key event. - * - * @see KeyEvent.getSource() - */ - public final int source; - /** - * The vendorId of the device that produced this key event. - * - * @see InputDevice.getVendorId() - */ - public final int vendorId; - /** - * The productId of the device that produced this key event. - * - * @see InputDevice.getProductId() - */ - public final int productId; - /** - * The repeat count for this event. - * - * @see KeyEvent.getRepeatCount() - */ - public final int repeatCount; /** * The Android key event that this Flutter key event was created from. * @@ -226,6 +152,10 @@ public static class FlutterKeyEvent { * framework. */ public final KeyEvent event; + /** + * The character produced by this event, including any combining characters pressed before it. + */ + @Nullable public final Character complexCharacter; public FlutterKeyEvent(@NonNull KeyEvent androidKeyEvent) { this(androidKeyEvent, null); @@ -233,56 +163,8 @@ public FlutterKeyEvent(@NonNull KeyEvent androidKeyEvent) { public FlutterKeyEvent( @NonNull KeyEvent androidKeyEvent, @Nullable Character complexCharacter) { - this( - androidKeyEvent.getDeviceId(), - androidKeyEvent.getFlags(), - androidKeyEvent.getUnicodeChar(0x0), - androidKeyEvent.getUnicodeChar(), - androidKeyEvent.getKeyCode(), - complexCharacter, - androidKeyEvent.getScanCode(), - androidKeyEvent.getMetaState(), - androidKeyEvent.getSource(), - androidKeyEvent.getRepeatCount(), - androidKeyEvent); - } - - public FlutterKeyEvent( - int deviceId, - int flags, - int plainCodePoint, - int codePoint, - int keyCode, - @Nullable Character complexCharacter, - int scanCode, - int metaState, - int source, - int repeatCount, - KeyEvent event) { - this.deviceId = deviceId; - this.flags = flags; - this.plainCodePoint = plainCodePoint; - this.codePoint = codePoint; - this.keyCode = keyCode; + this.event = androidKeyEvent; this.complexCharacter = complexCharacter; - this.scanCode = scanCode; - this.metaState = metaState; - this.source = source; - this.repeatCount = repeatCount; - this.event = event; - InputDevice device = InputDevice.getDevice(deviceId); - if (device != null) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - this.vendorId = device.getVendorId(); - this.productId = device.getProductId(); - } else { - this.vendorId = 0; - this.productId = 0; - } - } else { - this.vendorId = 0; - this.productId = 0; - } } } }