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.adid;
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 Advertising Id and
34  * limitAdTracking value.
35  *
36  * <p>The implementor of this service needs to override the onGetAdId method and provide a
37  * device-level unique advertising Id and limitAdTracking value on that device.
38  *
39  * @hide
40  */
41 @SystemApi
42 public abstract class AdIdProviderService 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 = "android.adservices.adid.AdIdProviderService";
47 
48     /**
49      * Abstract method which will be overridden by provider to provide the adId. For multi-user,
50      * multi-profiles on-device scenarios, separate instance of service per user is expected to
51      * implement this method.
52      */
53     @NonNull
onGetAdId(int clientUid, @NonNull String clientPackageName)54     public abstract AdId onGetAdId(int clientUid, @NonNull String clientPackageName)
55             throws IOException;
56 
57     private final android.adservices.adid.IAdIdProviderService mInterface =
58             new android.adservices.adid.IAdIdProviderService.Stub() {
59                 @Override
60                 public void getAdIdProvider(
61                         int appUID,
62                         @NonNull String packageName,
63                         @NonNull IGetAdIdProviderCallback resultCallback)
64                         throws RemoteException {
65                     try {
66                         AdId adId = onGetAdId(appUID, packageName);
67                         GetAdIdResult adIdInternal =
68                                 new GetAdIdResult.Builder()
69                                         .setStatusCode(STATUS_SUCCESS)
70                                         .setErrorMessage("")
71                                         .setAdId(adId.getAdId())
72                                         .setLatEnabled(adId.isLimitAdTrackingEnabled())
73                                         .build();
74 
75                         resultCallback.onResult(adIdInternal);
76                     } catch (Throwable e) {
77                         resultCallback.onError(e.getMessage());
78                     }
79                 }
80             };
81 
82     @Nullable
83     @Override
onBind(@ullable Intent intent)84     public final IBinder onBind(@Nullable Intent intent) {
85         return mInterface.asBinder();
86     }
87 }
88