• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /** AsyncCallable to handle the processing of the downloaded vendor data */
34 class OnDevicePersonalizationDataProcessingAsyncCallable implements AsyncCallable {
35     private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger();
36 
37     private static final ServiceFlowOrchestrator sSfo = ServiceFlowOrchestrator.getInstance();
38 
39     private final String mPackageName;
40     private final Context mContext;
41     private final Injector mInjector;
42 
43     @VisibleForTesting
44     static class Injector {
getFutureCallback( SettableFuture<Boolean> downloadFlowFuture)45         FutureCallback<DownloadCompletedOutputParcel> getFutureCallback(
46                 SettableFuture<Boolean> downloadFlowFuture) {
47             return new FutureCallback<>() {
48                 @Override
49                 public void onSuccess(DownloadCompletedOutputParcel result) {
50                     downloadFlowFuture.set(true);
51                 }
52 
53                 @Override
54                 public void onFailure(Throwable t) {
55                     downloadFlowFuture.setException(t);
56                 }
57             };
58         }
59     }
60 
61     OnDevicePersonalizationDataProcessingAsyncCallable(String packageName, Context context) {
62         this(packageName, context, new Injector());
63     }
64 
65     @VisibleForTesting
66     OnDevicePersonalizationDataProcessingAsyncCallable(
67             String packageName, Context context, Injector injector) {
68         mPackageName = packageName;
69         mContext = context;
70         mInjector = injector;
71     }
72 
73     /**
74      * Processes the downloaded files for the given package and stores the data into sqlite vendor
75      * tables.
76      */
77     @Override
78     public ListenableFuture<Boolean> call() {
79         SettableFuture<Boolean> downloadFlowFuture = SettableFuture.create();
80         FutureCallback<DownloadCompletedOutputParcel> callback =
81                 mInjector.getFutureCallback(downloadFlowFuture);
82 
83         sSfo.schedule(ServiceFlowType.DOWNLOAD_FLOW, mPackageName, mContext, callback);
84 
85         return downloadFlowFuture;
86     }
87 }
88