1 /*
2  * Copyright (C) 2022 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.sdksandbox;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Bundle;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 /** Exception thrown by {@link SdkSandboxManager#loadSdk} */
26 public final class LoadSdkException extends Exception implements Parcelable {
27 
28     @SdkSandboxManager.LoadSdkErrorCode private final int mLoadSdkErrorCode;
29     private final Bundle mExtraInformation;
30 
31     /**
32      * Initializes a {@link LoadSdkException} with a Throwable and a Bundle.
33      *
34      * @param cause The cause of the exception, which is saved for later retrieval by the {@link
35      *     #getCause()} method.
36      * @param extraInfo Extra error information. This is empty if there is no such information.
37      */
LoadSdkException(@onNull Throwable cause, @NonNull Bundle extraInfo)38     public LoadSdkException(@NonNull Throwable cause, @NonNull Bundle extraInfo) {
39         this(SdkSandboxManager.LOAD_SDK_SDK_DEFINED_ERROR, cause.getMessage(), cause, extraInfo);
40     }
41 
42     /**
43      * Initializes a {@link LoadSdkException} with a result code and a message
44      *
45      * @param loadSdkErrorCode The result code.
46      * @param message The detailed message which is saved for later retrieval by the {@link
47      *     #getMessage()} method.
48      * @hide
49      */
LoadSdkException( @dkSandboxManager.LoadSdkErrorCode int loadSdkErrorCode, @Nullable String message)50     public LoadSdkException(
51             @SdkSandboxManager.LoadSdkErrorCode int loadSdkErrorCode, @Nullable String message) {
52         this(loadSdkErrorCode, message, /*cause=*/ null);
53     }
54 
55     /**
56      * Initializes a {@link LoadSdkException} with a result code, a message and a cause.
57      *
58      * @param loadSdkErrorCode The result code.
59      * @param message The detailed message which is saved for later retrieval by the {@link
60      *     #getMessage()} method.
61      * @param cause The cause of the exception, which is saved for later retrieval by the {@link
62      *     #getCause()} method. A null value is permitted, and indicates that the cause is
63      *     nonexistent or unknown.
64      * @hide
65      */
LoadSdkException( @dkSandboxManager.LoadSdkErrorCode int loadSdkErrorCode, @Nullable String message, @Nullable Throwable cause)66     public LoadSdkException(
67             @SdkSandboxManager.LoadSdkErrorCode int loadSdkErrorCode,
68             @Nullable String message,
69             @Nullable Throwable cause) {
70         this(loadSdkErrorCode, message, cause, new Bundle());
71     }
72 
73     /**
74      * Initializes a {@link LoadSdkException} with a result code, a message, a cause and extra
75      * information.
76      *
77      * @param loadSdkErrorCode The result code.
78      * @param message The detailed message which is saved for later retrieval by the {@link
79      *     #getMessage()} method.
80      * @param cause The cause of the exception, which is saved for later retrieval by the {@link
81      *     #getCause()} method. A null value is permitted, and indicates that the cause is
82      *     nonexistent or unknown.
83      * @param extraInfo Extra error information. This is empty if there is no such information.
84      * @hide
85      */
LoadSdkException( @dkSandboxManager.LoadSdkErrorCode int loadSdkErrorCode, @Nullable String message, @Nullable Throwable cause, @NonNull Bundle extraInfo)86     public LoadSdkException(
87             @SdkSandboxManager.LoadSdkErrorCode int loadSdkErrorCode,
88             @Nullable String message,
89             @Nullable Throwable cause,
90             @NonNull Bundle extraInfo) {
91         super(message, cause);
92         mLoadSdkErrorCode = loadSdkErrorCode;
93         mExtraInformation = extraInfo;
94     }
95 
96     @NonNull
97     public static final Creator<LoadSdkException> CREATOR =
98             new Creator<LoadSdkException>() {
99                 @Override
100                 public LoadSdkException createFromParcel(Parcel in) {
101                     int errorCode = in.readInt();
102                     String message = in.readString();
103                     Bundle extraInformation = in.readBundle();
104                     return new LoadSdkException(errorCode, message, null, extraInformation);
105                 }
106 
107                 @Override
108                 public LoadSdkException[] newArray(int size) {
109                     return new LoadSdkException[size];
110                 }
111             };
112 
113     /**
114      * Returns the result code this exception was constructed with.
115      *
116      * @return The loadSdk result code.
117      */
118     @SdkSandboxManager.LoadSdkErrorCode
getLoadSdkErrorCode()119     public int getLoadSdkErrorCode() {
120         return mLoadSdkErrorCode;
121     }
122 
123     /**
124      * Returns the extra error information this exception was constructed with.
125      *
126      * @return The extra error information Bundle.
127      */
128     @NonNull
getExtraInformation()129     public Bundle getExtraInformation() {
130         return mExtraInformation;
131     }
132 
133     @Override
describeContents()134     public int describeContents() {
135         return 0;
136     }
137 
138     @Override
writeToParcel(@onNull Parcel destination, int flags)139     public void writeToParcel(@NonNull Parcel destination, int flags) {
140         destination.writeInt(mLoadSdkErrorCode);
141         destination.writeString(this.getMessage());
142         destination.writeBundle(mExtraInformation);
143     }
144 }
145