blob: 796725b665d310bb15807e97d1e5270c52b5fff6 [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
Tyler Gunn711d876fd2014-09-19 11:17:02 -070021import android.annotation.SystemApi;
Ihab Awadb8e85c72014-08-23 20:34:57 -070022import android.os.RemoteException;
Ihab Awadb8e85c72014-08-23 20:34:57 -070023
24import 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.
Tyler Gunn711d876fd2014-09-19 11:17:02 -070032 * @hide
Ihab Awadb8e85c72014-08-23 20:34:57 -070033 */
Tyler Gunn711d876fd2014-09-19 11:17:02 -070034@SystemApi
Ihab Awadb8e85c72014-08-23 20:34:57 -070035public final class RemoteConference {
36
Nancy Chen1d834f52014-09-05 11:03:21 -070037 public abstract static class Callback {
Ihab Awadb8e85c72014-08-23 20:34:57 -070038 public void onStateChanged(RemoteConference conference, int oldState, int newState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -070039 public void onDisconnected(RemoteConference conference, DisconnectCause disconnectCause) {}
Ihab Awadb8e85c72014-08-23 20:34:57 -070040 public void onConnectionAdded(RemoteConference conference, RemoteConnection connection) {}
41 public void onConnectionRemoved(RemoteConference conference, RemoteConnection connection) {}
42 public void onCapabilitiesChanged(RemoteConference conference, int capabilities) {}
43 public void onDestroyed(RemoteConference conference) {}
44 }
45
46 private final String mId;
47 private final IConnectionService mConnectionService;
48
Nancy Chen1d834f52014-09-05 11:03:21 -070049 private final Set<Callback> mCallbacks = new CopyOnWriteArraySet<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070050 private final List<RemoteConnection> mChildConnections = new CopyOnWriteArrayList<>();
51 private final List<RemoteConnection> mUnmodifiableChildConnections =
52 Collections.unmodifiableList(mChildConnections);
53
54 private int mState = Connection.STATE_NEW;
Andrew Lee7f3d41f2014-09-11 17:33:16 -070055 private DisconnectCause mDisconnectCause;
Ihab Awadb8e85c72014-08-23 20:34:57 -070056 private int mCallCapabilities;
Ihab Awadb8e85c72014-08-23 20:34:57 -070057
58 /** {@hide} */
59 RemoteConference(String id, IConnectionService connectionService) {
60 mId = id;
61 mConnectionService = connectionService;
62 }
63
64 /** {@hide} */
65 String getId() {
66 return mId;
67 }
68
69 /** {@hide} */
70 void setDestroyed() {
71 for (RemoteConnection connection : mChildConnections) {
72 connection.setConference(null);
73 }
Nancy Chen1d834f52014-09-05 11:03:21 -070074 for (Callback c : mCallbacks) {
75 c.onDestroyed(this);
Ihab Awadb8e85c72014-08-23 20:34:57 -070076 }
77 }
78
79 /** {@hide} */
80 void setState(int newState) {
81 if (newState != Connection.STATE_ACTIVE &&
82 newState != Connection.STATE_HOLDING &&
83 newState != Connection.STATE_DISCONNECTED) {
84 Log.w(this, "Unsupported state transition for Conference call.",
85 Connection.stateToString(newState));
86 return;
87 }
88
89 if (mState != newState) {
90 int oldState = mState;
91 mState = newState;
Nancy Chen1d834f52014-09-05 11:03:21 -070092 for (Callback c : mCallbacks) {
93 c.onStateChanged(this, oldState, newState);
Ihab Awadb8e85c72014-08-23 20:34:57 -070094 }
95 }
96 }
97
98 /** {@hide} */
99 void addConnection(RemoteConnection connection) {
100 if (!mChildConnections.contains(connection)) {
101 mChildConnections.add(connection);
102 connection.setConference(this);
Nancy Chen1d834f52014-09-05 11:03:21 -0700103 for (Callback c : mCallbacks) {
104 c.onConnectionAdded(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700105 }
106 }
107 }
108
109 /** {@hide} */
110 void removeConnection(RemoteConnection connection) {
111 if (mChildConnections.contains(connection)) {
112 mChildConnections.remove(connection);
113 connection.setConference(null);
Nancy Chen1d834f52014-09-05 11:03:21 -0700114 for (Callback c : mCallbacks) {
115 c.onConnectionRemoved(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700116 }
117 }
118 }
119
120 /** {@hide} */
121 void setCallCapabilities(int capabilities) {
122 if (mCallCapabilities != capabilities) {
123 mCallCapabilities = capabilities;
Nancy Chen1d834f52014-09-05 11:03:21 -0700124 for (Callback c : mCallbacks) {
125 c.onCapabilitiesChanged(this, mCallCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700126 }
127 }
128 }
129
130 /** {@hide} */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700131 void setDisconnected(DisconnectCause disconnectCause) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700132 if (mState != Connection.STATE_DISCONNECTED) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700133 mDisconnectCause = disconnectCause;
Ihab Awadb8e85c72014-08-23 20:34:57 -0700134 setState(Connection.STATE_DISCONNECTED);
Nancy Chen1d834f52014-09-05 11:03:21 -0700135 for (Callback c : mCallbacks) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700136 c.onDisconnected(this, disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700137 }
138 }
139 }
140
141 public final List<RemoteConnection> getConnections() {
142 return mUnmodifiableChildConnections;
143 }
144
145 public final int getState() {
146 return mState;
147 }
148
149 public final int getCallCapabilities() {
150 return mCallCapabilities;
151 }
152
153 public void disconnect() {
154 try {
155 mConnectionService.disconnect(mId);
156 } catch (RemoteException e) {
157 }
158 }
159
160 public void separate(RemoteConnection connection) {
161 if (mChildConnections.contains(connection)) {
162 try {
163 mConnectionService.splitFromConference(connection.getId());
164 } catch (RemoteException e) {
165 }
166 }
167 }
168
169 public void hold() {
170 try {
171 mConnectionService.hold(mId);
172 } catch (RemoteException e) {
173 }
174 }
175
176 public void unhold() {
177 try {
178 mConnectionService.unhold(mId);
179 } catch (RemoteException e) {
180 }
181 }
182
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700183 public DisconnectCause getDisconnectCause() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700184 return mDisconnectCause;
185 }
186
Yorke Lee58bacc52014-09-16 10:43:06 -0700187 public void playDtmfTone(char digit) {
188 try {
189 mConnectionService.playDtmfTone(mId, digit);
190 } catch (RemoteException e) {
191 }
192 }
193
194 public void stopDtmfTone() {
195 try {
196 mConnectionService.stopDtmfTone(mId);
197 } catch (RemoteException e) {
198 }
199 }
200
201 public void setAudioState(AudioState state) {
202 try {
203 mConnectionService.onAudioStateChanged(mId, state);
204 } catch (RemoteException e) {
205 }
206 }
207
Andrew Lee100e2932014-09-08 15:34:24 -0700208 public final void registerCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700209 mCallbacks.add(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700210 }
211
Andrew Lee100e2932014-09-08 15:34:24 -0700212 public final void unregisterCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700213 mCallbacks.remove(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700214 }
215}