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