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