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.READ_SAFETY_CENTER_STATUS 20 import android.content.BroadcastReceiver 21 import android.content.Context 22 import android.content.Intent 23 import android.content.IntentFilter 24 import android.os.Build.VERSION_CODES.TIRAMISU 25 import android.safetycenter.SafetyCenterManager.ACTION_SAFETY_CENTER_ENABLED_CHANGED 26 import androidx.annotation.RequiresApi 27 import com.android.safetycenter.testing.Coroutines.TIMEOUT_LONG 28 import com.android.safetycenter.testing.Coroutines.TIMEOUT_SHORT 29 import com.android.safetycenter.testing.Coroutines.runBlockingWithTimeout 30 import com.android.safetycenter.testing.ShellPermissions.callWithShellPermissionIdentity 31 import java.time.Duration 32 33 /** Broadcast receiver used for testing broadcasts sent when the SafetyCenter flag changes. */ 34 @RequiresApi(TIRAMISU) 35 class SafetyCenterEnabledChangedReceiver(private val context: Context) : BroadcastReceiver() { 36 37 private val mSafetySourceIntentHandler = SafetySourceIntentHandler() 38 39 init { 40 context.registerReceiver(this, IntentFilter(ACTION_SAFETY_CENTER_ENABLED_CHANGED)) 41 } 42 onReceivenull43 override fun onReceive(context: Context, intent: Intent?) { 44 if (intent == null) { 45 throw IllegalArgumentException("Received null intent") 46 } 47 48 if (intent.action != ACTION_SAFETY_CENTER_ENABLED_CHANGED) { 49 throw IllegalArgumentException("Received intent with action: ${intent.action}") 50 } 51 52 runBlockingWithTimeout { mSafetySourceIntentHandler.handle(context, intent) } 53 } 54 setSafetyCenterEnabledWithReceiverPermissionAndWaitnull55 fun setSafetyCenterEnabledWithReceiverPermissionAndWait( 56 value: Boolean, 57 timeout: Duration = TIMEOUT_LONG 58 ): Boolean = 59 callWithShellPermissionIdentity(READ_SAFETY_CENTER_STATUS) { 60 SafetyCenterFlags.isEnabled = value 61 receiveSafetyCenterEnabledChanged(timeout) 62 } 63 setSafetyCenterEnabledWithoutReceiverPermissionAndWaitnull64 fun setSafetyCenterEnabledWithoutReceiverPermissionAndWait( 65 value: Boolean, 66 ) { 67 SafetyCenterFlags.isEnabled = value 68 WaitForBroadcasts.waitForBroadcasts() 69 receiveSafetyCenterEnabledChanged(TIMEOUT_SHORT) 70 } 71 unregisternull72 fun unregister() { 73 context.unregisterReceiver(this) 74 mSafetySourceIntentHandler.cancel() 75 } 76 77 /** 78 * Waits for an [ACTION_SAFETY_CENTER_ENABLED_CHANGED] to be received by this receiver within 79 * the given [timeout]. 80 */ receiveSafetyCenterEnabledChangednull81 fun receiveSafetyCenterEnabledChanged(timeout: Duration = TIMEOUT_LONG): Boolean = 82 runBlockingWithTimeout(timeout) { 83 mSafetySourceIntentHandler.receiveSafetyCenterEnabledChanged() 84 } 85 } 86