1 /* 2 * Copyright (C) 2017 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.settings.applications; 18 19 import android.content.pm.ApplicationInfo; 20 import android.content.pm.PackageManager; 21 import android.content.pm.UserInfo; 22 import android.os.AsyncTask; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 26 import com.android.settingslib.wrapper.PackageManagerWrapper; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** 32 * Lists apps for current user that fit some criteria specified by includeInCount method 33 * implementation. 34 * This class is similar to {@link AppCounter} class, but but builds actual list of apps instead 35 * of just counting them. 36 */ 37 public abstract class AppLister extends AsyncTask<Void, Void, List<UserAppInfo>> { 38 protected final PackageManagerWrapper mPm; 39 protected final UserManager mUm; 40 41 public AppLister(PackageManagerWrapper packageManager, UserManager userManager) { 42 mPm = packageManager; 43 mUm = userManager; 44 } 45 46 @Override 47 protected List<UserAppInfo> doInBackground(Void... params) { 48 final List<UserAppInfo> result = new ArrayList<>(); 49 for (UserInfo user : mUm.getProfiles(UserHandle.myUserId())) { 50 final List<ApplicationInfo> list = 51 mPm.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS 52 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS 53 | (user.isAdmin() ? PackageManager.MATCH_ANY_USER : 0), 54 user.id); 55 for (ApplicationInfo info : list) { 56 if (includeInCount(info)) { 57 result.add(new UserAppInfo(user, info)); 58 } 59 } 60 } 61 return result; 62 } 63 64 @Override 65 protected void onPostExecute(List<UserAppInfo> list) { 66 onAppListBuilt(list); 67 } 68 69 protected abstract void onAppListBuilt(List<UserAppInfo> list); 70 protected abstract boolean includeInCount(ApplicationInfo info); 71 } 72