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