1 /*
2  * Copyright (C) 2023 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 android.app.admin;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.HashSet;
26 import java.util.Objects;
27 import java.util.Set;
28 
29 /**
30  * Class used to identify the authority of the {@link EnforcingAdmin} based on a certain role/roles
31  * it holds.
32  *
33  * @hide
34  */
35 @SystemApi
36 public final class RoleAuthority extends Authority {
37     private final Set<String> mRoles;
38 
39     /**
40      * Constructor for a role authority that accepts the list of roles held by the admin.
41      */
RoleAuthority(@onNull Set<String> roles)42     public RoleAuthority(@NonNull Set<String> roles) {
43         mRoles = new HashSet<>(Objects.requireNonNull(roles));
44     }
45 
RoleAuthority(Parcel source)46     private RoleAuthority(Parcel source) {
47         mRoles = new HashSet<>();
48         int size = source.readInt();
49         for (int i = 0; i < size; i++) {
50             mRoles.add(source.readString());
51         }
52     }
53 
54     /**
55      * Returns the list of roles held by the associated admin.
56      */
57     @NonNull
getRoles()58     public Set<String> getRoles() {
59         return mRoles;
60     }
61 
62     @Override
describeContents()63     public int describeContents() {
64         return 0;
65     }
66 
67     @Override
writeToParcel(@onNull Parcel dest, int flags)68     public void writeToParcel(@NonNull Parcel dest, int flags) {
69         dest.writeInt(mRoles.size());
70         for (String role : mRoles) {
71             dest.writeString(role);
72         }
73     }
74 
75     @Override
equals(@ullable Object o)76     public boolean equals(@Nullable Object o) {
77         if (this == o) return true;
78         if (o == null || getClass() != o.getClass()) return false;
79         RoleAuthority other = (RoleAuthority) o;
80         return Objects.equals(mRoles, other.mRoles);
81     }
82 
83     @Override
hashCode()84     public int hashCode() {
85         return Objects.hash(mRoles);
86     }
87 
88     @Override
toString()89     public String toString() {
90         return "RoleAuthority { mRoles= " + mRoles + " }";
91     }
92 
93     @NonNull
94     public static final Parcelable.Creator<RoleAuthority> CREATOR =
95             new Parcelable.Creator<RoleAuthority>() {
96                 @Override
97                 public RoleAuthority createFromParcel(Parcel source) {
98                     return new RoleAuthority(source);
99                 }
100 
101                 @Override
102                 public RoleAuthority[] newArray(int size) {
103                     return new RoleAuthority[size];
104                 }
105             };
106 }
107