Loading packages/SettingsLib/src/com/android/settingslib/supervision/SupervisionIntentProvider.kt +12 −5 Original line number Diff line number Diff line Loading @@ -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 { Loading Loading @@ -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) Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/supervision/SupervisionIntentProviderTest.kt +18 −14 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -49,6 +50,8 @@ class SupervisionIntentProviderTest { @Mock private lateinit var mockSupervisionManager: SupervisionManager @Mock private lateinit var mockRoleManager: RoleManager private lateinit var context: Context @Before Loading @@ -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) } } Loading Loading @@ -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, Loading @@ -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>()) Loading @@ -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())) Loading @@ -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())) Loading @@ -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())) Loading @@ -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())) Loading @@ -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())) Loading Loading
packages/SettingsLib/src/com/android/settingslib/supervision/SupervisionIntentProvider.kt +12 −5 Original line number Diff line number Diff line Loading @@ -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 { Loading Loading @@ -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) Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/supervision/SupervisionIntentProviderTest.kt +18 −14 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -49,6 +50,8 @@ class SupervisionIntentProviderTest { @Mock private lateinit var mockSupervisionManager: SupervisionManager @Mock private lateinit var mockRoleManager: RoleManager private lateinit var context: Context @Before Loading @@ -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) } } Loading Loading @@ -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, Loading @@ -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>()) Loading @@ -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())) Loading @@ -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())) Loading @@ -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())) Loading @@ -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())) Loading @@ -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())) Loading