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