blob: fba3ee3265f9045ff76e314bf03d78da23f7c7ff [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
21import android.os.RemoteException;
Ihab Awadb8e85c72014-08-23 20:34:57 -070022
Ihab Awad50e35062014-09-30 09:17:03 -070023import java.util.ArrayList;
Ihab Awadb8e85c72014-08-23 20:34:57 -070024import java.util.Collections;
25import java.util.List;
26import java.util.Set;
27import java.util.concurrent.CopyOnWriteArrayList;
28import java.util.concurrent.CopyOnWriteArraySet;
29
30/**
31 * Represents a conference call which can contain any number of {@link Connection} objects.
32 */
33public final class RemoteConference {
34
Nancy Chen1d834f52014-09-05 11:03:21 -070035 public abstract static class Callback {
Ihab Awadb8e85c72014-08-23 20:34:57 -070036 public void onStateChanged(RemoteConference conference, int oldState, int newState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -070037 public void onDisconnected(RemoteConference conference, DisconnectCause disconnectCause) {}
Ihab Awadb8e85c72014-08-23 20:34:57 -070038 public void onConnectionAdded(RemoteConference conference, RemoteConnection connection) {}
39 public void onConnectionRemoved(RemoteConference conference, RemoteConnection connection) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -080040 public void onConnectionCapabilitiesChanged(
41 RemoteConference conference,
42 int connectionCapabilities) {}
Ihab Awad50e35062014-09-30 09:17:03 -070043 public void onConferenceableConnectionsChanged(
44 RemoteConference conference,
45 List<RemoteConnection> conferenceableConnections) {}
Ihab Awadb8e85c72014-08-23 20:34:57 -070046 public void onDestroyed(RemoteConference conference) {}
47 }
48
49 private final String mId;
50 private final IConnectionService mConnectionService;
51
Nancy Chen1d834f52014-09-05 11:03:21 -070052 private final Set<Callback> mCallbacks = new CopyOnWriteArraySet<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070053 private final List<RemoteConnection> mChildConnections = new CopyOnWriteArrayList<>();
54 private final List<RemoteConnection> mUnmodifiableChildConnections =
55 Collections.unmodifiableList(mChildConnections);
Ihab Awad50e35062014-09-30 09:17:03 -070056 private final List<RemoteConnection> mConferenceableConnections = new ArrayList<>();
57 private final List<RemoteConnection> mUnmodifiableConferenceableConnections =
58 Collections.unmodifiableList(mConferenceableConnections);
Ihab Awadb8e85c72014-08-23 20:34:57 -070059
60 private int mState = Connection.STATE_NEW;
Andrew Lee7f3d41f2014-09-11 17:33:16 -070061 private DisconnectCause mDisconnectCause;
Ihab Awad5c9c86e2014-11-12 13:41:16 -080062 private int mConnectionCapabilities;
Ihab Awadb8e85c72014-08-23 20:34:57 -070063
64 /** {@hide} */
65 RemoteConference(String id, IConnectionService connectionService) {
66 mId = id;
67 mConnectionService = connectionService;
68 }
69
70 /** {@hide} */
71 String getId() {
72 return mId;
73 }
74
75 /** {@hide} */
76 void setDestroyed() {
77 for (RemoteConnection connection : mChildConnections) {
78 connection.setConference(null);
79 }
Nancy Chen1d834f52014-09-05 11:03:21 -070080 for (Callback c : mCallbacks) {
81 c.onDestroyed(this);
Ihab Awadb8e85c72014-08-23 20:34:57 -070082 }
83 }
84
85 /** {@hide} */
86 void setState(int newState) {
87 if (newState != Connection.STATE_ACTIVE &&
88 newState != Connection.STATE_HOLDING &&
89 newState != Connection.STATE_DISCONNECTED) {
90 Log.w(this, "Unsupported state transition for Conference call.",
91 Connection.stateToString(newState));
92 return;
93 }
94
95 if (mState != newState) {
96 int oldState = mState;
97 mState = newState;
Nancy Chen1d834f52014-09-05 11:03:21 -070098 for (Callback c : mCallbacks) {
99 c.onStateChanged(this, oldState, newState);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700100 }
101 }
102 }
103
104 /** {@hide} */
105 void addConnection(RemoteConnection connection) {
106 if (!mChildConnections.contains(connection)) {
107 mChildConnections.add(connection);
108 connection.setConference(this);
Nancy Chen1d834f52014-09-05 11:03:21 -0700109 for (Callback c : mCallbacks) {
110 c.onConnectionAdded(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700111 }
112 }
113 }
114
115 /** {@hide} */
116 void removeConnection(RemoteConnection connection) {
117 if (mChildConnections.contains(connection)) {
118 mChildConnections.remove(connection);
119 connection.setConference(null);
Nancy Chen1d834f52014-09-05 11:03:21 -0700120 for (Callback c : mCallbacks) {
121 c.onConnectionRemoved(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700122 }
123 }
124 }
125
126 /** {@hide} */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800127 void setConnectionCapabilities(int connectionCapabilities) {
128 if (mConnectionCapabilities != connectionCapabilities) {
129 mConnectionCapabilities = connectionCapabilities;
Nancy Chen1d834f52014-09-05 11:03:21 -0700130 for (Callback c : mCallbacks) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800131 c.onConnectionCapabilitiesChanged(this, mConnectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700132 }
133 }
134 }
135
Ihab Awad50e35062014-09-30 09:17:03 -0700136 /** @hide */
137 void setConferenceableConnections(List<RemoteConnection> conferenceableConnections) {
138 mConferenceableConnections.clear();
139 mConferenceableConnections.addAll(conferenceableConnections);
140 for (Callback c : mCallbacks) {
141 c.onConferenceableConnectionsChanged(this, mUnmodifiableConferenceableConnections);
142 }
143 }
144
Ihab Awadb8e85c72014-08-23 20:34:57 -0700145 /** {@hide} */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700146 void setDisconnected(DisconnectCause disconnectCause) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700147 if (mState != Connection.STATE_DISCONNECTED) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700148 mDisconnectCause = disconnectCause;
Ihab Awadb8e85c72014-08-23 20:34:57 -0700149 setState(Connection.STATE_DISCONNECTED);
Nancy Chen1d834f52014-09-05 11:03:21 -0700150 for (Callback c : mCallbacks) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700151 c.onDisconnected(this, disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700152 }
153 }
154 }
155
156 public final List<RemoteConnection> getConnections() {
157 return mUnmodifiableChildConnections;
158 }
159
160 public final int getState() {
161 return mState;
162 }
163
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800164 public final int getConnectionCapabilities() {
165 return mConnectionCapabilities;
Ihab Awadb8e85c72014-08-23 20:34:57 -0700166 }
167
168 public void disconnect() {
169 try {
170 mConnectionService.disconnect(mId);
171 } catch (RemoteException e) {
172 }
173 }
174
175 public void separate(RemoteConnection connection) {
176 if (mChildConnections.contains(connection)) {
177 try {
178 mConnectionService.splitFromConference(connection.getId());
179 } catch (RemoteException e) {
180 }
181 }
182 }
183
mike dooley95ea5762014-09-25 14:49:21 -0700184 public void merge() {
185 try {
186 mConnectionService.mergeConference(mId);
187 } catch (RemoteException e) {
188 }
189 }
190
191 public void swap() {
192 try {
193 mConnectionService.swapConference(mId);
194 } catch (RemoteException e) {
195 }
196 }
197
Ihab Awadb8e85c72014-08-23 20:34:57 -0700198 public void hold() {
199 try {
200 mConnectionService.hold(mId);
201 } catch (RemoteException e) {
202 }
203 }
204
205 public void unhold() {
206 try {
207 mConnectionService.unhold(mId);
208 } catch (RemoteException e) {
209 }
210 }
211
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700212 public DisconnectCause getDisconnectCause() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700213 return mDisconnectCause;
214 }
215
Yorke Lee58bacc52014-09-16 10:43:06 -0700216 public void playDtmfTone(char digit) {
217 try {
218 mConnectionService.playDtmfTone(mId, digit);
219 } catch (RemoteException e) {
220 }
221 }
222
223 public void stopDtmfTone() {
224 try {
225 mConnectionService.stopDtmfTone(mId);
226 } catch (RemoteException e) {
227 }
228 }
229
230 public void setAudioState(AudioState state) {
231 try {
232 mConnectionService.onAudioStateChanged(mId, state);
233 } catch (RemoteException e) {
234 }
235 }
236
Ihab Awad50e35062014-09-30 09:17:03 -0700237 public List<RemoteConnection> getConferenceableConnections() {
238 return mUnmodifiableConferenceableConnections;
239 }
240
Andrew Lee100e2932014-09-08 15:34:24 -0700241 public final void registerCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700242 mCallbacks.add(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700243 }
244
Andrew Lee100e2932014-09-08 15:34:24 -0700245 public final void unregisterCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700246 mCallbacks.remove(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700247 }
248}