1 /*
2  * Copyright (C) 2021 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.server.permission.access
18 
19 import android.os.UserHandle
20 
21 sealed class AccessUri(
22     val scheme: String
23 ) {
24     override fun equals(other: Any?): Boolean {
25         throw NotImplementedError()
26     }
27 
28     override fun hashCode(): Int {
29         throw NotImplementedError()
30     }
31 
32     override fun toString(): String {
33         throw NotImplementedError()
34     }
35 }
36 
37 data class AppOpUri(
38     val appOpName: String
39 ) : AccessUri(SCHEME) {
40     override fun toString(): String = "$scheme:///$appOpName"
41 
42     companion object {
43         const val SCHEME = "app-op"
44     }
45 }
46 
47 data class PackageUri(
48     val packageName: String,
49     val userId: Int
50 ) : AccessUri(SCHEME) {
51     override fun toString(): String = "$scheme:///$packageName/$userId"
52 
53     companion object {
54         const val SCHEME = "package"
55     }
56 }
57 
58 data class PermissionUri(
59     val permissionName: String
60 ) : AccessUri(SCHEME) {
61     override fun toString(): String = "$scheme:///$permissionName"
62 
63     companion object {
64         const val SCHEME = "permission"
65     }
66 }
67 
68 data class UidUri(
69     val uid: Int
70 ) : AccessUri(SCHEME) {
71     val userId: Int
72         get() = UserHandle.getUserId(uid)
73 
74     val appId: Int
75         get() = UserHandle.getAppId(uid)
76 
77     override fun toString(): String = "$scheme:///$uid"
78 
79     companion object {
80         const val SCHEME = "uid"
81     }
82 }
83