1 /*
2  * Copyright (C) 2023 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.settings.spa.app.specialaccess
18 
19 import android.Manifest
20 import android.app.AppOpsManager
21 import android.app.role.RoleManager
22 import android.app.settings.SettingsEnums
23 import android.companion.AssociationRequest
24 import android.content.Context
25 import com.android.media.flags.Flags;
26 import com.android.settings.R
27 import com.android.settings.overlay.FeatureFactory.Companion.featureFactory
28 import com.android.settingslib.spaprivileged.model.app.AppOps
29 import com.android.settingslib.spaprivileged.template.app.AppOpPermissionListModel
30 import com.android.settingslib.spaprivileged.template.app.AppOpPermissionRecord
31 import com.android.settingslib.spaprivileged.template.app.TogglePermissionAppListProvider
32 
33 object MediaRoutingControlAppListProvider : TogglePermissionAppListProvider {
34     override val permissionType = "MediaRoutingControl"
createModelnull35     override fun createModel(context: Context) = MediaRoutingControlAppsListModel(context)
36 }
37 
38 class MediaRoutingControlAppsListModel(context: Context) : AppOpPermissionListModel(context) {
39     override val pageTitleResId = R.string.media_routing_control_title
40     override val switchTitleResId = R.string.allow_media_routing_control
41     override val footerResId = R.string.allow_media_routing_description
42     override val appOps = AppOps(
43         op = AppOpsManager.OP_MEDIA_ROUTING_CONTROL,
44         setModeByUid = true,
45     )
46     override val permission = Manifest.permission.MEDIA_ROUTING_CONTROL
47     private val roleManager = context.getSystemService(RoleManager::class.java)
48 
49     override fun setAllowed(record: AppOpPermissionRecord, newAllowed: Boolean) {
50         super.setAllowed(record, newAllowed)
51         logPermissionToggleAction(newAllowed)
52     }
53 
54     override fun isChangeable(record: AppOpPermissionRecord): Boolean {
55         return Flags.enablePrivilegedRoutingForMediaRoutingControl()
56                 && super.isChangeable(record)
57                 && (this.roleManager?.getRoleHolders(AssociationRequest.DEVICE_PROFILE_WATCH)
58                 ?.contains(record.app.packageName) == true)
59     }
60 
61     private fun logPermissionToggleAction(newAllowed: Boolean) {
62         featureFactory.metricsFeatureProvider.action(
63                 context,
64                 SettingsEnums.MEDIA_ROUTING_CONTROL,
65                 if (newAllowed)
66                     VALUE_LOGGING_ALLOWED
67                 else
68                     VALUE_LOGGING_DENIED
69         )
70     }
71 
72     companion object {
73         const val VALUE_LOGGING_ALLOWED = 1
74         const val VALUE_LOGGING_DENIED = 0
75     }
76 }