blob: a8879ae8aff45147be44f30181e7dd484c98666b [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
Evan Charlton0e094d92014-11-08 15:49:16 -080021import android.annotation.SystemApi;
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.
Evan Charlton0e094d92014-11-08 15:49:16 -080033 * @hide
Ihab Awadb8e85c72014-08-23 20:34:57 -070034 */
Evan Charlton0e094d92014-11-08 15:49:16 -080035@SystemApi
Ihab Awadb8e85c72014-08-23 20:34:57 -070036public final class RemoteConference {
37
Nancy Chen1d834f52014-09-05 11:03:21 -070038 public abstract static class Callback {
Ihab Awadb8e85c72014-08-23 20:34:57 -070039 public void onStateChanged(RemoteConference conference, int oldState, int newState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -070040 public void onDisconnected(RemoteConference conference, DisconnectCause disconnectCause) {}
Ihab Awadb8e85c72014-08-23 20:34:57 -070041 public void onConnectionAdded(RemoteConference conference, RemoteConnection connection) {}
42 public void onConnectionRemoved(RemoteConference conference, RemoteConnection connection) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -080043 public void onConnectionCapabilitiesChanged(
44 RemoteConference conference,
45 int connectionCapabilities) {}
Ihab Awad50e35062014-09-30 09:17:03 -070046 public void onConferenceableConnectionsChanged(
47 RemoteConference conference,
48 List<RemoteConnection> conferenceableConnections) {}
Ihab Awadb8e85c72014-08-23 20:34:57 -070049 public void onDestroyed(RemoteConference conference) {}
50 }
51
52 private final String mId;
53 private final IConnectionService mConnectionService;
54
Nancy Chen1d834f52014-09-05 11:03:21 -070055 private final Set<Callback> mCallbacks = new CopyOnWriteArraySet<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070056 private final List<RemoteConnection> mChildConnections = new CopyOnWriteArrayList<>();
57 private final List<RemoteConnection> mUnmodifiableChildConnections =
58 Collections.unmodifiableList(mChildConnections);
Ihab Awad50e35062014-09-30 09:17:03 -070059 private final List<RemoteConnection> mConferenceableConnections = new ArrayList<>();
60 private final List<RemoteConnection> mUnmodifiableConferenceableConnections =
61 Collections.unmodifiableList(mConferenceableConnections);
Ihab Awadb8e85c72014-08-23 20:34:57 -070062
63 private int mState = Connection.STATE_NEW;
Andrew Lee7f3d41f2014-09-11 17:33:16 -070064 private DisconnectCause mDisconnectCause;
Ihab Awad5c9c86e2014-11-12 13:41:16 -080065 private int mConnectionCapabilities;
Ihab Awadb8e85c72014-08-23 20:34:57 -070066
67 /** {@hide} */
68 RemoteConference(String id, IConnectionService connectionService) {
69 mId = id;
70 mConnectionService = connectionService;
71 }
72
73 /** {@hide} */
74 String getId() {
75 return mId;
76 }
77
78 /** {@hide} */
79 void setDestroyed() {
80 for (RemoteConnection connection : mChildConnections) {
81 connection.setConference(null);
82 }
Nancy Chen1d834f52014-09-05 11:03:21 -070083 for (Callback c : mCallbacks) {
84 c.onDestroyed(this);
Ihab Awadb8e85c72014-08-23 20:34:57 -070085 }
86 }
87
88 /** {@hide} */
89 void setState(int newState) {
90 if (newState != Connection.STATE_ACTIVE &&
91 newState != Connection.STATE_HOLDING &&
92 newState != Connection.STATE_DISCONNECTED) {
93 Log.w(this, "Unsupported state transition for Conference call.",
94 Connection.stateToString(newState));
95 return;
96 }
97
98 if (mState != newState) {
99 int oldState = mState;
100 mState = newState;
Nancy Chen1d834f52014-09-05 11:03:21 -0700101 for (Callback c : mCallbacks) {
102 c.onStateChanged(this, oldState, newState);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700103 }
104 }
105 }
106
107 /** {@hide} */
108 void addConnection(RemoteConnection connection) {
109 if (!mChildConnections.contains(connection)) {
110 mChildConnections.add(connection);
111 connection.setConference(this);
Nancy Chen1d834f52014-09-05 11:03:21 -0700112 for (Callback c : mCallbacks) {
113 c.onConnectionAdded(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700114 }
115 }
116 }
117
118 /** {@hide} */
119 void removeConnection(RemoteConnection connection) {
120 if (mChildConnections.contains(connection)) {
121 mChildConnections.remove(connection);
122 connection.setConference(null);
Nancy Chen1d834f52014-09-05 11:03:21 -0700123 for (Callback c : mCallbacks) {
124 c.onConnectionRemoved(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700125 }
126 }
127 }
128
129 /** {@hide} */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800130 void setConnectionCapabilities(int connectionCapabilities) {
131 if (mConnectionCapabilities != connectionCapabilities) {
132 mConnectionCapabilities = connectionCapabilities;
Nancy Chen1d834f52014-09-05 11:03:21 -0700133 for (Callback c : mCallbacks) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800134 c.onConnectionCapabilitiesChanged(this, mConnectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700135 }
136 }
137 }
138
Ihab Awad50e35062014-09-30 09:17:03 -0700139 /** @hide */
140 void setConferenceableConnections(List<RemoteConnection> conferenceableConnections) {
141 mConferenceableConnections.clear();
142 mConferenceableConnections.addAll(conferenceableConnections);
143 for (Callback c : mCallbacks) {
144 c.onConferenceableConnectionsChanged(this, mUnmodifiableConferenceableConnections);
145 }
146 }
147
Ihab Awadb8e85c72014-08-23 20:34:57 -0700148 /** {@hide} */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700149 void setDisconnected(DisconnectCause disconnectCause) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700150 if (mState != Connection.STATE_DISCONNECTED) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700151 mDisconnectCause = disconnectCause;
Ihab Awadb8e85c72014-08-23 20:34:57 -0700152 setState(Connection.STATE_DISCONNECTED);
Nancy Chen1d834f52014-09-05 11:03:21 -0700153 for (Callback c : mCallbacks) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700154 c.onDisconnected(this, disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700155 }
156 }
157 }
158
159 public final List<RemoteConnection> getConnections() {
160 return mUnmodifiableChildConnections;
161 }
162
163 public final int getState() {
164 return mState;
165 }
166
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800167 /** @hide */
168 @Deprecated public final int getCallCapabilities() {
169 return getConnectionCapabilities();
170 }
171
172 public final int getConnectionCapabilities() {
173 return mConnectionCapabilities;
Ihab Awadb8e85c72014-08-23 20:34:57 -0700174 }
175
176 public void disconnect() {
177 try {
178 mConnectionService.disconnect(mId);
179 } catch (RemoteException e) {
180 }
181 }
182
183 public void separate(RemoteConnection connection) {
184 if (mChildConnections.contains(connection)) {
185 try {
186 mConnectionService.splitFromConference(connection.getId());
187 } catch (RemoteException e) {
188 }
189 }
190 }
191
mike dooley95ea5762014-09-25 14:49:21 -0700192 public void merge() {
193 try {
194 mConnectionService.mergeConference(mId);
195 } catch (RemoteException e) {
196 }
197 }
198
199 public void swap() {
200 try {
201 mConnectionService.swapConference(mId);
202 } catch (RemoteException e) {
203 }
204 }
205
Ihab Awadb8e85c72014-08-23 20:34:57 -0700206 public void hold() {
207 try {
208 mConnectionService.hold(mId);
209 } catch (RemoteException e) {
210 }
211 }
212
213 public void unhold() {
214 try {
215 mConnectionService.unhold(mId);
216 } catch (RemoteException e) {
217 }
218 }
219
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700220 public DisconnectCause getDisconnectCause() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700221 return mDisconnectCause;
222 }
223
Yorke Lee58bacc52014-09-16 10:43:06 -0700224 public void playDtmfTone(char digit) {
225 try {
226 mConnectionService.playDtmfTone(mId, digit);
227 } catch (RemoteException e) {
228 }
229 }
230
231 public void stopDtmfTone() {
232 try {
233 mConnectionService.stopDtmfTone(mId);
234 } catch (RemoteException e) {
235 }
236 }
237
238 public void setAudioState(AudioState state) {
239 try {
240 mConnectionService.onAudioStateChanged(mId, state);
241 } catch (RemoteException e) {
242 }
243 }
244
Ihab Awad50e35062014-09-30 09:17:03 -0700245 public List<RemoteConnection> getConferenceableConnections() {
246 return mUnmodifiableConferenceableConnections;
247 }
248
Andrew Lee100e2932014-09-08 15:34:24 -0700249 public final void registerCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700250 mCallbacks.add(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700251 }
252
Andrew Lee100e2932014-09-08 15:34:24 -0700253 public final void unregisterCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700254 mCallbacks.remove(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700255 }
256}