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.SystemApi; 21 import android.annotation.TestApi; 22 import android.car.annotation.AddedInOrBefore; 23 import android.car.user.UserRemovalResult; 24 25 import com.android.car.internal.util.DebugUtils; 26 import com.android.internal.annotations.VisibleForTesting; 27 28 import java.lang.annotation.Retention; 29 import java.lang.annotation.RetentionPolicy; 30 31 /** 32 * Result of a {@link CarDevicePolicyManager#removeUser(android.os.UserHandle)} operation. 33 * 34 * @hide 35 */ 36 @SystemApi 37 @TestApi 38 public final class RemoveUserResult { 39 40 /** 41 * User was removed. 42 */ 43 @AddedInOrBefore(majorVersion = 33) 44 public static final int STATUS_SUCCESS = 1; 45 46 /** 47 * User was removed, and it was the last admin user. 48 */ 49 @AddedInOrBefore(majorVersion = 33) 50 public static final int STATUS_SUCCESS_LAST_ADMIN_REMOVED = 2; 51 52 /** 53 * When the user is set as ephemeral so that it is scheduled for removal. This occurs when the 54 * user can't be immediately removed, such as when the current user is being removed. 55 */ 56 @AddedInOrBefore(majorVersion = 33) 57 public static final int STATUS_SUCCESS_SET_EPHEMERAL = 3; 58 59 /** 60 * User was not removed because it doesn't exist. 61 */ 62 @AddedInOrBefore(majorVersion = 33) 63 public static final int STATUS_FAILURE_USER_DOES_NOT_EXIST = 4; 64 65 /** 66 * User was not removed because arguments passed to the method were invalid. 67 */ 68 @AddedInOrBefore(majorVersion = 33) 69 public static final int STATUS_FAILURE_INVALID_ARGUMENTS = 5; 70 71 /** 72 * When last admin user has been set as ephemeral so that it is scheduled for removal. This 73 * occurs when the user can't be immediately removed, such as when the current user is being 74 * removed. 75 */ 76 @AddedInOrBefore(majorVersion = 33) 77 public static final int STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL = 6; 78 79 /** 80 * User was not removed for some other reason not described above. 81 */ 82 @AddedInOrBefore(majorVersion = 33) 83 public static final int STATUS_FAILURE_GENERIC = 100; 84 85 /** @hide */ 86 @IntDef(prefix = "STATUS_", value = { 87 STATUS_SUCCESS, 88 STATUS_SUCCESS_LAST_ADMIN_REMOVED, 89 STATUS_SUCCESS_SET_EPHEMERAL, 90 STATUS_FAILURE_USER_DOES_NOT_EXIST, 91 STATUS_FAILURE_INVALID_ARGUMENTS, 92 STATUS_FAILURE_GENERIC, 93 STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL 94 }) 95 @Retention(RetentionPolicy.SOURCE) 96 public @interface Status { 97 } 98 99 private final @Status int mStatus; 100 101 /** 102 * Must mark as public even though unit test is on the same package, as actual classes are 103 * provided by different jar files. 104 * 105 * @hide 106 */ 107 @VisibleForTesting RemoveUserResult(@serRemovalResult.Status int status)108 public RemoveUserResult(@UserRemovalResult.Status int status) { 109 switch (status) { 110 case UserRemovalResult.STATUS_SUCCESSFUL: 111 mStatus = STATUS_SUCCESS; 112 break; 113 case UserRemovalResult.STATUS_SUCCESSFUL_LAST_ADMIN_REMOVED: 114 mStatus = STATUS_SUCCESS_LAST_ADMIN_REMOVED; 115 break; 116 case UserRemovalResult.STATUS_SUCCESSFUL_SET_EPHEMERAL: 117 mStatus = STATUS_SUCCESS_SET_EPHEMERAL; 118 break; 119 case UserRemovalResult.STATUS_USER_DOES_NOT_EXIST: 120 mStatus = STATUS_FAILURE_USER_DOES_NOT_EXIST; 121 break; 122 case UserRemovalResult.STATUS_INVALID_REQUEST: 123 mStatus = STATUS_FAILURE_INVALID_ARGUMENTS; 124 break; 125 case UserRemovalResult.STATUS_SUCCESSFUL_LAST_ADMIN_SET_EPHEMERAL: 126 mStatus = STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL; 127 break; 128 default: 129 mStatus = STATUS_FAILURE_GENERIC; 130 } 131 } 132 133 /** 134 * Gets the specific result of the operation. 135 * 136 * @return either {@link RemoveUserResult#STATUS_SUCCESS}, 137 * {@link RemoveUserResult#STATUS_SUCCESS_LAST_ADMIN_REMOVED}, 138 * {@link RemoveUserResult#STATUS_SUCCESS_SET_EPHEMERAL}, 139 * {@link RemoveUserResult#STATUS_FAILURE_USER_DOES_NOT_EXIST}, 140 * {@link RemoveUserResult#STATUS_FAILURE_INVALID_ARGUMENTS}, 141 * {@link RemoveUserResult#STATUS_FAILURE_GENERIC}, or 142 * {@link RemoveUserResult#STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL}. 143 */ 144 @AddedInOrBefore(majorVersion = 33) getStatus()145 public @Status int getStatus() { 146 return mStatus; 147 } 148 149 /** 150 * Gets whether the operation was successful or not. 151 */ 152 @AddedInOrBefore(majorVersion = 33) isSuccess()153 public boolean isSuccess() { 154 return mStatus == STATUS_SUCCESS || mStatus == STATUS_SUCCESS_LAST_ADMIN_REMOVED 155 || mStatus == STATUS_SUCCESS_SET_EPHEMERAL 156 || mStatus == STATUS_SUCCESS_LAST_ADMIN_SET_EPHEMERAL; 157 } 158 159 @Override 160 @AddedInOrBefore(majorVersion = 33) toString()161 public String toString() { 162 return "RemoveUserResult[" + statusToString(mStatus) + "]"; 163 } 164 165 /** @hide */ 166 @AddedInOrBefore(majorVersion = 33) statusToString(@tatus int status)167 public static String statusToString(@Status int status) { 168 return DebugUtils.valueToString(RemoveUserResult.class, "STATUS_", status); 169 } 170 } 171