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.telecom;
18 
19 import android.annotation.IntDef;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.text.TextUtils;
24 
25 import androidx.annotation.NonNull;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 
30 /**
31  * This class represents a set of exceptions that can occur when requesting a
32  * {@link CallEndpoint} change.
33  */
34 public final class CallEndpointException extends RuntimeException implements Parcelable {
35     /** @hide */
36     public static final String CHANGE_ERROR = "ChangeErrorKey";
37 
38     /**
39      * The operation has failed because requested CallEndpoint does not exist.
40      */
41     public static final int ERROR_ENDPOINT_DOES_NOT_EXIST = 1;
42 
43     /**
44      * The operation was not completed on time.
45      */
46     public static final int ERROR_REQUEST_TIME_OUT = 2;
47 
48     /**
49      * The operation was canceled by another request.
50      */
51     public static final int ERROR_ANOTHER_REQUEST = 3;
52 
53     /**
54      * The operation has failed due to an unknown or unspecified error.
55      */
56     public static final int ERROR_UNSPECIFIED = 4;
57 
58     private int mCode = ERROR_UNSPECIFIED;
59     private final String mMessage;
60 
61     @Override
describeContents()62     public int describeContents() {
63         return 0;
64     }
65 
66     @Override
writeToParcel(@onNull Parcel dest, int flags)67     public void writeToParcel(@NonNull Parcel dest, int flags) {
68         dest.writeString8(mMessage);
69         dest.writeInt(mCode);
70     }
71 
72     /**
73      * Responsible for creating CallEndpointException objects for deserialized Parcels.
74      */
75     public static final @android.annotation.NonNull Parcelable.Creator<CallEndpointException>
76             CREATOR = new Parcelable.Creator<>() {
77                 @Override
78                 public CallEndpointException createFromParcel(Parcel source) {
79                     return new CallEndpointException(source.readString8(), source.readInt());
80                 }
81 
82                 @Override
83                 public CallEndpointException[] newArray(int size) {
84                     return new CallEndpointException[size];
85                 }
86             };
87 
88     /** @hide */
89     @Retention(RetentionPolicy.SOURCE)
90     @IntDef({ERROR_ENDPOINT_DOES_NOT_EXIST, ERROR_REQUEST_TIME_OUT, ERROR_ANOTHER_REQUEST,
91             ERROR_UNSPECIFIED})
92     public @interface CallEndpointErrorCode {
93     }
94 
CallEndpointException(@ullable String message, @CallEndpointErrorCode int code)95     public CallEndpointException(@Nullable String message, @CallEndpointErrorCode int code) {
96         super(getMessage(message, code));
97         mCode = code;
98         mMessage = message;
99     }
100 
getCode()101     public @CallEndpointErrorCode int getCode() {
102         return mCode;
103     }
104 
getMessage(String message, int code)105     private static String getMessage(String message, int code) {
106         StringBuilder builder;
107         if (!TextUtils.isEmpty(message)) {
108             builder = new StringBuilder(message);
109             builder.append(" (code: ");
110             builder.append(code);
111             builder.append(")");
112             return builder.toString();
113         } else {
114             return "code: " + code;
115         }
116     }
117 }
118