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.permissioncontroller.role.model;
18 
19 import android.os.Build;
20 
21 import androidx.annotation.NonNull;
22 
23 import com.android.modules.utils.build.SdkLevel;
24 
25 import java.util.Objects;
26 
27 /**
28  * A permission to be granted or revoke by a {@link Role}.
29  */
30 public class Permission {
31 
32     /**
33      * The name of the permission.
34      */
35     @NonNull
36     private final String mName;
37 
38     /**
39      * The minimum SDK version for this permission to be granted.
40      */
41     private final int mMinSdkVersion;
42 
Permission(@onNull String name, int minSdkVersion)43     public Permission(@NonNull String name, int minSdkVersion) {
44         mName = name;
45         mMinSdkVersion = minSdkVersion;
46     }
47 
48     @NonNull
getName()49     public String getName() {
50         return mName;
51     }
52 
getMinSdkVersion()53     public int getMinSdkVersion() {
54         return mMinSdkVersion;
55     }
56 
57     /**
58      * Check whether this permission is available.
59      *
60      * @return whether this permission is available
61      */
isAvailable()62     public boolean isAvailable() {
63         // Workaround to match the value 33+ for T+ in roles.xml before SDK finalization.
64         if (mMinSdkVersion >= 33) {
65             return SdkLevel.isAtLeastT();
66         } else {
67             return Build.VERSION.SDK_INT >= mMinSdkVersion;
68         }
69     }
70 
71     @Override
toString()72     public String toString() {
73         return "Permission{"
74                 + "mName='" + mName + '\''
75                 + ", mMinSdkVersion=" + mMinSdkVersion
76                 + '}';
77     }
78 
79     @Override
equals(Object object)80     public boolean equals(Object object) {
81         if (this == object) {
82             return true;
83         }
84         if (object == null || getClass() != object.getClass()) {
85             return false;
86         }
87         Permission that = (Permission) object;
88         return mMinSdkVersion == that.mMinSdkVersion
89                 && mName.equals(that.mName);
90     }
91 
92     @Override
hashCode()93     public int hashCode() {
94         return Objects.hash(mName, mMinSdkVersion);
95     }
96 }
97