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

Commit 2933df54 authored by Nate Myren's avatar Nate Myren
Browse files

Create LightPermission data structure

Rather than have the LightAppPermGroup store an awkward "PermissionInfo
+ Permission state (which is flags and grant state)" pair, create a
LightPermission object.

Test: navigate to App Permission Fragment, verify that state is still
loaded correctly.

Change-Id: I9f6577b2647327c45dddb4b9f9882543ec6d0753
parent c0d3f088
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ android_app {
        "androidx.leanback_leanback-preference",
        "androidx.lifecycle_lifecycle-extensions",
        "androidx.lifecycle_lifecycle-common-java8",
        "kotlinx-coroutines-android",
        "SettingsLibHelpUtils",
        "SettingsLibRestrictedLockUtils",
        "SettingsLibAppPreference",
+3 −4
Original line number Diff line number Diff line
@@ -21,8 +21,7 @@ import android.content.pm.PermissionInfo
import android.os.Build
import android.os.UserHandle
import com.android.permissioncontroller.permission.model.livedatatypes.LightAppPermGroup
import com.android.permissioncontroller.permission.model.livedatatypes.LightPermInfo
import com.android.permissioncontroller.permission.model.livedatatypes.PermState
import com.android.permissioncontroller.permission.model.livedatatypes.LightPermission
import com.android.permissioncontroller.permission.utils.SoftRestrictedPermissionPolicy
import com.android.permissioncontroller.permission.utils.Utils
import com.android.permissioncontroller.permission.utils.Utils.OS_PKG
@@ -88,7 +87,7 @@ class AppPermGroupLiveData(
            return
        }

        val permissionMap = mutableMapOf<String, Pair<LightPermInfo, PermState>>()
        val permissionMap = mutableMapOf<String, LightPermission>()
        val whitelistedRestricted = app.packageManager.getWhitelistedRestrictedPermissions(
                packageName, Utils.FLAGS_PERMISSION_WHITELIST_ALL)
        for ((permName, permState) in permStates) {
@@ -99,7 +98,7 @@ class AppPermGroupLiveData(
                    !SoftRestrictedPermissionPolicy.shouldShow(packageInfo, permName,
                            permState.permFlags)
            if ((!isHardRestricted || isWhitelisted) && (!isSoftRestricted)) {
                permissionMap[permName] = permInfo to permState
                permissionMap[permName] = LightPermission(permInfo, permState)
            }
        }
        value = LightAppPermGroup(packageInfo, permGroup.groupInfo, permissionMap)
+7 −11
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.permissioncontroller.permission.model.livedatatypes

import android.content.pm.PackageManager
import android.os.UserHandle

/**
@@ -30,7 +29,7 @@ import android.os.UserHandle
data class LightAppPermGroup(
    val packageInfo: LightPackageInfo,
    val permGroupInfo: LightPermGroupInfo,
    val permissions: Map<String, Pair<LightPermInfo, PermState>>
    val permissions: Map<String, LightPermission>
) {
    /**
     * The current userHandle of this AppPermGroup.
@@ -41,7 +40,7 @@ data class LightAppPermGroup(
     * The names of all background permissions in the permission group which are requested by the
     * package.
     */
    val backgroundPermNames = permissions.mapNotNull { it.value.first.backgroundPermission }
    val backgroundPermNames = permissions.mapNotNull { it.value.backgroundPermission }

    /**
     * Whether or not this App Permission Group has a permission which has a background mode
@@ -57,16 +56,14 @@ data class LightAppPermGroup(
     * Whether any of this App Permission Group's foreground permissions are fixed by policy
     */
    val isForegroundPolicyFixed = permissions.any {
        !backgroundPermNames.contains(it.key) &&
            it.value.second.permFlags and PackageManager.FLAG_PERMISSION_POLICY_FIXED != 0
        !backgroundPermNames.contains(it.key) && it.value.isPolicyFixed
    }

    /**
     * Whether any of this App Permission Group's background permissions are fixed by policy
     */
    val isBackgroundPolicyFixed = permissions.any {
        backgroundPermNames.contains(it.key) &&
            it.value.second.permFlags and PackageManager.FLAG_PERMISSION_POLICY_FIXED != 0
        backgroundPermNames.contains(it.key) && it.value.isPolicyFixed
    }

    /**
@@ -79,21 +76,20 @@ data class LightAppPermGroup(
     * Whether this App Permission Group's permissions are fixed by the system
     */
    val isSystemFixed = permissions.any {
        !backgroundPermNames.contains(it.key) &&
            it.value.second.permFlags and PackageManager.FLAG_PERMISSION_SYSTEM_FIXED != 0
        !backgroundPermNames.contains(it.key) && it.value.isSystemFixed
    }

    /**
     * Whether any of this App Permission Group's foreground permissions are granted
     */
    val isForegroundGranted = permissions.any {
        !backgroundPermNames.contains(it.key) && it.value.second.granted
        !backgroundPermNames.contains(it.key) && it.value.grantedIncludingAppOp
    }

    /**
     * Whether any of this App Permission Group's background permissions are granted
     */
    val isBackgroundGranted = permissions.any {
        backgroundPermNames.contains(it.key) && it.value.second.granted
        backgroundPermNames.contains(it.key) && it.value.grantedIncludingAppOp
    }
}
 No newline at end of file
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.permissioncontroller.permission.model.livedatatypes

import android.content.pm.PackageManager

/**
 * Represents a single permission, and its state
 *
 * @param permInfo: The permissionInfo this represents
 * @param grantedIncludingAppOp: Whether or not this permission is functionally granted.
 * A non-granted app op but granted permission is counted as not granted
 * @param flags: The PermissionController flags for this permission
 */
data class LightPermission(
    val permInfo: LightPermInfo,
    val grantedIncludingAppOp: Boolean,
    val flags: Int
) {

    constructor(permInfo: LightPermInfo, permState: PermState):
        this(permInfo, permState.granted, permState.permFlags)

    /** The name of this permission */
    val name = permInfo.name
    /** The background permission name of this permission, if it exists */
    val backgroundPermission: String? = permInfo.backgroundPermission
    /** Whether this permission is fixed by policy */
    val isPolicyFixed = flags and PackageManager.FLAG_PERMISSION_POLICY_FIXED != 0
    /** Whether this permission is fixed by the system */
    val isSystemFixed = flags and PackageManager.FLAG_PERMISSION_SYSTEM_FIXED != 0
}
 No newline at end of file
+4 −3
Original line number Diff line number Diff line
@@ -425,8 +425,8 @@ class AppPermissionViewModel(
        val group = lightAppPermGroup ?: return null
        val permissionSnapshot = ArrayList<PermissionState>()

        for ((permName, permissionPair) in group.permissions) {
            permissionSnapshot.add(PermissionState(permName, permissionPair.second.granted))
        for ((permName, permission) in group.permissions) {
            permissionSnapshot.add(PermissionState(permName, permission.grantedIncludingAppOp))
        }

        return permissionSnapshot
@@ -496,7 +496,8 @@ class AppPermissionViewModel(
    }

    private fun getIndividualPermissionDetailResId(group: LightAppPermGroup): Pair<Int, Int> {
        return when (val numRevoked = group.permissions.filter { !it.value.second.granted }.size) {
        return when (val numRevoked =
            group.permissions.filter { !it.value.grantedIncludingAppOp }.size) {
            0 -> R.string.permission_revoked_none to numRevoked
            group.permissions.size -> R.string.permission_revoked_all to numRevoked
            else -> R.string.permission_revoked_count to numRevoked