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