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