Conference.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;
20
21import java.util.Collections;
22import java.util.List;
23import java.util.Set;
24import java.util.concurrent.CopyOnWriteArrayList;
25import java.util.concurrent.CopyOnWriteArraySet;
26
27/**
28 * Represents a conference call which can contain any number of {@link Connection} objects.
29 * @hide
30 */
31@SystemApi
32public abstract class Conference {
33
34    /** @hide */
35    public abstract static class Listener {
36        public void onStateChanged(Conference conference, int oldState, int newState) {}
37        public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {}
38        public void onConnectionAdded(Conference conference, Connection connection) {}
39        public void onConnectionRemoved(Conference conference, Connection connection) {}
40        public void onDestroyed(Conference conference) {}
41        public void onCapabilitiesChanged(Conference conference, int capabilities) {}
42    }
43
44    private final Set<Listener> mListeners = new CopyOnWriteArraySet<>();
45    private final List<Connection> mChildConnections = new CopyOnWriteArrayList<>();
46    private final List<Connection> mUnmodifiableChildConnections =
47            Collections.unmodifiableList(mChildConnections);
48
49    private PhoneAccountHandle mPhoneAccount;
50    private AudioState mAudioState;
51    private int mState = Connection.STATE_NEW;
52    private DisconnectCause mDisconnectCause;
53    private int mCapabilities;
54    private String mDisconnectMessage;
55
56    /**
57     * Constructs a new Conference with a mandatory {@link PhoneAccountHandle}
58     *
59     * @param phoneAccount The {@code PhoneAccountHandle} associated with the conference.
60     */
61    public Conference(PhoneAccountHandle phoneAccount) {
62        mPhoneAccount = phoneAccount;
63    }
64
65    /**
66     * Returns the {@link PhoneAccountHandle} the conference call is being placed through.
67     *
68     * @return A {@code PhoneAccountHandle} object representing the PhoneAccount of the conference.
69     */
70    public final PhoneAccountHandle getPhoneAccountHandle() {
71        return mPhoneAccount;
72    }
73
74    /**
75     * Returns the list of connections currently associated with the conference call.
76     *
77     * @return A list of {@code Connection} objects which represent the children of the conference.
78     */
79    public final List<Connection> getConnections() {
80        return mUnmodifiableChildConnections;
81    }
82
83    /**
84     * Gets the state of the conference call. See {@link Connection} for valid values.
85     *
86     * @return A constant representing the state the conference call is currently in.
87     */
88    public final int getState() {
89        return mState;
90    }
91
92    /**
93     * Returns the capabilities of a conference. See {@link PhoneCapabilities} for valid values.
94     *
95     * @return A bitmask of the {@code PhoneCapabilities} of the conference call.
96     */
97    public final int getCapabilities() {
98        return mCapabilities;
99    }
100
101    /**
102     * @return The audio state of the conference, describing how its audio is currently
103     *         being routed by the system. This is {@code null} if this Conference
104     *         does not directly know about its audio state.
105     */
106    public final AudioState getAudioState() {
107        return mAudioState;
108    }
109
110    /**
111     * Invoked when the Conference and all it's {@link Connection}s should be disconnected.
112     */
113    public void onDisconnect() {}
114
115    /**
116     * Invoked when the specified {@link Connection} should be separated from the conference call.
117     *
118     * @param connection The connection to separate.
119     */
120    public void onSeparate(Connection connection) {}
121
122    /**
123     * Invoked when the conference should be put on hold.
124     */
125    public void onHold() {}
126
127    /**
128     * Invoked when the conference should be moved from hold to active.
129     */
130    public void onUnhold() {}
131
132    /**
133     * Invoked when the child calls should be merged. Only invoked if the conference contains the
134     * capability {@link PhoneCapabilities#MERGE_CONFERENCE}.
135     */
136    public void onMerge() {}
137
138    /**
139     * Invoked when the child calls should be swapped. Only invoked if the conference contains the
140     * capability {@link PhoneCapabilities#SWAP_CONFERENCE}.
141     */
142    public void onSwap() {}
143
144    /**
145     * Notifies this conference of a request to play a DTMF tone.
146     *
147     * @param c A DTMF character.
148     */
149    public void onPlayDtmfTone(char c) {}
150
151    /**
152     * Notifies this conference of a request to stop any currently playing DTMF tones.
153     */
154    public void onStopDtmfTone() {}
155
156    /**
157     * Notifies this conference that the {@link #getAudioState()} property has a new value.
158     *
159     * @param state The new call audio state.
160     */
161    public void onAudioStateChanged(AudioState state) {}
162
163    /**
164     * Sets state to be on hold.
165     */
166    public final void setOnHold() {
167        setState(Connection.STATE_HOLDING);
168    }
169
170    /**
171     * Sets state to be active.
172     */
173    public final void setActive() {
174        setState(Connection.STATE_ACTIVE);
175    }
176
177    /**
178     * Sets state to disconnected.
179     *
180     * @param disconnectCause The reason for the disconnection, as described by
181     *     {@link android.telecom.DisconnectCause}.
182     */
183    public final void setDisconnected(DisconnectCause disconnectCause) {
184        mDisconnectCause = disconnectCause;;
185        setState(Connection.STATE_DISCONNECTED);
186        for (Listener l : mListeners) {
187            l.onDisconnected(this, mDisconnectCause);
188        }
189    }
190
191    /**
192     * Sets the capabilities of a conference. See {@link PhoneCapabilities} for valid values.
193     *
194     * @param capabilities A bitmask of the {@code PhoneCapabilities} of the conference call.
195     */
196    public final void setCapabilities(int capabilities) {
197        if (capabilities != mCapabilities) {
198            mCapabilities = capabilities;
199
200            for (Listener l : mListeners) {
201                l.onCapabilitiesChanged(this, mCapabilities);
202            }
203        }
204    }
205
206    /**
207     * Adds the specified connection as a child of this conference.
208     *
209     * @param connection The connection to add.
210     * @return True if the connection was successfully added.
211     */
212    public final boolean addConnection(Connection connection) {
213        if (connection != null && !mChildConnections.contains(connection)) {
214            if (connection.setConference(this)) {
215                mChildConnections.add(connection);
216                for (Listener l : mListeners) {
217                    l.onConnectionAdded(this, connection);
218                }
219                return true;
220            }
221        }
222        return false;
223    }
224
225    /**
226     * Removes the specified connection as a child of this conference.
227     *
228     * @param connection The connection to remove.
229     */
230    public final void removeConnection(Connection connection) {
231        Log.d(this, "removing %s from %s", connection, mChildConnections);
232        if (connection != null && mChildConnections.remove(connection)) {
233            connection.resetConference();
234            for (Listener l : mListeners) {
235                l.onConnectionRemoved(this, connection);
236            }
237        }
238    }
239
240    /**
241     * Tears down the conference object and any of its current connections.
242     */
243    public final void destroy() {
244        Log.d(this, "destroying conference : %s", this);
245        // Tear down the children.
246        for (Connection connection : mChildConnections) {
247            Log.d(this, "removing connection %s", connection);
248            removeConnection(connection);
249        }
250
251        // If not yet disconnected, set the conference call as disconnected first.
252        if (mState != Connection.STATE_DISCONNECTED) {
253            Log.d(this, "setting to disconnected");
254            setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
255        }
256
257        // ...and notify.
258        for (Listener l : mListeners) {
259            l.onDestroyed(this);
260        }
261    }
262
263    /**
264     * Add a listener to be notified of a state change.
265     *
266     * @param listener The new listener.
267     * @return This conference.
268     * @hide
269     */
270    public final Conference addListener(Listener listener) {
271        mListeners.add(listener);
272        return this;
273    }
274
275    /**
276     * Removes the specified listener.
277     *
278     * @param listener The listener to remove.
279     * @return This conference.
280     * @hide
281     */
282    public final Conference removeListener(Listener listener) {
283        mListeners.remove(listener);
284        return this;
285    }
286
287    /**
288     * Inform this Conference that the state of its audio output has been changed externally.
289     *
290     * @param state The new audio state.
291     * @hide
292     */
293    final void setAudioState(AudioState state) {
294        Log.d(this, "setAudioState %s", state);
295        mAudioState = state;
296        onAudioStateChanged(state);
297    }
298
299    private void setState(int newState) {
300        if (newState != Connection.STATE_ACTIVE &&
301                newState != Connection.STATE_HOLDING &&
302                newState != Connection.STATE_DISCONNECTED) {
303            Log.w(this, "Unsupported state transition for Conference call.",
304                    Connection.stateToString(newState));
305            return;
306        }
307
308        if (mState != newState) {
309            int oldState = mState;
310            mState = newState;
311            for (Listener l : mListeners) {
312                l.onStateChanged(this, oldState, newState);
313            }
314        }
315    }
316}
317