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.app.Application 20 import android.os.Bundle 21 import androidx.fragment.app.Fragment 22 import androidx.lifecycle.AndroidViewModel 23 import androidx.lifecycle.Transformations 24 import androidx.navigation.fragment.findNavController 25 import com.android.permissioncontroller.R 26 import com.android.permissioncontroller.permission.data.PermGroupsPackagesLiveData 27 import com.android.permissioncontroller.permission.data.PermGroupsPackagesUiInfoLiveData 28 import com.android.permissioncontroller.permission.data.SmartUpdateMediatorLiveData 29 import com.android.permissioncontroller.permission.data.StandardPermGroupNamesLiveData 30 import com.android.permissioncontroller.permission.data.unusedAutoRevokePackagesLiveData 31 import com.android.permissioncontroller.permission.utils.navigateSafe 32 33 /** 34 * A ViewModel for the ManageStandardPermissionsFragment. Provides a LiveData which watches over all 35 * platform permission groups, and sends async updates when these groups have changes. It also 36 * provides a liveData which watches the custom permission groups of the system, and provides 37 * a list of group names. 38 * @param app The current application of the fragment 39 */ 40 class ManageStandardPermissionsViewModel( 41 private val app: Application 42 ) : AndroidViewModel(app) { 43 44 val uiDataLiveData = PermGroupsPackagesUiInfoLiveData(app, 45 StandardPermGroupNamesLiveData) 46 val numCustomPermGroups = NumCustomPermGroupsWithPackagesLiveData() 47 val numAutoRevoked = Transformations.map(unusedAutoRevokePackagesLiveData) { 48 it?.size ?: 0 49 } 50 51 /** 52 * Navigate to the Custom Permissions screen 53 * 54 * @param fragment The fragment we are navigating from 55 * @param args The args to pass to the new fragment 56 */ 57 fun showCustomPermissions(fragment: Fragment, args: Bundle) { 58 fragment.findNavController().navigateSafe(R.id.standard_to_custom, args) 59 } 60 61 /** 62 * Navigate to a Permission Apps fragment 63 * 64 * @param fragment The fragment we are navigating from 65 * @param args The args to pass to the new fragment 66 */ 67 fun showPermissionApps(fragment: Fragment, args: Bundle) { 68 fragment.findNavController().navigateSafe(R.id.manage_to_perm_apps, args) 69 } 70 71 fun showAutoRevoke(fragment: Fragment, args: Bundle) { 72 fragment.findNavController().navigateSafe(R.id.manage_to_auto_revoke, args) 73 } 74 } 75 76 /** 77 * A LiveData which tracks the number of custom permission groups that are used by at least one 78 * package 79 */ 80 class NumCustomPermGroupsWithPackagesLiveData() : 81 SmartUpdateMediatorLiveData<Int>() { 82 83 private val customPermGroupPackages = PermGroupsPackagesLiveData.get(customGroups = true) 84 85 init { 86 addSource(customPermGroupPackages) { 87 update() 88 } 89 } 90 91 override fun onUpdate() { 92 value = customPermGroupPackages.value?.size ?: 0 93 } 94 }