blob: f931bc555330634f71c37435d384ff2c0ac71b9f [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
23import java.util.Collections;
24import java.util.List;
25import java.util.Set;
26import java.util.concurrent.CopyOnWriteArrayList;
27import java.util.concurrent.CopyOnWriteArraySet;
28
29/**
30 * Represents a conference call which can contain any number of {@link Connection} objects.
31 */
32public final class RemoteConference {
33
Nancy Chen1d834f52014-09-05 11:03:21 -070034 public abstract static class Callback {
Ihab Awadb8e85c72014-08-23 20:34:57 -070035 public void onStateChanged(RemoteConference conference, int oldState, int newState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -070036 public void onDisconnected(RemoteConference conference, DisconnectCause disconnectCause) {}
Ihab Awadb8e85c72014-08-23 20:34:57 -070037 public void onConnectionAdded(RemoteConference conference, RemoteConnection connection) {}
38 public void onConnectionRemoved(RemoteConference conference, RemoteConnection connection) {}
39 public void onCapabilitiesChanged(RemoteConference conference, int capabilities) {}
40 public void onDestroyed(RemoteConference conference) {}
41 }
42
43 private final String mId;
44 private final IConnectionService mConnectionService;
45
Nancy Chen1d834f52014-09-05 11:03:21 -070046 private final Set<Callback> mCallbacks = new CopyOnWriteArraySet<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070047 private final List<RemoteConnection> mChildConnections = new CopyOnWriteArrayList<>();
48 private final List<RemoteConnection> mUnmodifiableChildConnections =
49 Collections.unmodifiableList(mChildConnections);
50
51 private int mState = Connection.STATE_NEW;
Andrew Lee7f3d41f2014-09-11 17:33:16 -070052 private DisconnectCause mDisconnectCause;
Ihab Awadb8e85c72014-08-23 20:34:57 -070053 private int mCallCapabilities;
Ihab Awadb8e85c72014-08-23 20:34:57 -070054
55 /** {@hide} */
56 RemoteConference(String id, IConnectionService connectionService) {
57 mId = id;
58 mConnectionService = connectionService;
59 }
60
61 /** {@hide} */
62 String getId() {
63 return mId;
64 }
65
66 /** {@hide} */
67 void setDestroyed() {
68 for (RemoteConnection connection : mChildConnections) {
69 connection.setConference(null);
70 }
Nancy Chen1d834f52014-09-05 11:03:21 -070071 for (Callback c : mCallbacks) {
72 c.onDestroyed(this);
Ihab Awadb8e85c72014-08-23 20:34:57 -070073 }
74 }
75
76 /** {@hide} */
77 void setState(int newState) {
78 if (newState != Connection.STATE_ACTIVE &&
79 newState != Connection.STATE_HOLDING &&
80 newState != Connection.STATE_DISCONNECTED) {
81 Log.w(this, "Unsupported state transition for Conference call.",
82 Connection.stateToString(newState));
83 return;
84 }
85
86 if (mState != newState) {
87 int oldState = mState;
88 mState = newState;
Nancy Chen1d834f52014-09-05 11:03:21 -070089 for (Callback c : mCallbacks) {
90 c.onStateChanged(this, oldState, newState);
Ihab Awadb8e85c72014-08-23 20:34:57 -070091 }
92 }
93 }
94
95 /** {@hide} */
96 void addConnection(RemoteConnection connection) {
97 if (!mChildConnections.contains(connection)) {
98 mChildConnections.add(connection);
99 connection.setConference(this);
Nancy Chen1d834f52014-09-05 11:03:21 -0700100 for (Callback c : mCallbacks) {
101 c.onConnectionAdded(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700102 }
103 }
104 }
105
106 /** {@hide} */
107 void removeConnection(RemoteConnection connection) {
108 if (mChildConnections.contains(connection)) {
109 mChildConnections.remove(connection);
110 connection.setConference(null);
Nancy Chen1d834f52014-09-05 11:03:21 -0700111 for (Callback c : mCallbacks) {
112 c.onConnectionRemoved(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700113 }
114 }
115 }
116
117 /** {@hide} */
118 void setCallCapabilities(int capabilities) {
119 if (mCallCapabilities != capabilities) {
120 mCallCapabilities = capabilities;
Nancy Chen1d834f52014-09-05 11:03:21 -0700121 for (Callback c : mCallbacks) {
122 c.onCapabilitiesChanged(this, mCallCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700123 }
124 }
125 }
126
127 /** {@hide} */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700128 void setDisconnected(DisconnectCause disconnectCause) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700129 if (mState != Connection.STATE_DISCONNECTED) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700130 mDisconnectCause = disconnectCause;
Ihab Awadb8e85c72014-08-23 20:34:57 -0700131 setState(Connection.STATE_DISCONNECTED);
Nancy Chen1d834f52014-09-05 11:03:21 -0700132 for (Callback c : mCallbacks) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700133 c.onDisconnected(this, disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700134 }
135 }
136 }
137
138 public final List<RemoteConnection> getConnections() {
139 return mUnmodifiableChildConnections;
140 }
141
142 public final int getState() {
143 return mState;
144 }
145
146 public final int getCallCapabilities() {
147 return mCallCapabilities;
148 }
149
150 public void disconnect() {
151 try {
152 mConnectionService.disconnect(mId);
153 } catch (RemoteException e) {
154 }
155 }
156
157 public void separate(RemoteConnection connection) {
158 if (mChildConnections.contains(connection)) {
159 try {
160 mConnectionService.splitFromConference(connection.getId());
161 } catch (RemoteException e) {
162 }
163 }
164 }
165
166 public void hold() {
167 try {
168 mConnectionService.hold(mId);
169 } catch (RemoteException e) {
170 }
171 }
172
173 public void unhold() {
174 try {
175 mConnectionService.unhold(mId);
176 } catch (RemoteException e) {
177 }
178 }
179
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700180 public DisconnectCause getDisconnectCause() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700181 return mDisconnectCause;
182 }
183
Yorke Lee58bacc52014-09-16 10:43:06 -0700184 public void playDtmfTone(char digit) {
185 try {
186 mConnectionService.playDtmfTone(mId, digit);
187 } catch (RemoteException e) {
188 }
189 }
190
191 public void stopDtmfTone() {
192 try {
193 mConnectionService.stopDtmfTone(mId);
194 } catch (RemoteException e) {
195 }
196 }
197
198 public void setAudioState(AudioState state) {
199 try {
200 mConnectionService.onAudioStateChanged(mId, state);
201 } catch (RemoteException e) {
202 }
203 }
204
Andrew Lee100e2932014-09-08 15:34:24 -0700205 public final void registerCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700206 mCallbacks.add(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700207 }
208
Andrew Lee100e2932014-09-08 15:34:24 -0700209 public final void unregisterCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700210 mCallbacks.remove(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700211 }
212}