blob: 393e054e8535d8d635047528a62646434cf5aaf6 [file] [log] [blame]
Santos Cordon63a84242013-07-23 13:32:52 -07001/*
2 * Copyright (C) 2013 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 com.android.phone;
18
19import com.google.android.collect.Lists;
20import com.google.android.collect.Maps;
Santos Cordon4ad64cd2013-08-15 00:36:14 -070021import com.google.android.collect.Sets;
Santos Cordon63a84242013-07-23 13:32:52 -070022import com.google.common.base.Preconditions;
Santos Cordone38b1ff2013-08-07 12:12:16 -070023import com.google.common.collect.ImmutableMap;
Santos Cordon4ad64cd2013-08-15 00:36:14 -070024import com.google.common.collect.ImmutableSortedSet;
Santos Cordon63a84242013-07-23 13:32:52 -070025
26import android.os.AsyncResult;
27import android.os.Handler;
28import android.os.Message;
Santos Cordon4ad64cd2013-08-15 00:36:14 -070029import android.os.SystemProperties;
Santos Cordone38b1ff2013-08-07 12:12:16 -070030import android.text.TextUtils;
31import android.util.Log;
Santos Cordon63a84242013-07-23 13:32:52 -070032
Santos Cordona3d05142013-07-29 11:25:17 -070033import com.android.internal.telephony.CallManager;
Santos Cordon63a84242013-07-23 13:32:52 -070034import com.android.internal.telephony.Connection;
Santos Cordoneead6ec2013-08-07 22:16:33 -070035import com.android.internal.telephony.Phone;
Santos Cordona3d05142013-07-29 11:25:17 -070036import com.android.internal.telephony.PhoneConstants;
Santos Cordon26e7b242013-08-07 21:15:45 -070037import com.android.internal.telephony.TelephonyCapabilities;
Santos Cordon69a69192013-08-22 14:25:42 -070038import com.android.phone.CallGatewayManager.RawGatewayInfo;
Santos Cordon995c8162013-07-29 09:22:22 -070039import com.android.services.telephony.common.Call;
Santos Cordon26e7b242013-08-07 21:15:45 -070040import com.android.services.telephony.common.Call.Capabilities;
Santos Cordona3d05142013-07-29 11:25:17 -070041import com.android.services.telephony.common.Call.State;
Santos Cordon63a84242013-07-23 13:32:52 -070042
43import java.util.ArrayList;
44import java.util.HashMap;
45import java.util.List;
Santos Cordon249efd02013-08-05 03:33:56 -070046import java.util.Map.Entry;
Santos Cordon4ad64cd2013-08-15 00:36:14 -070047import java.util.SortedSet;
Santos Cordon63a84242013-07-23 13:32:52 -070048import java.util.concurrent.atomic.AtomicInteger;
49
50/**
51 * Creates a Call model from Call state and data received from the telephony
52 * layer. The telephony layer maintains 3 conceptual objects: Phone, Call,
53 * Connection.
54 *
55 * Phone represents the radio and there is an implementation per technology
56 * type such as GSMPhone, SipPhone, CDMAPhone, etc. Generally, we will only ever
57 * deal with one instance of this object for the lifetime of this class.
58 *
59 * There are 3 Call instances that exist for the lifetime of this class which
60 * are created by CallTracker. The three are RingingCall, ForegroundCall, and
61 * BackgroundCall.
62 *
63 * A Connection most closely resembles what the layperson would consider a call.
64 * A Connection is created when a user dials and it is "owned" by one of the
65 * three Call instances. Which of the three Calls owns the Connection changes
66 * as the Connection goes between ACTIVE, HOLD, RINGING, and other states.
67 *
68 * This class models a new Call class from Connection objects received from
69 * the telephony layer. We use Connection references as identifiers for a call;
70 * new reference = new call.
71 *
72 * TODO(klp): Create a new Call class to replace the simple call Id ints
73 * being used currently.
74 *
75 * The new Call models are parcellable for transfer via the CallHandlerService
76 * API.
77 */
78public class CallModeler extends Handler {
79
80 private static final String TAG = CallModeler.class.getSimpleName();
Santos Cordon4ad64cd2013-08-15 00:36:14 -070081 private static final boolean DBG =
82 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Santos Cordon63a84242013-07-23 13:32:52 -070083
84 private static final int CALL_ID_START_VALUE = 1;
Santos Cordon63a84242013-07-23 13:32:52 -070085
Santos Cordon998f42b2013-08-02 16:13:12 -070086 private final CallStateMonitor mCallStateMonitor;
87 private final CallManager mCallManager;
Santos Cordon69a69192013-08-22 14:25:42 -070088 private final CallGatewayManager mCallGatewayManager;
Santos Cordon998f42b2013-08-02 16:13:12 -070089 private final HashMap<Connection, Call> mCallMap = Maps.newHashMap();
Santos Cordon4ad64cd2013-08-15 00:36:14 -070090 private final HashMap<Connection, Call> mConfCallMap = Maps.newHashMap();
Santos Cordon998f42b2013-08-02 16:13:12 -070091 private final AtomicInteger mNextCallId = new AtomicInteger(CALL_ID_START_VALUE);
Christine Chendaf7bf62013-08-05 19:12:31 -070092 private final ArrayList<Listener> mListeners = new ArrayList<Listener>();
Christine Chenee09a492013-08-06 16:02:29 -070093 private RejectWithTextMessageManager mRejectWithTextMessageManager;
Santos Cordon63a84242013-07-23 13:32:52 -070094
Christine Chenee09a492013-08-06 16:02:29 -070095 public CallModeler(CallStateMonitor callStateMonitor, CallManager callManager,
Santos Cordon69a69192013-08-22 14:25:42 -070096 RejectWithTextMessageManager rejectWithTextMessageManager,
97 CallGatewayManager callGatewayManager) {
Santos Cordon63a84242013-07-23 13:32:52 -070098 mCallStateMonitor = callStateMonitor;
Santos Cordona3d05142013-07-29 11:25:17 -070099 mCallManager = callManager;
Christine Chenee09a492013-08-06 16:02:29 -0700100 mRejectWithTextMessageManager = rejectWithTextMessageManager;
Santos Cordon69a69192013-08-22 14:25:42 -0700101 mCallGatewayManager = callGatewayManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700102
103 mCallStateMonitor.addListener(this);
104 }
105
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700106 @Override
Santos Cordon63a84242013-07-23 13:32:52 -0700107 public void handleMessage(Message msg) {
108 switch(msg.what) {
109 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
110 onNewRingingConnection((AsyncResult) msg.obj);
111 break;
112 case CallStateMonitor.PHONE_DISCONNECT:
113 onDisconnect((AsyncResult) msg.obj);
Santos Cordon995c8162013-07-29 09:22:22 -0700114 break;
115 case CallStateMonitor.PHONE_STATE_CHANGED:
116 onPhoneStateChanged((AsyncResult) msg.obj);
117 break;
Santos Cordon63a84242013-07-23 13:32:52 -0700118 default:
119 break;
120 }
121 }
122
Christine Chendaf7bf62013-08-05 19:12:31 -0700123 public void addListener(Listener listener) {
Santos Cordon63a84242013-07-23 13:32:52 -0700124 Preconditions.checkNotNull(listener);
Christine Chendaf7bf62013-08-05 19:12:31 -0700125 Preconditions.checkNotNull(mListeners);
Christine Chen4748abd2013-08-07 15:44:15 -0700126 if (!mListeners.contains(listener)) {
127 mListeners.add(listener);
128 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700129 }
130
131 public List<Call> getFullList() {
132 final List<Call> retval = Lists.newArrayList();
133 doUpdate(true, retval);
134 return retval;
Santos Cordon63a84242013-07-23 13:32:52 -0700135 }
136
Santos Cordon249efd02013-08-05 03:33:56 -0700137 public CallResult getCallWithId(int callId) {
138 // max 8 connections, so this should be fast even through we are traversing the entire map.
139 for (Entry<Connection, Call> entry : mCallMap.entrySet()) {
140 if (entry.getValue().getCallId() == callId) {
141 return new CallResult(entry.getValue(), entry.getKey());
142 }
143 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700144
145 for (Entry<Connection, Call> entry : mConfCallMap.entrySet()) {
146 if (entry.getValue().getCallId() == callId) {
147 if (entry.getValue().getChildCallIds().size() == 0) {
148 return null;
149 }
150 final CallResult child = getCallWithId(entry.getValue().getChildCallIds().first());
151 return new CallResult(entry.getValue(), child.getActionableCall(),
152 child.getConnection());
153 }
154 }
Santos Cordon249efd02013-08-05 03:33:56 -0700155 return null;
156 }
157
Santos Cordonaf763a12013-08-19 20:04:58 -0700158 public boolean hasLiveCall() {
159 return hasLiveCallInternal(mCallMap) ||
160 hasLiveCallInternal(mConfCallMap);
161 }
162
163 private boolean hasLiveCallInternal(HashMap<Connection, Call> map) {
164 for (Call call : map.values()) {
165 final int state = call.getState();
166 if (state == Call.State.ACTIVE ||
167 state == Call.State.CALL_WAITING ||
168 state == Call.State.CONFERENCED ||
169 state == Call.State.DIALING ||
170 state == Call.State.INCOMING ||
171 state == Call.State.ONHOLD) {
172 return true;
173 }
174 }
175 return false;
176 }
177
Santos Cordon2b73bd62013-08-27 14:53:43 -0700178 public boolean hasOutstandingActiveOrDialingCall() {
179 return hasOutstandingActiveOrDialingCallInternal(mCallMap) ||
180 hasOutstandingActiveOrDialingCallInternal(mConfCallMap);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700181 }
182
Santos Cordon2b73bd62013-08-27 14:53:43 -0700183 private static boolean hasOutstandingActiveOrDialingCallInternal(
184 HashMap<Connection, Call> map) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700185 for (Call call : map.values()) {
186 final int state = call.getState();
Santos Cordon2b73bd62013-08-27 14:53:43 -0700187 if (state == Call.State.ACTIVE ||
188 state == Call.State.DIALING) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700189 return true;
190 }
191 }
192
193 return false;
194 }
195
Santos Cordon63a84242013-07-23 13:32:52 -0700196 private void onNewRingingConnection(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700197 Log.i(TAG, "onNewRingingConnection");
Santos Cordon63a84242013-07-23 13:32:52 -0700198 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700199 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700200
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700201 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700202
Christine Chendaf7bf62013-08-05 19:12:31 -0700203 for (int i = 0; i < mListeners.size(); ++i) {
Christine Chenee09a492013-08-06 16:02:29 -0700204 if (call != null) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700205 mListeners.get(i).onIncoming(call);
Christine Chenee09a492013-08-06 16:02:29 -0700206 }
Santos Cordon63a84242013-07-23 13:32:52 -0700207 }
208 }
209
210 private void onDisconnect(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700211 Log.i(TAG, "onDisconnect");
Santos Cordon63a84242013-07-23 13:32:52 -0700212 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700213 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700214
Santos Cordon995c8162013-07-29 09:22:22 -0700215 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700216 final boolean wasConferenced = call.getState() == State.CONFERENCED;
217
218 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700219
Christine Chendaf7bf62013-08-05 19:12:31 -0700220 for (int i = 0; i < mListeners.size(); ++i) {
221 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700222 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700223
224 // If it was a conferenced call, we need to run the entire update
225 // to make the proper changes to parent conference calls.
226 if (wasConferenced) {
227 onPhoneStateChanged(null);
228 }
229
230 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700231 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700232
233 // TODO(klp): Do a final check to see if there are any active calls.
234 // If there are not, totally cancel all calls
Santos Cordon63a84242013-07-23 13:32:52 -0700235 }
236
Santos Cordona3d05142013-07-29 11:25:17 -0700237 /**
238 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700239 */
Santos Cordon995c8162013-07-29 09:22:22 -0700240 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700241 Log.i(TAG, "onPhoneStateChanged: ");
Santos Cordon998f42b2013-08-02 16:13:12 -0700242 final List<Call> updatedCalls = Lists.newArrayList();
243 doUpdate(false, updatedCalls);
244
Christine Chendaf7bf62013-08-05 19:12:31 -0700245 for (int i = 0; i < mListeners.size(); ++i) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700246 mListeners.get(i).onUpdate(updatedCalls);
Santos Cordon998f42b2013-08-02 16:13:12 -0700247 }
248 }
249
250
251 /**
252 * Go through the Calls from CallManager and return the list of calls that were updated.
253 * Or, the full list if requested.
254 */
255 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700256 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
257 telephonyCalls.addAll(mCallManager.getRingingCalls());
258 telephonyCalls.addAll(mCallManager.getForegroundCalls());
259 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
260
Santos Cordona3d05142013-07-29 11:25:17 -0700261 // Cycle through all the Connections on all the Calls. Update our Call objects
262 // to reflect any new state and send the updated Call objects to the handler service.
263 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700264
265 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700266 // new connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700267 // a state in the internal.telephony.Call object. This ensures that staleness
268 // check below fails and we always add the item to the update list if it is new.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700269 final Call call = getCallFromMap(mCallMap, connection, true);
Santos Cordona3d05142013-07-29 11:25:17 -0700270
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700271 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordon2b73bd62013-08-27 14:53:43 -0700272
273 Log.i(TAG, "doUpdate: " + call);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700274 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700275 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700276 }
277 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700278
279 // We do a second loop to address conference call scenarios. We do this as a separate
280 // loop to ensure all child calls are up to date before we start updating the parent
281 // conference calls.
282 for (Connection connection : telephonyCall.getConnections()) {
283 updateForConferenceCalls(connection, out);
284 }
285
Santos Cordona3d05142013-07-29 11:25:17 -0700286 }
Santos Cordona3d05142013-07-29 11:25:17 -0700287 }
288
Santos Cordone38b1ff2013-08-07 12:12:16 -0700289 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700290 * Checks to see if the connection is the first connection in a conference call.
291 * If it is a conference call, we will create a new Conference Call object or
292 * update the existing conference call object for that connection.
293 * If it is not a conference call but a previous associated conference call still exists,
294 * we mark it as idle and remove it from the map.
295 * In both cases above, we add the Calls to be updated to the UI.
296 * @param connection The connection object to check.
297 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
298 */
299 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
300 // We consider this connection a conference connection if the call it
301 // belongs to is a multiparty call AND it is the first connection.
302 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
303 connection.getCall().getEarliestConnection() == connection;
304
305 boolean changed = false;
306
307 // If this connection is the main connection for the conference call, then create or update
308 // a Call object for that conference call.
309 if (isConferenceCallConnection) {
310 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
311 changed = updateCallFromConnection(confCall, connection, true);
312
313 if (changed) {
314 updatedCalls.add(confCall);
315 }
316
317 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
318
319 // It is possible that through a conference call split, there may be lingering conference
320 // calls where this connection was the main connection. We clean those up here.
321 } else {
322 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
323
324 // We found a conference call for this connection, which is no longer a conference call.
325 // Kill it!
326 if (oldConfCall != null) {
327 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
328 mConfCallMap.remove(connection);
329 oldConfCall.setState(State.IDLE);
330 changed = true;
331
332 // add to the list of calls to update
333 updatedCalls.add(oldConfCall);
334 }
335 }
336
337 return changed;
338 }
339
340 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700341 * Sets the new call state onto the call and performs some additional logic
342 * associated with setting the state.
343 */
344 private void setNewState(Call call, int newState, Connection connection) {
345 Preconditions.checkState(call.getState() != newState);
346
347 // When starting an outgoing call, we need to grab gateway information
348 // for the call, if available, and set it.
349 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
350
351 if (newState == Call.State.DIALING) {
352 if (!info.isEmpty()) {
353 call.setGatewayNumber(info.getFormattedGatewayNumber());
354 call.setGatewayPackage(info.packageName);
355 }
356 } else if (!Call.State.isConnected(newState)) {
357 mCallGatewayManager.clearGatewayData(connection);
358 }
359
360 call.setState(newState);
361 }
362
363 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700364 * Updates the Call properties to match the state of the connection object
365 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700366 * @param call The call object to update.
367 * @param connection The connection object from which to update call.
368 * @param isForConference There are slight differences in how we populate data for conference
369 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700370 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700371 private boolean updateCallFromConnection(Call call, Connection connection,
372 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700373 boolean changed = false;
374
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700375 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700376
377 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700378 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700379 changed = true;
380 }
381
Santos Cordone38b1ff2013-08-07 12:12:16 -0700382 final Call.DisconnectCause newDisconnectCause =
383 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
384 if (call.getDisconnectCause() != newDisconnectCause) {
385 call.setDisconnectCause(newDisconnectCause);
386 changed = true;
387 }
388
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700389 final long oldConnectTime = call.getConnectTime();
390 if (oldConnectTime != connection.getConnectTime()) {
391 call.setConnectTime(connection.getConnectTime());
392 changed = true;
393 }
394
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700395 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700396 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700397 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700398 String newNumber = connection.getAddress();
399 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
400 if (!info.isEmpty()) {
401 newNumber = info.trueNumber;
402 }
403 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
404 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700405 changed = true;
406 }
407
Santos Cordon69a69192013-08-22 14:25:42 -0700408 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700409 final int newNumberPresentation = connection.getNumberPresentation();
410 if (call.getNumberPresentation() != newNumberPresentation) {
411 call.setNumberPresentation(newNumberPresentation);
412 changed = true;
413 }
414
Santos Cordon69a69192013-08-22 14:25:42 -0700415 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700416 final String oldCnapName = call.getCnapName();
417 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
418 call.setCnapName(connection.getCnapName());
419 changed = true;
420 }
Santos Cordon69a69192013-08-22 14:25:42 -0700421
422 // Name Presentation
423 final int newCnapNamePresentation = connection.getCnapNamePresentation();
424 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
425 call.setCnapNamePresentation(newCnapNamePresentation);
426 changed = true;
427 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700428 } else {
429
430 // update the list of children by:
431 // 1) Saving the old set
432 // 2) Removing all children
433 // 3) Adding the correct children into the Call
434 // 4) Comparing the new children set with the old children set
435 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
436 call.removeAllChildren();
437
438 if (connection.getCall() != null) {
439 for (Connection childConn : connection.getCall().getConnections()) {
440 final Call childCall = getCallFromMap(mCallMap, childConn, false);
441 if (childCall != null && childConn.isAlive()) {
442 call.addChildId(childCall.getCallId());
443 }
444 }
445 }
446 changed |= oldSet.equals(call.getChildCallIds());
447 }
448
Santos Cordoneead6ec2013-08-07 22:16:33 -0700449 /**
450 * !!! Uses values from connection and call collected above so this part must be last !!!
451 */
452 final int newCapabilities = getCapabilitiesFor(connection, call);
Santos Cordon26e7b242013-08-07 21:15:45 -0700453 if (call.getCapabilities() != newCapabilities) {
454 call.setCapabilities(newCapabilities);
455 changed = true;
456 }
457
Santos Cordone38b1ff2013-08-07 12:12:16 -0700458 return changed;
459 }
460
Santos Cordon26e7b242013-08-07 21:15:45 -0700461 /**
462 * Returns a mask of capabilities for the connection such as merge, hold, etc.
463 */
Santos Cordoneead6ec2013-08-07 22:16:33 -0700464 private int getCapabilitiesFor(Connection connection, Call call) {
465 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
466 final Phone phone = connection.getCall().getPhone();
467
468 final boolean canHold = TelephonyCapabilities.supportsAnswerAndHold(phone);
469 boolean canAddCall = false;
470 boolean canMergeCall = false;
471 boolean canSwapCall = false;
472
473 // only applies to active calls
474 if (callIsActive) {
475 canAddCall = PhoneUtils.okToAddCall(mCallManager);
476 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
477 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
478 }
479
480 // special rules section!
481 // CDMA always has Add
482 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
483 canAddCall = true;
484 } else {
485 // if neither merge nor add is on...then allow add
486 canAddCall |= !(canAddCall || canMergeCall);
487 }
488
Santos Cordon26e7b242013-08-07 21:15:45 -0700489 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700490 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700491 retval |= Capabilities.HOLD;
492 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700493 if (canAddCall) {
494 retval |= Capabilities.ADD_CALL;
495 }
496 if (canMergeCall) {
497 retval |= Capabilities.MERGE_CALLS;
498 }
499 if (canSwapCall) {
500 retval |= Capabilities.SWAP_CALLS;
501 }
Santos Cordon26e7b242013-08-07 21:15:45 -0700502
503 return retval;
504 }
505
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700506 /**
507 * Returns true if the Connection is part of a multiparty call.
508 * We do this by checking the isMultiparty() method of the telephony.Call object and also
509 * checking to see if more than one of it's children is alive.
510 */
511 private boolean isPartOfLiveConferenceCall(Connection connection) {
512 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
513 int count = 0;
514 for (Connection currConn : connection.getCall().getConnections()) {
515 if (currConn.isAlive()) {
516 count++;
517 if (count >= 2) {
518 return true;
519 }
520 }
521 }
522 }
523 return false;
524 }
525
526 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
527
Santos Cordona3d05142013-07-29 11:25:17 -0700528 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700529 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700530 case ACTIVE:
531 retval = State.ACTIVE;
532 break;
533 case INCOMING:
534 retval = State.INCOMING;
535 break;
536 case DIALING:
537 case ALERTING:
538 retval = State.DIALING;
539 break;
540 case WAITING:
541 retval = State.CALL_WAITING;
542 break;
543 case HOLDING:
544 retval = State.ONHOLD;
545 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700546 case DISCONNECTED:
547 case DISCONNECTING:
548 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700549 default:
550 }
551
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700552 // If we are dealing with a potential child call (not the parent conference call),
553 // the check to see if we have to set the state to CONFERENCED.
554 if (!isForConference) {
555
556 // if the connection is part of a multiparty call, and it is live,
557 // annotate it with CONFERENCED state instead.
558 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
559 return State.CONFERENCED;
560 }
561 }
562
Santos Cordona3d05142013-07-29 11:25:17 -0700563 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700564 }
565
Santos Cordone38b1ff2013-08-07 12:12:16 -0700566 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
567 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
568 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
569 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
570 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
571 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
572 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
573 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
574 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
575 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
576 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
577 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
578 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
579 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
580 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
581 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
582 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
583 Call.DisconnectCause.CDMA_RETRY_ORDER)
584 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
585 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
586 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
587 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
588 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
589 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
590 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
591 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
592 Call.DisconnectCause.ERROR_UNSPECIFIED)
593 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
594 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
595 .put(Connection.DisconnectCause.INCOMING_MISSED,
596 Call.DisconnectCause.INCOMING_MISSED)
597 .put(Connection.DisconnectCause.INCOMING_REJECTED,
598 Call.DisconnectCause.INCOMING_REJECTED)
599 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
600 Call.DisconnectCause.INVALID_CREDENTIALS)
601 .put(Connection.DisconnectCause.INVALID_NUMBER,
602 Call.DisconnectCause.INVALID_NUMBER)
603 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
604 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
605 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
606 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
607 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
608 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
609 Call.DisconnectCause.NOT_DISCONNECTED)
610 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
611 Call.DisconnectCause.NUMBER_UNREACHABLE)
612 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
613 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
614 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
615 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
616 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
617 Call.DisconnectCause.SERVER_UNREACHABLE)
618 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
619 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
620 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
621 .build();
622
623 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
624 Connection.DisconnectCause causeSource) {
625
626 if (CAUSE_MAP.containsKey(causeSource)) {
627 return CAUSE_MAP.get(causeSource);
628 }
629
630 return Call.DisconnectCause.UNKNOWN;
631 }
632
Santos Cordon63a84242013-07-23 13:32:52 -0700633 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700634 * Gets an existing callId for a connection, or creates one if none exists.
635 * This function does NOT set any of the Connection data onto the Call class.
636 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700637 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700638 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
639 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700640 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700641
642 // Find the call id or create if missing and requested.
643 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700644 if (map.containsKey(conn)) {
645 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700646 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700647 call = createNewCall();
648 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700649 }
650 }
Santos Cordon995c8162013-07-29 09:22:22 -0700651 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700652 }
653
654 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700655 * Creates a brand new connection for the call.
656 */
657 private Call createNewCall() {
658 int callId;
659 int newNextCallId;
660 do {
661 callId = mNextCallId.get();
662
663 // protect against overflow
664 newNextCallId = (callId == Integer.MAX_VALUE ?
665 CALL_ID_START_VALUE : callId + 1);
666
667 // Keep looping if the change was not atomic OR the value is already taken.
668 // The call to containsValue() is linear, however, most devices support a
669 // maximum of 7 connections so it's not expensive.
670 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
671
672 return new Call(callId);
673 }
674
675 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700676 * Listener interface for changes to Calls.
677 */
678 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700679 void onDisconnect(Call call);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700680 void onIncoming(Call call);
681 void onUpdate(List<Call> calls);
Santos Cordon63a84242013-07-23 13:32:52 -0700682 }
Santos Cordon249efd02013-08-05 03:33:56 -0700683
684 /**
685 * Result class for accessing a call by connection.
686 */
687 public static class CallResult {
688 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700689 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700690 public Connection mConnection;
691
692 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700693 this(call, call, connection);
694 }
695
696 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700697 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700698 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700699 mConnection = connection;
700 }
701
702 public Call getCall() {
703 return mCall;
704 }
705
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700706 // The call that should be used for call actions like hanging up.
707 public Call getActionableCall() {
708 return mActionableCall;
709 }
710
Santos Cordon249efd02013-08-05 03:33:56 -0700711 public Connection getConnection() {
712 return mConnection;
713 }
714 }
Santos Cordon63a84242013-07-23 13:32:52 -0700715}