blob: 206046d6db0b8e9589f4333e891641e534def4f2 [file] [log] [blame]
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001/*
2 * Copyright (C) 2014 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.os.Parcel;
20import android.os.Parcelable;
21import android.media.ToneGenerator;
22import android.text.TextUtils;
23
24import java.util.Objects;
25
26/**
27 * Describes the cause of a disconnected call. This always includes a code describing the generic
Santos Cordond9e614f2014-10-28 13:10:36 -070028 * cause of the disconnect. Optionally, it may include a label and/or description to display to the
29 * user. It is the responsibility of the {@link ConnectionService} to provide localized versions of
30 * the label and description. It also may contain a reason for the disconnect, which is intended for
31 * logging and not for display to the user.
Andrew Lee7f3d41f2014-09-11 17:33:16 -070032 */
33public final class DisconnectCause implements Parcelable {
34
35 /** Disconnected because of an unknown or unspecified reason. */
36 public static final int UNKNOWN = 0;
37 /** Disconnected because there was an error, such as a problem with the network. */
38 public static final int ERROR = 1;
39 /** Disconnected because of a local user-initiated action, such as hanging up. */
40 public static final int LOCAL = 2;
41 /**
42 * Disconnected because of a remote user-initiated action, such as the other party hanging up
43 * up.
44 */
45 public static final int REMOTE = 3;
46 /** Disconnected because it has been canceled. */
47 public static final int CANCELED = 4;
48 /** Disconnected because there was no response to an incoming call. */
49 public static final int MISSED = 5;
50 /** Disconnected because the user rejected an incoming call. */
51 public static final int REJECTED = 6;
52 /** Disconnected because the other party was busy. */
53 public static final int BUSY = 7;
54 /**
55 * Disconnected because of a restriction on placing the call, such as dialing in airplane
56 * mode.
57 */
58 public static final int RESTRICTED = 8;
59 /** Disconnected for reason not described by other disconnect codes. */
60 public static final int OTHER = 9;
61
62 private int mDisconnectCode;
63 private CharSequence mDisconnectLabel;
64 private CharSequence mDisconnectDescription;
65 private String mDisconnectReason;
66 private int mToneToPlay;
67
68 /**
69 * Creates a new DisconnectCause.
70 *
71 * @param code The code for the disconnect cause.
72 */
73 public DisconnectCause(int code) {
74 this(code, null, null, null, ToneGenerator.TONE_UNKNOWN);
75 }
76
77 /**
78 * Creates a new DisconnectCause.
79 *
80 * @param code The code for the disconnect cause.
81 * @param reason The reason for the disconnect.
82 */
83 public DisconnectCause(int code, String reason) {
84 this(code, null, null, reason, ToneGenerator.TONE_UNKNOWN);
85 }
86
87 /**
88 * Creates a new DisconnectCause.
Santos Cordond9e614f2014-10-28 13:10:36 -070089 *
Nancy Chenf4cf77c2014-09-19 10:53:21 -070090 * @param label The localized label to show to the user to explain the disconnect.
91 * @param code The code for the disconnect cause.
92 * @param description The localized description to show to the user to explain the disconnect.
93 * @param reason The reason for the disconnect.
94 */
95 public DisconnectCause(int code, CharSequence label, CharSequence description, String reason) {
96 this(code, label, description, reason, ToneGenerator.TONE_UNKNOWN);
97 }
98
99 /**
100 * Creates a new DisconnectCause.
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700101 *
102 * @param code The code for the disconnect cause.
103 * @param label The localized label to show to the user to explain the disconnect.
104 * @param description The localized description to show to the user to explain the disconnect.
105 * @param reason The reason for the disconnect.
106 * @param toneToPlay The tone to play on disconnect, as defined in {@link ToneGenerator}.
107 */
108 public DisconnectCause(int code, CharSequence label, CharSequence description, String reason,
109 int toneToPlay) {
110 mDisconnectCode = code;
111 mDisconnectLabel = label;
112 mDisconnectDescription = description;
113 mDisconnectReason = reason;
114 mToneToPlay = toneToPlay;
115 }
116
117 /**
118 * Returns the code for the reason for this disconnect.
119 *
120 * @return The disconnect code.
121 */
122 public int getCode() {
123 return mDisconnectCode;
124 }
125
126 /**
127 * Returns a short label which explains the reason for the disconnect cause and is for display
128 * in the user interface. The {@link ConnectionService } is responsible for providing and
129 * localizing this label. If there is no string provided, returns null.
130 *
131 * @return The disconnect label.
132 */
133 public CharSequence getLabel() {
134 return mDisconnectLabel;
135 }
136
137 /**
138 * Returns a description which explains the reason for the disconnect cause and is for display
139 * in the user interface. The {@link ConnectionService } is responsible for providing and
140 * localizing this message. If there is no string provided, returns null.
141 *
142 * @return The disconnect description.
143 */
144 public CharSequence getDescription() {
145 return mDisconnectDescription;
146 }
147
148 /**
149 * Returns an explanation of the reason for the disconnect. This is not intended for display to
150 * the user and is used mainly for logging.
151 *
152 * @return The disconnect reason.
153 */
154 public String getReason() {
155 return mDisconnectReason;
156 }
157
158 /**
159 * Returns the tone to play when disconnected.
160 *
161 * @return the tone as defined in {@link ToneGenerator} to play when disconnected.
162 */
163 public int getTone() {
164 return mToneToPlay;
165 }
166
167 public static final Creator<DisconnectCause> CREATOR = new Creator<DisconnectCause>() {
168 @Override
169 public DisconnectCause createFromParcel(Parcel source) {
170 int code = source.readInt();
171 CharSequence label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
172 CharSequence description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
173 String reason = source.readString();
174 int tone = source.readInt();
175 return new DisconnectCause(code, label, description, reason, tone);
176 }
177
178 @Override
179 public DisconnectCause[] newArray(int size) {
180 return new DisconnectCause[size];
181 }
182 };
183
184 @Override
185 public void writeToParcel(Parcel destination, int flags) {
186 destination.writeInt(mDisconnectCode);
187 TextUtils.writeToParcel(mDisconnectLabel, destination, flags);
188 TextUtils.writeToParcel(mDisconnectDescription, destination, flags);
189 destination.writeString(mDisconnectReason);
190 destination.writeInt(mToneToPlay);
191 }
192
193 @Override
194 public int describeContents() {
195 return 0;
196 }
197
198 @Override
199 public int hashCode() {
200 return Objects.hashCode(mDisconnectCode)
201 + Objects.hashCode(mDisconnectLabel)
202 + Objects.hashCode(mDisconnectDescription)
203 + Objects.hashCode(mDisconnectReason)
204 + Objects.hashCode(mToneToPlay);
205 }
206
207 @Override
208 public boolean equals(Object o) {
209 if (o instanceof DisconnectCause) {
210 DisconnectCause d = (DisconnectCause) o;
211 return Objects.equals(mDisconnectCode, d.getCode())
212 && Objects.equals(mDisconnectLabel, d.getLabel())
213 && Objects.equals(mDisconnectDescription, d.getDescription())
214 && Objects.equals(mDisconnectReason, d.getReason())
215 && Objects.equals(mToneToPlay, d.getTone());
216 }
217 return false;
218 }
219
220 @Override
221 public String toString() {
222 String code = "";
223 switch (getCode()) {
224 case ERROR:
225 code = "ERROR";
226 break;
227 case LOCAL:
228 code = "LOCAL";
229 break;
230 case REMOTE:
231 code = "REMOTE";
232 break;
233 case MISSED:
234 code = "MISSED";
235 break;
236 case REJECTED:
237 code = "REJECTED";
238 break;
239 case BUSY:
240 code = "BUSY";
241 break;
242 case RESTRICTED:
243 code = "RESTRICTED";
244 break;
245 case OTHER:
246 code = "OTHER";
247 break;
248 case UNKNOWN:
249 default:
250 code = "UNKNOWN";
251 }
252 String label = mDisconnectLabel == null ? "" : mDisconnectLabel.toString();
253 String description = mDisconnectDescription == null
254 ? "" : mDisconnectDescription.toString();
255 String reason = mDisconnectReason == null ? "" : mDisconnectReason;
256 return "DisconnectCause [ Code: (" + code + ")"
257 + " Label: (" + label + ")"
258 + " Description: (" + description + ")"
259 + " Reason: (" + reason + ")"
260 + " Tone: (" + mToneToPlay + ") ]";
261 }
262}