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 android.app; 18 19 import static android.Manifest.permission.GET_BACKGROUND_INSTALLED_PACKAGES; 20 import static android.annotation.SystemApi.Client.PRIVILEGED_APPS; 21 22 import android.annotation.FlaggedApi; 23 import android.annotation.NonNull; 24 import android.annotation.RequiresPermission; 25 import android.annotation.SystemApi; 26 import android.annotation.SystemService; 27 import android.content.Context; 28 import android.content.pm.IBackgroundInstallControlService; 29 import android.content.pm.PackageInfo; 30 import android.content.pm.PackageManager; 31 import android.os.RemoteException; 32 import android.os.ServiceManager; 33 34 import java.util.List; 35 36 /** 37 * BackgroundInstallControlManager client allows apps to query apps installed in background. 38 * 39 * <p>Any applications that was installed without an accompanying installer UI activity paired 40 * with recorded user interaction event is considered background installed. This is determined by 41 * analysis of user-activity logs. 42 * 43 * <p>Warning: BackgroundInstallControl should not be considered a definitive 44 * authority of identifying background installed applications. Consumers can use this as a 45 * supplementary signal, but must perform additional due diligence to confirm the install nature 46 * of the package. 47 * 48 * @hide 49 */ 50 @FlaggedApi(Flags.FLAG_BIC_CLIENT) 51 @SystemApi(client = PRIVILEGED_APPS) 52 @SystemService(Context.BACKGROUND_INSTALL_CONTROL_SERVICE) 53 public final class BackgroundInstallControlManager { 54 55 private static final String TAG = "BackgroundInstallControlManager"; 56 private static IBackgroundInstallControlService sService; 57 private final Context mContext; 58 BackgroundInstallControlManager(Context context)59 BackgroundInstallControlManager(Context context) { 60 mContext = context; 61 } 62 getService()63 private static IBackgroundInstallControlService getService() { 64 if (sService == null) { 65 sService = 66 IBackgroundInstallControlService.Stub.asInterface( 67 ServiceManager.getService(Context.BACKGROUND_INSTALL_CONTROL_SERVICE)); 68 } 69 return sService; 70 } 71 72 /** 73 * Returns a full list of {@link PackageInfo} of apps currently installed for the current user 74 * that are considered installed in the background. 75 * 76 * <p>Refer to top level doc {@link BackgroundInstallControlManager} for more details on 77 * background-installed applications. 78 * <p> 79 * 80 * @param flags - Flags will be used to call 81 * {@link PackageManager#getInstalledPackages(PackageInfoFlags)} to retrieve installed packages. 82 * @return A list of packages retrieved from {@link PackageManager} with non-background 83 * installed app filter applied. 84 * 85 * @hide 86 */ 87 @FlaggedApi(Flags.FLAG_BIC_CLIENT) 88 @SystemApi 89 @RequiresPermission(GET_BACKGROUND_INSTALLED_PACKAGES) getBackgroundInstalledPackages( @ackageManager.PackageInfoFlagsBits long flags)90 public @NonNull List<PackageInfo> getBackgroundInstalledPackages( 91 @PackageManager.PackageInfoFlagsBits long flags) { 92 List<PackageInfo> backgroundInstalledPackages; 93 try { 94 return getService() 95 .getBackgroundInstalledPackages(flags, mContext.getUserId()) 96 .getList(); 97 } catch (RemoteException e) { 98 throw e.rethrowFromSystemServer(); 99 } 100 } 101 102 } 103