blob: a5e20446b22720dcf5c9932674a78e2e4629fa59 [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 Cordon2eaff902013-08-05 04:37:55 -0700178 public boolean hasOutstandingActiveCall() {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700179 return hasOutstandingActiveCallInternal(mCallMap) ||
180 hasOutstandingActiveCallInternal(mConfCallMap);
181 }
182
183 private static boolean hasOutstandingActiveCallInternal(HashMap<Connection, Call> map) {
184 for (Call call : map.values()) {
185 final int state = call.getState();
186 if (Call.State.ACTIVE == state) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700187 return true;
188 }
189 }
190
191 return false;
192 }
193
Santos Cordon63a84242013-07-23 13:32:52 -0700194 private void onNewRingingConnection(AsyncResult r) {
195 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700196 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700197
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700198 updateCallFromConnection(call, conn, false);
Santos Cordona3d05142013-07-29 11:25:17 -0700199 call.setState(Call.State.INCOMING);
Santos Cordon63a84242013-07-23 13:32:52 -0700200
Christine Chendaf7bf62013-08-05 19:12:31 -0700201 for (int i = 0; i < mListeners.size(); ++i) {
Christine Chenee09a492013-08-06 16:02:29 -0700202 if (call != null) {
203 mListeners.get(i).onIncoming(call,
204 mRejectWithTextMessageManager.loadCannedResponses());
205 }
Santos Cordon63a84242013-07-23 13:32:52 -0700206 }
207 }
208
209 private void onDisconnect(AsyncResult r) {
210 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700211 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700212
Santos Cordon995c8162013-07-29 09:22:22 -0700213 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700214 final boolean wasConferenced = call.getState() == State.CONFERENCED;
215
216 updateCallFromConnection(call, conn, false);
217 call.setState(Call.State.DISCONNECTED);
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 Cordon998f42b2013-08-02 16:13:12 -0700240 final List<Call> updatedCalls = Lists.newArrayList();
241 doUpdate(false, updatedCalls);
242
Christine Chendaf7bf62013-08-05 19:12:31 -0700243 for (int i = 0; i < mListeners.size(); ++i) {
244 mListeners.get(i).onUpdate(updatedCalls, false);
Santos Cordon998f42b2013-08-02 16:13:12 -0700245 }
246 }
247
248
249 /**
250 * Go through the Calls from CallManager and return the list of calls that were updated.
251 * Or, the full list if requested.
252 */
253 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700254 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
255 telephonyCalls.addAll(mCallManager.getRingingCalls());
256 telephonyCalls.addAll(mCallManager.getForegroundCalls());
257 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
258
Santos Cordona3d05142013-07-29 11:25:17 -0700259 // Cycle through all the Connections on all the Calls. Update our Call objects
260 // to reflect any new state and send the updated Call objects to the handler service.
261 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700262
263 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700264 // new connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700265 // a state in the internal.telephony.Call object. This ensures that staleness
266 // check below fails and we always add the item to the update list if it is new.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700267 final Call call = getCallFromMap(mCallMap, connection, true);
Santos Cordona3d05142013-07-29 11:25:17 -0700268
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700269 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700270 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700271 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700272 }
273 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700274
275 // We do a second loop to address conference call scenarios. We do this as a separate
276 // loop to ensure all child calls are up to date before we start updating the parent
277 // conference calls.
278 for (Connection connection : telephonyCall.getConnections()) {
279 updateForConferenceCalls(connection, out);
280 }
281
Santos Cordona3d05142013-07-29 11:25:17 -0700282 }
Santos Cordona3d05142013-07-29 11:25:17 -0700283 }
284
Santos Cordone38b1ff2013-08-07 12:12:16 -0700285 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700286 * Checks to see if the connection is the first connection in a conference call.
287 * If it is a conference call, we will create a new Conference Call object or
288 * update the existing conference call object for that connection.
289 * If it is not a conference call but a previous associated conference call still exists,
290 * we mark it as idle and remove it from the map.
291 * In both cases above, we add the Calls to be updated to the UI.
292 * @param connection The connection object to check.
293 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
294 */
295 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
296 // We consider this connection a conference connection if the call it
297 // belongs to is a multiparty call AND it is the first connection.
298 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
299 connection.getCall().getEarliestConnection() == connection;
300
301 boolean changed = false;
302
303 // If this connection is the main connection for the conference call, then create or update
304 // a Call object for that conference call.
305 if (isConferenceCallConnection) {
306 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
307 changed = updateCallFromConnection(confCall, connection, true);
308
309 if (changed) {
310 updatedCalls.add(confCall);
311 }
312
313 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
314
315 // It is possible that through a conference call split, there may be lingering conference
316 // calls where this connection was the main connection. We clean those up here.
317 } else {
318 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
319
320 // We found a conference call for this connection, which is no longer a conference call.
321 // Kill it!
322 if (oldConfCall != null) {
323 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
324 mConfCallMap.remove(connection);
325 oldConfCall.setState(State.IDLE);
326 changed = true;
327
328 // add to the list of calls to update
329 updatedCalls.add(oldConfCall);
330 }
331 }
332
333 return changed;
334 }
335
336 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700337 * Sets the new call state onto the call and performs some additional logic
338 * associated with setting the state.
339 */
340 private void setNewState(Call call, int newState, Connection connection) {
341 Preconditions.checkState(call.getState() != newState);
342
343 // When starting an outgoing call, we need to grab gateway information
344 // for the call, if available, and set it.
345 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
346
347 if (newState == Call.State.DIALING) {
348 if (!info.isEmpty()) {
349 call.setGatewayNumber(info.getFormattedGatewayNumber());
350 call.setGatewayPackage(info.packageName);
351 }
352 } else if (!Call.State.isConnected(newState)) {
353 mCallGatewayManager.clearGatewayData(connection);
354 }
355
356 call.setState(newState);
357 }
358
359 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700360 * Updates the Call properties to match the state of the connection object
361 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700362 * @param call The call object to update.
363 * @param connection The connection object from which to update call.
364 * @param isForConference There are slight differences in how we populate data for conference
365 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700366 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700367 private boolean updateCallFromConnection(Call call, Connection connection,
368 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700369 boolean changed = false;
370
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700371 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700372
373 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700374 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700375 changed = true;
376 }
377
Santos Cordone38b1ff2013-08-07 12:12:16 -0700378 final Call.DisconnectCause newDisconnectCause =
379 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
380 if (call.getDisconnectCause() != newDisconnectCause) {
381 call.setDisconnectCause(newDisconnectCause);
382 changed = true;
383 }
384
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700385 final long oldConnectTime = call.getConnectTime();
386 if (oldConnectTime != connection.getConnectTime()) {
387 call.setConnectTime(connection.getConnectTime());
388 changed = true;
389 }
390
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700391 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700392 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700393 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700394 String newNumber = connection.getAddress();
395 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
396 if (!info.isEmpty()) {
397 newNumber = info.trueNumber;
398 }
399 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
400 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700401 changed = true;
402 }
403
Santos Cordon69a69192013-08-22 14:25:42 -0700404 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700405 final int newNumberPresentation = connection.getNumberPresentation();
406 if (call.getNumberPresentation() != newNumberPresentation) {
407 call.setNumberPresentation(newNumberPresentation);
408 changed = true;
409 }
410
Santos Cordon69a69192013-08-22 14:25:42 -0700411 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700412 final String oldCnapName = call.getCnapName();
413 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
414 call.setCnapName(connection.getCnapName());
415 changed = true;
416 }
Santos Cordon69a69192013-08-22 14:25:42 -0700417
418 // Name Presentation
419 final int newCnapNamePresentation = connection.getCnapNamePresentation();
420 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
421 call.setCnapNamePresentation(newCnapNamePresentation);
422 changed = true;
423 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700424 } else {
425
426 // update the list of children by:
427 // 1) Saving the old set
428 // 2) Removing all children
429 // 3) Adding the correct children into the Call
430 // 4) Comparing the new children set with the old children set
431 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
432 call.removeAllChildren();
433
434 if (connection.getCall() != null) {
435 for (Connection childConn : connection.getCall().getConnections()) {
436 final Call childCall = getCallFromMap(mCallMap, childConn, false);
437 if (childCall != null && childConn.isAlive()) {
438 call.addChildId(childCall.getCallId());
439 }
440 }
441 }
442 changed |= oldSet.equals(call.getChildCallIds());
443 }
444
Santos Cordoneead6ec2013-08-07 22:16:33 -0700445 /**
446 * !!! Uses values from connection and call collected above so this part must be last !!!
447 */
448 final int newCapabilities = getCapabilitiesFor(connection, call);
Santos Cordon26e7b242013-08-07 21:15:45 -0700449 if (call.getCapabilities() != newCapabilities) {
450 call.setCapabilities(newCapabilities);
451 changed = true;
452 }
453
Santos Cordone38b1ff2013-08-07 12:12:16 -0700454 return changed;
455 }
456
Santos Cordon26e7b242013-08-07 21:15:45 -0700457 /**
458 * Returns a mask of capabilities for the connection such as merge, hold, etc.
459 */
Santos Cordoneead6ec2013-08-07 22:16:33 -0700460 private int getCapabilitiesFor(Connection connection, Call call) {
461 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
462 final Phone phone = connection.getCall().getPhone();
463
464 final boolean canHold = TelephonyCapabilities.supportsAnswerAndHold(phone);
465 boolean canAddCall = false;
466 boolean canMergeCall = false;
467 boolean canSwapCall = false;
468
469 // only applies to active calls
470 if (callIsActive) {
471 canAddCall = PhoneUtils.okToAddCall(mCallManager);
472 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
473 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
474 }
475
476 // special rules section!
477 // CDMA always has Add
478 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
479 canAddCall = true;
480 } else {
481 // if neither merge nor add is on...then allow add
482 canAddCall |= !(canAddCall || canMergeCall);
483 }
484
Santos Cordon26e7b242013-08-07 21:15:45 -0700485 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700486 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700487 retval |= Capabilities.HOLD;
488 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700489 if (canAddCall) {
490 retval |= Capabilities.ADD_CALL;
491 }
492 if (canMergeCall) {
493 retval |= Capabilities.MERGE_CALLS;
494 }
495 if (canSwapCall) {
496 retval |= Capabilities.SWAP_CALLS;
497 }
Santos Cordon26e7b242013-08-07 21:15:45 -0700498
499 return retval;
500 }
501
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700502 /**
503 * Returns true if the Connection is part of a multiparty call.
504 * We do this by checking the isMultiparty() method of the telephony.Call object and also
505 * checking to see if more than one of it's children is alive.
506 */
507 private boolean isPartOfLiveConferenceCall(Connection connection) {
508 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
509 int count = 0;
510 for (Connection currConn : connection.getCall().getConnections()) {
511 if (currConn.isAlive()) {
512 count++;
513 if (count >= 2) {
514 return true;
515 }
516 }
517 }
518 }
519 return false;
520 }
521
522 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
523
Santos Cordona3d05142013-07-29 11:25:17 -0700524 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700525 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700526 case ACTIVE:
527 retval = State.ACTIVE;
528 break;
529 case INCOMING:
530 retval = State.INCOMING;
531 break;
532 case DIALING:
533 case ALERTING:
534 retval = State.DIALING;
535 break;
536 case WAITING:
537 retval = State.CALL_WAITING;
538 break;
539 case HOLDING:
540 retval = State.ONHOLD;
541 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700542 case DISCONNECTED:
543 case DISCONNECTING:
544 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700545 default:
546 }
547
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700548 // If we are dealing with a potential child call (not the parent conference call),
549 // the check to see if we have to set the state to CONFERENCED.
550 if (!isForConference) {
551
552 // if the connection is part of a multiparty call, and it is live,
553 // annotate it with CONFERENCED state instead.
554 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
555 return State.CONFERENCED;
556 }
557 }
558
Santos Cordona3d05142013-07-29 11:25:17 -0700559 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700560 }
561
Santos Cordone38b1ff2013-08-07 12:12:16 -0700562 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
563 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
564 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
565 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
566 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
567 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
568 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
569 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
570 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
571 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
572 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
573 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
574 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
575 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
576 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
577 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
578 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
579 Call.DisconnectCause.CDMA_RETRY_ORDER)
580 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
581 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
582 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
583 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
584 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
585 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
586 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
587 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
588 Call.DisconnectCause.ERROR_UNSPECIFIED)
589 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
590 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
591 .put(Connection.DisconnectCause.INCOMING_MISSED,
592 Call.DisconnectCause.INCOMING_MISSED)
593 .put(Connection.DisconnectCause.INCOMING_REJECTED,
594 Call.DisconnectCause.INCOMING_REJECTED)
595 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
596 Call.DisconnectCause.INVALID_CREDENTIALS)
597 .put(Connection.DisconnectCause.INVALID_NUMBER,
598 Call.DisconnectCause.INVALID_NUMBER)
599 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
600 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
601 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
602 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
603 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
604 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
605 Call.DisconnectCause.NOT_DISCONNECTED)
606 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
607 Call.DisconnectCause.NUMBER_UNREACHABLE)
608 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
609 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
610 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
611 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
612 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
613 Call.DisconnectCause.SERVER_UNREACHABLE)
614 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
615 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
616 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
617 .build();
618
619 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
620 Connection.DisconnectCause causeSource) {
621
622 if (CAUSE_MAP.containsKey(causeSource)) {
623 return CAUSE_MAP.get(causeSource);
624 }
625
626 return Call.DisconnectCause.UNKNOWN;
627 }
628
Santos Cordon63a84242013-07-23 13:32:52 -0700629 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700630 * Gets an existing callId for a connection, or creates one if none exists.
631 * This function does NOT set any of the Connection data onto the Call class.
632 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700633 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700634 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
635 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700636 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700637
638 // Find the call id or create if missing and requested.
639 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700640 if (map.containsKey(conn)) {
641 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700642 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700643 call = createNewCall();
644 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700645 }
646 }
Santos Cordon995c8162013-07-29 09:22:22 -0700647 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700648 }
649
650 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700651 * Creates a brand new connection for the call.
652 */
653 private Call createNewCall() {
654 int callId;
655 int newNextCallId;
656 do {
657 callId = mNextCallId.get();
658
659 // protect against overflow
660 newNextCallId = (callId == Integer.MAX_VALUE ?
661 CALL_ID_START_VALUE : callId + 1);
662
663 // Keep looping if the change was not atomic OR the value is already taken.
664 // The call to containsValue() is linear, however, most devices support a
665 // maximum of 7 connections so it's not expensive.
666 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
667
668 return new Call(callId);
669 }
670
671 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700672 * Listener interface for changes to Calls.
673 */
674 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700675 void onDisconnect(Call call);
Christine Chenee09a492013-08-06 16:02:29 -0700676 void onIncoming(Call call, ArrayList<String> textReponses);
Santos Cordon998f42b2013-08-02 16:13:12 -0700677 void onUpdate(List<Call> calls, boolean fullUpdate);
Santos Cordon63a84242013-07-23 13:32:52 -0700678 }
Santos Cordon249efd02013-08-05 03:33:56 -0700679
680 /**
681 * Result class for accessing a call by connection.
682 */
683 public static class CallResult {
684 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700685 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700686 public Connection mConnection;
687
688 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700689 this(call, call, connection);
690 }
691
692 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700693 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700694 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700695 mConnection = connection;
696 }
697
698 public Call getCall() {
699 return mCall;
700 }
701
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700702 // The call that should be used for call actions like hanging up.
703 public Call getActionableCall() {
704 return mActionableCall;
705 }
706
Santos Cordon249efd02013-08-05 03:33:56 -0700707 public Connection getConnection() {
708 return mConnection;
709 }
710 }
Santos Cordon63a84242013-07-23 13:32:52 -0700711}