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 android.car.admin;
18 
19 import android.annotation.IntDef;
20 import android.annotation.SystemApi;
21 import android.car.user.UserRemovalResult;
22 
23 import com.android.car.internal.util.DebugUtils;
24 import com.android.internal.annotations.VisibleForTesting;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 /**
30  * Result of a {@link CarDevicePolicyManager#removeUser(android.os.UserHandle)} operation.
31  *
32  * @hide
33  */
34 @SystemApi
35 public final class RemoveUserResult {
36 
37     /**
38      * User was removed.
39      */
40     public static final int STATUS_SUCCESS = 1;
41 
42     /**
43      * User was removed, and it was the last admin user.
44      */
45     public static final int STATUS_SUCCESS_LAST_ADMIN_REMOVED = 2;
46 
47     /**
48      * When the user is set as ephemeral so that it is scheduled for removal. This occurs when the
49      * user can't be immediately removed, such as when the current user is being removed.
50      */
51     public static final int STATUS_SUCCESS_SET_EPHEMERAL = 3;
52 
53     /**
54      * User was not removed because it doesn't exist.
55      */
56     public static final int STATUS_FAILURE_USER_DOES_NOT_EXIST = 4;
57 
58     /**
59      * User was not removed because arguments passed to the method were invalid.
60      */
61     public static final int STATUS_FAILURE_INVALID_ARGUMENTS = 5;
62 
63     /**
64      * When last admin user has been set as ephemeral so that it is scheduled for removal. This
65      * occurs when the user can't be immediately removed, such as when the current user is being
66      * removed.
67      */
68     public static final int STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL = 6;
69 
70     /**
71      * User was not removed for some other reason not described above.
72      */
73     public static final int STATUS_FAILURE_GENERIC = 100;
74 
75     /** @hide */
76     @IntDef(prefix = "STATUS_", value = {
77             STATUS_SUCCESS,
78             STATUS_SUCCESS_LAST_ADMIN_REMOVED,
79             STATUS_SUCCESS_SET_EPHEMERAL,
80             STATUS_FAILURE_USER_DOES_NOT_EXIST,
81             STATUS_FAILURE_INVALID_ARGUMENTS,
82             STATUS_FAILURE_GENERIC,
83             STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL
84     })
85     @Retention(RetentionPolicy.SOURCE)
86     public @interface Status {
87     }
88 
89     private final @Status int mStatus;
90 
91     /**
92      * Must mark as public even though unit test is on the same package, as actual classes are
93      * provided by different jar files.
94      *
95      * @hide
96      */
97     @VisibleForTesting
RemoveUserResult(@serRemovalResult.Status int status)98     public RemoveUserResult(@UserRemovalResult.Status int status) {
99         switch (status) {
100             case UserRemovalResult.STATUS_SUCCESSFUL:
101                 mStatus = STATUS_SUCCESS;
102                 break;
103             case UserRemovalResult.STATUS_SUCCESSFUL_LAST_ADMIN_REMOVED:
104                 mStatus = STATUS_SUCCESS_LAST_ADMIN_REMOVED;
105                 break;
106             case UserRemovalResult.STATUS_SUCCESSFUL_SET_EPHEMERAL:
107                 mStatus = STATUS_SUCCESS_SET_EPHEMERAL;
108                 break;
109             case UserRemovalResult.STATUS_USER_DOES_NOT_EXIST:
110                 mStatus = STATUS_FAILURE_USER_DOES_NOT_EXIST;
111                 break;
112             case UserRemovalResult.STATUS_INVALID_REQUEST:
113                 mStatus = STATUS_FAILURE_INVALID_ARGUMENTS;
114                 break;
115             case UserRemovalResult.STATUS_SUCCESSFUL_LAST_ADMIN_SET_EPHEMERAL:
116                 mStatus = STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL;
117                 break;
118             default:
119                 mStatus = STATUS_FAILURE_GENERIC;
120         }
121     }
122 
123     /**
124      * Gets the specific result of the operation.
125      *
126      * @return either {@link RemoveUserResult#STATUS_SUCCESS},
127      *         {@link RemoveUserResult#STATUS_SUCCESS_LAST_ADMIN_REMOVED},
128      *         {@link RemoveUserResult#STATUS_SUCCESS_SET_EPHEMERAL},
129      *         {@link RemoveUserResult#STATUS_FAILURE_USER_DOES_NOT_EXIST},
130      *         {@link RemoveUserResult#STATUS_FAILURE_INVALID_ARGUMENTS},
131      *         {@link RemoveUserResult#STATUS_FAILURE_GENERIC}, or
132      *         {@link RemoveUserResult#STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL}.
133      */
getStatus()134     public @Status int getStatus() {
135         return mStatus;
136     }
137 
138     /**
139      * Gets whether the operation was successful or not.
140      */
isSuccess()141     public boolean isSuccess() {
142         return mStatus == STATUS_SUCCESS || mStatus == STATUS_SUCCESS_LAST_ADMIN_REMOVED
143                 || mStatus == STATUS_SUCCESS_SET_EPHEMERAL
144                 || mStatus == STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL;
145     }
146 
147     @Override
toString()148     public String toString() {
149         return "RemoveUserResult[" + statusToString(mStatus) + "]";
150     }
151 
152     /** @hide */
statusToString(@tatus int status)153     public static String statusToString(@Status int status) {
154         return DebugUtils.valueToString(RemoveUserResult.class, "STATUS_", status);
155     }
156 }
157