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

Commit 89124e54 authored by Sandy Pan's avatar Sandy Pan
Browse files

Create Intent for KM activity which starts OAuth flow for PIN recovery

Change-Id: I534cac3913f2178d81528a1853017f9cf2abbf5a
Test: atest SupervisionIntentProviderTest
Bug: 399484695
Flag: EXEMPT unreachable code
parent 24e19925
Loading
Loading
Loading
Loading
+34 −1
Original line number Diff line number Diff line
@@ -20,9 +20,27 @@ import android.app.supervision.SupervisionManager
import android.content.Context
import android.content.Intent

/** Helper class meant to provide an intent to launch the supervision settings page. */
/** Helper class meant to provide intent to launch supervision features. */
object SupervisionIntentProvider {
    private const val ACTION_SHOW_PARENTAL_CONTROLS = "android.settings.SHOW_PARENTAL_CONTROLS"
    private const val ACTION_SETUP_PIN_RECOVERY =
        "android.settings.supervision.action.SET_PIN_RECOVERY"
    private const val ACTION_VERIFY_PIN_RECOVERY =
        "android.settings.supervision.action.VERIFY_PIN_RECOVERY"
    private const val ACTION_UPDATE_PIN_RECOVERY =
        "android.settings.supervision.action.UPDATE_PIN_RECOVERY"
    private const val ACTION_SET_VERIFIED_PIN_RECOVERY =
        "android.settings.supervision.action.SET_VERIFIED_PIN_RECOVERY"
    private const val ACTION_POST_SETUP_VERIFY_PIN_RECOVERY =
        "android.settings.supervision.action.POST_SETUP_VERIFY_PIN_RECOVERY"

    enum class PinRecoveryAction(val action: String) {
        SET(ACTION_SETUP_PIN_RECOVERY),
        VERIFY(ACTION_VERIFY_PIN_RECOVERY),
        UPDATE(ACTION_UPDATE_PIN_RECOVERY),
        SET_VERIFIED(ACTION_SET_VERIFIED_PIN_RECOVERY),
        POST_SETUP_VERIFY(ACTION_POST_SETUP_VERIFY_PIN_RECOVERY),
    }

    /**
     * Returns an [Intent] to the supervision settings page or null if supervision is disabled or
@@ -41,4 +59,19 @@ object SupervisionIntentProvider {
            context.packageManager.queryIntentActivitiesAsUser(intent, 0, context.userId)
        return if (activities.isNotEmpty()) intent else null
    }

    /**
     * Returns an [Intent] to the supervision pin recovery activity or null if supervision is
     * disabled or the intent is not resolvable.
     */
    @JvmStatic
    fun getPinRecoveryIntent(context: Context, action: PinRecoveryAction): Intent? {
        val supervisionManager = context.getSystemService(SupervisionManager::class.java)
        val supervisionAppPackage = supervisionManager?.activeSupervisionAppPackage ?: return null

        val intent = Intent(action.action).setPackage(supervisionAppPackage)
        val activities =
            context.packageManager.queryIntentActivitiesAsUser(intent, 0, context.userId)
        return if (activities.isNotEmpty()) intent else null
    }
}
+123 −0
Original line number Diff line number Diff line
@@ -100,6 +100,129 @@ class SupervisionIntentProviderTest {
        assertThat(intent?.`package`).isEqualTo(SUPERVISION_APP_PACKAGE)
    }

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

        val intent =
            SupervisionIntentProvider.getPinRecoveryIntent(
                context,
                SupervisionIntentProvider.PinRecoveryAction.SET,
            )

        assertThat(intent).isNull()
    }

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

        val intent =
            SupervisionIntentProvider.getPinRecoveryIntent(
                context,
                SupervisionIntentProvider.PinRecoveryAction.SET,
            )

        assertThat(intent).isNull()
    }

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

        val intent =
            SupervisionIntentProvider.getPinRecoveryIntent(
                context,
                SupervisionIntentProvider.PinRecoveryAction.SET,
            )

        assertThat(intent).isNotNull()
        assertThat(intent?.action).isEqualTo("android.settings.supervision.action.SET_PIN_RECOVERY")
        assertThat(intent?.`package`).isEqualTo(SUPERVISION_APP_PACKAGE)
    }

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

        val intent =
            SupervisionIntentProvider.getPinRecoveryIntent(
                context,
                SupervisionIntentProvider.PinRecoveryAction.VERIFY,
            )

        assertThat(intent).isNotNull()
        assertThat(intent?.action)
            .isEqualTo("android.settings.supervision.action.VERIFY_PIN_RECOVERY")
        assertThat(intent?.`package`).isEqualTo(SUPERVISION_APP_PACKAGE)
    }

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

        val intent =
            SupervisionIntentProvider.getPinRecoveryIntent(
                context,
                SupervisionIntentProvider.PinRecoveryAction.UPDATE,
            )

        assertThat(intent).isNotNull()
        assertThat(intent?.action)
            .isEqualTo("android.settings.supervision.action.UPDATE_PIN_RECOVERY")
        assertThat(intent?.`package`).isEqualTo(SUPERVISION_APP_PACKAGE)
    }

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

        val intent =
            SupervisionIntentProvider.getPinRecoveryIntent(
                context,
                SupervisionIntentProvider.PinRecoveryAction.SET_VERIFIED,
            )

        assertThat(intent).isNotNull()
        assertThat(intent?.action)
            .isEqualTo("android.settings.supervision.action.SET_VERIFIED_PIN_RECOVERY")
        assertThat(intent?.`package`).isEqualTo(SUPERVISION_APP_PACKAGE)
    }

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

        val intent =
            SupervisionIntentProvider.getPinRecoveryIntent(
                context,
                SupervisionIntentProvider.PinRecoveryAction.POST_SETUP_VERIFY,
            )

        assertThat(intent).isNotNull()
        assertThat(intent?.action)
            .isEqualTo("android.settings.supervision.action.POST_SETUP_VERIFY_PIN_RECOVERY")
        assertThat(intent?.`package`).isEqualTo(SUPERVISION_APP_PACKAGE)
    }

    private companion object {
        const val SUPERVISION_APP_PACKAGE = "app.supervision"
    }