1 /*
2  * Copyright (C) 2020 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.role.persistence;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.annotation.SystemApi.Client;
24 import android.permission.flags.Flags;
25 
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Set;
29 
30 /**
31  * State of all roles.
32  *
33  * TODO(b/147914847): Remove @hide when it becomes the default.
34  * @hide
35  */
36 @SystemApi(client = Client.SYSTEM_SERVER)
37 public final class RolesState {
38     /**
39      * The version of the roles.
40      */
41     private final int mVersion;
42 
43     /**
44      * The hash of all packages in the system.
45      */
46     @Nullable
47     private final String mPackagesHash;
48 
49     /**
50      * The roles.
51      */
52     @NonNull
53     private final Map<String, Set<String>> mRoles;
54 
55     /**
56      * The names of roles with fallback enabled.
57      */
58     @NonNull
59     private final Set<String> mFallbackEnabledRoles;
60 
61     /**
62      * Create a new instance of this class.
63      *
64      * @param version the version of the roles
65      * @param packagesHash the hash of all packages in the system
66      * @param roles the roles
67      */
RolesState(int version, @Nullable String packagesHash, @NonNull Map<String, Set<String>> roles)68     public RolesState(int version, @Nullable String packagesHash,
69             @NonNull Map<String, Set<String>> roles) {
70         this(version, packagesHash, roles, roles.keySet());
71     }
72 
73     /**
74      * Create a new instance of this class.
75      *
76      * @param version the version of the roles
77      * @param packagesHash the hash of all packages in the system
78      * @param roles the roles
79      * @param fallbackEnabledRoles the roles with fallback enabled
80      */
81     @FlaggedApi(Flags.FLAG_SYSTEM_SERVER_ROLE_CONTROLLER_ENABLED)
RolesState(int version, @Nullable String packagesHash, @NonNull Map<String, Set<String>> roles, @NonNull Set<String> fallbackEnabledRoles)82     public RolesState(int version, @Nullable String packagesHash,
83             @NonNull Map<String, Set<String>> roles, @NonNull Set<String> fallbackEnabledRoles) {
84         mVersion = version;
85         mPackagesHash = packagesHash;
86         mRoles = roles;
87         mFallbackEnabledRoles = fallbackEnabledRoles;
88     }
89 
90     /**
91      * Get the version of the roles.
92      *
93      * @return the version of the roles
94      */
getVersion()95     public int getVersion() {
96         return mVersion;
97     }
98 
99     /**
100      * Get the hash of all packages in the system.
101      *
102      * @return the hash of all packages in the system
103      */
104     @Nullable
getPackagesHash()105     public String getPackagesHash() {
106         return mPackagesHash;
107     }
108 
109     /**
110      * Get the roles.
111      *
112      * @return the roles
113      */
114     @NonNull
getRoles()115     public Map<String, Set<String>> getRoles() {
116         return mRoles;
117     }
118 
119     /**
120      * Get the fallback enabled roles.
121      *
122      * @return fallback enabled roles
123      */
124     @NonNull
125     @FlaggedApi(Flags.FLAG_SYSTEM_SERVER_ROLE_CONTROLLER_ENABLED)
getFallbackEnabledRoles()126     public Set<String> getFallbackEnabledRoles() {
127         return mFallbackEnabledRoles;
128     }
129 
130     @Override
equals(Object object)131     public boolean equals(Object object) {
132         if (this == object) {
133             return true;
134         }
135         if (object == null || getClass() != object.getClass()) {
136             return false;
137         }
138         RolesState that = (RolesState) object;
139         return mVersion == that.mVersion
140                 && Objects.equals(mPackagesHash, that.mPackagesHash)
141                 && Objects.equals(mRoles, that.mRoles)
142                 && Objects.equals(mFallbackEnabledRoles, that.mFallbackEnabledRoles);
143     }
144 
145     @Override
hashCode()146     public int hashCode() {
147         return Objects.hash(mVersion, mPackagesHash, mRoles, mFallbackEnabledRoles);
148     }
149 }
150