blob: ae0ac4896412a42b02f756f4a7650f7bb0c15ef0 [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;
Christine Chenaf2fd0a2013-09-13 16:27:40 -070023import android.telephony.PhoneNumberUtils;
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 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) {
Christine Chen69050202013-09-14 15:36:35 -0700148 return new CallResult(entry.getValue(), entry.getKey());
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700149 }
150 }
Santos Cordon249efd02013-08-05 03:33:56 -0700151 return null;
152 }
153
Santos Cordonaf763a12013-08-19 20:04:58 -0700154 public boolean hasLiveCall() {
155 return hasLiveCallInternal(mCallMap) ||
156 hasLiveCallInternal(mConfCallMap);
157 }
158
159 private boolean hasLiveCallInternal(HashMap<Connection, Call> map) {
160 for (Call call : map.values()) {
161 final int state = call.getState();
162 if (state == Call.State.ACTIVE ||
163 state == Call.State.CALL_WAITING ||
164 state == Call.State.CONFERENCED ||
165 state == Call.State.DIALING ||
166 state == Call.State.INCOMING ||
167 state == Call.State.ONHOLD) {
168 return true;
169 }
170 }
171 return false;
172 }
173
Santos Cordon2b73bd62013-08-27 14:53:43 -0700174 public boolean hasOutstandingActiveOrDialingCall() {
175 return hasOutstandingActiveOrDialingCallInternal(mCallMap) ||
176 hasOutstandingActiveOrDialingCallInternal(mConfCallMap);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700177 }
178
Santos Cordon2b73bd62013-08-27 14:53:43 -0700179 private static boolean hasOutstandingActiveOrDialingCallInternal(
180 HashMap<Connection, Call> map) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700181 for (Call call : map.values()) {
182 final int state = call.getState();
Santos Cordon2b73bd62013-08-27 14:53:43 -0700183 if (state == Call.State.ACTIVE ||
184 state == Call.State.DIALING) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700185 return true;
186 }
187 }
188
189 return false;
190 }
191
Chiao Cheng3f015c92013-09-06 15:56:27 -0700192
193 /**
194 * Handles the POST_ON_DIAL_CHARS message from the Phone (see our call to
195 * mPhone.setOnPostDialCharacter() above.)
196 *
197 * TODO: NEED TO TEST THIS SEQUENCE now that we no longer handle "dialable" key events here in
198 * the InCallScreen: we do directly to the Dialer UI instead. Similarly, we may now need to go
199 * directly to the Dialer to handle POST_ON_DIAL_CHARS too.
200 */
201 private void onPostDialChars(AsyncResult r, char ch) {
202 final Connection c = (Connection) r.result;
203
204 if (c != null) {
205 final Connection.PostDialState state = (Connection.PostDialState) r.userObj;
206
207 switch (state) {
208 // TODO(klp): add other post dial related functions
209 case WAIT:
210 final Call call = getCallFromMap(mCallMap, c, false);
211 if (call == null) {
212 Log.i(TAG, "Call no longer exists. Skipping onPostDialWait().");
213 } else {
214 for (Listener mListener : mListeners) {
215 mListener.onPostDialWait(call.getCallId(),
216 c.getRemainingPostDialString());
217 }
218 }
219 break;
220
221 default:
222 break;
223 }
224 }
225 }
226
Santos Cordon63a84242013-07-23 13:32:52 -0700227 private void onNewRingingConnection(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700228 Log.i(TAG, "onNewRingingConnection");
Santos Cordon63a84242013-07-23 13:32:52 -0700229 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700230 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700231
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700232 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700233
Christine Chendaf7bf62013-08-05 19:12:31 -0700234 for (int i = 0; i < mListeners.size(); ++i) {
Christine Chenee09a492013-08-06 16:02:29 -0700235 if (call != null) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700236 mListeners.get(i).onIncoming(call);
Christine Chenee09a492013-08-06 16:02:29 -0700237 }
Santos Cordon63a84242013-07-23 13:32:52 -0700238 }
239 }
240
241 private void onDisconnect(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700242 Log.i(TAG, "onDisconnect");
Santos Cordon63a84242013-07-23 13:32:52 -0700243 final Connection conn = (Connection) r.result;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700244 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700245
Santos Cordon995c8162013-07-29 09:22:22 -0700246 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700247 final boolean wasConferenced = call.getState() == State.CONFERENCED;
248
249 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700250
Christine Chendaf7bf62013-08-05 19:12:31 -0700251 for (int i = 0; i < mListeners.size(); ++i) {
252 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700253 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700254
255 // If it was a conferenced call, we need to run the entire update
256 // to make the proper changes to parent conference calls.
257 if (wasConferenced) {
258 onPhoneStateChanged(null);
259 }
260
261 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700262 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700263
264 // TODO(klp): Do a final check to see if there are any active calls.
265 // If there are not, totally cancel all calls
Santos Cordon63a84242013-07-23 13:32:52 -0700266 }
267
Santos Cordona3d05142013-07-29 11:25:17 -0700268 /**
269 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700270 */
Santos Cordon995c8162013-07-29 09:22:22 -0700271 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700272 Log.i(TAG, "onPhoneStateChanged: ");
Santos Cordon998f42b2013-08-02 16:13:12 -0700273 final List<Call> updatedCalls = Lists.newArrayList();
274 doUpdate(false, updatedCalls);
275
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700276 if (updatedCalls.size() > 0) {
277 for (int i = 0; i < mListeners.size(); ++i) {
278 mListeners.get(i).onUpdate(updatedCalls);
279 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700280 }
281 }
282
283
284 /**
285 * Go through the Calls from CallManager and return the list of calls that were updated.
286 * Or, the full list if requested.
287 */
288 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700289 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
290 telephonyCalls.addAll(mCallManager.getRingingCalls());
291 telephonyCalls.addAll(mCallManager.getForegroundCalls());
292 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
293
Santos Cordona3d05142013-07-29 11:25:17 -0700294 // Cycle through all the Connections on all the Calls. Update our Call objects
295 // to reflect any new state and send the updated Call objects to the handler service.
296 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700297
298 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon12a03aa2013-09-12 23:34:05 -0700299 // We only send updates for live calls which are not incoming (ringing).
300 // Disconnected and incoming calls are handled by onDisconnect and
301 // onNewRingingConnection.
302 boolean shouldUpdate = connection.getState().isAlive() &&
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700303 !connection.getState().isRinging();
304
305 // New connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700306 // a state in the internal.telephony.Call object. This ensures that staleness
307 // check below fails and we always add the item to the update list if it is new.
Santos Cordon12a03aa2013-09-12 23:34:05 -0700308 final Call call = getCallFromMap(mCallMap, connection, shouldUpdate /* create */);
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700309
Santos Cordon12a03aa2013-09-12 23:34:05 -0700310 if (call == null || !shouldUpdate) {
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700311 continue;
312 }
Santos Cordona3d05142013-07-29 11:25:17 -0700313
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700314 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordon2b73bd62013-08-27 14:53:43 -0700315
Santos Cordone38b1ff2013-08-07 12:12:16 -0700316 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700317 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700318 }
319 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700320
321 // We do a second loop to address conference call scenarios. We do this as a separate
322 // loop to ensure all child calls are up to date before we start updating the parent
323 // conference calls.
324 for (Connection connection : telephonyCall.getConnections()) {
325 updateForConferenceCalls(connection, out);
326 }
327
Santos Cordona3d05142013-07-29 11:25:17 -0700328 }
Santos Cordona3d05142013-07-29 11:25:17 -0700329 }
330
Santos Cordone38b1ff2013-08-07 12:12:16 -0700331 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700332 * Checks to see if the connection is the first connection in a conference call.
333 * If it is a conference call, we will create a new Conference Call object or
334 * update the existing conference call object for that connection.
335 * If it is not a conference call but a previous associated conference call still exists,
336 * we mark it as idle and remove it from the map.
337 * In both cases above, we add the Calls to be updated to the UI.
338 * @param connection The connection object to check.
339 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
340 */
341 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
342 // We consider this connection a conference connection if the call it
Yorke Leecd3f9692013-09-14 13:51:27 -0700343 // belongs to is a multiparty call AND it is the first live connection.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700344 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
Yorke Leecd3f9692013-09-14 13:51:27 -0700345 getEarliestLiveConnection(connection.getCall()) == connection;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700346
347 boolean changed = false;
348
349 // If this connection is the main connection for the conference call, then create or update
350 // a Call object for that conference call.
351 if (isConferenceCallConnection) {
352 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
353 changed = updateCallFromConnection(confCall, connection, true);
354
355 if (changed) {
356 updatedCalls.add(confCall);
357 }
358
359 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
360
361 // It is possible that through a conference call split, there may be lingering conference
362 // calls where this connection was the main connection. We clean those up here.
363 } else {
364 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
365
366 // We found a conference call for this connection, which is no longer a conference call.
367 // Kill it!
368 if (oldConfCall != null) {
369 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
370 mConfCallMap.remove(connection);
371 oldConfCall.setState(State.IDLE);
372 changed = true;
373
374 // add to the list of calls to update
375 updatedCalls.add(oldConfCall);
376 }
377 }
378
379 return changed;
380 }
381
Yorke Leecd3f9692013-09-14 13:51:27 -0700382 private Connection getEarliestLiveConnection(com.android.internal.telephony.Call call) {
383 final List<Connection> connections = call.getConnections();
384 final int size = connections.size();
385 Connection earliestConn = null;
386 long earliestTime = Long.MAX_VALUE;
387 for (int i = 0; i < size; i++) {
388 final Connection connection = connections.get(i);
389 if (!connection.isAlive()) continue;
390 final long time = connection.getCreateTime();
391 if (time < earliestTime) {
392 earliestTime = time;
393 earliestConn = connection;
394 }
395 }
396 return earliestConn;
397 }
398
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700399 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700400 * Sets the new call state onto the call and performs some additional logic
401 * associated with setting the state.
402 */
403 private void setNewState(Call call, int newState, Connection connection) {
404 Preconditions.checkState(call.getState() != newState);
405
406 // When starting an outgoing call, we need to grab gateway information
407 // for the call, if available, and set it.
408 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
409
410 if (newState == Call.State.DIALING) {
411 if (!info.isEmpty()) {
412 call.setGatewayNumber(info.getFormattedGatewayNumber());
413 call.setGatewayPackage(info.packageName);
414 }
415 } else if (!Call.State.isConnected(newState)) {
416 mCallGatewayManager.clearGatewayData(connection);
417 }
418
419 call.setState(newState);
420 }
421
422 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700423 * Updates the Call properties to match the state of the connection object
424 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700425 * @param call The call object to update.
426 * @param connection The connection object from which to update call.
427 * @param isForConference There are slight differences in how we populate data for conference
428 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700429 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700430 private boolean updateCallFromConnection(Call call, Connection connection,
431 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700432 boolean changed = false;
433
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700434 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700435
436 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700437 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700438 changed = true;
439 }
440
Santos Cordone38b1ff2013-08-07 12:12:16 -0700441 final Call.DisconnectCause newDisconnectCause =
442 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
443 if (call.getDisconnectCause() != newDisconnectCause) {
444 call.setDisconnectCause(newDisconnectCause);
445 changed = true;
446 }
447
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700448 final long oldConnectTime = call.getConnectTime();
449 if (oldConnectTime != connection.getConnectTime()) {
450 call.setConnectTime(connection.getConnectTime());
451 changed = true;
452 }
453
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700454 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700455 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700456 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700457 String newNumber = connection.getAddress();
458 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
459 if (!info.isEmpty()) {
460 newNumber = info.trueNumber;
461 }
462 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
463 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700464 changed = true;
465 }
466
Santos Cordon69a69192013-08-22 14:25:42 -0700467 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700468 final int newNumberPresentation = connection.getNumberPresentation();
469 if (call.getNumberPresentation() != newNumberPresentation) {
470 call.setNumberPresentation(newNumberPresentation);
471 changed = true;
472 }
473
Santos Cordon69a69192013-08-22 14:25:42 -0700474 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700475 final String oldCnapName = call.getCnapName();
476 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
477 call.setCnapName(connection.getCnapName());
478 changed = true;
479 }
Santos Cordon69a69192013-08-22 14:25:42 -0700480
481 // Name Presentation
482 final int newCnapNamePresentation = connection.getCnapNamePresentation();
483 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
484 call.setCnapNamePresentation(newCnapNamePresentation);
485 changed = true;
486 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700487 } else {
488
489 // update the list of children by:
490 // 1) Saving the old set
491 // 2) Removing all children
492 // 3) Adding the correct children into the Call
493 // 4) Comparing the new children set with the old children set
494 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
495 call.removeAllChildren();
496
497 if (connection.getCall() != null) {
498 for (Connection childConn : connection.getCall().getConnections()) {
499 final Call childCall = getCallFromMap(mCallMap, childConn, false);
500 if (childCall != null && childConn.isAlive()) {
501 call.addChildId(childCall.getCallId());
502 }
503 }
504 }
Christine Chen45277022013-09-05 10:55:37 -0700505 changed |= !oldSet.equals(call.getChildCallIds());
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700506 }
507
Santos Cordoneead6ec2013-08-07 22:16:33 -0700508 /**
509 * !!! Uses values from connection and call collected above so this part must be last !!!
510 */
511 final int newCapabilities = getCapabilitiesFor(connection, call);
Santos Cordon26e7b242013-08-07 21:15:45 -0700512 if (call.getCapabilities() != newCapabilities) {
513 call.setCapabilities(newCapabilities);
514 changed = true;
515 }
516
Santos Cordone38b1ff2013-08-07 12:12:16 -0700517 return changed;
518 }
519
Santos Cordon26e7b242013-08-07 21:15:45 -0700520 /**
521 * Returns a mask of capabilities for the connection such as merge, hold, etc.
522 */
Santos Cordoneead6ec2013-08-07 22:16:33 -0700523 private int getCapabilitiesFor(Connection connection, Call call) {
524 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
525 final Phone phone = connection.getCall().getPhone();
526
Santos Cordoneead6ec2013-08-07 22:16:33 -0700527 boolean canAddCall = false;
528 boolean canMergeCall = false;
529 boolean canSwapCall = false;
Yorke Lee814da302013-08-30 16:01:07 -0700530 boolean canRespondViaText = false;
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700531 boolean canMute = false;
532
533 final boolean supportHold = PhoneUtils.okToSupportHold(mCallManager);
534 final boolean canHold = PhoneUtils.okToHoldCall(mCallManager);
Santos Cordoneead6ec2013-08-07 22:16:33 -0700535
536 // only applies to active calls
537 if (callIsActive) {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700538 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
539 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
540 }
541
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700542 canAddCall = PhoneUtils.okToAddCall(mCallManager);
543
544 // "Mute": only enabled when the foreground call is ACTIVE.
545 // (It's meaningless while on hold, or while DIALING/ALERTING.)
546 // It's also explicitly disabled during emergency calls or if
547 // emergency callback mode (ECM) is active.
548 boolean isEmergencyCall = false;
549 if (connection != null) {
550 isEmergencyCall = PhoneNumberUtils.isLocalEmergencyNumber(connection.getAddress(),
551 phone.getContext());
552 }
553 boolean isECM = PhoneUtils.isPhoneInEcm(phone);
554 if (isEmergencyCall || isECM) { // disable "Mute" item
555 canMute = false;
556 } else {
557 canMute = callIsActive;
558 }
559
Yorke Lee814da302013-08-30 16:01:07 -0700560 canRespondViaText = RejectWithTextMessageManager.allowRespondViaSmsForCall(call,
561 connection);
562
Santos Cordoneead6ec2013-08-07 22:16:33 -0700563 // special rules section!
564 // CDMA always has Add
565 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
566 canAddCall = true;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700567 }
568
Santos Cordon26e7b242013-08-07 21:15:45 -0700569 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700570 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700571 retval |= Capabilities.HOLD;
572 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700573 if (supportHold) {
574 retval |= Capabilities.SUPPORT_HOLD;
575 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700576 if (canAddCall) {
577 retval |= Capabilities.ADD_CALL;
578 }
579 if (canMergeCall) {
580 retval |= Capabilities.MERGE_CALLS;
581 }
582 if (canSwapCall) {
583 retval |= Capabilities.SWAP_CALLS;
584 }
Yorke Lee814da302013-08-30 16:01:07 -0700585 if (canRespondViaText) {
586 retval |= Capabilities.RESPOND_VIA_TEXT;
587 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700588 if (canMute) {
589 retval |= Capabilities.MUTE;
590 }
Yorke Lee814da302013-08-30 16:01:07 -0700591
Santos Cordon26e7b242013-08-07 21:15:45 -0700592 return retval;
593 }
594
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700595 /**
596 * Returns true if the Connection is part of a multiparty call.
597 * We do this by checking the isMultiparty() method of the telephony.Call object and also
598 * checking to see if more than one of it's children is alive.
599 */
600 private boolean isPartOfLiveConferenceCall(Connection connection) {
601 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
602 int count = 0;
603 for (Connection currConn : connection.getCall().getConnections()) {
604 if (currConn.isAlive()) {
605 count++;
606 if (count >= 2) {
607 return true;
608 }
609 }
610 }
611 }
612 return false;
613 }
614
615 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
616
Santos Cordona3d05142013-07-29 11:25:17 -0700617 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700618 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700619 case ACTIVE:
620 retval = State.ACTIVE;
621 break;
622 case INCOMING:
623 retval = State.INCOMING;
624 break;
625 case DIALING:
626 case ALERTING:
627 retval = State.DIALING;
628 break;
629 case WAITING:
630 retval = State.CALL_WAITING;
631 break;
632 case HOLDING:
633 retval = State.ONHOLD;
634 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700635 case DISCONNECTED:
636 case DISCONNECTING:
637 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700638 default:
639 }
640
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700641 // If we are dealing with a potential child call (not the parent conference call),
642 // the check to see if we have to set the state to CONFERENCED.
643 if (!isForConference) {
644
645 // if the connection is part of a multiparty call, and it is live,
646 // annotate it with CONFERENCED state instead.
647 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
648 return State.CONFERENCED;
649 }
650 }
651
Santos Cordona3d05142013-07-29 11:25:17 -0700652 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700653 }
654
Santos Cordone38b1ff2013-08-07 12:12:16 -0700655 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
656 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
657 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
658 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
659 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
660 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
661 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
662 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
663 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
664 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
665 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
666 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
667 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
668 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
669 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
670 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
671 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
672 Call.DisconnectCause.CDMA_RETRY_ORDER)
673 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
674 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
675 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
676 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
677 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
678 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
679 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
680 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
681 Call.DisconnectCause.ERROR_UNSPECIFIED)
682 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
683 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
684 .put(Connection.DisconnectCause.INCOMING_MISSED,
685 Call.DisconnectCause.INCOMING_MISSED)
686 .put(Connection.DisconnectCause.INCOMING_REJECTED,
687 Call.DisconnectCause.INCOMING_REJECTED)
688 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
689 Call.DisconnectCause.INVALID_CREDENTIALS)
690 .put(Connection.DisconnectCause.INVALID_NUMBER,
691 Call.DisconnectCause.INVALID_NUMBER)
692 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
693 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
694 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
695 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
696 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
697 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
698 Call.DisconnectCause.NOT_DISCONNECTED)
699 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
700 Call.DisconnectCause.NUMBER_UNREACHABLE)
701 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
702 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
703 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
704 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
705 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
706 Call.DisconnectCause.SERVER_UNREACHABLE)
707 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
708 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
709 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
710 .build();
711
712 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
713 Connection.DisconnectCause causeSource) {
714
715 if (CAUSE_MAP.containsKey(causeSource)) {
716 return CAUSE_MAP.get(causeSource);
717 }
718
719 return Call.DisconnectCause.UNKNOWN;
720 }
721
Santos Cordon63a84242013-07-23 13:32:52 -0700722 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700723 * Gets an existing callId for a connection, or creates one if none exists.
724 * This function does NOT set any of the Connection data onto the Call class.
725 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700726 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700727 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
728 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700729 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700730
731 // Find the call id or create if missing and requested.
732 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700733 if (map.containsKey(conn)) {
734 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700735 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700736 call = createNewCall();
737 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700738 }
739 }
Santos Cordon995c8162013-07-29 09:22:22 -0700740 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700741 }
742
743 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700744 * Creates a brand new connection for the call.
745 */
746 private Call createNewCall() {
747 int callId;
748 int newNextCallId;
749 do {
750 callId = mNextCallId.get();
751
752 // protect against overflow
753 newNextCallId = (callId == Integer.MAX_VALUE ?
754 CALL_ID_START_VALUE : callId + 1);
755
756 // Keep looping if the change was not atomic OR the value is already taken.
757 // The call to containsValue() is linear, however, most devices support a
758 // maximum of 7 connections so it's not expensive.
759 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
760
761 return new Call(callId);
762 }
763
764 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700765 * Listener interface for changes to Calls.
766 */
767 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700768 void onDisconnect(Call call);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700769 void onIncoming(Call call);
770 void onUpdate(List<Call> calls);
Chiao Cheng3f015c92013-09-06 15:56:27 -0700771 void onPostDialWait(int callId, String remainingChars);
Santos Cordon63a84242013-07-23 13:32:52 -0700772 }
Santos Cordon249efd02013-08-05 03:33:56 -0700773
774 /**
775 * Result class for accessing a call by connection.
776 */
777 public static class CallResult {
778 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700779 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700780 public Connection mConnection;
781
782 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700783 this(call, call, connection);
784 }
785
786 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700787 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700788 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700789 mConnection = connection;
790 }
791
792 public Call getCall() {
793 return mCall;
794 }
795
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700796 // The call that should be used for call actions like hanging up.
797 public Call getActionableCall() {
798 return mActionableCall;
799 }
800
Santos Cordon249efd02013-08-05 03:33:56 -0700801 public Connection getConnection() {
802 return mConnection;
803 }
804 }
Santos Cordon63a84242013-07-23 13:32:52 -0700805}