blob: 8e99b3878de02b465e7e1dfdefdfded66531728e [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 Cordona5d5db82013-09-15 13:00:34 -070031import com.android.internal.telephony.TelephonyCapabilities;
32import com.android.internal.telephony.cdma.CdmaCallWaitingNotification;
Santos Cordon69a69192013-08-22 14:25:42 -070033import com.android.phone.CallGatewayManager.RawGatewayInfo;
Santos Cordon995c8162013-07-29 09:22:22 -070034import com.android.services.telephony.common.Call;
Santos Cordon26e7b242013-08-07 21:15:45 -070035import com.android.services.telephony.common.Call.Capabilities;
Santos Cordona3d05142013-07-29 11:25:17 -070036import com.android.services.telephony.common.Call.State;
Santos Cordon63a84242013-07-23 13:32:52 -070037
Yorke Lee814da302013-08-30 16:01:07 -070038import com.google.android.collect.Maps;
39import com.google.common.base.Preconditions;
40import com.google.common.collect.ImmutableMap;
41import com.google.common.collect.ImmutableSortedSet;
Santos Cordonad1ed6d2013-09-16 03:04:23 -070042import com.google.common.collect.Lists;
Yorke Lee814da302013-08-30 16:01:07 -070043
Santos Cordon63a84242013-07-23 13:32:52 -070044import java.util.ArrayList;
Santos Cordonad1ed6d2013-09-16 03:04:23 -070045import java.util.Collections;
Santos Cordon63a84242013-07-23 13:32:52 -070046import java.util.HashMap;
47import java.util.List;
Santos Cordon249efd02013-08-05 03:33:56 -070048import java.util.Map.Entry;
Santos Cordon63a84242013-07-23 13:32:52 -070049import java.util.concurrent.atomic.AtomicInteger;
50
51/**
52 * Creates a Call model from Call state and data received from the telephony
53 * layer. The telephony layer maintains 3 conceptual objects: Phone, Call,
54 * Connection.
55 *
56 * Phone represents the radio and there is an implementation per technology
57 * type such as GSMPhone, SipPhone, CDMAPhone, etc. Generally, we will only ever
58 * deal with one instance of this object for the lifetime of this class.
59 *
60 * There are 3 Call instances that exist for the lifetime of this class which
61 * are created by CallTracker. The three are RingingCall, ForegroundCall, and
62 * BackgroundCall.
63 *
64 * A Connection most closely resembles what the layperson would consider a call.
65 * A Connection is created when a user dials and it is "owned" by one of the
66 * three Call instances. Which of the three Calls owns the Connection changes
67 * as the Connection goes between ACTIVE, HOLD, RINGING, and other states.
68 *
69 * This class models a new Call class from Connection objects received from
70 * the telephony layer. We use Connection references as identifiers for a call;
71 * new reference = new call.
72 *
Christine Chen91db67d2013-09-18 12:01:11 -070073 * TODO: Create a new Call class to replace the simple call Id ints
Santos Cordon63a84242013-07-23 13:32:52 -070074 * being used currently.
75 *
76 * The new Call models are parcellable for transfer via the CallHandlerService
77 * API.
78 */
79public class CallModeler extends Handler {
80
81 private static final String TAG = CallModeler.class.getSimpleName();
Santos Cordon4ad64cd2013-08-15 00:36:14 -070082 private static final boolean DBG =
83 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Santos Cordon63a84242013-07-23 13:32:52 -070084
85 private static final int CALL_ID_START_VALUE = 1;
Santos Cordon63a84242013-07-23 13:32:52 -070086
Santos Cordon998f42b2013-08-02 16:13:12 -070087 private final CallStateMonitor mCallStateMonitor;
88 private final CallManager mCallManager;
Santos Cordon69a69192013-08-22 14:25:42 -070089 private final CallGatewayManager mCallGatewayManager;
Santos Cordon998f42b2013-08-02 16:13:12 -070090 private final HashMap<Connection, Call> mCallMap = Maps.newHashMap();
Santos Cordon4ad64cd2013-08-15 00:36:14 -070091 private final HashMap<Connection, Call> mConfCallMap = Maps.newHashMap();
Santos Cordon998f42b2013-08-02 16:13:12 -070092 private final AtomicInteger mNextCallId = new AtomicInteger(CALL_ID_START_VALUE);
Christine Chendaf7bf62013-08-05 19:12:31 -070093 private final ArrayList<Listener> mListeners = new ArrayList<Listener>();
Christine Chenee09a492013-08-06 16:02:29 -070094 private RejectWithTextMessageManager mRejectWithTextMessageManager;
Santos Cordona5d5db82013-09-15 13:00:34 -070095 private Connection mCdmaIncomingConnection;
Santos Cordonad1ed6d2013-09-16 03:04:23 -070096 private Connection mCdmaOutgoingConnection;
Santos Cordon63a84242013-07-23 13:32:52 -070097
Christine Chenee09a492013-08-06 16:02:29 -070098 public CallModeler(CallStateMonitor callStateMonitor, CallManager callManager,
Santos Cordon69a69192013-08-22 14:25:42 -070099 RejectWithTextMessageManager rejectWithTextMessageManager,
100 CallGatewayManager callGatewayManager) {
Santos Cordon63a84242013-07-23 13:32:52 -0700101 mCallStateMonitor = callStateMonitor;
Santos Cordona3d05142013-07-29 11:25:17 -0700102 mCallManager = callManager;
Christine Chenee09a492013-08-06 16:02:29 -0700103 mRejectWithTextMessageManager = rejectWithTextMessageManager;
Santos Cordon69a69192013-08-22 14:25:42 -0700104 mCallGatewayManager = callGatewayManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700105
106 mCallStateMonitor.addListener(this);
107 }
108
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700109 @Override
Santos Cordon63a84242013-07-23 13:32:52 -0700110 public void handleMessage(Message msg) {
111 switch(msg.what) {
112 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700113 // We let the CallNotifier handle the new ringing connection first. When the custom
114 // ringtone and send_to_voicemail settings are retrieved, CallNotifier will directly
115 // call CallModeler's onNewRingingConnection.
Santos Cordon63a84242013-07-23 13:32:52 -0700116 break;
117 case CallStateMonitor.PHONE_DISCONNECT:
Santos Cordona5d5db82013-09-15 13:00:34 -0700118 onDisconnect((Connection) ((AsyncResult) msg.obj).result);
Santos Cordon995c8162013-07-29 09:22:22 -0700119 break;
120 case CallStateMonitor.PHONE_STATE_CHANGED:
121 onPhoneStateChanged((AsyncResult) msg.obj);
122 break;
Chiao Cheng3f015c92013-09-06 15:56:27 -0700123 case CallStateMonitor.PHONE_ON_DIAL_CHARS:
124 onPostDialChars((AsyncResult) msg.obj, (char) msg.arg1);
125 break;
Santos Cordon63a84242013-07-23 13:32:52 -0700126 default:
127 break;
128 }
129 }
130
Christine Chendaf7bf62013-08-05 19:12:31 -0700131 public void addListener(Listener listener) {
Santos Cordon63a84242013-07-23 13:32:52 -0700132 Preconditions.checkNotNull(listener);
Christine Chendaf7bf62013-08-05 19:12:31 -0700133 Preconditions.checkNotNull(mListeners);
Christine Chen4748abd2013-08-07 15:44:15 -0700134 if (!mListeners.contains(listener)) {
135 mListeners.add(listener);
136 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700137 }
138
139 public List<Call> getFullList() {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700140 final List<Call> calls =
141 Lists.newArrayListWithCapacity(mCallMap.size() + mConfCallMap.size());
142 calls.addAll(mCallMap.values());
143 calls.addAll(mConfCallMap.values());
144 return calls;
Santos Cordon63a84242013-07-23 13:32:52 -0700145 }
146
Santos Cordon249efd02013-08-05 03:33:56 -0700147 public CallResult getCallWithId(int callId) {
148 // max 8 connections, so this should be fast even through we are traversing the entire map.
149 for (Entry<Connection, Call> entry : mCallMap.entrySet()) {
150 if (entry.getValue().getCallId() == callId) {
151 return new CallResult(entry.getValue(), entry.getKey());
152 }
153 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700154
155 for (Entry<Connection, Call> entry : mConfCallMap.entrySet()) {
156 if (entry.getValue().getCallId() == callId) {
Christine Chen69050202013-09-14 15:36:35 -0700157 return new CallResult(entry.getValue(), entry.getKey());
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700158 }
159 }
Santos Cordon249efd02013-08-05 03:33:56 -0700160 return null;
161 }
162
Santos Cordonaf763a12013-08-19 20:04:58 -0700163 public boolean hasLiveCall() {
164 return hasLiveCallInternal(mCallMap) ||
165 hasLiveCallInternal(mConfCallMap);
166 }
167
Santos Cordona5d5db82013-09-15 13:00:34 -0700168 public void onCdmaCallWaiting(CdmaCallWaitingNotification callWaitingInfo) {
169 // We dont get the traditional onIncomingCall notification for cdma call waiting,
170 // but the Connection does actually exist. We need to find it in the set of ringing calls
171 // and pass it through our normal incoming logic.
172 final com.android.internal.telephony.Call teleCall =
173 mCallManager.getFirstActiveRingingCall();
174
175 if (teleCall.getState() == com.android.internal.telephony.Call.State.WAITING) {
176 Connection connection = teleCall.getLatestConnection();
177
178 if (connection != null) {
179 String number = connection.getAddress();
180 if (number != null && number.equals(callWaitingInfo.number)) {
181 Call call = onNewRingingConnection(connection);
182 mCdmaIncomingConnection = connection;
183 return;
184 }
185 }
186 }
187
188 Log.e(TAG, "CDMA Call waiting notification without a matching connection.");
189 }
190
191 public void onCdmaCallWaitingReject() {
192 // Cdma call was rejected...
193 if (mCdmaIncomingConnection != null) {
194 onDisconnect(mCdmaIncomingConnection);
195 mCdmaIncomingConnection = null;
196 } else {
197 Log.e(TAG, "CDMA Call waiting rejection without an incoming call.");
198 }
199 }
200
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700201 /**
202 * CDMA Calls have no sense of "dialing" state. For outgoing calls 3way calls we want to
203 * mimick this state so that the the UI can notify the user that there is a "dialing"
204 * call.
205 */
206 public void setCdmaOutgoing3WayCall(Connection connection) {
207 boolean wasSet = mCdmaOutgoingConnection != null;
208
209 mCdmaOutgoingConnection = connection;
210
211 // If we reset the connection, that mean we can now tell the user that the call is actually
212 // part of the conference call and move it out of the dialing state. To do this, issue a
213 // new update completely.
214 if (wasSet && mCdmaOutgoingConnection == null) {
215 onPhoneStateChanged(null);
216 }
217 }
218
Santos Cordonaf763a12013-08-19 20:04:58 -0700219 private boolean hasLiveCallInternal(HashMap<Connection, Call> map) {
220 for (Call call : map.values()) {
221 final int state = call.getState();
222 if (state == Call.State.ACTIVE ||
223 state == Call.State.CALL_WAITING ||
224 state == Call.State.CONFERENCED ||
225 state == Call.State.DIALING ||
226 state == Call.State.INCOMING ||
Christine Chen3e0f0412013-09-18 20:33:49 -0700227 state == Call.State.ONHOLD ||
228 state == Call.State.DISCONNECTING) {
Santos Cordonaf763a12013-08-19 20:04:58 -0700229 return true;
230 }
231 }
232 return false;
233 }
234
Santos Cordon2b73bd62013-08-27 14:53:43 -0700235 public boolean hasOutstandingActiveOrDialingCall() {
236 return hasOutstandingActiveOrDialingCallInternal(mCallMap) ||
237 hasOutstandingActiveOrDialingCallInternal(mConfCallMap);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700238 }
239
Santos Cordon2b73bd62013-08-27 14:53:43 -0700240 private static boolean hasOutstandingActiveOrDialingCallInternal(
241 HashMap<Connection, Call> map) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700242 for (Call call : map.values()) {
243 final int state = call.getState();
Santos Cordon2b73bd62013-08-27 14:53:43 -0700244 if (state == Call.State.ACTIVE ||
245 state == Call.State.DIALING) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700246 return true;
247 }
248 }
249
250 return false;
251 }
252
Chiao Cheng3f015c92013-09-06 15:56:27 -0700253
254 /**
255 * Handles the POST_ON_DIAL_CHARS message from the Phone (see our call to
256 * mPhone.setOnPostDialCharacter() above.)
257 *
258 * TODO: NEED TO TEST THIS SEQUENCE now that we no longer handle "dialable" key events here in
259 * the InCallScreen: we do directly to the Dialer UI instead. Similarly, we may now need to go
260 * directly to the Dialer to handle POST_ON_DIAL_CHARS too.
261 */
262 private void onPostDialChars(AsyncResult r, char ch) {
263 final Connection c = (Connection) r.result;
264
265 if (c != null) {
266 final Connection.PostDialState state = (Connection.PostDialState) r.userObj;
267
268 switch (state) {
269 // TODO(klp): add other post dial related functions
270 case WAIT:
271 final Call call = getCallFromMap(mCallMap, c, false);
272 if (call == null) {
273 Log.i(TAG, "Call no longer exists. Skipping onPostDialWait().");
274 } else {
275 for (Listener mListener : mListeners) {
276 mListener.onPostDialWait(call.getCallId(),
277 c.getRemainingPostDialString());
278 }
279 }
280 break;
281
282 default:
283 break;
284 }
285 }
286 }
287
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700288 /* package */ Call onNewRingingConnection(Connection conn) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700289 Log.i(TAG, "onNewRingingConnection");
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700290 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700291
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700292 if (call != null) {
293 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700294
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700295 for (int i = 0; i < mListeners.size(); ++i) {
296 mListeners.get(i).onIncoming(call);
Christine Chenee09a492013-08-06 16:02:29 -0700297 }
Santos Cordon63a84242013-07-23 13:32:52 -0700298 }
Santos Cordona5d5db82013-09-15 13:00:34 -0700299
300 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700301 }
302
Santos Cordona5d5db82013-09-15 13:00:34 -0700303 private void onDisconnect(Connection conn) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700304 Log.i(TAG, "onDisconnect");
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700305 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700306
Santos Cordon995c8162013-07-29 09:22:22 -0700307 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700308 final boolean wasConferenced = call.getState() == State.CONFERENCED;
309
310 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700311
Christine Chendaf7bf62013-08-05 19:12:31 -0700312 for (int i = 0; i < mListeners.size(); ++i) {
313 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700314 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700315
316 // If it was a conferenced call, we need to run the entire update
317 // to make the proper changes to parent conference calls.
318 if (wasConferenced) {
319 onPhoneStateChanged(null);
320 }
321
322 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700323 }
324 }
325
Santos Cordona3d05142013-07-29 11:25:17 -0700326 /**
327 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700328 */
Santos Cordon995c8162013-07-29 09:22:22 -0700329 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700330 Log.i(TAG, "onPhoneStateChanged: ");
Santos Cordon998f42b2013-08-02 16:13:12 -0700331 final List<Call> updatedCalls = Lists.newArrayList();
332 doUpdate(false, updatedCalls);
333
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700334 if (updatedCalls.size() > 0) {
335 for (int i = 0; i < mListeners.size(); ++i) {
336 mListeners.get(i).onUpdate(updatedCalls);
337 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700338 }
339 }
340
341
342 /**
343 * Go through the Calls from CallManager and return the list of calls that were updated.
344 * Or, the full list if requested.
345 */
346 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700347 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
348 telephonyCalls.addAll(mCallManager.getRingingCalls());
349 telephonyCalls.addAll(mCallManager.getForegroundCalls());
350 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
351
Santos Cordona3d05142013-07-29 11:25:17 -0700352 // Cycle through all the Connections on all the Calls. Update our Call objects
353 // to reflect any new state and send the updated Call objects to the handler service.
354 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700355
356 for (Connection connection : telephonyCall.getConnections()) {
Christine Chen3e0f0412013-09-18 20:33:49 -0700357 if (DBG) Log.d(TAG, "connection: " + connection + connection.getState());
Santos Cordona5d5db82013-09-15 13:00:34 -0700358
Santos Cordon12a03aa2013-09-12 23:34:05 -0700359 // We only send updates for live calls which are not incoming (ringing).
360 // Disconnected and incoming calls are handled by onDisconnect and
361 // onNewRingingConnection.
Christine Chen3e0f0412013-09-18 20:33:49 -0700362 boolean shouldUpdate =
363 connection.getState() !=
364 com.android.internal.telephony.Call.State.DISCONNECTED &&
365 connection.getState() !=
366 com.android.internal.telephony.Call.State.IDLE &&
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700367 !connection.getState().isRinging();
368
369 // New connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700370 // a state in the internal.telephony.Call object. This ensures that staleness
371 // check below fails and we always add the item to the update list if it is new.
Santos Cordon12a03aa2013-09-12 23:34:05 -0700372 final Call call = getCallFromMap(mCallMap, connection, shouldUpdate /* create */);
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700373
Santos Cordon12a03aa2013-09-12 23:34:05 -0700374 if (call == null || !shouldUpdate) {
Santos Cordona5d5db82013-09-15 13:00:34 -0700375 if (DBG) Log.d(TAG, "update skipped");
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700376 continue;
377 }
Santos Cordona3d05142013-07-29 11:25:17 -0700378
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700379 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordon2b73bd62013-08-27 14:53:43 -0700380
Santos Cordone38b1ff2013-08-07 12:12:16 -0700381 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700382 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700383 }
384 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700385
386 // We do a second loop to address conference call scenarios. We do this as a separate
387 // loop to ensure all child calls are up to date before we start updating the parent
388 // conference calls.
389 for (Connection connection : telephonyCall.getConnections()) {
390 updateForConferenceCalls(connection, out);
391 }
392
Santos Cordona3d05142013-07-29 11:25:17 -0700393 }
Santos Cordona3d05142013-07-29 11:25:17 -0700394 }
395
Santos Cordone38b1ff2013-08-07 12:12:16 -0700396 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700397 * Checks to see if the connection is the first connection in a conference call.
398 * If it is a conference call, we will create a new Conference Call object or
399 * update the existing conference call object for that connection.
400 * If it is not a conference call but a previous associated conference call still exists,
401 * we mark it as idle and remove it from the map.
402 * In both cases above, we add the Calls to be updated to the UI.
403 * @param connection The connection object to check.
404 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
405 */
406 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
407 // We consider this connection a conference connection if the call it
Yorke Leecd3f9692013-09-14 13:51:27 -0700408 // belongs to is a multiparty call AND it is the first live connection.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700409 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
Yorke Leecd3f9692013-09-14 13:51:27 -0700410 getEarliestLiveConnection(connection.getCall()) == connection;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700411
412 boolean changed = false;
413
414 // If this connection is the main connection for the conference call, then create or update
415 // a Call object for that conference call.
416 if (isConferenceCallConnection) {
417 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
418 changed = updateCallFromConnection(confCall, connection, true);
419
420 if (changed) {
421 updatedCalls.add(confCall);
422 }
423
424 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
425
426 // It is possible that through a conference call split, there may be lingering conference
427 // calls where this connection was the main connection. We clean those up here.
428 } else {
429 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
430
431 // We found a conference call for this connection, which is no longer a conference call.
432 // Kill it!
433 if (oldConfCall != null) {
434 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
435 mConfCallMap.remove(connection);
436 oldConfCall.setState(State.IDLE);
437 changed = true;
438
439 // add to the list of calls to update
440 updatedCalls.add(oldConfCall);
441 }
442 }
443
444 return changed;
445 }
446
Yorke Leecd3f9692013-09-14 13:51:27 -0700447 private Connection getEarliestLiveConnection(com.android.internal.telephony.Call call) {
448 final List<Connection> connections = call.getConnections();
449 final int size = connections.size();
450 Connection earliestConn = null;
451 long earliestTime = Long.MAX_VALUE;
452 for (int i = 0; i < size; i++) {
453 final Connection connection = connections.get(i);
454 if (!connection.isAlive()) continue;
455 final long time = connection.getCreateTime();
456 if (time < earliestTime) {
457 earliestTime = time;
458 earliestConn = connection;
459 }
460 }
461 return earliestConn;
462 }
463
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700464 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700465 * Sets the new call state onto the call and performs some additional logic
466 * associated with setting the state.
467 */
468 private void setNewState(Call call, int newState, Connection connection) {
469 Preconditions.checkState(call.getState() != newState);
470
471 // When starting an outgoing call, we need to grab gateway information
472 // for the call, if available, and set it.
473 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
474
475 if (newState == Call.State.DIALING) {
476 if (!info.isEmpty()) {
477 call.setGatewayNumber(info.getFormattedGatewayNumber());
478 call.setGatewayPackage(info.packageName);
479 }
480 } else if (!Call.State.isConnected(newState)) {
481 mCallGatewayManager.clearGatewayData(connection);
482 }
483
484 call.setState(newState);
485 }
486
487 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700488 * Updates the Call properties to match the state of the connection object
489 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700490 * @param call The call object to update.
491 * @param connection The connection object from which to update call.
492 * @param isForConference There are slight differences in how we populate data for conference
493 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700494 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700495 private boolean updateCallFromConnection(Call call, Connection connection,
496 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700497 boolean changed = false;
498
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700499 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700500
501 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700502 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700503 changed = true;
504 }
505
Santos Cordone38b1ff2013-08-07 12:12:16 -0700506 final Call.DisconnectCause newDisconnectCause =
507 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
508 if (call.getDisconnectCause() != newDisconnectCause) {
509 call.setDisconnectCause(newDisconnectCause);
510 changed = true;
511 }
512
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700513 final long oldConnectTime = call.getConnectTime();
514 if (oldConnectTime != connection.getConnectTime()) {
515 call.setConnectTime(connection.getConnectTime());
516 changed = true;
517 }
518
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700519 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700520 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700521 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700522 String newNumber = connection.getAddress();
523 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
524 if (!info.isEmpty()) {
525 newNumber = info.trueNumber;
526 }
527 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
528 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700529 changed = true;
530 }
531
Santos Cordon69a69192013-08-22 14:25:42 -0700532 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700533 final int newNumberPresentation = connection.getNumberPresentation();
534 if (call.getNumberPresentation() != newNumberPresentation) {
535 call.setNumberPresentation(newNumberPresentation);
536 changed = true;
537 }
538
Santos Cordon69a69192013-08-22 14:25:42 -0700539 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700540 final String oldCnapName = call.getCnapName();
541 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
542 call.setCnapName(connection.getCnapName());
543 changed = true;
544 }
Santos Cordon69a69192013-08-22 14:25:42 -0700545
546 // Name Presentation
547 final int newCnapNamePresentation = connection.getCnapNamePresentation();
548 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
549 call.setCnapNamePresentation(newCnapNamePresentation);
550 changed = true;
551 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700552 } else {
553
554 // update the list of children by:
555 // 1) Saving the old set
556 // 2) Removing all children
557 // 3) Adding the correct children into the Call
558 // 4) Comparing the new children set with the old children set
559 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
560 call.removeAllChildren();
561
562 if (connection.getCall() != null) {
563 for (Connection childConn : connection.getCall().getConnections()) {
564 final Call childCall = getCallFromMap(mCallMap, childConn, false);
565 if (childCall != null && childConn.isAlive()) {
566 call.addChildId(childCall.getCallId());
567 }
568 }
569 }
Christine Chen45277022013-09-05 10:55:37 -0700570 changed |= !oldSet.equals(call.getChildCallIds());
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700571 }
572
Santos Cordoneead6ec2013-08-07 22:16:33 -0700573 /**
574 * !!! Uses values from connection and call collected above so this part must be last !!!
575 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700576 final int newCapabilities = getCapabilitiesFor(connection, call, isForConference);
Santos Cordon26e7b242013-08-07 21:15:45 -0700577 if (call.getCapabilities() != newCapabilities) {
578 call.setCapabilities(newCapabilities);
579 changed = true;
580 }
581
Santos Cordone38b1ff2013-08-07 12:12:16 -0700582 return changed;
583 }
584
Santos Cordon26e7b242013-08-07 21:15:45 -0700585 /**
586 * Returns a mask of capabilities for the connection such as merge, hold, etc.
587 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700588 private int getCapabilitiesFor(Connection connection, Call call, boolean isForConference) {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700589 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
590 final Phone phone = connection.getCall().getPhone();
591
Santos Cordoneead6ec2013-08-07 22:16:33 -0700592 boolean canAddCall = false;
593 boolean canMergeCall = false;
594 boolean canSwapCall = false;
Yorke Lee814da302013-08-30 16:01:07 -0700595 boolean canRespondViaText = false;
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700596 boolean canMute = false;
597
598 final boolean supportHold = PhoneUtils.okToSupportHold(mCallManager);
Christine Chen94853bc2013-09-17 16:53:33 -0700599 final boolean canHold = (supportHold ? PhoneUtils.okToHoldCall(mCallManager) : false);
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700600 final boolean genericConf = isForConference &&
601 (connection.getCall().getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA);
Santos Cordoneead6ec2013-08-07 22:16:33 -0700602
603 // only applies to active calls
604 if (callIsActive) {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700605 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
606 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
607 }
608
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700609 canAddCall = PhoneUtils.okToAddCall(mCallManager);
610
611 // "Mute": only enabled when the foreground call is ACTIVE.
612 // (It's meaningless while on hold, or while DIALING/ALERTING.)
613 // It's also explicitly disabled during emergency calls or if
614 // emergency callback mode (ECM) is active.
615 boolean isEmergencyCall = false;
616 if (connection != null) {
617 isEmergencyCall = PhoneNumberUtils.isLocalEmergencyNumber(connection.getAddress(),
618 phone.getContext());
619 }
620 boolean isECM = PhoneUtils.isPhoneInEcm(phone);
621 if (isEmergencyCall || isECM) { // disable "Mute" item
622 canMute = false;
623 } else {
624 canMute = callIsActive;
625 }
626
Yorke Lee814da302013-08-30 16:01:07 -0700627 canRespondViaText = RejectWithTextMessageManager.allowRespondViaSmsForCall(call,
628 connection);
629
Santos Cordoneead6ec2013-08-07 22:16:33 -0700630 // special rules section!
631 // CDMA always has Add
632 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
633 canAddCall = true;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700634 }
635
Santos Cordon26e7b242013-08-07 21:15:45 -0700636 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700637 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700638 retval |= Capabilities.HOLD;
639 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700640 if (supportHold) {
641 retval |= Capabilities.SUPPORT_HOLD;
642 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700643 if (canAddCall) {
644 retval |= Capabilities.ADD_CALL;
645 }
646 if (canMergeCall) {
647 retval |= Capabilities.MERGE_CALLS;
648 }
649 if (canSwapCall) {
650 retval |= Capabilities.SWAP_CALLS;
651 }
Yorke Lee814da302013-08-30 16:01:07 -0700652 if (canRespondViaText) {
653 retval |= Capabilities.RESPOND_VIA_TEXT;
654 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700655 if (canMute) {
656 retval |= Capabilities.MUTE;
657 }
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700658 if (genericConf) {
659 retval |= Capabilities.GENERIC_CONFERENCE;
660 }
Yorke Lee814da302013-08-30 16:01:07 -0700661
Santos Cordon26e7b242013-08-07 21:15:45 -0700662 return retval;
663 }
664
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700665 /**
666 * Returns true if the Connection is part of a multiparty call.
667 * We do this by checking the isMultiparty() method of the telephony.Call object and also
668 * checking to see if more than one of it's children is alive.
669 */
670 private boolean isPartOfLiveConferenceCall(Connection connection) {
671 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
672 int count = 0;
673 for (Connection currConn : connection.getCall().getConnections()) {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700674
675 // Only count connections which are alive and never cound the special
676 // "dialing" 3way call for CDMA calls.
677 if (currConn.isAlive() && currConn != mCdmaOutgoingConnection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700678 count++;
679 if (count >= 2) {
680 return true;
681 }
682 }
683 }
684 }
685 return false;
686 }
687
688 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
689
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700690 // For the "fake" outgoing CDMA call, we need to always treat it as an outgoing call.
691 if (mCdmaOutgoingConnection == connection) {
692 return State.DIALING;
693 }
694
Santos Cordona3d05142013-07-29 11:25:17 -0700695 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700696 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700697 case ACTIVE:
698 retval = State.ACTIVE;
699 break;
700 case INCOMING:
701 retval = State.INCOMING;
702 break;
703 case DIALING:
704 case ALERTING:
705 retval = State.DIALING;
706 break;
707 case WAITING:
708 retval = State.CALL_WAITING;
709 break;
710 case HOLDING:
711 retval = State.ONHOLD;
712 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700713 case DISCONNECTING:
Christine Chen3e0f0412013-09-18 20:33:49 -0700714 retval = State.DISCONNECTING;
715 break;
716 case DISCONNECTED:
Santos Cordone38b1ff2013-08-07 12:12:16 -0700717 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700718 default:
719 }
720
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700721 // If we are dealing with a potential child call (not the parent conference call),
722 // the check to see if we have to set the state to CONFERENCED.
723 if (!isForConference) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700724 // if the connection is part of a multiparty call, and it is live,
725 // annotate it with CONFERENCED state instead.
726 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
727 return State.CONFERENCED;
728 }
729 }
730
Santos Cordona3d05142013-07-29 11:25:17 -0700731 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700732 }
733
Santos Cordone38b1ff2013-08-07 12:12:16 -0700734 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
735 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
736 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
737 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
738 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
739 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
740 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
741 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
742 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
743 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
744 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
745 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
746 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
747 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
748 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
749 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
750 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
751 Call.DisconnectCause.CDMA_RETRY_ORDER)
752 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
753 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
754 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
755 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
756 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
757 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
758 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
759 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
760 Call.DisconnectCause.ERROR_UNSPECIFIED)
761 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
762 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
763 .put(Connection.DisconnectCause.INCOMING_MISSED,
764 Call.DisconnectCause.INCOMING_MISSED)
765 .put(Connection.DisconnectCause.INCOMING_REJECTED,
766 Call.DisconnectCause.INCOMING_REJECTED)
767 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
768 Call.DisconnectCause.INVALID_CREDENTIALS)
769 .put(Connection.DisconnectCause.INVALID_NUMBER,
770 Call.DisconnectCause.INVALID_NUMBER)
771 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
772 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
773 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
774 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
775 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
776 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
777 Call.DisconnectCause.NOT_DISCONNECTED)
778 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
779 Call.DisconnectCause.NUMBER_UNREACHABLE)
780 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
781 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
782 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
783 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
784 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
785 Call.DisconnectCause.SERVER_UNREACHABLE)
786 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
787 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
788 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
789 .build();
790
791 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
792 Connection.DisconnectCause causeSource) {
793
794 if (CAUSE_MAP.containsKey(causeSource)) {
795 return CAUSE_MAP.get(causeSource);
796 }
797
798 return Call.DisconnectCause.UNKNOWN;
799 }
800
Santos Cordon63a84242013-07-23 13:32:52 -0700801 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700802 * Gets an existing callId for a connection, or creates one if none exists.
803 * This function does NOT set any of the Connection data onto the Call class.
804 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700805 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700806 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
807 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700808 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700809
810 // Find the call id or create if missing and requested.
811 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700812 if (map.containsKey(conn)) {
813 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700814 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700815 call = createNewCall();
816 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700817 }
818 }
Santos Cordon995c8162013-07-29 09:22:22 -0700819 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700820 }
821
822 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700823 * Creates a brand new connection for the call.
824 */
825 private Call createNewCall() {
826 int callId;
827 int newNextCallId;
828 do {
829 callId = mNextCallId.get();
830
831 // protect against overflow
832 newNextCallId = (callId == Integer.MAX_VALUE ?
833 CALL_ID_START_VALUE : callId + 1);
834
835 // Keep looping if the change was not atomic OR the value is already taken.
836 // The call to containsValue() is linear, however, most devices support a
837 // maximum of 7 connections so it's not expensive.
838 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
839
840 return new Call(callId);
841 }
842
843 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700844 * Listener interface for changes to Calls.
845 */
846 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700847 void onDisconnect(Call call);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700848 void onIncoming(Call call);
849 void onUpdate(List<Call> calls);
Chiao Cheng3f015c92013-09-06 15:56:27 -0700850 void onPostDialWait(int callId, String remainingChars);
Santos Cordon63a84242013-07-23 13:32:52 -0700851 }
Santos Cordon249efd02013-08-05 03:33:56 -0700852
853 /**
854 * Result class for accessing a call by connection.
855 */
856 public static class CallResult {
857 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700858 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700859 public Connection mConnection;
860
861 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700862 this(call, call, connection);
863 }
864
865 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700866 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700867 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700868 mConnection = connection;
869 }
870
871 public Call getCall() {
872 return mCall;
873 }
874
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700875 // The call that should be used for call actions like hanging up.
876 public Call getActionableCall() {
877 return mActionableCall;
878 }
879
Santos Cordon249efd02013-08-05 03:33:56 -0700880 public Connection getConnection() {
881 return mConnection;
882 }
883 }
Santos Cordon63a84242013-07-23 13:32:52 -0700884}