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.service.measurement.util; 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageManager; 22 import android.net.Uri; 23 24 import com.android.adservices.service.common.compat.PackageManagerCompatUtils; 25 26 import java.util.List; 27 import java.util.stream.Collectors; 28 29 /** Application utilities for measurement. */ 30 // TODO(b/311183933): Remove passed in Context from static method. 31 @SuppressWarnings("AvoidStaticContext") 32 public class Applications { 33 public static final String ANDROID_APP_SCHEME = "android-app"; 34 public static final String SCHEME_DELIMITER = "://"; 35 36 /** 37 * @param context the context of the application. 38 * @return the list of currently installed applications. 39 */ getCurrentInstalledApplicationsList(Context context)40 public static List<Uri> getCurrentInstalledApplicationsList(Context context) { 41 PackageManager packageManager = context.getPackageManager(); 42 List<ApplicationInfo> applicationInfoList = 43 PackageManagerCompatUtils.getInstalledApplications( 44 packageManager, PackageManager.GET_META_DATA); 45 46 return applicationInfoList.stream() 47 .map( 48 applicationInfo -> 49 Uri.parse( 50 ANDROID_APP_SCHEME 51 + SCHEME_DELIMITER 52 + applicationInfo.packageName)) 53 .collect(Collectors.toList()); 54 } 55 56 /** 57 * @param context the context of the application. 58 * @param appDestinations the list of apps to check install status. 59 * @return true if any of the apps are installed currently. 60 */ anyAppsInstalled(Context context, List<Uri> appDestinations)61 public static boolean anyAppsInstalled(Context context, List<Uri> appDestinations) { 62 return appDestinations.stream() 63 .anyMatch( 64 uri -> { 65 try { 66 context.getPackageManager().getApplicationInfo(uri.getHost(), 0); 67 return true; 68 } catch (PackageManager.NameNotFoundException e) { 69 return false; 70 } 71 }); 72 } 73 } 74