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