Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f29b9048 authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "New BroadcastReceiverFlow" into main

parents cd1181e4 e468422b
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.spaprivileged.framework.common

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn

/**
 * A [BroadcastReceiver] flow for the given [intentFilter].
 */
fun Context.broadcastReceiverFlow(intentFilter: IntentFilter): Flow<Intent> = callbackFlow {
    val broadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            trySend(intent)
        }
    }
    registerReceiver(broadcastReceiver, intentFilter, Context.RECEIVER_NOT_EXPORTED)

    awaitClose { unregisterReceiver(broadcastReceiver) }
}.conflate().flowOn(Dispatchers.Default)
+80 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.spaprivileged.framework.common

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spa.testutils.firstWithTimeoutOrNull
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock

@RunWith(AndroidJUnit4::class)
class BroadcastReceiverFlowTest {

    private var registeredBroadcastReceiver: BroadcastReceiver? = null

    private val context = mock<Context> {
        on {
            registerReceiver(any(), eq(INTENT_FILTER), eq(Context.RECEIVER_NOT_EXPORTED))
        } doAnswer {
            registeredBroadcastReceiver = it.arguments[0] as BroadcastReceiver
            null
        }
    }

    @Test
    fun broadcastReceiverFlow_registered() = runBlocking {
        val flow = context.broadcastReceiverFlow(INTENT_FILTER)

        flow.firstWithTimeoutOrNull()

        assertThat(registeredBroadcastReceiver).isNotNull()
    }

    @Test
    fun broadcastReceiverFlow_isCalledOnReceive() = runBlocking {
        var onReceiveIsCalled = false
        launch {
            context.broadcastReceiverFlow(INTENT_FILTER).first {
                onReceiveIsCalled = true
                true
            }
        }

        delay(100)
        registeredBroadcastReceiver!!.onReceive(context, Intent())
        delay(100)

        assertThat(onReceiveIsCalled).isTrue()
    }

    private companion object {
        val INTENT_FILTER = IntentFilter()
    }
}