1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.test; 18 19 import android.adservices.ondevicepersonalization.DownloadCompletedInput; 20 import android.adservices.ondevicepersonalization.DownloadCompletedOutput; 21 import android.adservices.ondevicepersonalization.IsolatedServiceException; 22 import android.adservices.ondevicepersonalization.IsolatedWorker; 23 import android.adservices.ondevicepersonalization.KeyValueStore; 24 import android.os.OutcomeReceiver; 25 import android.util.Log; 26 27 import java.util.ArrayList; 28 import java.util.Arrays; 29 import java.util.List; 30 import java.util.Set; 31 32 // TODO(b/249345663) Move this class and related manifest to separate APK for more realistic testing 33 public class TestPersonalizationHandler implements IsolatedWorker { 34 public final String TAG = "TestIsolatedWorker"; 35 private final KeyValueStore mRemoteData; 36 TestPersonalizationHandler(KeyValueStore remoteData)37 TestPersonalizationHandler(KeyValueStore remoteData) { 38 mRemoteData = remoteData; 39 } 40 41 @Override onDownloadCompleted( DownloadCompletedInput input, OutcomeReceiver<DownloadCompletedOutput, IsolatedServiceException> receiver)42 public void onDownloadCompleted( 43 DownloadCompletedInput input, 44 OutcomeReceiver<DownloadCompletedOutput, IsolatedServiceException> receiver) { 45 try { 46 Log.d(TAG, "Starting filterData."); 47 Log.d(TAG, "Existing keyExtra: " 48 + Arrays.toString(mRemoteData.get("keyExtra"))); 49 Log.d(TAG, "Existing keySet: " + mRemoteData.keySet()); 50 51 DownloadCompletedOutput result = 52 new DownloadCompletedOutput.Builder() 53 .setRetainedKeys(getFilteredKeys(input.getDownloadedContents())) 54 .build(); 55 receiver.onResult(result); 56 } catch (Exception e) { 57 Log.e(TAG, "Error occurred in onDownload", e); 58 } 59 } 60 getFilteredKeys(KeyValueStore data)61 private List<String> getFilteredKeys(KeyValueStore data) { 62 Set<String> filteredKeys = data.keySet(); 63 filteredKeys.remove("key3"); 64 return new ArrayList<>(filteredKeys); 65 } 66 } 67