blob: 1591ac805df8fc6e1fe434650a19cf157df2c082 [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 Cordon995c8162013-07-29 09:22:22 -070038import com.android.services.telephony.common.Call;
Santos Cordon26e7b242013-08-07 21:15:45 -070039import com.android.services.telephony.common.Call.Capabilities;
Santos Cordona3d05142013-07-29 11:25:17 -070040import com.android.services.telephony.common.Call.State;
Santos Cordon63a84242013-07-23 13:32:52 -070041
42import java.util.ArrayList;
43import java.util.HashMap;
44import java.util.List;
Santos Cordon249efd02013-08-05 03:33:56 -070045import java.util.Map.Entry;
Santos Cordon4ad64cd2013-08-15 00:36:14 -070046import java.util.SortedSet;
Santos Cordon63a84242013-07-23 13:32:52 -070047import java.util.concurrent.atomic.AtomicInteger;
48
49/**
50 * Creates a Call model from Call state and data received from the telephony
51 * layer. The telephony layer maintains 3 conceptual objects: Phone, Call,
52 * Connection.
53 *
54 * Phone represents the radio and there is an implementation per technology
55 * type such as GSMPhone, SipPhone, CDMAPhone, etc. Generally, we will only ever
56 * deal with one instance of this object for the lifetime of this class.
57 *
58 * There are 3 Call instances that exist for the lifetime of this class which
59 * are created by CallTracker. The three are RingingCall, ForegroundCall, and
60 * BackgroundCall.
61 *
62 * A Connection most closely resembles what the layperson would consider a call.
63 * A Connection is created when a user dials and it is "owned" by one of the
64 * three Call instances. Which of the three Calls owns the Connection changes
65 * as the Connection goes between ACTIVE, HOLD, RINGING, and other states.
66 *
67 * This class models a new Call class from Connection objects received from
68 * the telephony layer. We use Connection references as identifiers for a call;
69 * new reference = new call.
70 *
71 * TODO(klp): Create a new Call class to replace the simple call Id ints
72 * being used currently.
73 *
74 * The new Call models are parcellable for transfer via the CallHandlerService
75 * API.
76 */
77public class CallModeler extends Handler {
78
79 private static final String TAG = CallModeler.class.getSimpleName();
Santos Cordon4ad64cd2013-08-15 00:36:14 -070080 private static final boolean DBG =
81 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Santos Cordon63a84242013-07-23 13:32:52 -070082
83 private static final int CALL_ID_START_VALUE = 1;
Santos Cordon63a84242013-07-23 13:32:52 -070084
Santos Cordon998f42b2013-08-02 16:13:12 -070085 private final CallStateMonitor mCallStateMonitor;
86 private final CallManager mCallManager;
87 private final HashMap<Connection, Call> mCallMap = Maps.newHashMap();
Santos Cordon4ad64cd2013-08-15 00:36:14 -070088 private final HashMap<Connection, Call> mConfCallMap = Maps.newHashMap();
Santos Cordon998f42b2013-08-02 16:13:12 -070089 private final AtomicInteger mNextCallId = new AtomicInteger(CALL_ID_START_VALUE);
Christine Chendaf7bf62013-08-05 19:12:31 -070090 private final ArrayList<Listener> mListeners = new ArrayList<Listener>();
Christine Chenee09a492013-08-06 16:02:29 -070091 private RejectWithTextMessageManager mRejectWithTextMessageManager;
Santos Cordon63a84242013-07-23 13:32:52 -070092
Christine Chenee09a492013-08-06 16:02:29 -070093 public CallModeler(CallStateMonitor callStateMonitor, CallManager callManager,
94 RejectWithTextMessageManager rejectWithTextMessageManager) {
Santos Cordon63a84242013-07-23 13:32:52 -070095 mCallStateMonitor = callStateMonitor;
Santos Cordona3d05142013-07-29 11:25:17 -070096 mCallManager = callManager;
Christine Chenee09a492013-08-06 16:02:29 -070097 mRejectWithTextMessageManager = rejectWithTextMessageManager;
Santos Cordon63a84242013-07-23 13:32:52 -070098
99 mCallStateMonitor.addListener(this);
100 }
101
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700102 @Override
Santos Cordon63a84242013-07-23 13:32:52 -0700103 public void handleMessage(Message msg) {
104 switch(msg.what) {
105 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
106 onNewRingingConnection((AsyncResult) msg.obj);
107 break;
108 case CallStateMonitor.PHONE_DISCONNECT:
109 onDisconnect((AsyncResult) msg.obj);
Santos Cordon995c8162013-07-29 09:22:22 -0700110 break;
111 case CallStateMonitor.PHONE_STATE_CHANGED:
112 onPhoneStateChanged((AsyncResult) msg.obj);
113 break;
Santos Cordon63a84242013-07-23 13:32:52 -0700114 default:
115 break;
116 }
117 }
118
Christine Chendaf7bf62013-08-05 19:12:31 -0700119 public void addListener(Listener listener) {
Santos Cordon63a84242013-07-23 13:32:52 -0700120 Preconditions.checkNotNull(listener);
Christine Chendaf7bf62013-08-05 19:12:31 -0700121 Preconditions.checkNotNull(mListeners);
Christine Chen4748abd2013-08-07 15:44:15 -0700122 if (!mListeners.contains(listener)) {
123 mListeners.add(listener);
124 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700125 }
126
127 public List<Call> getFullList() {
128 final List<Call> retval = Lists.newArrayList();
129 doUpdate(true, retval);
130 return retval;
Santos Cordon63a84242013-07-23 13:32:52 -0700131 }
132
Santos Cordon249efd02013-08-05 03:33:56 -0700133 public CallResult getCallWithId(int callId) {
134 // max 8 connections, so this should be fast even through we are traversing the entire map.
135 for (Entry<Connection, Call> entry : mCallMap.entrySet()) {
136 if (entry.getValue().getCallId() == callId) {
137 return new CallResult(entry.getValue(), entry.getKey());
138 }
139 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700140
141 for (Entry<Connection, Call> entry : mConfCallMap.entrySet()) {
142 if (entry.getValue().getCallId() == callId) {
143 if (entry.getValue().getChildCallIds().size() == 0) {
144 return null;
145 }
146 final CallResult child = getCallWithId(entry.getValue().getChildCallIds().first());
147 return new CallResult(entry.getValue(), child.getActionableCall(),
148 child.getConnection());
149 }
150 }
Santos Cordon249efd02013-08-05 03:33:56 -0700151 return null;
152 }
153
Santos Cordon2eaff902013-08-05 04:37:55 -0700154 public boolean hasOutstandingActiveCall() {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700155 return hasOutstandingActiveCallInternal(mCallMap) ||
156 hasOutstandingActiveCallInternal(mConfCallMap);
157 }
158
159 private static boolean hasOutstandingActiveCallInternal(HashMap<Connection, Call> map) {
160 for (Call call : map.values()) {
161 final int state = call.getState();
162 if (Call.State.ACTIVE == state) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700163 return true;
164 }
165 }
166
167 return false;
168 }
169
Santos Cordon63a84242013-07-23 13:32:52 -0700170 private void onNewRingingConnection(AsyncResult r) {
171 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700172 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700173
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700174 updateCallFromConnection(call, conn, false);
Santos Cordona3d05142013-07-29 11:25:17 -0700175 call.setState(Call.State.INCOMING);
Santos Cordon63a84242013-07-23 13:32:52 -0700176
Christine Chendaf7bf62013-08-05 19:12:31 -0700177 for (int i = 0; i < mListeners.size(); ++i) {
Christine Chenee09a492013-08-06 16:02:29 -0700178 if (call != null) {
179 mListeners.get(i).onIncoming(call,
180 mRejectWithTextMessageManager.loadCannedResponses());
181 }
Santos Cordon63a84242013-07-23 13:32:52 -0700182 }
183 }
184
185 private void onDisconnect(AsyncResult r) {
186 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700187 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700188
Santos Cordon995c8162013-07-29 09:22:22 -0700189 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700190 final boolean wasConferenced = call.getState() == State.CONFERENCED;
191
192 updateCallFromConnection(call, conn, false);
193 call.setState(Call.State.DISCONNECTED);
Santos Cordon63a84242013-07-23 13:32:52 -0700194
Christine Chendaf7bf62013-08-05 19:12:31 -0700195 for (int i = 0; i < mListeners.size(); ++i) {
196 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700197 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700198
199 // If it was a conferenced call, we need to run the entire update
200 // to make the proper changes to parent conference calls.
201 if (wasConferenced) {
202 onPhoneStateChanged(null);
203 }
204
205 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700206 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700207
208 // TODO(klp): Do a final check to see if there are any active calls.
209 // If there are not, totally cancel all calls
Santos Cordon63a84242013-07-23 13:32:52 -0700210 }
211
Santos Cordona3d05142013-07-29 11:25:17 -0700212 /**
213 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700214 */
Santos Cordon995c8162013-07-29 09:22:22 -0700215 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700216 final List<Call> updatedCalls = Lists.newArrayList();
217 doUpdate(false, updatedCalls);
218
Christine Chendaf7bf62013-08-05 19:12:31 -0700219 for (int i = 0; i < mListeners.size(); ++i) {
220 mListeners.get(i).onUpdate(updatedCalls, false);
Santos Cordon998f42b2013-08-02 16:13:12 -0700221 }
222 }
223
224
225 /**
226 * Go through the Calls from CallManager and return the list of calls that were updated.
227 * Or, the full list if requested.
228 */
229 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700230 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
231 telephonyCalls.addAll(mCallManager.getRingingCalls());
232 telephonyCalls.addAll(mCallManager.getForegroundCalls());
233 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
234
Santos Cordona3d05142013-07-29 11:25:17 -0700235 // Cycle through all the Connections on all the Calls. Update our Call objects
236 // to reflect any new state and send the updated Call objects to the handler service.
237 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700238
239 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700240 // new connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700241 // a state in the internal.telephony.Call object. This ensures that staleness
242 // check below fails and we always add the item to the update list if it is new.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700243 final Call call = getCallFromMap(mCallMap, connection, true);
Santos Cordona3d05142013-07-29 11:25:17 -0700244
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700245 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700246 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700247 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700248 }
249 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700250
251 // We do a second loop to address conference call scenarios. We do this as a separate
252 // loop to ensure all child calls are up to date before we start updating the parent
253 // conference calls.
254 for (Connection connection : telephonyCall.getConnections()) {
255 updateForConferenceCalls(connection, out);
256 }
257
Santos Cordona3d05142013-07-29 11:25:17 -0700258 }
Santos Cordona3d05142013-07-29 11:25:17 -0700259 }
260
Santos Cordone38b1ff2013-08-07 12:12:16 -0700261 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700262 * Checks to see if the connection is the first connection in a conference call.
263 * If it is a conference call, we will create a new Conference Call object or
264 * update the existing conference call object for that connection.
265 * If it is not a conference call but a previous associated conference call still exists,
266 * we mark it as idle and remove it from the map.
267 * In both cases above, we add the Calls to be updated to the UI.
268 * @param connection The connection object to check.
269 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
270 */
271 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
272 // We consider this connection a conference connection if the call it
273 // belongs to is a multiparty call AND it is the first connection.
274 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
275 connection.getCall().getEarliestConnection() == connection;
276
277 boolean changed = false;
278
279 // If this connection is the main connection for the conference call, then create or update
280 // a Call object for that conference call.
281 if (isConferenceCallConnection) {
282 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
283 changed = updateCallFromConnection(confCall, connection, true);
284
285 if (changed) {
286 updatedCalls.add(confCall);
287 }
288
289 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
290
291 // It is possible that through a conference call split, there may be lingering conference
292 // calls where this connection was the main connection. We clean those up here.
293 } else {
294 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
295
296 // We found a conference call for this connection, which is no longer a conference call.
297 // Kill it!
298 if (oldConfCall != null) {
299 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
300 mConfCallMap.remove(connection);
301 oldConfCall.setState(State.IDLE);
302 changed = true;
303
304 // add to the list of calls to update
305 updatedCalls.add(oldConfCall);
306 }
307 }
308
309 return changed;
310 }
311
312 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700313 * Updates the Call properties to match the state of the connection object
314 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700315 * @param call The call object to update.
316 * @param connection The connection object from which to update call.
317 * @param isForConference There are slight differences in how we populate data for conference
318 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700319 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700320 private boolean updateCallFromConnection(Call call, Connection connection,
321 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700322 boolean changed = false;
323
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700324 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700325
326 if (call.getState() != newState) {
327 call.setState(newState);
328 changed = true;
329 }
330
Santos Cordone38b1ff2013-08-07 12:12:16 -0700331 final Call.DisconnectCause newDisconnectCause =
332 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
333 if (call.getDisconnectCause() != newDisconnectCause) {
334 call.setDisconnectCause(newDisconnectCause);
335 changed = true;
336 }
337
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700338 final long oldConnectTime = call.getConnectTime();
339 if (oldConnectTime != connection.getConnectTime()) {
340 call.setConnectTime(connection.getConnectTime());
341 changed = true;
342 }
343
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700344 if (!isForConference) {
345 final String oldNumber = call.getNumber();
346 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(connection.getAddress())) {
347 call.setNumber(connection.getAddress());
348 changed = true;
349 }
350
351 final int newNumberPresentation = connection.getNumberPresentation();
352 if (call.getNumberPresentation() != newNumberPresentation) {
353 call.setNumberPresentation(newNumberPresentation);
354 changed = true;
355 }
356
357 final int newCnapNamePresentation = connection.getCnapNamePresentation();
358 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
359 call.setCnapNamePresentation(newCnapNamePresentation);
360 changed = true;
361 }
362
363 final String oldCnapName = call.getCnapName();
364 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
365 call.setCnapName(connection.getCnapName());
366 changed = true;
367 }
368 } else {
369
370 // update the list of children by:
371 // 1) Saving the old set
372 // 2) Removing all children
373 // 3) Adding the correct children into the Call
374 // 4) Comparing the new children set with the old children set
375 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
376 call.removeAllChildren();
377
378 if (connection.getCall() != null) {
379 for (Connection childConn : connection.getCall().getConnections()) {
380 final Call childCall = getCallFromMap(mCallMap, childConn, false);
381 if (childCall != null && childConn.isAlive()) {
382 call.addChildId(childCall.getCallId());
383 }
384 }
385 }
386 changed |= oldSet.equals(call.getChildCallIds());
387 }
388
Santos Cordoneead6ec2013-08-07 22:16:33 -0700389 /**
390 * !!! Uses values from connection and call collected above so this part must be last !!!
391 */
392 final int newCapabilities = getCapabilitiesFor(connection, call);
Santos Cordon26e7b242013-08-07 21:15:45 -0700393 if (call.getCapabilities() != newCapabilities) {
394 call.setCapabilities(newCapabilities);
395 changed = true;
396 }
397
Santos Cordone38b1ff2013-08-07 12:12:16 -0700398 return changed;
399 }
400
Santos Cordon26e7b242013-08-07 21:15:45 -0700401 /**
402 * Returns a mask of capabilities for the connection such as merge, hold, etc.
403 */
Santos Cordoneead6ec2013-08-07 22:16:33 -0700404 private int getCapabilitiesFor(Connection connection, Call call) {
405 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
406 final Phone phone = connection.getCall().getPhone();
407
408 final boolean canHold = TelephonyCapabilities.supportsAnswerAndHold(phone);
409 boolean canAddCall = false;
410 boolean canMergeCall = false;
411 boolean canSwapCall = false;
412
413 // only applies to active calls
414 if (callIsActive) {
415 canAddCall = PhoneUtils.okToAddCall(mCallManager);
416 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
417 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
418 }
419
420 // special rules section!
421 // CDMA always has Add
422 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
423 canAddCall = true;
424 } else {
425 // if neither merge nor add is on...then allow add
426 canAddCall |= !(canAddCall || canMergeCall);
427 }
428
Santos Cordon26e7b242013-08-07 21:15:45 -0700429 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700430 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700431 retval |= Capabilities.HOLD;
432 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700433 if (canAddCall) {
434 retval |= Capabilities.ADD_CALL;
435 }
436 if (canMergeCall) {
437 retval |= Capabilities.MERGE_CALLS;
438 }
439 if (canSwapCall) {
440 retval |= Capabilities.SWAP_CALLS;
441 }
Santos Cordon26e7b242013-08-07 21:15:45 -0700442
443 return retval;
444 }
445
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700446 /**
447 * Returns true if the Connection is part of a multiparty call.
448 * We do this by checking the isMultiparty() method of the telephony.Call object and also
449 * checking to see if more than one of it's children is alive.
450 */
451 private boolean isPartOfLiveConferenceCall(Connection connection) {
452 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
453 int count = 0;
454 for (Connection currConn : connection.getCall().getConnections()) {
455 if (currConn.isAlive()) {
456 count++;
457 if (count >= 2) {
458 return true;
459 }
460 }
461 }
462 }
463 return false;
464 }
465
466 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
467
Santos Cordona3d05142013-07-29 11:25:17 -0700468 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700469 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700470 case ACTIVE:
471 retval = State.ACTIVE;
472 break;
473 case INCOMING:
474 retval = State.INCOMING;
475 break;
476 case DIALING:
477 case ALERTING:
478 retval = State.DIALING;
479 break;
480 case WAITING:
481 retval = State.CALL_WAITING;
482 break;
483 case HOLDING:
484 retval = State.ONHOLD;
485 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700486 case DISCONNECTED:
487 case DISCONNECTING:
488 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700489 default:
490 }
491
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700492 // If we are dealing with a potential child call (not the parent conference call),
493 // the check to see if we have to set the state to CONFERENCED.
494 if (!isForConference) {
495
496 // if the connection is part of a multiparty call, and it is live,
497 // annotate it with CONFERENCED state instead.
498 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
499 return State.CONFERENCED;
500 }
501 }
502
Santos Cordona3d05142013-07-29 11:25:17 -0700503 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700504 }
505
Santos Cordone38b1ff2013-08-07 12:12:16 -0700506 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
507 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
508 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
509 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
510 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
511 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
512 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
513 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
514 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
515 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
516 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
517 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
518 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
519 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
520 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
521 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
522 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
523 Call.DisconnectCause.CDMA_RETRY_ORDER)
524 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
525 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
526 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
527 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
528 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
529 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
530 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
531 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
532 Call.DisconnectCause.ERROR_UNSPECIFIED)
533 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
534 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
535 .put(Connection.DisconnectCause.INCOMING_MISSED,
536 Call.DisconnectCause.INCOMING_MISSED)
537 .put(Connection.DisconnectCause.INCOMING_REJECTED,
538 Call.DisconnectCause.INCOMING_REJECTED)
539 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
540 Call.DisconnectCause.INVALID_CREDENTIALS)
541 .put(Connection.DisconnectCause.INVALID_NUMBER,
542 Call.DisconnectCause.INVALID_NUMBER)
543 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
544 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
545 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
546 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
547 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
548 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
549 Call.DisconnectCause.NOT_DISCONNECTED)
550 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
551 Call.DisconnectCause.NUMBER_UNREACHABLE)
552 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
553 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
554 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
555 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
556 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
557 Call.DisconnectCause.SERVER_UNREACHABLE)
558 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
559 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
560 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
561 .build();
562
563 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
564 Connection.DisconnectCause causeSource) {
565
566 if (CAUSE_MAP.containsKey(causeSource)) {
567 return CAUSE_MAP.get(causeSource);
568 }
569
570 return Call.DisconnectCause.UNKNOWN;
571 }
572
Santos Cordon63a84242013-07-23 13:32:52 -0700573 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700574 * Gets an existing callId for a connection, or creates one if none exists.
575 * This function does NOT set any of the Connection data onto the Call class.
576 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700577 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700578 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
579 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700580 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700581
582 // Find the call id or create if missing and requested.
583 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700584 if (map.containsKey(conn)) {
585 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700586 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700587 call = createNewCall();
588 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700589 }
590 }
Santos Cordon995c8162013-07-29 09:22:22 -0700591 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700592 }
593
594 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700595 * Creates a brand new connection for the call.
596 */
597 private Call createNewCall() {
598 int callId;
599 int newNextCallId;
600 do {
601 callId = mNextCallId.get();
602
603 // protect against overflow
604 newNextCallId = (callId == Integer.MAX_VALUE ?
605 CALL_ID_START_VALUE : callId + 1);
606
607 // Keep looping if the change was not atomic OR the value is already taken.
608 // The call to containsValue() is linear, however, most devices support a
609 // maximum of 7 connections so it's not expensive.
610 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
611
612 return new Call(callId);
613 }
614
615 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700616 * Listener interface for changes to Calls.
617 */
618 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700619 void onDisconnect(Call call);
Christine Chenee09a492013-08-06 16:02:29 -0700620 void onIncoming(Call call, ArrayList<String> textReponses);
Santos Cordon998f42b2013-08-02 16:13:12 -0700621 void onUpdate(List<Call> calls, boolean fullUpdate);
Santos Cordon63a84242013-07-23 13:32:52 -0700622 }
Santos Cordon249efd02013-08-05 03:33:56 -0700623
624 /**
625 * Result class for accessing a call by connection.
626 */
627 public static class CallResult {
628 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700629 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700630 public Connection mConnection;
631
632 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700633 this(call, call, connection);
634 }
635
636 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700637 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700638 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700639 mConnection = connection;
640 }
641
642 public Call getCall() {
643 return mCall;
644 }
645
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700646 // The call that should be used for call actions like hanging up.
647 public Call getActionableCall() {
648 return mActionableCall;
649 }
650
Santos Cordon249efd02013-08-05 03:33:56 -0700651 public Connection getConnection() {
652 return mConnection;
653 }
654 }
Santos Cordon63a84242013-07-23 13:32:52 -0700655}