blob: 996e0918491188d506a8251caeb2ef64861add38 [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;
22import android.telephony.DisconnectCause;
23
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.
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) {}
37 public void onDisconnected(RemoteConference conference, int cause, String message) {}
38 public void onConnectionAdded(RemoteConference conference, RemoteConnection connection) {}
39 public void onConnectionRemoved(RemoteConference conference, RemoteConnection connection) {}
40 public void onCapabilitiesChanged(RemoteConference conference, int capabilities) {}
41 public void onDestroyed(RemoteConference conference) {}
42 }
43
44 private final String mId;
45 private final IConnectionService mConnectionService;
46
Nancy Chen1d834f52014-09-05 11:03:21 -070047 private final Set<Callback> mCallbacks = new CopyOnWriteArraySet<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070048 private final List<RemoteConnection> mChildConnections = new CopyOnWriteArrayList<>();
49 private final List<RemoteConnection> mUnmodifiableChildConnections =
50 Collections.unmodifiableList(mChildConnections);
51
52 private int mState = Connection.STATE_NEW;
53 private int mDisconnectCause = DisconnectCause.NOT_VALID;
54 private int mCallCapabilities;
55 private String mDisconnectMessage;
56
57 /** {@hide} */
58 RemoteConference(String id, IConnectionService connectionService) {
59 mId = id;
60 mConnectionService = connectionService;
61 }
62
63 /** {@hide} */
64 String getId() {
65 return mId;
66 }
67
68 /** {@hide} */
69 void setDestroyed() {
70 for (RemoteConnection connection : mChildConnections) {
71 connection.setConference(null);
72 }
Nancy Chen1d834f52014-09-05 11:03:21 -070073 for (Callback c : mCallbacks) {
74 c.onDestroyed(this);
Ihab Awadb8e85c72014-08-23 20:34:57 -070075 }
76 }
77
78 /** {@hide} */
79 void setState(int newState) {
80 if (newState != Connection.STATE_ACTIVE &&
81 newState != Connection.STATE_HOLDING &&
82 newState != Connection.STATE_DISCONNECTED) {
83 Log.w(this, "Unsupported state transition for Conference call.",
84 Connection.stateToString(newState));
85 return;
86 }
87
88 if (mState != newState) {
89 int oldState = mState;
90 mState = newState;
Nancy Chen1d834f52014-09-05 11:03:21 -070091 for (Callback c : mCallbacks) {
92 c.onStateChanged(this, oldState, newState);
Ihab Awadb8e85c72014-08-23 20:34:57 -070093 }
94 }
95 }
96
97 /** {@hide} */
98 void addConnection(RemoteConnection connection) {
99 if (!mChildConnections.contains(connection)) {
100 mChildConnections.add(connection);
101 connection.setConference(this);
Nancy Chen1d834f52014-09-05 11:03:21 -0700102 for (Callback c : mCallbacks) {
103 c.onConnectionAdded(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700104 }
105 }
106 }
107
108 /** {@hide} */
109 void removeConnection(RemoteConnection connection) {
110 if (mChildConnections.contains(connection)) {
111 mChildConnections.remove(connection);
112 connection.setConference(null);
Nancy Chen1d834f52014-09-05 11:03:21 -0700113 for (Callback c : mCallbacks) {
114 c.onConnectionRemoved(this, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700115 }
116 }
117 }
118
119 /** {@hide} */
120 void setCallCapabilities(int capabilities) {
121 if (mCallCapabilities != capabilities) {
122 mCallCapabilities = capabilities;
Nancy Chen1d834f52014-09-05 11:03:21 -0700123 for (Callback c : mCallbacks) {
124 c.onCapabilitiesChanged(this, mCallCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700125 }
126 }
127 }
128
129 /** {@hide} */
130 void setDisconnected(int cause, String message) {
131 if (mState != Connection.STATE_DISCONNECTED) {
132 mDisconnectCause = cause;
133 mDisconnectMessage = message;
134 setState(Connection.STATE_DISCONNECTED);
Nancy Chen1d834f52014-09-05 11:03:21 -0700135 for (Callback c : mCallbacks) {
136 c.onDisconnected(this, cause, message);
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
183 public int getDisconnectCause() {
184 return mDisconnectCause;
185 }
186
187 public String getDisconnectMessage() {
188 return mDisconnectMessage;
189 }
190
Andrew Lee100e2932014-09-08 15:34:24 -0700191 public final void registerCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700192 mCallbacks.add(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700193 }
194
Andrew Lee100e2932014-09-08 15:34:24 -0700195 public final void unregisterCallback(Callback callback) {
Nancy Chen1d834f52014-09-05 11:03:21 -0700196 mCallbacks.remove(callback);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700197 }
198}