Merge "Change callback argument to boolean." am: 3f90a7e59e am: 53dcdd11eb am: bbe9622787

Original change: https://android-review.googlesource.com/c/platform/system/libhidl/+/1555798

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Id592f3b843fec7c91b8d3e1e871a7418c0571e78
diff --git a/transport/HidlLazyUtils.cpp b/transport/HidlLazyUtils.cpp
index ce37dd0..be7470f 100644
--- a/transport/HidlLazyUtils.cpp
+++ b/transport/HidlLazyUtils.cpp
@@ -40,8 +40,7 @@
 
     void reRegister();
 
-    void setActiveServicesCountCallback(
-            const std::function<bool(int)>& activeServicesCountCallback);
+    void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
 
   protected:
     Return<void> onClients(const sp<IBase>& service, bool clients) override;
@@ -80,7 +79,12 @@
     /**
      * Callback for reporting the number of services with clients.
      */
-    std::function<bool(int)> mActiveServicesCountCallback;
+    std::function<bool(bool)> mActiveServicesCallback;
+
+    /**
+     * Previous value passed to the active services callback.
+     */
+    std::optional<bool> mPreviousHasClients;
 };
 
 class LazyServiceRegistrarImpl {
@@ -91,8 +95,7 @@
                              const std::string& name);
     bool tryUnregister();
     void reRegister();
-    void setActiveServicesCountCallback(
-            const std::function<bool(int)>& activeServicesCountCallback);
+    void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
 
   private:
     sp<ClientCounterCallback> mClientCallback;
@@ -166,8 +169,12 @@
               << "/" << registered.name << " has clients: " << clients;
 
     bool handledInCallback = false;
-    if (mActiveServicesCountCallback != nullptr) {
-        handledInCallback = mActiveServicesCountCallback(numWithClients);
+    if (mActiveServicesCallback != nullptr) {
+        bool hasClients = numWithClients != 0;
+        if (hasClients != mPreviousHasClients) {
+            handledInCallback = mActiveServicesCallback(hasClients);
+            mPreviousHasClients = hasClients;
+        }
     }
 
     // If there is no callback defined or the callback did not handle this
@@ -229,9 +236,9 @@
     reRegister();
 }
 
-void ClientCounterCallback::setActiveServicesCountCallback(
-        const std::function<bool(int)>& activeServicesCountCallback) {
-    mActiveServicesCountCallback = activeServicesCountCallback;
+void ClientCounterCallback::setActiveServicesCallback(
+        const std::function<bool(bool)>& activeServicesCallback) {
+    mActiveServicesCallback = activeServicesCallback;
 }
 
 status_t LazyServiceRegistrarImpl::registerService(
@@ -251,9 +258,9 @@
     mClientCallback->reRegister();
 }
 
-void LazyServiceRegistrarImpl::setActiveServicesCountCallback(
-        const std::function<bool(int)>& activeServicesCountCallback) {
-    mClientCallback->setActiveServicesCountCallback(activeServicesCountCallback);
+void LazyServiceRegistrarImpl::setActiveServicesCallback(
+        const std::function<bool(bool)>& activeServicesCallback) {
+    mClientCallback->setActiveServicesCallback(activeServicesCallback);
 }
 
 }  // namespace details
@@ -280,9 +287,9 @@
     mImpl->reRegister();
 }
 
-void LazyServiceRegistrar::setActiveServicesCountCallback(
-        const std::function<bool(int)>& activeServicesCountCallback) {
-    mImpl->setActiveServicesCountCallback(activeServicesCountCallback);
+void LazyServiceRegistrar::setActiveServicesCallback(
+        const std::function<bool(bool)>& activeServicesCallback) {
+    mImpl->setActiveServicesCallback(activeServicesCallback);
 }
 
 }  // namespace hardware
diff --git a/transport/include/hidl/HidlLazyUtils.h b/transport/include/hidl/HidlLazyUtils.h
index 44fbcb2..427611b 100644
--- a/transport/include/hidl/HidlLazyUtils.h
+++ b/transport/include/hidl/HidlLazyUtils.h
@@ -44,10 +44,10 @@
      status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
                               const std::string& name = "default");
      /**
-      * Set a callback that is executed when the total number of services with
-      * clients changes.
-      * The callback takes an argument, which is the number of registered
-      * lazy HALs for this process which have clients.
+      * Set a callback that is invoked when the active HAL count (i.e. HALs with clients)
+      * registered with this process drops to zero (or becomes nonzero).
+      * The callback takes a boolean argument, which is 'true' if there is
+      * at least one HAL with clients.
       *
       * Callback return value:
       * - false: Default behavior for lazy HALs (shut down the process if there
@@ -61,8 +61,7 @@
       *
       * This method should be called before 'registerService' to avoid races.
       */
-     void setActiveServicesCountCallback(
-             const std::function<bool(int)>& activeServicesCountCallback);
+     void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
 
      /**
       * Try to unregister all services previously registered with 'registerService'.