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

Commit 9f757ebe authored by Zhou Liu's avatar Zhou Liu Committed by Android (Google) Code Review
Browse files

Merge "Replace action SHOW_PARENTAL_CONTROLS with SUPERVISION_SETTINGS" into main

parents 9bbd4b7d f653193b
Loading
Loading
Loading
Loading
+31 −5
Original line number Diff line number Diff line
@@ -18,8 +18,11 @@ package com.android.settingslib.supervision

import android.app.role.RoleManager
import android.app.supervision.SupervisionManager
import android.app.supervision.flags.Flags
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.provider.Settings
import androidx.annotation.RequiresPermission

/** Helper class meant to provide intent to launch supervision features. */
@@ -27,6 +30,7 @@ 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_SUPERVISION_SETTINGS = "android.settings.SUPERVISION_SETTINGS"
    private const val ACTION_VERIFY_PIN_RECOVERY =
        "android.settings.supervision.action.VERIFY_PIN_RECOVERY"
    private const val ACTION_UPDATE_PIN_RECOVERY =
@@ -54,13 +58,23 @@ object SupervisionIntentProvider {
     */
    @JvmStatic
    fun getSettingsIntent(context: Context): Intent? {
        val (intentAction, intentPackage) =
            if (Flags.enableSupervisionSettingsScreen()) {
                val settingsAppPackage = getSettingsAppPackage(context)
                ACTION_SUPERVISION_SETTINGS to settingsAppPackage
            } else {
                val supervisionManager = context.getSystemService(SupervisionManager::class.java)
        val supervisionAppPackage = supervisionManager?.activeSupervisionAppPackage ?: return null
                val supervisionAppPackage = supervisionManager?.activeSupervisionAppPackage
                ACTION_SHOW_PARENTAL_CONTROLS to supervisionAppPackage
            }

        if (intentPackage == null) {
            return null
        }

        val intent =
            Intent(ACTION_SHOW_PARENTAL_CONTROLS)
                .setPackage(supervisionAppPackage)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            Intent(intentAction).setPackage(intentPackage).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

        val activities =
            context.packageManager.queryIntentActivitiesAsUser(intent, 0, context.userId)
        return if (activities.isNotEmpty()) intent else null
@@ -98,6 +112,18 @@ object SupervisionIntentProvider {
        return if (activities.isNotEmpty()) intent else null
    }

    /** Returns the System Settings application's package name */
    @JvmStatic
    private fun getSettingsAppPackage(context: Context): String {
        val packageManager = context.getPackageManager()
        val results =
            packageManager.queryIntentActivities(
                Intent(Settings.ACTION_SETTINGS),
                PackageManager.MATCH_SYSTEM_ONLY,
            )
        return results.firstOrNull()?.activityInfo?.packageName ?: SETTINGS_PKG
    }

    /**
     * Returns an [Intent] to confirm supervision credentials or null if the intent is not
     * resolvable.
+30 −14
Original line number Diff line number Diff line
@@ -18,11 +18,15 @@ package com.android.settingslib.supervision

import android.app.role.RoleManager
import android.app.supervision.SupervisionManager
import android.app.supervision.flags.Flags
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.platform.test.annotations.EnableFlags
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.common.truth.Truth.assertThat
@@ -63,8 +67,12 @@ class SupervisionIntentProviderTest {
    }

    @Test
    fun getSettingsIntent_nullSupervisionPackage() {
        mockSupervisionManager.stub { on { activeSupervisionAppPackage } doReturn null }
    @EnableFlags(Flags.FLAG_ENABLE_SUPERVISION_SETTINGS_SCREEN)
    fun getSettingsIntent_unresolvedIntent() {
        mockPackageManager.stub {
            on { queryIntentActivitiesAsUser(any<Intent>(), any<Int>(), any<Int>()) } doReturn
                emptyList<ResolveInfo>()
        }

        val intent = SupervisionIntentProvider.getSettingsIntent(context)

@@ -72,35 +80,43 @@ class SupervisionIntentProviderTest {
    }

    @Test
    fun getSettingsIntent_unresolvedIntent() {
        mockSupervisionManager.stub {
            on { activeSupervisionAppPackage } doReturn SUPERVISION_APP_PACKAGE
        }
    @EnableFlags(Flags.FLAG_ENABLE_SUPERVISION_SETTINGS_SCREEN)
    fun getSettingsIntent_resolvedIntent_defaultSettingsPackage() {
        mockPackageManager.stub {
            on { queryIntentActivitiesAsUser(any<Intent>(), any<Int>(), any<Int>()) } doReturn
                emptyList<ResolveInfo>()
                listOf(ResolveInfo())
        }

        val intent = SupervisionIntentProvider.getSettingsIntent(context)

        assertThat(intent).isNull()
        assertThat(intent).isNotNull()
        assertThat(intent?.action).isEqualTo("android.settings.SUPERVISION_SETTINGS")
        assertThat(intent?.`package`).isEqualTo("com.android.settings")
    }

    @Test
    fun getSettingsIntent_resolvedIntent() {
        mockSupervisionManager.stub {
            on { activeSupervisionAppPackage } doReturn SUPERVISION_APP_PACKAGE
    @EnableFlags(Flags.FLAG_ENABLE_SUPERVISION_SETTINGS_SCREEN)
    fun getSettingsIntent_resolvedIntent_getSettingsPackageFromPackageManager() {
        val expectedSettingsPackage = "com.android.expected_settings"
        val resolveInfo =
            ResolveInfo().apply {
                this.activityInfo =
                    ActivityInfo().apply {
                        applicationInfo =
                            ApplicationInfo().apply { packageName = expectedSettingsPackage }
                    }
            }
        mockPackageManager.stub {
            on { queryIntentActivitiesAsUser(any<Intent>(), any<Int>(), any<Int>()) } doReturn
                listOf(ResolveInfo())
            on { queryIntentActivities(any<Intent>(), any<Int>()) } doReturn listOf(resolveInfo)
        }

        val intent = SupervisionIntentProvider.getSettingsIntent(context)

        assertThat(intent).isNotNull()
        assertThat(intent?.action).isEqualTo("android.settings.SHOW_PARENTAL_CONTROLS")
        assertThat(intent?.`package`).isEqualTo(SUPERVISION_APP_PACKAGE)
        assertThat(intent?.action).isEqualTo("android.settings.SUPERVISION_SETTINGS")
        assertThat(intent?.`package`).isEqualTo(expectedSettingsPackage)
    }

    @Test