| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 17 | package android.telecom; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 18 | |
| Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 19 | import com.android.internal.telecom.IConnectionService; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 20 | |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 21 | import android.os.Handler; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 23 | |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 25 | import java.util.Collections; |
| 26 | import java.util.List; |
| 27 | import java.util.Set; |
| 28 | import java.util.concurrent.CopyOnWriteArrayList; |
| 29 | import java.util.concurrent.CopyOnWriteArraySet; |
| 30 | |
| 31 | /** |
| 32 | * Represents a conference call which can contain any number of {@link Connection} objects. |
| 33 | */ |
| 34 | public final class RemoteConference { |
| 35 | |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 36 | public abstract static class Callback { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 37 | public void onStateChanged(RemoteConference conference, int oldState, int newState) {} |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 38 | public void onDisconnected(RemoteConference conference, DisconnectCause disconnectCause) {} |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 39 | public void onConnectionAdded(RemoteConference conference, RemoteConnection connection) {} |
| 40 | public void onConnectionRemoved(RemoteConference conference, RemoteConnection connection) {} |
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 41 | public void onConnectionCapabilitiesChanged( |
| 42 | RemoteConference conference, |
| 43 | int connectionCapabilities) {} |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 44 | public void onConferenceableConnectionsChanged( |
| 45 | RemoteConference conference, |
| 46 | List<RemoteConnection> conferenceableConnections) {} |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 47 | public void onDestroyed(RemoteConference conference) {} |
| 48 | } |
| 49 | |
| 50 | private final String mId; |
| 51 | private final IConnectionService mConnectionService; |
| 52 | |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 53 | private final Set<CallbackRecord<Callback>> mCallbackRecords = new CopyOnWriteArraySet<>(); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 54 | private final List<RemoteConnection> mChildConnections = new CopyOnWriteArrayList<>(); |
| 55 | private final List<RemoteConnection> mUnmodifiableChildConnections = |
| 56 | Collections.unmodifiableList(mChildConnections); |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 57 | private final List<RemoteConnection> mConferenceableConnections = new ArrayList<>(); |
| 58 | private final List<RemoteConnection> mUnmodifiableConferenceableConnections = |
| 59 | Collections.unmodifiableList(mConferenceableConnections); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 60 | |
| 61 | private int mState = Connection.STATE_NEW; |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 62 | private DisconnectCause mDisconnectCause; |
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 63 | private int mConnectionCapabilities; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 64 | |
| 65 | /** {@hide} */ |
| 66 | RemoteConference(String id, IConnectionService connectionService) { |
| 67 | mId = id; |
| 68 | mConnectionService = connectionService; |
| 69 | } |
| 70 | |
| 71 | /** {@hide} */ |
| 72 | String getId() { |
| 73 | return mId; |
| 74 | } |
| 75 | |
| 76 | /** {@hide} */ |
| 77 | void setDestroyed() { |
| 78 | for (RemoteConnection connection : mChildConnections) { |
| 79 | connection.setConference(null); |
| 80 | } |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 81 | for (CallbackRecord<Callback> record : mCallbackRecords) { |
| 82 | final RemoteConference conference = this; |
| 83 | final Callback callback = record.getCallback(); |
| 84 | record.getHandler().post(new Runnable() { |
| 85 | @Override |
| 86 | public void run() { |
| 87 | callback.onDestroyed(conference); |
| 88 | } |
| 89 | }); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | /** {@hide} */ |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 94 | void setState(final int newState) { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 95 | if (newState != Connection.STATE_ACTIVE && |
| 96 | newState != Connection.STATE_HOLDING && |
| 97 | newState != Connection.STATE_DISCONNECTED) { |
| 98 | Log.w(this, "Unsupported state transition for Conference call.", |
| 99 | Connection.stateToString(newState)); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (mState != newState) { |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 104 | final int oldState = mState; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 105 | mState = newState; |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 106 | for (CallbackRecord<Callback> record : mCallbackRecords) { |
| 107 | final RemoteConference conference = this; |
| 108 | final Callback callback = record.getCallback(); |
| 109 | record.getHandler().post(new Runnable() { |
| 110 | @Override |
| 111 | public void run() { |
| 112 | callback.onStateChanged(conference, oldState, newState); |
| 113 | } |
| 114 | }); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** {@hide} */ |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 120 | void addConnection(final RemoteConnection connection) { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 121 | if (!mChildConnections.contains(connection)) { |
| 122 | mChildConnections.add(connection); |
| 123 | connection.setConference(this); |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 124 | for (CallbackRecord<Callback> record : mCallbackRecords) { |
| 125 | final RemoteConference conference = this; |
| 126 | final Callback callback = record.getCallback(); |
| 127 | record.getHandler().post(new Runnable() { |
| 128 | @Override |
| 129 | public void run() { |
| 130 | callback.onConnectionAdded(conference, connection); |
| 131 | } |
| 132 | }); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** {@hide} */ |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 138 | void removeConnection(final RemoteConnection connection) { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 139 | if (mChildConnections.contains(connection)) { |
| 140 | mChildConnections.remove(connection); |
| 141 | connection.setConference(null); |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 142 | for (CallbackRecord<Callback> record : mCallbackRecords) { |
| 143 | final RemoteConference conference = this; |
| 144 | final Callback callback = record.getCallback(); |
| 145 | record.getHandler().post(new Runnable() { |
| 146 | @Override |
| 147 | public void run() { |
| 148 | callback.onConnectionRemoved(conference, connection); |
| 149 | } |
| 150 | }); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** {@hide} */ |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 156 | void setConnectionCapabilities(final int connectionCapabilities) { |
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 157 | if (mConnectionCapabilities != connectionCapabilities) { |
| 158 | mConnectionCapabilities = connectionCapabilities; |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 159 | for (CallbackRecord<Callback> record : mCallbackRecords) { |
| 160 | final RemoteConference conference = this; |
| 161 | final Callback callback = record.getCallback(); |
| 162 | record.getHandler().post(new Runnable() { |
| 163 | @Override |
| 164 | public void run() { |
| 165 | callback.onConnectionCapabilitiesChanged( |
| 166 | conference, mConnectionCapabilities); |
| 167 | } |
| 168 | }); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 173 | /** @hide */ |
| 174 | void setConferenceableConnections(List<RemoteConnection> conferenceableConnections) { |
| 175 | mConferenceableConnections.clear(); |
| 176 | mConferenceableConnections.addAll(conferenceableConnections); |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 177 | for (CallbackRecord<Callback> record : mCallbackRecords) { |
| 178 | final RemoteConference conference = this; |
| 179 | final Callback callback = record.getCallback(); |
| 180 | record.getHandler().post(new Runnable() { |
| 181 | @Override |
| 182 | public void run() { |
| 183 | callback.onConferenceableConnectionsChanged( |
| 184 | conference, mUnmodifiableConferenceableConnections); |
| 185 | } |
| 186 | }); |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 190 | /** {@hide} */ |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 191 | void setDisconnected(final DisconnectCause disconnectCause) { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 192 | if (mState != Connection.STATE_DISCONNECTED) { |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 193 | mDisconnectCause = disconnectCause; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 194 | setState(Connection.STATE_DISCONNECTED); |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 195 | for (CallbackRecord<Callback> record : mCallbackRecords) { |
| 196 | final RemoteConference conference = this; |
| 197 | final Callback callback = record.getCallback(); |
| 198 | record.getHandler().post(new Runnable() { |
| 199 | @Override |
| 200 | public void run() { |
| 201 | callback.onDisconnected(conference, disconnectCause); |
| 202 | } |
| 203 | }); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | public final List<RemoteConnection> getConnections() { |
| 209 | return mUnmodifiableChildConnections; |
| 210 | } |
| 211 | |
| 212 | public final int getState() { |
| 213 | return mState; |
| 214 | } |
| 215 | |
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 216 | public final int getConnectionCapabilities() { |
| 217 | return mConnectionCapabilities; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | public void disconnect() { |
| 221 | try { |
| 222 | mConnectionService.disconnect(mId); |
| 223 | } catch (RemoteException e) { |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | public void separate(RemoteConnection connection) { |
| 228 | if (mChildConnections.contains(connection)) { |
| 229 | try { |
| 230 | mConnectionService.splitFromConference(connection.getId()); |
| 231 | } catch (RemoteException e) { |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| mike dooley | 95ea576 | 2014-09-25 14:49:21 -0700 | [diff] [blame] | 236 | public void merge() { |
| 237 | try { |
| 238 | mConnectionService.mergeConference(mId); |
| 239 | } catch (RemoteException e) { |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | public void swap() { |
| 244 | try { |
| 245 | mConnectionService.swapConference(mId); |
| 246 | } catch (RemoteException e) { |
| 247 | } |
| 248 | } |
| 249 | |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 250 | public void hold() { |
| 251 | try { |
| 252 | mConnectionService.hold(mId); |
| 253 | } catch (RemoteException e) { |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | public void unhold() { |
| 258 | try { |
| 259 | mConnectionService.unhold(mId); |
| 260 | } catch (RemoteException e) { |
| 261 | } |
| 262 | } |
| 263 | |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 264 | public DisconnectCause getDisconnectCause() { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 265 | return mDisconnectCause; |
| 266 | } |
| 267 | |
| Yorke Lee | 58bacc5 | 2014-09-16 10:43:06 -0700 | [diff] [blame] | 268 | public void playDtmfTone(char digit) { |
| 269 | try { |
| 270 | mConnectionService.playDtmfTone(mId, digit); |
| 271 | } catch (RemoteException e) { |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | public void stopDtmfTone() { |
| 276 | try { |
| 277 | mConnectionService.stopDtmfTone(mId); |
| 278 | } catch (RemoteException e) { |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | public void setAudioState(AudioState state) { |
| 283 | try { |
| 284 | mConnectionService.onAudioStateChanged(mId, state); |
| 285 | } catch (RemoteException e) { |
| 286 | } |
| 287 | } |
| 288 | |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 289 | public List<RemoteConnection> getConferenceableConnections() { |
| 290 | return mUnmodifiableConferenceableConnections; |
| 291 | } |
| 292 | |
| Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 293 | public final void registerCallback(Callback callback) { |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 294 | registerCallback(callback, new Handler()); |
| 295 | } |
| 296 | |
| 297 | public final void registerCallback(Callback callback, Handler handler) { |
| 298 | unregisterCallback(callback); |
| 299 | if (callback != null && handler != null) { |
| 300 | mCallbackRecords.add(new CallbackRecord(callback, handler)); |
| 301 | } |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 304 | public final void unregisterCallback(Callback callback) { |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame^] | 305 | if (callback != null) { |
| 306 | for (CallbackRecord<Callback> record : mCallbackRecords) { |
| 307 | if (record.getCallback() == callback) { |
| 308 | mCallbackRecords.remove(record); |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 313 | } |
| 314 | } |