blob: 75da0be25e7d2d42a97939bbc4002be5693220b7 [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 ||
227 state == Call.State.ONHOLD) {
228 return true;
229 }
230 }
231 return false;
232 }
233
Santos Cordon2b73bd62013-08-27 14:53:43 -0700234 public boolean hasOutstandingActiveOrDialingCall() {
235 return hasOutstandingActiveOrDialingCallInternal(mCallMap) ||
236 hasOutstandingActiveOrDialingCallInternal(mConfCallMap);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700237 }
238
Santos Cordon2b73bd62013-08-27 14:53:43 -0700239 private static boolean hasOutstandingActiveOrDialingCallInternal(
240 HashMap<Connection, Call> map) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700241 for (Call call : map.values()) {
242 final int state = call.getState();
Santos Cordon2b73bd62013-08-27 14:53:43 -0700243 if (state == Call.State.ACTIVE ||
244 state == Call.State.DIALING) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700245 return true;
246 }
247 }
248
249 return false;
250 }
251
Chiao Cheng3f015c92013-09-06 15:56:27 -0700252
253 /**
254 * Handles the POST_ON_DIAL_CHARS message from the Phone (see our call to
255 * mPhone.setOnPostDialCharacter() above.)
256 *
257 * TODO: NEED TO TEST THIS SEQUENCE now that we no longer handle "dialable" key events here in
258 * the InCallScreen: we do directly to the Dialer UI instead. Similarly, we may now need to go
259 * directly to the Dialer to handle POST_ON_DIAL_CHARS too.
260 */
261 private void onPostDialChars(AsyncResult r, char ch) {
262 final Connection c = (Connection) r.result;
263
264 if (c != null) {
265 final Connection.PostDialState state = (Connection.PostDialState) r.userObj;
266
267 switch (state) {
268 // TODO(klp): add other post dial related functions
269 case WAIT:
270 final Call call = getCallFromMap(mCallMap, c, false);
271 if (call == null) {
272 Log.i(TAG, "Call no longer exists. Skipping onPostDialWait().");
273 } else {
274 for (Listener mListener : mListeners) {
275 mListener.onPostDialWait(call.getCallId(),
276 c.getRemainingPostDialString());
277 }
278 }
279 break;
280
281 default:
282 break;
283 }
284 }
285 }
286
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700287 /* package */ Call onNewRingingConnection(Connection conn) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700288 Log.i(TAG, "onNewRingingConnection");
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700289 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700290
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700291 if (call != null) {
292 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700293
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700294 for (int i = 0; i < mListeners.size(); ++i) {
295 mListeners.get(i).onIncoming(call);
Christine Chenee09a492013-08-06 16:02:29 -0700296 }
Santos Cordon63a84242013-07-23 13:32:52 -0700297 }
Santos Cordona5d5db82013-09-15 13:00:34 -0700298
299 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700300 }
301
Santos Cordona5d5db82013-09-15 13:00:34 -0700302 private void onDisconnect(Connection conn) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700303 Log.i(TAG, "onDisconnect");
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700304 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700305
Santos Cordon995c8162013-07-29 09:22:22 -0700306 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700307 final boolean wasConferenced = call.getState() == State.CONFERENCED;
308
309 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700310
Christine Chendaf7bf62013-08-05 19:12:31 -0700311 for (int i = 0; i < mListeners.size(); ++i) {
312 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700313 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700314
315 // If it was a conferenced call, we need to run the entire update
316 // to make the proper changes to parent conference calls.
317 if (wasConferenced) {
318 onPhoneStateChanged(null);
319 }
320
321 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700322 }
323 }
324
Santos Cordona3d05142013-07-29 11:25:17 -0700325 /**
326 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700327 */
Santos Cordon995c8162013-07-29 09:22:22 -0700328 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700329 Log.i(TAG, "onPhoneStateChanged: ");
Santos Cordon998f42b2013-08-02 16:13:12 -0700330 final List<Call> updatedCalls = Lists.newArrayList();
331 doUpdate(false, updatedCalls);
332
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700333 if (updatedCalls.size() > 0) {
334 for (int i = 0; i < mListeners.size(); ++i) {
335 mListeners.get(i).onUpdate(updatedCalls);
336 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700337 }
338 }
339
340
341 /**
342 * Go through the Calls from CallManager and return the list of calls that were updated.
343 * Or, the full list if requested.
344 */
345 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700346 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
347 telephonyCalls.addAll(mCallManager.getRingingCalls());
348 telephonyCalls.addAll(mCallManager.getForegroundCalls());
349 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
350
Santos Cordona3d05142013-07-29 11:25:17 -0700351 // Cycle through all the Connections on all the Calls. Update our Call objects
352 // to reflect any new state and send the updated Call objects to the handler service.
353 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700354
355 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordona5d5db82013-09-15 13:00:34 -0700356 if (DBG) Log.d(TAG, "connection: " + connection);
357
Santos Cordon12a03aa2013-09-12 23:34:05 -0700358 // We only send updates for live calls which are not incoming (ringing).
359 // Disconnected and incoming calls are handled by onDisconnect and
360 // onNewRingingConnection.
361 boolean shouldUpdate = connection.getState().isAlive() &&
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700362 !connection.getState().isRinging();
363
364 // New connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700365 // a state in the internal.telephony.Call object. This ensures that staleness
366 // check below fails and we always add the item to the update list if it is new.
Santos Cordon12a03aa2013-09-12 23:34:05 -0700367 final Call call = getCallFromMap(mCallMap, connection, shouldUpdate /* create */);
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700368
Santos Cordon12a03aa2013-09-12 23:34:05 -0700369 if (call == null || !shouldUpdate) {
Santos Cordona5d5db82013-09-15 13:00:34 -0700370 if (DBG) Log.d(TAG, "update skipped");
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700371 continue;
372 }
Santos Cordona3d05142013-07-29 11:25:17 -0700373
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700374 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordon2b73bd62013-08-27 14:53:43 -0700375
Santos Cordone38b1ff2013-08-07 12:12:16 -0700376 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700377 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700378 }
379 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700380
381 // We do a second loop to address conference call scenarios. We do this as a separate
382 // loop to ensure all child calls are up to date before we start updating the parent
383 // conference calls.
384 for (Connection connection : telephonyCall.getConnections()) {
385 updateForConferenceCalls(connection, out);
386 }
387
Santos Cordona3d05142013-07-29 11:25:17 -0700388 }
Santos Cordona3d05142013-07-29 11:25:17 -0700389 }
390
Santos Cordone38b1ff2013-08-07 12:12:16 -0700391 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700392 * Checks to see if the connection is the first connection in a conference call.
393 * If it is a conference call, we will create a new Conference Call object or
394 * update the existing conference call object for that connection.
395 * If it is not a conference call but a previous associated conference call still exists,
396 * we mark it as idle and remove it from the map.
397 * In both cases above, we add the Calls to be updated to the UI.
398 * @param connection The connection object to check.
399 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
400 */
401 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
402 // We consider this connection a conference connection if the call it
Yorke Leecd3f9692013-09-14 13:51:27 -0700403 // belongs to is a multiparty call AND it is the first live connection.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700404 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
Yorke Leecd3f9692013-09-14 13:51:27 -0700405 getEarliestLiveConnection(connection.getCall()) == connection;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700406
407 boolean changed = false;
408
409 // If this connection is the main connection for the conference call, then create or update
410 // a Call object for that conference call.
411 if (isConferenceCallConnection) {
412 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
413 changed = updateCallFromConnection(confCall, connection, true);
414
415 if (changed) {
416 updatedCalls.add(confCall);
417 }
418
419 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
420
421 // It is possible that through a conference call split, there may be lingering conference
422 // calls where this connection was the main connection. We clean those up here.
423 } else {
424 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
425
426 // We found a conference call for this connection, which is no longer a conference call.
427 // Kill it!
428 if (oldConfCall != null) {
429 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
430 mConfCallMap.remove(connection);
431 oldConfCall.setState(State.IDLE);
432 changed = true;
433
434 // add to the list of calls to update
435 updatedCalls.add(oldConfCall);
436 }
437 }
438
439 return changed;
440 }
441
Yorke Leecd3f9692013-09-14 13:51:27 -0700442 private Connection getEarliestLiveConnection(com.android.internal.telephony.Call call) {
443 final List<Connection> connections = call.getConnections();
444 final int size = connections.size();
445 Connection earliestConn = null;
446 long earliestTime = Long.MAX_VALUE;
447 for (int i = 0; i < size; i++) {
448 final Connection connection = connections.get(i);
449 if (!connection.isAlive()) continue;
450 final long time = connection.getCreateTime();
451 if (time < earliestTime) {
452 earliestTime = time;
453 earliestConn = connection;
454 }
455 }
456 return earliestConn;
457 }
458
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700459 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700460 * Sets the new call state onto the call and performs some additional logic
461 * associated with setting the state.
462 */
463 private void setNewState(Call call, int newState, Connection connection) {
464 Preconditions.checkState(call.getState() != newState);
465
466 // When starting an outgoing call, we need to grab gateway information
467 // for the call, if available, and set it.
468 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
469
470 if (newState == Call.State.DIALING) {
471 if (!info.isEmpty()) {
472 call.setGatewayNumber(info.getFormattedGatewayNumber());
473 call.setGatewayPackage(info.packageName);
474 }
475 } else if (!Call.State.isConnected(newState)) {
476 mCallGatewayManager.clearGatewayData(connection);
477 }
478
479 call.setState(newState);
480 }
481
482 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700483 * Updates the Call properties to match the state of the connection object
484 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700485 * @param call The call object to update.
486 * @param connection The connection object from which to update call.
487 * @param isForConference There are slight differences in how we populate data for conference
488 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700489 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700490 private boolean updateCallFromConnection(Call call, Connection connection,
491 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700492 boolean changed = false;
493
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700494 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700495
496 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700497 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700498 changed = true;
499 }
500
Santos Cordone38b1ff2013-08-07 12:12:16 -0700501 final Call.DisconnectCause newDisconnectCause =
502 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
503 if (call.getDisconnectCause() != newDisconnectCause) {
504 call.setDisconnectCause(newDisconnectCause);
505 changed = true;
506 }
507
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700508 final long oldConnectTime = call.getConnectTime();
509 if (oldConnectTime != connection.getConnectTime()) {
510 call.setConnectTime(connection.getConnectTime());
511 changed = true;
512 }
513
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700514 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700515 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700516 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700517 String newNumber = connection.getAddress();
518 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
519 if (!info.isEmpty()) {
520 newNumber = info.trueNumber;
521 }
522 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
523 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700524 changed = true;
525 }
526
Santos Cordon69a69192013-08-22 14:25:42 -0700527 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700528 final int newNumberPresentation = connection.getNumberPresentation();
529 if (call.getNumberPresentation() != newNumberPresentation) {
530 call.setNumberPresentation(newNumberPresentation);
531 changed = true;
532 }
533
Santos Cordon69a69192013-08-22 14:25:42 -0700534 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700535 final String oldCnapName = call.getCnapName();
536 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
537 call.setCnapName(connection.getCnapName());
538 changed = true;
539 }
Santos Cordon69a69192013-08-22 14:25:42 -0700540
541 // Name Presentation
542 final int newCnapNamePresentation = connection.getCnapNamePresentation();
543 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
544 call.setCnapNamePresentation(newCnapNamePresentation);
545 changed = true;
546 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700547 } else {
548
549 // update the list of children by:
550 // 1) Saving the old set
551 // 2) Removing all children
552 // 3) Adding the correct children into the Call
553 // 4) Comparing the new children set with the old children set
554 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
555 call.removeAllChildren();
556
557 if (connection.getCall() != null) {
558 for (Connection childConn : connection.getCall().getConnections()) {
559 final Call childCall = getCallFromMap(mCallMap, childConn, false);
560 if (childCall != null && childConn.isAlive()) {
561 call.addChildId(childCall.getCallId());
562 }
563 }
564 }
Christine Chen45277022013-09-05 10:55:37 -0700565 changed |= !oldSet.equals(call.getChildCallIds());
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700566 }
567
Santos Cordoneead6ec2013-08-07 22:16:33 -0700568 /**
569 * !!! Uses values from connection and call collected above so this part must be last !!!
570 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700571 final int newCapabilities = getCapabilitiesFor(connection, call, isForConference);
Santos Cordon26e7b242013-08-07 21:15:45 -0700572 if (call.getCapabilities() != newCapabilities) {
573 call.setCapabilities(newCapabilities);
574 changed = true;
575 }
576
Santos Cordone38b1ff2013-08-07 12:12:16 -0700577 return changed;
578 }
579
Santos Cordon26e7b242013-08-07 21:15:45 -0700580 /**
581 * Returns a mask of capabilities for the connection such as merge, hold, etc.
582 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700583 private int getCapabilitiesFor(Connection connection, Call call, boolean isForConference) {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700584 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
585 final Phone phone = connection.getCall().getPhone();
586
Santos Cordoneead6ec2013-08-07 22:16:33 -0700587 boolean canAddCall = false;
588 boolean canMergeCall = false;
589 boolean canSwapCall = false;
Yorke Lee814da302013-08-30 16:01:07 -0700590 boolean canRespondViaText = false;
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700591 boolean canMute = false;
592
593 final boolean supportHold = PhoneUtils.okToSupportHold(mCallManager);
Christine Chen94853bc2013-09-17 16:53:33 -0700594 final boolean canHold = (supportHold ? PhoneUtils.okToHoldCall(mCallManager) : false);
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700595 final boolean genericConf = isForConference &&
596 (connection.getCall().getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA);
Santos Cordoneead6ec2013-08-07 22:16:33 -0700597
598 // only applies to active calls
599 if (callIsActive) {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700600 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
601 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
602 }
603
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700604 canAddCall = PhoneUtils.okToAddCall(mCallManager);
605
606 // "Mute": only enabled when the foreground call is ACTIVE.
607 // (It's meaningless while on hold, or while DIALING/ALERTING.)
608 // It's also explicitly disabled during emergency calls or if
609 // emergency callback mode (ECM) is active.
610 boolean isEmergencyCall = false;
611 if (connection != null) {
612 isEmergencyCall = PhoneNumberUtils.isLocalEmergencyNumber(connection.getAddress(),
613 phone.getContext());
614 }
615 boolean isECM = PhoneUtils.isPhoneInEcm(phone);
616 if (isEmergencyCall || isECM) { // disable "Mute" item
617 canMute = false;
618 } else {
619 canMute = callIsActive;
620 }
621
Yorke Lee814da302013-08-30 16:01:07 -0700622 canRespondViaText = RejectWithTextMessageManager.allowRespondViaSmsForCall(call,
623 connection);
624
Santos Cordoneead6ec2013-08-07 22:16:33 -0700625 // special rules section!
626 // CDMA always has Add
627 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
628 canAddCall = true;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700629 }
630
Santos Cordon26e7b242013-08-07 21:15:45 -0700631 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700632 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700633 retval |= Capabilities.HOLD;
634 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700635 if (supportHold) {
636 retval |= Capabilities.SUPPORT_HOLD;
637 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700638 if (canAddCall) {
639 retval |= Capabilities.ADD_CALL;
640 }
641 if (canMergeCall) {
642 retval |= Capabilities.MERGE_CALLS;
643 }
644 if (canSwapCall) {
645 retval |= Capabilities.SWAP_CALLS;
646 }
Yorke Lee814da302013-08-30 16:01:07 -0700647 if (canRespondViaText) {
648 retval |= Capabilities.RESPOND_VIA_TEXT;
649 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700650 if (canMute) {
651 retval |= Capabilities.MUTE;
652 }
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700653 if (genericConf) {
654 retval |= Capabilities.GENERIC_CONFERENCE;
655 }
Yorke Lee814da302013-08-30 16:01:07 -0700656
Santos Cordon26e7b242013-08-07 21:15:45 -0700657 return retval;
658 }
659
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700660 /**
661 * Returns true if the Connection is part of a multiparty call.
662 * We do this by checking the isMultiparty() method of the telephony.Call object and also
663 * checking to see if more than one of it's children is alive.
664 */
665 private boolean isPartOfLiveConferenceCall(Connection connection) {
666 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
667 int count = 0;
668 for (Connection currConn : connection.getCall().getConnections()) {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700669
670 // Only count connections which are alive and never cound the special
671 // "dialing" 3way call for CDMA calls.
672 if (currConn.isAlive() && currConn != mCdmaOutgoingConnection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700673 count++;
674 if (count >= 2) {
675 return true;
676 }
677 }
678 }
679 }
680 return false;
681 }
682
683 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
684
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700685 // For the "fake" outgoing CDMA call, we need to always treat it as an outgoing call.
686 if (mCdmaOutgoingConnection == connection) {
687 return State.DIALING;
688 }
689
Santos Cordona3d05142013-07-29 11:25:17 -0700690 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700691 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700692 case ACTIVE:
693 retval = State.ACTIVE;
694 break;
695 case INCOMING:
696 retval = State.INCOMING;
697 break;
698 case DIALING:
699 case ALERTING:
700 retval = State.DIALING;
701 break;
702 case WAITING:
703 retval = State.CALL_WAITING;
704 break;
705 case HOLDING:
706 retval = State.ONHOLD;
707 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700708 case DISCONNECTED:
709 case DISCONNECTING:
710 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700711 default:
712 }
713
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700714 // If we are dealing with a potential child call (not the parent conference call),
715 // the check to see if we have to set the state to CONFERENCED.
716 if (!isForConference) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700717 // if the connection is part of a multiparty call, and it is live,
718 // annotate it with CONFERENCED state instead.
719 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
720 return State.CONFERENCED;
721 }
722 }
723
Santos Cordona3d05142013-07-29 11:25:17 -0700724 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700725 }
726
Santos Cordone38b1ff2013-08-07 12:12:16 -0700727 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
728 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
729 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
730 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
731 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
732 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
733 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
734 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
735 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
736 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
737 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
738 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
739 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
740 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
741 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
742 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
743 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
744 Call.DisconnectCause.CDMA_RETRY_ORDER)
745 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
746 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
747 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
748 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
749 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
750 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
751 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
752 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
753 Call.DisconnectCause.ERROR_UNSPECIFIED)
754 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
755 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
756 .put(Connection.DisconnectCause.INCOMING_MISSED,
757 Call.DisconnectCause.INCOMING_MISSED)
758 .put(Connection.DisconnectCause.INCOMING_REJECTED,
759 Call.DisconnectCause.INCOMING_REJECTED)
760 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
761 Call.DisconnectCause.INVALID_CREDENTIALS)
762 .put(Connection.DisconnectCause.INVALID_NUMBER,
763 Call.DisconnectCause.INVALID_NUMBER)
764 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
765 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
766 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
767 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
768 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
769 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
770 Call.DisconnectCause.NOT_DISCONNECTED)
771 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
772 Call.DisconnectCause.NUMBER_UNREACHABLE)
773 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
774 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
775 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
776 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
777 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
778 Call.DisconnectCause.SERVER_UNREACHABLE)
779 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
780 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
781 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
782 .build();
783
784 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
785 Connection.DisconnectCause causeSource) {
786
787 if (CAUSE_MAP.containsKey(causeSource)) {
788 return CAUSE_MAP.get(causeSource);
789 }
790
791 return Call.DisconnectCause.UNKNOWN;
792 }
793
Santos Cordon63a84242013-07-23 13:32:52 -0700794 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700795 * Gets an existing callId for a connection, or creates one if none exists.
796 * This function does NOT set any of the Connection data onto the Call class.
797 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700798 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700799 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
800 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700801 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700802
803 // Find the call id or create if missing and requested.
804 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700805 if (map.containsKey(conn)) {
806 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700807 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700808 call = createNewCall();
809 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700810 }
811 }
Santos Cordon995c8162013-07-29 09:22:22 -0700812 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700813 }
814
815 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700816 * Creates a brand new connection for the call.
817 */
818 private Call createNewCall() {
819 int callId;
820 int newNextCallId;
821 do {
822 callId = mNextCallId.get();
823
824 // protect against overflow
825 newNextCallId = (callId == Integer.MAX_VALUE ?
826 CALL_ID_START_VALUE : callId + 1);
827
828 // Keep looping if the change was not atomic OR the value is already taken.
829 // The call to containsValue() is linear, however, most devices support a
830 // maximum of 7 connections so it's not expensive.
831 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
832
833 return new Call(callId);
834 }
835
836 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700837 * Listener interface for changes to Calls.
838 */
839 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700840 void onDisconnect(Call call);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700841 void onIncoming(Call call);
842 void onUpdate(List<Call> calls);
Chiao Cheng3f015c92013-09-06 15:56:27 -0700843 void onPostDialWait(int callId, String remainingChars);
Santos Cordon63a84242013-07-23 13:32:52 -0700844 }
Santos Cordon249efd02013-08-05 03:33:56 -0700845
846 /**
847 * Result class for accessing a call by connection.
848 */
849 public static class CallResult {
850 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700851 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700852 public Connection mConnection;
853
854 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700855 this(call, call, connection);
856 }
857
858 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700859 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700860 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700861 mConnection = connection;
862 }
863
864 public Call getCall() {
865 return mCall;
866 }
867
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700868 // The call that should be used for call actions like hanging up.
869 public Call getActionableCall() {
870 return mActionableCall;
871 }
872
Santos Cordon249efd02013-08-05 03:33:56 -0700873 public Connection getConnection() {
874 return mConnection;
875 }
876 }
Santos Cordon63a84242013-07-23 13:32:52 -0700877}