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.android.ondevicepersonalization.services.download; 18 19 import android.adservices.ondevicepersonalization.DownloadCompletedOutputParcel; 20 import android.content.Context; 21 22 import androidx.annotation.VisibleForTesting; 23 24 import com.android.ondevicepersonalization.internal.util.LoggerFactory; 25 import com.android.ondevicepersonalization.services.serviceflow.ServiceFlowOrchestrator; 26 import com.android.ondevicepersonalization.services.serviceflow.ServiceFlowType; 27 28 import com.google.common.util.concurrent.AsyncCallable; 29 import com.google.common.util.concurrent.FutureCallback; 30 import com.google.common.util.concurrent.ListenableFuture; 31 import com.google.common.util.concurrent.SettableFuture; 32 33 /** 34 * AsyncCallable to handle the processing of the downloaded vendor data 35 */ 36 public class OnDevicePersonalizationDataProcessingAsyncCallable implements AsyncCallable { 37 private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger(); 38 39 private static final ServiceFlowOrchestrator sSfo = ServiceFlowOrchestrator.getInstance(); 40 41 private final String mPackageName; 42 private final Context mContext; 43 private final Injector mInjector; 44 45 @VisibleForTesting 46 public static class Injector { getFutureCallback( SettableFuture<Boolean> downloadFlowFuture)47 FutureCallback<DownloadCompletedOutputParcel> getFutureCallback( 48 SettableFuture<Boolean> downloadFlowFuture) { 49 return new FutureCallback<>() { 50 @Override 51 public void onSuccess(DownloadCompletedOutputParcel result) { 52 downloadFlowFuture.set(true); 53 } 54 55 @Override 56 public void onFailure(Throwable t) { 57 downloadFlowFuture.setException(t); 58 } 59 }; 60 } 61 } 62 63 public OnDevicePersonalizationDataProcessingAsyncCallable(String packageName, 64 Context context) { 65 this(packageName, context, new Injector()); 66 } 67 68 @VisibleForTesting 69 public OnDevicePersonalizationDataProcessingAsyncCallable(String packageName, 70 Context context, Injector injector) { 71 mPackageName = packageName; 72 mContext = context; 73 mInjector = injector; 74 } 75 76 /** 77 * Processes the downloaded files for the given package and stores the data into sqlite 78 * vendor tables. 79 */ 80 public ListenableFuture<Boolean> call() { 81 SettableFuture<Boolean> downloadFlowFuture = SettableFuture.create(); 82 FutureCallback<DownloadCompletedOutputParcel> callback = 83 mInjector.getFutureCallback(downloadFlowFuture); 84 85 sSfo.schedule(ServiceFlowType.DOWNLOAD_FLOW, mPackageName, mContext, callback); 86 87 return downloadFlowFuture; 88 } 89 } 90