• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.permissioncontroller.permission.model.livedatatypes.v31
18 
19 import android.app.AppOpsManager.OP_FLAG_SELF
20 import android.app.AppOpsManager.OP_FLAG_TRUSTED_PROXIED
21 import android.app.AppOpsManager.OP_FLAG_TRUSTED_PROXY
22 import android.app.AppOpsManager.PackageOps
23 import android.os.UserHandle
24 import com.android.permissioncontroller.permission.utils.PermissionMapping.getPlatformPermissionGroupForOp
25 
26 /**
27  * Light version of [PackageOps] class, tracking the last permission access for system permission
28  * groups.
29  */
30 data class LightPackageOps(
31     /** Name of the package. */
32     val packageName: String,
33     /** [UserHandle] running the package. */
34     val userHandle: UserHandle,
35     /**
36      * Mapping of permission group name to the last access time of any op backing a permission in
37      * the group.
38      */
39     val lastPermissionGroupAccessTimesMs: Map<String, Long>
40 ) {
41     constructor(
42         ops: Set<String>,
43         packageOps: PackageOps
44     ) : this(
45         packageOps.packageName,
46         UserHandle.getUserHandleForUid(packageOps.uid),
47         createLastPermissionGroupAccessTimesMap(ops, packageOps))
48 
49     /** Companion object for [LightPackageOps]. */
50     companion object {
51         /** Flags to use for querying an op's last access time. */
52         private const val OPS_LAST_ACCESS_FLAGS =
53             OP_FLAG_SELF or OP_FLAG_TRUSTED_PROXIED or OP_FLAG_TRUSTED_PROXY
54 
55         /** Creates a mapping from permission group to the last time it was accessed. */
createLastPermissionGroupAccessTimesMapnull56         private fun createLastPermissionGroupAccessTimesMap(
57             opNames: Set<String>,
58             packageOps: PackageOps
59         ): Map<String, Long> {
60             val lastAccessTimeMs = mutableMapOf<String, Long>()
61             // Add keys for all permissions groups covered by the provided ops, regardless of
62             // whether they have been observed recently.
63             for (permissionGroup in
64                 opNames.mapNotNull { getPlatformPermissionGroupForOp(it) }.toSet()) {
65                 lastAccessTimeMs[permissionGroup] = -1
66             }
67 
68             for (opEntry in packageOps.ops) {
69                 val permissionGroupOfOp = getPlatformPermissionGroupForOp(opEntry.opStr) ?: continue
70                 lastAccessTimeMs[permissionGroupOfOp] =
71                     maxOf(
72                         lastAccessTimeMs[permissionGroupOfOp] ?: -1,
73                         opEntry.getLastAccessTime(OPS_LAST_ACCESS_FLAGS))
74             }
75 
76             return lastAccessTimeMs
77         }
78     }
79 }
80