Try closing PFDs sent in map after binder call

- Iterate through all PFDs sent by caller after the binder call to send
  to system-server is complete. This prevents any potential leakages if
  the client does not close the FDs themselves.

Bug: 347173075
Test: cts
Change-Id: I06652b561d3548f7d5ec976b7a787e9c6c3d0b74
diff --git a/core/java/android/service/ondeviceintelligence/OnDeviceIntelligenceService.java b/core/java/android/service/ondeviceintelligence/OnDeviceIntelligenceService.java
index 293015f..d82fe1c 100644
--- a/core/java/android/service/ondeviceintelligence/OnDeviceIntelligenceService.java
+++ b/core/java/android/service/ondeviceintelligence/OnDeviceIntelligenceService.java
@@ -59,6 +59,7 @@
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -224,6 +225,7 @@
                                         Bundle bundle = new Bundle();
                                         parcelFileDescriptorMap.forEach(bundle::putParcelable);
                                         remoteCallback.sendResult(bundle);
+                                        tryClosePfds(parcelFileDescriptorMap.values());
                                     }));
                 }
 
@@ -444,6 +446,16 @@
         };
     }
 
+    private static void tryClosePfds(Collection<ParcelFileDescriptor> pfds) {
+        pfds.forEach(pfd -> {
+            try {
+                pfd.close();
+            } catch (Exception e) {
+                Log.w(TAG, "Error closing FD", e);
+            }
+        });
+    }
+
     private void onGetReadOnlyFileDescriptor(@NonNull String fileName,
             @NonNull AndroidFuture<ParcelFileDescriptor> future) {
         Slog.v(TAG, "onGetReadOnlyFileDescriptor " + fileName);