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