summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/permission/model
diff options
context:
space:
mode:
authorKarishma Vakil <kvakil@google.com>2022-11-16 06:05:19 +0000
committerKarishma Vakil <kvakil@google.com>2022-11-18 02:06:49 +0000
commit6ea29396072fd1cfee9edbe4389a4ef89f2929a2 (patch)
tree7459c932c3abff814555da9a0413e30df4462c04 /PermissionController/src/com/android/permissioncontroller/permission/model
parent7f6e7b0f26bc0fa258b7ffb513da9673d2948335 (diff)
downloadPermission-6ea29396072fd1cfee9edbe4389a4ef89f2929a2.tar.gz
Revert "Revert "[Permissions Hub Refactor] Add new ViewModel for Permissions Hub Main""
This reverts commit 7f6e7b0f26bc0fa258b7ffb513da9673d2948335. Reason for revert: Resubmitting with fix - adding some optimization so fewer computations (such as live data isinitialized checks, ui data building, ui redrawing) are done during live data updates. This removes the ANR and speeds up data load. Bug: 257314894 Test: atest CtsPermission3TestCases:PermissionHistoryTest Change-Id: Ibef33135cbba43fd8d7bab6927a4c813b00d62d6
Diffstat (limited to 'PermissionController/src/com/android/permissioncontroller/permission/model')
-rw-r--r--PermissionController/src/com/android/permissioncontroller/permission/model/livedatatypes/v31/LightPackageOps.kt74
1 files changed, 74 insertions, 0 deletions
diff --git a/PermissionController/src/com/android/permissioncontroller/permission/model/livedatatypes/v31/LightPackageOps.kt b/PermissionController/src/com/android/permissioncontroller/permission/model/livedatatypes/v31/LightPackageOps.kt
new file mode 100644
index 000000000..f6db88454
--- /dev/null
+++ b/PermissionController/src/com/android/permissioncontroller/permission/model/livedatatypes/v31/LightPackageOps.kt
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2022 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.v31
+
+import android.app.AppOpsManager.OP_FLAG_SELF
+import android.app.AppOpsManager.OP_FLAG_TRUSTED_PROXIED
+import android.app.AppOpsManager.OP_FLAG_TRUSTED_PROXY
+import android.app.AppOpsManager.OpEntry
+import android.app.AppOpsManager.PackageOps
+import android.app.AppOpsManager.opToPermission
+import android.os.UserHandle
+import com.android.permissioncontroller.permission.utils.PermissionMapping.getGroupOfPlatformPermission
+
+/**
+ * Light version of [PackageOps] class, tracking the last permission access for system permission
+ * groups.
+ */
+data class LightPackageOps(
+ /** Name of the package. */
+ val packageName: String,
+ /** [UserHandle] running the package. */
+ val userHandle: UserHandle,
+ /**
+ * Mapping of permission group name to the last access time of any op backing a permission in
+ * the group.
+ */
+ val lastPermissionGroupAccessTimesMs: Map<String, Long>
+) {
+ constructor(
+ packageOps: PackageOps
+ ) : this(
+ packageOps.packageName,
+ UserHandle.getUserHandleForUid(packageOps.uid),
+ packageOps.ops
+ .groupBy { getPermissionGroupForOp(it.opStr) }
+ // Filter out ops that have no corresponding permission group
+ .filterKeys { it != NO_PERM_GROUP }
+ // Map each permission group to the latest access time across all its ops
+ .mapValues { getLatestLastAccessTimeMs(it.value) })
+
+ /** Companion object for [LightPackageOps]. */
+ companion object {
+ /** Key to represent the absence of a permission group in a map. */
+ private const val NO_PERM_GROUP = "no_perm_group"
+ /** Flags to use for querying an op's last access time. */
+ private const val OPS_LAST_ACCESS_FLAGS =
+ OP_FLAG_SELF or OP_FLAG_TRUSTED_PROXIED or OP_FLAG_TRUSTED_PROXY
+
+ /**
+ * Returns the permission group for the permission that the provided op backs, if any, else
+ * returns [NO_PERM_GROUP].
+ */
+ private fun getPermissionGroupForOp(opName: String) =
+ opToPermission(opName)?.let { getGroupOfPlatformPermission(it) } ?: NO_PERM_GROUP
+
+ /** Returns the latest of all last access times of all the provided [OpEntry] instances. */
+ private fun getLatestLastAccessTimeMs(opEntries: List<OpEntry>) =
+ opEntries.maxOf { it.getLastAccessTime(OPS_LAST_ACCESS_FLAGS) }
+ }
+}