AppWithAdminGrantedPermissionsCounter.java revision dee1a22c45c78dd1d4a681314045b0757b63623d
1/* 2 * Copyright (C) 2017 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.admin.DevicePolicyManager; 18import android.content.Context; 19import android.content.pm.ApplicationInfo; 20import android.content.pm.PackageManager; 21import android.os.Build; 22import android.os.RemoteException; 23import android.os.UserHandle; 24 25import com.android.settings.wrapper.DevicePolicyManagerWrapper; 26import com.android.settings.wrapper.IPackageManagerWrapper; 27import com.android.settingslib.wrapper.PackageManagerWrapper; 28 29/** 30 * Counts installed apps across all users that have been granted one or more specific permissions by 31 * the admin. 32 */ 33public abstract class AppWithAdminGrantedPermissionsCounter extends AppCounter { 34 35 private final String[] mPermissions; 36 private final IPackageManagerWrapper mPackageManagerService; 37 private final DevicePolicyManagerWrapper mDevicePolicyManager; 38 39 public AppWithAdminGrantedPermissionsCounter(Context context, String[] permissions, 40 PackageManagerWrapper packageManager, IPackageManagerWrapper packageManagerService, 41 DevicePolicyManagerWrapper devicePolicyManager) { 42 super(context, packageManager); 43 mPermissions = permissions; 44 mPackageManagerService = packageManagerService; 45 mDevicePolicyManager = devicePolicyManager; 46 } 47 48 @Override 49 protected boolean includeInCount(ApplicationInfo info) { 50 return includeInCount(mPermissions, mDevicePolicyManager, mPm, mPackageManagerService, 51 info); 52 } 53 54 public static boolean includeInCount(String[] permissions, 55 DevicePolicyManagerWrapper devicePolicyManager, PackageManagerWrapper packageManager, 56 IPackageManagerWrapper packageManagerService, ApplicationInfo info) { 57 if (info.targetSdkVersion >= Build.VERSION_CODES.M) { 58 // The app uses run-time permissions. Check whether one or more of the permissions were 59 // granted by enterprise policy. 60 for (final String permission : permissions) { 61 if (devicePolicyManager.getPermissionGrantState(null /* admin */, info.packageName, 62 permission) == DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED) { 63 return true; 64 } 65 } 66 return false; 67 } 68 69 // The app uses install-time permissions. Check whether the app requested one or more of the 70 // permissions and was installed by enterprise policy, implicitly granting permissions. 71 if (packageManager.getInstallReason(info.packageName, 72 new UserHandle(UserHandle.getUserId(info.uid))) 73 != PackageManager.INSTALL_REASON_POLICY) { 74 return false; 75 } 76 try { 77 for (final String permission : permissions) { 78 if (packageManagerService.checkUidPermission(permission, info.uid) 79 == PackageManager.PERMISSION_GRANTED) { 80 return true; 81 } 82 } 83 } catch (RemoteException exception) { 84 } 85 return false; 86 } 87} 88