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