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 package android.app.admin;
17 
18 import static android.app.admin.DevicePolicyManager.isValidOperationSafetyReason;
19 
20 import android.annotation.NonNull;
21 import android.annotation.TestApi;
22 import android.app.admin.DevicePolicyManager.DevicePolicyOperation;
23 import android.app.admin.DevicePolicyManager.OperationSafetyReason;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import com.android.internal.util.Preconditions;
28 
29 import java.util.Arrays;
30 import java.util.List;
31 
32 /**
33  * Exception thrown when a {@link android.app.admin.DevicePolicyManager} operation failed because it
34  * was not safe to be executed at that moment.
35  *
36  * <p>For example, it can be thrown on
37  * {@link android.content.pm.PackageManager#FEATURE_AUTOMOTIVE automotive devices} when the vehicle
38  * is moving.
39  */
40 @SuppressWarnings("serial")
41 public final class UnsafeStateException extends IllegalStateException implements Parcelable {
42 
43     private final @DevicePolicyOperation int mOperation;
44     private final @OperationSafetyReason int mReason;
45 
46     /** @hide */
47     @TestApi
UnsafeStateException(@evicePolicyOperation int operation, @OperationSafetyReason int reason)48     public UnsafeStateException(@DevicePolicyOperation int operation,
49             @OperationSafetyReason int reason) {
50         super();
51         Preconditions.checkArgument(isValidOperationSafetyReason(reason), "invalid reason %d",
52                 reason);
53         mOperation = operation;
54         mReason = reason;
55     }
56 
57     /** @hide */
58     @TestApi
getOperation()59     public @DevicePolicyOperation int getOperation() {
60         return mOperation;
61     }
62 
63     /**
64      * Gets the reasons the operation is unsafe.
65      *
66      * @return currently, only valid reason is
67      * {@link android.app.admin.DevicePolicyManager#OPERATION_SAFETY_REASON_DRIVING_DISTRACTION}.
68      */
69     @NonNull
getReasons()70     public List<Integer> getReasons() {
71         return Arrays.asList(mReason);
72     }
73 
74     /** @hide */
75     @Override
getMessage()76     public String getMessage() {
77         return DevicePolicyManager.operationSafetyReasonToString(mReason);
78     }
79 
80     @Override
describeContents()81     public int describeContents() {
82         return 0;
83     }
84 
85     @Override
writeToParcel(@onNull Parcel dest, int flags)86     public void writeToParcel(@NonNull Parcel dest, int flags) {
87         dest.writeInt(mOperation);
88         dest.writeInt(mReason);
89     }
90 
91     @NonNull
92     public static final Creator<UnsafeStateException> CREATOR =
93             new Creator<UnsafeStateException>() {
94 
95         @Override
96         public UnsafeStateException createFromParcel(Parcel source) {
97             return new UnsafeStateException(source.readInt(), source.readInt());
98         }
99 
100         @Override
101         public UnsafeStateException[] newArray(int size) {
102             return new UnsafeStateException[size];
103         }
104     };
105 }
106