Add the key event source, vendorId, and productId from Android (flutter/engine#9186)

This adds the key event source from Android so that the framework can differentiate between keyboard events and game controller events. Also added vendor and product ids so that Flutter code can decide to do special processing based on the kind of device, if desired.
This commit is contained in:
Greg Spencer
2019-06-04 16:46:23 -07:00
committed by GitHub
parent 15e1a974be
commit 722d833715

View File

@@ -4,8 +4,10 @@
package io.flutter.embedding.engine.systemchannels;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.InputDevice;
import android.view.KeyEvent;
import java.util.HashMap;
@@ -55,12 +57,16 @@ public class KeyEventChannel {
if (event.complexCharacter != null) {
message.put("character", event.complexCharacter.toString());
}
message.put("source", event.source);
message.put("vendorId", event.vendorId);
message.put("productId", event.productId);
}
/**
* Key event as defined by Flutter.
*/
public static class FlutterKeyEvent {
public final int deviceId;
public final int flags;
public final int plainCodePoint;
public final int codePoint;
@@ -69,6 +75,9 @@ public class KeyEventChannel {
public final Character complexCharacter;
public final int scanCode;
public final int metaState;
public final int source;
public final int vendorId;
public final int productId;
public FlutterKeyEvent(
@NonNull KeyEvent androidKeyEvent
@@ -81,25 +90,30 @@ public class KeyEventChannel {
@Nullable Character complexCharacter
) {
this(
androidKeyEvent.getDeviceId(),
androidKeyEvent.getFlags(),
androidKeyEvent.getUnicodeChar(0x0),
androidKeyEvent.getUnicodeChar(),
androidKeyEvent.getKeyCode(),
complexCharacter,
androidKeyEvent.getScanCode(),
androidKeyEvent.getMetaState()
androidKeyEvent.getMetaState(),
androidKeyEvent.getSource()
);
}
public FlutterKeyEvent(
int deviceId,
int flags,
int plainCodePoint,
int codePoint,
int keyCode,
@Nullable Character complexCharacter,
int scanCode,
int metaState
int metaState,
int source
) {
this.deviceId = deviceId;
this.flags = flags;
this.plainCodePoint = plainCodePoint;
this.codePoint = codePoint;
@@ -107,6 +121,20 @@ public class KeyEventChannel {
this.complexCharacter = complexCharacter;
this.scanCode = scanCode;
this.metaState = metaState;
this.source = source;
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;
}
}
}
}