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 android.adservices.appsetid;
18 
19 import static android.adservices.common.AdServicesStatusUtils.STATUS_SUCCESS;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.annotation.SdkConstant;
24 import android.annotation.SystemApi;
25 import android.app.Service;
26 import android.content.Intent;
27 import android.os.IBinder;
28 import android.os.RemoteException;
29 
30 import java.io.IOException;
31 
32 /**
33  * Abstract Base class for provider service to implement generation of AppSetId with appropriate
34  * appSetId scope value.
35  *
36  * <p>The implementor of this service needs to override the onGetAppSetIdProvider method and provide
37  * an app-scoped or developer-account scoped unique appSetId.
38  *
39  * @hide
40  */
41 @SystemApi
42 public abstract class AppSetIdProviderService extends Service {
43 
44     /** The intent that the service must respond to. Add it to the intent filter of the service. */
45     @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
46     public static final String SERVICE_INTERFACE =
47             "android.adservices.appsetid.AppSetIdProviderService";
48 
49     /** Abstract method which will be overridden by provider to provide the appsetid. */
50     @NonNull
onGetAppSetId(int clientUid, @NonNull String clientPackageName)51     public abstract AppSetId onGetAppSetId(int clientUid, @NonNull String clientPackageName)
52             throws IOException;
53 
54     private final android.adservices.appsetid.IAppSetIdProviderService mInterface =
55             new android.adservices.appsetid.IAppSetIdProviderService.Stub() {
56                 @Override
57                 public void getAppSetId(
58                         int appUID,
59                         @NonNull String packageName,
60                         @NonNull IGetAppSetIdProviderCallback resultCallback)
61                         throws RemoteException {
62                     try {
63                         AppSetId appsetId = onGetAppSetId(appUID, packageName);
64                         GetAppSetIdResult appsetIdInternal =
65                                 new GetAppSetIdResult.Builder()
66                                         .setStatusCode(STATUS_SUCCESS)
67                                         .setErrorMessage("")
68                                         .setAppSetId(appsetId.getId())
69                                         .setAppSetIdScope(appsetId.getScope())
70                                         .build();
71 
72                         resultCallback.onResult(appsetIdInternal);
73                     } catch (Throwable e) {
74                         resultCallback.onError(e.getMessage());
75                     }
76                 }
77             };
78 
79     @Nullable
80     @Override
onBind(@ullable Intent intent)81     public final IBinder onBind(@Nullable Intent intent) {
82         return mInterface.asBinder();
83     }
84 }
85