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 public class Applications { 31 public static final String ANDROID_APP_SCHEME = "android-app"; 32 public static final String SCHEME_DELIMITER = "://"; 33 34 /** 35 * @param context the context of the application. 36 * @return the list of currently installed applications. 37 */ getCurrentInstalledApplicationsList(Context context)38 public static List<Uri> getCurrentInstalledApplicationsList(Context context) { 39 PackageManager packageManager = context.getPackageManager(); 40 List<ApplicationInfo> applicationInfoList = 41 PackageManagerCompatUtils.getInstalledApplications( 42 packageManager, PackageManager.GET_META_DATA); 43 44 return applicationInfoList.stream() 45 .map( 46 applicationInfo -> 47 Uri.parse( 48 ANDROID_APP_SCHEME 49 + SCHEME_DELIMITER 50 + applicationInfo.packageName)) 51 .collect(Collectors.toList()); 52 } 53 54 /** 55 * @param context the context of the application. 56 * @param appDestinations the list of apps to check install status. 57 * @return true if any of the apps are installed currently. 58 */ anyAppsInstalled(Context context, List<Uri> appDestinations)59 public static boolean anyAppsInstalled(Context context, List<Uri> appDestinations) { 60 return appDestinations.stream() 61 .anyMatch( 62 uri -> { 63 try { 64 context.getPackageManager().getApplicationInfo(uri.getHost(), 0); 65 return true; 66 } catch (PackageManager.NameNotFoundException e) { 67 return false; 68 } 69 }); 70 } 71 } 72