• 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.content.pm.ResolveInfo;
21 import android.content.pm.ServiceInfo;
22 
23 import java.util.List;
24 
25 /**
26  * Common constants for AdServices
27  *
28  * @hide
29  */
30 public class AdServicesCommon {
AdServicesCommon()31     private AdServicesCommon() {}
32 
33     /** Intent action to discover the Topics service in the APK. */
34     public static final String ACTION_TOPICS_SERVICE = "android.adservices.TOPICS_SERVICE";
35 
36     /** Intent action to discover the Custom Audience service in the APK. */
37     public static final String ACTION_CUSTOM_AUDIENCE_SERVICE =
38             "android.adservices.customaudience.CUSTOM_AUDIENCE_SERVICE";
39 
40     /** Intent action to discover the AdSelection service in the APK. */
41     public static final String ACTION_AD_SELECTION_SERVICE =
42             "android.adservices.adselection.AD_SELECTION_SERVICE";
43 
44     /** Intent action to discover the Measurement service in the APK. */
45     public static final String ACTION_MEASUREMENT_SERVICE =
46             "android.adservices.MEASUREMENT_SERVICE";
47 
48     /** Intent action to discover the AdId service in the APK. */
49     public static final String ACTION_ADID_SERVICE = "android.adservices.ADID_SERVICE";
50 
51     /** Intent action to discover the AdId Provider service. */
52     public static final String ACTION_ADID_PROVIDER_SERVICE = AdIdProviderService.SERVICE_INTERFACE;
53 
54     /** Intent action to discover the AppSetId service in the APK. */
55     public static final String ACTION_APPSETID_SERVICE = "android.adservices.APPSETID_SERVICE";
56 
57     /** Intent action to discover the AppSetId Provider service. */
58     public static final String ACTION_APPSETID_PROVIDER_SERVICE =
59             AppSetIdProviderService.SERVICE_INTERFACE;
60 
61     /** Intent action to discover the AdServicesCommon service in the APK. */
62     public static final String ACTION_AD_SERVICES_COMMON_SERVICE =
63             "android.adservices.AD_SERVICES_COMMON_SERVICE";
64 
65     // Used to differentiate between AdServices APK package name and AdExtServices APK package name.
66     // The AdExtServices APK package name suffix is android.ext.services.
67     public static final String ADSERVICES_APK_PACKAGE_NAME_SUFFIX = "android.adservices";
68 
69     /** The package name suffix of the AdServices APK within the ExtServices apex on R/S */
70     public static final String ADEXTSERVICES_PACKAGE_NAME_SUFFIX = "android.ext.adservices.api";
71 
72     /**
73      * Suffix for the ExtServices APEX Package name. Used to figure out the installed apex version.
74      */
75     public static final String EXTSERVICES_APEX_NAME_SUFFIX = "android.extservices";
76 
77     /** The package name of the active AdServices APK on this device. */
resolveAdServicesService( List<ResolveInfo> intentResolveInfos, String intentAction)78     public static ServiceInfo resolveAdServicesService(
79             List<ResolveInfo> intentResolveInfos, String intentAction) {
80         if (intentResolveInfos == null || intentResolveInfos.isEmpty()) {
81             LogUtil.e(
82                     "Failed to find resolveInfo for adServices service. Intent action: "
83                             + intentAction);
84             return null;
85         }
86 
87         // On T+ devices, we may have two versions of the services present due to
88         // b/263904312.
89         if (intentResolveInfos.size() > 2) {
90             StringBuilder intents = new StringBuilder("");
91             for (ResolveInfo intentResolveInfo : intentResolveInfos) {
92                 if (intentResolveInfo != null && intentResolveInfo.serviceInfo != null) {
93                     intents.append(intentResolveInfo.serviceInfo.packageName);
94                 }
95             }
96             LogUtil.e("Found multiple services " + intents + " for " + intentAction);
97             return null;
98         }
99 
100         // On T+ devices, only use the service that comes from AdServices APK. The package name of
101         // AdService is com.[google.]android.adservices while the package name of AdExtServices APK
102         // is com.[google.]android.ext.adservices.
103         ServiceInfo serviceInfo = null;
104 
105         // We have already checked if there are 0 OR more than 2 services returned.
106         switch (intentResolveInfos.size()) {
107             case 2:
108                 // In the case of 2, always use the one from AdServicesApk.
109                 if (intentResolveInfos.get(0) != null
110                         && intentResolveInfos.get(0).serviceInfo != null
111                         && intentResolveInfos.get(0).serviceInfo.packageName != null
112                         && intentResolveInfos
113                                 .get(0)
114                                 .serviceInfo
115                                 .packageName
116                                 .contains(ADSERVICES_APK_PACKAGE_NAME_SUFFIX)) {
117                     serviceInfo = intentResolveInfos.get(0).serviceInfo;
118                 } else if (intentResolveInfos.get(1) != null
119                         && intentResolveInfos.get(1).serviceInfo != null
120                         && intentResolveInfos.get(1).serviceInfo.packageName != null
121                         && intentResolveInfos
122                                 .get(1)
123                                 .serviceInfo
124                                 .packageName
125                                 .contains(ADSERVICES_APK_PACKAGE_NAME_SUFFIX)) {
126                     serviceInfo = intentResolveInfos.get(1).serviceInfo;
127                 }
128                 break;
129 
130             case 1:
131                 serviceInfo = intentResolveInfos.get(0).serviceInfo;
132                 break;
133         }
134         return serviceInfo;
135     }
136 }
137