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.ViewModel 24 import androidx.lifecycle.ViewModelProvider 25 import androidx.navigation.fragment.findNavController 26 import com.android.permissioncontroller.R 27 import com.android.permissioncontroller.permission.data.PermGroupsPackagesLiveData 28 import com.android.permissioncontroller.permission.data.PermGroupsPackagesUiInfoLiveData 29 import com.android.permissioncontroller.permission.data.SmartUpdateMediatorLiveData 30 import com.android.permissioncontroller.permission.utils.navigateSafe 31 32 /** 33 * A ViewModel for the ManageCustomPermissionsFragment. Provides a LiveData which watches over all 34 * custom permission groups, and sends async updates when these groups have changes. 35 * 36 * @param app The current application of the fragment 37 */ 38 class ManageCustomPermissionsViewModel(private val app: Application) : AndroidViewModel(app) { 39 40 val uiDataLiveData = PermGroupsPackagesUiInfoLiveData(app, UsedCustomPermGroupNamesLiveData()) 41 42 /** 43 * Navigate to a Permission Apps fragment 44 * 45 * @param fragment The fragment we are navigating from 46 * @param args The args to pass to the new fragment 47 */ showPermissionAppsnull48 fun showPermissionApps(fragment: Fragment, args: Bundle) { 49 fragment.findNavController().navigateSafe(R.id.manage_to_perm_apps, args) 50 } 51 } 52 53 /** 54 * Factory for a ManageCustomPermissionsViewModel 55 * 56 * @param app The current application of the fragment 57 */ 58 class ManageCustomPermissionsViewModelFactory(private val app: Application) : 59 ViewModelProvider.Factory { createnull60 override fun <T : ViewModel> create(modelClass: Class<T>): T { 61 @Suppress("UNCHECKED_CAST") return ManageCustomPermissionsViewModel(app) as T 62 } 63 } 64 65 /** 66 * A LiveData which tracks the names of Custom Permission Groups which are used by at least one 67 * package. This includes single-permission permission groups, as well as the Undefined permission 68 * group, and any other permission groups not defined by the system. 69 */ 70 class UsedCustomPermGroupNamesLiveData : SmartUpdateMediatorLiveData<List<String>>() { 71 72 init { <lambda>null73 addSource(PermGroupsPackagesLiveData.get(customGroups = true)) { value = it.keys.toList() } 74 } 75 onUpdatenull76 override fun onUpdate() { 77 /* No op override */ 78 } 79 } 80