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