1 /* 2 * Copyright (C) 2021 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.car.settingslib.applications; 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageManager; 22 import android.content.pm.UserInfo; 23 import android.os.AsyncTask; 24 import android.os.UserHandle; 25 import android.os.UserManager; 26 27 import java.util.List; 28 29 /** 30 * TODO(b/208511815): copied from Settings "as-is"; ideally should be move to SettingsLib, but if 31 * not, we should copy the unit tests as well. 32 */ 33 public abstract class AppCounter extends AsyncTask<Void, Void, Integer> { 34 35 protected final PackageManager mPm; 36 protected final UserManager mUm; 37 AppCounter(Context context, PackageManager packageManager)38 public AppCounter(Context context, PackageManager packageManager) { 39 mPm = packageManager; 40 mUm = (UserManager) context.getSystemService(Context.USER_SERVICE); 41 } 42 43 @Override doInBackground(Void... params)44 protected Integer doInBackground(Void... params) { 45 int count = 0; 46 for (UserInfo user : mUm.getProfiles(UserHandle.myUserId())) { 47 final List<ApplicationInfo> list = 48 mPm.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS 49 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS 50 | (user.isAdmin() ? PackageManager.MATCH_ANY_USER : 0), 51 user.id); 52 for (ApplicationInfo info : list) { 53 if (includeInCount(info)) { 54 count++; 55 } 56 } 57 } 58 return count; 59 } 60 61 @Override onPostExecute(Integer count)62 protected void onPostExecute(Integer count) { 63 onCountComplete(count); 64 } 65 executeInForeground()66 void executeInForeground() { 67 onPostExecute(doInBackground()); 68 } 69 onCountComplete(int num)70 protected abstract void onCountComplete(int num); includeInCount(ApplicationInfo info)71 protected abstract boolean includeInCount(ApplicationInfo info); 72 } 73