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

Commit 5b55f9da authored by Yuting Fang's avatar Yuting Fang Committed by Android (Google) Code Review
Browse files

Merge "Reformat permission Kotlin code in frameworks/base/service" into main

parents 53128e41 c2c630dd
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -25,6 +25,6 @@ hidden_api_txt_checksorted_hook = ${REPO_ROOT}/tools/platform-compat/hiddenapi/c

hidden_api_txt_exclude_hook = ${REPO_ROOT}/frameworks/base/tools/hiddenapi/exclude.sh ${PREUPLOAD_COMMIT} ${REPO_ROOT}

ktfmt_hook = ${REPO_ROOT}/external/ktfmt/ktfmt.py --check -i ${REPO_ROOT}/frameworks/base/packages/SystemUI/ktfmt_includes.txt ${PREUPLOAD_FILES}
ktfmt_hook = ${REPO_ROOT}/external/ktfmt/ktfmt.py --check -i ${REPO_ROOT}/frameworks/base/ktfmt_includes.txt ${PREUPLOAD_FILES}

ktlint_hook = ${REPO_ROOT}/prebuilts/ktlint/ktlint.py --no-verify-format -f ${PREUPLOAD_FILES}
+1 −0
Original line number Diff line number Diff line
+services/permission
+packages/SystemUI
-packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt
-packages/SystemUI/animation/src/com/android/systemui/animation/ViewHierarchyAnimator.kt
+95 −64
Original line number Diff line number Diff line
@@ -43,8 +43,7 @@ import kotlin.contracts.contract

@Keep
class AccessCheckingService(context: Context) : SystemService(context) {
    @Volatile
    private lateinit var state: AccessState
    @Volatile private lateinit var state: AccessState
    private val stateLock = Any()

