Add Hinge Angle Sensor to default impl for HAL 2.1

Bug: 144139857
Test: Verify this type is exposed when VTS is run
Change-Id: I994f1b4c77729b76760b7cafc19b825c98ca97ca
diff --git a/sensors/2.1/default/SensorsV2_1.cpp b/sensors/2.1/default/SensorsV2_1.cpp
index adf874e..2e3d315 100644
--- a/sensors/2.1/default/SensorsV2_1.cpp
+++ b/sensors/2.1/default/SensorsV2_1.cpp
@@ -16,17 +16,48 @@
 
 #include "SensorsV2_1.h"
 
+#include "Sensor.h"
+
 namespace android {
 namespace hardware {
 namespace sensors {
 namespace V2_1 {
 namespace implementation {
 
+using V2_X::implementation::ISensorsEventCallback;
+using V2_X::implementation::OnChangeSensor;
+
+class HingeAngleSensor : public OnChangeSensor {
+  public:
+    HingeAngleSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
+        : OnChangeSensor(callback) {
+        mSensorInfo.sensorHandle = sensorHandle;
+        mSensorInfo.name = "Hinge Angle Sensor";
+        mSensorInfo.vendor = "Vendor String";
+        mSensorInfo.version = 1;
+        mSensorInfo.type = SensorType::HINGE_ANGLE;
+        mSensorInfo.typeAsString = "";
+        mSensorInfo.maxRange = 360.0f;
+        mSensorInfo.resolution = 1.0f;
+        mSensorInfo.power = 0.001f;
+        mSensorInfo.minDelay = 40 * 1000;  // microseconds
+        mSensorInfo.maxDelay = V2_X::implementation::kDefaultMaxDelayUs;
+        mSensorInfo.fifoReservedEventCount = 0;
+        mSensorInfo.fifoMaxEventCount = 0;
+        mSensorInfo.requiredPermission = "";
+        mSensorInfo.flags = static_cast<uint32_t>(V1_0::SensorFlagBits::ON_CHANGE_MODE);
+    }
+};
+
+SensorsV2_1::SensorsV2_1() {
+    AddSensor<HingeAngleSensor>();
+}
+
 // Methods from ::android::hardware::sensors::V2_1::ISensors follow.
 Return<void> SensorsV2_1::getSensorsList_2_1(ISensors::getSensorsList_2_1_cb _hidl_cb) {
     std::vector<SensorInfo> sensors;
     for (const auto& sensor : mSensors) {
-        sensors.push_back(convertToNewSensorInfo(sensor.second->getSensorInfo()));
+        sensors.push_back(sensor.second->getSensorInfo());
     }
 
     // Call the HIDL callback with the SensorInfo
diff --git a/sensors/2.1/default/SensorsV2_1.h b/sensors/2.1/default/SensorsV2_1.h
index 9cd16a7..9f7fe04 100644
--- a/sensors/2.1/default/SensorsV2_1.h
+++ b/sensors/2.1/default/SensorsV2_1.h
@@ -49,6 +49,8 @@
 };
 
 struct SensorsV2_1 : public Sensors {
+    SensorsV2_1();
+
     // Methods from ::android::hardware::sensors::V2_1::ISensors follow.
     Return<void> getSensorsList_2_1(ISensors::getSensorsList_2_1_cb _hidl_cb) override;
 
diff --git a/sensors/common/default/2.X/Android.bp b/sensors/common/default/2.X/Android.bp
index ea75a10..8b0d52f 100644
--- a/sensors/common/default/2.X/Android.bp
+++ b/sensors/common/default/2.X/Android.bp
@@ -26,6 +26,7 @@
     shared_libs: [
         "android.hardware.sensors@1.0",
         "android.hardware.sensors@2.0",
+        "android.hardware.sensors@2.1",
         "libcutils",
         "libfmq",
         "libhidlbase",
diff --git a/sensors/common/default/2.X/Sensor.cpp b/sensors/common/default/2.X/Sensor.cpp
index 4c40d1f..1841dff 100644
--- a/sensors/common/default/2.X/Sensor.cpp
+++ b/sensors/common/default/2.X/Sensor.cpp
@@ -26,16 +26,14 @@
 namespace V2_X {
 namespace implementation {
 
-using ::android::hardware::sensors::V1_0::Event;
 using ::android::hardware::sensors::V1_0::MetaDataEventType;
 using ::android::hardware::sensors::V1_0::OperationMode;
 using ::android::hardware::sensors::V1_0::Result;
 using ::android::hardware::sensors::V1_0::SensorFlagBits;
-using ::android::hardware::sensors::V1_0::SensorInfo;
 using ::android::hardware::sensors::V1_0::SensorStatus;
-using ::android::hardware::sensors::V1_0::SensorType;
-
-static constexpr float kDefaultMaxDelayUs = 10 * 1000 * 1000;
+using ::android::hardware::sensors::V2_1::Event;
+using ::android::hardware::sensors::V2_1::SensorInfo;
+using ::android::hardware::sensors::V2_1::SensorType;
 
 Sensor::Sensor(ISensorsEventCallback* callback)
     : mIsEnabled(false),
diff --git a/sensors/common/default/2.X/Sensor.h b/sensors/common/default/2.X/Sensor.h
index 8592c41..2f8a143 100644
--- a/sensors/common/default/2.X/Sensor.h
+++ b/sensors/common/default/2.X/Sensor.h
@@ -18,6 +18,7 @@
 #define ANDROID_HARDWARE_SENSORS_V2_X_SENSOR_H
 
 #include <android/hardware/sensors/1.0/types.h>
+#include <android/hardware/sensors/2.1/types.h>
 
 #include <condition_variable>
 #include <memory>
@@ -31,9 +32,11 @@
 namespace V2_X {
 namespace implementation {
 
+static constexpr float kDefaultMaxDelayUs = 10 * 1000 * 1000;
+
 class ISensorsEventCallback {
   public:
-    using Event = ::android::hardware::sensors::V1_0::Event;
+    using Event = ::android::hardware::sensors::V2_1::Event;
 
     virtual ~ISensorsEventCallback(){};
     virtual void postEvents(const std::vector<Event>& events, bool wakeup) = 0;
@@ -41,11 +44,11 @@
 
 class Sensor {
   public:
-    using Event = ::android::hardware::sensors::V1_0::Event;
     using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
     using Result = ::android::hardware::sensors::V1_0::Result;
-    using SensorInfo = ::android::hardware::sensors::V1_0::SensorInfo;
-    using SensorType = ::android::hardware::sensors::V1_0::SensorType;
+    using Event = ::android::hardware::sensors::V2_1::Event;
+    using SensorInfo = ::android::hardware::sensors::V2_1::SensorInfo;
+    using SensorType = ::android::hardware::sensors::V2_1::SensorType;
 
     Sensor(ISensorsEventCallback* callback);
     virtual ~Sensor();
diff --git a/sensors/common/default/2.X/Sensors.h b/sensors/common/default/2.X/Sensors.h
index de998eb..ee8240d 100644
--- a/sensors/common/default/2.X/Sensors.h
+++ b/sensors/common/default/2.X/Sensors.h
@@ -82,7 +82,8 @@
     Return<void> getSensorsList(V2_0::ISensors::getSensorsList_cb _hidl_cb) override {
         std::vector<V1_0::SensorInfo> sensors;
         for (const auto& sensor : mSensors) {
-            sensors.push_back(sensor.second->getSensorInfo());
+            sensors.push_back(
+                    V2_1::implementation::convertToOldSensorInfo(sensor.second->getSensorInfo()));
         }
 
         // Call the HIDL callback with the SensorInfo
@@ -187,7 +188,7 @@
     Return<Result> injectSensorData(const Event& event) override {
         auto sensor = mSensors.find(event.sensorHandle);
         if (sensor != mSensors.end()) {
-            return sensor->second->injectEvent(event);
+            return sensor->second->injectEvent(V2_1::implementation::convertToNewEvent(event));
         }
 
         return Result::BAD_VALUE;
@@ -210,9 +211,9 @@
         return Return<void>();
     }
 
-    void postEvents(const std::vector<Event>& events, bool wakeup) override {
+    void postEvents(const std::vector<V2_1::Event>& events, bool wakeup) override {
         std::lock_guard<std::mutex> lock(mWriteLock);
-        if (mEventQueue->write(V2_1::implementation::convertToNewEvents(events))) {
+        if (mEventQueue->write(events)) {
             mEventQueueFlag->wake(static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS));
 
             if (wakeup) {