blob: e223892847bc7c68962d288db3b949b735afd8e4 [file] [log] [blame]
Junhoedf3d822022-11-24 09:26:37 +00001/*
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
17package android.telecom;
18
19import android.annotation.IntDef;
20import android.annotation.Nullable;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.text.TextUtils;
24
25import androidx.annotation.NonNull;
26
27import java.lang.annotation.Retention;
28import 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 */
34public 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
62 public int describeContents() {
63 return 0;
64 }
65
66 @Override
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
95 public CallEndpointException(@Nullable String message) {
96 super(getMessage(message, ERROR_UNSPECIFIED));
97 mMessage = message;
98 }
99
100 public CallEndpointException(@Nullable String message, @CallEndpointErrorCode int code) {
101 super(getMessage(message, code));
102 mCode = code;
103 mMessage = message;
104 }
105
106 public CallEndpointException(@Nullable String message, @CallEndpointErrorCode int code,
107 @Nullable Throwable cause) {
108 super(getMessage(message, code), cause);
109 mCode = code;
110 mMessage = message;
111 }
112
113
114 public @CallEndpointErrorCode int getCode() {
115 return mCode;
116 }
117
118 private static String getMessage(String message, int code) {
119 StringBuilder builder;
120 if (!TextUtils.isEmpty(message)) {
121 builder = new StringBuilder(message);
122 builder.append(" (code: ");
123 builder.append(code);
124 builder.append(")");
125 return builder.toString();
126 } else {
127 return "code: " + code;
128 }
129 }
130}