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