• 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 android.adservices.adid;
18 
19 import static android.adservices.common.AdServicesStatusUtils.STATUS_SUCCESS;
20 
21 import android.adservices.AdServicesFrameworkHelper;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.SdkConstant;
25 import android.annotation.SystemApi;
26 import android.app.Service;
27 import android.content.Intent;
28 import android.os.IBinder;
29 import android.os.RemoteException;
30 
31 import java.io.IOException;
32 
33 /**
34  * Abstract Base class for provider service to implement generation of Advertising Id and
35  * limitAdTracking value.
36  *
37  * <p>The implementor of this service needs to override the onGetAdId method and provide a
38  * device-level unique advertising Id and limitAdTracking value on that device.
39  *
40  * @hide
41  */
42 @SystemApi
43 public abstract class AdIdProviderService extends Service {
44 
45     /** The intent that the service must respond to. Add it to the intent filter of the service. */
46     @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
47     public static final String SERVICE_INTERFACE = "android.adservices.adid.AdIdProviderService";
48 
49     /**
50      * Abstract method which will be overridden by provider to provide the adId. For multi-user,
51      * multi-profiles on-device scenarios, separate instance of service per user is expected to
52      * implement this method.
53      */
54     @NonNull
onGetAdId(int clientUid, @NonNull String clientPackageName)55     public abstract AdId onGetAdId(int clientUid, @NonNull String clientPackageName)
56             throws IOException;
57 
58     private final android.adservices.adid.IAdIdProviderService mInterface =
59             new android.adservices.adid.IAdIdProviderService.Stub() {
60                 @Override
61                 public void getAdIdProvider(
62                         int appUID,
63                         @NonNull String packageName,
64                         @NonNull IGetAdIdProviderCallback resultCallback)
65                         throws RemoteException {
66                     try {
67                         AdId adId = onGetAdId(appUID, packageName);
68                         GetAdIdResult adIdInternal =
69                                 new GetAdIdResult.Builder()
70                                         .setStatusCode(STATUS_SUCCESS)
71                                         .setErrorMessage("")
72                                         .setAdId(adId.getAdId())
73                                         .setLatEnabled(adId.isLimitAdTrackingEnabled())
74                                         .build();
75 
76                         resultCallback.onResult(adIdInternal);
77                     } catch (Throwable e) {
78                         resultCallback.onError(
79                                 AdServicesFrameworkHelper.getExceptionStackTraceString(e));
80                     }
81                 }
82             };
83 
84     @Nullable
85     @Override
onBind(@ullable Intent intent)86     public final IBinder onBind(@Nullable Intent intent) {
87         return mInterface.asBinder();
88     }
89 }
90