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

Commit d8ab1fae authored by Sandy Pan's avatar Sandy Pan
Browse files

Update supervisionIntentProvider to use role holder package

Context: as the recovery flow needs to work without active supervision
package (when supervision is disabled), so we should use the role holder
package instead.

Bug: 393657542
Test: SupervisionIntentProviderTest.kt
Flag: EXEMPT unused code
Change-Id: If27bbecf787bf51b0ffa194457ecf1c717a93ed5
parent 004451be
Loading
Loading
Loading
Loading
+12 −5
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package com.android.settingslib.supervision

import android.app.role.RoleManager
import android.app.supervision.SupervisionManager
import android.content.Context
import android.content.Intent
import androidx.annotation.RequiresPermission

/** Helper class meant to provide intent to launch supervision features. */
object SupervisionIntentProvider {
@@ -64,14 +66,19 @@ object SupervisionIntentProvider {
    }

    /**
     * Returns an [Intent] to the supervision pin recovery activity or null if supervision is
     * disabled or the intent is not resolvable.
     * Returns an [Intent] to the supervision pin recovery activity or null if there's no
     * [android.app.role.RoleManager.ROLE_SYSTEM_SUPERVISION] role holder or the intent is not
     * resolvable.
     */
    @RequiresPermission("android.permission.MANAGE_ROLE_HOLDERS")
    @JvmStatic
    fun getPinRecoveryIntent(context: Context, action: PinRecoveryAction): Intent? {
        val supervisionManager = context.getSystemService(SupervisionManager::class.java)
        val supervisionAppPackage = supervisionManager?.activeSupervisionAppPackage ?: return null

        val roleHolders =
            context
                .getSystemService(RoleManager::class.java)
                ?.getRoleHolders(RoleManager.ROLE_SYSTEM_SUPERVISION)
        // Supervision role is exclusive, only one app may hold this role per user.
        val supervisionAppPackage = roleHolders?.firstOrNull() ?: return null
        val intent = Intent(action.action).setPackage(supervisionAppPackage)
        val activities =
            context.packageManager.queryIntentActivitiesAsUser(intent, 0, context.userId)
+18 −14
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settingslib.supervision

import android.app.role.RoleManager
import android.app.supervision.SupervisionManager
import android.content.Context
import android.content.ContextWrapper
@@ -49,6 +50,8 @@ class SupervisionIntentProviderTest {

    @Mock private lateinit var mockSupervisionManager: SupervisionManager

    @Mock private lateinit var mockRoleManager: RoleManager

    private lateinit var context: Context

    @Before
@@ -60,6 +63,7 @@ class SupervisionIntentProviderTest {
                override fun getSystemService(name: String) =
                    when (name) {
                        Context.SUPERVISION_SERVICE -> mockSupervisionManager
                        Context.ROLE_SERVICE -> mockRoleManager
                        else -> super.getSystemService(name)
                    }
            }
@@ -102,8 +106,8 @@ class SupervisionIntentProviderTest {

    @Test
    fun getPinRecoveryIntent_nullSupervisionPackage() {
        `when`(mockSupervisionManager.activeSupervisionAppPackage).thenReturn(null)

        `when`(mockRoleManager.getRoleHolders(RoleManager.ROLE_SYSTEM_SUPERVISION))
            .thenReturn(emptyList())
        val intent =
            SupervisionIntentProvider.getPinRecoveryIntent(
                context,
@@ -115,8 +119,8 @@ class SupervisionIntentProviderTest {

    @Test
    fun getPinRecoveryIntent_unresolvedIntent() {
        `when`(mockSupervisionManager.activeSupervisionAppPackage)
            .thenReturn(SUPERVISION_APP_PACKAGE)
        `when`(mockRoleManager.getRoleHolders(RoleManager.ROLE_SYSTEM_SUPERVISION))
            .thenReturn(listOf(SUPERVISION_APP_PACKAGE))
        `when`(mockPackageManager.queryIntentActivitiesAsUser(any<Intent>(), anyInt(), anyInt()))
            .thenReturn(emptyList<ResolveInfo>())

@@ -140,8 +144,8 @@ class SupervisionIntentProviderTest {

    @Test
    fun getPinRecoveryIntent_setup_resolvedIntent() {
        `when`(mockSupervisionManager.activeSupervisionAppPackage)
            .thenReturn(SUPERVISION_APP_PACKAGE)
        `when`(mockRoleManager.getRoleHolders(RoleManager.ROLE_SYSTEM_SUPERVISION))
            .thenReturn(listOf(SUPERVISION_APP_PACKAGE))
        `when`(mockPackageManager.queryIntentActivitiesAsUser(any<Intent>(), anyInt(), anyInt()))
            .thenReturn(listOf(ResolveInfo()))

@@ -158,8 +162,8 @@ class SupervisionIntentProviderTest {

    @Test
    fun getPinRecoveryIntent_verify_resolvedIntent() {
        `when`(mockSupervisionManager.activeSupervisionAppPackage)
            .thenReturn(SUPERVISION_APP_PACKAGE)
        `when`(mockRoleManager.getRoleHolders(RoleManager.ROLE_SYSTEM_SUPERVISION))
            .thenReturn(listOf(SUPERVISION_APP_PACKAGE))
        `when`(mockPackageManager.queryIntentActivitiesAsUser(any<Intent>(), anyInt(), anyInt()))
            .thenReturn(listOf(ResolveInfo()))

@@ -177,8 +181,8 @@ class SupervisionIntentProviderTest {

    @Test
    fun getPinRecoveryIntent_update_resolvedIntent() {
        `when`(mockSupervisionManager.activeSupervisionAppPackage)
            .thenReturn(SUPERVISION_APP_PACKAGE)
        `when`(mockRoleManager.getRoleHolders(RoleManager.ROLE_SYSTEM_SUPERVISION))
            .thenReturn(listOf(SUPERVISION_APP_PACKAGE))
        `when`(mockPackageManager.queryIntentActivitiesAsUser(any<Intent>(), anyInt(), anyInt()))
            .thenReturn(listOf(ResolveInfo()))

@@ -196,8 +200,8 @@ class SupervisionIntentProviderTest {

    @Test
    fun getPinRecoveryIntent_setVerified_resolvedIntent() {
        `when`(mockSupervisionManager.activeSupervisionAppPackage)
            .thenReturn(SUPERVISION_APP_PACKAGE)
        `when`(mockRoleManager.getRoleHolders(RoleManager.ROLE_SYSTEM_SUPERVISION))
            .thenReturn(listOf(SUPERVISION_APP_PACKAGE))
        `when`(mockPackageManager.queryIntentActivitiesAsUser(any<Intent>(), anyInt(), anyInt()))
            .thenReturn(listOf(ResolveInfo()))

@@ -215,8 +219,8 @@ class SupervisionIntentProviderTest {

    @Test
    fun getPinRecoveryIntent_postSetupVerify_resolvedIntent() {
        `when`(mockSupervisionManager.activeSupervisionAppPackage)
            .thenReturn(SUPERVISION_APP_PACKAGE)
        `when`(mockRoleManager.getRoleHolders(RoleManager.ROLE_SYSTEM_SUPERVISION))
            .thenReturn(listOf(SUPERVISION_APP_PACKAGE))
        `when`(mockPackageManager.queryIntentActivitiesAsUser(any<Intent>(), anyInt(), anyInt()))
            .thenReturn(listOf(ResolveInfo()))