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.bluetooth;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.os.SystemClock;
22
23import java.util.UUID;
24
25/**
26 * This class represents a single call, its state and properties.
27 * It implements {@link Parcelable} for inter-process message passing.
28 * @hide
29 */
30public final class BluetoothHeadsetClientCall implements Parcelable {
31
32    /* Call state */
33    /**
34     * Call is active.
35     */
36    public static final int CALL_STATE_ACTIVE = 0;
37    /**
38     * Call is in held state.
39     */
40    public static final int CALL_STATE_HELD = 1;
41    /**
42     * Outgoing call that is being dialed right now.
43     */
44    public static final int CALL_STATE_DIALING = 2;
45    /**
46     * Outgoing call that remote party has already been alerted about.
47     */
48    public static final int CALL_STATE_ALERTING = 3;
49    /**
50     * Incoming call that can be accepted or rejected.
51     */
52    public static final int CALL_STATE_INCOMING = 4;
53    /**
54     * Waiting call state when there is already an active call.
55     */
56    public static final int CALL_STATE_WAITING = 5;
57    /**
58     * Call that has been held by response and hold
59     * (see Bluetooth specification for further references).
60     */
61    public static final int CALL_STATE_HELD_BY_RESPONSE_AND_HOLD = 6;
62    /**
63     * Call that has been already terminated and should not be referenced as a valid call.
64     */
65    public static final int CALL_STATE_TERMINATED = 7;
66
67    private final BluetoothDevice mDevice;
68    private final int mId;
69    private int mState;
70    private String mNumber;
71    private boolean mMultiParty;
72    private final boolean mOutgoing;
73    private final UUID mUUID;
74    private final long mCreationElapsedMilli;
75
76    /**
77     * Creates BluetoothHeadsetClientCall instance.
78     */
79    public BluetoothHeadsetClientCall(BluetoothDevice device, int id, int state, String number,
80            boolean multiParty, boolean outgoing) {
81        this(device, id, UUID.randomUUID(), state, number, multiParty, outgoing);
82    }
83
84    public BluetoothHeadsetClientCall(BluetoothDevice device, int id, UUID uuid, int state,
85            String number, boolean multiParty, boolean outgoing) {
86        mDevice = device;
87        mId = id;
88        mUUID = uuid;
89        mState = state;
90        mNumber = number != null ? number : "";
91        mMultiParty = multiParty;
92        mOutgoing = outgoing;
93        mCreationElapsedMilli = SystemClock.elapsedRealtime();
94    }
95
96    /**
97     * Sets call's state.
98     *
99     * <p>Note: This is an internal function and shouldn't be exposed</p>
100     *
101     * @param  state    new call state.
102     */
103    public void setState(int state) {
104        mState = state;
105    }
106
107    /**
108     * Sets call's number.
109     *
110     * <p>Note: This is an internal function and shouldn't be exposed</p>
111     *
112     * @param number    String representing phone number.
113     */
114    public void setNumber(String number) {
115        mNumber = number;
116    }
117
118    /**
119     * Sets this call as multi party call.
120     *
121     * <p>Note: This is an internal function and shouldn't be exposed</p>
122     *
123     * @param multiParty    if <code>true</code> sets this call as a part
124     *                      of multi party conference.
125     */
126    public void setMultiParty(boolean multiParty) {
127        mMultiParty = multiParty;
128    }
129
130    /**
131     * Gets call's device.
132     *
133     * @return call device.
134     */
135    public BluetoothDevice getDevice() {
136        return mDevice;
137    }
138
139    /**
140     * Gets call's Id.
141     *
142     * @return call id.
143     */
144    public int getId() {
145        return mId;
146    }
147
148    /**
149     * Gets call's UUID.
150     *
151     * @return call uuid
152     * @hide
153     */
154    public UUID getUUID() {
155        return mUUID;
156    }
157
158    /**
159     * Gets call's current state.
160     *
161     * @return state of this particular phone call.
162     */
163    public int getState() {
164        return mState;
165    }
166
167    /**
168     * Gets call's number.
169     *
170     * @return string representing phone number.
171     */
172    public String getNumber() {
173        return mNumber;
174    }
175
176    /**
177     * Gets call's creation time in millis since epoch.
178     *
179     * @return long representing the creation time.
180     */
181    public long getCreationElapsedMilli() {
182        return mCreationElapsedMilli;
183    }
184
185    /**
186     * Checks if call is an active call in a conference mode (aka multi party).
187     *
188     * @return <code>true</code> if call is a multi party call,
189     *         <code>false</code> otherwise.
190     */
191    public boolean isMultiParty() {
192        return mMultiParty;
193    }
194
195    /**
196     * Checks if this call is an outgoing call.
197     *
198     * @return <code>true</code> if its outgoing call,
199     *         <code>false</code> otherwise.
200     */
201    public boolean isOutgoing() {
202        return mOutgoing;
203    }
204
205    public String toString() {
206        return toString(false);
207    }
208
209    public String toString(boolean loggable) {
210        StringBuilder builder = new StringBuilder("BluetoothHeadsetClientCall{mDevice: ");
211        builder.append(loggable ? mDevice : mDevice.hashCode());
212        builder.append(", mId: ");
213        builder.append(mId);
214        builder.append(", mUUID: ");
215        builder.append(mUUID);
216        builder.append(", mState: ");
217        switch (mState) {
218            case CALL_STATE_ACTIVE: builder.append("ACTIVE"); break;
219            case CALL_STATE_HELD: builder.append("HELD"); break;
220            case CALL_STATE_DIALING: builder.append("DIALING"); break;
221            case CALL_STATE_ALERTING: builder.append("ALERTING"); break;
222            case CALL_STATE_INCOMING: builder.append("INCOMING"); break;
223            case CALL_STATE_WAITING: builder.append("WAITING"); break;
224            case CALL_STATE_HELD_BY_RESPONSE_AND_HOLD: builder.append("HELD_BY_RESPONSE_AND_HOLD"); break;
225            case CALL_STATE_TERMINATED: builder.append("TERMINATED"); break;
226            default: builder.append(mState); break;
227        }
228        builder.append(", mNumber: ");
229        builder.append(loggable ? mNumber : mNumber.hashCode());
230        builder.append(", mMultiParty: ");
231        builder.append(mMultiParty);
232        builder.append(", mOutgoing: ");
233        builder.append(mOutgoing);
234        builder.append("}");
235        return builder.toString();
236    }
237
238    /**
239     * {@link Parcelable.Creator} interface implementation.
240     */
241    public static final Parcelable.Creator<BluetoothHeadsetClientCall> CREATOR =
242            new Parcelable.Creator<BluetoothHeadsetClientCall>() {
243                @Override
244                public BluetoothHeadsetClientCall createFromParcel(Parcel in) {
245                    return new BluetoothHeadsetClientCall((BluetoothDevice)in.readParcelable(null),
246                            in.readInt(), UUID.fromString(in.readString()), in.readInt(),
247                            in.readString(), in.readInt() == 1, in.readInt() == 1);
248                }
249
250                @Override
251                public BluetoothHeadsetClientCall[] newArray(int size) {
252                    return new BluetoothHeadsetClientCall[size];
253                }
254            };
255
256    @Override
257    public void writeToParcel(Parcel out, int flags) {
258        out.writeParcelable(mDevice, 0);
259        out.writeInt(mId);
260        out.writeString(mUUID.toString());
261        out.writeInt(mState);
262        out.writeString(mNumber);
263        out.writeInt(mMultiParty ? 1 : 0);
264        out.writeInt(mOutgoing ? 1 : 0);
265    }
266
267    @Override
268    public int describeContents() {
269        return 0;
270    }
271}
272