• 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 package com.android.adservices;
17 
18 import android.adservices.adid.AdIdProviderService;
19 import android.adservices.appsetid.AppSetIdProviderService;
20 import android.adservices.cobalt.AdServicesCobaltUploadService;
21 import android.annotation.Nullable;
22 import android.content.pm.ResolveInfo;
23 import android.content.pm.ServiceInfo;
24 
25 import java.util.List;
26 import java.util.stream.Collectors;
27 
28 // TODO(b/295321663): need to split constants into AdServicesCommonConstants so they can be used by
29 // host-side test artifacts.
30 
31 /**
32  * Common constants for AdServices
33  *
34  * @hide
35  */
36 public final class AdServicesCommon {
AdServicesCommon()37     private AdServicesCommon() {}
38 
39     /** Intent action to discover the Topics service in the APK. */
40     public static final String ACTION_TOPICS_SERVICE = "android.adservices.TOPICS_SERVICE";
41 
42     /** Intent action to discover the Custom Audience service in the APK. */
43     public static final String ACTION_CUSTOM_AUDIENCE_SERVICE =
44             "android.adservices.customaudience.CUSTOM_AUDIENCE_SERVICE";
45 
46     /** Intent action to discover the AdSelection service in the APK. */
47     public static final String ACTION_AD_SELECTION_SERVICE =
48             "android.adservices.adselection.AD_SELECTION_SERVICE";
49 
50     /** Intent action to discover the protected signals service in the APK. */
51     public static final String ACTION_PROTECTED_SIGNALS_SERVICE =
52             "android.adservices.signals.PROTECTED_SIGNALS_SERVICE";
53 
54     /** Intent action to discover the Measurement service in the APK. */
55     public static final String ACTION_MEASUREMENT_SERVICE =
56             "android.adservices.MEASUREMENT_SERVICE";
57 
58     /** Intent action to discover the AdId service in the APK. */
59     public static final String ACTION_ADID_SERVICE = "android.adservices.ADID_SERVICE";
60 
61     /** Intent action to discover the AdId Provider service. */
62     public static final String ACTION_ADID_PROVIDER_SERVICE = AdIdProviderService.SERVICE_INTERFACE;
63 
64     /** Intent action to discover the AppSetId service in the APK. */
65     public static final String ACTION_APPSETID_SERVICE = "android.adservices.APPSETID_SERVICE";
66 
67     /** Intent action to discover the AppSetId Provider service. */
68     public static final String ACTION_APPSETID_PROVIDER_SERVICE =
69             AppSetIdProviderService.SERVICE_INTERFACE;
70 
71     /** Intent action to discover the AdServicesCommon service in the APK. */
72     public static final String ACTION_AD_SERVICES_COMMON_SERVICE =
73             "android.adservices.AD_SERVICES_COMMON_SERVICE";
74 
75     /** Intent action to discover the AdServices Cobalt upload service. */
76     public static final String ACTION_AD_SERVICES_COBALT_UPLOAD_SERVICE =
77             AdServicesCobaltUploadService.SERVICE_INTERFACE;
78 
79     /** Intent action to discover the Shell Command service in the APK. */
80     public static final String ACTION_SHELL_COMMAND_SERVICE =
81             "android.adservices.SHELL_COMMAND_SERVICE";
82 
83     /** Intent action to discover OnDevicePersonalization on the device. */
84     public static final String ACTION_ON_DEVICE_PERSONALIZATION_SERVICE =
85             "android.OnDevicePersonalizationService";
86 
87     // Used to differentiate between AdServices APK package name and AdExtServices APK package name.
88     // The AdExtServices APK package name suffix is android.ext.services.
89     public static final String ADSERVICES_APK_PACKAGE_NAME_SUFFIX = "android.adservices.api";
90 
91     /** The package name suffix of the ExtServices APK on R/S */
92     public static final String ADEXTSERVICES_PACKAGE_NAME_SUFFIX = "android.ext.services";
93 
94     /** Suffix for the AdServices APEX package name. */
95     public static final String ADSERVICES_APEX_NAME_SUFFIX = "android.adservices";
96 
97     /**
98      * Suffix for the ExtServices APEX Package name. Used to figure out the installed apex version.
99      */
100     public static final String EXTSERVICES_APEX_NAME_SUFFIX = "android.extservices";
101 
102     /**
103      * Prefix for system properties used for debugging purposes (like simulating unsupported devices
104      * or change some behavior without changing a flag).
105      */
106     public static final String SYSTEM_PROPERTY_FOR_DEBUGGING_PREFIX = "debug.adservices.";
107 
108     /** System property used to simulate AdServices behavior on devices with low memory. */
109     public static final String SYSTEM_PROPERTY_FOR_DEBUGGING_FEATURE_RAM_LOW =
110             SYSTEM_PROPERTY_FOR_DEBUGGING_PREFIX + "low_ram_device";
111 
112     /** The name of System Property for the binder timeout */
113     public static final String BINDER_TIMEOUT_SYSTEM_PROPERTY_NAME = "binder_timeout";
114 
115     /** System property used to allow test to override the binder's timeout. */
116     public static final String SYSTEM_PROPERTY_FOR_DEBUGGING_BINDER_TIMEOUT =
117             SYSTEM_PROPERTY_FOR_DEBUGGING_PREFIX + BINDER_TIMEOUT_SYSTEM_PROPERTY_NAME;
118 
119     /** Path name for Adservice class names */
120     public static final String ADSERVICES_CLASS_PATH_PREFIX = "com.android.adservices.";
121 
122     /** The package name of the active AdServices APK on this device. */
resolveAdServicesService( @ullable List<ResolveInfo> intentResolveInfos, String intentAction)123     public static ServiceInfo resolveAdServicesService(
124             @Nullable List<ResolveInfo> intentResolveInfos, String intentAction) {
125         int size = intentResolveInfos == null ? 0 : intentResolveInfos.size();
126 
127         enforceSingleServiceForAdIdAndAppSetId(intentAction, size);
128 
129         switch (size) {
130             case 0:
131                 LogUtil.e(
132                         "Failed to find resolveInfo for adServices service. Intent action: %s",
133                         intentAction);
134                 return null;
135 
136             case 1:
137                 return intentResolveInfos.get(0).serviceInfo;
138 
139             case 2:
140                 // On T+ devices, we may have two versions of the services present due to
141                 // b/263904312. Only use the service that comes from AdServices APK. The package
142                 // name of AdService is com.[google.]android.adservices.api while the package name
143                 // of ExtServices APK is com.[google.]android.ext.services.
144                 ServiceInfo serviceInfo = getServiceInfoIfAdServices(intentResolveInfos.get(0));
145                 return serviceInfo == null
146                         ? getServiceInfoIfAdServices(intentResolveInfos.get(1))
147                         : serviceInfo;
148 
149             default:
150                 List<String> intents =
151                         intentResolveInfos.stream()
152                                 .filter(s -> s != null && s.serviceInfo != null)
153                                 .map(s -> s.serviceInfo.packageName)
154                                 .collect(Collectors.toList());
155                 LogUtil.e("Found multiple services %s for %s", intents, intentAction);
156                 return null;
157         }
158     }
159 
getServiceInfoIfAdServices(@ullable ResolveInfo resolveInfo)160     private static ServiceInfo getServiceInfoIfAdServices(@Nullable ResolveInfo resolveInfo) {
161         if (resolveInfo == null) {
162             return null;
163         }
164 
165         ServiceInfo serviceInfo = resolveInfo.serviceInfo;
166         if (serviceInfo == null || serviceInfo.packageName == null) {
167             return null;
168         }
169 
170         return serviceInfo.packageName.endsWith(ADSERVICES_APK_PACKAGE_NAME_SUFFIX)
171                 ? serviceInfo
172                 : null;
173     }
174 
175     // It was designed to allow OEM to override the AdId/AppSetId Provider Service in GMS Core.
176     // However, this is never available because if the device has both GMS Core and an
177     // overriding Provider, it falls into this case and returns null. Therefore, let it
178     // explicitly throw in case any OEM wants to actually override it, and we can apply
179     // a change to allow it if needed.
enforceSingleServiceForAdIdAndAppSetId( String intentAction, int numberOfResolvedServices)180     private static void enforceSingleServiceForAdIdAndAppSetId(
181             String intentAction, int numberOfResolvedServices) {
182         if ((ACTION_ADID_PROVIDER_SERVICE.equals(intentAction)
183                         || ACTION_APPSETID_PROVIDER_SERVICE.equals(intentAction))
184                 && numberOfResolvedServices > 1) {
185             throw new IllegalStateException("Found multiple services for " + intentAction);
186         }
187     }
188 }
189