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(
39     private val app: Application
40 ) : AndroidViewModel(app) {
41 
42     val uiDataLiveData = PermGroupsPackagesUiInfoLiveData(app,
43         UsedCustomPermGroupNamesLiveData())
44 
45     /**
46      * Navigate to a Permission Apps fragment
47      *
48      * @param fragment The fragment we are navigating from
49      * @param args The args to pass to the new fragment
50      */
showPermissionAppsnull51     fun showPermissionApps(fragment: Fragment, args: Bundle) {
52         fragment.findNavController().navigateSafe(R.id.manage_to_perm_apps, args)
53     }
54 }
55 
56 /**
57  * Factory for a ManageCustomPermissionsViewModel
58  *
59  * @param app The current application of the fragment
60  */
61 class ManageCustomPermissionsViewModelFactory(
62     private val app: Application
63 ) : ViewModelProvider.Factory {
createnull64     override fun <T : ViewModel> create(modelClass: Class<T>): T {
65         @Suppress("UNCHECKED_CAST")
66         return ManageCustomPermissionsViewModel(app) as T
67     }
68 }
69 
70 /**
71  * A LiveData which tracks the names of Custom Permission Groups which are used by at least one
72  * package. This includes single-permission permission groups, as well as the Undefined permission
73  * group, and any other permission groups not defined by the system.
74  */
75 class UsedCustomPermGroupNamesLiveData :
76     SmartUpdateMediatorLiveData<List<String>>() {
77 
78     init {
<lambda>null79         addSource(PermGroupsPackagesLiveData.get(customGroups = true)) {
80             value = it.keys.toList()
81         }
82     }
83 
onUpdatenull84     override fun onUpdate() { /* No op override */ }
85 }
86