1 /* <lambda>null2 * Copyright (C) 2021 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.ui.model 18 19 import android.app.Application 20 import androidx.lifecycle.AndroidViewModel 21 import androidx.lifecycle.LiveData 22 import androidx.lifecycle.MediatorLiveData 23 import com.android.permissioncontroller.permission.data.PermGroupsPackagesUiInfoLiveData 24 import com.android.permissioncontroller.permission.data.StandardPermGroupNamesLiveData 25 import com.android.permissioncontroller.permission.model.livedatatypes.PermGroupPackagesUiInfo 26 27 /** 28 * A [androidx.lifecycle.ViewModel] for [ManagePermissionsFragment] and 29 * [ManagePermissionsOtherFragment]. However, [ManagePermissionsViewModel] is designed in a way so 30 * that its owner should be an [Activity][androidx.fragment.app.FragmentActivity] rather than 31 * individual [Fragments][androidx.fragment.app.Fragment], and the aforementioned Fragments that 32 * manage different sets of the permission groups should to share a single instance of 33 * [ManagePermissionsViewModel]. 34 */ 35 class ManagePermissionsViewModel(app: Application) : AndroidViewModel(app) { 36 37 /** [LiveData] that contains a list of all platform-defined permission groups. */ 38 val standardPermGroupsLiveData: LiveData<List<PermGroupPackagesUiInfo>> = 39 MediatorLiveData<List<PermGroupPackagesUiInfo>>().apply { 40 addSource(PermGroupsPackagesUiInfoLiveData(app, StandardPermGroupNamesLiveData)) { 41 permGroups -> 42 value = permGroups.values.filterNotNull() 43 } 44 } 45 46 /** 47 * [LiveData] that contains a list of platform-defined permission groups, such that at least one 48 * the permissions in the group has been requested at runtime by at least one non-system 49 * application or has been pregranted to a non-system application. 50 * 51 * @see com.android.permissioncontroller.permission.ui.television.ManagePermissionsFragment 52 */ 53 val usedPermissionGroups: LiveData<List<PermGroupPackagesUiInfo>> = 54 MediatorLiveData<List<PermGroupPackagesUiInfo>>().apply { 55 addSource(standardPermGroupsLiveData) { permGroups -> 56 value = permGroups.filter { it.nonSystemUserSetOrPreGranted > 0 } 57 } 58 } 59 60 /** 61 * [LiveData] that contains a list of platform-defined permission groups, such that all of the 62 * permissions in the group neither has been requested at runtime by any of the non-system 63 * applications nor has been pregranted to any such application. But at least one of the 64 * permissions in the group is requested by or pregranted to at least one system application, 65 * other than the Shell (we do not show permission groups that are granted only to the Shell, 66 * because it has all the permissions granted). 67 * 68 * @see com.android.permissioncontroller.permission.ui.television.ManagePermissionsOtherFragment 69 */ 70 val unusedPermissionGroups: LiveData<List<PermGroupPackagesUiInfo>> = 71 MediatorLiveData<List<PermGroupPackagesUiInfo>>().apply { 72 addSource(standardPermGroupsLiveData) { permGroups -> 73 value = 74 permGroups 75 .filter { it.nonSystemUserSetOrPreGranted == 0 } 76 .filter { it.systemUserSetOrPreGranted > 0 } 77 .filterNot { it.onlyShellPackageGranted } 78 } 79 } 80 81 /** 82 * [LiveData] that contains a list of the application-defined permission groups (a.k.a. "custom" 83 * permissions), such that at least one of the permissions in the group has been requested at 84 * runtime by or has been pregranted to at least one application (system or non-system). 85 * 86 * @see com.android.permissioncontroller.permission.ui.television.ManagePermissionsOtherFragment 87 */ 88 val additionalPermissionGroups: LiveData<List<PermGroupPackagesUiInfo>> = 89 MediatorLiveData<List<PermGroupPackagesUiInfo>>().apply { 90 addSource(PermGroupsPackagesUiInfoLiveData(app, UsedCustomPermGroupNamesLiveData())) { 91 permGroups -> 92 value = 93 permGroups.values.filterNotNull().filter { 94 (it.nonSystemUserSetOrPreGranted > 0) or (it.systemUserSetOrPreGranted > 0) 95 } 96 } 97 } 98 99 /** 100 * [LiveData] that indicates whether there any unused or additional permission groups. 101 * 102 * @see com.android.permissioncontroller.permission.ui.television.ManagePermissionsFragment 103 */ 104 @get:JvmName("hasUnusedOrAdditionalPermissionGroups") 105 val hasUnusedOrAdditionalPermissionGroups: LiveData<Boolean> = 106 MediatorLiveData<Boolean>().apply { 107 val updateValue: (Any?) -> Unit = { 108 value = 109 !unusedPermissionGroups.value.isNullOrEmpty() || 110 !additionalPermissionGroups.value.isNullOrEmpty() 111 } 112 addSource(unusedPermissionGroups, updateValue) 113 addSource(additionalPermissionGroups, updateValue) 114 } 115 } 116