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