| 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 | |
| Tyler Gunn | 711d876fd | 2014-09-19 11:17:02 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
| 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 | |
| 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. |
| Tyler Gunn | 711d876fd | 2014-09-19 11:17:02 -0700 | [diff] [blame] | 32 | * @hide |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 33 | */ |
| Tyler Gunn | 711d876fd | 2014-09-19 11:17:02 -0700 | [diff] [blame] | 34 | @SystemApi |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 35 | public final class RemoteConference { |
| 36 | |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 37 | public abstract static class Callback { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 38 | public void onStateChanged(RemoteConference conference, int oldState, int newState) {} |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 39 | public void onDisconnected(RemoteConference conference, DisconnectCause disconnectCause) {} |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 40 | 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 Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 49 | private final Set<Callback> mCallbacks = new CopyOnWriteArraySet<>(); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 50 | 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 Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 55 | private DisconnectCause mDisconnectCause; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 56 | private int mCallCapabilities; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 57 | |
| 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 Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 74 | for (Callback c : mCallbacks) { |
| 75 | c.onDestroyed(this); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 76 | } |
| 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 Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 92 | for (Callback c : mCallbacks) { |
| 93 | c.onStateChanged(this, oldState, newState); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** {@hide} */ |
| 99 | void addConnection(RemoteConnection connection) { |
| 100 | if (!mChildConnections.contains(connection)) { |
| 101 | mChildConnections.add(connection); |
| 102 | connection.setConference(this); |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 103 | for (Callback c : mCallbacks) { |
| 104 | c.onConnectionAdded(this, connection); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** {@hide} */ |
| 110 | void removeConnection(RemoteConnection connection) { |
| 111 | if (mChildConnections.contains(connection)) { |
| 112 | mChildConnections.remove(connection); |
| 113 | connection.setConference(null); |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 114 | for (Callback c : mCallbacks) { |
| 115 | c.onConnectionRemoved(this, connection); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** {@hide} */ |
| 121 | void setCallCapabilities(int capabilities) { |
| 122 | if (mCallCapabilities != capabilities) { |
| 123 | mCallCapabilities = capabilities; |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 124 | for (Callback c : mCallbacks) { |
| 125 | c.onCapabilitiesChanged(this, mCallCapabilities); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** {@hide} */ |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 131 | void setDisconnected(DisconnectCause disconnectCause) { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 132 | if (mState != Connection.STATE_DISCONNECTED) { |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 133 | mDisconnectCause = disconnectCause; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 134 | setState(Connection.STATE_DISCONNECTED); |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 135 | for (Callback c : mCallbacks) { |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 136 | c.onDisconnected(this, disconnectCause); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 137 | } |
| 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 | |
| mike dooley | 95ea576 | 2014-09-25 14:49:21 -0700 | [diff] [blame^] | 169 | public void merge() { |
| 170 | try { |
| 171 | mConnectionService.mergeConference(mId); |
| 172 | } catch (RemoteException e) { |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public void swap() { |
| 177 | try { |
| 178 | mConnectionService.swapConference(mId); |
| 179 | } catch (RemoteException e) { |
| 180 | } |
| 181 | } |
| 182 | |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 183 | public void hold() { |
| 184 | try { |
| 185 | mConnectionService.hold(mId); |
| 186 | } catch (RemoteException e) { |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public void unhold() { |
| 191 | try { |
| 192 | mConnectionService.unhold(mId); |
| 193 | } catch (RemoteException e) { |
| 194 | } |
| 195 | } |
| 196 | |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 197 | public DisconnectCause getDisconnectCause() { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 198 | return mDisconnectCause; |
| 199 | } |
| 200 | |
| Yorke Lee | 58bacc5 | 2014-09-16 10:43:06 -0700 | [diff] [blame] | 201 | public void playDtmfTone(char digit) { |
| 202 | try { |
| 203 | mConnectionService.playDtmfTone(mId, digit); |
| 204 | } catch (RemoteException e) { |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | public void stopDtmfTone() { |
| 209 | try { |
| 210 | mConnectionService.stopDtmfTone(mId); |
| 211 | } catch (RemoteException e) { |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | public void setAudioState(AudioState state) { |
| 216 | try { |
| 217 | mConnectionService.onAudioStateChanged(mId, state); |
| 218 | } catch (RemoteException e) { |
| 219 | } |
| 220 | } |
| 221 | |
| Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 222 | public final void registerCallback(Callback callback) { |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 223 | mCallbacks.add(callback); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 226 | public final void unregisterCallback(Callback callback) { |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 227 | mCallbacks.remove(callback); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 228 | } |
| 229 | } |