[Catalyst] Support multiple restriction keys
NO_IFTTT=Catalyst only Bug: 377600992 Flag: com.android.settings.flags.catalyst Test: testdpc Change-Id: If7212b5402f7e271b7fdbd2a43bed0e11ee6f15f
This commit is contained in:
@@ -18,27 +18,42 @@ package com.android.settings
|
||||
|
||||
import android.content.Context
|
||||
import android.os.UserHandle
|
||||
import android.os.UserManager
|
||||
import androidx.annotation.CallSuper
|
||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin
|
||||
import com.android.settingslib.RestrictedLockUtilsInternal
|
||||
import com.android.settingslib.metadata.PreferenceRestrictionProvider
|
||||
|
||||
/** Mixin to support restriction. */
|
||||
interface PreferenceRestrictionMixin : PreferenceRestrictionProvider {
|
||||
|
||||
val restrictionKey: String
|
||||
/**
|
||||
* Keys for restriction.
|
||||
*
|
||||
* Preference is restricted when **ANY** key in the list is restricted.
|
||||
*/
|
||||
val restrictionKeys: Array<String>
|
||||
|
||||
val useAdminDisabledSummary: Boolean
|
||||
get() = false
|
||||
|
||||
@CallSuper fun isEnabled(context: Context) = !context.hasBaseUserRestriction(restrictionKey)
|
||||
@CallSuper fun isEnabled(context: Context) = !context.hasBaseUserRestriction(restrictionKeys)
|
||||
|
||||
override fun isRestricted(context: Context) =
|
||||
RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
|
||||
context,
|
||||
restrictionKey,
|
||||
UserHandle.myUserId(),
|
||||
) != null
|
||||
context.getRestrictionEnforcedAdmin(restrictionKeys) != null
|
||||
}
|
||||
|
||||
fun Context.hasBaseUserRestriction(restrictionKey: String) =
|
||||
RestrictedLockUtilsInternal.hasBaseUserRestriction(this, restrictionKey, UserHandle.myUserId())
|
||||
/** Returns the admin that has enforced restriction on given keys. */
|
||||
fun Context.getRestrictionEnforcedAdmin(restrictionKeys: Array<String>): EnforcedAdmin? {
|
||||
val userId = UserHandle.myUserId()
|
||||
return restrictionKeys.firstNotNullOfOrNull {
|
||||
RestrictedLockUtilsInternal.checkIfRestrictionEnforced(this, it, userId)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns if there is **any** base user restriction on given keys. */
|
||||
fun Context.hasBaseUserRestriction(restrictionKeys: Array<String>): Boolean {
|
||||
val userManager = getSystemService(UserManager::class.java) ?: return false
|
||||
val userHandle = UserHandle.of(UserHandle.myUserId())
|
||||
return restrictionKeys.any { userManager.hasBaseUserRestriction(it, userHandle) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user