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.app.admin;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.content.pm.PackageManager;
24 import android.util.AndroidException;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 /**
30  * Thrown to indicate a failure during {@link DevicePolicyManager#provisionFullyManagedDevice} and
31  * {@link DevicePolicyManager#createAndProvisionManagedProfile}.
32  *
33  * @hide
34  *
35  */
36 @SystemApi
37 public class ProvisioningException extends AndroidException {
38 
39     /**
40      * Service-specific error code for {@link DevicePolicyManager#provisionFullyManagedDevice} and
41      * {@link DevicePolicyManager#createAndProvisionManagedProfile}:
42      * Indicates a generic failure.
43      */
44     public static final int ERROR_UNKNOWN = 0;
45 
46     /**
47      * Service-specific error code for {@link DevicePolicyManager#provisionFullyManagedDevice} and
48      * {@link DevicePolicyManager#createAndProvisionManagedProfile}:
49      * Indicates the call to {@link DevicePolicyManager#checkProvisioningPrecondition} returned an
50      * error code.
51      */
52     public static final int ERROR_PRE_CONDITION_FAILED = 1;
53 
54     /**
55      * Service-specific error code for {@link DevicePolicyManager#createAndProvisionManagedProfile}:
56      * Indicates that the profile creation failed.
57      */
58     public static final int ERROR_PROFILE_CREATION_FAILED = 2;
59 
60     /**
61      * Service-specific error code for {@link DevicePolicyManager#createAndProvisionManagedProfile}:
62      * Indicates the call to {@link PackageManager#installExistingPackageAsUser} has failed.
63      */
64     public static final int ERROR_ADMIN_PACKAGE_INSTALLATION_FAILED = 3;
65 
66     /**
67      * Service-specific error code for {@link DevicePolicyManager#createAndProvisionManagedProfile}:
68      * Indicates that setting the profile owner failed.
69      */
70     public static final int ERROR_SETTING_PROFILE_OWNER_FAILED = 4;
71 
72     /**
73      * Service-specific error code for {@link DevicePolicyManager#createAndProvisionManagedProfile}:
74      * Indicates that starting the newly created profile has failed.
75      */
76     public static final int ERROR_STARTING_PROFILE_FAILED = 5;
77 
78     /**
79      * Service-specific error code for {@link DevicePolicyManager#provisionFullyManagedDevice}:
80      * Indicates that removing the non required apps have failed.
81      */
82     public static final int ERROR_REMOVE_NON_REQUIRED_APPS_FAILED = 6;
83 
84     /**
85      * Service-specific error code for {@link DevicePolicyManager#provisionFullyManagedDevice}:
86      * Indicates that setting the device owner failed.
87      */
88     public static final int ERROR_SET_DEVICE_OWNER_FAILED = 7;
89 
90     /**
91      * Service-specific error codes for {@link DevicePolicyManager#createAndProvisionManagedProfile}
92      * and {@link DevicePolicyManager#provisionFullyManagedDevice} indicating all the errors
93      * during provisioning.
94      *
95      * @hide
96      */
97     @Retention(RetentionPolicy.SOURCE)
98     @IntDef(prefix = { "ERROR_" }, value = {
99             ERROR_UNKNOWN, ERROR_PRE_CONDITION_FAILED,
100             ERROR_PROFILE_CREATION_FAILED,
101             ERROR_ADMIN_PACKAGE_INSTALLATION_FAILED,
102             ERROR_SETTING_PROFILE_OWNER_FAILED,
103             ERROR_STARTING_PROFILE_FAILED,
104             ERROR_REMOVE_NON_REQUIRED_APPS_FAILED,
105             ERROR_SET_DEVICE_OWNER_FAILED
106     })
107     public @interface ProvisioningError {}
108 
109     private final @ProvisioningError int mProvisioningError;
110 
111     /**
112      * Constructs a {@link ProvisioningException}.
113      *
114      * @param cause the cause
115      * @param provisioningError the error code
116      */
ProvisioningException(@onNull Exception cause, @ProvisioningError int provisioningError)117     public ProvisioningException(@NonNull Exception cause,
118             @ProvisioningError int provisioningError) {
119         this(cause, provisioningError, /* errorMessage= */ null);
120     }
121 
122     /**
123      * Constructs a {@link ProvisioningException}.
124      *
125      * @param cause the cause
126      * @param provisioningError the error code
127      * @param errorMessage a {@code String} error message that give a more specific
128      *                     description of the exception; can be {@code null}
129      */
ProvisioningException(@onNull Exception cause, @ProvisioningError int provisioningError, @Nullable String errorMessage)130     public ProvisioningException(@NonNull Exception cause,
131             @ProvisioningError int provisioningError,
132             @Nullable String errorMessage) {
133         super(errorMessage, cause);
134         mProvisioningError = provisioningError;
135     }
136 
137     /**
138      * Returns the provisioning error specified at construction time.
139      */
getProvisioningError()140     public @ProvisioningError int getProvisioningError() {
141         return mProvisioningError;
142     }
143 }
144