Add a class to represent the result of a download processing task.

Currently, it holds the list of keys to be retained. This gives us the
option to extend it in the future with flags, wildcard expressions etc.

Bug: 228200518
Test: atest
Change-Id: I06db8019e86b6b952cad737f6b28ac30d50f51cc
diff --git a/framework/java/android/ondevicepersonalization/DownloadResult.aidl b/framework/java/android/ondevicepersonalization/DownloadResult.aidl
new file mode 100644
index 0000000..528b289
--- /dev/null
+++ b/framework/java/android/ondevicepersonalization/DownloadResult.aidl
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.ondevicepersonalization;
+
+parcelable DownloadResult;
diff --git a/framework/java/android/ondevicepersonalization/DownloadResult.java b/framework/java/android/ondevicepersonalization/DownloadResult.java
new file mode 100644
index 0000000..9dd632d
--- /dev/null
+++ b/framework/java/android/ondevicepersonalization/DownloadResult.java
@@ -0,0 +1,212 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.ondevicepersonalization;
+
+import android.annotation.Nullable;
+import android.os.Parcelable;
+
+import com.android.ondevicepersonalization.internal.util.DataClass;
+
+import java.util.List;
+
+/**
+ * The result of the download post-processing task.
+ *
+ * @hide
+ */
+@DataClass(genBuilder = true, genEqualsHashCode = true)
+public final class DownloadResult implements Parcelable {
+    /** The keys to be retained in the REMOTE_DATA table. */
+    @Nullable private List<String> mKeysToRetain = null;
+
+
+
+    // Code below generated by codegen v1.0.23.
+    //
+    // DO NOT MODIFY!
+    // CHECKSTYLE:OFF Generated code
+    //
+    // To regenerate run:
+    // $ codegen $ANDROID_BUILD_TOP/packages/modules/OnDevicePersonalization/framework/java/android/ondevicepersonalization/DownloadResult.java
+    //
+    // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
+    //   Settings > Editor > Code Style > Formatter Control
+    //@formatter:off
+
+
+    @DataClass.Generated.Member
+    /* package-private */ DownloadResult(
+            @Nullable List<String> keysToRetain) {
+        this.mKeysToRetain = keysToRetain;
+
+        // onConstructed(); // You can define this method to get a callback
+    }
+
+    /**
+     * The keys to be retained in the REMOTE_DATA table.
+     */
+    @DataClass.Generated.Member
+    public @Nullable List<String> getKeysToRetain() {
+        return mKeysToRetain;
+    }
+
+    @Override
+    @DataClass.Generated.Member
+    public boolean equals(@Nullable Object o) {
+        // You can override field equality logic by defining either of the methods like:
+        // boolean fieldNameEquals(DownloadResult other) { ... }
+        // boolean fieldNameEquals(FieldType otherValue) { ... }
+
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        @SuppressWarnings("unchecked")
+        DownloadResult that = (DownloadResult) o;
+        //noinspection PointlessBooleanExpression
+        return true
+                && java.util.Objects.equals(mKeysToRetain, that.mKeysToRetain);
+    }
+
+    @Override
+    @DataClass.Generated.Member
+    public int hashCode() {
+        // You can override field hashCode logic by defining methods like:
+        // int fieldNameHashCode() { ... }
+
+        int _hash = 1;
+        _hash = 31 * _hash + java.util.Objects.hashCode(mKeysToRetain);
+        return _hash;
+    }
+
+    @Override
+    @DataClass.Generated.Member
+    public void writeToParcel(@android.annotation.NonNull android.os.Parcel dest, int flags) {
+        // You can override field parcelling by defining methods like:
+        // void parcelFieldName(Parcel dest, int flags) { ... }
+
+        byte flg = 0;
+        if (mKeysToRetain != null) flg |= 0x1;
+        dest.writeByte(flg);
+        if (mKeysToRetain != null) dest.writeStringList(mKeysToRetain);
+    }
+
+    @Override
+    @DataClass.Generated.Member
+    public int describeContents() { return 0; }
+
+    /** @hide */
+    @SuppressWarnings({"unchecked", "RedundantCast"})
+    @DataClass.Generated.Member
+    /* package-private */ DownloadResult(@android.annotation.NonNull android.os.Parcel in) {
+        // You can override field unparcelling by defining methods like:
+        // static FieldType unparcelFieldName(Parcel in) { ... }
+
+        byte flg = in.readByte();
+        List<String> keysToRetain = null;
+        if ((flg & 0x1) != 0) {
+            keysToRetain = new java.util.ArrayList<>();
+            in.readStringList(keysToRetain);
+        }
+
+        this.mKeysToRetain = keysToRetain;
+
+        // onConstructed(); // You can define this method to get a callback
+    }
+
+    @DataClass.Generated.Member
+    public static final @android.annotation.NonNull Parcelable.Creator<DownloadResult> CREATOR
+            = new Parcelable.Creator<DownloadResult>() {
+        @Override
+        public DownloadResult[] newArray(int size) {
+            return new DownloadResult[size];
+        }
+
+        @Override
+        public DownloadResult createFromParcel(@android.annotation.NonNull android.os.Parcel in) {
+            return new DownloadResult(in);
+        }
+    };
+
+    /**
+     * A builder for {@link DownloadResult}
+     */
+    @SuppressWarnings("WeakerAccess")
+    @DataClass.Generated.Member
+    public static final class Builder {
+
+        private @Nullable List<String> mKeysToRetain;
+
+        private long mBuilderFieldsSet = 0L;
+
+        public Builder() {
+        }
+
+        /**
+         * The keys to be retained in the REMOTE_DATA table.
+         */
+        @DataClass.Generated.Member
+        public @android.annotation.NonNull Builder setKeysToRetain(@android.annotation.NonNull List<String> value) {
+            checkNotUsed();
+            mBuilderFieldsSet |= 0x1;
+            mKeysToRetain = value;
+            return this;
+        }
+
+        /** @see #setKeysToRetain */
+        @DataClass.Generated.Member
+        public @android.annotation.NonNull Builder addKeysToRetain(@android.annotation.NonNull String value) {
+            // You can refine this method's name by providing item's singular name, e.g.:
+            // @DataClass.PluralOf("item")) mItems = ...
+
+            if (mKeysToRetain == null) setKeysToRetain(new java.util.ArrayList<>());
+            mKeysToRetain.add(value);
+            return this;
+        }
+
+        /** Builds the instance. This builder should not be touched after calling this! */
+        public @android.annotation.NonNull DownloadResult build() {
+            checkNotUsed();
+            mBuilderFieldsSet |= 0x2; // Mark builder used
+
+            if ((mBuilderFieldsSet & 0x1) == 0) {
+                mKeysToRetain = null;
+            }
+            DownloadResult o = new DownloadResult(
+                    mKeysToRetain);
+            return o;
+        }
+
+        private void checkNotUsed() {
+            if ((mBuilderFieldsSet & 0x2) != 0) {
+                throw new IllegalStateException(
+                        "This Builder should not be reused. Use a new Builder instance instead");
+            }
+        }
+    }
+
+    @DataClass.Generated(
+            time = 1671734248808L,
+            codegenVersion = "1.0.23",
+            sourceFile = "packages/modules/OnDevicePersonalization/framework/java/android/ondevicepersonalization/DownloadResult.java",
+            inputSignatures = "private @android.annotation.Nullable java.util.List<java.lang.String> mKeysToRetain\nclass DownloadResult extends java.lang.Object implements [android.os.Parcelable]\n@com.android.ondevicepersonalization.internal.util.DataClass(genBuilder=true, genEqualsHashCode=true)")
+    @Deprecated
+    private void __metadata() {}
+
+
+    //@formatter:on
+    // End of generated code
+
+}
diff --git a/framework/java/android/ondevicepersonalization/PersonalizationService.java b/framework/java/android/ondevicepersonalization/PersonalizationService.java
index 1439fa3..c88b3da 100644
--- a/framework/java/android/ondevicepersonalization/PersonalizationService.java
+++ b/framework/java/android/ondevicepersonalization/PersonalizationService.java
@@ -64,7 +64,7 @@
      */
     public interface DownloadCallback {
         /** Retains the provided keys */
-        void onSuccess(List<String> keysToRetain);
+        void onSuccess(DownloadResult downloadResult);
 
         /** Error in download processing. The platform will retry the download. */
         void onError();
@@ -195,10 +195,9 @@
                 OnDevicePersonalizationContext odpContext =
                         new OnDevicePersonalizationContextImpl(binder);
                 var wrappedCallback = new DownloadCallback() {
-                    @Override public void onSuccess(List<String> keysToRetain) {
+                    @Override public void onSuccess(DownloadResult result) {
                         Bundle bundle = new Bundle();
-                        bundle.putStringArray(Constants.EXTRA_RESULT,
-                                keysToRetain.toArray(new String[0]));
+                        bundle.putParcelable(Constants.EXTRA_RESULT, result);
                         try {
                             callback.onSuccess(bundle);
                         } catch (RemoteException e) {
diff --git a/src/com/android/ondevicepersonalization/services/download/OnDevicePersonalizationDataProcessingAsyncCallable.java b/src/com/android/ondevicepersonalization/services/download/OnDevicePersonalizationDataProcessingAsyncCallable.java
index a865b1d..d6c6dd8 100644
--- a/src/com/android/ondevicepersonalization/services/download/OnDevicePersonalizationDataProcessingAsyncCallable.java
+++ b/src/com/android/ondevicepersonalization/services/download/OnDevicePersonalizationDataProcessingAsyncCallable.java
@@ -21,6 +21,7 @@
 import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.ondevicepersonalization.Constants;
+import android.ondevicepersonalization.DownloadResult;
 import android.os.Bundle;
 import android.os.ParcelFileDescriptor;
 import android.util.JsonReader;
@@ -185,10 +186,14 @@
             Map<String, VendorData> vendorDataMap) {
         Log.d(TAG, "Plugin filter code completed successfully");
         List<VendorData> filteredList = new ArrayList<>();
-        String[] retainedKeys = pluginResult.getStringArray(Constants.EXTRA_RESULT);
-        for (String key : retainedKeys) {
-            if (vendorDataMap.containsKey(key)) {
-                filteredList.add(vendorDataMap.get(key));
+        DownloadResult downloadResult = pluginResult.getParcelable(
+                Constants.EXTRA_RESULT, DownloadResult.class);
+        List<String> retainedKeys = downloadResult.getKeysToRetain();
+        if (retainedKeys != null) {
+            for (String key : retainedKeys) {
+                if (vendorDataMap.containsKey(key)) {
+                    filteredList.add(vendorDataMap.get(key));
+                }
             }
         }
         mDao.batchUpdateOrInsertVendorDataTransaction(filteredList,
diff --git a/tests/frameworktests/src/android/ondevicepersonalization/OnDevicePersonalizationFrameworkClassesTest.java b/tests/frameworktests/src/android/ondevicepersonalization/OnDevicePersonalizationFrameworkClassesTest.java
index 26201fa..dccdba8 100644
--- a/tests/frameworktests/src/android/ondevicepersonalization/OnDevicePersonalizationFrameworkClassesTest.java
+++ b/tests/frameworktests/src/android/ondevicepersonalization/OnDevicePersonalizationFrameworkClassesTest.java
@@ -212,4 +212,22 @@
         assertEquals(result, result2);
         assertEquals("abc", result2.getContent());
     }
+
+    /**
+     * Tests that the DownloadResult object serializes correctly.
+     */
+    @Test
+    public void teetDownloadResult() {
+        DownloadResult result = new DownloadResult.Builder()
+                .addKeysToRetain("abc").addKeysToRetain("def").build();
+
+        Parcel parcel = Parcel.obtain();
+        result.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        DownloadResult result2 = DownloadResult.CREATOR.createFromParcel(parcel);
+
+        assertEquals(result, result2);
+        assertEquals("abc", result2.getKeysToRetain().get(0));
+        assertEquals("def", result2.getKeysToRetain().get(1));
+    }
 }
diff --git a/tests/servicetests/src/com/test/TestPersonalizationService.java b/tests/servicetests/src/com/test/TestPersonalizationService.java
index 657df65..26081f1 100644
--- a/tests/servicetests/src/com/test/TestPersonalizationService.java
+++ b/tests/servicetests/src/com/test/TestPersonalizationService.java
@@ -19,6 +19,7 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.ondevicepersonalization.AppRequestResult;
+import android.ondevicepersonalization.DownloadResult;
 import android.ondevicepersonalization.OnDevicePersonalizationContext;
 import android.ondevicepersonalization.PersonalizationService;
 import android.ondevicepersonalization.RenderContentResult;
@@ -55,7 +56,11 @@
                     public void onResult(@NonNull Map<String, byte[]> result) {
                         Log.d(TAG, "OutcomeReceiver onResult: " + result);
                         // Get the keys to keep from the downloaded data
-                        callback.onSuccess(getFilteredKeys(fd));
+                        DownloadResult downloadResult =
+                                new DownloadResult.Builder()
+                                .setKeysToRetain(getFilteredKeys(fd))
+                                .build();
+                        callback.onSuccess(downloadResult);
                     }
 
                     @Override