• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
37  * shown
38  */
39 class AllAppPermissionsViewModel(
40     packageName: String,
41     user: UserHandle,
42     filterGroup: String?
43 ) : ViewModel() {
44 
45     val allPackagePermissionsLiveData = AllPackagePermissionsLiveData(packageName, user,
46         filterGroup)
47 
48     class AllPackagePermissionsLiveData(
49         packageName: String,
50         user: UserHandle,
51         private val filterGroup: String?
52     ) : SmartUpdateMediatorLiveData<@kotlin.jvm.JvmSuppressWildcards
53     Map<String, List<String>>>() {
54 
55         private val packagePermsLiveData =
56             PackagePermissionsLiveData[packageName, user]
57         private val packageInfoLiveData = LightPackageInfoLiveData[packageName, user]
58 
59         init {
<lambda>null60             addSource(packagePermsLiveData) {
61                 update()
62             }
<lambda>null63             addSource(packageInfoLiveData) {
64                 update()
65             }
66         }
67 
onUpdatenull68         override fun onUpdate() {
69             if (!packagePermsLiveData.isInitialized || packagePermsLiveData.isStale ||
70                 !packageInfoLiveData.isInitialized) {
71                 return
72             }
73             val permissions = packagePermsLiveData.value
74             val packageInfo = packageInfoLiveData.value
75             if (permissions == null || packageInfo == null) {
76                 value = null
77                 return
78             }
79 
80             value = permissions
81                 .filter { filterGroup == null || it.key == filterGroup }
82                 .filter { (it.key != Manifest.permission_group.STORAGE ||
83                         Utils.shouldShowStorage(packageInfo!!)) }
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
95  * shown
96  */
97 class AllAppPermissionsViewModelFactory(
98     private val packageName: String,
99     private val user: UserHandle,
100     private val filterGroup: String?
101 ) : ViewModelProvider.Factory {
102 
createnull103     override fun <T : ViewModel> create(modelClass: Class<T>): T {
104         @Suppress("UNCHECKED_CAST")
105         return AllAppPermissionsViewModel(packageName, user, filterGroup) as T
106     }
107 }
108