blob: 94584a23af87a2393d58de615d944425effc9672 [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 Cordona3d05142013-07-29 11:25:17 -0700202 call.setState(Call.State.INCOMING);
Santos Cordon63a84242013-07-23 13:32:52 -0700203
Christine Chendaf7bf62013-08-05 19:12:31 -0700204 for (int i = 0; i < mListeners.size(); ++i) {
Christine Chenee09a492013-08-06 16:02:29 -0700205 if (call != null) {
206 mListeners.get(i).onIncoming(call,
207 mRejectWithTextMessageManager.loadCannedResponses());
208 }
Santos Cordon63a84242013-07-23 13:32:52 -0700209 }
210 }
211
212 private void onDisconnect(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700213 Log.i(TAG, "onDisconnect");
Santos Cordon63a84242013-07-23 13:32:52 -0700214 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700215 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700216
Santos Cordon995c8162013-07-29 09:22:22 -0700217 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700218 final boolean wasConferenced = call.getState() == State.CONFERENCED;
219
220 updateCallFromConnection(call, conn, false);
221 call.setState(Call.State.DISCONNECTED);
Santos Cordon63a84242013-07-23 13:32:52 -0700222
Christine Chendaf7bf62013-08-05 19:12:31 -0700223 for (int i = 0; i < mListeners.size(); ++i) {
224 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700225 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700226
227 // If it was a conferenced call, we need to run the entire update
228 // to make the proper changes to parent conference calls.
229 if (wasConferenced) {
230 onPhoneStateChanged(null);
231 }
232
233 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700234 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700235
236 // TODO(klp): Do a final check to see if there are any active calls.
237 // If there are not, totally cancel all calls
Santos Cordon63a84242013-07-23 13:32:52 -0700238 }
239
Santos Cordona3d05142013-07-29 11:25:17 -0700240 /**
241 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700242 */
Santos Cordon995c8162013-07-29 09:22:22 -0700243 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700244 Log.i(TAG, "onPhoneStateChanged: ");
Santos Cordon998f42b2013-08-02 16:13:12 -0700245 final List<Call> updatedCalls = Lists.newArrayList();
246 doUpdate(false, updatedCalls);
247
Christine Chendaf7bf62013-08-05 19:12:31 -0700248 for (int i = 0; i < mListeners.size(); ++i) {
249 mListeners.get(i).onUpdate(updatedCalls, false);
Santos Cordon998f42b2013-08-02 16:13:12 -0700250 }
251 }
252
253
254 /**
255 * Go through the Calls from CallManager and return the list of calls that were updated.
256 * Or, the full list if requested.
257 */
258 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700259 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
260 telephonyCalls.addAll(mCallManager.getRingingCalls());
261 telephonyCalls.addAll(mCallManager.getForegroundCalls());
262 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
263
Santos Cordona3d05142013-07-29 11:25:17 -0700264 // Cycle through all the Connections on all the Calls. Update our Call objects
265 // to reflect any new state and send the updated Call objects to the handler service.
266 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700267
268 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700269 // new connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700270 // a state in the internal.telephony.Call object. This ensures that staleness
271 // check below fails and we always add the item to the update list if it is new.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700272 final Call call = getCallFromMap(mCallMap, connection, true);
Santos Cordona3d05142013-07-29 11:25:17 -0700273
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700274 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordon2b73bd62013-08-27 14:53:43 -0700275
276 Log.i(TAG, "doUpdate: " + call);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700277 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700278 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700279 }
280 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700281
282 // We do a second loop to address conference call scenarios. We do this as a separate
283 // loop to ensure all child calls are up to date before we start updating the parent
284 // conference calls.
285 for (Connection connection : telephonyCall.getConnections()) {
286 updateForConferenceCalls(connection, out);
287 }
288
Santos Cordona3d05142013-07-29 11:25:17 -0700289 }
Santos Cordona3d05142013-07-29 11:25:17 -0700290 }
291
Santos Cordone38b1ff2013-08-07 12:12:16 -0700292 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700293 * Checks to see if the connection is the first connection in a conference call.
294 * If it is a conference call, we will create a new Conference Call object or
295 * update the existing conference call object for that connection.
296 * If it is not a conference call but a previous associated conference call still exists,
297 * we mark it as idle and remove it from the map.
298 * In both cases above, we add the Calls to be updated to the UI.
299 * @param connection The connection object to check.
300 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
301 */
302 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
303 // We consider this connection a conference connection if the call it
304 // belongs to is a multiparty call AND it is the first connection.
305 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
306 connection.getCall().getEarliestConnection() == connection;
307
308 boolean changed = false;
309
310 // If this connection is the main connection for the conference call, then create or update
311 // a Call object for that conference call.
312 if (isConferenceCallConnection) {
313 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
314 changed = updateCallFromConnection(confCall, connection, true);
315
316 if (changed) {
317 updatedCalls.add(confCall);
318 }
319
320 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
321
322 // It is possible that through a conference call split, there may be lingering conference
323 // calls where this connection was the main connection. We clean those up here.
324 } else {
325 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
326
327 // We found a conference call for this connection, which is no longer a conference call.
328 // Kill it!
329 if (oldConfCall != null) {
330 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
331 mConfCallMap.remove(connection);
332 oldConfCall.setState(State.IDLE);
333 changed = true;
334
335 // add to the list of calls to update
336 updatedCalls.add(oldConfCall);
337 }
338 }
339
340 return changed;
341 }
342
343 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700344 * Sets the new call state onto the call and performs some additional logic
345 * associated with setting the state.
346 */
347 private void setNewState(Call call, int newState, Connection connection) {
348 Preconditions.checkState(call.getState() != newState);
349
350 // When starting an outgoing call, we need to grab gateway information
351 // for the call, if available, and set it.
352 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
353
354 if (newState == Call.State.DIALING) {
355 if (!info.isEmpty()) {
356 call.setGatewayNumber(info.getFormattedGatewayNumber());
357 call.setGatewayPackage(info.packageName);
358 }
359 } else if (!Call.State.isConnected(newState)) {
360 mCallGatewayManager.clearGatewayData(connection);
361 }
362
363 call.setState(newState);
364 }
365
366 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700367 * Updates the Call properties to match the state of the connection object
368 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700369 * @param call The call object to update.
370 * @param connection The connection object from which to update call.
371 * @param isForConference There are slight differences in how we populate data for conference
372 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700373 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700374 private boolean updateCallFromConnection(Call call, Connection connection,
375 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700376 boolean changed = false;
377
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700378 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700379
380 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700381 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700382 changed = true;
383 }
384
Santos Cordone38b1ff2013-08-07 12:12:16 -0700385 final Call.DisconnectCause newDisconnectCause =
386 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
387 if (call.getDisconnectCause() != newDisconnectCause) {
388 call.setDisconnectCause(newDisconnectCause);
389 changed = true;
390 }
391
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700392 final long oldConnectTime = call.getConnectTime();
393 if (oldConnectTime != connection.getConnectTime()) {
394 call.setConnectTime(connection.getConnectTime());
395 changed = true;
396 }
397
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700398 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700399 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700400 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700401 String newNumber = connection.getAddress();
402 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
403 if (!info.isEmpty()) {
404 newNumber = info.trueNumber;
405 }
406 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
407 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700408 changed = true;
409 }
410
Santos Cordon69a69192013-08-22 14:25:42 -0700411 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700412 final int newNumberPresentation = connection.getNumberPresentation();
413 if (call.getNumberPresentation() != newNumberPresentation) {
414 call.setNumberPresentation(newNumberPresentation);
415 changed = true;
416 }
417
Santos Cordon69a69192013-08-22 14:25:42 -0700418 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700419 final String oldCnapName = call.getCnapName();
420 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
421 call.setCnapName(connection.getCnapName());
422 changed = true;
423 }
Santos Cordon69a69192013-08-22 14:25:42 -0700424
425 // Name Presentation
426 final int newCnapNamePresentation = connection.getCnapNamePresentation();
427 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
428 call.setCnapNamePresentation(newCnapNamePresentation);
429 changed = true;
430 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700431 } else {
432
433 // update the list of children by:
434 // 1) Saving the old set
435 // 2) Removing all children
436 // 3) Adding the correct children into the Call
437 // 4) Comparing the new children set with the old children set
438 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
439 call.removeAllChildren();
440
441 if (connection.getCall() != null) {
442 for (Connection childConn : connection.getCall().getConnections()) {
443 final Call childCall = getCallFromMap(mCallMap, childConn, false);
444 if (childCall != null && childConn.isAlive()) {
445 call.addChildId(childCall.getCallId());
446 }
447 }
448 }
449 changed |= oldSet.equals(call.getChildCallIds());
450 }
451
Santos Cordoneead6ec2013-08-07 22:16:33 -0700452 /**
453 * !!! Uses values from connection and call collected above so this part must be last !!!
454 */
455 final int newCapabilities = getCapabilitiesFor(connection, call);
Santos Cordon26e7b242013-08-07 21:15:45 -0700456 if (call.getCapabilities() != newCapabilities) {
457 call.setCapabilities(newCapabilities);
458 changed = true;
459 }
460
Santos Cordone38b1ff2013-08-07 12:12:16 -0700461 return changed;
462 }
463
Santos Cordon26e7b242013-08-07 21:15:45 -0700464 /**
465 * Returns a mask of capabilities for the connection such as merge, hold, etc.
466 */
Santos Cordoneead6ec2013-08-07 22:16:33 -0700467 private int getCapabilitiesFor(Connection connection, Call call) {
468 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
469 final Phone phone = connection.getCall().getPhone();
470
471 final boolean canHold = TelephonyCapabilities.supportsAnswerAndHold(phone);
472 boolean canAddCall = false;
473 boolean canMergeCall = false;
474 boolean canSwapCall = false;
475
476 // only applies to active calls
477 if (callIsActive) {
478 canAddCall = PhoneUtils.okToAddCall(mCallManager);
479 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
480 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
481 }
482
483 // special rules section!
484 // CDMA always has Add
485 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
486 canAddCall = true;
487 } else {
488 // if neither merge nor add is on...then allow add
489 canAddCall |= !(canAddCall || canMergeCall);
490 }
491
Santos Cordon26e7b242013-08-07 21:15:45 -0700492 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700493 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700494 retval |= Capabilities.HOLD;
495 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700496 if (canAddCall) {
497 retval |= Capabilities.ADD_CALL;
498 }
499 if (canMergeCall) {
500 retval |= Capabilities.MERGE_CALLS;
501 }
502 if (canSwapCall) {
503 retval |= Capabilities.SWAP_CALLS;
504 }
Santos Cordon26e7b242013-08-07 21:15:45 -0700505
506 return retval;
507 }
508
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700509 /**
510 * Returns true if the Connection is part of a multiparty call.
511 * We do this by checking the isMultiparty() method of the telephony.Call object and also
512 * checking to see if more than one of it's children is alive.
513 */
514 private boolean isPartOfLiveConferenceCall(Connection connection) {
515 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
516 int count = 0;
517 for (Connection currConn : connection.getCall().getConnections()) {
518 if (currConn.isAlive()) {
519 count++;
520 if (count >= 2) {
521 return true;
522 }
523 }
524 }
525 }
526 return false;
527 }
528
529 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
530
Santos Cordona3d05142013-07-29 11:25:17 -0700531 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700532 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700533 case ACTIVE:
534 retval = State.ACTIVE;
535 break;
536 case INCOMING:
537 retval = State.INCOMING;
538 break;
539 case DIALING:
540 case ALERTING:
541 retval = State.DIALING;
542 break;
543 case WAITING:
544 retval = State.CALL_WAITING;
545 break;
546 case HOLDING:
547 retval = State.ONHOLD;
548 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700549 case DISCONNECTED:
550 case DISCONNECTING:
551 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700552 default:
553 }
554
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700555 // If we are dealing with a potential child call (not the parent conference call),
556 // the check to see if we have to set the state to CONFERENCED.
557 if (!isForConference) {
558
559 // if the connection is part of a multiparty call, and it is live,
560 // annotate it with CONFERENCED state instead.
561 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
562 return State.CONFERENCED;
563 }
564 }
565
Santos Cordona3d05142013-07-29 11:25:17 -0700566 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700567 }
568
Santos Cordone38b1ff2013-08-07 12:12:16 -0700569 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
570 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
571 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
572 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
573 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
574 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
575 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
576 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
577 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
578 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
579 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
580 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
581 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
582 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
583 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
584 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
585 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
586 Call.DisconnectCause.CDMA_RETRY_ORDER)
587 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
588 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
589 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
590 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
591 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
592 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
593 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
594 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
595 Call.DisconnectCause.ERROR_UNSPECIFIED)
596 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
597 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
598 .put(Connection.DisconnectCause.INCOMING_MISSED,
599 Call.DisconnectCause.INCOMING_MISSED)
600 .put(Connection.DisconnectCause.INCOMING_REJECTED,
601 Call.DisconnectCause.INCOMING_REJECTED)
602 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
603 Call.DisconnectCause.INVALID_CREDENTIALS)
604 .put(Connection.DisconnectCause.INVALID_NUMBER,
605 Call.DisconnectCause.INVALID_NUMBER)
606 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
607 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
608 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
609 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
610 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
611 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
612 Call.DisconnectCause.NOT_DISCONNECTED)
613 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
614 Call.DisconnectCause.NUMBER_UNREACHABLE)
615 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
616 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
617 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
618 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
619 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
620 Call.DisconnectCause.SERVER_UNREACHABLE)
621 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
622 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
623 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
624 .build();
625
626 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
627 Connection.DisconnectCause causeSource) {
628
629 if (CAUSE_MAP.containsKey(causeSource)) {
630 return CAUSE_MAP.get(causeSource);
631 }
632
633 return Call.DisconnectCause.UNKNOWN;
634 }
635
Santos Cordon63a84242013-07-23 13:32:52 -0700636 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700637 * Gets an existing callId for a connection, or creates one if none exists.
638 * This function does NOT set any of the Connection data onto the Call class.
639 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700640 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700641 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
642 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700643 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700644
645 // Find the call id or create if missing and requested.
646 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700647 if (map.containsKey(conn)) {
648 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700649 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700650 call = createNewCall();
651 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700652 }
653 }
Santos Cordon995c8162013-07-29 09:22:22 -0700654 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700655 }
656
657 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700658 * Creates a brand new connection for the call.
659 */
660 private Call createNewCall() {
661 int callId;
662 int newNextCallId;
663 do {
664 callId = mNextCallId.get();
665
666 // protect against overflow
667 newNextCallId = (callId == Integer.MAX_VALUE ?
668 CALL_ID_START_VALUE : callId + 1);
669
670 // Keep looping if the change was not atomic OR the value is already taken.
671 // The call to containsValue() is linear, however, most devices support a
672 // maximum of 7 connections so it's not expensive.
673 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
674
675 return new Call(callId);
676 }
677
678 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700679 * Listener interface for changes to Calls.
680 */
681 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700682 void onDisconnect(Call call);
Christine Chenee09a492013-08-06 16:02:29 -0700683 void onIncoming(Call call, ArrayList<String> textReponses);
Santos Cordon998f42b2013-08-02 16:13:12 -0700684 void onUpdate(List<Call> calls, boolean fullUpdate);
Santos Cordon63a84242013-07-23 13:32:52 -0700685 }
Santos Cordon249efd02013-08-05 03:33:56 -0700686
687 /**
688 * Result class for accessing a call by connection.
689 */
690 public static class CallResult {
691 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700692 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700693 public Connection mConnection;
694
695 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700696 this(call, call, connection);
697 }
698
699 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700700 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700701 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700702 mConnection = connection;
703 }
704
705 public Call getCall() {
706 return mCall;
707 }
708
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700709 // The call that should be used for call actions like hanging up.
710 public Call getActionableCall() {
711 return mActionableCall;
712 }
713
Santos Cordon249efd02013-08-05 03:33:56 -0700714 public Connection getConnection() {
715 return mConnection;
716 }
717 }
Santos Cordon63a84242013-07-23 13:32:52 -0700718}