AppCounter.java revision b836da263d8559255c528fe68410649d979cf123
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.app.AppGlobals;
18import android.content.Context;
19import android.content.pm.ApplicationInfo;
20import android.content.pm.PackageManager;
21import android.content.pm.UserInfo;
22import android.os.AsyncTask;
23import android.os.UserHandle;
24import android.os.UserManager;
25
26import java.util.List;
27
28public abstract class AppCounter extends AsyncTask<Void, Void, Integer> {
29
30    protected final PackageManagerWrapper mPm;
31    protected final UserManager mUm;
32
33    public AppCounter(Context context, PackageManagerWrapper packageManager) {
34        mPm = packageManager;
35        mUm = UserManager.get(context);
36    }
37
38    @Override
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
57    protected void onPostExecute(Integer count) {
58        onCountComplete(count);
59    }
60
61    void executeInForeground() {
62        onPostExecute(doInBackground());
63    }
64
65    protected abstract void onCountComplete(int num);
66    protected abstract boolean includeInCount(ApplicationInfo info);
67}
68