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

Commit 7fc77654 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

[Spa] Pass restriction key to SystemBlockedByAdmin

Also listen to UserManager.ACTION_USER_RESTRICTIONS_CHANGED for
user restrictions change.

Bug: 422439682
Flag: EXEMPT new library
Test: unit test
Test: manual test
Change-Id: Iec7eec6f5601d6c2314217c80589d3dac74aa04b
parent 04b5cb20
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.spa.flow

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn

private const val TAG = "BroadcastReceiverFlow"

/**
 * A [BroadcastReceiver] flow for the given [intentFilter].
 *
 * Note: This version does not set receiver flags, so only works for system broadcasts.
 */
@RequiresApi(Build.VERSION_CODES.O)
fun Context.broadcastReceiverFlow(intentFilter: IntentFilter): Flow<Intent> =
    broadcastReceiverFlow(intentFilter = intentFilter, flags = 0)

/** A [BroadcastReceiver] flow for the given [intentFilter]. */
@RequiresApi(Build.VERSION_CODES.O)
fun Context.broadcastReceiverFlow(intentFilter: IntentFilter, flags: Int): Flow<Intent> =
    callbackFlow {
            val broadcastReceiver =
                object : BroadcastReceiver() {
                    override fun onReceive(context: Context, intent: Intent) {
                        Log.d(TAG, "onReceive: $intent")
                        trySend(intent)
                    }
                }
            registerReceiver(broadcastReceiver, intentFilter, flags)

            awaitClose { unregisterReceiver(broadcastReceiver) }
        }
        .catch { e -> Log.e(TAG, "Error while broadcastReceiverFlow", e) }
        .conflate()
        .flowOn(Dispatchers.Default)
+8 −2
Original line number Diff line number Diff line
@@ -16,15 +16,21 @@

package com.android.settingslib.spa.system.restricted

import android.app.admin.DevicePolicyManager
import android.content.Context
import android.content.Intent
import android.provider.Settings
import com.android.settingslib.spa.restricted.BlockedWithDetails

internal class SystemBlockedByAdmin(private val context: Context) : BlockedWithDetails {
internal class SystemBlockedByAdmin(private val context: Context, private val key: String) :
    BlockedWithDetails {
    override val canOverrideSwitchChecked = true

    override fun showDetails() {
        context.startActivity(Intent(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS))
        context.startActivity(
            Intent(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS).apply {
                putExtra(DevicePolicyManager.EXTRA_RESTRICTION, key)
            }
        )
    }
}
+11 −4
Original line number Diff line number Diff line
@@ -19,17 +19,20 @@ package com.android.settingslib.spa.system.restricted
import android.Manifest.permission.INTERACT_ACROSS_USERS
import android.Manifest.permission.MANAGE_USERS
import android.content.Context
import android.content.IntentFilter
import android.os.Bundle
import android.os.UserManager
import android.util.Log
import androidx.annotation.RequiresPermission
import androidx.core.os.bundleOf
import com.android.settingslib.spa.flow.broadcastReceiverFlow
import com.android.settingslib.spa.restricted.NoRestricted
import com.android.settingslib.spa.restricted.RestrictedMode
import com.android.settingslib.spa.restricted.RestrictedRepository
import com.android.settingslib.spa.restricted.Restrictions
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart

/**
 * To use this, please register in [com.android.settingslib.spa.framework.common.SpaEnvironment].
@@ -50,14 +53,18 @@ class SystemRestrictedRepository(private val context: Context) : RestrictedRepos
    @RequiresPermission(anyOf = [MANAGE_USERS, INTERACT_ACROSS_USERS])
    override fun restrictedModeFlow(restrictions: Restrictions): Flow<RestrictedMode> {
        check(restrictions is SystemRestrictions)
        return flow { emit(getRestrictedMode(restrictions)) }
        return context
            .broadcastReceiverFlow(IntentFilter(UserManager.ACTION_USER_RESTRICTIONS_CHANGED))
            .map {}
            .onStart { emit(Unit) }
            .map { getRestrictedMode(restrictions) }
    }

    @RequiresPermission(anyOf = [MANAGE_USERS, INTERACT_ACROSS_USERS])
    private fun getRestrictedMode(restrictions: SystemRestrictions): RestrictedMode {
        val userRestrictions = getUserRestrictions()
        if (restrictions.keys.any { key -> userRestrictions.getBoolean(key) }) {
            return SystemBlockedByAdmin(context)
        for (key in restrictions.keys) {
            if (userRestrictions.getBoolean(key)) return SystemBlockedByAdmin(context, key)
        }
        return NoRestricted
    }
+64 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.spa.system.restricted

import android.app.admin.DevicePolicyManager
import android.content.Context
import android.os.UserManager
import android.provider.Settings
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.doNothing
import org.mockito.kotlin.spy
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@RunWith(AndroidJUnit4::class)
class SystemBlockedByAdminTest {
    private val context: Context =
        spy(ApplicationProvider.getApplicationContext()) {
            doNothing().whenever(mock).startActivity(any())
        }

    @Test
    fun canOverrideSwitchChecked_isTrue() {
        val systemBlockedByAdmin = SystemBlockedByAdmin(context, RESTRICTION_KEY)

        assertThat(systemBlockedByAdmin.canOverrideSwitchChecked).isTrue()
    }

    @Test
    fun showDetails() {
        val systemBlockedByAdmin = SystemBlockedByAdmin(context, RESTRICTION_KEY)

        systemBlockedByAdmin.showDetails()

        val intent = argumentCaptor { verify(context).startActivity(capture()) }.firstValue
        assertThat(intent.action).isEqualTo(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS)
        assertThat(intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION))
            .isEqualTo(RESTRICTION_KEY)
    }

    private companion object {
        const val RESTRICTION_KEY = UserManager.DISALLOW_ADJUST_VOLUME
    }
}
+92 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.spa.flow

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.doThrow
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.stub

@RunWith(AndroidJUnit4::class)
class BroadcastReceiverFlowTest {

    private var registeredBroadcastReceiver: BroadcastReceiver? = null

    private val context =
        mock<Context> {
            on { registerReceiver(any(), eq(INTENT_FILTER), any()) } 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()
    }

    @Test
    fun broadcastReceiverFlow_unregisterReceiverThrowException_noCrash() = runBlocking {
        context.stub { on { unregisterReceiver(any()) } doThrow IllegalArgumentException() }
        val flow = context.broadcastReceiverFlow(INTENT_FILTER)

        flow.firstWithTimeoutOrNull()

        assertThat(registeredBroadcastReceiver).isNotNull()
    }

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