1 /* 2 * Copyright (C) 2023 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 com.android.adservices.common; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ResolveInfo; 24 import android.content.pm.ServiceInfo; 25 import android.util.Log; 26 27 import com.android.adservices.AdServicesCommon; 28 import com.android.compatibility.common.util.ShellUtils; 29 30 import java.util.List; 31 32 /** Class to place Adservices CTS related helper method. */ 33 public class AdservicesTestHelper { 34 // Used to get the package name. Copied over from com.android.adservices.AdServicesCommon 35 private static final String TOPICS_SERVICE_NAME = "android.adservices.TOPICS_SERVICE"; 36 private static final String DEFAULT_LOG_TAG = "adservices"; 37 private static final String FORCE_KILL_PROCESS_COMMAND = "am force-stop"; 38 39 /** 40 * Used to get the package name. Copied over from com.android.adservices.AndroidServiceBinder 41 * 42 * @param context the context 43 * @param logTag the tag used for logging 44 * @return Adservices package name 45 */ getAdServicesPackageName( @onNull Context context, @NonNull String logTag)46 public static String getAdServicesPackageName( 47 @NonNull Context context, @NonNull String logTag) { 48 final Intent intent = new Intent(TOPICS_SERVICE_NAME); 49 final List<ResolveInfo> resolveInfos = 50 context.getPackageManager() 51 .queryIntentServices(intent, PackageManager.MATCH_SYSTEM_ONLY); 52 final ServiceInfo serviceInfo = 53 AdServicesCommon.resolveAdServicesService(resolveInfos, TOPICS_SERVICE_NAME); 54 if (serviceInfo == null) { 55 Log.e(logTag, "Failed to find serviceInfo for adServices service"); 56 return null; 57 } 58 59 return serviceInfo.packageName; 60 } 61 62 /** 63 * Used to get the package name. An overloading method of {@code 64 * getAdservicesPackageName(context, logTag)} by using {@code DEFAULT_LOG_TAG}. 65 * 66 * @param context the context 67 * @return Adservices package name 68 */ getAdServicesPackageName(@onNull Context context)69 public static String getAdServicesPackageName(@NonNull Context context) { 70 return getAdServicesPackageName(context, DEFAULT_LOG_TAG); 71 } 72 73 /** 74 * Kill the Adservices process. 75 * 76 * @param context the context used to get Adservices package name. 77 * @param logTag the tag used for logging 78 */ killAdservicesProcess(@onNull Context context, @NonNull String logTag)79 public static void killAdservicesProcess(@NonNull Context context, @NonNull String logTag) { 80 ShellUtils.runShellCommand( 81 "%s %s", FORCE_KILL_PROCESS_COMMAND, getAdServicesPackageName(context, logTag)); 82 } 83 84 /** 85 * Kill the Adservices process. An overloading method of {@code killAdservicesProcess(context, 86 * logTag)} by using {@code DEFAULT_LOG_TAG}. 87 * 88 * @param context the context used to get Adservices package name. 89 */ killAdservicesProcess(@onNull Context context)90 public static void killAdservicesProcess(@NonNull Context context) { 91 killAdservicesProcess(context, DEFAULT_LOG_TAG); 92 } 93 94 /** 95 * Kill the Adservices process. An overloading method of {@code killAdservicesProcess(context, 96 * logTag)} by using Adservices package name directly. 97 * 98 * @param adservicesPackageName the Adservices package name. 99 */ killAdservicesProcess(@onNull String adservicesPackageName)100 public static void killAdservicesProcess(@NonNull String adservicesPackageName) { 101 ShellUtils.runShellCommand("%s %s", FORCE_KILL_PROCESS_COMMAND, adservicesPackageName); 102 } 103 104 /** 105 * Check whether the device is supported. Adservices doesn't support non-phone device. 106 * 107 * @return if the device is supported. 108 * @deprecated use {@link AdServicesSupportRule} instead. 109 */ 110 @Deprecated isDeviceSupported()111 public static boolean isDeviceSupported() { 112 return AdServicesSupportRule.isDeviceSupported(); 113 } 114 } 115