1 /* 2 * Copyright (C) 2019 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 17 package com.android.permissioncontroller.permission.model.livedatatypes 18 19 import android.app.Application 20 import android.content.pm.PackageManager 21 import android.content.pm.PermissionInfo 22 23 /** 24 * A light version of the system PermissionInfo 25 * 26 * @param name The name of this permission 27 * @param packageName The name of the package which defines this permission 28 * @param group The optional name of the group this permission is in 29 * @param backgroundPermission The background permission associated with this permission 30 * @param protection The protection level of this permission 31 * @param protection Extra information about the protection of this permission 32 * @param flags The system flags of this permission 33 */ 34 data class LightPermInfo( 35 val name: String, 36 val packageName: String, 37 val group: String?, 38 val backgroundPermission: String?, 39 val protection: Int, 40 val protectionFlags: Int, 41 val flags: Int 42 ) { 43 constructor( 44 permInfo: PermissionInfo 45 ) : this( 46 permInfo.name, 47 permInfo.packageName, 48 permInfo.group, 49 permInfo.backgroundPermission, 50 permInfo.protection, 51 permInfo.protectionFlags, 52 permInfo.flags 53 ) 54 55 /** 56 * Gets the PermissionInfo for this permission from the system. 57 * 58 * @param app The current application, which will be used to get the PermissionInfo 59 * @return The PermissionInfo corresponding to this permission, or null, if no such permission 60 * exists 61 */ toPermissionInfonull62 fun toPermissionInfo(app: Application): PermissionInfo? { 63 try { 64 return app.packageManager.getPermissionInfo(name, 0) 65 } catch (e: PackageManager.NameNotFoundException) {} 66 return null 67 } 68 } 69