From 053555a70f45a50d57a972c97ed6ec338b861151 Mon Sep 17 00:00:00 2001 From: Vaibhav Devmurari Date: Tue, 16 Aug 2022 15:45:47 +0000 Subject: [PATCH] Save country code information in InputDevice Country code information is passed by a HID device over sysfs. We can read that information and save it in InputDevice which will be later used for auto-detecting Layout for PK. More information in DD: go/pk_auto_layout_detection Test: atest InputDeviceTest Bug: 242715614 Change-Id: I97bde1f2338ad601a7e2636600038e6ca124e3ff --- core/java/android/view/InputDevice.java | 26 ++++++++++++++++--- core/jni/android_view_InputDevice.cpp | 8 +++--- .../input/InputDeviceLightsManagerTest.java | 5 ++-- .../input/InputDeviceSensorManagerTest.java | 4 +-- .../virtual/InputManagerMockHelper.java | 3 ++- .../vibrator/InputDeviceDelegateTest.java | 4 ++- .../vibrator/VibratorManagerServiceTest.java | 4 ++- .../android/test/input/InputDeviceTest.java | 14 +++++----- 8 files changed, 47 insertions(+), 21 deletions(-) diff --git a/core/java/android/view/InputDevice.java b/core/java/android/view/InputDevice.java index addbab07c4789..2a0246b200eb3 100644 --- a/core/java/android/view/InputDevice.java +++ b/core/java/android/view/InputDevice.java @@ -25,6 +25,7 @@ import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; import android.hardware.BatteryState; import android.hardware.SensorManager; +import android.hardware.input.InputDeviceCountryCode; import android.hardware.input.InputDeviceIdentifier; import android.hardware.input.InputManager; import android.hardware.lights.LightsManager; @@ -72,6 +73,8 @@ public final class InputDevice implements Parcelable { private final int mSources; private final int mKeyboardType; private final KeyCharacterMap mKeyCharacterMap; + @InputDeviceCountryCode + private final int mCountryCode; private final boolean mHasVibrator; private final boolean mHasMicrophone; private final boolean mHasButtonUnderPad; @@ -462,8 +465,9 @@ public final class InputDevice implements Parcelable { @VisibleForTesting public InputDevice(int id, int generation, int controllerNumber, String name, int vendorId, int productId, String descriptor, boolean isExternal, int sources, int keyboardType, - KeyCharacterMap keyCharacterMap, boolean hasVibrator, boolean hasMicrophone, - boolean hasButtonUnderPad, boolean hasSensor, boolean hasBattery) { + KeyCharacterMap keyCharacterMap, @InputDeviceCountryCode int countryCode, + boolean hasVibrator, boolean hasMicrophone, boolean hasButtonUnderPad, + boolean hasSensor, boolean hasBattery) { mId = id; mGeneration = generation; mControllerNumber = controllerNumber; @@ -475,6 +479,7 @@ public final class InputDevice implements Parcelable { mSources = sources; mKeyboardType = keyboardType; mKeyCharacterMap = keyCharacterMap; + mCountryCode = countryCode; mHasVibrator = hasVibrator; mHasMicrophone = hasMicrophone; mHasButtonUnderPad = hasButtonUnderPad; @@ -495,6 +500,7 @@ public final class InputDevice implements Parcelable { mIsExternal = in.readInt() != 0; mSources = in.readInt(); mKeyboardType = in.readInt(); + mCountryCode = in.readInt(); mHasVibrator = in.readInt() != 0; mHasMicrophone = in.readInt() != 0; mHasButtonUnderPad = in.readInt() != 0; @@ -728,6 +734,16 @@ public final class InputDevice implements Parcelable { return mKeyCharacterMap; } + /** + * Gets Country code associated with the device + * + * @hide + */ + @InputDeviceCountryCode + public int getCountryCode() { + return mCountryCode; + } + /** * Gets whether the device is capable of producing the list of keycodes. * @param keys The list of android keycodes to check for. @@ -1147,6 +1163,7 @@ public final class InputDevice implements Parcelable { out.writeInt(mIsExternal ? 1 : 0); out.writeInt(mSources); out.writeInt(mKeyboardType); + out.writeInt(mCountryCode); out.writeInt(mHasVibrator ? 1 : 0); out.writeInt(mHasMicrophone ? 1 : 0); out.writeInt(mHasButtonUnderPad ? 1 : 0); @@ -1178,7 +1195,8 @@ public final class InputDevice implements Parcelable { description.append("Input Device ").append(mId).append(": ").append(mName).append("\n"); description.append(" Descriptor: ").append(mDescriptor).append("\n"); description.append(" Generation: ").append(mGeneration).append("\n"); - description.append(" Location: ").append(mIsExternal ? "external" : "built-in").append("\n"); + description.append(" Location: ").append(mIsExternal ? "external" : "built-in").append( + "\n"); description.append(" Keyboard Type: "); switch (mKeyboardType) { @@ -1194,6 +1212,8 @@ public final class InputDevice implements Parcelable { } description.append("\n"); + description.append(" Country Code: ").append(mCountryCode).append("\n"); + description.append(" Has Vibrator: ").append(mHasVibrator).append("\n"); description.append(" Has Sensor: ").append(mHasSensor).append("\n"); diff --git a/core/jni/android_view_InputDevice.cpp b/core/jni/android_view_InputDevice.cpp index 9cc72437a0234..aece8c35a9e6a 100644 --- a/core/jni/android_view_InputDevice.cpp +++ b/core/jni/android_view_InputDevice.cpp @@ -69,9 +69,9 @@ jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& devi static_cast(ident.product), descriptorObj.get(), deviceInfo.isExternal(), deviceInfo.getSources(), deviceInfo.getKeyboardType(), kcmObj.get(), - deviceInfo.hasVibrator(), hasMic, - deviceInfo.hasButtonUnderPad(), deviceInfo.hasSensor(), - deviceInfo.hasBattery())); + deviceInfo.getCountryCode(), deviceInfo.hasVibrator(), + hasMic, deviceInfo.hasButtonUnderPad(), + deviceInfo.hasSensor(), deviceInfo.hasBattery())); const std::vector& ranges = deviceInfo.getMotionRanges(); for (const InputDeviceInfo::MotionRange& range: ranges) { @@ -94,7 +94,7 @@ int register_android_view_InputDevice(JNIEnv* env) gInputDeviceClassInfo.ctor = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "", "(IIILjava/lang/String;IILjava/lang/" - "String;ZIILandroid/view/KeyCharacterMap;ZZZZZ)V"); + "String;ZIILandroid/view/KeyCharacterMap;IZZZZZ)V"); gInputDeviceClassInfo.addMotionRange = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "addMotionRange", "(IIFFFFF)V"); diff --git a/core/tests/coretests/src/android/hardware/input/InputDeviceLightsManagerTest.java b/core/tests/coretests/src/android/hardware/input/InputDeviceLightsManagerTest.java index 3e1db364cd821..e13a3323d31ac 100644 --- a/core/tests/coretests/src/android/hardware/input/InputDeviceLightsManagerTest.java +++ b/core/tests/coretests/src/android/hardware/input/InputDeviceLightsManagerTest.java @@ -23,7 +23,6 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertNotNull; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; @@ -114,8 +113,8 @@ public class InputDeviceLightsManagerTest { return new InputDevice(id, 0 /* generation */, 0 /* controllerNumber */, "name", 0 /* vendorId */, 0 /* productId */, "descriptor", true /* isExternal */, 0 /* sources */, 0 /* keyboardType */, null /* keyCharacterMap */, - false /* hasVibrator */, false /* hasMicrophone */, false /* hasButtonUnderpad */, - false /* hasSensor */, false /* hasBattery */); + InputDeviceCountryCode.INVALID, false /* hasVibrator */, false /* hasMicrophone */, + false /* hasButtonUnderpad */, false /* hasSensor */, false /* hasBattery */); } private void mockLights(Light[] lights) throws Exception { diff --git a/core/tests/coretests/src/android/hardware/input/InputDeviceSensorManagerTest.java b/core/tests/coretests/src/android/hardware/input/InputDeviceSensorManagerTest.java index 341ee37aee391..54ee4349c3e1f 100644 --- a/core/tests/coretests/src/android/hardware/input/InputDeviceSensorManagerTest.java +++ b/core/tests/coretests/src/android/hardware/input/InputDeviceSensorManagerTest.java @@ -148,8 +148,8 @@ public class InputDeviceSensorManagerTest { InputDevice d = new InputDevice(id, 0 /* generation */, 0 /* controllerNumber */, "name", 0 /* vendorId */, 0 /* productId */, "descriptor", true /* isExternal */, 0 /* sources */, 0 /* keyboardType */, null /* keyCharacterMap */, - false /* hasVibrator */, false /* hasMicrophone */, false /* hasButtonUnderpad */, - true /* hasSensor */, false /* hasBattery */); + InputDeviceCountryCode.INVALID, false /* hasVibrator */, false /* hasMicrophone */, + false /* hasButtonUnderpad */, true /* hasSensor */, false /* hasBattery */); assertTrue(d.hasSensor()); return d; } diff --git a/services/tests/servicestests/src/com/android/server/companion/virtual/InputManagerMockHelper.java b/services/tests/servicestests/src/com/android/server/companion/virtual/InputManagerMockHelper.java index 5a6d2d398f7de..b7f564094cde7 100644 --- a/services/tests/servicestests/src/com/android/server/companion/virtual/InputManagerMockHelper.java +++ b/services/tests/servicestests/src/com/android/server/companion/virtual/InputManagerMockHelper.java @@ -25,6 +25,7 @@ import static org.mockito.Mockito.when; import android.hardware.input.IInputDevicesChangedListener; import android.hardware.input.IInputManager; +import android.hardware.input.InputDeviceCountryCode; import android.hardware.input.InputManager; import android.os.RemoteException; import android.testing.TestableLooper; @@ -84,7 +85,7 @@ class InputManagerMockHelper { final InputDevice device = new InputDevice(mDevices.size() /*id*/, 1 /*generation*/, 0, inv.getArgument(0) /*name*/, inv.getArgument(1) /*vendorId*/, inv.getArgument(2) /*productId*/, inv.getArgument(3) /*descriptor*/, true, 0, 0, - null, false, false, false, false, false); + null, InputDeviceCountryCode.INVALID, false, false, false, false, false); mDevices.add(device); try { mDevicesChangedListener.onInputDevicesChanged( diff --git a/services/tests/servicestests/src/com/android/server/vibrator/InputDeviceDelegateTest.java b/services/tests/servicestests/src/com/android/server/vibrator/InputDeviceDelegateTest.java index 3f9caa95df4ab..ec4ad8965f7d4 100644 --- a/services/tests/servicestests/src/com/android/server/vibrator/InputDeviceDelegateTest.java +++ b/services/tests/servicestests/src/com/android/server/vibrator/InputDeviceDelegateTest.java @@ -33,6 +33,7 @@ import android.content.Context; import android.content.ContextWrapper; import android.hardware.input.IInputDevicesChangedListener; import android.hardware.input.IInputManager; +import android.hardware.input.InputDeviceCountryCode; import android.hardware.input.InputManager; import android.os.CombinedVibration; import android.os.Handler; @@ -328,7 +329,8 @@ public class InputDeviceDelegateTest { private InputDevice createInputDevice(int id, boolean hasVibrator) { return new InputDevice(id, 0, 0, "name", 0, 0, "description", false, 0, 0, - null, hasVibrator, false, false, false /* hasSensor */, false /* hasBattery */); + null, InputDeviceCountryCode.INVALID, hasVibrator, false, false, + false /* hasSensor */, false /* hasBattery */); } diff --git a/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java index b8e1612049f06..1a8df719c2230 100644 --- a/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java @@ -46,6 +46,7 @@ import android.content.Context; import android.content.ContextWrapper; import android.content.pm.PackageManagerInternal; import android.hardware.input.IInputManager; +import android.hardware.input.InputDeviceCountryCode; import android.hardware.input.InputManager; import android.hardware.vibrator.IVibrator; import android.hardware.vibrator.IVibratorManager; @@ -1814,7 +1815,8 @@ public class VibratorManagerServiceTest { private InputDevice createInputDeviceWithVibrator(int id) { return new InputDevice(id, 0, 0, "name", 0, 0, "description", false, 0, 0, - null, /* hasVibrator= */ true, false, false, false, false); + null, InputDeviceCountryCode.INVALID, /* hasVibrator= */ true, false, false, false, + false); } private static void addLocalServiceMock(Class clazz, T mock) { diff --git a/tests/Input/src/com/android/test/input/InputDeviceTest.java b/tests/Input/src/com/android/test/input/InputDeviceTest.java index 63500774816a3..836d406d903cd 100644 --- a/tests/Input/src/com/android/test/input/InputDeviceTest.java +++ b/tests/Input/src/com/android/test/input/InputDeviceTest.java @@ -17,8 +17,8 @@ package android.view; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import android.hardware.input.InputDeviceCountryCode; import android.os.Parcel; import androidx.test.ext.junit.runners.AndroidJUnit4; @@ -55,11 +55,12 @@ public class InputDeviceTest { assertEquals(device.isExternal(), outDevice.isExternal()); assertEquals(device.getSources(), outDevice.getSources()); assertEquals(device.getKeyboardType(), outDevice.getKeyboardType()); + assertEquals(device.getCountryCode(), outDevice.getCountryCode()); assertEquals(device.getMotionRanges().size(), outDevice.getMotionRanges().size()); KeyCharacterMap keyCharacterMap = device.getKeyCharacterMap(); KeyCharacterMap outKeyCharacterMap = outDevice.getKeyCharacterMap(); - assertTrue("keyCharacterMap not equal", keyCharacterMap.equals(outKeyCharacterMap)); + assertEquals("keyCharacterMap not equal", keyCharacterMap, outKeyCharacterMap); for (int j = 0; j < device.getMotionRanges().size(); j++) { assertMotionRangeEquals(device.getMotionRanges().get(j), @@ -70,10 +71,11 @@ public class InputDeviceTest { private void assertInputDeviceParcelUnparcel(KeyCharacterMap keyCharacterMap) { final InputDevice device = new InputDevice(DEVICE_ID, 0 /* generation */, 0 /* controllerNumber */, "name", - 0 /* vendorId */, 0 /* productId */, "descriptor", true /* isExternal */, - 0 /* sources */, 0 /* keyboardType */, keyCharacterMap, - false /* hasVibrator */, false /* hasMicrophone */, false /* hasButtonUnderpad */, - true /* hasSensor */, false /* hasBattery */); + 0 /* vendorId */, 0 /* productId */, "descriptor", true /* isExternal */, + 0 /* sources */, 0 /* keyboardType */, keyCharacterMap, + InputDeviceCountryCode.INTERNATIONAL, false /* hasVibrator */, + false /* hasMicrophone */, false /* hasButtonUnderpad */, + true /* hasSensor */, false /* hasBattery */); Parcel parcel = Parcel.obtain(); device.writeToParcel(parcel, 0);