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.app.AppGlobals; 18 import android.content.Context; 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 java.util.List; 27 28 public abstract class AppCounter extends AsyncTask<Void, Void, Integer> { 29 30 protected final PackageManagerWrapper mPm; 31 protected final UserManager mUm; 32 AppCounter(Context context, PackageManagerWrapper packageManager)33 public AppCounter(Context context, PackageManagerWrapper packageManager) { 34 mPm = packageManager; 35 mUm = (UserManager) context.getSystemService(Context.USER_SERVICE); 36 } 37 38 @Override doInBackground(Void... params)39 protected Integer doInBackground(Void... params) { 40 int count = 0; 41 for (UserInfo user : mUm.getProfiles(UserHandle.myUserId())) { 42 final List<ApplicationInfo> list = 43 mPm.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS 44 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS 45 | (user.isAdmin() ? PackageManager.MATCH_ANY_USER : 0), 46 user.id); 47 for (ApplicationInfo info : list) { 48 if (includeInCount(info)) { 49 count++; 50 } 51 } 52 } 53 return count; 54 } 55 56 @Override onPostExecute(Integer count)57 protected void onPostExecute(Integer count) { 58 onCountComplete(count); 59 } 60 executeInForeground()61 void executeInForeground() { 62 onPostExecute(doInBackground()); 63 } 64 onCountComplete(int num)65 protected abstract void onCountComplete(int num); includeInCount(ApplicationInfo info)66 protected abstract boolean includeInCount(ApplicationInfo info); 67 } 68