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 android.car.admin;
18 
19 import android.annotation.IntDef;
20 import android.annotation.TestApi;
21 import android.car.user.UserStopResult;
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#stopUser} operation.
31  *
32  * @hide
33  */
34 @TestApi
35 public final class StopUserResult {
36 
37     /**
38      * User was stopped.
39      */
40     public static final int STATUS_SUCCESS = 1;
41 
42     /**
43      * User was the current user.
44      */
45     public static final int STATUS_FAILURE_CURRENT_USER = 2;
46 
47     /**
48      * User was the system user.
49      */
50     public static final int STATUS_FAILURE_SYSTEM_USER = 3;
51 
52     /**
53      * User was not stopped because it does not exist.
54      */
55     public static final int STATUS_FAILURE_USER_DOES_NOT_EXIST = 4;
56 
57     /**
58      * User was not stopped for some other reason not described above.
59      */
60     public static final int STATUS_FAILURE_GENERIC = 100;
61 
62     /** @hide */
63     @IntDef(prefix = "STATUS_", value = {
64             STATUS_SUCCESS,
65             STATUS_FAILURE_CURRENT_USER,
66             STATUS_FAILURE_SYSTEM_USER,
67             STATUS_FAILURE_USER_DOES_NOT_EXIST,
68             STATUS_FAILURE_GENERIC
69     })
70     @Retention(RetentionPolicy.SOURCE)
71     public @interface Status {
72     }
73 
74     private final @Status int mStatus;
75 
76     /** @hide */
77     @VisibleForTesting
StopUserResult(@serStopResult.Status int status)78     public StopUserResult(@UserStopResult.Status int status) {
79         switch(status) {
80             case UserStopResult.STATUS_SUCCESSFUL:
81                 mStatus = STATUS_SUCCESS;
82                 break;
83             case UserStopResult.STATUS_FAILURE_CURRENT_USER:
84                 mStatus = STATUS_FAILURE_CURRENT_USER;
85                 break;
86             case UserStopResult.STATUS_FAILURE_SYSTEM_USER:
87                 mStatus = STATUS_FAILURE_SYSTEM_USER;
88                 break;
89             case UserStopResult.STATUS_USER_DOES_NOT_EXIST:
90                 mStatus = STATUS_FAILURE_USER_DOES_NOT_EXIST;
91                 break;
92             default:
93                 mStatus = STATUS_FAILURE_GENERIC;
94         }
95     }
96 
97     /**
98      * Gets the specific result of the operation.
99      *
100      * @return either {@link StopUserResult#STATUS_SUCCESS},
101      *         {@link StopUserResult#STATUS_FAILURE_CURRENT_USER},
102      *         {@link StopUserResult#STATUS_FAILURE_SYSTEM_USER},
103      *         {@link StopUserResult#STATUS_FAILURE_USER_DOES_NOT_EXIST}, or
104      *         {@link StopUserResult#STATUS_FAILURE_GENERIC}.
105      */
getStatus()106     public @Status int getStatus() {
107         return mStatus;
108     }
109 
110     /**
111      * Gets whether the operation was successful or not.
112      */
isSuccess()113     public boolean isSuccess() {
114         return mStatus == STATUS_SUCCESS;
115     }
116 
117     @Override
toString()118     public String toString() {
119         return "StopUserResult[" + statusToString(mStatus) + "]";
120     }
121 
statusToString(int status)122     private static String statusToString(int status) {
123         return DebugUtils.valueToString(StopUserResult.class, "STATUS_", status);
124     }
125 }
126