RemoteConference.java revision 95ea5765dca757c8c9f0445eba6735dac996c62b
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 17package android.telecom; 18 19import com.android.internal.telecom.IConnectionService; 20 21import android.annotation.SystemApi; 22import android.os.RemoteException; 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 * @hide 33 */ 34@SystemApi 35public final class RemoteConference { 36 37 public abstract static class Callback { 38 public void onStateChanged(RemoteConference conference, int oldState, int newState) {} 39 public void onDisconnected(RemoteConference conference, DisconnectCause disconnectCause) {} 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 49 private final Set<Callback> mCallbacks = new CopyOnWriteArraySet<>(); 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; 55 private DisconnectCause mDisconnectCause; 56 private int mCallCapabilities; 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 } 74 for (Callback c : mCallbacks) { 75 c.onDestroyed(this); 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; 92 for (Callback c : mCallbacks) { 93 c.onStateChanged(this, oldState, newState); 94 } 95 } 96 } 97 98 /** {@hide} */ 99 void addConnection(RemoteConnection connection) { 100 if (!mChildConnections.contains(connection)) { 101 mChildConnections.add(connection); 102 connection.setConference(this); 103 for (Callback c : mCallbacks) { 104 c.onConnectionAdded(this, connection); 105 } 106 } 107 } 108 109 /** {@hide} */ 110 void removeConnection(RemoteConnection connection) { 111 if (mChildConnections.contains(connection)) { 112 mChildConnections.remove(connection); 113 connection.setConference(null); 114 for (Callback c : mCallbacks) { 115 c.onConnectionRemoved(this, connection); 116 } 117 } 118 } 119 120 /** {@hide} */ 121 void setCallCapabilities(int capabilities) { 122 if (mCallCapabilities != capabilities) { 123 mCallCapabilities = capabilities; 124 for (Callback c : mCallbacks) { 125 c.onCapabilitiesChanged(this, mCallCapabilities); 126 } 127 } 128 } 129 130 /** {@hide} */ 131 void setDisconnected(DisconnectCause disconnectCause) { 132 if (mState != Connection.STATE_DISCONNECTED) { 133 mDisconnectCause = disconnectCause; 134 setState(Connection.STATE_DISCONNECTED); 135 for (Callback c : mCallbacks) { 136 c.onDisconnected(this, disconnectCause); 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 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 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 197 public DisconnectCause getDisconnectCause() { 198 return mDisconnectCause; 199 } 200 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 222 public final void registerCallback(Callback callback) { 223 mCallbacks.add(callback); 224 } 225 226 public final void unregisterCallback(Callback callback) { 227 mCallbacks.remove(callback); 228 } 229} 230