1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.applications; 16 17 import android.content.Context; 18 import android.content.Intent; 19 import android.content.pm.ApplicationInfo; 20 import android.content.pm.FeatureFlags; 21 import android.content.pm.FeatureFlagsImpl; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ResolveInfo; 24 import android.os.UserHandle; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.VisibleForTesting; 28 29 import java.util.List; 30 31 public abstract class InstalledAppCounter extends AppCounter { 32 33 /** 34 * Count all installed packages, irrespective of install reason. 35 */ 36 public static final int IGNORE_INSTALL_REASON = -1; 37 38 private final int mInstallReason; 39 InstalledAppCounter(@onNull Context context, int installReason, @NonNull PackageManager packageManager)40 public InstalledAppCounter(@NonNull Context context, int installReason, 41 @NonNull PackageManager packageManager) { 42 this(context, installReason, packageManager, new FeatureFlagsImpl()); 43 } 44 45 @VisibleForTesting InstalledAppCounter(@onNull Context context, int installReason, @NonNull PackageManager packageManager, @NonNull FeatureFlags featureFlags)46 InstalledAppCounter(@NonNull Context context, int installReason, 47 @NonNull PackageManager packageManager, @NonNull FeatureFlags featureFlags) { 48 super(context, packageManager, featureFlags); 49 mInstallReason = installReason; 50 } 51 52 @Override includeInCount(ApplicationInfo info)53 protected boolean includeInCount(ApplicationInfo info) { 54 return includeInCount(mInstallReason, mPm, info); 55 } 56 includeInCount(int installReason, PackageManager pm, ApplicationInfo info)57 public static boolean includeInCount(int installReason, PackageManager pm, 58 ApplicationInfo info) { 59 final int userId = UserHandle.getUserId(info.uid); 60 if (installReason != IGNORE_INSTALL_REASON 61 && pm.getInstallReason(info.packageName, 62 new UserHandle(userId)) != installReason) { 63 return false; 64 } 65 if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) { 66 return true; 67 } 68 if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { 69 return true; 70 } 71 Intent launchIntent = new Intent(Intent.ACTION_MAIN, null) 72 .addCategory(Intent.CATEGORY_LAUNCHER) 73 .setPackage(info.packageName); 74 List<ResolveInfo> intents = pm.queryIntentActivitiesAsUser( 75 launchIntent, 76 PackageManager.GET_DISABLED_COMPONENTS 77 | PackageManager.MATCH_DIRECT_BOOT_AWARE 78 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, 79 userId); 80 return intents != null && intents.size() != 0; 81 } 82 } 83