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.IPackageManager;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ParceledListSlice;
23 import android.content.pm.UserInfo;
24 import android.os.AsyncTask;
25 import android.os.RemoteException;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 
29 public abstract class AppCounter extends AsyncTask<Void, Void, Integer> {
30 
31     protected final PackageManager mPm;
32     protected final IPackageManager mIpm;
33     protected final UserManager mUm;
34 
AppCounter(Context context)35     public AppCounter(Context context) {
36         mPm = context.getPackageManager();
37         mIpm = AppGlobals.getPackageManager();
38         mUm = UserManager.get(context);
39     }
40 
41     @Override
doInBackground(Void... params)42     protected Integer doInBackground(Void... params) {
43         int count = 0;
44         for (UserInfo user : mUm.getProfiles(UserHandle.myUserId())) {
45             try {
46                 @SuppressWarnings("unchecked")
47                 ParceledListSlice<ApplicationInfo> list =
48                         mIpm.getInstalledApplications(PackageManager.GET_DISABLED_COMPONENTS
49                                 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
50                                 | (user.isAdmin() ? PackageManager.GET_UNINSTALLED_PACKAGES : 0),
51                                 user.id);
52                 for (ApplicationInfo info : list.getList()) {
53                     if (includeInCount(info)) {
54                         count++;
55                     }
56                 }
57             } catch (RemoteException e) {
58             }
59         }
60         return count;
61     }
62 
63     @Override
onPostExecute(Integer count)64     protected void onPostExecute(Integer count) {
65         onCountComplete(count);
66     }
67 
onCountComplete(int num)68     protected abstract void onCountComplete(int num);
includeInCount(ApplicationInfo info)69     protected abstract boolean includeInCount(ApplicationInfo info);
70 }
71