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

Commit 7f552c79 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Supervision] Remove SupervisionHelper singleton

Fix: 412899640
Flag: android.app.supervision.flags.enable_supervision_settings_screen
Test: atest&manual
Change-Id: I115bda8b1762ed331de2f6b5796ee8cedb0776a2
parent 053c424b
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -35,8 +35,7 @@ class SupervisionChangePinPreference : PreferenceMetadata {
        get() = R.string.supervision_change_pin_preference_title

    override fun intent(context: Context): Intent? {
        val supervisionHelper = SupervisionHelper.getInstance(context)
        if (!supervisionHelper.isSupervisingCredentialSet()) {
        if (!context.isSupervisingCredentialSet) {
            Log.w(SupervisionLog.TAG, "Supervising credential not set")
            return null
        }
+5 −21
Original line number Diff line number Diff line
@@ -22,29 +22,13 @@ import android.os.UserHandle
import android.os.UserManager
import android.os.UserManager.USER_TYPE_PROFILE_SUPERVISING
import android.util.Log
import androidx.annotation.VisibleForTesting
import com.android.settingslib.supervision.SupervisionLog.TAG

/** Convenience methods for interacting with the supervising user profile. */
open class SupervisionHelper private constructor(context: Context) {
    private val mUserManager = context.getSystemService(UserManager::class.java)
    private val mKeyguardManager = context.getSystemService(KeyguardManager::class.java)

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

    companion object {
        @Volatile @VisibleForTesting var sInstance: SupervisionHelper? = null

        fun getInstance(context: Context): SupervisionHelper {
            return sInstance
                ?: synchronized(this) {
                    sInstance ?: SupervisionHelper(context).also { sInstance = it }
                }
        }
    }
val Context.isSupervisingCredentialSet: Boolean
    get() {
        val supervisingUserId = supervisingUserHandle?.identifier ?: return false
        return getSystemService(KeyguardManager::class.java)?.isDeviceSecure(supervisingUserId) ==
            true
    }

val Context.supervisingUserHandle: UserHandle?
+1 −2
Original line number Diff line number Diff line
@@ -138,11 +138,10 @@ class SupervisionMainSwitchPreference(

    override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
        if (newValue !is Boolean) return true
        val supervisionHelper = SupervisionHelper.getInstance(preference.context)

        // If supervision is being enabled but either the supervising profile hasn't been created
        // or the credentials aren't set, launch SetupSupervisionActivity.
        if (newValue && !supervisionHelper.isSupervisingCredentialSet()) {
        if (newValue && !preference.context.isSupervisingCredentialSet) {
            val intent = Intent(lifeCycleContext, SetupSupervisionActivity::class.java)
            lifeCycleContext.startActivityForResult(intent, REQUEST_CODE_SET_UP_SUPERVISION, null)
            return false
+1 −2
Original line number Diff line number Diff line
@@ -36,8 +36,7 @@ class SupervisionPinManagementScreen :
    override val key: String
        get() = KEY

    override fun isAvailable(context: Context) =
        SupervisionHelper.getInstance(context).isSupervisingCredentialSet()
    override fun isAvailable(context: Context) = context.isSupervisingCredentialSet

    override val title: Int
        get() = R.string.supervision_pin_management_preference_title
+0 −1
Original line number Diff line number Diff line
@@ -53,7 +53,6 @@ class ConfirmSupervisionCredentialsActivityTest {

    @Before
    fun setUp() {
        SupervisionHelper.sInstance = null
        mActivity =
            spy(
                Robolectric.buildActivity(ConfirmSupervisionCredentialsActivity::class.java).get()
Loading