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