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.Nullable;
21 import android.annotation.SystemApi;
22 import android.car.user.UserCreationResult;
23 import android.os.UserHandle;
24 import android.util.Slog;
25 
26 import com.android.car.internal.util.DebugUtils;
27 import com.android.internal.annotations.VisibleForTesting;
28 
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 
32 /**
33  * Result of a {@link CarDevicePolicyManager#createUser(String, int)} operation.
34  *
35  * @hide
36  */
37 @SystemApi
38 public final class CreateUserResult {
39 
40     private static final String TAG = CreateUserResult.class.getSimpleName();
41 
42     /**
43      * User was created.
44      */
45     public static final int STATUS_SUCCESS = 1;
46 
47     /**
48      * User was not created because arguments passed to the method were invalid.
49      */
50     public static final int STATUS_FAILURE_INVALID_ARGUMENTS = 2;
51 
52     /**
53      * User was not created 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_FAILURE_INVALID_ARGUMENTS,
61             STATUS_FAILURE_GENERIC
62     })
63     @Retention(RetentionPolicy.SOURCE)
64     public @interface Status {
65     }
66 
67     private final @Status int mStatus;
68     private final @Nullable UserHandle mUserHandle;
69 
70     /**
71      * Must mark as public even though unit test is on the same package, as actual classes are
72      * provided by different jar files.
73      *
74      * @hide
75      */
76     @VisibleForTesting
CreateUserResult(@ullable UserCreationResult result)77     public CreateUserResult(@Nullable UserCreationResult result) {
78         if (result == null) {
79             mStatus = STATUS_FAILURE_GENERIC;
80             mUserHandle = null;
81             return;
82         }
83         int status = result.getStatus();
84         if (status == UserCreationResult.STATUS_SUCCESSFUL) {
85             mUserHandle = result.getUser();
86             if (mUserHandle == null) {
87                 Slog.w(TAG, "Successful UserCreationResult with no user: " + result);
88                 mStatus = STATUS_FAILURE_GENERIC;
89             } else {
90                 mStatus = STATUS_SUCCESS;
91             }
92             return;
93         }
94 
95         mUserHandle = null;
96         switch (status) {
97             case UserCreationResult.STATUS_INVALID_REQUEST:
98                 mStatus = STATUS_FAILURE_INVALID_ARGUMENTS;
99                 break;
100             default:
101                 mStatus = STATUS_FAILURE_GENERIC;
102         }
103     }
104 
105     /**
106      * Return {@code CreateUserResult} with generic error.
107      *
108      * @hide
109      */
forGenericError()110     public static CreateUserResult forGenericError() {
111         return new CreateUserResult(null);
112     }
113 
114     /**
115      * Gets the specific result of the operation.
116      *
117      * @return either {@link CreateUserResult#STATUS_SUCCESS} or
118      *         {@link CreateUserResult#STATUS_FAILURE_GENERIC}.
119      */
getStatus()120     public @Status int getStatus() {
121         return mStatus;
122     }
123 
124     /**
125      * Gets whether the operation was successful or not.
126      */
isSuccess()127     public boolean isSuccess() {
128         return mStatus == STATUS_SUCCESS;
129     }
130 
131     /**
132      * Gets the {@link UserHandle} of the created user (or {@code null} in case of failure).
133      */
134     @Nullable
getUserHandle()135     public UserHandle getUserHandle() {
136         return mUserHandle;
137     }
138 
139     @Override
toString()140     public String toString() {
141         StringBuilder string = new StringBuilder("CreateUserResult[")
142                 .append(statusToString(mStatus));
143         if (mUserHandle != null) {
144             string.append(":").append(mUserHandle.getIdentifier());
145         }
146 
147         return string.append(']').toString();
148     }
149 
statusToString(int status)150     private static String statusToString(int status) {
151         return DebugUtils.valueToString(CreateUserResult.class, "STATUS_", status);
152     }
153 }
154