blob: b11b8254bba3017727fac89dd3fc5e277fae48c5 [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
Yorke Lee814da302013-08-30 16:01:07 -070019import android.content.Context;
Santos Cordon63a84242013-07-23 13:32:52 -070020import android.os.AsyncResult;
21import android.os.Handler;
22import android.os.Message;
Santos Cordon4ad64cd2013-08-15 00:36:14 -070023import android.os.SystemProperties;
Santos Cordone38b1ff2013-08-07 12:12:16 -070024import android.text.TextUtils;
25import android.util.Log;
Santos Cordon63a84242013-07-23 13:32:52 -070026
Santos Cordona3d05142013-07-29 11:25:17 -070027import com.android.internal.telephony.CallManager;
Santos Cordon63a84242013-07-23 13:32:52 -070028import com.android.internal.telephony.Connection;
Santos Cordoneead6ec2013-08-07 22:16:33 -070029import com.android.internal.telephony.Phone;
Santos Cordona3d05142013-07-29 11:25:17 -070030import com.android.internal.telephony.PhoneConstants;
Santos Cordon26e7b242013-08-07 21:15:45 -070031import com.android.internal.telephony.TelephonyCapabilities;
Santos Cordon69a69192013-08-22 14:25:42 -070032import com.android.phone.CallGatewayManager.RawGatewayInfo;
Santos Cordon995c8162013-07-29 09:22:22 -070033import com.android.services.telephony.common.Call;
Santos Cordon26e7b242013-08-07 21:15:45 -070034import com.android.services.telephony.common.Call.Capabilities;
Santos Cordona3d05142013-07-29 11:25:17 -070035import com.android.services.telephony.common.Call.State;
Santos Cordon63a84242013-07-23 13:32:52 -070036
Yorke Lee814da302013-08-30 16:01:07 -070037import com.google.android.collect.Lists;
38import com.google.android.collect.Maps;
39import com.google.common.base.Preconditions;
40import com.google.common.collect.ImmutableMap;
41import com.google.common.collect.ImmutableSortedSet;
42
Santos Cordon63a84242013-07-23 13:32:52 -070043import java.util.ArrayList;
44import java.util.HashMap;
45import java.util.List;
Santos Cordon249efd02013-08-05 03:33:56 -070046import java.util.Map.Entry;
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;
Santos Cordon69a69192013-08-22 14:25:42 -070087 private final CallGatewayManager mCallGatewayManager;
Santos Cordon998f42b2013-08-02 16:13:12 -070088 private final HashMap<Connection, Call> mCallMap = Maps.newHashMap();
Santos Cordon4ad64cd2013-08-15 00:36:14 -070089 private final HashMap<Connection, Call> mConfCallMap = Maps.newHashMap();
Santos Cordon998f42b2013-08-02 16:13:12 -070090 private final AtomicInteger mNextCallId = new AtomicInteger(CALL_ID_START_VALUE);
Christine Chendaf7bf62013-08-05 19:12:31 -070091 private final ArrayList<Listener> mListeners = new ArrayList<Listener>();
Christine Chenee09a492013-08-06 16:02:29 -070092 private RejectWithTextMessageManager mRejectWithTextMessageManager;
Santos Cordon63a84242013-07-23 13:32:52 -070093
Christine Chenee09a492013-08-06 16:02:29 -070094 public CallModeler(CallStateMonitor callStateMonitor, CallManager callManager,
Santos Cordon69a69192013-08-22 14:25:42 -070095 RejectWithTextMessageManager rejectWithTextMessageManager,
96 CallGatewayManager callGatewayManager) {
Santos Cordon63a84242013-07-23 13:32:52 -070097 mCallStateMonitor = callStateMonitor;
Santos Cordona3d05142013-07-29 11:25:17 -070098 mCallManager = callManager;
Christine Chenee09a492013-08-06 16:02:29 -070099 mRejectWithTextMessageManager = rejectWithTextMessageManager;
Santos Cordon69a69192013-08-22 14:25:42 -0700100 mCallGatewayManager = callGatewayManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700101
102 mCallStateMonitor.addListener(this);
103 }
104
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700105 @Override
Santos Cordon63a84242013-07-23 13:32:52 -0700106 public void handleMessage(Message msg) {
107 switch(msg.what) {
108 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
109 onNewRingingConnection((AsyncResult) msg.obj);
110 break;
111 case CallStateMonitor.PHONE_DISCONNECT:
112 onDisconnect((AsyncResult) msg.obj);
Santos Cordon995c8162013-07-29 09:22:22 -0700113 break;
114 case CallStateMonitor.PHONE_STATE_CHANGED:
115 onPhoneStateChanged((AsyncResult) msg.obj);
116 break;
Santos Cordon63a84242013-07-23 13:32:52 -0700117 default:
118 break;
119 }
120 }
121
Christine Chendaf7bf62013-08-05 19:12:31 -0700122 public void addListener(Listener listener) {
Santos Cordon63a84242013-07-23 13:32:52 -0700123 Preconditions.checkNotNull(listener);
Christine Chendaf7bf62013-08-05 19:12:31 -0700124 Preconditions.checkNotNull(mListeners);
Christine Chen4748abd2013-08-07 15:44:15 -0700125 if (!mListeners.contains(listener)) {
126 mListeners.add(listener);
127 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700128 }
129
130 public List<Call> getFullList() {
131 final List<Call> retval = Lists.newArrayList();
132 doUpdate(true, retval);
133 return retval;
Santos Cordon63a84242013-07-23 13:32:52 -0700134 }
135
Santos Cordon249efd02013-08-05 03:33:56 -0700136 public CallResult getCallWithId(int callId) {
137 // max 8 connections, so this should be fast even through we are traversing the entire map.
138 for (Entry<Connection, Call> entry : mCallMap.entrySet()) {
139 if (entry.getValue().getCallId() == callId) {
140 return new CallResult(entry.getValue(), entry.getKey());
141 }
142 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700143
144 for (Entry<Connection, Call> entry : mConfCallMap.entrySet()) {
145 if (entry.getValue().getCallId() == callId) {
146 if (entry.getValue().getChildCallIds().size() == 0) {
147 return null;
148 }
149 final CallResult child = getCallWithId(entry.getValue().getChildCallIds().first());
150 return new CallResult(entry.getValue(), child.getActionableCall(),
151 child.getConnection());
152 }
153 }
Santos Cordon249efd02013-08-05 03:33:56 -0700154 return null;
155 }
156
Santos Cordonaf763a12013-08-19 20:04:58 -0700157 public boolean hasLiveCall() {
158 return hasLiveCallInternal(mCallMap) ||
159 hasLiveCallInternal(mConfCallMap);
160 }
161
162 private boolean hasLiveCallInternal(HashMap<Connection, Call> map) {
163 for (Call call : map.values()) {
164 final int state = call.getState();
165 if (state == Call.State.ACTIVE ||
166 state == Call.State.CALL_WAITING ||
167 state == Call.State.CONFERENCED ||
168 state == Call.State.DIALING ||
169 state == Call.State.INCOMING ||
170 state == Call.State.ONHOLD) {
171 return true;
172 }
173 }
174 return false;
175 }
176
Santos Cordon2b73bd62013-08-27 14:53:43 -0700177 public boolean hasOutstandingActiveOrDialingCall() {
178 return hasOutstandingActiveOrDialingCallInternal(mCallMap) ||
179 hasOutstandingActiveOrDialingCallInternal(mConfCallMap);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700180 }
181
Santos Cordon2b73bd62013-08-27 14:53:43 -0700182 private static boolean hasOutstandingActiveOrDialingCallInternal(
183 HashMap<Connection, Call> map) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700184 for (Call call : map.values()) {
185 final int state = call.getState();
Santos Cordon2b73bd62013-08-27 14:53:43 -0700186 if (state == Call.State.ACTIVE ||
187 state == Call.State.DIALING) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700188 return true;
189 }
190 }
191
192 return false;
193 }
194
Santos Cordon63a84242013-07-23 13:32:52 -0700195 private void onNewRingingConnection(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700196 Log.i(TAG, "onNewRingingConnection");
Santos Cordon63a84242013-07-23 13:32:52 -0700197 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700198 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700199
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700200 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700201
Christine Chendaf7bf62013-08-05 19:12:31 -0700202 for (int i = 0; i < mListeners.size(); ++i) {
Christine Chenee09a492013-08-06 16:02:29 -0700203 if (call != null) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700204 mListeners.get(i).onIncoming(call);
Christine Chenee09a492013-08-06 16:02:29 -0700205 }
Santos Cordon63a84242013-07-23 13:32:52 -0700206 }
207 }
208
209 private void onDisconnect(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700210 Log.i(TAG, "onDisconnect");
Santos Cordon63a84242013-07-23 13:32:52 -0700211 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700212 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700213
Santos Cordon995c8162013-07-29 09:22:22 -0700214 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700215 final boolean wasConferenced = call.getState() == State.CONFERENCED;
216
217 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700218
Christine Chendaf7bf62013-08-05 19:12:31 -0700219 for (int i = 0; i < mListeners.size(); ++i) {
220 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700221 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700222
223 // If it was a conferenced call, we need to run the entire update
224 // to make the proper changes to parent conference calls.
225 if (wasConferenced) {
226 onPhoneStateChanged(null);
227 }
228
229 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700230 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700231
232 // TODO(klp): Do a final check to see if there are any active calls.
233 // If there are not, totally cancel all calls
Santos Cordon63a84242013-07-23 13:32:52 -0700234 }
235
Santos Cordona3d05142013-07-29 11:25:17 -0700236 /**
237 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700238 */
Santos Cordon995c8162013-07-29 09:22:22 -0700239 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700240 Log.i(TAG, "onPhoneStateChanged: ");
Santos Cordon998f42b2013-08-02 16:13:12 -0700241 final List<Call> updatedCalls = Lists.newArrayList();
242 doUpdate(false, updatedCalls);
243
Christine Chendaf7bf62013-08-05 19:12:31 -0700244 for (int i = 0; i < mListeners.size(); ++i) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700245 mListeners.get(i).onUpdate(updatedCalls);
Santos Cordon998f42b2013-08-02 16:13:12 -0700246 }
247 }
248
249
250 /**
251 * Go through the Calls from CallManager and return the list of calls that were updated.
252 * Or, the full list if requested.
253 */
254 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700255 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
256 telephonyCalls.addAll(mCallManager.getRingingCalls());
257 telephonyCalls.addAll(mCallManager.getForegroundCalls());
258 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
259
Santos Cordona3d05142013-07-29 11:25:17 -0700260 // Cycle through all the Connections on all the Calls. Update our Call objects
261 // to reflect any new state and send the updated Call objects to the handler service.
262 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700263
264 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700265 // new connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700266 // a state in the internal.telephony.Call object. This ensures that staleness
267 // check below fails and we always add the item to the update list if it is new.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700268 final Call call = getCallFromMap(mCallMap, connection, true);
Santos Cordona3d05142013-07-29 11:25:17 -0700269
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700270 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordon2b73bd62013-08-27 14:53:43 -0700271
272 Log.i(TAG, "doUpdate: " + call);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700273 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700274 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700275 }
276 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700277
278 // We do a second loop to address conference call scenarios. We do this as a separate
279 // loop to ensure all child calls are up to date before we start updating the parent
280 // conference calls.
281 for (Connection connection : telephonyCall.getConnections()) {
282 updateForConferenceCalls(connection, out);
283 }
284
Santos Cordona3d05142013-07-29 11:25:17 -0700285 }
Santos Cordona3d05142013-07-29 11:25:17 -0700286 }
287
Santos Cordone38b1ff2013-08-07 12:12:16 -0700288 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700289 * Checks to see if the connection is the first connection in a conference call.
290 * If it is a conference call, we will create a new Conference Call object or
291 * update the existing conference call object for that connection.
292 * If it is not a conference call but a previous associated conference call still exists,
293 * we mark it as idle and remove it from the map.
294 * In both cases above, we add the Calls to be updated to the UI.
295 * @param connection The connection object to check.
296 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
297 */
298 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
299 // We consider this connection a conference connection if the call it
300 // belongs to is a multiparty call AND it is the first connection.
301 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
302 connection.getCall().getEarliestConnection() == connection;
303
304 boolean changed = false;
305
306 // If this connection is the main connection for the conference call, then create or update
307 // a Call object for that conference call.
308 if (isConferenceCallConnection) {
309 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
310 changed = updateCallFromConnection(confCall, connection, true);
311
312 if (changed) {
313 updatedCalls.add(confCall);
314 }
315
316 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
317
318 // It is possible that through a conference call split, there may be lingering conference
319 // calls where this connection was the main connection. We clean those up here.
320 } else {
321 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
322
323 // We found a conference call for this connection, which is no longer a conference call.
324 // Kill it!
325 if (oldConfCall != null) {
326 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
327 mConfCallMap.remove(connection);
328 oldConfCall.setState(State.IDLE);
329 changed = true;
330
331 // add to the list of calls to update
332 updatedCalls.add(oldConfCall);
333 }
334 }
335
336 return changed;
337 }
338
339 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700340 * Sets the new call state onto the call and performs some additional logic
341 * associated with setting the state.
342 */
343 private void setNewState(Call call, int newState, Connection connection) {
344 Preconditions.checkState(call.getState() != newState);
345
346 // When starting an outgoing call, we need to grab gateway information
347 // for the call, if available, and set it.
348 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
349
350 if (newState == Call.State.DIALING) {
351 if (!info.isEmpty()) {
352 call.setGatewayNumber(info.getFormattedGatewayNumber());
353 call.setGatewayPackage(info.packageName);
354 }
355 } else if (!Call.State.isConnected(newState)) {
356 mCallGatewayManager.clearGatewayData(connection);
357 }
358
359 call.setState(newState);
360 }
361
362 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700363 * Updates the Call properties to match the state of the connection object
364 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700365 * @param call The call object to update.
366 * @param connection The connection object from which to update call.
367 * @param isForConference There are slight differences in how we populate data for conference
368 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700369 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700370 private boolean updateCallFromConnection(Call call, Connection connection,
371 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700372 boolean changed = false;
373
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700374 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700375
376 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700377 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700378 changed = true;
379 }
380
Santos Cordone38b1ff2013-08-07 12:12:16 -0700381 final Call.DisconnectCause newDisconnectCause =
382 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
383 if (call.getDisconnectCause() != newDisconnectCause) {
384 call.setDisconnectCause(newDisconnectCause);
385 changed = true;
386 }
387
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700388 final long oldConnectTime = call.getConnectTime();
389 if (oldConnectTime != connection.getConnectTime()) {
390 call.setConnectTime(connection.getConnectTime());
391 changed = true;
392 }
393
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700394 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700395 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700396 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700397 String newNumber = connection.getAddress();
398 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
399 if (!info.isEmpty()) {
400 newNumber = info.trueNumber;
401 }
402 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
403 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700404 changed = true;
405 }
406
Santos Cordon69a69192013-08-22 14:25:42 -0700407 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700408 final int newNumberPresentation = connection.getNumberPresentation();
409 if (call.getNumberPresentation() != newNumberPresentation) {
410 call.setNumberPresentation(newNumberPresentation);
411 changed = true;
412 }
413
Santos Cordon69a69192013-08-22 14:25:42 -0700414 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700415 final String oldCnapName = call.getCnapName();
416 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
417 call.setCnapName(connection.getCnapName());
418 changed = true;
419 }
Santos Cordon69a69192013-08-22 14:25:42 -0700420
421 // Name Presentation
422 final int newCnapNamePresentation = connection.getCnapNamePresentation();
423 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
424 call.setCnapNamePresentation(newCnapNamePresentation);
425 changed = true;
426 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700427 } else {
428
429 // update the list of children by:
430 // 1) Saving the old set
431 // 2) Removing all children
432 // 3) Adding the correct children into the Call
433 // 4) Comparing the new children set with the old children set
434 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
435 call.removeAllChildren();
436
437 if (connection.getCall() != null) {
438 for (Connection childConn : connection.getCall().getConnections()) {
439 final Call childCall = getCallFromMap(mCallMap, childConn, false);
440 if (childCall != null && childConn.isAlive()) {
441 call.addChildId(childCall.getCallId());
442 }
443 }
444 }
Christine Chen45277022013-09-05 10:55:37 -0700445 changed |= !oldSet.equals(call.getChildCallIds());
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700446 }
447
Santos Cordoneead6ec2013-08-07 22:16:33 -0700448 /**
449 * !!! Uses values from connection and call collected above so this part must be last !!!
450 */
451 final int newCapabilities = getCapabilitiesFor(connection, call);
Santos Cordon26e7b242013-08-07 21:15:45 -0700452 if (call.getCapabilities() != newCapabilities) {
453 call.setCapabilities(newCapabilities);
454 changed = true;
455 }
456
Santos Cordone38b1ff2013-08-07 12:12:16 -0700457 return changed;
458 }
459
Santos Cordon26e7b242013-08-07 21:15:45 -0700460 /**
461 * Returns a mask of capabilities for the connection such as merge, hold, etc.
462 */
Santos Cordoneead6ec2013-08-07 22:16:33 -0700463 private int getCapabilitiesFor(Connection connection, Call call) {
464 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
465 final Phone phone = connection.getCall().getPhone();
466
467 final boolean canHold = TelephonyCapabilities.supportsAnswerAndHold(phone);
468 boolean canAddCall = false;
469 boolean canMergeCall = false;
470 boolean canSwapCall = false;
Yorke Lee814da302013-08-30 16:01:07 -0700471 boolean canRespondViaText = false;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700472
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
Yorke Lee814da302013-08-30 16:01:07 -0700480 canRespondViaText = RejectWithTextMessageManager.allowRespondViaSmsForCall(call,
481 connection);
482
Santos Cordoneead6ec2013-08-07 22:16:33 -0700483 // 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
Yorke Lee814da302013-08-30 16:01:07 -0700506 if (canRespondViaText) {
507 retval |= Capabilities.RESPOND_VIA_TEXT;
508 }
509
Santos Cordon26e7b242013-08-07 21:15:45 -0700510 return retval;
511 }
512
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700513 /**
514 * Returns true if the Connection is part of a multiparty call.
515 * We do this by checking the isMultiparty() method of the telephony.Call object and also
516 * checking to see if more than one of it's children is alive.
517 */
518 private boolean isPartOfLiveConferenceCall(Connection connection) {
519 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
520 int count = 0;
521 for (Connection currConn : connection.getCall().getConnections()) {
522 if (currConn.isAlive()) {
523 count++;
524 if (count >= 2) {
525 return true;
526 }
527 }
528 }
529 }
530 return false;
531 }
532
533 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
534
Santos Cordona3d05142013-07-29 11:25:17 -0700535 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700536 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700537 case ACTIVE:
538 retval = State.ACTIVE;
539 break;
540 case INCOMING:
541 retval = State.INCOMING;
542 break;
543 case DIALING:
544 case ALERTING:
545 retval = State.DIALING;
546 break;
547 case WAITING:
548 retval = State.CALL_WAITING;
549 break;
550 case HOLDING:
551 retval = State.ONHOLD;
552 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700553 case DISCONNECTED:
554 case DISCONNECTING:
555 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700556 default:
557 }
558
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700559 // If we are dealing with a potential child call (not the parent conference call),
560 // the check to see if we have to set the state to CONFERENCED.
561 if (!isForConference) {
562
563 // if the connection is part of a multiparty call, and it is live,
564 // annotate it with CONFERENCED state instead.
565 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
566 return State.CONFERENCED;
567 }
568 }
569
Santos Cordona3d05142013-07-29 11:25:17 -0700570 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700571 }
572
Santos Cordone38b1ff2013-08-07 12:12:16 -0700573 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
574 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
575 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
576 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
577 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
578 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
579 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
580 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
581 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
582 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
583 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
584 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
585 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
586 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
587 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
588 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
589 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
590 Call.DisconnectCause.CDMA_RETRY_ORDER)
591 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
592 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
593 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
594 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
595 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
596 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
597 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
598 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
599 Call.DisconnectCause.ERROR_UNSPECIFIED)
600 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
601 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
602 .put(Connection.DisconnectCause.INCOMING_MISSED,
603 Call.DisconnectCause.INCOMING_MISSED)
604 .put(Connection.DisconnectCause.INCOMING_REJECTED,
605 Call.DisconnectCause.INCOMING_REJECTED)
606 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
607 Call.DisconnectCause.INVALID_CREDENTIALS)
608 .put(Connection.DisconnectCause.INVALID_NUMBER,
609 Call.DisconnectCause.INVALID_NUMBER)
610 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
611 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
612 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
613 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
614 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
615 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
616 Call.DisconnectCause.NOT_DISCONNECTED)
617 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
618 Call.DisconnectCause.NUMBER_UNREACHABLE)
619 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
620 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
621 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
622 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
623 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
624 Call.DisconnectCause.SERVER_UNREACHABLE)
625 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
626 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
627 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
628 .build();
629
630 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
631 Connection.DisconnectCause causeSource) {
632
633 if (CAUSE_MAP.containsKey(causeSource)) {
634 return CAUSE_MAP.get(causeSource);
635 }
636
637 return Call.DisconnectCause.UNKNOWN;
638 }
639
Santos Cordon63a84242013-07-23 13:32:52 -0700640 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700641 * Gets an existing callId for a connection, or creates one if none exists.
642 * This function does NOT set any of the Connection data onto the Call class.
643 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700644 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700645 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
646 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700647 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700648
649 // Find the call id or create if missing and requested.
650 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700651 if (map.containsKey(conn)) {
652 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700653 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700654 call = createNewCall();
655 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700656 }
657 }
Santos Cordon995c8162013-07-29 09:22:22 -0700658 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700659 }
660
661 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700662 * Creates a brand new connection for the call.
663 */
664 private Call createNewCall() {
665 int callId;
666 int newNextCallId;
667 do {
668 callId = mNextCallId.get();
669
670 // protect against overflow
671 newNextCallId = (callId == Integer.MAX_VALUE ?
672 CALL_ID_START_VALUE : callId + 1);
673
674 // Keep looping if the change was not atomic OR the value is already taken.
675 // The call to containsValue() is linear, however, most devices support a
676 // maximum of 7 connections so it's not expensive.
677 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
678
679 return new Call(callId);
680 }
681
682 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700683 * Listener interface for changes to Calls.
684 */
685 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700686 void onDisconnect(Call call);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700687 void onIncoming(Call call);
688 void onUpdate(List<Call> calls);
Santos Cordon63a84242013-07-23 13:32:52 -0700689 }
Santos Cordon249efd02013-08-05 03:33:56 -0700690
691 /**
692 * Result class for accessing a call by connection.
693 */
694 public static class CallResult {
695 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700696 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700697 public Connection mConnection;
698
699 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700700 this(call, call, connection);
701 }
702
703 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700704 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700705 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700706 mConnection = connection;
707 }
708
709 public Call getCall() {
710 return mCall;
711 }
712
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700713 // The call that should be used for call actions like hanging up.
714 public Call getActionableCall() {
715 return mActionableCall;
716 }
717
Santos Cordon249efd02013-08-05 03:33:56 -0700718 public Connection getConnection() {
719 return mConnection;
720 }
721 }
Santos Cordon63a84242013-07-23 13:32:52 -0700722}