    private val policy = AccessPolicy()
@@ -86,17 +85,22 @@ class AccessCheckingService(context: Context) : SystemService(context) {

        val state = MutableAccessState()
        policy.initialize(
            state, userIds, packageStates, disabledSystemPackageStates, knownPackages, isLeanback,
            configPermissions, privilegedPermissionAllowlistPackages, permissionAllowlist,
            state,
            userIds,
            packageStates,
            disabledSystemPackageStates,
            knownPackages,
            isLeanback,
            configPermissions,
            privilegedPermissionAllowlistPackages,
            permissionAllowlist,
            implicitToSourcePermissions
        )
        persistence.initialize()
        persistence.read(state)
        this.state = state

        mutateState {
            with(policy) { onInitialized() }
        }
        mutateState { with(policy) { onInitialized() } }

        appOpService.initialize()
        permissionService.initialize()
@@ -106,12 +110,15 @@ class AccessCheckingService(context: Context) : SystemService(context) {
        get() = PackageManager.FEATURE_LEANBACK in availableFeatures

    private val SystemConfig.privilegedPermissionAllowlistPackages: IndexedListSet<String>
        get() = MutableIndexedListSet<String>().apply {
        get() =
            MutableIndexedListSet<String>().apply {
                this += "android"
                if (PackageManager.FEATURE_AUTOMOTIVE in availableFeatures) {
                // Note that SystemProperties.get(String, String) forces returning an empty string
                    // Note that SystemProperties.get(String, String) forces returning an empty
                    // string
                    // even if we pass null for the def parameter.
                val carServicePackage = SystemProperties.get("ro.android.car.carservice.package")
                    val carServicePackage =
                        SystemProperties.get("ro.android.car.carservice.package")
                    if (carServicePackage.isNotEmpty()) {
                        this += carServicePackage
                    }
@@ -120,7 +127,8 @@ class AccessCheckingService(context: Context) : SystemService(context) {

    private val SystemConfig.implicitToSourcePermissions: IndexedMap<String, IndexedListSet<String>>
        @Suppress("UNCHECKED_CAST")
        get() = MutableIndexedMap<String, MutableIndexedListSet<String>>().apply {
        get() =
            MutableIndexedMap<String, MutableIndexedListSet<String>>().apply {
                splitPermissions.forEach { splitPermissionInfo ->
                    val sourcePermissionName = splitPermissionInfo.splitPermission
                    splitPermissionInfo.newPermissions.forEach { implicitPermissionName ->
@@ -131,15 +139,11 @@ class AccessCheckingService(context: Context) : SystemService(context) {
            } as IndexedMap<String, IndexedListSet<String>>

    internal fun onUserAdded(userId: Int) {
        mutateState {
            with(policy) { onUserAdded(userId) }
        }
        mutateState { with(policy) { onUserAdded(userId) } }
    }

    internal fun onUserRemoved(userId: Int) {
        mutateState {
            with(policy) { onUserRemoved(userId) }
        }
        mutateState { with(policy) { onUserRemoved(userId) } }
    }

    internal fun onStorageVolumeMounted(
@@ -152,8 +156,12 @@ class AccessCheckingService(context: Context) : SystemService(context) {
        mutateState {
            with(policy) {
                onStorageVolumeMounted(
                    packageStates, disabledSystemPackageStates, knownPackages, volumeUuid,
                    packageNames, isSystemUpdated
                    packageStates,
                    disabledSystemPackageStates,
                    knownPackages,
                    volumeUuid,
                    packageNames,
                    isSystemUpdated
                )
            }
        }
@@ -165,7 +173,10 @@ class AccessCheckingService(context: Context) : SystemService(context) {
        mutateState {
            with(policy) {
                onPackageAdded(
                    packageStates, disabledSystemPackageStates, knownPackages, packageName
                    packageStates,
                    disabledSystemPackageStates,
                    knownPackages,
                    packageName
                )
            }
        }
@@ -177,7 +188,11 @@ class AccessCheckingService(context: Context) : SystemService(context) {
        mutateState {
            with(policy) {
                onPackageRemoved(
                    packageStates, disabledSystemPackageStates, knownPackages, packageName, appId
                    packageStates,
                    disabledSystemPackageStates,
                    knownPackages,
                    packageName,
                    appId
                )
            }
        }
@@ -189,7 +204,11 @@ class AccessCheckingService(context: Context) : SystemService(context) {
        mutateState {
            with(policy) {
                onPackageInstalled(
                    packageStates, disabledSystemPackageStates, knownPackages, packageName, userId
                    packageStates,
                    disabledSystemPackageStates,
                    knownPackages,
                    packageName,
                    userId
                )
            }
        }
@@ -201,7 +220,11 @@ class AccessCheckingService(context: Context) : SystemService(context) {
        mutateState {
            with(policy) {
                onPackageUninstalled(
                    packageStates, disabledSystemPackageStates, knownPackages, packageName, appId,
                    packageStates,
                    disabledSystemPackageStates,
                    knownPackages,
                    packageName,
                    appId,
                    userId
                )
            }
@@ -224,23 +247,30 @@ class AccessCheckingService(context: Context) : SystemService(context) {

    private fun PackageManagerInternal.getKnownPackages(
        packageStates: Map<String, PackageState>
    ): IntMap<Array<String>> = MutableIntMap<Array<String>>().apply {
    ): IntMap<Array<String>> =
        MutableIntMap<Array<String>>().apply {
            this[KnownPackages.PACKAGE_INSTALLER] =
                getKnownPackageNames(KnownPackages.PACKAGE_INSTALLER, UserHandle.USER_SYSTEM)
        this[KnownPackages.PACKAGE_PERMISSION_CONTROLLER] = getKnownPackageNames(
            KnownPackages.PACKAGE_PERMISSION_CONTROLLER, UserHandle.USER_SYSTEM
            this[KnownPackages.PACKAGE_PERMISSION_CONTROLLER] =
                getKnownPackageNames(
                    KnownPackages.PACKAGE_PERMISSION_CONTROLLER,
                    UserHandle.USER_SYSTEM
                )
            this[KnownPackages.PACKAGE_VERIFIER] =
                getKnownPackageNames(KnownPackages.PACKAGE_VERIFIER, UserHandle.USER_SYSTEM)
            this[KnownPackages.PACKAGE_SETUP_WIZARD] =
                getKnownPackageNames(KnownPackages.PACKAGE_SETUP_WIZARD, UserHandle.USER_SYSTEM)
        this[KnownPackages.PACKAGE_SYSTEM_TEXT_CLASSIFIER] = getKnownPackageNames(
            KnownPackages.PACKAGE_SYSTEM_TEXT_CLASSIFIER, UserHandle.USER_SYSTEM
            this[KnownPackages.PACKAGE_SYSTEM_TEXT_CLASSIFIER] =
                getKnownPackageNames(
                    KnownPackages.PACKAGE_SYSTEM_TEXT_CLASSIFIER,
                    UserHandle.USER_SYSTEM
                )
            this[KnownPackages.PACKAGE_CONFIGURATOR] =
                getKnownPackageNames(KnownPackages.PACKAGE_CONFIGURATOR, UserHandle.USER_SYSTEM)
        this[KnownPackages.PACKAGE_INCIDENT_REPORT_APPROVER] = getKnownPackageNames(
            KnownPackages.PACKAGE_INCIDENT_REPORT_APPROVER, UserHandle.USER_SYSTEM
            this[KnownPackages.PACKAGE_INCIDENT_REPORT_APPROVER] =
                getKnownPackageNames(
                    KnownPackages.PACKAGE_INCIDENT_REPORT_APPROVER,
                    UserHandle.USER_SYSTEM
                )
            this[KnownPackages.PACKAGE_APP_PREDICTOR] =
                getKnownPackageNames(KnownPackages.PACKAGE_APP_PREDICTOR, UserHandle.USER_SYSTEM)
@@ -248,7 +278,8 @@ class AccessCheckingService(context: Context) : SystemService(context) {
                getKnownPackageNames(KnownPackages.PACKAGE_COMPANION, UserHandle.USER_SYSTEM)
            this[KnownPackages.PACKAGE_RETAIL_DEMO] =
                getKnownPackageNames(KnownPackages.PACKAGE_RETAIL_DEMO, UserHandle.USER_SYSTEM)
                .filter { isProfileOwner(it, packageStates) }.toTypedArray()
                    .filter { isProfileOwner(it, packageStates) }
                    .toTypedArray()
            this[KnownPackages.PACKAGE_RECENTS] =
                getKnownPackageNames(KnownPackages.PACKAGE_RECENTS, UserHandle.USER_SYSTEM)
        }
+22 −33
Original line number Diff line number Diff line
@@ -38,16 +38,11 @@ import com.android.server.permission.access.util.writeWithReserveCopy
import java.io.File
import java.io.FileNotFoundException

class AccessPersistence(
    private val policy: AccessPolicy
) {
class AccessPersistence(private val policy: AccessPolicy) {
    private val scheduleLock = Any()
    @GuardedBy("scheduleLock")
    private val pendingMutationTimesMillis = SparseLongArray()
    @GuardedBy("scheduleLock")
    private val pendingStates = MutableIntMap<AccessState>()
    @GuardedBy("scheduleLock")
    private lateinit var writeHandler: WriteHandler
    @GuardedBy("scheduleLock") private val pendingMutationTimesMillis = SparseLongArray()
    @GuardedBy("scheduleLock") private val pendingStates = MutableIntMap<AccessState>()
    @GuardedBy("scheduleLock") private lateinit var writeHandler: WriteHandler

    private val writeLock = Any()

@@ -60,13 +55,12 @@ class AccessPersistence(
     */
    fun read(state: MutableAccessState) {
        readSystemState(state)
        state.externalState.userIds.forEachIndexed { _, userId ->
            readUserState(state, userId)
        }
        state.externalState.userIds.forEachIndexed { _, userId -> readUserState(state, userId) }
    }

    private fun readSystemState(state: MutableAccessState) {
        val fileExists = systemFile.parse {
        val fileExists =
            systemFile.parse {
                // This is the canonical way to call an extension function in a different class.
                // TODO(b/259469752): Use context receiver for this when it becomes stable.
                with(policy) { parseSystemState(state) }
@@ -79,9 +73,8 @@ class AccessPersistence(
    }

    private fun readUserState(state: MutableAccessState, userId: Int) {
        val fileExists = getUserFile(userId).parse {
            with(policy) { parseUserState(state, userId) }
        }
        val fileExists =
            getUserFile(userId).parse { with(policy) { parseUserState(state, userId) } }

        if (!fileExists) {
            policy.migrateUserState(state, userId)
@@ -90,8 +83,8 @@ class AccessPersistence(
    }

    /**
     * @return {@code true} if the file is successfully read from the disk; {@code false} if
     * the file doesn't exist yet.
     * @return {@code true} if the file is successfully read from the disk; {@code false} if the
     *   file doesn't exist yet.
     */
    private inline fun File.parse(block: BinaryXmlPullParser.() -> Unit): Boolean =
        try {
@@ -106,9 +99,7 @@ class AccessPersistence(

    fun write(state: AccessState) {
        state.systemState.write(state, UserHandle.USER_ALL)
        state.userStates.forEachIndexed { _, userId, userState ->
            userState.write(state, userId)
        }
        state.userStates.forEachIndexed { _, userId, userState -> userState.write(state, userId) }
    }

    private fun WritableState.write(state: AccessState, userId: Int) {
@@ -127,8 +118,10 @@ class AccessPersistence(
                    if (currentDelayMillis > MAX_WRITE_DELAY_MILLIS) {
                        message.sendToTarget()
                    } else {
                        val newDelayMillis = WRITE_DELAY_TIME_MILLIS
                            .coerceAtMost(MAX_WRITE_DELAY_MILLIS - currentDelayMillis)
                        val newDelayMillis =
                            WRITE_DELAY_TIME_MILLIS.coerceAtMost(
                                MAX_WRITE_DELAY_MILLIS - currentDelayMillis
                            )
                        writeHandler.sendMessageDelayed(message, newDelayMillis)
                    }
                }
@@ -161,15 +154,11 @@ class AccessPersistence(
    }

    private fun writeSystemState(state: AccessState) {
        systemFile.serialize {
            with(policy) { serializeSystemState(state) }
        }
        systemFile.serialize { with(policy) { serializeSystemState(state) } }
    }

    private fun writeUserState(state: AccessState, userId: Int) {
        getUserFile(userId).serialize {
            with(policy) { serializeUserState(state, userId) }
        }
        getUserFile(userId).serialize { with(policy) { serializeUserState(state, userId) } }
    }

    private inline fun File.serialize(block: BinaryXmlSerializer.() -> Unit) {
+41 −80
Original line number Diff line number Diff line
@@ -37,14 +37,17 @@ import com.android.server.permission.access.util.tagName
import com.android.server.pm.permission.PermissionAllowlist
import com.android.server.pm.pkg.PackageState

class AccessPolicy private constructor(
class AccessPolicy
private constructor(
    private val schemePolicies: IndexedMap<String, IndexedMap<String, SchemePolicy>>
) {
    @Suppress("UNCHECKED_CAST")
    constructor() : this(
    constructor() :
        this(
            MutableIndexedMap<String, MutableIndexedMap<String, SchemePolicy>>().apply {
                fun addPolicy(policy: SchemePolicy) {
                getOrPut(policy.subjectScheme) { MutableIndexedMap() }[policy.objectScheme] = policy
                    getOrPut(policy.subjectScheme) { MutableIndexedMap() }[policy.objectScheme] =
                        policy
                }
                addPolicy(AppIdPermissionPolicy())
                addPolicy(DevicePermissionPolicy())
@@ -92,23 +95,17 @@ class AccessPolicy private constructor(
    }

    fun GetStateScope.onStateMutated() {
        forEachSchemePolicy {
            with(it) { onStateMutated() }
        }
        forEachSchemePolicy { with(it) { onStateMutated() } }
    }

    fun MutateStateScope.onInitialized() {
        forEachSchemePolicy {
            with(it) { onInitialized() }
        }
        forEachSchemePolicy { with(it) { onInitialized() } }
    }

    fun MutateStateScope.onUserAdded(userId: Int) {
        newState.mutateExternalState().mutateUserIds() += userId
        newState.mutateUserStatesNoWrite()[userId] = MutableUserState()
        forEachSchemePolicy {
            with(it) { onUserAdded(userId) }
        }
        forEachSchemePolicy { with(it) { onUserAdded(userId) } }
        newState.externalState.packageStates.forEach { (_, packageState) ->
            upgradePackageVersion(packageState, userId)
        }
@@ -117,9 +114,7 @@ class AccessPolicy private constructor(
    fun MutateStateScope.onUserRemoved(userId: Int) {
        newState.mutateExternalState().mutateUserIds() -= userId
        newState.mutateUserStatesNoWrite() -= userId
        forEachSchemePolicy {
            with(it) { onUserRemoved(userId) }
        }
        forEachSchemePolicy { with(it) { onUserRemoved(userId) } }
    }

    fun MutateStateScope.onStorageVolumeMounted(
@@ -154,9 +149,7 @@ class AccessPolicy private constructor(
            setKnownPackages(knownPackages)
        }
        addedAppIds.forEachIndexed { _, appId ->
            forEachSchemePolicy {
                with(it) { onAppIdAdded(appId) }
            }
            forEachSchemePolicy { with(it) { onAppIdAdded(appId) } }
        }
        forEachSchemePolicy {
            with(it) { onStorageVolumeMounted(volumeUuid, packageNames, isSystemUpdated) }
@@ -192,13 +185,9 @@ class AccessPolicy private constructor(
            setKnownPackages(knownPackages)
        }
        if (isAppIdAdded) {
            forEachSchemePolicy {
                with(it) { onAppIdAdded(appId) }
            }
        }
        forEachSchemePolicy {
            with(it) { onPackageAdded(packageState) }
            forEachSchemePolicy { with(it) { onAppIdAdded(appId) } }
        }
        forEachSchemePolicy { with(it) { onPackageAdded(packageState) } }
        newState.userStates.forEachIndexed { _, userId, _ ->
            upgradePackageVersion(packageState, userId)
        }
@@ -227,13 +216,9 @@ class AccessPolicy private constructor(
            }
            setKnownPackages(knownPackages)
        }
        forEachSchemePolicy {
            with(it) { onPackageRemoved(packageName, appId) }
        }
        forEachSchemePolicy { with(it) { onPackageRemoved(packageName, appId) } }
        if (isAppIdRemoved) {
            forEachSchemePolicy {
                with(it) { onAppIdRemoved(appId) }
            }
            forEachSchemePolicy { with(it) { onAppIdRemoved(appId) } }
        }
        newState.userStates.forEachIndexed { userStateIndex, _, userState ->
            if (packageName in userState.packageVersions) {
@@ -258,9 +243,7 @@ class AccessPolicy private constructor(
        checkNotNull(packageState) {
            "Installed package $packageName isn't found in packageStates in onPackageInstalled()"
        }
        forEachSchemePolicy {
            with(it) { onPackageInstalled(packageState, userId) }
        }
        forEachSchemePolicy { with(it) { onPackageInstalled(packageState, userId) } }
    }

    fun MutateStateScope.onPackageUninstalled(
@@ -276,9 +259,7 @@ class AccessPolicy private constructor(
            setDisabledSystemPackageStates(disabledSystemPackageStates)
            setKnownPackages(knownPackages)
        }
        forEachSchemePolicy {
            with(it) { onPackageUninstalled(packageName, appId, userId) }
        }
        forEachSchemePolicy { with(it) { onPackageUninstalled(packageName, appId, userId) } }
    }

    fun MutateStateScope.onSystemReady(
@@ -292,21 +273,15 @@ class AccessPolicy private constructor(
            setKnownPackages(knownPackages)
            setSystemReady(true)
        }
        forEachSchemePolicy {
            with(it) { onSystemReady() }
        }
        forEachSchemePolicy { with(it) { onSystemReady() } }
    }

    fun migrateSystemState(state: MutableAccessState) {
        forEachSchemePolicy {
            with(it) { migrateSystemState(state) }
        }
        forEachSchemePolicy { with(it) { migrateSystemState(state) } }
    }

    fun migrateUserState(state: MutableAccessState, userId: Int) {
        forEachSchemePolicy {
            with(it) { migrateUserState(state, userId) }
        }
        forEachSchemePolicy { with(it) { migrateUserState(state, userId) } }
    }

    private fun MutateStateScope.upgradePackageVersion(packageState: PackageState, userId: Int) {
@@ -330,8 +305,10 @@ class AccessPolicy private constructor(
                    VERSION_LATEST
            }
            version == VERSION_LATEST -> {}
            else -> Slog.w(
                LOG_TAG, "Unexpected version $version for package $packageName," +
            else ->
                Slog.w(
                    LOG_TAG,
                    "Unexpected version $version for package $packageName," +
                        "latest version is $VERSION_LATEST"
                )
        }
@@ -341,11 +318,7 @@ class AccessPolicy private constructor(
        forEachTag {
            when (tagName) {
                TAG_ACCESS -> {
                    forEachTag {
                        forEachSchemePolicy {
                            with(it) { parseSystemState(state) }
                        }
                    }
                    forEachTag { forEachSchemePolicy { with(it) { parseSystemState(state) } } }
                }
                else -> Slog.w(LOG_TAG, "Ignoring unknown tag $tagName when parsing system state")
            }
@@ -353,11 +326,7 @@ class AccessPolicy private constructor(
    }

    fun BinaryXmlSerializer.serializeSystemState(state: AccessState) {
        tag(TAG_ACCESS) {
            forEachSchemePolicy {
                with(it) { serializeSystemState(state) }
            }
        }
        tag(TAG_ACCESS) { forEachSchemePolicy { with(it) { serializeSystemState(state) } } }
    }

    fun BinaryXmlPullParser.parseUserState(state: MutableAccessState, userId: Int) {
@@ -370,9 +339,7 @@ class AccessPolicy private constructor(
                            TAG_DEFAULT_PERMISSION_GRANT ->
                                parseDefaultPermissionGrant(state, userId)
                            else -> {
                                forEachSchemePolicy {
                                    with(it) { parseUserState(state, userId) }
                                }
                                forEachSchemePolicy { with(it) { parseUserState(state, userId) } }
                            }
                        }
                    }
@@ -428,9 +395,7 @@ class AccessPolicy private constructor(
            serializeDefaultPermissionGrantFingerprint(
                state.userStates[userId]!!.defaultPermissionGrantFingerprint
            )
            forEachSchemePolicy {
                with(it) { serializeUserState(state, userId) }
            }
            forEachSchemePolicy { with(it) { serializeUserState(state, userId) } }
        }
    }

@@ -451,9 +416,7 @@ class AccessPolicy private constructor(
        fingerprint: String?
    ) {
        if (fingerprint != null) {
            tag(TAG_DEFAULT_PERMISSION_GRANT) {
                attributeInterned(ATTR_FINGERPRINT, fingerprint)
            }
            tag(TAG_DEFAULT_PERMISSION_GRANT) { attributeInterned(ATTR_FINGERPRINT, fingerprint) }
        }
    }

@@ -462,9 +425,7 @@ class AccessPolicy private constructor(

    private inline fun forEachSchemePolicy(action: (SchemePolicy) -> Unit) {
        schemePolicies.forEachIndexed { _, _, objectSchemePolicies ->
            objectSchemePolicies.forEachIndexed { _, _, schemePolicy ->
                action(schemePolicy)
            }
            objectSchemePolicies.forEachIndexed { _, _, schemePolicy -> action(schemePolicy) }
        }
    }

Loading