• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.compatibility.common.util.SystemUtil
28 import com.android.safetycenter.testing.Coroutines.TIMEOUT_LONG
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     ) =
59         callWithShellPermissionIdentity(READ_SAFETY_CENTER_STATUS) {
60             setSafetyCenterEnabledWithoutReceiverPermissionAndWait(value, timeout)
61         }
62 
setSafetyCenterEnabledWithoutReceiverPermissionAndWaitnull63     fun setSafetyCenterEnabledWithoutReceiverPermissionAndWait(
64         value: Boolean,
65         timeout: Duration = TIMEOUT_LONG
66     ): Boolean {
67         SafetyCenterFlags.isEnabled = value
68         if (timeout < TIMEOUT_LONG) {
69             SystemUtil.waitForBroadcasts()
70         }
71         return receiveSafetyCenterEnabledChanged(timeout)
72     }
73 
unregisternull74     fun unregister() {
75         context.unregisterReceiver(this)
76         mSafetySourceIntentHandler.cancel()
77     }
78 
79     /**
80      * Waits for an [ACTION_SAFETY_CENTER_ENABLED_CHANGED] to be received by this receiver within
81      * the given [timeout].
82      */
receiveSafetyCenterEnabledChangednull83     fun receiveSafetyCenterEnabledChanged(timeout: Duration = TIMEOUT_LONG): Boolean =
84         runBlockingWithTimeout(timeout) {
85             mSafetySourceIntentHandler.receiveSafetyCenterEnabledChanged()
86         }
87 }
88