Default flush implementation for Sensors 2.0

Bug: 111070257
Test: Builds, passes corresponding VTS tests
Change-Id: I5b46232ffb0dd2c85bccd9dad34dd04d29a5677d
diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp
index 21c1591..d3e3f7e 100644
--- a/sensors/2.0/default/Sensor.cpp
+++ b/sensors/2.0/default/Sensor.cpp
@@ -24,6 +24,7 @@
 namespace V2_0 {
 namespace implementation {
 
+using ::android::hardware::sensors::V1_0::MetaDataEventType;
 using ::android::hardware::sensors::V1_0::SensorFlagBits;
 using ::android::hardware::sensors::V1_0::SensorStatus;
 
@@ -64,6 +65,25 @@
     }
 }
 
+Result Sensor::flush() {
+    // Only generate a flush complete event if the sensor is enabled and if the sensor is not a
+    // one-shot sensor.
+    if (!mIsEnabled || (mSensorInfo.flags & static_cast<uint32_t>(SensorFlagBits::ONE_SHOT_MODE))) {
+        return Result::BAD_VALUE;
+    }
+
+    // Note: If a sensor supports batching, write all of the currently batched events for the sensor
+    // to the Event FMQ prior to writing the flush complete event.
+    Event ev;
+    ev.sensorHandle = mSensorInfo.sensorHandle;
+    ev.sensorType = SensorType::ADDITIONAL_INFO;
+    ev.u.meta.what = MetaDataEventType::META_DATA_FLUSH_COMPLETE;
+    std::vector<Event> evs{ev};
+    mCallback->postEvents(evs);
+
+    return Result::OK;
+}
+
 void Sensor::startThread(Sensor* sensor) {
     sensor->run();
 }
diff --git a/sensors/2.0/default/Sensor.h b/sensors/2.0/default/Sensor.h
index e467b56..75d9aab 100644
--- a/sensors/2.0/default/Sensor.h
+++ b/sensors/2.0/default/Sensor.h
@@ -25,6 +25,7 @@
 #include <vector>
 
 using ::android::hardware::sensors::V1_0::Event;
+using ::android::hardware::sensors::V1_0::Result;
 using ::android::hardware::sensors::V1_0::SensorInfo;
 using ::android::hardware::sensors::V1_0::SensorType;
 
@@ -48,6 +49,7 @@
     const SensorInfo& getSensorInfo() const;
     void batch(int32_t samplingPeriodNs);
     void activate(bool enable);
+    Result flush();
 
    protected:
     void run();
diff --git a/sensors/2.0/default/Sensors.cpp b/sensors/2.0/default/Sensors.cpp
index 39c1ded..cceb7d5 100644
--- a/sensors/2.0/default/Sensors.cpp
+++ b/sensors/2.0/default/Sensors.cpp
@@ -112,9 +112,12 @@
     return Result::BAD_VALUE;
 }
 
-Return<Result> Sensors::flush(int32_t /* sensorHandle */) {
-    // TODO implement
-    return Result{};
+Return<Result> Sensors::flush(int32_t sensorHandle) {
+    auto sensor = mSensors.find(sensorHandle);
+    if (sensor != mSensors.end()) {
+        return sensor->second->flush();
+    }
+    return Result::BAD_VALUE;
 }
 
 Return<Result> Sensors::injectSensorData(const Event& /* event */) {