InstalledAppCounter.java revision dee1a22c45c78dd1d4a681314045b0757b63623d
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.Intent;
19import android.content.pm.ApplicationInfo;
20import android.content.pm.ResolveInfo;
21import android.content.pm.PackageManager;
22import android.os.UserHandle;
23
24import com.android.settingslib.wrapper.PackageManagerWrapper;
25
26import java.util.List;
27
28public abstract class InstalledAppCounter extends AppCounter {
29
30    /**
31     * Count all installed packages, irrespective of install reason.
32     */
33    public static final int IGNORE_INSTALL_REASON = -1;
34
35    private final int mInstallReason;
36
37    public InstalledAppCounter(Context context, int installReason,
38            PackageManagerWrapper packageManager) {
39        super(context, packageManager);
40        mInstallReason = installReason;
41    }
42
43    @Override
44    protected boolean includeInCount(ApplicationInfo info) {
45        return includeInCount(mInstallReason, mPm, info);
46    }
47
48    public static boolean includeInCount(int installReason, PackageManagerWrapper pm,
49            ApplicationInfo info) {
50        final int userId = UserHandle.getUserId(info.uid);
51        if (installReason != IGNORE_INSTALL_REASON
52                && pm.getInstallReason(info.packageName,
53                        new UserHandle(userId)) != installReason) {
54            return false;
55        }
56        if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
57            return true;
58        }
59        if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
60            return true;
61        }
62        Intent launchIntent = new Intent(Intent.ACTION_MAIN, null)
63                .addCategory(Intent.CATEGORY_LAUNCHER)
64                .setPackage(info.packageName);
65        List<ResolveInfo> intents = pm.queryIntentActivitiesAsUser(
66                launchIntent,
67                PackageManager.GET_DISABLED_COMPONENTS
68                        | PackageManager.MATCH_DIRECT_BOOT_AWARE
69                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
70                userId);
71        return intents != null && intents.size() != 0;
72    }
73}
74