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.data.common; 18 19 import android.content.pm.PackageManager; 20 21 import androidx.annotation.NonNull; 22 23 import com.android.adservices.service.Flags; 24 import com.android.adservices.service.common.AllowLists; 25 import com.android.adservices.service.common.compat.PackageManagerCompatUtils; 26 27 import java.util.List; 28 import java.util.Set; 29 import java.util.stream.Collectors; 30 31 /** Contains utility functions for cleaning up data from disallowed apps. */ 32 public final class CleanupUtils { 33 34 /** 35 * Takes a list of packages which may or may not be allowed to use PPAPIs and removes all the 36 * packages that are allowed to use PPAPIs. 37 * 38 * @param packages a list of package names 39 * @param packageManager the package manager 40 * @param flags the adservices flags 41 */ removeAllowedPackages( @onNull List<String> packages, @NonNull PackageManager packageManager, @NonNull Flags flags)42 public static void removeAllowedPackages( 43 @NonNull List<String> packages, 44 @NonNull PackageManager packageManager, 45 @NonNull Flags flags) { 46 if (!packages.isEmpty()) { 47 Set<String> allowedPackages = 48 PackageManagerCompatUtils.getInstalledApplications(packageManager, 0).stream() 49 .map(applicationInfo -> applicationInfo.packageName) 50 .collect(Collectors.toSet()); 51 52 String appAllowList = flags.getPpapiAppAllowList(); 53 if (!AllowLists.doesAllowListAllowAll(appAllowList)) { 54 allowedPackages.retainAll(AllowLists.splitAllowList(appAllowList)); 55 } 56 57 // Packages must be both installed and allowlisted, or else they should be removed 58 packages.removeAll(allowedPackages); 59 } 60 } 61 } 62