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.Collections; 27 import java.util.Map; 28 import java.util.Objects; 29 import java.util.Set; 30 31 /** 32 * State of all roles for a user. 33 * 34 * TODO(b/147914847): Remove @hide when it becomes the default. 35 * @hide 36 */ 37 @SystemApi(client = Client.SYSTEM_SERVER) 38 public final class RolesState { 39 /** 40 * The version of the roles. 41 */ 42 private final int mVersion; 43 44 /** 45 * The hash of all packages in the system. 46 */ 47 @Nullable 48 private final String mPackagesHash; 49 50 /** 51 * The roles. 52 */ 53 @NonNull 54 private final Map<String, Set<String>> mRoles; 55 56 /** 57 * The names of roles with fallback enabled. 58 */ 59 @NonNull 60 private final Set<String> mFallbackEnabledRoles; 61 62 /** 63 * The active users for cross user roles. 64 */ 65 @NonNull 66 private final Map<String, Integer> mActiveUserIds; 67 68 /** 69 * Create a new instance of this class. 70 * 71 * @param version the version of the roles 72 * @param packagesHash the hash of all packages in the system 73 * @param roles the roles 74 */ RolesState(int version, @Nullable String packagesHash, @NonNull Map<String, Set<String>> roles)75 public RolesState(int version, @Nullable String packagesHash, 76 @NonNull Map<String, Set<String>> roles) { 77 this(version, packagesHash, roles, roles.keySet()); 78 } 79 80 /** 81 * Create a new instance of this class. 82 * 83 * @param version the version of the roles 84 * @param packagesHash the hash of all packages in the system 85 * @param roles the roles 86 * @param fallbackEnabledRoles the roles with fallback enabled 87 */ 88 @FlaggedApi(Flags.FLAG_SYSTEM_SERVER_ROLE_CONTROLLER_ENABLED) RolesState(int version, @Nullable String packagesHash, @NonNull Map<String, Set<String>> roles, @NonNull Set<String> fallbackEnabledRoles)89 public RolesState(int version, @Nullable String packagesHash, 90 @NonNull Map<String, Set<String>> roles, @NonNull Set<String> fallbackEnabledRoles) { 91 this(version, packagesHash, roles, fallbackEnabledRoles, Collections.emptyMap()); 92 } 93 94 /** 95 * Create a new instance of this class. 96 * 97 * @param version the version of the roles 98 * @param packagesHash the hash of all packages in the system 99 * @param roles the roles 100 * @param fallbackEnabledRoles the roles with fallback enabled 101 * @param activeUserIds the active users for cross user roles 102 * @hide 103 */ RolesState(int version, @Nullable String packagesHash, @NonNull Map<String, Set<String>> roles, @NonNull Set<String> fallbackEnabledRoles, @NonNull Map<String, Integer> activeUserIds)104 public RolesState(int version, @Nullable String packagesHash, 105 @NonNull Map<String, Set<String>> roles, @NonNull Set<String> fallbackEnabledRoles, 106 @NonNull Map<String, Integer> activeUserIds) { 107 mVersion = version; 108 mPackagesHash = packagesHash; 109 mRoles = roles; 110 mFallbackEnabledRoles = fallbackEnabledRoles; 111 mActiveUserIds = activeUserIds; 112 } 113 114 /** 115 * Get the version of the roles. 116 * 117 * @return the version of the roles 118 */ getVersion()119 public int getVersion() { 120 return mVersion; 121 } 122 123 /** 124 * Get the hash of all packages in the system. 125 * 126 * @return the hash of all packages in the system 127 */ 128 @Nullable getPackagesHash()129 public String getPackagesHash() { 130 return mPackagesHash; 131 } 132 133 /** 134 * Get the roles. 135 * 136 * @return the roles 137 */ 138 @NonNull getRoles()139 public Map<String, Set<String>> getRoles() { 140 return mRoles; 141 } 142 143 /** 144 * Get the fallback enabled roles. 145 * 146 * @return fallback enabled roles 147 */ 148 @NonNull 149 @FlaggedApi(Flags.FLAG_SYSTEM_SERVER_ROLE_CONTROLLER_ENABLED) getFallbackEnabledRoles()150 public Set<String> getFallbackEnabledRoles() { 151 return mFallbackEnabledRoles; 152 } 153 154 /** 155 * Get the active users for cross user roles. 156 * 157 * @return active users for cross user roles 158 * @hide 159 */ 160 @NonNull getActiveUserIds()161 public Map<String, Integer> getActiveUserIds() { 162 return mActiveUserIds; 163 } 164 165 @Override equals(Object object)166 public boolean equals(Object object) { 167 if (this == object) { 168 return true; 169 } 170 if (object == null || getClass() != object.getClass()) { 171 return false; 172 } 173 RolesState that = (RolesState) object; 174 return mVersion == that.mVersion 175 && Objects.equals(mPackagesHash, that.mPackagesHash) 176 && Objects.equals(mRoles, that.mRoles) 177 && Objects.equals(mFallbackEnabledRoles, that.mFallbackEnabledRoles) 178 && Objects.equals(mActiveUserIds, that.mActiveUserIds); 179 } 180 181 @Override hashCode()182 public int hashCode() { 183 return Objects.hash(mVersion, mPackagesHash, mRoles, mFallbackEnabledRoles, mActiveUserIds); 184 } 185 } 186