summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/permission/ui/model/AllAppPermissionsViewModel.kt
blob: d789f4e1eb8aa55aa5f880031d38ab9a392dcc18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.permissioncontroller.permission.ui.model

import android.Manifest
import android.os.UserHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.android.permissioncontroller.permission.data.LightPackageInfoLiveData
import com.android.permissioncontroller.permission.data.PackagePermissionsLiveData
import com.android.permissioncontroller.permission.data.SmartUpdateMediatorLiveData
import com.android.permissioncontroller.permission.data.get
import com.android.permissioncontroller.permission.utils.Utils

/**
 * ViewModel for the AllAppPermissionsFragment. Has a liveData with the UI information for all
 * Permissions (organized by group) that this package requests, and all the installed, non-runtime,
 * normal protection permissions as well.
 *
 * @param packageName The name of the package this viewModel is representing
 * @param user The user of the package this viewModel is representing
 * @param filterGroup An optional single group that should be shown, no other groups will be
 * shown
 */
class AllAppPermissionsViewModel(
    packageName: String,
    user: UserHandle,
    filterGroup: String?
) : ViewModel() {

    val allPackagePermissionsLiveData = AllPackagePermissionsLiveData(packageName, user,
        filterGroup)

    class AllPackagePermissionsLiveData(
        packageName: String,
        user: UserHandle,
        private val filterGroup: String?
    ) : SmartUpdateMediatorLiveData<@kotlin.jvm.JvmSuppressWildcards
    Map<String, List<String>>>() {

        private val packagePermsLiveData =
            PackagePermissionsLiveData[packageName, user]
        private val packageInfoLiveData = LightPackageInfoLiveData[packageName, user]

        init {
            addSource(packagePermsLiveData) {
                update()
            }
            addSource(packageInfoLiveData) {
                update()
            }
        }

        override fun onUpdate() {
            if (!packagePermsLiveData.isInitialized || packagePermsLiveData.isStale ||
                !packageInfoLiveData.isInitialized) {
                return
            }
            val permissions = packagePermsLiveData.value
            val packageInfo = packageInfoLiveData.value
            if (permissions == null || packageInfo == null) {
                value = null
                return
            }

            value = permissions
                .filter { filterGroup == null || it.key == filterGroup }
                .filter { (it.key != Manifest.permission_group.STORAGE ||
                        Utils.shouldShowStorage(packageInfo)) }
        }
    }
}

/**
 * Factory for an AllAppPermissionsViewModel.
 *
 * @param app The current application
 * @param packageName The name of the package this viewModel is representing
 * @param user The user of the package this viewModel is representing
 * @param filterGroup An optional single group that should be shown, no other groups will be
 * shown
 */
class AllAppPermissionsViewModelFactory(
    private val packageName: String,
    private val user: UserHandle,
    private val filterGroup: String?
) : ViewModelProvider.Factory {

    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        @Suppress("UNCHECKED_CAST")
        return AllAppPermissionsViewModel(packageName, user, filterGroup) as T
    }
}