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

Commit 053c424b authored by Jacky Wang's avatar Jacky Wang
Browse files

[Supervision] Add supervisingUserHandle extensions

Remove getSupervisingUserHandle from SupervisionHelper.

Bug: 412899640
Flag: android.app.supervision.flags.enable_supervision_settings_screen
Test: atest&manual
Change-Id: Ia26af400c99ced40eb3af6750a254b7605665da2
parent 74fa573a
Loading
Loading
Loading
Loading
+11 −17
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ import androidx.annotation.RequiresPermission
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.android.settings.R
import com.android.settingslib.supervision.SupervisionLog
import com.android.settingslib.supervision.SupervisionLog.TAG

/**
 * Activity for confirming supervision credentials using device credential authentication.
@@ -60,10 +60,7 @@ open class ConfirmSupervisionCredentialsActivity : FragmentActivity() {
            @RequiresPermission(anyOf = [INTERACT_ACROSS_USERS_FULL, MANAGE_USERS])
            override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                tryStopProfile()
                Log.w(
                    SupervisionLog.TAG,
                    "onAuthenticationError(errorCode=$errorCode, errString=$errString)",
                )
                Log.w(TAG, "onAuthenticationError(errorCode=$errorCode, errString=$errString)")
                setResult(RESULT_CANCELED)
                finish()
            }
@@ -92,9 +89,9 @@ open class ConfirmSupervisionCredentialsActivity : FragmentActivity() {
            return
        }

        val supervisingUser = SupervisionHelper.getInstance(this).getSupervisingUserHandle()
        val supervisingUser = supervisingUserHandle
        if (supervisingUser == null) {
            Log.w(SupervisionLog.TAG, "No supervising user exists, cannot verify credentials.")
            Log.w(TAG, "No supervising user exists, cannot verify credentials.")
            setResult(RESULT_CANCELED)
            finish()
            return
@@ -102,10 +99,7 @@ open class ConfirmSupervisionCredentialsActivity : FragmentActivity() {

        val activityManager = getSystemService(ActivityManager::class.java)
        if (!activityManager.startProfile(supervisingUser)) {
            Log.w(
                SupervisionLog.TAG,
                "Unable to start supervising user, cannot verify credentials.",
            )
            Log.w(TAG, "Unable to start supervising user, cannot verify credentials.")
            setResult(RESULT_CANCELED)
            finish()
            return
@@ -132,7 +126,7 @@ open class ConfirmSupervisionCredentialsActivity : FragmentActivity() {
    private fun callerHasSupervisionRole(): Boolean {
        val roleManager = getSystemService(RoleManager::class.java)
        if (roleManager == null) {
            Log.w(SupervisionLog.TAG, "null RoleManager")
            Log.w(TAG, "null RoleManager")
            return false
        }
        return roleManager
@@ -143,7 +137,7 @@ open class ConfirmSupervisionCredentialsActivity : FragmentActivity() {
    private fun callerIsSystemUid(): Boolean {
        val callingUid = Binder.getCallingUid()
        if (callingUid != Process.SYSTEM_UID) {
            Log.w(SupervisionLog.TAG, "callingUid: $callingUid is not SYSTEM_UID")
            Log.w(TAG, "callingUid: $callingUid is not SYSTEM_UID")
            return false
        }
        return true
@@ -151,14 +145,14 @@ open class ConfirmSupervisionCredentialsActivity : FragmentActivity() {

    @RequiresPermission(anyOf = [INTERACT_ACROSS_USERS_FULL, MANAGE_USERS])
    private fun tryStopProfile() {
        val supervisingUser = SupervisionHelper.getInstance(this).getSupervisingUserHandle()
        val activityManager = getSystemService(ActivityManager::class.java)
        val supervisingUser = supervisingUserHandle
        if (supervisingUser == null) {
            Log.w(SupervisionLog.TAG, "Cannot stop supervising profile because it does not exist.")
            Log.w(TAG, "Cannot stop supervising profile because it does not exist.")
            return
        }
        val activityManager = getSystemService(ActivityManager::class.java)
        if (!activityManager.stopProfile(supervisingUser)) {
            Log.w(SupervisionLog.TAG, "Could not stop the supervising profile.")
            Log.w(TAG, "Could not stop the supervising profile.")
        }
    }
}
+4 −7
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ import android.Manifest.permission.CREATE_USERS
import android.Manifest.permission.INTERACT_ACROSS_USERS
import android.Manifest.permission.INTERACT_ACROSS_USERS_FULL
import android.Manifest.permission.MANAGE_USERS
import android.app.Activity
import android.app.ActivityManager
import android.app.KeyguardManager
import android.app.admin.DevicePolicyManager.ACTION_SET_NEW_PASSWORD
@@ -69,11 +68,10 @@ class SetupSupervisionActivity : FragmentActivity() {

    @RequiresPermission(anyOf = [CREATE_USERS, MANAGE_USERS])
    private fun enableSupervision() {
        val supervisionHelper = SupervisionHelper.getInstance(this)
        var supervisingUser = supervisionHelper.getSupervisingUserHandle()
        val userManager = getSystemService(UserManager::class.java)
        var supervisingUser = userManager.supervisingUserHandle
        // If a supervising profile does not already exist on the device, create one
        if (supervisingUser == null) {
            val userManager = getSystemService(UserManager::class.java)
            val userInfo =
                userManager.createUser("Supervising", USER_TYPE_PROFILE_SUPERVISING, /* flags= */ 0)
            if (userInfo != null) {
@@ -120,8 +118,7 @@ class SetupSupervisionActivity : FragmentActivity() {
    }

    private fun handleSetLockResult(resultCode: Int) {
        val supervisionHelper = SupervisionHelper.getInstance(this)
        val supervisingUser = supervisionHelper.getSupervisingUserHandle()
        val supervisingUser = supervisingUserHandle
        if (supervisingUser == null) {
            Log.w(SupervisionLog.TAG, "No supervising user handle found after lock setup.")
            setResult(RESULT_CANCELED)
@@ -147,7 +144,7 @@ class SetupSupervisionActivity : FragmentActivity() {
    }

    private fun handlePinRecoveryResult(resultCode: Int) {
        if (resultCode == Activity.RESULT_CANCELED) {
        if (resultCode == RESULT_CANCELED) {
            Log.i(SupervisionLog.TAG, "PIN recovery setup was skipped by the user.")
        }
        setResult(RESULT_OK)
+5 −6
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.settings.supervision
import android.Manifest.permission.INTERACT_ACROSS_USERS_FULL
import android.Manifest.permission.MANAGE_USERS
import android.annotation.RequiresPermission
import android.app.Activity
import android.app.ActivityManager
import android.app.ComponentCaller
import android.content.Intent
@@ -28,7 +27,7 @@ import androidx.annotation.OpenForTesting
import androidx.annotation.VisibleForTesting
import androidx.fragment.app.FragmentActivity
import com.android.settings.password.ChooseLockGeneric
import com.android.settingslib.supervision.SupervisionLog
import com.android.settingslib.supervision.SupervisionLog.TAG

/**
 * An Activity that acts as a proxy to set the supervising user's credentials.
@@ -47,7 +46,7 @@ open class SupervisionCredentialProxyActivity : FragmentActivity() {
    @VisibleForTesting
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val supervisingUser = SupervisionHelper.getInstance(this).getSupervisingUserHandle()
        val supervisingUser = supervisingUserHandle
        if (supervisingUser == null) {
            errorHandler("SupervisingUserHandle is null")
            return
@@ -77,7 +76,7 @@ open class SupervisionCredentialProxyActivity : FragmentActivity() {
            errorHandler("Unexpected request code: $requestCode")
            return
        }
        val supervisingUser = SupervisionHelper.getInstance(this).getSupervisingUserHandle()
        val supervisingUser = supervisingUserHandle
        if (supervisingUser == null) {
            errorHandler("Cannot stop supervising profile because it does not exist.")
            return
@@ -92,8 +91,8 @@ open class SupervisionCredentialProxyActivity : FragmentActivity() {
    }

    private fun errorHandler(errStr: String) {
        Log.w(SupervisionLog.TAG, errStr)
        setResult(Activity.RESULT_CANCELED)
        Log.w(TAG, errStr)
        setResult(RESULT_CANCELED)
        finish()
    }

+7 −10
Original line number Diff line number Diff line
@@ -30,17 +30,8 @@ open class SupervisionHelper private constructor(context: Context) {
    private val mUserManager = context.getSystemService(UserManager::class.java)
    private val mKeyguardManager = context.getSystemService(KeyguardManager::class.java)

    fun getSupervisingUserHandle(): UserHandle? {
        for (user in (mUserManager?.users ?: emptyList())) {
            if (user.userType.equals(USER_TYPE_PROFILE_SUPERVISING)) {
                return user.userHandle
            }
        }
        return null
    }

    fun isSupervisingCredentialSet(): Boolean {
        val supervisingUserId = getSupervisingUserHandle()?.identifier ?: return false
        val supervisingUserId = mUserManager.supervisingUserHandle?.identifier ?: return false
        return mKeyguardManager?.isDeviceSecure(supervisingUserId) == true
    }

@@ -56,6 +47,12 @@ open class SupervisionHelper private constructor(context: Context) {
    }
}

val Context.supervisingUserHandle: UserHandle?
    get() = getSystemService(UserManager::class.java).supervisingUserHandle

val UserManager?.supervisingUserHandle: UserHandle?
    get() = this?.users?.firstOrNull { it.userType == USER_TYPE_PROFILE_SUPERVISING }?.userHandle

/** Returns the package name of the system supervision app, or null if not found. */
val Context.supervisionPackageName: String?
    get() {