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 17 package com.android.services.telephony; 18 19 import android.telecom.Conference; 20 import android.telecom.Connection; 21 import android.telecom.DisconnectCause; 22 import android.telecom.PhoneAccountHandle; 23 24 import com.android.internal.telephony.Call; 25 import com.android.phone.PhoneUtils; 26 27 import java.util.ArrayList; 28 import java.util.Collection; 29 import java.util.Collections; 30 import java.util.HashSet; 31 import java.util.List; 32 import java.util.Set; 33 import java.util.stream.Collectors; 34 35 /** 36 * Maintains a list of all the known TelephonyConnections connections and controls GSM and 37 * default IMS conference call behavior. This functionality is characterized by the support of 38 * two top-level calls, in contrast to a CDMA conference call which automatically starts a 39 * conference when there are two calls. 40 */ 41 final class TelephonyConferenceController { 42 private static final int TELEPHONY_CONFERENCE_MAX_SIZE = 5; 43 44 private final TelephonyConnection.TelephonyConnectionListener mTelephonyConnectionListener = 45 new TelephonyConnection.TelephonyConnectionListener() { 46 @Override 47 public void onStateChanged(Connection c, int state) { 48 Log.v(this, "onStateChange triggered in Conf Controller : connection = " + c 49 + " state = " + state); 50 recalculate(); 51 } 52 53 @Override 54 public void onDisconnected(Connection c, DisconnectCause disconnectCause) { 55 recalculate(); 56 } 57 58 @Override 59 public void onDestroyed(Connection connection) { 60 // Only TelephonyConnections are added. 61 remove((TelephonyConnection) connection); 62 } 63 }; 64 65 /** The known connections. */ 66 private final List<TelephonyConnection> mTelephonyConnections = new ArrayList<>(); 67 68 private final TelephonyConnectionServiceProxy mConnectionService; 69 private boolean mTriggerRecalculate = false; 70 TelephonyConferenceController(TelephonyConnectionServiceProxy connectionService)71 public TelephonyConferenceController(TelephonyConnectionServiceProxy connectionService) { 72 mConnectionService = connectionService; 73 } 74 /** The TelephonyConference connection object. */ 75 private TelephonyConference mTelephonyConference; 76 shouldRecalculate()77 boolean shouldRecalculate() { 78 Log.d(this, "shouldRecalculate is " + mTriggerRecalculate); 79 return mTriggerRecalculate; 80 } 81 add(TelephonyConnection connection)82 void add(TelephonyConnection connection) { 83 if (mTelephonyConnections.contains(connection)) { 84 // Adding a duplicate realistically shouldn't happen. 85 Log.w(this, "add - connection already tracked; connection=%s", connection); 86 return; 87 } 88 mTelephonyConnections.add(connection); 89 connection.addTelephonyConnectionListener(mTelephonyConnectionListener); 90 recalculate(); 91 } 92 remove(TelephonyConnection connection)93 void remove(TelephonyConnection connection) { 94 if (!mTelephonyConnections.contains(connection)) { 95 // Debug only since TelephonyConnectionService tries to clean up the connections tracked 96 // when the original connection changes. It does this proactively. 97 Log.d(this, "remove - connection not tracked; connection=%s", connection); 98 return; 99 } 100 connection.removeTelephonyConnectionListener(mTelephonyConnectionListener); 101 mTelephonyConnections.remove(connection); 102 recalculate(); 103 } 104 recalculate()105 void recalculate() { 106 recalculateConference(); 107 recalculateConferenceable(); 108 } 109 isFullConference(Conference conference)110 private boolean isFullConference(Conference conference) { 111 return conference.getConnections().size() >= TELEPHONY_CONFERENCE_MAX_SIZE; 112 } 113 participatesInFullConference(Connection connection)114 private boolean participatesInFullConference(Connection connection) { 115 return connection.getConference() != null && 116 isFullConference(connection.getConference()); 117 } 118 119 /** 120 * Calculates the conference-capable state of all GSM connections in this connection service. 121 */ recalculateConferenceable()122 private void recalculateConferenceable() { 123 Log.v(this, "recalculateConferenceable : %d", mTelephonyConnections.size()); 124 HashSet<Connection> conferenceableConnections = new HashSet<>(mTelephonyConnections.size()); 125 126 // Loop through and collect all calls which are active or holding 127 for (TelephonyConnection connection : mTelephonyConnections) { 128 Log.d(this, "recalc - %s %s supportsConf? %s", connection.getState(), connection, 129 connection.isConferenceSupported()); 130 131 if (connection.isConferenceSupported() && !participatesInFullConference(connection)) { 132 switch (connection.getState()) { 133 case Connection.STATE_ACTIVE: 134 //fall through 135 case Connection.STATE_HOLDING: 136 conferenceableConnections.add(connection); 137 continue; 138 default: 139 break; 140 } 141 } 142 143 connection.setConferenceableConnections(Collections.<Connection>emptyList()); 144 } 145 146 Log.v(this, "conferenceable: " + conferenceableConnections.size()); 147 148 // Go through all the conferenceable connections and add all other conferenceable 149 // connections that is not the connection itself 150 for (Connection c : conferenceableConnections) { 151 List<Connection> connections = conferenceableConnections 152 .stream() 153 // Filter out this connection from the list of connections 154 .filter(connection -> c != connection) 155 .collect(Collectors.toList()); 156 c.setConferenceableConnections(connections); 157 } 158 159 // Set the conference as conferenceable with all of the connections that are not in the 160 // conference. 161 if (mTelephonyConference != null) { 162 if (!isFullConference(mTelephonyConference)) { 163 List<Connection> nonConferencedConnections = mTelephonyConnections 164 .stream() 165 // Only retrieve Connections that are not in a conference (but support 166 // conferences). 167 .filter(c -> c.isConferenceSupported() && c.getConference() == null) 168 .collect(Collectors.toList()); 169 mTelephonyConference.setConferenceableConnections(nonConferencedConnections); 170 } else { 171 Log.d(this, "cannot merge anymore due it is full"); 172 mTelephonyConference 173 .setConferenceableConnections(Collections.<Connection>emptyList()); 174 } 175 } 176 // TODO: Do not allow conferencing of already conferenced connections. 177 } 178 recalculateConference()179 private void recalculateConference() { 180 Set<TelephonyConnection> conferencedConnections = new HashSet<>(); 181 int numGsmConnections = 0; 182 183 for (TelephonyConnection connection : mTelephonyConnections) { 184 com.android.internal.telephony.Connection radioConnection = 185 connection.getOriginalConnection(); 186 if (radioConnection != null) { 187 Call.State state = radioConnection.getState(); 188 Call call = radioConnection.getCall(); 189 if ((state == Call.State.ACTIVE || state == Call.State.HOLDING) && 190 (call != null && call.isMultiparty())) { 191 numGsmConnections++; 192 conferencedConnections.add(connection); 193 } 194 } 195 } 196 197 Log.d(this, "Recalculate conference calls %s %s.", 198 mTelephonyConference, conferencedConnections); 199 200 // Check if all conferenced connections are in Connection Service 201 boolean allConnInService = true; 202 Collection<Connection> allConnections = mConnectionService.getAllConnections(); 203 for (Connection connection : conferencedConnections) { 204 Log.v (this, "Finding connection in Connection Service for " + connection); 205 if (!allConnections.contains(connection)) { 206 allConnInService = false; 207 Log.v(this, "Finding connection in Connection Service Failed"); 208 break; 209 } 210 } 211 212 Log.d(this, "Is there a match for all connections in connection service " + 213 allConnInService); 214 215 // If this is a GSM conference and the number of connections drops below 2, we will 216 // terminate the conference. 217 if (numGsmConnections < 2) { 218 Log.d(this, "not enough connections to be a conference!"); 219 220 // No more connections are conferenced, destroy any existing conference. 221 if (mTelephonyConference != null) { 222 Log.d(this, "with a conference to destroy!"); 223 mTelephonyConference.destroyTelephonyConference(); 224 mTelephonyConference = null; 225 } 226 } else { 227 if (mTelephonyConference != null) { 228 List<Connection> existingConnections = mTelephonyConference.getConnections(); 229 // Remove any that no longer exist 230 for (Connection connection : existingConnections) { 231 if (connection instanceof TelephonyConnection && 232 !conferencedConnections.contains(connection)) { 233 mTelephonyConference.removeTelephonyConnection(connection); 234 } 235 } 236 if (allConnInService) { 237 mTriggerRecalculate = false; 238 // Add any new ones 239 for (Connection connection : conferencedConnections) { 240 if (!existingConnections.contains(connection)) { 241 mTelephonyConference.addTelephonyConnection(connection); 242 } 243 } 244 } else { 245 Log.d(this, "Trigger recalculate later"); 246 mTriggerRecalculate = true; 247 } 248 } else { 249 if (allConnInService) { 250 mTriggerRecalculate = false; 251 252 // Get PhoneAccount from one of the conferenced connections and use it to set 253 // the phone account on the conference. 254 PhoneAccountHandle phoneAccountHandle = null; 255 if (!conferencedConnections.isEmpty()) { 256 TelephonyConnection telephonyConnection = 257 conferencedConnections.iterator().next(); 258 phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle( 259 telephonyConnection.getPhone()); 260 } 261 262 mTelephonyConference = new TelephonyConference(phoneAccountHandle); 263 Log.i(this, "Creating new TelephonyConference to hold conferenced connections." 264 + " conference=" + mTelephonyConference); 265 boolean isDowngradedConference = false; 266 for (TelephonyConnection connection : conferencedConnections) { 267 Log.d(this, "Adding a connection to a conference call: %s %s", 268 mTelephonyConference, connection); 269 if ((connection.getConnectionProperties() 270 & Connection.PROPERTY_IS_DOWNGRADED_CONFERENCE) != 0) { 271 // Remove all instances of PROPERTY_IS_DOWNGRADED_CONFERENCE. This 272 // property should only be set on the parent call (i.e. the newly 273 // created TelephonyConference. 274 Log.d(this, "Removing PROPERTY_IS_DOWNGRADED_CONFERENCE from connection" 275 + " %s", connection); 276 int newProperties = connection.getConnectionProperties() 277 & ~Connection.PROPERTY_IS_DOWNGRADED_CONFERENCE; 278 connection.setTelephonyConnectionProperties(newProperties); 279 isDowngradedConference = true; 280 } 281 mTelephonyConference.addTelephonyConnection(connection); 282 } 283 // Reapply the downgraded-conference flag to the parent conference if it was on 284 // one of the children. 285 if (isDowngradedConference) { 286 mTelephonyConference.setConnectionProperties( 287 mTelephonyConference.getConnectionProperties() 288 | Connection.PROPERTY_IS_DOWNGRADED_CONFERENCE); 289 } 290 mTelephonyConference.updateCallRadioTechAfterCreation(); 291 mConnectionService.addConference(mTelephonyConference); 292 } else { 293 Log.d(this, "Trigger recalculate later"); 294 mTriggerRecalculate = true; 295 } 296 } 297 if (mTelephonyConference != null) { 298 Connection conferencedConnection = mTelephonyConference.getPrimaryConnection(); 299 Log.v(this, "Primary Conferenced connection is " + conferencedConnection); 300 if (conferencedConnection != null) { 301 switch (conferencedConnection.getState()) { 302 case Connection.STATE_ACTIVE: 303 Log.v(this, "Setting conference to active"); 304 mTelephonyConference.setActive(); 305 break; 306 case Connection.STATE_HOLDING: 307 Log.v(this, "Setting conference to hold"); 308 mTelephonyConference.setOnHold(); 309 break; 310 } 311 } 312 } 313 } 314 } 315 } 316