InputMapper refactor: Configure empty InputDevice

Configure the Device prior to populating mappers for mappers to
receive correct property map

Test: m checkinput && atest libinput_tests inputflinger_tests
Bug: 256009910

Change-Id: I2a348029afa6c566506f1d79b655173bb8e7a8af
diff --git a/services/inputflinger/reader/InputDevice.cpp b/services/inputflinger/reader/InputDevice.cpp
index 0a64a1c..90ba003 100644
--- a/services/inputflinger/reader/InputDevice.cpp
+++ b/services/inputflinger/reader/InputDevice.cpp
@@ -151,21 +151,22 @@
         return;
     }
     std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
-    std::vector<std::unique_ptr<InputMapper>> mappers;
-
-    mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
+    mDevices.insert(
+            {eventHubId,
+             std::make_pair<std::unique_ptr<InputDeviceContext>,
+                            std::vector<std::unique_ptr<InputMapper>>>(std::move(contextPtr), {})});
 }
 
-void InputDevice::addEventHubDevice(int32_t eventHubId,
-                                    const InputReaderConfiguration& readerConfig) {
-    if (mDevices.find(eventHubId) != mDevices.end()) {
-        return;
-    }
-    std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
-    std::vector<std::unique_ptr<InputMapper>> mappers = createMappers(*contextPtr, readerConfig);
-
-    // insert the context into the devices set
-    mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
+void InputDevice::populateMappers(int32_t eventHubId,
+                                  const InputReaderConfiguration& readerConfig) {
+    auto targetDevice = mDevices.find(eventHubId);
+    LOG_ALWAYS_FATAL_IF(targetDevice == mDevices.end(),
+                        "InputDevice::populateMappers(): missing device with eventHubId %d ",
+                        eventHubId);
+    // create and add mappers to device
+    InputDeviceContext& context = *targetDevice->second.first;
+    std::vector<std::unique_ptr<InputMapper>> mappers = createMappers(context, readerConfig);
+    targetDevice->second.second = std::move(mappers);
     // Must change generation to flag this device as changed
     bumpGeneration();
 }
@@ -440,29 +441,29 @@
 }
 
 std::vector<std::unique_ptr<InputMapper>> InputDevice::createMappers(
-        InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig) {
-    ftl::Flags<InputDeviceClass> classes = contextPtr.getDeviceClasses();
+        InputDeviceContext& context, const InputReaderConfiguration& readerConfig) {
+    ftl::Flags<InputDeviceClass> classes = context.getDeviceClasses();
     std::vector<std::unique_ptr<InputMapper>> mappers;
 
     // Switch-like devices.
     if (classes.test(InputDeviceClass::SWITCH)) {
-        mappers.push_back(createInputMapper<SwitchInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(createInputMapper<SwitchInputMapper>(context, readerConfig));
     }
 
     // Scroll wheel-like devices.
     if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
-        mappers.push_back(createInputMapper<RotaryEncoderInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(createInputMapper<RotaryEncoderInputMapper>(context, readerConfig));
     }
 
     // Vibrator-like devices.
     if (classes.test(InputDeviceClass::VIBRATOR)) {
-        mappers.push_back(createInputMapper<VibratorInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(createInputMapper<VibratorInputMapper>(context, readerConfig));
     }
 
     // Battery-like devices or light-containing devices.
     // PeripheralController will be created with associated EventHub device.
     if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
-        mController = std::make_unique<PeripheralController>(contextPtr);
+        mController = std::make_unique<PeripheralController>(context);
     }
 
     // Keyboard-like devices.
@@ -482,13 +483,13 @@
     }
 
     if (keyboardSource != 0) {
-        mappers.push_back(createInputMapper<KeyboardInputMapper>(contextPtr, readerConfig,
+        mappers.push_back(createInputMapper<KeyboardInputMapper>(context, readerConfig,
                                                                  keyboardSource, keyboardType));
     }
 
     // Cursor-like devices.
     if (classes.test(InputDeviceClass::CURSOR)) {
-        mappers.push_back(createInputMapper<CursorInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(createInputMapper<CursorInputMapper>(context, readerConfig));
     }
 
     // Touchscreens and touchpad devices.
@@ -496,31 +497,31 @@
             sysprop::InputProperties::enable_touchpad_gestures_library().value_or(true);
     // TODO(b/272518665): Fix the new touchpad stack for Sony DualShock 4 (5c4, 9cc) touchpads, or
     // at least load this setting from the IDC file.
-    const InputDeviceIdentifier identifier = contextPtr.getDeviceIdentifier();
+    const InputDeviceIdentifier identifier = context.getDeviceIdentifier();
     const bool isSonyDualShock4Touchpad = identifier.vendor == 0x054c &&
             (identifier.product == 0x05c4 || identifier.product == 0x09cc);
     if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
         classes.test(InputDeviceClass::TOUCH_MT) && !isSonyDualShock4Touchpad) {
-        mappers.push_back(createInputMapper<TouchpadInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(createInputMapper<TouchpadInputMapper>(context, readerConfig));
     } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
-        mappers.push_back(std::make_unique<MultiTouchInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(std::make_unique<MultiTouchInputMapper>(context, readerConfig));
     } else if (classes.test(InputDeviceClass::TOUCH)) {
-        mappers.push_back(std::make_unique<SingleTouchInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(std::make_unique<SingleTouchInputMapper>(context, readerConfig));
     }
 
     // Joystick-like devices.
     if (classes.test(InputDeviceClass::JOYSTICK)) {
-        mappers.push_back(createInputMapper<JoystickInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(createInputMapper<JoystickInputMapper>(context, readerConfig));
     }
 
     // Motion sensor enabled devices.
     if (classes.test(InputDeviceClass::SENSOR)) {
-        mappers.push_back(createInputMapper<SensorInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(createInputMapper<SensorInputMapper>(context, readerConfig));
     }
 
     // External stylus-like devices.
     if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
-        mappers.push_back(createInputMapper<ExternalStylusInputMapper>(contextPtr, readerConfig));
+        mappers.push_back(createInputMapper<ExternalStylusInputMapper>(context, readerConfig));
     }
     return mappers;
 }