1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.packageinstaller.permission.service;
18
19import android.content.pm.ApplicationInfo;
20import android.content.pm.PackageInfo;
21import android.content.pm.PackageManager;
22import android.content.pm.permission.RuntimePermissionPresentationInfo;
23import android.permissionpresenterservice.RuntimePermissionPresenterService;
24import android.util.ArraySet;
25import android.util.Log;
26import com.android.packageinstaller.permission.model.AppPermissionGroup;
27import com.android.packageinstaller.permission.model.AppPermissions;
28import com.android.packageinstaller.permission.utils.Utils;
29
30import java.util.ArrayList;
31import java.util.List;
32
33/**
34 * Service that provides presentation information for runtime permissions.
35 */
36public final class RuntimePermissionPresenterServiceImpl extends RuntimePermissionPresenterService {
37    private static final String LOG_TAG = "PermissionPresenter";
38
39    @Override
40    public List<RuntimePermissionPresentationInfo> onGetAppPermissions(String packageName) {
41        final PackageInfo packageInfo;
42        try {
43            packageInfo = getPackageManager().getPackageInfo(packageName,
44                    PackageManager.GET_PERMISSIONS);
45        } catch (PackageManager.NameNotFoundException e) {
46            Log.e(LOG_TAG, "Error getting package:" + packageName, e);
47            return null;
48        }
49
50        List<RuntimePermissionPresentationInfo> permissions = new ArrayList<>();
51
52        AppPermissions appPermissions =  new AppPermissions(this, packageInfo, null, false, null);
53        for (AppPermissionGroup group : appPermissions.getPermissionGroups()) {
54            if (Utils.shouldShowPermission(group, packageName)) {
55                final boolean granted = group.areRuntimePermissionsGranted();
56                final boolean standard = Utils.OS_PKG.equals(group.getDeclaringPackage());
57                RuntimePermissionPresentationInfo permission =
58                        new RuntimePermissionPresentationInfo(group.getLabel(),
59                                granted, standard);
60                permissions.add(permission);
61            }
62        }
63
64        return permissions;
65    }
66
67    @Override
68    public List<ApplicationInfo> onGetAppsUsingPermissions(boolean system) {
69        final List<ApplicationInfo> appInfos = Utils.getAllInstalledApplications(this);
70        if (appInfos == null || appInfos.isEmpty()) {
71            return null;
72        }
73        List<ApplicationInfo> appsResult = new ArrayList<>();
74        ArraySet<String> launcherPackages = Utils.getLauncherPackages(this);
75        final int appInfosSize = appInfos.size();
76        for (int i = 0; i < appInfosSize; i++) {
77            ApplicationInfo appInfo = appInfos.get(i);
78            final String packageName = appInfo.packageName;
79            final PackageInfo packageInfo;
80            try {
81                packageInfo = getPackageManager().getPackageInfo(
82                        packageName, PackageManager.GET_PERMISSIONS);
83            } catch (PackageManager.NameNotFoundException e) {
84                Log.e(LOG_TAG, "Error getting package info for:" + packageName, e);
85                continue;
86
87            }
88            AppPermissions appPermissions =  new AppPermissions(this,
89                    packageInfo, null, false, null);
90            boolean shouldShow = false;
91
92
93            for (AppPermissionGroup group : appPermissions.getPermissionGroups()) {
94                if (Utils.shouldShowPermission(group, packageName)) {
95                    shouldShow = true;
96                    break;
97                }
98            }
99            if (shouldShow) {
100                if (Utils.isSystem(appPermissions, launcherPackages) == system) {
101                    appsResult.add(appInfo);
102                }
103            }
104        }
105        return appsResult;
106    }
107}
108