blob: 11792af17f9045c23b15c18e1b1eda40af43b8e2 [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) {
Chiao Cheng3f015c92013-09-06 15:56:27 -0700268 case WAIT:
269 final Call call = getCallFromMap(mCallMap, c, false);
270 if (call == null) {
271 Log.i(TAG, "Call no longer exists. Skipping onPostDialWait().");
272 } else {
273 for (Listener mListener : mListeners) {
Yorke Leede41f672013-09-19 13:46:55 -0700274 mListener.onPostDialAction(state, call.getCallId(),
275 c.getRemainingPostDialString(), ch);
Chiao Cheng3f015c92013-09-06 15:56:27 -0700276 }
277 }
278 break;
Chiao Cheng3f015c92013-09-06 15:56:27 -0700279 default:
Yorke Leede41f672013-09-19 13:46:55 -0700280 // This is primarily to cause the DTMFTonePlayer to play local tones.
281 // Other listeners simply perform no-ops.
282 for (Listener mListener : mListeners) {
283 mListener.onPostDialAction(state, 0, "", ch);
284 }
Chiao Cheng3f015c92013-09-06 15:56:27 -0700285 break;
286 }
287 }
288 }
289
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700290 /* package */ Call onNewRingingConnection(Connection conn) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700291 Log.i(TAG, "onNewRingingConnection");
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700292 final Call call = getCallFromMap(mCallMap, conn, true);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700293
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700294 if (call != null) {
295 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700296
Christine Chenfb0cc2b2013-09-16 14:21:29 -0700297 for (int i = 0; i < mListeners.size(); ++i) {
298 mListeners.get(i).onIncoming(call);
Christine Chenee09a492013-08-06 16:02:29 -0700299 }
Santos Cordon63a84242013-07-23 13:32:52 -0700300 }
Santos Cordona5d5db82013-09-15 13:00:34 -0700301
302 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700303 }
304
Santos Cordona5d5db82013-09-15 13:00:34 -0700305 private void onDisconnect(Connection conn) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700306 Log.i(TAG, "onDisconnect");
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700307 final Call call = getCallFromMap(mCallMap, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700308
Santos Cordon995c8162013-07-29 09:22:22 -0700309 if (call != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700310 final boolean wasConferenced = call.getState() == State.CONFERENCED;
311
312 updateCallFromConnection(call, conn, false);
Santos Cordon63a84242013-07-23 13:32:52 -0700313
Christine Chendaf7bf62013-08-05 19:12:31 -0700314 for (int i = 0; i < mListeners.size(); ++i) {
315 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700316 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700317
318 // If it was a conferenced call, we need to run the entire update
319 // to make the proper changes to parent conference calls.
320 if (wasConferenced) {
321 onPhoneStateChanged(null);
322 }
323
324 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700325 }
326 }
327
Santos Cordona3d05142013-07-29 11:25:17 -0700328 /**
329 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700330 */
Santos Cordon995c8162013-07-29 09:22:22 -0700331 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon2b73bd62013-08-27 14:53:43 -0700332 Log.i(TAG, "onPhoneStateChanged: ");
Santos Cordon998f42b2013-08-02 16:13:12 -0700333 final List<Call> updatedCalls = Lists.newArrayList();
334 doUpdate(false, updatedCalls);
335
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700336 if (updatedCalls.size() > 0) {
337 for (int i = 0; i < mListeners.size(); ++i) {
338 mListeners.get(i).onUpdate(updatedCalls);
339 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700340 }
341 }
342
343
344 /**
345 * Go through the Calls from CallManager and return the list of calls that were updated.
346 * Or, the full list if requested.
347 */
348 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700349 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
350 telephonyCalls.addAll(mCallManager.getRingingCalls());
351 telephonyCalls.addAll(mCallManager.getForegroundCalls());
352 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
353
Santos Cordona3d05142013-07-29 11:25:17 -0700354 // Cycle through all the Connections on all the Calls. Update our Call objects
355 // to reflect any new state and send the updated Call objects to the handler service.
356 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
Santos Cordona3d05142013-07-29 11:25:17 -0700357
358 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordona5d5db82013-09-15 13:00:34 -0700359 if (DBG) Log.d(TAG, "connection: " + connection);
360
Santos Cordon12a03aa2013-09-12 23:34:05 -0700361 // We only send updates for live calls which are not incoming (ringing).
362 // Disconnected and incoming calls are handled by onDisconnect and
363 // onNewRingingConnection.
364 boolean shouldUpdate = connection.getState().isAlive() &&
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700365 !connection.getState().isRinging();
366
367 // New connections return a Call with INVALID state, which does not translate to
Santos Cordone38b1ff2013-08-07 12:12:16 -0700368 // a state in the internal.telephony.Call object. This ensures that staleness
369 // check below fails and we always add the item to the update list if it is new.
Santos Cordon12a03aa2013-09-12 23:34:05 -0700370 final Call call = getCallFromMap(mCallMap, connection, shouldUpdate /* create */);
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700371
Santos Cordon12a03aa2013-09-12 23:34:05 -0700372 if (call == null || !shouldUpdate) {
Santos Cordona5d5db82013-09-15 13:00:34 -0700373 if (DBG) Log.d(TAG, "update skipped");
Santos Cordon71d5c6e2013-09-05 21:34:33 -0700374 continue;
375 }
Santos Cordona3d05142013-07-29 11:25:17 -0700376
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700377 boolean changed = updateCallFromConnection(call, connection, false);
Santos Cordon2b73bd62013-08-27 14:53:43 -0700378
Santos Cordone38b1ff2013-08-07 12:12:16 -0700379 if (fullUpdate || changed) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700380 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700381 }
382 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700383
384 // We do a second loop to address conference call scenarios. We do this as a separate
385 // loop to ensure all child calls are up to date before we start updating the parent
386 // conference calls.
387 for (Connection connection : telephonyCall.getConnections()) {
388 updateForConferenceCalls(connection, out);
389 }
390
Santos Cordona3d05142013-07-29 11:25:17 -0700391 }
Santos Cordona3d05142013-07-29 11:25:17 -0700392 }
393
Santos Cordone38b1ff2013-08-07 12:12:16 -0700394 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700395 * Checks to see if the connection is the first connection in a conference call.
396 * If it is a conference call, we will create a new Conference Call object or
397 * update the existing conference call object for that connection.
398 * If it is not a conference call but a previous associated conference call still exists,
399 * we mark it as idle and remove it from the map.
400 * In both cases above, we add the Calls to be updated to the UI.
401 * @param connection The connection object to check.
402 * @param updatedCalls List of 'updated' calls that will be sent to the UI.
403 */
404 private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) {
405 // We consider this connection a conference connection if the call it
Yorke Leecd3f9692013-09-14 13:51:27 -0700406 // belongs to is a multiparty call AND it is the first live connection.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700407 final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) &&
Yorke Leecd3f9692013-09-14 13:51:27 -0700408 getEarliestLiveConnection(connection.getCall()) == connection;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700409
410 boolean changed = false;
411
412 // If this connection is the main connection for the conference call, then create or update
413 // a Call object for that conference call.
414 if (isConferenceCallConnection) {
415 final Call confCall = getCallFromMap(mConfCallMap, connection, true);
416 changed = updateCallFromConnection(confCall, connection, true);
417
418 if (changed) {
419 updatedCalls.add(confCall);
420 }
421
422 if (DBG) Log.d(TAG, "Updating a conference call: " + confCall);
423
424 // It is possible that through a conference call split, there may be lingering conference
425 // calls where this connection was the main connection. We clean those up here.
426 } else {
427 final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false);
428
429 // We found a conference call for this connection, which is no longer a conference call.
430 // Kill it!
431 if (oldConfCall != null) {
432 if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall);
433 mConfCallMap.remove(connection);
434 oldConfCall.setState(State.IDLE);
435 changed = true;
436
437 // add to the list of calls to update
438 updatedCalls.add(oldConfCall);
439 }
440 }
441
442 return changed;
443 }
444
Yorke Leecd3f9692013-09-14 13:51:27 -0700445 private Connection getEarliestLiveConnection(com.android.internal.telephony.Call call) {
446 final List<Connection> connections = call.getConnections();
447 final int size = connections.size();
448 Connection earliestConn = null;
449 long earliestTime = Long.MAX_VALUE;
450 for (int i = 0; i < size; i++) {
451 final Connection connection = connections.get(i);
452 if (!connection.isAlive()) continue;
453 final long time = connection.getCreateTime();
454 if (time < earliestTime) {
455 earliestTime = time;
456 earliestConn = connection;
457 }
458 }
459 return earliestConn;
460 }
461
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700462 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700463 * Sets the new call state onto the call and performs some additional logic
464 * associated with setting the state.
465 */
466 private void setNewState(Call call, int newState, Connection connection) {
467 Preconditions.checkState(call.getState() != newState);
468
469 // When starting an outgoing call, we need to grab gateway information
470 // for the call, if available, and set it.
471 final RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
472
473 if (newState == Call.State.DIALING) {
474 if (!info.isEmpty()) {
475 call.setGatewayNumber(info.getFormattedGatewayNumber());
476 call.setGatewayPackage(info.packageName);
477 }
478 } else if (!Call.State.isConnected(newState)) {
479 mCallGatewayManager.clearGatewayData(connection);
480 }
481
482 call.setState(newState);
483 }
484
485 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700486 * Updates the Call properties to match the state of the connection object
487 * that it represents.
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700488 * @param call The call object to update.
489 * @param connection The connection object from which to update call.
490 * @param isForConference There are slight differences in how we populate data for conference
491 * calls. This boolean tells us which method to use.
Santos Cordone38b1ff2013-08-07 12:12:16 -0700492 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700493 private boolean updateCallFromConnection(Call call, Connection connection,
494 boolean isForConference) {
Santos Cordone38b1ff2013-08-07 12:12:16 -0700495 boolean changed = false;
496
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700497 final int newState = translateStateFromTelephony(connection, isForConference);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700498
499 if (call.getState() != newState) {
Santos Cordon69a69192013-08-22 14:25:42 -0700500 setNewState(call, newState, connection);
Santos Cordone38b1ff2013-08-07 12:12:16 -0700501 changed = true;
502 }
503
Santos Cordone38b1ff2013-08-07 12:12:16 -0700504 final Call.DisconnectCause newDisconnectCause =
505 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
506 if (call.getDisconnectCause() != newDisconnectCause) {
507 call.setDisconnectCause(newDisconnectCause);
508 changed = true;
509 }
510
Santos Cordonbbe8ecf2013-08-13 15:26:18 -0700511 final long oldConnectTime = call.getConnectTime();
512 if (oldConnectTime != connection.getConnectTime()) {
513 call.setConnectTime(connection.getConnectTime());
514 changed = true;
515 }
516
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700517 if (!isForConference) {
Santos Cordon69a69192013-08-22 14:25:42 -0700518 // Number
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700519 final String oldNumber = call.getNumber();
Santos Cordon69a69192013-08-22 14:25:42 -0700520 String newNumber = connection.getAddress();
521 RawGatewayInfo info = mCallGatewayManager.getGatewayInfo(connection);
522 if (!info.isEmpty()) {
523 newNumber = info.trueNumber;
524 }
525 if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(newNumber)) {
526 call.setNumber(newNumber);
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700527 changed = true;
528 }
529
Santos Cordon69a69192013-08-22 14:25:42 -0700530 // Number presentation
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700531 final int newNumberPresentation = connection.getNumberPresentation();
532 if (call.getNumberPresentation() != newNumberPresentation) {
533 call.setNumberPresentation(newNumberPresentation);
534 changed = true;
535 }
536
Santos Cordon69a69192013-08-22 14:25:42 -0700537 // Name
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700538 final String oldCnapName = call.getCnapName();
539 if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) {
540 call.setCnapName(connection.getCnapName());
541 changed = true;
542 }
Santos Cordon69a69192013-08-22 14:25:42 -0700543
544 // Name Presentation
545 final int newCnapNamePresentation = connection.getCnapNamePresentation();
546 if (call.getCnapNamePresentation() != newCnapNamePresentation) {
547 call.setCnapNamePresentation(newCnapNamePresentation);
548 changed = true;
549 }
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700550 } else {
551
552 // update the list of children by:
553 // 1) Saving the old set
554 // 2) Removing all children
555 // 3) Adding the correct children into the Call
556 // 4) Comparing the new children set with the old children set
557 ImmutableSortedSet<Integer> oldSet = call.getChildCallIds();
558 call.removeAllChildren();
559
560 if (connection.getCall() != null) {
561 for (Connection childConn : connection.getCall().getConnections()) {
562 final Call childCall = getCallFromMap(mCallMap, childConn, false);
563 if (childCall != null && childConn.isAlive()) {
564 call.addChildId(childCall.getCallId());
565 }
566 }
567 }
Christine Chen45277022013-09-05 10:55:37 -0700568 changed |= !oldSet.equals(call.getChildCallIds());
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700569 }
570
Santos Cordoneead6ec2013-08-07 22:16:33 -0700571 /**
572 * !!! Uses values from connection and call collected above so this part must be last !!!
573 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700574 final int newCapabilities = getCapabilitiesFor(connection, call, isForConference);
Santos Cordon26e7b242013-08-07 21:15:45 -0700575 if (call.getCapabilities() != newCapabilities) {
576 call.setCapabilities(newCapabilities);
577 changed = true;
578 }
579
Santos Cordone38b1ff2013-08-07 12:12:16 -0700580 return changed;
581 }
582
Santos Cordon26e7b242013-08-07 21:15:45 -0700583 /**
584 * Returns a mask of capabilities for the connection such as merge, hold, etc.
585 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700586 private int getCapabilitiesFor(Connection connection, Call call, boolean isForConference) {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700587 final boolean callIsActive = (call.getState() == Call.State.ACTIVE);
588 final Phone phone = connection.getCall().getPhone();
589
Santos Cordoneead6ec2013-08-07 22:16:33 -0700590 boolean canAddCall = false;
591 boolean canMergeCall = false;
592 boolean canSwapCall = false;
Yorke Lee814da302013-08-30 16:01:07 -0700593 boolean canRespondViaText = false;
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700594 boolean canMute = false;
595
596 final boolean supportHold = PhoneUtils.okToSupportHold(mCallManager);
Christine Chen94853bc2013-09-17 16:53:33 -0700597 final boolean canHold = (supportHold ? PhoneUtils.okToHoldCall(mCallManager) : false);
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700598 final boolean genericConf = isForConference &&
599 (connection.getCall().getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA);
Santos Cordoneead6ec2013-08-07 22:16:33 -0700600
601 // only applies to active calls
602 if (callIsActive) {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700603 canMergeCall = PhoneUtils.okToMergeCalls(mCallManager);
604 canSwapCall = PhoneUtils.okToSwapCalls(mCallManager);
605 }
606
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700607 canAddCall = PhoneUtils.okToAddCall(mCallManager);
608
609 // "Mute": only enabled when the foreground call is ACTIVE.
610 // (It's meaningless while on hold, or while DIALING/ALERTING.)
611 // It's also explicitly disabled during emergency calls or if
612 // emergency callback mode (ECM) is active.
613 boolean isEmergencyCall = false;
614 if (connection != null) {
615 isEmergencyCall = PhoneNumberUtils.isLocalEmergencyNumber(connection.getAddress(),
616 phone.getContext());
617 }
618 boolean isECM = PhoneUtils.isPhoneInEcm(phone);
619 if (isEmergencyCall || isECM) { // disable "Mute" item
620 canMute = false;
621 } else {
622 canMute = callIsActive;
623 }
624
Yorke Lee814da302013-08-30 16:01:07 -0700625 canRespondViaText = RejectWithTextMessageManager.allowRespondViaSmsForCall(call,
626 connection);
627
Santos Cordoneead6ec2013-08-07 22:16:33 -0700628 // special rules section!
629 // CDMA always has Add
630 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
631 canAddCall = true;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700632 }
633
Santos Cordon26e7b242013-08-07 21:15:45 -0700634 int retval = 0x0;
Santos Cordoneead6ec2013-08-07 22:16:33 -0700635 if (canHold) {
Santos Cordon26e7b242013-08-07 21:15:45 -0700636 retval |= Capabilities.HOLD;
637 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700638 if (supportHold) {
639 retval |= Capabilities.SUPPORT_HOLD;
640 }
Santos Cordoneead6ec2013-08-07 22:16:33 -0700641 if (canAddCall) {
642 retval |= Capabilities.ADD_CALL;
643 }
644 if (canMergeCall) {
645 retval |= Capabilities.MERGE_CALLS;
646 }
647 if (canSwapCall) {
648 retval |= Capabilities.SWAP_CALLS;
649 }
Yorke Lee814da302013-08-30 16:01:07 -0700650 if (canRespondViaText) {
651 retval |= Capabilities.RESPOND_VIA_TEXT;
652 }
Christine Chenaf2fd0a2013-09-13 16:27:40 -0700653 if (canMute) {
654 retval |= Capabilities.MUTE;
655 }
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700656 if (genericConf) {
657 retval |= Capabilities.GENERIC_CONFERENCE;
658 }
Yorke Lee814da302013-08-30 16:01:07 -0700659
Santos Cordon26e7b242013-08-07 21:15:45 -0700660 return retval;
661 }
662
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700663 /**
664 * Returns true if the Connection is part of a multiparty call.
665 * We do this by checking the isMultiparty() method of the telephony.Call object and also
666 * checking to see if more than one of it's children is alive.
667 */
668 private boolean isPartOfLiveConferenceCall(Connection connection) {
669 if (connection.getCall() != null && connection.getCall().isMultiparty()) {
670 int count = 0;
671 for (Connection currConn : connection.getCall().getConnections()) {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700672
673 // Only count connections which are alive and never cound the special
674 // "dialing" 3way call for CDMA calls.
675 if (currConn.isAlive() && currConn != mCdmaOutgoingConnection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700676 count++;
677 if (count >= 2) {
678 return true;
679 }
680 }
681 }
682 }
683 return false;
684 }
685
686 private int translateStateFromTelephony(Connection connection, boolean isForConference) {
687
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700688 // For the "fake" outgoing CDMA call, we need to always treat it as an outgoing call.
689 if (mCdmaOutgoingConnection == connection) {
690 return State.DIALING;
691 }
692
Santos Cordona3d05142013-07-29 11:25:17 -0700693 int retval = State.IDLE;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700694 switch (connection.getState()) {
Santos Cordona3d05142013-07-29 11:25:17 -0700695 case ACTIVE:
696 retval = State.ACTIVE;
697 break;
698 case INCOMING:
699 retval = State.INCOMING;
700 break;
701 case DIALING:
702 case ALERTING:
703 retval = State.DIALING;
704 break;
705 case WAITING:
706 retval = State.CALL_WAITING;
707 break;
708 case HOLDING:
709 retval = State.ONHOLD;
710 break;
Santos Cordone38b1ff2013-08-07 12:12:16 -0700711 case DISCONNECTED:
712 case DISCONNECTING:
713 retval = State.DISCONNECTED;
Santos Cordona3d05142013-07-29 11:25:17 -0700714 default:
715 }
716
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700717 // If we are dealing with a potential child call (not the parent conference call),
718 // the check to see if we have to set the state to CONFERENCED.
719 if (!isForConference) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700720 // if the connection is part of a multiparty call, and it is live,
721 // annotate it with CONFERENCED state instead.
722 if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) {
723 return State.CONFERENCED;
724 }
725 }
726
Santos Cordona3d05142013-07-29 11:25:17 -0700727 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700728 }
729
Santos Cordone38b1ff2013-08-07 12:12:16 -0700730 private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP =
731 ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder()
732 .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY)
733 .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED)
734 .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED,
735 Call.DisconnectCause.CDMA_ACCESS_BLOCKED)
736 .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE,
737 Call.DisconnectCause.CDMA_ACCESS_FAILURE)
738 .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP)
739 .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT)
740 .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE,
741 Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE)
742 .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY,
743 Call.DisconnectCause.CDMA_NOT_EMERGENCY)
744 .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED)
745 .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER)
746 .put(Connection.DisconnectCause.CDMA_RETRY_ORDER,
747 Call.DisconnectCause.CDMA_RETRY_ORDER)
748 .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT)
749 .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION)
750 .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED)
751 .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY,
752 Call.DisconnectCause.CS_RESTRICTED_EMERGENCY)
753 .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL,
754 Call.DisconnectCause.CS_RESTRICTED_NORMAL)
755 .put(Connection.DisconnectCause.ERROR_UNSPECIFIED,
756 Call.DisconnectCause.ERROR_UNSPECIFIED)
757 .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED)
758 .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR)
759 .put(Connection.DisconnectCause.INCOMING_MISSED,
760 Call.DisconnectCause.INCOMING_MISSED)
761 .put(Connection.DisconnectCause.INCOMING_REJECTED,
762 Call.DisconnectCause.INCOMING_REJECTED)
763 .put(Connection.DisconnectCause.INVALID_CREDENTIALS,
764 Call.DisconnectCause.INVALID_CREDENTIALS)
765 .put(Connection.DisconnectCause.INVALID_NUMBER,
766 Call.DisconnectCause.INVALID_NUMBER)
767 .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED)
768 .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL)
769 .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL)
770 .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI)
771 .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL)
772 .put(Connection.DisconnectCause.NOT_DISCONNECTED,
773 Call.DisconnectCause.NOT_DISCONNECTED)
774 .put(Connection.DisconnectCause.NUMBER_UNREACHABLE,
775 Call.DisconnectCause.NUMBER_UNREACHABLE)
776 .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK)
777 .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE)
778 .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF)
779 .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR)
780 .put(Connection.DisconnectCause.SERVER_UNREACHABLE,
781 Call.DisconnectCause.SERVER_UNREACHABLE)
782 .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT)
783 .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER,
784 Call.DisconnectCause.UNOBTAINABLE_NUMBER)
785 .build();
786
787 private Call.DisconnectCause translateDisconnectCauseFromTelephony(
788 Connection.DisconnectCause causeSource) {
789
790 if (CAUSE_MAP.containsKey(causeSource)) {
791 return CAUSE_MAP.get(causeSource);
792 }
793
794 return Call.DisconnectCause.UNKNOWN;
795 }
796
Santos Cordon63a84242013-07-23 13:32:52 -0700797 /**
Santos Cordone38b1ff2013-08-07 12:12:16 -0700798 * Gets an existing callId for a connection, or creates one if none exists.
799 * This function does NOT set any of the Connection data onto the Call class.
800 * A separate call to updateCallFromConnection must be made for that purpose.
Santos Cordon63a84242013-07-23 13:32:52 -0700801 */
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700802 private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn,
803 boolean createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700804 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700805
806 // Find the call id or create if missing and requested.
807 if (conn != null) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700808 if (map.containsKey(conn)) {
809 call = map.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700810 } else if (createIfMissing) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700811 call = createNewCall();
812 map.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700813 }
814 }
Santos Cordon995c8162013-07-29 09:22:22 -0700815 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700816 }
817
818 /**
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700819 * Creates a brand new connection for the call.
820 */
821 private Call createNewCall() {
822 int callId;
823 int newNextCallId;
824 do {
825 callId = mNextCallId.get();
826
827 // protect against overflow
828 newNextCallId = (callId == Integer.MAX_VALUE ?
829 CALL_ID_START_VALUE : callId + 1);
830
831 // Keep looping if the change was not atomic OR the value is already taken.
832 // The call to containsValue() is linear, however, most devices support a
833 // maximum of 7 connections so it's not expensive.
834 } while (!mNextCallId.compareAndSet(callId, newNextCallId));
835
836 return new Call(callId);
837 }
838
839 /**
Santos Cordon63a84242013-07-23 13:32:52 -0700840 * Listener interface for changes to Calls.
841 */
842 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700843 void onDisconnect(Call call);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700844 void onIncoming(Call call);
845 void onUpdate(List<Call> calls);
Yorke Leede41f672013-09-19 13:46:55 -0700846 void onPostDialAction(Connection.PostDialState state, int callId, String remainingChars,
847 char c);
Santos Cordon63a84242013-07-23 13:32:52 -0700848 }
Santos Cordon249efd02013-08-05 03:33:56 -0700849
850 /**
851 * Result class for accessing a call by connection.
852 */
853 public static class CallResult {
854 public Call mCall;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700855 public Call mActionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700856 public Connection mConnection;
857
858 private CallResult(Call call, Connection connection) {
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700859 this(call, call, connection);
860 }
861
862 private CallResult(Call call, Call actionableCall, Connection connection) {
Santos Cordon249efd02013-08-05 03:33:56 -0700863 mCall = call;
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700864 mActionableCall = actionableCall;
Santos Cordon249efd02013-08-05 03:33:56 -0700865 mConnection = connection;
866 }
867
868 public Call getCall() {
869 return mCall;
870 }
871
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700872 // The call that should be used for call actions like hanging up.
873 public Call getActionableCall() {
874 return mActionableCall;
875 }
876
Santos Cordon249efd02013-08-05 03:33:56 -0700877 public Connection getConnection() {
878 return mConnection;
879 }
880 }
Santos Cordon63a84242013-07-23 13:32:52 -0700881}