blob: 271144ed43ba54c1c8f617bf32284d7a2b8df3da [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:
Santos Cordona5d5db82013-09-15 13:00:34 -0700113 onNewRingingConnection((Connection) ((AsyncResult) msg.obj).result);
Santos Cordon63a84242013-07-23 13:32:52 -0700114 break;
115 case CallStateMonitor.PHONE_DISCONNECT:
Santos Cordona5d5db82013-09-15 13:00:34 -0700116 onDisconnect((Connection) ((AsyncResult) msg.obj).result);
Santos Cordon995c8162013-07-29 09:22:22 -0700117 break;
118 case CallStateMonitor.PHONE_STATE_CHANGED:
119 onPhoneStateChanged((AsyncResult) msg.obj);
120 break;
Chiao Cheng3f015c92013-09-06 15:56:27 -0700121 case CallStateMonitor.PHONE_ON_DIAL_CHARS:
122 onPostDialChars((AsyncResult) msg.obj, (char) msg.arg1);
123 break;
Santos Cordon63a84242013-07-23 13:32:52 -0700124 default:
125 break;
126 }
127 }
128
Christine Chendaf7bf62013-08-05 19:12:31 -0700129 public void addListener(Listener listener) {
Santos Cordon63a84242013-07-23 13:32:52 -0700130 Preconditions.checkNotNull(listener);
Christine Chendaf7bf62013-08-05 19:12:31 -0700131 Preconditions.checkNotNull(mListeners);
Christine Chen4748abd2013-08-07 15:44:15 -0700132 if (!mListeners.contains(listener)) {
133 mListeners.add(listener);
134 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700135 }
136
137 public List<Call> getFullList() {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700138 final List<Call> calls =
139 Lists.newArrayListWithCapacity(mCallMap.size() + mConfCallMap.size());
140 calls.addAll(mCallMap.values());
141 calls.addAll(mConfCallMap.values());
142 return calls;
Santos Cordon63a84242013-07-23 13:32:52 -0700143 }
144
Santos Cordon249efd02013-08-05 03:33:56 -0700145 public CallResult getCallWithId(int callId) {
146 // max 8 connections, so this should be fast even through we are traversing the entire map.
147 for (Entry<Connection, Call> entry : mCallMap.entrySet()) {
148 if (entry.getValue().getCallId() == callId) {
149 return new CallResult(entry.getValue(), entry.getKey());
150 }
151 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700152
153 for (Entry<Connection, Call> entry : mConfCallMap.entrySet()) {
154 if (entry.getValue().getCallId() == callId) {
Christine Chen69050202013-09-14 15:36:35 -0700155 return new CallResult(entry.getValue(), entry.getKey());
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700156 }
157 }
Santos Cordon249efd02013-08-05 03:33:56 -0700158 return null;
159 }
160
Santos Cordonaf763a12013-08-19 20:04:58 -0700161 public boolean hasLiveCall() {
162 return hasLiveCallInternal(mCallMap) ||
163 hasLiveCallInternal(mConfCallMap);
164 }
165
Santos Cordona5d5db82013-09-15 13:00:34 -0700166 public void onCdmaCallWaiting(CdmaCallWaitingNotification callWaitingInfo) {
167 // We dont get the traditional onIncomingCall notification for cdma call waiting,
168 // but the Connection does actually exist. We need to find it in the set of ringing calls
169 // and pass it through our normal incoming logic.
170 final com.android.internal.telephony.Call teleCall =
171 mCallManager.getFirstActiveRingingCall();
172
173 if (teleCall.getState() == com.android.internal.telephony.Call.State.WAITING) {
174 Connection connection = teleCall.getLatestConnection();
175
176 if (connection != null) {
177 String number = connection.getAddress();
178 if (number != null && number.equals(callWaitingInfo.number)) {
179 Call call = onNewRingingConnection(connection);
180 mCdmaIncomingConnection = connection;
181 return;
182 }
183 }
184 }
185
186 Log.e(TAG, "CDMA Call waiting notification without a matching connection.");
187 }
188
189 public void onCdmaCallWaitingReject() {
190 // Cdma call was rejected...
191 if (mCdmaIncomingConnection != null) {
192 onDisconnect(mCdmaIncomingConnection);
193 mCdmaIncomingConnection = null;
194 } else {
195 Log.e(TAG, "CDMA Call waiting rejection without an incoming call.");
196 }
197 }
198
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700199 /**
200 * CDMA Calls have no sense of "dialing" state. For outgoing calls 3way calls we want to
201 * mimick this state so that the the UI can notify the user that there is a "dialing"
202 * call.
203 */
204 public void setCdmaOutgoing3WayCall(Connection connection) {
205 boolean wasSet = mCdmaOutgoingConnection != null;
206
207 mCdmaOutgoingConnection = connection;
208
209 // If we reset the connection, that mean we can now tell the user that the call is actually
210 // part of the conference call and move it out of the dialing state. To do this, issue a
211 // new update completely.
212 if (wasSet && mCdmaOutgoingConnection == null) {
213 onPhoneStateChanged(null);
214 }
215 }
216
Santos Cordonaf763a12013-08-19 20:04:58 -0700217 private boolean hasLiveCallInternal(HashMap<Connection, Call> map) {
218 for (Call call : map.values()) {
219 final int state = call.getState();
220 if (state == Call.State.ACTIVE ||
221 state == Call.State.CALL_WAITING ||
222 state == Call.State.CONFERENCED ||
223 state == Call.State.DIALING ||
224 state == Call.State.INCOMING ||
225 state == Call.State.ONHOLD) {
226 return true;
227 }
228 }
229 return false;
230 }
231
Santos Cordon2b73bd62013-08-27 14:53:43 -0700232 public boolean hasOutstandingActiveOrDialingCall() {
233 return hasOutstandingActiveOrDialingCallInternal(mCallMap) ||
234 hasOutstandingActiveOrDialingCallInternal(mConfCallMap);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700235 }
236
Santos Cordon2b73bd62013-08-27 14:53:43 -0700237 private static boolean hasOutstandingActiveOrDialingCallInternal(
238 HashMap<Connection, Call> map) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700239 for (Call call : map.values()) {
240 final int state = call.getState();
Santos Cordon2b73bd62013-08-27 14:53:43 -0700241 if (state == Call.State.ACTIVE ||
242 state == Call.State.DIALING) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700243 return true;
244 }
245 }
246
247 return false;
248 }
249
Chiao Cheng3f015c92013-09-06 15:56:27 -0700250
251 /**
252 * Handles the POST_ON_DIAL_CHARS message from the Phone (see our call to
253 * mPhone.setOnPostDialCharacter() above.)
254 *
255 * TODO: NEED TO TEST THIS SEQUENCE now that we no longer handle "dialable" key events here in
256 * the InCallScreen: we do directly to the Dialer UI instead. Similarly, we may now need to go
257 * directly to the Dialer to handle POST_ON_DIAL_CHARS too.
258 */
259 private void onPostDialChars(AsyncResult r, char ch) {
260 final Connection c = (Connection) r.result;
261
262 if (c != null) {
263 final Connection.PostDialState state = (Connection.PostDialState) r.userObj;
264
265 switch (state) {
266 // TODO(klp): add other post dial related functions
267 case WAIT:
268 final Call call = getCallFromMap(mCallMap, c, false);
269 if (call == null) {
270 Log.i(TAG, "Call no longer exists. Skipping onPostDialWait().");
271 } else {
272 for (Listener mListener : mListeners) {
273 mListener.onPostDialWait(call.getCallId(),
274 c.getRemainingPostDialString());
275 }
276 }
277 break;
278
279 default:
280 break;
281 }
282 }
283 }
284
Santos Cordona5d5db82013-09-15 13:00:34 -0700285 private Call onNewRingingConnection(Connection conn) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700286 Log.i(TAG, "onNewRingingConnection");
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700287 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700288
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700289 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700290
Christine Chendaf7bf62013-08-05 19:12:31 -0700291 for (int i = 0; i < mListeners.size(); ++i) {
Christine Chenee09a492013-08-06 16:02:29 -0700292 if (call != null) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700293 mListeners.get(i).onIncoming(call);
Christine Chenee09a492013-08-06 16:02:29 -0700294 }
Santos Cordon63a84242013-07-23 13:32:52 -0700295 }
Santos Cordona5d5db82013-09-15 13:00:34 -0700296
297 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700298 }
299
Santos Cordona5d5db82013-09-15 13:00:34 -0700300 private void onDisconnect(Connection conn) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700301 Log.i(TAG, "onDisconnect");
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700302 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700303
Santos Cordon995c8162013-07-29 09:22:22 -0700304 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700305 final boolean wasConferenced = call.getState() == State.CONFERENCED;
306
307 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700308
Christine Chendaf7bf62013-08-05 19:12:31 -0700309 for (int i = 0; i < mListeners.size(); ++i) {
310 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700311 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700312
313 // If it was a conferenced call, we need to run the entire update
314 // to make the proper changes to parent conference calls.
315 if (wasConferenced) {
316 onPhoneStateChanged(null);
317 }
318
319 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700320 }
321 }
322
Santos Cordona3d05142013-07-29 11:25:17 -0700323 /**
324 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700325 */
Santos Cordon995c8162013-07-29 09:22:22 -0700326 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700327 Log.i(TAG, "onPhoneStateChanged: ");
Santos Cordon998f42b2013-08-02 16:13:12 -0700328 final List<Call> updatedCalls = Lists.newArrayList();
329 doUpdate(false, updatedCalls);
330
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700331 if (updatedCalls.size() > 0) {
332 for (int i = 0; i < mListeners.size(); ++i) {
333 mListeners.get(i).onUpdate(updatedCalls);
334 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700335 }
336 }
337
338
339 /**
340 * Go through the Calls from CallManager and return the list of calls that were updated.
341 * Or, the full list if requested.
342 */
343 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700344 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
345 telephonyCalls.addAll(mCallManager.getRingingCalls());
346 telephonyCalls.addAll(mCallManager.getForegroundCalls());
347 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
348
Santos Cordona3d05142013-07-29 11:25:17 -0700349 // Cycle through all the Connections on all the Calls. Update our Call objects
350 // to reflect any new state and send the updated Call objects to the handler service.
351 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700352
353 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordona5d5db82013-09-15 13:00:34 -0700354 if (DBG) Log.d(TAG, "connection: " + connection);
355
Santos Cordon12a03aa2013-09-12 23:34:05 -0700356 // We only send updates for live calls which are not incoming (ringing).
357 // Disconnected and incoming calls are handled by onDisconnect and
358 // onNewRingingConnection.
359 boolean shouldUpdate = connection.getState().isAlive() &&
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700360 !connection.getState().isRinging();
361
362 // New connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700363 // a state in the internal.telephony.Call object. This ensures that staleness
364 // check below fails and we always add the item to the update list if it is new.
Santos Cordon12a03aa2013-09-12 23:34:05 -0700365 final Call call = getCallFromMap(mCallMap, connection, shouldUpdate /* create */);
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700366
Santos Cordon12a03aa2013-09-12 23:34:05 -0700367 if (call == null || !shouldUpdate) {
Santos Cordona5d5db82013-09-15 13:00:34 -0700368 if (DBG) Log.d(TAG, "update skipped");
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700369 continue;
370 }
Santos Cordona3d05142013-07-29 11:25:17 -0700371
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700372 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordon2b73bd62013-08-27 14:53:43 -0700373
Santos Cordone38b1ff2013-08-07 12:12:16 -0700374 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700375 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700376 }
377 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700378
379 // We do a second loop to address conference call scenarios. We do this as a separate
380 // loop to ensure all child calls are up to date before we start updating the parent
381 // conference calls.
382 for (Connection connection : telephonyCall.getConnections()) {
383 updateForConferenceCalls(connection, out);
384 }
385
Santos Cordona3d05142013-07-29 11:25:17 -0700386 }
Santos Cordona3d05142013-07-29 11:25:17 -0700387 }
388
Santos Cordone38b1ff2013-08-07 12:12:16 -0700389 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700390 * Checks to see if the connection is the first connection in a conference call.
391 * If it is a conference call, we will create a new Conference Call object or
392 * update the existing conference call object for that connection.
393 * If it is not a conference call but a previous associated conference call still exists,
394 * we mark it as idle and remove it from the map.
395 * In both cases above, we add the Calls to be updated to the UI.
396 * @param connection The connection object to check.
397 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
398 */
399 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
400 // We consider this connection a conference connection if the call it
Yorke Leecd3f9692013-09-14 13:51:27 -0700401 // belongs to is a multiparty call AND it is the first live connection.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700402 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
Yorke Leecd3f9692013-09-14 13:51:27 -0700403 getEarliestLiveConnection(connection.getCall()) == connection;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700404
405 boolean changed = false;
406
407 // If this connection is the main connection for the conference call, then create or update
408 // a Call object for that conference call.
409 if (isConferenceCallConnection) {
410 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
411 changed = updateCallFromConnection(confCall, connection, true);
412
413 if (changed) {
414 updatedCalls.add(confCall);
415 }
416
417 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
418
419 // It is possible that through a conference call split, there may be lingering conference
420 // calls where this connection was the main connection. We clean those up here.
421 } else {
422 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
423
424 // We found a conference call for this connection, which is no longer a conference call.
425 // Kill it!
426 if (oldConfCall != null) {
427 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
428 mConfCallMap.remove(connection);
429 oldConfCall.setState(State.IDLE);
430 changed = true;
431
432 // add to the list of calls to update
433 updatedCalls.add(oldConfCall);
434 }
435 }
436
437 return changed;
438 }
439
Yorke Leecd3f9692013-09-14 13:51:27 -0700440 private Connection getEarliestLiveConnection(com.android.internal.telephony.Call call) {
441 final List<Connection> connections = call.getConnections();
442 final int size = connections.size();
443 Connection earliestConn = null;
444 long earliestTime = Long.MAX_VALUE;
445 for (int i = 0; i < size; i++) {
446 final Connection connection = connections.get(i);
447 if (!connection.isAlive()) continue;
448 final long time = connection.getCreateTime();
449 if (time < earliestTime) {
450 earliestTime = time;
451 earliestConn = connection;
452 }
453 }
454 return earliestConn;
455 }
456
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700457 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700458 * Sets the new call state onto the call and performs some additional logic
459 * associated with setting the state.
460 */
461 private void setNewState(Call call, int newState, Connection connection) {
462 Preconditions.checkState(call.getState() != newState);
463
464 // When starting an outgoing call, we need to grab gateway information
465 // for the call, if available, and set it.
466 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
467
468 if (newState == Call.State.DIALING) {
469 if (!info.isEmpty()) {
470 call.setGatewayNumber(info.getFormattedGatewayNumber());
471 call.setGatewayPackage(info.packageName);
472 }
473 } else if (!Call.State.isConnected(newState)) {
474 mCallGatewayManager.clearGatewayData(connection);
475 }
476
477 call.setState(newState);
478 }
479
480 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700481 * Updates the Call properties to match the state of the connection object
482 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700483 * @param call The call object to update.
484 * @param connection The connection object from which to update call.
485 * @param isForConference There are slight differences in how we populate data for conference
486 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700487 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700488 private boolean updateCallFromConnection(Call call, Connection connection,
489 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700490 boolean changed = false;
491
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700492 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700493
494 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700495 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700496 changed = true;
497 }
498
Santos Cordone38b1ff2013-08-07 12:12:16 -0700499 final Call.DisconnectCause newDisconnectCause =
500 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
501 if (call.getDisconnectCause() != newDisconnectCause) {
502 call.setDisconnectCause(newDisconnectCause);
503 changed = true;
504 }
505
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700506 final long oldConnectTime = call.getConnectTime();
507 if (oldConnectTime != connection.getConnectTime()) {
508 call.setConnectTime(connection.getConnectTime());
509 changed = true;
510 }
511
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700512 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700513 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700514 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700515 String newNumber = connection.getAddress();
516 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
517 if (!info.isEmpty()) {
518 newNumber = info.trueNumber;
519 }
520 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
521 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700522 changed = true;
523 }
524
Santos Cordon69a69192013-08-22 14:25:42 -0700525 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700526 final int newNumberPresentation = connection.getNumberPresentation();
527 if (call.getNumberPresentation() != newNumberPresentation) {
528 call.setNumberPresentation(newNumberPresentation);
529 changed = true;
530 }
531
Santos Cordon69a69192013-08-22 14:25:42 -0700532 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700533 final String oldCnapName = call.getCnapName();
534 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
535 call.setCnapName(connection.getCnapName());
536 changed = true;
537 }
Santos Cordon69a69192013-08-22 14:25:42 -0700538
539 // Name Presentation
540 final int newCnapNamePresentation = connection.getCnapNamePresentation();
541 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
542 call.setCnapNamePresentation(newCnapNamePresentation);
543 changed = true;
544 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700545 } else {
546
547 // update the list of children by:
548 // 1) Saving the old set
549 // 2) Removing all children
550 // 3) Adding the correct children into the Call
551 // 4) Comparing the new children set with the old children set
552 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
553 call.removeAllChildren();
554
555 if (connection.getCall() != null) {
556 for (Connection childConn : connection.getCall().getConnections()) {
557 final Call childCall = getCallFromMap(mCallMap, childConn, false);
558 if (childCall != null && childConn.isAlive()) {
559 call.addChildId(childCall.getCallId());
560 }
561 }
562 }
Christine Chen45277022013-09-05 10:55:37 -0700563 changed |= !oldSet.equals(call.getChildCallIds());
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700564 }
565
Santos Cordoneead6ec2013-08-07 22:16:33 -0700566 /**
567 * !!! Uses values from connection and call collected above so this part must be last !!!
568 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700569 final int newCapabilities = getCapabilitiesFor(connection, call, isForConference);
Santos Cordon26e7b242013-08-07 21:15:45 -0700570 if (call.getCapabilities() != newCapabilities) {
571 call.setCapabilities(newCapabilities);
572 changed = true;
573 }
574
Santos Cordone38b1ff2013-08-07 12:12:16 -0700575 return changed;
576 }
577
Santos Cordon26e7b242013-08-07 21:15:45 -0700578 /**
579 * Returns a mask of capabilities for the connection such as merge, hold, etc.
580 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700581 private int getCapabilitiesFor(Connection connection, Call call, boolean isForConference) {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700582 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
583 final Phone phone = connection.getCall().getPhone();
584
Santos Cordoneead6ec2013-08-07 22:16:33 -0700585 boolean canAddCall = false;
586 boolean canMergeCall = false;
587 boolean canSwapCall = false;
Yorke Lee814da302013-08-30 16:01:07 -0700588 boolean canRespondViaText = false;
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700589 boolean canMute = false;
590
591 final boolean supportHold = PhoneUtils.okToSupportHold(mCallManager);
Christine Chen94853bc2013-09-17 16:53:33 -0700592 final boolean canHold = (supportHold ? PhoneUtils.okToHoldCall(mCallManager) : false);
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700593 final boolean genericConf = isForConference &&
594 (connection.getCall().getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA);
Santos Cordoneead6ec2013-08-07 22:16:33 -0700595
596 // only applies to active calls
597 if (callIsActive) {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700598 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
599 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
600 }
601
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700602 canAddCall = PhoneUtils.okToAddCall(mCallManager);
603
604 // "Mute": only enabled when the foreground call is ACTIVE.
605 // (It's meaningless while on hold, or while DIALING/ALERTING.)
606 // It's also explicitly disabled during emergency calls or if
607 // emergency callback mode (ECM) is active.
608 boolean isEmergencyCall = false;
609 if (connection != null) {
610 isEmergencyCall = PhoneNumberUtils.isLocalEmergencyNumber(connection.getAddress(),
611 phone.getContext());
612 }
613 boolean isECM = PhoneUtils.isPhoneInEcm(phone);
614 if (isEmergencyCall || isECM) { // disable "Mute" item
615 canMute = false;
616 } else {
617 canMute = callIsActive;
618 }
619
Yorke Lee814da302013-08-30 16:01:07 -0700620 canRespondViaText = RejectWithTextMessageManager.allowRespondViaSmsForCall(call,
621 connection);
622
Santos Cordoneead6ec2013-08-07 22:16:33 -0700623 // special rules section!
624 // CDMA always has Add
625 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
626 canAddCall = true;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700627 }
628
Santos Cordon26e7b242013-08-07 21:15:45 -0700629 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700630 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700631 retval |= Capabilities.HOLD;
632 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700633 if (supportHold) {
634 retval |= Capabilities.SUPPORT_HOLD;
635 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700636 if (canAddCall) {
637 retval |= Capabilities.ADD_CALL;
638 }
639 if (canMergeCall) {
640 retval |= Capabilities.MERGE_CALLS;
641 }
642 if (canSwapCall) {
643 retval |= Capabilities.SWAP_CALLS;
644 }
Yorke Lee814da302013-08-30 16:01:07 -0700645 if (canRespondViaText) {
646 retval |= Capabilities.RESPOND_VIA_TEXT;
647 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700648 if (canMute) {
649 retval |= Capabilities.MUTE;
650 }
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700651 if (genericConf) {
652 retval |= Capabilities.GENERIC_CONFERENCE;
653 }
Yorke Lee814da302013-08-30 16:01:07 -0700654
Santos Cordon26e7b242013-08-07 21:15:45 -0700655 return retval;
656 }
657
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700658 /**
659 * Returns true if the Connection is part of a multiparty call.
660 * We do this by checking the isMultiparty() method of the telephony.Call object and also
661 * checking to see if more than one of it's children is alive.
662 */
663 private boolean isPartOfLiveConferenceCall(Connection connection) {
664 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
665 int count = 0;
666 for (Connection currConn : connection.getCall().getConnections()) {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700667
668 // Only count connections which are alive and never cound the special
669 // "dialing" 3way call for CDMA calls.
670 if (currConn.isAlive() && currConn != mCdmaOutgoingConnection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700671 count++;
672 if (count >= 2) {
673 return true;
674 }
675 }
676 }
677 }
678 return false;
679 }
680
681 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
682
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700683 // For the "fake" outgoing CDMA call, we need to always treat it as an outgoing call.
684 if (mCdmaOutgoingConnection == connection) {
685 return State.DIALING;
686 }
687
Santos Cordona3d05142013-07-29 11:25:17 -0700688 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700689 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700690 case ACTIVE:
691 retval = State.ACTIVE;
692 break;
693 case INCOMING:
694 retval = State.INCOMING;
695 break;
696 case DIALING:
697 case ALERTING:
698 retval = State.DIALING;
699 break;
700 case WAITING:
701 retval = State.CALL_WAITING;
702 break;
703 case HOLDING:
704 retval = State.ONHOLD;
705 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700706 case DISCONNECTED:
707 case DISCONNECTING:
708 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700709 default:
710 }
711
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700712 // If we are dealing with a potential child call (not the parent conference call),
713 // the check to see if we have to set the state to CONFERENCED.
714 if (!isForConference) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700715 // if the connection is part of a multiparty call, and it is live,
716 // annotate it with CONFERENCED state instead.
717 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
718 return State.CONFERENCED;
719 }
720 }
721
Santos Cordona3d05142013-07-29 11:25:17 -0700722 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700723 }
724
Santos Cordone38b1ff2013-08-07 12:12:16 -0700725 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
726 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
727 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
728 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
729 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
730 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
731 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
732 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
733 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
734 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
735 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
736 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
737 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
738 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
739 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
740 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
741 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
742 Call.DisconnectCause.CDMA_RETRY_ORDER)
743 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
744 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
745 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
746 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
747 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
748 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
749 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
750 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
751 Call.DisconnectCause.ERROR_UNSPECIFIED)
752 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
753 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
754 .put(Connection.DisconnectCause.INCOMING_MISSED,
755 Call.DisconnectCause.INCOMING_MISSED)
756 .put(Connection.DisconnectCause.INCOMING_REJECTED,
757 Call.DisconnectCause.INCOMING_REJECTED)
758 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
759 Call.DisconnectCause.INVALID_CREDENTIALS)
760 .put(Connection.DisconnectCause.INVALID_NUMBER,
761 Call.DisconnectCause.INVALID_NUMBER)
762 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
763 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
764 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
765 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
766 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
767 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
768 Call.DisconnectCause.NOT_DISCONNECTED)
769 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
770 Call.DisconnectCause.NUMBER_UNREACHABLE)
771 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
772 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
773 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
774 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
775 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
776 Call.DisconnectCause.SERVER_UNREACHABLE)
777 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
778 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
779 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
780 .build();
781
782 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
783 Connection.DisconnectCause causeSource) {
784
785 if (CAUSE_MAP.containsKey(causeSource)) {
786 return CAUSE_MAP.get(causeSource);
787 }
788
789 return Call.DisconnectCause.UNKNOWN;
790 }
791
Santos Cordon63a84242013-07-23 13:32:52 -0700792 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700793 * Gets an existing callId for a connection, or creates one if none exists.
794 * This function does NOT set any of the Connection data onto the Call class.
795 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700796 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700797 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
798 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700799 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700800
801 // Find the call id or create if missing and requested.
802 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700803 if (map.containsKey(conn)) {
804 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700805 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700806 call = createNewCall();
807 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700808 }
809 }
Santos Cordon995c8162013-07-29 09:22:22 -0700810 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700811 }
812
813 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700814 * Creates a brand new connection for the call.
815 */
816 private Call createNewCall() {
817 int callId;
818 int newNextCallId;
819 do {
820 callId = mNextCallId.get();
821
822 // protect against overflow
823 newNextCallId = (callId == Integer.MAX_VALUE ?
824 CALL_ID_START_VALUE : callId + 1);
825
826 // Keep looping if the change was not atomic OR the value is already taken.
827 // The call to containsValue() is linear, however, most devices support a
828 // maximum of 7 connections so it's not expensive.
829 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
830
831 return new Call(callId);
832 }
833
834 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700835 * Listener interface for changes to Calls.
836 */
837 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700838 void onDisconnect(Call call);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700839 void onIncoming(Call call);
840 void onUpdate(List<Call> calls);
Chiao Cheng3f015c92013-09-06 15:56:27 -0700841 void onPostDialWait(int callId, String remainingChars);
Santos Cordon63a84242013-07-23 13:32:52 -0700842 }
Santos Cordon249efd02013-08-05 03:33:56 -0700843
844 /**
845 * Result class for accessing a call by connection.
846 */
847 public static class CallResult {
848 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700849 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700850 public Connection mConnection;
851
852 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700853 this(call, call, connection);
854 }
855
856 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700857 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700858 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700859 mConnection = connection;
860 }
861
862 public Call getCall() {
863 return mCall;
864 }
865
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700866 // The call that should be used for call actions like hanging up.
867 public Call getActionableCall() {
868 return mActionableCall;
869 }
870
Santos Cordon249efd02013-08-05 03:33:56 -0700871 public Connection getConnection() {
872 return mConnection;
873 }
874 }
Santos Cordon63a84242013-07-23 13:32:52 -0700875}