Snap for 11292102 from 73274ef152cd0ba9c9f2945a2fc9798efa6b7c5e to 24Q2-release

Change-Id: I0f762a75003a7acbc331e0110fad4696b03012a8
diff --git a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp
index 101b983..8df91c2 100644
--- a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp
+++ b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp
@@ -20,6 +20,7 @@
 #include <convertV2_1.h>
 #include <hardware/sensors-base.h>
 #include <log/log.h>
+#include <com_android_libhardware_dynamic_sensors_flags.h>
 
 #include <chrono>
 #include <thread>
@@ -32,6 +33,8 @@
 template<class T> using Return = ::android::hardware::Return<T>;
 using ::android::hardware::Void;
 
+namespace dynamic_sensors_flags = com::android::libhardware::dynamic::sensors::flags;
+
 namespace android {
 namespace SensorHalExt {
 
@@ -165,6 +168,8 @@
     std::vector<Event> events;
     Event hal_event;
     bool wakeup;
+    bool disconnectDynamicSensorFlag =
+        dynamic_sensors_flags::dynamic_sensors_hal_disconnect_dynamic_sensor();
 
     if (e.type == SENSOR_TYPE_DYNAMIC_SENSOR_META) {
         const dynamic_sensor_meta_event_t* sensor_meta;
@@ -172,21 +177,25 @@
         sensor_meta = static_cast<const dynamic_sensor_meta_event_t*>(
                 &(e.dynamic_sensor_meta));
         if (sensor_meta->connected != 0) {
-            // The sensor framework must be notified of the connected sensor
-            // through the callback before handling the sensor added event. If
-            // it isn't, it will assert when looking up the sensor handle when
-            // processing the sensor added event.
-            //
-            // TODO (b/201529167): Fix dynamic sensors addition / removal when
-            //                     converting to AIDL.
-            // The sensor framework runs in a separate process from the sensor
-            // HAL, and it processes events in a dedicated thread, so it's
-            // possible the event handling can be done before the callback is
-            // run. Thus, a delay is added after sending notification of the
-            // connected sensor.
             onSensorConnected(sensor_meta->handle, sensor_meta->sensor);
+        } else if (disconnectDynamicSensorFlag) {
+            onSensorDisconnected(sensor_meta->handle);
+        }
+        // The sensor framework must be notified of the connected sensor
+        // through the callback before handling the sensor added event. If
+        // it isn't, it will assert when looking up the sensor handle when
+        // processing the sensor added event.
+        //
+        // TODO (b/201529167): Fix dynamic sensors addition / removal when
+        //                     converting to AIDL.
+        // The sensor framework runs in a separate process from the sensor
+        // HAL, and it processes events in a dedicated thread, so it's
+        // possible the event handling can be done before the callback is
+        // run. Thus, a delay is added after sending notification of the
+        // connected sensor.
+        if (disconnectDynamicSensorFlag || sensor_meta->connected != 0) {
             std::this_thread::sleep_for(std::chrono::milliseconds(1000));
-       }
+        }
     }
 
     convertFromSensorEvent(e, &hal_event);
@@ -226,6 +235,14 @@
     mHalProxyCallback->onDynamicSensorsConnected_2_1(sensor_list);
 }
 
+void DynamicSensorsSubHal::onSensorDisconnected(int handle) {
+    hidl_vec<int32_t> handleList;
+    handleList.resize(1);
+    handleList[0] = handle;
+
+    mHalProxyCallback->onDynamicSensorsDisconnected(handleList);
+}
+
 } // namespace SensorHalExt
 } // namespace android
 
diff --git a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.h b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.h
index f59b00a..0d90dc0 100644
--- a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.h
+++ b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.h
@@ -77,6 +77,7 @@
                                                       kDynamicHandleBase;
 
     void onSensorConnected(int handle, const sensor_t* sensor_info);
+    void onSensorDisconnected(int handle);
 
     std::unique_ptr<DynamicSensorManager> mDynamicSensorManager;
     sp<IHalProxyCallback> mHalProxyCallback;