1 /*
2  * Copyright (C) 2020 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.Manifest
20 import android.os.UserHandle
21 import androidx.lifecycle.ViewModel
22 import androidx.lifecycle.ViewModelProvider
23 import com.android.permissioncontroller.permission.data.LightPackageInfoLiveData
24 import com.android.permissioncontroller.permission.data.PackagePermissionsLiveData
25 import com.android.permissioncontroller.permission.data.SmartUpdateMediatorLiveData
26 import com.android.permissioncontroller.permission.data.get
27 import com.android.permissioncontroller.permission.utils.Utils
28 
29 /**
30  * ViewModel for the AllAppPermissionsFragment. Has a liveData with the UI information for all
31  * Permissions (organized by group) that this package requests, and all the installed, non-runtime,
32  * normal protection permissions as well.
33  *
34  * @param packageName The name of the package this viewModel is representing
35  * @param user The user of the package this viewModel is representing
36  * @param filterGroup An optional single group that should be shown, no other groups will be shown
37  */
38 class AllAppPermissionsViewModel(packageName: String, user: UserHandle, filterGroup: String?) :
39     ViewModel() {
40 
41     val allPackagePermissionsLiveData =
42         AllPackagePermissionsLiveData(packageName, user, filterGroup)
43 
44     class AllPackagePermissionsLiveData(
45         packageName: String,
46         user: UserHandle,
47         private val filterGroup: String?
48     ) : SmartUpdateMediatorLiveData<@kotlin.jvm.JvmSuppressWildcards Map<String, List<String>>>() {
49 
50         private val packagePermsLiveData = PackagePermissionsLiveData[packageName, user]
51         private val packageInfoLiveData = LightPackageInfoLiveData[packageName, user]
52 
53         init {
<lambda>null54             addSource(packagePermsLiveData) { update() }
<lambda>null55             addSource(packageInfoLiveData) { update() }
56         }
57 
onUpdatenull58         override fun onUpdate() {
59             if (
60                 !packagePermsLiveData.isInitialized ||
61                     packagePermsLiveData.isStale ||
62                     !packageInfoLiveData.isInitialized
63             ) {
64                 return
65             }
66             val permissions = packagePermsLiveData.value
67             val packageInfo = packageInfoLiveData.value
68             if (permissions == null || packageInfo == null) {
69                 value = null
70                 return
71             }
72 
73             value =
74                 permissions
75                     .filter { filterGroup == null || it.key == filterGroup }
76                     .filter {
77                         (it.key != Manifest.permission_group.STORAGE ||
78                             Utils.shouldShowStorage(packageInfo))
79                     }
80                     .filter {
81                         (!Utils.isHealthPermissionGroup(it.key) ||
82                             Utils.shouldShowHealthPermission(packageInfo, it.key))
83                     }
84         }
85     }
86 }
87 
88 /**
89  * Factory for an AllAppPermissionsViewModel.
90  *
91  * @param app The current application
92  * @param packageName The name of the package this viewModel is representing
93  * @param user The user of the package this viewModel is representing
94  * @param filterGroup An optional single group that should be shown, no other groups will be shown
95  */
96 class AllAppPermissionsViewModelFactory(
97     private val packageName: String,
98     private val user: UserHandle,
99     private val filterGroup: String?
100 ) : ViewModelProvider.Factory {
101 
createnull102     override fun <T : ViewModel> create(modelClass: Class<T>): T {
103         @Suppress("UNCHECKED_CAST")
104         return AllAppPermissionsViewModel(packageName, user, filterGroup) as T
105     }
106 }
107