[CDM perm sync] Remove SystemDataTransferRequest when disassociate

Fix: 220921569

Remove all the system data transfer requests for the association when
it's being disassociated.

Fix: 220921569

Test: Manually tested it in the CDM test app. Added unit tests.
Change-Id: I24fd4fd5639e0801e4f5df6b5616c3910b87f247
diff --git a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
index a3e5e3c..6696d37 100644
--- a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
+++ b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
@@ -870,6 +870,9 @@
         // Removing the association.
         mAssociationStore.removeAssociation(associationId);
 
+        // Remove all the system data transfer requests for the association.
+        mSystemDataTransferRequestStore.removeRequestsByAssociationId(userId, associationId);
+
         final List<AssociationInfo> otherAssociations =
                 mAssociationStore.getAssociationsForPackage(userId, packageName);
 
diff --git a/services/companion/java/com/android/server/companion/datatransfer/SystemDataTransferRequestStore.java b/services/companion/java/com/android/server/companion/datatransfer/SystemDataTransferRequestStore.java
index f24cc14..ab0a062 100644
--- a/services/companion/java/com/android/server/companion/datatransfer/SystemDataTransferRequestStore.java
+++ b/services/companion/java/com/android/server/companion/datatransfer/SystemDataTransferRequestStore.java
@@ -81,7 +81,6 @@
 
     private static final String XML_TAG_REQUESTS = "requests";
     private static final String XML_TAG_REQUEST = "request";
-    private static final String XML_TAG_LIST = "list";
 
     private static final String XML_ATTR_ASSOCIATION_ID = "association_id";
     private static final String XML_ATTR_DATA_TYPE = "data_type";
@@ -122,6 +121,7 @@
     }
 
     void writeRequest(@UserIdInt int userId, SystemDataTransferRequest request) {
+        Slog.i(LOG_TAG, "Writing request=" + request + " to store.");
         ArrayList<SystemDataTransferRequest> cachedRequests;
         synchronized (mLock) {
             // Write to cache
@@ -133,6 +133,23 @@
         mExecutor.execute(() -> writeRequestsToStore(userId, cachedRequests));
     }
 
+    /**
+     * Remove requests by association id. userId must be the one which owns the associationId.
+     */
+    public void removeRequestsByAssociationId(@UserIdInt int userId, int associationId) {
+        Slog.i(LOG_TAG, "Removing system data transfer requests for userId=" + userId
+                + ", associationId=" + associationId);
+        ArrayList<SystemDataTransferRequest> cachedRequests;
+        synchronized (mLock) {
+            // Remove requests from cache
+            cachedRequests = readRequestsFromCache(userId);
+            cachedRequests.removeIf(request -> request.getAssociationId() == associationId);
+            mCachedPerUser.set(userId, cachedRequests);
+        }
+        // Remove requests from store
+        mExecutor.execute(() -> writeRequestsToStore(userId, cachedRequests));
+    }
+
     @GuardedBy("mLock")
     private ArrayList<SystemDataTransferRequest> readRequestsFromCache(@UserIdInt int userId) {
         ArrayList<SystemDataTransferRequest> cachedRequests = mCachedPerUser.get(userId);