1 /* 2 * Copyright (C) 2022 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.safetycenter.testing 18 19 import android.Manifest.permission.MANAGE_SAFETY_CENTER 20 import android.Manifest.permission.READ_SAFETY_CENTER_STATUS 21 import android.Manifest.permission.SEND_SAFETY_CENTER_UPDATE 22 import android.os.Build.VERSION_CODES.TIRAMISU 23 import android.safetycenter.SafetyCenterData 24 import android.safetycenter.SafetyCenterManager 25 import android.safetycenter.SafetyCenterManager.OnSafetyCenterDataChangedListener 26 import android.safetycenter.SafetyEvent 27 import android.safetycenter.SafetySourceData 28 import android.safetycenter.SafetySourceErrorDetails 29 import android.safetycenter.config.SafetyCenterConfig 30 import androidx.annotation.RequiresApi 31 import com.android.safetycenter.testing.ShellPermissions.callWithShellPermissionIdentity 32 import java.util.concurrent.Executor 33 34 /** 35 * Extension methods for [SafetyCenterManager] that delegate to the relevant implementation, but 36 * making each call with the appropriate permission. 37 */ 38 @RequiresApi(TIRAMISU) 39 object SafetyCenterApisWithShellPermissions { 40 41 /** 42 * Calls [SafetyCenterManager.isSafetyCenterEnabled] adopting Shell's 43 * [READ_SAFETY_CENTER_STATUS] permission. 44 */ SafetyCenterManagernull45 fun SafetyCenterManager.isSafetyCenterEnabledWithPermission(): Boolean = 46 callWithShellPermissionIdentity(READ_SAFETY_CENTER_STATUS) { isSafetyCenterEnabled } 47 48 /** 49 * Calls [SafetyCenterManager.setSafetySourceData] adopting Shell's [SEND_SAFETY_CENTER_UPDATE] 50 * permission. 51 */ SafetyCenterManagernull52 fun SafetyCenterManager.setSafetySourceDataWithPermission( 53 safetySourceId: String, 54 safetySourceData: SafetySourceData?, 55 safetyEvent: SafetyEvent 56 ) { 57 callWithShellPermissionIdentity(SEND_SAFETY_CENTER_UPDATE) { 58 setSafetySourceData(safetySourceId, safetySourceData, safetyEvent) 59 } 60 } 61 62 /** 63 * Calls [SafetyCenterManager.getSafetySourceData] adopting Shell's [SEND_SAFETY_CENTER_UPDATE] 64 * permission. 65 */ SafetyCenterManagernull66 fun SafetyCenterManager.getSafetySourceDataWithPermission(id: String): SafetySourceData? = 67 callWithShellPermissionIdentity(SEND_SAFETY_CENTER_UPDATE) { getSafetySourceData(id) } 68 69 /** 70 * Calls [SafetyCenterManager.reportSafetySourceError] adopting Shell's 71 * [SEND_SAFETY_CENTER_UPDATE] permission. 72 */ SafetyCenterManagernull73 fun SafetyCenterManager.reportSafetySourceErrorWithPermission( 74 safetySourceId: String, 75 safetySourceErrorDetails: SafetySourceErrorDetails 76 ) { 77 callWithShellPermissionIdentity(SEND_SAFETY_CENTER_UPDATE) { 78 reportSafetySourceError(safetySourceId, safetySourceErrorDetails) 79 } 80 } 81 82 /** 83 * Calls [SafetyCenterManager.refreshSafetySources] adopting Shell's [MANAGE_SAFETY_CENTER] 84 * permission. 85 */ SafetyCenterManagernull86 fun SafetyCenterManager.refreshSafetySourcesWithPermission( 87 refreshReason: Int, 88 safetySourceIds: List<String>? = null 89 ) { 90 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { 91 if (safetySourceIds != null) { 92 refreshSafetySources(refreshReason, safetySourceIds) 93 } else { 94 refreshSafetySources(refreshReason) 95 } 96 } 97 } 98 99 /** 100 * Calls [SafetyCenterManager.getSafetyCenterConfig] adopting Shell's [MANAGE_SAFETY_CENTER] 101 * permission. 102 */ SafetyCenterManagernull103 fun SafetyCenterManager.getSafetyCenterConfigWithPermission(): SafetyCenterConfig? = 104 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { safetyCenterConfig } 105 106 /** 107 * Calls [SafetyCenterManager.getSafetyCenterData] adopting Shell's [MANAGE_SAFETY_CENTER] 108 * permission. 109 */ SafetyCenterManagernull110 fun SafetyCenterManager.getSafetyCenterDataWithPermission(): SafetyCenterData = 111 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { safetyCenterData } 112 113 /** 114 * Calls [SafetyCenterManager.addOnSafetyCenterDataChangedListener] adopting Shell's 115 * [MANAGE_SAFETY_CENTER] permission. 116 */ SafetyCenterManagernull117 fun SafetyCenterManager.addOnSafetyCenterDataChangedListenerWithPermission( 118 executor: Executor, 119 listener: OnSafetyCenterDataChangedListener 120 ) { 121 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { 122 addOnSafetyCenterDataChangedListener(executor, listener) 123 } 124 } 125 126 /** 127 * Calls [SafetyCenterManager.removeOnSafetyCenterDataChangedListener] adopting Shell's 128 * [MANAGE_SAFETY_CENTER] permission. 129 */ SafetyCenterManagernull130 fun SafetyCenterManager.removeOnSafetyCenterDataChangedListenerWithPermission( 131 listener: OnSafetyCenterDataChangedListener 132 ) { 133 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { 134 removeOnSafetyCenterDataChangedListener(listener) 135 } 136 } 137 138 /** 139 * Calls [SafetyCenterManager.dismissSafetyCenterIssue] adopting Shell's [MANAGE_SAFETY_CENTER] 140 * permission. 141 */ SafetyCenterManagernull142 fun SafetyCenterManager.dismissSafetyCenterIssueWithPermission(safetyCenterIssueId: String) { 143 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { 144 dismissSafetyCenterIssue(safetyCenterIssueId) 145 } 146 } 147 148 /** 149 * Calls [SafetyCenterManager.executeSafetyCenterIssueAction] adopting Shell's 150 * [MANAGE_SAFETY_CENTER] permission. 151 */ SafetyCenterManagernull152 fun SafetyCenterManager.executeSafetyCenterIssueActionWithPermission( 153 safetyCenterIssueId: String, 154 safetyCenterIssueActionId: String 155 ) { 156 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { 157 executeSafetyCenterIssueAction(safetyCenterIssueId, safetyCenterIssueActionId) 158 } 159 } 160 161 /** 162 * Calls [SafetyCenterManager.clearAllSafetySourceDataForTests] adopting Shell's 163 * [MANAGE_SAFETY_CENTER] permission. 164 */ SafetyCenterManagernull165 fun SafetyCenterManager.clearAllSafetySourceDataForTestsWithPermission() = 166 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { clearAllSafetySourceDataForTests() } 167 168 /** 169 * Calls [SafetyCenterManager.setSafetyCenterConfigForTests] adopting Shell's 170 * [MANAGE_SAFETY_CENTER] permission. 171 */ SafetyCenterManagernull172 fun SafetyCenterManager.setSafetyCenterConfigForTestsWithPermission( 173 safetyCenterConfig: SafetyCenterConfig 174 ) { 175 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { 176 setSafetyCenterConfigForTests(safetyCenterConfig) 177 } 178 } 179 180 /** 181 * Calls [SafetyCenterManager.clearSafetyCenterConfigForTests] adopting Shell's 182 * [MANAGE_SAFETY_CENTER] permission. 183 */ SafetyCenterManagernull184 fun SafetyCenterManager.clearSafetyCenterConfigForTestsWithPermission() { 185 callWithShellPermissionIdentity(MANAGE_SAFETY_CENTER) { clearSafetyCenterConfigForTests() } 186 } 187 } 188