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.bedstead.nene.users;
18 
19 import java.util.Set;
20 
21 /**
22  * Represents information about an Android User type.
23  */
24 public final class UserType {
25 
26     public static final String SECONDARY_USER_TYPE_NAME = "android.os.usertype.full.SECONDARY";
27     public static final String SYSTEM_USER_TYPE_NAME = "android.os.usertype.full.SYSTEM";
28     public static final String MANAGED_PROFILE_TYPE_NAME = "android.os.usertype.profile.MANAGED";
29 
30     public static final int UNLIMITED = -1;
31 
32     public enum BaseType {
33         SYSTEM, PROFILE, FULL
34     }
35 
36     static final class MutableUserType {
37         String mName;
38         Set<BaseType> mBaseType;
39         Boolean mEnabled;
40         Integer mMaxAllowed;
41         Integer mMaxAllowedPerParent;
42     }
43 
44     private final MutableUserType mMutableUserType;
45 
UserType(MutableUserType mutableUserType)46     UserType(MutableUserType mutableUserType) {
47         mMutableUserType = mutableUserType;
48     }
49 
name()50     public String name() {
51         return mMutableUserType.mName;
52     }
53 
baseType()54     public Set<BaseType> baseType() {
55         return mMutableUserType.mBaseType;
56     }
57 
enabled()58     public Boolean enabled() {
59         return mMutableUserType.mEnabled;
60     }
61 
62     /**
63      * The maximum number of this user type allowed on the device.
64      *
65      * <p>This value will be {@link #UNLIMITED} if there is no limit.
66      */
maxAllowed()67     public Integer maxAllowed() {
68         return mMutableUserType.mMaxAllowed;
69     }
70 
71     /**
72      * The maximum number of this user type allowed for a single parent profile
73      *
74      * <p>This value will be {@link #UNLIMITED} if there is no limit.
75      */
maxAllowedPerParent()76     public Integer maxAllowedPerParent() {
77         return mMutableUserType.mMaxAllowedPerParent;
78     }
79 
80     @Override
toString()81     public String toString() {
82         StringBuilder stringBuilder = new StringBuilder("UserType{");
83         stringBuilder.append("name=" + mMutableUserType.mName);
84         stringBuilder.append(", baseType=" + mMutableUserType.mBaseType);
85         stringBuilder.append(", enabled=" + mMutableUserType.mEnabled);
86         stringBuilder.append(", maxAllowed=" + mMutableUserType.mMaxAllowed);
87         stringBuilder.append(", maxAllowedPerParent=" + mMutableUserType.mMaxAllowedPerParent);
88         return stringBuilder.toString();
89     }
90 
91     @Override
hashCode()92     public int hashCode() {
93         return name().hashCode();
94     }
95 
96     @Override
equals(Object obj)97     public boolean equals(Object obj) {
98         if (obj == null || !(obj instanceof UserType)) {
99             return false;
100         }
101 
102         UserType other = (UserType) obj;
103         return other.name().equals(name());
104     }
105 }
106