1 /* 2 * Copyright (C) 2022 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.service 18 19 import android.permission.PermissionManager 20 import com.android.permissioncontroller.permission.utils.PermissionMapping 21 22 /** 23 * Takes a list of split permissions, and provides methods that return which split-permissions will 24 * be active given an app's targetSdk. 25 */ 26 class SplitPermissionIndex() { 27 private lateinit var permToGroupSplits: Set<SplitPermissionIndexEntry> 28 private lateinit var groupToGroupSplits: Set<SplitPermissionIndexEntry> 29 30 constructor(groupToGroupSplits: Set<SplitPermissionIndexEntry>) : this() { 31 this.groupToGroupSplits = groupToGroupSplits 32 } 33 34 constructor(splitPermissionInfos: List<PermissionManager.SplitPermissionInfo>) : this() { 35 val permToGroupSplits: MutableSet<SplitPermissionIndexEntry> = mutableSetOf() 36 val groupToGroupSplits: MutableSet<SplitPermissionIndexEntry> = mutableSetOf() 37 for (splitPerm in splitPermissionInfos) { 38 val oldPerm = splitPerm.splitPermission 39 for (newPerm in splitPerm.newPermissions) { 40 val oldPermGroup = PermissionMapping.getGroupOfPlatformPermission(oldPerm) 41 val newPermGroup = PermissionMapping.getGroupOfPlatformPermission(newPerm) 42 if (newPermGroup != null) { 43 permToGroupSplits.add(SplitPermissionIndexEntry( 44 oldPerm, splitPerm.targetSdk, newPermGroup)) 45 } 46 if (oldPermGroup != null && newPermGroup != null) { 47 groupToGroupSplits.add(SplitPermissionIndexEntry( 48 oldPermGroup, splitPerm.targetSdk, newPermGroup)) 49 } 50 } 51 } 52 this.permToGroupSplits = permToGroupSplits 53 this.groupToGroupSplits = groupToGroupSplits 54 } 55 56 /** 57 * Given a permission, return which groups split *from* it for the given targetSdk. 58 */ getPermToGroupSplitsFromnull59 fun getPermToGroupSplitsFrom(oldPermission: String, targetSdk: Int): List<String> { 60 return permToGroupSplits 61 .filter { it.oldPerm == oldPermission && it.targetSdk < targetSdk } 62 .map { it.newPerm } 63 .toList() 64 } 65 66 /** 67 * Given a permission group, return which groups split *from* it for the given targetSdk. 68 */ getGroupToGroupSplitsFromnull69 fun getGroupToGroupSplitsFrom(oldPermissionGroup: String, targetSdk: Int): List<String> { 70 return groupToGroupSplits 71 .filter { it.oldPerm == oldPermissionGroup && it.targetSdk < targetSdk } 72 .map { it.newPerm } 73 .toList() 74 } 75 76 /** 77 * Given a permission group, return which permissions split *to* it for the given targetSdk. 78 */ getGroupToGroupSplitsTonull79 fun getGroupToGroupSplitsTo(newPermissionGroup: String, targetSdk: Int): List<String> { 80 return groupToGroupSplits 81 .filter { it.newPerm == newPermissionGroup && it.targetSdk < targetSdk } 82 .map { it.oldPerm } 83 .toList() 84 } 85 86 data class SplitPermissionIndexEntry( 87 val oldPerm: String, 88 val targetSdk: Int, 89 val newPerm: String 90 ) 91 }