blob: 502f2cb4beb0e04c7519e55b64436dafe979abbe [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
Santos Cordon63a84242013-07-23 13:32:52 -070019import android.os.AsyncResult;
20import android.os.Handler;
21import android.os.Message;
Santos Cordon4ad64cd2013-08-15 00:36:14 -070022import android.os.SystemProperties;
Santos Cordone38b1ff2013-08-07 12:12:16 -070023import android.text.TextUtils;
24import android.util.Log;
Santos Cordon63a84242013-07-23 13:32:52 -070025
Santos Cordona3d05142013-07-29 11:25:17 -070026import com.android.internal.telephony.CallManager;
Santos Cordon63a84242013-07-23 13:32:52 -070027import com.android.internal.telephony.Connection;
Santos Cordoneead6ec2013-08-07 22:16:33 -070028import com.android.internal.telephony.Phone;
Santos Cordona3d05142013-07-29 11:25:17 -070029import com.android.internal.telephony.PhoneConstants;
Santos Cordon26e7b242013-08-07 21:15:45 -070030import com.android.internal.telephony.TelephonyCapabilities;
Santos Cordon69a69192013-08-22 14:25:42 -070031import com.android.phone.CallGatewayManager.RawGatewayInfo;
Santos Cordon995c8162013-07-29 09:22:22 -070032import com.android.services.telephony.common.Call;
Santos Cordon26e7b242013-08-07 21:15:45 -070033import com.android.services.telephony.common.Call.Capabilities;
Santos Cordona3d05142013-07-29 11:25:17 -070034import com.android.services.telephony.common.Call.State;
Santos Cordon63a84242013-07-23 13:32:52 -070035
Yorke Lee814da302013-08-30 16:01:07 -070036import com.google.android.collect.Lists;
37import com.google.android.collect.Maps;
38import com.google.common.base.Preconditions;
39import com.google.common.collect.ImmutableMap;
40import com.google.common.collect.ImmutableSortedSet;
41
Santos Cordon63a84242013-07-23 13:32:52 -070042import java.util.ArrayList;
43import java.util.HashMap;
44import java.util.List;
Santos Cordon249efd02013-08-05 03:33:56 -070045import java.util.Map.Entry;
Santos Cordon63a84242013-07-23 13:32:52 -070046import java.util.concurrent.atomic.AtomicInteger;
47
48/**
49 * Creates a Call model from Call state and data received from the telephony
50 * layer. The telephony layer maintains 3 conceptual objects: Phone, Call,
51 * Connection.
52 *
53 * Phone represents the radio and there is an implementation per technology
54 * type such as GSMPhone, SipPhone, CDMAPhone, etc. Generally, we will only ever
55 * deal with one instance of this object for the lifetime of this class.
56 *
57 * There are 3 Call instances that exist for the lifetime of this class which
58 * are created by CallTracker. The three are RingingCall, ForegroundCall, and
59 * BackgroundCall.
60 *
61 * A Connection most closely resembles what the layperson would consider a call.
62 * A Connection is created when a user dials and it is "owned" by one of the
63 * three Call instances. Which of the three Calls owns the Connection changes
64 * as the Connection goes between ACTIVE, HOLD, RINGING, and other states.
65 *
66 * This class models a new Call class from Connection objects received from
67 * the telephony layer. We use Connection references as identifiers for a call;
68 * new reference = new call.
69 *
70 * TODO(klp): Create a new Call class to replace the simple call Id ints
71 * being used currently.
72 *
73 * The new Call models are parcellable for transfer via the CallHandlerService
74 * API.
75 */
76public class CallModeler extends Handler {
77
78 private static final String TAG = CallModeler.class.getSimpleName();
Santos Cordon4ad64cd2013-08-15 00:36:14 -070079 private static final boolean DBG =
80 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Santos Cordon63a84242013-07-23 13:32:52 -070081
82 private static final int CALL_ID_START_VALUE = 1;
Santos Cordon63a84242013-07-23 13:32:52 -070083
Santos Cordon998f42b2013-08-02 16:13:12 -070084 private final CallStateMonitor mCallStateMonitor;
85 private final CallManager mCallManager;
Santos Cordon69a69192013-08-22 14:25:42 -070086 private final CallGatewayManager mCallGatewayManager;
Santos Cordon998f42b2013-08-02 16:13:12 -070087 private final HashMap<Connection, Call> mCallMap = Maps.newHashMap();
Santos Cordon4ad64cd2013-08-15 00:36:14 -070088 private final HashMap<Connection, Call> mConfCallMap = Maps.newHashMap();
Santos Cordon998f42b2013-08-02 16:13:12 -070089 private final AtomicInteger mNextCallId = new AtomicInteger(CALL_ID_START_VALUE);
Christine Chendaf7bf62013-08-05 19:12:31 -070090 private final ArrayList<Listener> mListeners = new ArrayList<Listener>();
Christine Chenee09a492013-08-06 16:02:29 -070091 private RejectWithTextMessageManager mRejectWithTextMessageManager;
Santos Cordon63a84242013-07-23 13:32:52 -070092
Christine Chenee09a492013-08-06 16:02:29 -070093 public CallModeler(CallStateMonitor callStateMonitor, CallManager callManager,
Santos Cordon69a69192013-08-22 14:25:42 -070094 RejectWithTextMessageManager rejectWithTextMessageManager,
95 CallGatewayManager callGatewayManager) {
Santos Cordon63a84242013-07-23 13:32:52 -070096 mCallStateMonitor = callStateMonitor;
Santos Cordona3d05142013-07-29 11:25:17 -070097 mCallManager = callManager;
Christine Chenee09a492013-08-06 16:02:29 -070098 mRejectWithTextMessageManager = rejectWithTextMessageManager;
Santos Cordon69a69192013-08-22 14:25:42 -070099 mCallGatewayManager = callGatewayManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700100
101 mCallStateMonitor.addListener(this);
102 }
103
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700104 @Override
Santos Cordon63a84242013-07-23 13:32:52 -0700105 public void handleMessage(Message msg) {
106 switch(msg.what) {
107 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
108 onNewRingingConnection((AsyncResult) msg.obj);
109 break;
110 case CallStateMonitor.PHONE_DISCONNECT:
111 onDisconnect((AsyncResult) msg.obj);
Santos Cordon995c8162013-07-29 09:22:22 -0700112 break;
113 case CallStateMonitor.PHONE_STATE_CHANGED:
114 onPhoneStateChanged((AsyncResult) msg.obj);
115 break;
Chiao Cheng3f015c92013-09-06 15:56:27 -0700116 case CallStateMonitor.PHONE_ON_DIAL_CHARS:
117 onPostDialChars((AsyncResult) msg.obj, (char) msg.arg1);
118 break;
Santos Cordon63a84242013-07-23 13:32:52 -0700119 default:
120 break;
121 }
122 }
123
Christine Chendaf7bf62013-08-05 19:12:31 -0700124 public void addListener(Listener listener) {
Santos Cordon63a84242013-07-23 13:32:52 -0700125 Preconditions.checkNotNull(listener);
Christine Chendaf7bf62013-08-05 19:12:31 -0700126 Preconditions.checkNotNull(mListeners);
Christine Chen4748abd2013-08-07 15:44:15 -0700127 if (!mListeners.contains(listener)) {
128 mListeners.add(listener);
129 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700130 }
131
132 public List<Call> getFullList() {
133 final List<Call> retval = Lists.newArrayList();
134 doUpdate(true, retval);
135 return retval;
Santos Cordon63a84242013-07-23 13:32:52 -0700136 }
137
Santos Cordon249efd02013-08-05 03:33:56 -0700138 public CallResult getCallWithId(int callId) {
139 // max 8 connections, so this should be fast even through we are traversing the entire map.
140 for (Entry<Connection, Call> entry : mCallMap.entrySet()) {
141 if (entry.getValue().getCallId() == callId) {
142 return new CallResult(entry.getValue(), entry.getKey());
143 }
144 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700145
146 for (Entry<Connection, Call> entry : mConfCallMap.entrySet()) {
147 if (entry.getValue().getCallId() == callId) {
148 if (entry.getValue().getChildCallIds().size() == 0) {
149 return null;
150 }
151 final CallResult child = getCallWithId(entry.getValue().getChildCallIds().first());
152 return new CallResult(entry.getValue(), child.getActionableCall(),
153 child.getConnection());
154 }
155 }
Santos Cordon249efd02013-08-05 03:33:56 -0700156 return null;
157 }
158
Santos Cordonaf763a12013-08-19 20:04:58 -0700159 public boolean hasLiveCall() {
160 return hasLiveCallInternal(mCallMap) ||
161 hasLiveCallInternal(mConfCallMap);
162 }
163
164 private boolean hasLiveCallInternal(HashMap<Connection, Call> map) {
165 for (Call call : map.values()) {
166 final int state = call.getState();
167 if (state == Call.State.ACTIVE ||
168 state == Call.State.CALL_WAITING ||
169 state == Call.State.CONFERENCED ||
170 state == Call.State.DIALING ||
171 state == Call.State.INCOMING ||
172 state == Call.State.ONHOLD) {
173 return true;
174 }
175 }
176 return false;
177 }
178
Santos Cordon2b73bd62013-08-27 14:53:43 -0700179 public boolean hasOutstandingActiveOrDialingCall() {
180 return hasOutstandingActiveOrDialingCallInternal(mCallMap) ||
181 hasOutstandingActiveOrDialingCallInternal(mConfCallMap);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700182 }
183
Santos Cordon2b73bd62013-08-27 14:53:43 -0700184 private static boolean hasOutstandingActiveOrDialingCallInternal(
185 HashMap<Connection, Call> map) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700186 for (Call call : map.values()) {
187 final int state = call.getState();
Santos Cordon2b73bd62013-08-27 14:53:43 -0700188 if (state == Call.State.ACTIVE ||
189 state == Call.State.DIALING) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700190 return true;
191 }
192 }
193
194 return false;
195 }
196
Chiao Cheng3f015c92013-09-06 15:56:27 -0700197
198 /**
199 * Handles the POST_ON_DIAL_CHARS message from the Phone (see our call to
200 * mPhone.setOnPostDialCharacter() above.)
201 *
202 * TODO: NEED TO TEST THIS SEQUENCE now that we no longer handle "dialable" key events here in
203 * the InCallScreen: we do directly to the Dialer UI instead. Similarly, we may now need to go
204 * directly to the Dialer to handle POST_ON_DIAL_CHARS too.
205 */
206 private void onPostDialChars(AsyncResult r, char ch) {
207 final Connection c = (Connection) r.result;
208
209 if (c != null) {
210 final Connection.PostDialState state = (Connection.PostDialState) r.userObj;
211
212 switch (state) {
213 // TODO(klp): add other post dial related functions
214 case WAIT:
215 final Call call = getCallFromMap(mCallMap, c, false);
216 if (call == null) {
217 Log.i(TAG, "Call no longer exists. Skipping onPostDialWait().");
218 } else {
219 for (Listener mListener : mListeners) {
220 mListener.onPostDialWait(call.getCallId(),
221 c.getRemainingPostDialString());
222 }
223 }
224 break;
225
226 default:
227 break;
228 }
229 }
230 }
231
Santos Cordon63a84242013-07-23 13:32:52 -0700232 private void onNewRingingConnection(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700233 Log.i(TAG, "onNewRingingConnection");
Santos Cordon63a84242013-07-23 13:32:52 -0700234 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700235 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700236
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700237 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700238
Christine Chendaf7bf62013-08-05 19:12:31 -0700239 for (int i = 0; i < mListeners.size(); ++i) {
Christine Chenee09a492013-08-06 16:02:29 -0700240 if (call != null) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700241 mListeners.get(i).onIncoming(call);
Christine Chenee09a492013-08-06 16:02:29 -0700242 }
Santos Cordon63a84242013-07-23 13:32:52 -0700243 }
244 }
245
246 private void onDisconnect(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700247 Log.i(TAG, "onDisconnect");
Santos Cordon63a84242013-07-23 13:32:52 -0700248 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700249 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700250
Santos Cordon995c8162013-07-29 09:22:22 -0700251 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700252 final boolean wasConferenced = call.getState() == State.CONFERENCED;
253
254 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700255
Christine Chendaf7bf62013-08-05 19:12:31 -0700256 for (int i = 0; i < mListeners.size(); ++i) {
257 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700258 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700259
260 // If it was a conferenced call, we need to run the entire update
261 // to make the proper changes to parent conference calls.
262 if (wasConferenced) {
263 onPhoneStateChanged(null);
264 }
265
266 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700267 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700268
269 // TODO(klp): Do a final check to see if there are any active calls.
270 // If there are not, totally cancel all calls
Santos Cordon63a84242013-07-23 13:32:52 -0700271 }
272
Santos Cordona3d05142013-07-29 11:25:17 -0700273 /**
274 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700275 */
Santos Cordon995c8162013-07-29 09:22:22 -0700276 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700277 Log.i(TAG, "onPhoneStateChanged: ");
Santos Cordon998f42b2013-08-02 16:13:12 -0700278 final List<Call> updatedCalls = Lists.newArrayList();
279 doUpdate(false, updatedCalls);
280
Christine Chendaf7bf62013-08-05 19:12:31 -0700281 for (int i = 0; i < mListeners.size(); ++i) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700282 mListeners.get(i).onUpdate(updatedCalls);
Santos Cordon998f42b2013-08-02 16:13:12 -0700283 }
284 }
285
286
287 /**
288 * Go through the Calls from CallManager and return the list of calls that were updated.
289 * Or, the full list if requested.
290 */
291 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700292 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
293 telephonyCalls.addAll(mCallManager.getRingingCalls());
294 telephonyCalls.addAll(mCallManager.getForegroundCalls());
295 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
296
Santos Cordona3d05142013-07-29 11:25:17 -0700297 // Cycle through all the Connections on all the Calls. Update our Call objects
298 // to reflect any new state and send the updated Call objects to the handler service.
299 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700300
301 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700302 // new connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700303 // a state in the internal.telephony.Call object. This ensures that staleness
304 // check below fails and we always add the item to the update list if it is new.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700305 final Call call = getCallFromMap(mCallMap, connection, true);
Santos Cordona3d05142013-07-29 11:25:17 -0700306
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700307 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordon2b73bd62013-08-27 14:53:43 -0700308
309 Log.i(TAG, "doUpdate: " + call);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700310 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700311 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700312 }
313 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700314
315 // We do a second loop to address conference call scenarios. We do this as a separate
316 // loop to ensure all child calls are up to date before we start updating the parent
317 // conference calls.
318 for (Connection connection : telephonyCall.getConnections()) {
319 updateForConferenceCalls(connection, out);
320 }
321
Santos Cordona3d05142013-07-29 11:25:17 -0700322 }
Santos Cordona3d05142013-07-29 11:25:17 -0700323 }
324
Santos Cordone38b1ff2013-08-07 12:12:16 -0700325 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700326 * Checks to see if the connection is the first connection in a conference call.
327 * If it is a conference call, we will create a new Conference Call object or
328 * update the existing conference call object for that connection.
329 * If it is not a conference call but a previous associated conference call still exists,
330 * we mark it as idle and remove it from the map.
331 * In both cases above, we add the Calls to be updated to the UI.
332 * @param connection The connection object to check.
333 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
334 */
335 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
336 // We consider this connection a conference connection if the call it
337 // belongs to is a multiparty call AND it is the first connection.
338 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
339 connection.getCall().getEarliestConnection() == connection;
340
341 boolean changed = false;
342
343 // If this connection is the main connection for the conference call, then create or update
344 // a Call object for that conference call.
345 if (isConferenceCallConnection) {
346 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
347 changed = updateCallFromConnection(confCall, connection, true);
348
349 if (changed) {
350 updatedCalls.add(confCall);
351 }
352
353 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
354
355 // It is possible that through a conference call split, there may be lingering conference
356 // calls where this connection was the main connection. We clean those up here.
357 } else {
358 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
359
360 // We found a conference call for this connection, which is no longer a conference call.
361 // Kill it!
362 if (oldConfCall != null) {
363 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
364 mConfCallMap.remove(connection);
365 oldConfCall.setState(State.IDLE);
366 changed = true;
367
368 // add to the list of calls to update
369 updatedCalls.add(oldConfCall);
370 }
371 }
372
373 return changed;
374 }
375
376 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700377 * Sets the new call state onto the call and performs some additional logic
378 * associated with setting the state.
379 */
380 private void setNewState(Call call, int newState, Connection connection) {
381 Preconditions.checkState(call.getState() != newState);
382
383 // When starting an outgoing call, we need to grab gateway information
384 // for the call, if available, and set it.
385 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
386
387 if (newState == Call.State.DIALING) {
388 if (!info.isEmpty()) {
389 call.setGatewayNumber(info.getFormattedGatewayNumber());
390 call.setGatewayPackage(info.packageName);
391 }
392 } else if (!Call.State.isConnected(newState)) {
393 mCallGatewayManager.clearGatewayData(connection);
394 }
395
396 call.setState(newState);
397 }
398
399 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700400 * Updates the Call properties to match the state of the connection object
401 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700402 * @param call The call object to update.
403 * @param connection The connection object from which to update call.
404 * @param isForConference There are slight differences in how we populate data for conference
405 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700406 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700407 private boolean updateCallFromConnection(Call call, Connection connection,
408 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700409 boolean changed = false;
410
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700411 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700412
413 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700414 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700415 changed = true;
416 }
417
Santos Cordone38b1ff2013-08-07 12:12:16 -0700418 final Call.DisconnectCause newDisconnectCause =
419 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
420 if (call.getDisconnectCause() != newDisconnectCause) {
421 call.setDisconnectCause(newDisconnectCause);
422 changed = true;
423 }
424
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700425 final long oldConnectTime = call.getConnectTime();
426 if (oldConnectTime != connection.getConnectTime()) {
427 call.setConnectTime(connection.getConnectTime());
428 changed = true;
429 }
430
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700431 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700432 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700433 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700434 String newNumber = connection.getAddress();
435 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
436 if (!info.isEmpty()) {
437 newNumber = info.trueNumber;
438 }
439 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
440 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700441 changed = true;
442 }
443
Santos Cordon69a69192013-08-22 14:25:42 -0700444 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700445 final int newNumberPresentation = connection.getNumberPresentation();
446 if (call.getNumberPresentation() != newNumberPresentation) {
447 call.setNumberPresentation(newNumberPresentation);
448 changed = true;
449 }
450
Santos Cordon69a69192013-08-22 14:25:42 -0700451 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700452 final String oldCnapName = call.getCnapName();
453 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
454 call.setCnapName(connection.getCnapName());
455 changed = true;
456 }
Santos Cordon69a69192013-08-22 14:25:42 -0700457
458 // Name Presentation
459 final int newCnapNamePresentation = connection.getCnapNamePresentation();
460 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
461 call.setCnapNamePresentation(newCnapNamePresentation);
462 changed = true;
463 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700464 } else {
465
466 // update the list of children by:
467 // 1) Saving the old set
468 // 2) Removing all children
469 // 3) Adding the correct children into the Call
470 // 4) Comparing the new children set with the old children set
471 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
472 call.removeAllChildren();
473
474 if (connection.getCall() != null) {
475 for (Connection childConn : connection.getCall().getConnections()) {
476 final Call childCall = getCallFromMap(mCallMap, childConn, false);
477 if (childCall != null && childConn.isAlive()) {
478 call.addChildId(childCall.getCallId());
479 }
480 }
481 }
482 changed |= oldSet.equals(call.getChildCallIds());
483 }
484
Santos Cordoneead6ec2013-08-07 22:16:33 -0700485 /**
486 * !!! Uses values from connection and call collected above so this part must be last !!!
487 */
488 final int newCapabilities = getCapabilitiesFor(connection, call);
Santos Cordon26e7b242013-08-07 21:15:45 -0700489 if (call.getCapabilities() != newCapabilities) {
490 call.setCapabilities(newCapabilities);
491 changed = true;
492 }
493
Santos Cordone38b1ff2013-08-07 12:12:16 -0700494 return changed;
495 }
496
Santos Cordon26e7b242013-08-07 21:15:45 -0700497 /**
498 * Returns a mask of capabilities for the connection such as merge, hold, etc.
499 */
Santos Cordoneead6ec2013-08-07 22:16:33 -0700500 private int getCapabilitiesFor(Connection connection, Call call) {
501 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
502 final Phone phone = connection.getCall().getPhone();
503
504 final boolean canHold = TelephonyCapabilities.supportsAnswerAndHold(phone);
505 boolean canAddCall = false;
506 boolean canMergeCall = false;
507 boolean canSwapCall = false;
Yorke Lee814da302013-08-30 16:01:07 -0700508 boolean canRespondViaText = false;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700509
510 // only applies to active calls
511 if (callIsActive) {
512 canAddCall = PhoneUtils.okToAddCall(mCallManager);
513 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
514 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
515 }
516
Yorke Lee814da302013-08-30 16:01:07 -0700517 canRespondViaText = RejectWithTextMessageManager.allowRespondViaSmsForCall(call,
518 connection);
519
Santos Cordoneead6ec2013-08-07 22:16:33 -0700520 // special rules section!
521 // CDMA always has Add
522 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
523 canAddCall = true;
524 } else {
525 // if neither merge nor add is on...then allow add
526 canAddCall |= !(canAddCall || canMergeCall);
527 }
528
Santos Cordon26e7b242013-08-07 21:15:45 -0700529 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700530 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700531 retval |= Capabilities.HOLD;
532 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700533 if (canAddCall) {
534 retval |= Capabilities.ADD_CALL;
535 }
536 if (canMergeCall) {
537 retval |= Capabilities.MERGE_CALLS;
538 }
539 if (canSwapCall) {
540 retval |= Capabilities.SWAP_CALLS;
541 }
Santos Cordon26e7b242013-08-07 21:15:45 -0700542
Yorke Lee814da302013-08-30 16:01:07 -0700543 if (canRespondViaText) {
544 retval |= Capabilities.RESPOND_VIA_TEXT;
545 }
546
Santos Cordon26e7b242013-08-07 21:15:45 -0700547 return retval;
548 }
549
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700550 /**
551 * Returns true if the Connection is part of a multiparty call.
552 * We do this by checking the isMultiparty() method of the telephony.Call object and also
553 * checking to see if more than one of it's children is alive.
554 */
555 private boolean isPartOfLiveConferenceCall(Connection connection) {
556 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
557 int count = 0;
558 for (Connection currConn : connection.getCall().getConnections()) {
559 if (currConn.isAlive()) {
560 count++;
561 if (count >= 2) {
562 return true;
563 }
564 }
565 }
566 }
567 return false;
568 }
569
570 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
571
Santos Cordona3d05142013-07-29 11:25:17 -0700572 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700573 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700574 case ACTIVE:
575 retval = State.ACTIVE;
576 break;
577 case INCOMING:
578 retval = State.INCOMING;
579 break;
580 case DIALING:
581 case ALERTING:
582 retval = State.DIALING;
583 break;
584 case WAITING:
585 retval = State.CALL_WAITING;
586 break;
587 case HOLDING:
588 retval = State.ONHOLD;
589 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700590 case DISCONNECTED:
591 case DISCONNECTING:
592 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700593 default:
594 }
595
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700596 // If we are dealing with a potential child call (not the parent conference call),
597 // the check to see if we have to set the state to CONFERENCED.
598 if (!isForConference) {
599
600 // if the connection is part of a multiparty call, and it is live,
601 // annotate it with CONFERENCED state instead.
602 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
603 return State.CONFERENCED;
604 }
605 }
606
Santos Cordona3d05142013-07-29 11:25:17 -0700607 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700608 }
609
Santos Cordone38b1ff2013-08-07 12:12:16 -0700610 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
611 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
612 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
613 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
614 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
615 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
616 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
617 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
618 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
619 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
620 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
621 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
622 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
623 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
624 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
625 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
626 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
627 Call.DisconnectCause.CDMA_RETRY_ORDER)
628 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
629 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
630 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
631 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
632 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
633 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
634 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
635 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
636 Call.DisconnectCause.ERROR_UNSPECIFIED)
637 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
638 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
639 .put(Connection.DisconnectCause.INCOMING_MISSED,
640 Call.DisconnectCause.INCOMING_MISSED)
641 .put(Connection.DisconnectCause.INCOMING_REJECTED,
642 Call.DisconnectCause.INCOMING_REJECTED)
643 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
644 Call.DisconnectCause.INVALID_CREDENTIALS)
645 .put(Connection.DisconnectCause.INVALID_NUMBER,
646 Call.DisconnectCause.INVALID_NUMBER)
647 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
648 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
649 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
650 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
651 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
652 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
653 Call.DisconnectCause.NOT_DISCONNECTED)
654 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
655 Call.DisconnectCause.NUMBER_UNREACHABLE)
656 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
657 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
658 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
659 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
660 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
661 Call.DisconnectCause.SERVER_UNREACHABLE)
662 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
663 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
664 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
665 .build();
666
667 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
668 Connection.DisconnectCause causeSource) {
669
670 if (CAUSE_MAP.containsKey(causeSource)) {
671 return CAUSE_MAP.get(causeSource);
672 }
673
674 return Call.DisconnectCause.UNKNOWN;
675 }
676
Santos Cordon63a84242013-07-23 13:32:52 -0700677 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700678 * Gets an existing callId for a connection, or creates one if none exists.
679 * This function does NOT set any of the Connection data onto the Call class.
680 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700681 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700682 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
683 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700684 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700685
686 // Find the call id or create if missing and requested.
687 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700688 if (map.containsKey(conn)) {
689 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700690 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700691 call = createNewCall();
692 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700693 }
694 }
Santos Cordon995c8162013-07-29 09:22:22 -0700695 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700696 }
697
698 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700699 * Creates a brand new connection for the call.
700 */
701 private Call createNewCall() {
702 int callId;
703 int newNextCallId;
704 do {
705 callId = mNextCallId.get();
706
707 // protect against overflow
708 newNextCallId = (callId == Integer.MAX_VALUE ?
709 CALL_ID_START_VALUE : callId + 1);
710
711 // Keep looping if the change was not atomic OR the value is already taken.
712 // The call to containsValue() is linear, however, most devices support a
713 // maximum of 7 connections so it's not expensive.
714 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
715
716 return new Call(callId);
717 }
718
719 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700720 * Listener interface for changes to Calls.
721 */
722 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700723 void onDisconnect(Call call);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700724 void onIncoming(Call call);
725 void onUpdate(List<Call> calls);
Chiao Cheng3f015c92013-09-06 15:56:27 -0700726 void onPostDialWait(int callId, String remainingChars);
Santos Cordon63a84242013-07-23 13:32:52 -0700727 }
Santos Cordon249efd02013-08-05 03:33:56 -0700728
729 /**
730 * Result class for accessing a call by connection.
731 */
732 public static class CallResult {
733 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700734 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700735 public Connection mConnection;
736
737 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700738 this(call, call, connection);
739 }
740
741 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700742 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700743 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700744 mConnection = connection;
745 }
746
747 public Call getCall() {
748 return mCall;
749 }
750
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700751 // The call that should be used for call actions like hanging up.
752 public Call getActionableCall() {
753 return mActionableCall;
754 }
755
Santos Cordon249efd02013-08-05 03:33:56 -0700756 public Connection getConnection() {
757 return mConnection;
758 }
759 }
Santos Cordon63a84242013-07-23 13:32:52 -0700760}