blob: a76bf5998a5144bf4204058dc43dcadc5fc46eb2 [file] [log] [blame]
Ihab Awadb8e85c72014-08-23 20:34:57 -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
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awadb8e85c72014-08-23 20:34:57 -070018
Tyler Gunnef9f6f92014-09-12 22:16:17 -070019import com.android.internal.telecom.IConnectionService;
Ihab Awadb8e85c72014-08-23 20:34:57 -070020
Andrew Lee011728f2015-04-23 15:47:06 -070021import android.os.Handler;
Ihab Awadb8e85c72014-08-23 20:34:57 -070022import android.os.RemoteException;
Ihab Awadb8e85c72014-08-23 20:34:57 -070023
Ihab Awad50e35062014-09-30 09:17:03 -070024import java.util.ArrayList;
Ihab Awadb8e85c72014-08-23 20:34:57 -070025import java.util.Collections;
26import java.util.List;
27import java.util.Set;
28import java.util.concurrent.CopyOnWriteArrayList;
29import java.util.concurrent.CopyOnWriteArraySet;
30
31/**
32 * Represents a conference call which can contain any number of {@link Connection} objects.
33 */
34public final class RemoteConference {
35
Nancy Chen1d834f52014-09-05 11:03:21 -070036 public abstract static class Callback {
Ihab Awadb8e85c72014-08-23 20:34:57 -070037 public void onStateChanged(RemoteConference conference, int oldState, int newState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -070038 public void onDisconnected(RemoteConference conference, DisconnectCause disconnectCause) {}
Ihab Awadb8e85c72014-08-23 20:34:57 -070039 public void onConnectionAdded(RemoteConference conference, RemoteConnection connection) {}
40 public void onConnectionRemoved(RemoteConference conference, RemoteConnection connection) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -080041 public void onConnectionCapabilitiesChanged(
42 RemoteConference conference,
43 int connectionCapabilities) {}
Ihab Awad50e35062014-09-30 09:17:03 -070044 public void onConferenceableConnectionsChanged(
45 RemoteConference conference,
46 List<RemoteConnection> conferenceableConnections) {}
Ihab Awadb8e85c72014-08-23 20:34:57 -070047 public void onDestroyed(RemoteConference conference) {}
48 }
49
50 private final String mId;
51 private final IConnectionService mConnectionService;
52
Andrew Lee011728f2015-04-23 15:47:06 -070053 private final Set<CallbackRecord<Callback>> mCallbackRecords = new CopyOnWriteArraySet<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070054 private final List<RemoteConnection> mChildConnections = new CopyOnWriteArrayList<>();
55 private final List<RemoteConnection> mUnmodifiableChildConnections =
56 Collections.unmodifiableList(mChildConnections);
Ihab Awad50e35062014-09-30 09:17:03 -070057 private final List<RemoteConnection> mConferenceableConnections = new ArrayList<>();
58 private final List<RemoteConnection> mUnmodifiableConferenceableConnections =
59 Collections.unmodifiableList(mConferenceableConnections);
Ihab Awadb8e85c72014-08-23 20:34:57 -070060
61 private int mState = Connection.STATE_NEW;
Andrew Lee7f3d41f2014-09-11 17:33:16 -070062 private DisconnectCause mDisconnectCause;
Ihab Awad5c9c86e2014-11-12 13:41:16 -080063 private int mConnectionCapabilities;
Ihab Awadb8e85c72014-08-23 20:34:57 -070064
65 /** {@hide} */
66 RemoteConference(String id, IConnectionService connectionService) {
67 mId = id;
68 mConnectionService = connectionService;
69 }
70
71 /** {@hide} */
72 String getId() {
73 return mId;
74 }
75
76 /** {@hide} */
77 void setDestroyed() {
78 for (RemoteConnection connection : mChildConnections) {
79 connection.setConference(null);
80 }
Andrew Lee011728f2015-04-23 15:47:06 -070081 for (CallbackRecord<Callback> record : mCallbackRecords) {
82 final RemoteConference conference = this;
83 final Callback callback = record.getCallback();
84 record.getHandler().post(new Runnable() {
85 @Override
86 public void run() {
87 callback.onDestroyed(conference);
88 }
89 });
Ihab Awadb8e85c72014-08-23 20:34:57 -070090 }
91 }
92
93 /** {@hide} */
Andrew Lee011728f2015-04-23 15:47:06 -070094 void setState(final int newState) {
Ihab Awadb8e85c72014-08-23 20:34:57 -070095 if (newState != Connection.STATE_ACTIVE &&
96 newState != Connection.STATE_HOLDING &&
97 newState != Connection.STATE_DISCONNECTED) {
98 Log.w(this, "Unsupported state transition for Conference call.",
99 Connection.stateToString(newState));
100 return;
101 }
102
103 if (mState != newState) {
Andrew Lee011728f2015-04-23 15:47:06 -0700104 final int oldState = mState;
Ihab Awadb8e85c72014-08-23 20:34:57 -0700105 mState = newState;
Andrew Lee011728f2015-04-23 15:47:06 -0700106 for (CallbackRecord<Callback> record : mCallbackRecords) {
107 final RemoteConference conference = this;
108 final Callback callback = record.getCallback();
109 record.getHandler().post(new Runnable() {
110 @Override
111 public void run() {
112 callback.onStateChanged(conference, oldState, newState);
113 }
114 });
Ihab Awadb8e85c72014-08-23 20:34:57 -0700115 }
116 }
117 }
118
119 /** {@hide} */
Andrew Lee011728f2015-04-23 15:47:06 -0700120 void addConnection(final RemoteConnection connection) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700121 if (!mChildConnections.contains(connection)) {
122 mChildConnections.add(connection);
123 connection.setConference(this);
Andrew Lee011728f2015-04-23 15:47:06 -0700124 for (CallbackRecord<Callback> record : mCallbackRecords) {
125 final RemoteConference conference = this;
126 final Callback callback = record.getCallback();
127 record.getHandler().post(new Runnable() {
128 @Override
129 public void run() {
130 callback.onConnectionAdded(conference, connection);
131 }
132 });
Ihab Awadb8e85c72014-08-23 20:34:57 -0700133 }
134 }
135 }
136
137 /** {@hide} */
Andrew Lee011728f2015-04-23 15:47:06 -0700138 void removeConnection(final RemoteConnection connection) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700139 if (mChildConnections.contains(connection)) {
140 mChildConnections.remove(connection);
141 connection.setConference(null);
Andrew Lee011728f2015-04-23 15:47:06 -0700142 for (CallbackRecord<Callback> record : mCallbackRecords) {
143 final RemoteConference conference = this;
144 final Callback callback = record.getCallback();
145 record.getHandler().post(new Runnable() {
146 @Override
147 public void run() {
148 callback.onConnectionRemoved(conference, connection);
149 }
150 });
Ihab Awadb8e85c72014-08-23 20:34:57 -0700151 }
152 }
153 }
154
155 /** {@hide} */
Andrew Lee011728f2015-04-23 15:47:06 -0700156 void setConnectionCapabilities(final int connectionCapabilities) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800157 if (mConnectionCapabilities != connectionCapabilities) {
158 mConnectionCapabilities = connectionCapabilities;
Andrew Lee011728f2015-04-23 15:47:06 -0700159 for (CallbackRecord<Callback> record : mCallbackRecords) {
160 final RemoteConference conference = this;
161 final Callback callback = record.getCallback();
162 record.getHandler().post(new Runnable() {
163 @Override
164 public void run() {
165 callback.onConnectionCapabilitiesChanged(
166 conference, mConnectionCapabilities);
167 }
168 });
Ihab Awadb8e85c72014-08-23 20:34:57 -0700169 }
170 }
171 }
172
Ihab Awad50e35062014-09-30 09:17:03 -0700173 /** @hide */
174 void setConferenceableConnections(List<RemoteConnection> conferenceableConnections) {
175 mConferenceableConnections.clear();
176 mConferenceableConnections.addAll(conferenceableConnections);
Andrew Lee011728f2015-04-23 15:47:06 -0700177 for (CallbackRecord<Callback> record : mCallbackRecords) {
178 final RemoteConference conference = this;
179 final Callback callback = record.getCallback();
180 record.getHandler().post(new Runnable() {
181 @Override
182 public void run() {
183 callback.onConferenceableConnectionsChanged(
184 conference, mUnmodifiableConferenceableConnections);
185 }
186 });
Ihab Awad50e35062014-09-30 09:17:03 -0700187 }
188 }
189
Ihab Awadb8e85c72014-08-23 20:34:57 -0700190 /** {@hide} */
Andrew Lee011728f2015-04-23 15:47:06 -0700191 void setDisconnected(final DisconnectCause disconnectCause) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700192 if (mState != Connection.STATE_DISCONNECTED) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700193 mDisconnectCause = disconnectCause;
Ihab Awadb8e85c72014-08-23 20:34:57 -0700194 setState(Connection.STATE_DISCONNECTED);
Andrew Lee011728f2015-04-23 15:47:06 -0700195 for (CallbackRecord<Callback> record : mCallbackRecords) {
196 final RemoteConference conference = this;
197 final Callback callback = record.getCallback();
198 record.getHandler().post(new Runnable() {
199 @Override
200 public void run() {
201 callback.onDisconnected(conference, disconnectCause);
202 }
203 });
Ihab Awadb8e85c72014-08-23 20:34:57 -0700204 }
205 }
206 }
207
208 public final List<RemoteConnection> getConnections() {
209 return mUnmodifiableChildConnections;
210 }
211
212 public final int getState() {
213 return mState;
214 }
215
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800216 public final int getConnectionCapabilities() {
217 return mConnectionCapabilities;
Ihab Awadb8e85c72014-08-23 20:34:57 -0700218 }
219
220 public void disconnect() {
221 try {
222 mConnectionService.disconnect(mId);
223 } catch (RemoteException e) {
224 }
225 }
226
227 public void separate(RemoteConnection connection) {
228 if (mChildConnections.contains(connection)) {
229 try {
230 mConnectionService.splitFromConference(connection.getId());
231 } catch (RemoteException e) {
232 }
233 }
234 }
235
mike dooley95ea5762014-09-25 14:49:21 -0700236 public void merge() {
237 try {
238 mConnectionService.mergeConference(mId);
239 } catch (RemoteException e) {
240 }
241 }
242
243 public void swap() {
244 try {
245 mConnectionService.swapConference(mId);
246 } catch (RemoteException e) {
247 }
248 }
249
Ihab Awadb8e85c72014-08-23 20:34:57 -0700250 public void hold() {
251 try {
252 mConnectionService.hold(mId);
253 } catch (RemoteException e) {
254 }
255 }
256
257 public void unhold() {
258 try {
259 mConnectionService.unhold(mId);
260 } catch (RemoteException e) {
261 }
262 }
263
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700264 public DisconnectCause getDisconnectCause() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700265 return mDisconnectCause;
266 }
267
Yorke Lee58bacc52014-09-16 10:43:06 -0700268 public void playDtmfTone(char digit) {
269 try {
270 mConnectionService.playDtmfTone(mId, digit);
271 } catch (RemoteException e) {
272 }
273 }
274
275 public void stopDtmfTone() {
276 try {
277 mConnectionService.stopDtmfTone(mId);
278 } catch (RemoteException e) {
279 }
280 }
281
282 public void setAudioState(AudioState state) {
283 try {
284 mConnectionService.onAudioStateChanged(mId, state);
285 } catch (RemoteException e) {
286 }
287 }
288
Ihab Awad50e35062014-09-30 09:17:03 -0700289 public List<RemoteConnection> getConferenceableConnections() {
290 return mUnmodifiableConferenceableConnections;
291 }
292
Andrew Lee100e2932014-09-08 15:34:24 -0700293 public final void registerCallback(Callback callback) {
Andrew Lee011728f2015-04-23 15:47:06 -0700294 registerCallback(callback, new Handler());
295 }
296
297 public final void registerCallback(Callback callback, Handler handler) {
298 unregisterCallback(callback);
299 if (callback != null && handler != null) {
300 mCallbackRecords.add(new CallbackRecord(callback, handler));
301 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700302 }
303
Andrew Lee100e2932014-09-08 15:34:24 -0700304 public final void unregisterCallback(Callback callback) {
Andrew Lee011728f2015-04-23 15:47:06 -0700305 if (callback != null) {
306 for (CallbackRecord<Callback> record : mCallbackRecords) {
307 if (record.getCallback() == callback) {
308 mCallbackRecords.remove(record);
309 break;
310 }
311 }
312 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700313 }
314}