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.pm.ApplicationInfo; 19 import android.content.pm.FeatureFlags; 20 import android.content.pm.FeatureFlagsImpl; 21 import android.content.pm.PackageManager; 22 import android.content.pm.PackageManager.ApplicationInfoFlags; 23 import android.content.pm.UserInfo; 24 import android.os.AsyncTask; 25 import android.os.SystemProperties; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 29 import androidx.annotation.NonNull; 30 import androidx.annotation.VisibleForTesting; 31 32 import com.android.settings.flags.Flags; 33 34 import java.util.List; 35 36 public abstract class AppCounter extends AsyncTask<Void, Void, Integer> { 37 38 protected final PackageManager mPm; 39 protected final UserManager mUm; 40 protected final FeatureFlags mFf; 41 42 @VisibleForTesting AppCounter(@onNull Context context, @NonNull PackageManager packageManager, @NonNull FeatureFlags featureFlags)43 AppCounter(@NonNull Context context, @NonNull PackageManager packageManager, 44 @NonNull FeatureFlags featureFlags) { 45 mPm = packageManager; 46 mUm = context.getSystemService(UserManager.class); 47 mFf = featureFlags; 48 } 49 AppCounter(@onNull Context context, @NonNull PackageManager packageManager)50 public AppCounter(@NonNull Context context, @NonNull PackageManager packageManager) { 51 this(context, packageManager, new FeatureFlagsImpl()); 52 } 53 54 @Override doInBackground(Void... params)55 protected Integer doInBackground(Void... params) { 56 int count = 0; 57 for (UserInfo user : mUm.getProfiles(UserHandle.myUserId())) { 58 long flags = PackageManager.GET_DISABLED_COMPONENTS 59 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS 60 | (isArchivingEnabled() ? PackageManager.MATCH_ARCHIVED_PACKAGES : 0) 61 | (user.isAdmin() ? PackageManager.MATCH_ANY_USER : 0); 62 ApplicationInfoFlags infoFlags = ApplicationInfoFlags.of(flags); 63 final List<ApplicationInfo> list = 64 mPm.getInstalledApplicationsAsUser(infoFlags, user.id); 65 for (ApplicationInfo info : list) { 66 if (includeInCount(info)) { 67 count++; 68 } 69 } 70 } 71 return count; 72 } 73 isArchivingEnabled()74 private boolean isArchivingEnabled() { 75 return mFf.archiving() || Flags.appArchiving(); 76 } 77 78 @Override onPostExecute(Integer count)79 protected void onPostExecute(Integer count) { 80 onCountComplete(count); 81 } 82 executeInForeground()83 void executeInForeground() { 84 onPostExecute(doInBackground()); 85 } 86 onCountComplete(int num)87 protected abstract void onCountComplete(int num); 88 includeInCount(ApplicationInfo info)89 protected abstract boolean includeInCount(ApplicationInfo info); 90 } 91