Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.phone; |
| 18 | |
| 19 | import com.google.android.collect.Lists; |
| 20 | import com.google.android.collect.Maps; |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 21 | import com.google.android.collect.Sets; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 22 | import com.google.common.base.Preconditions; |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableMap; |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 24 | import com.google.common.collect.ImmutableSortedSet; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 25 | |
| 26 | import android.os.AsyncResult; |
| 27 | import android.os.Handler; |
| 28 | import android.os.Message; |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 29 | import android.os.SystemProperties; |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
| 31 | import android.util.Log; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 32 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 33 | import com.android.internal.telephony.CallManager; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 34 | import com.android.internal.telephony.Connection; |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 35 | import com.android.internal.telephony.Phone; |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 36 | import com.android.internal.telephony.PhoneConstants; |
Santos Cordon | 26e7b24 | 2013-08-07 21:15:45 -0700 | [diff] [blame] | 37 | import com.android.internal.telephony.TelephonyCapabilities; |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 38 | import com.android.services.telephony.common.Call; |
Santos Cordon | 26e7b24 | 2013-08-07 21:15:45 -0700 | [diff] [blame] | 39 | import com.android.services.telephony.common.Call.Capabilities; |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 40 | import com.android.services.telephony.common.Call.State; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 41 | |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.HashMap; |
| 44 | import java.util.List; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 45 | import java.util.Map.Entry; |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 46 | import java.util.SortedSet; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 47 | import java.util.concurrent.atomic.AtomicInteger; |
| 48 | |
| 49 | /** |
| 50 | * Creates a Call model from Call state and data received from the telephony |
| 51 | * layer. The telephony layer maintains 3 conceptual objects: Phone, Call, |
| 52 | * Connection. |
| 53 | * |
| 54 | * Phone represents the radio and there is an implementation per technology |
| 55 | * type such as GSMPhone, SipPhone, CDMAPhone, etc. Generally, we will only ever |
| 56 | * deal with one instance of this object for the lifetime of this class. |
| 57 | * |
| 58 | * There are 3 Call instances that exist for the lifetime of this class which |
| 59 | * are created by CallTracker. The three are RingingCall, ForegroundCall, and |
| 60 | * BackgroundCall. |
| 61 | * |
| 62 | * A Connection most closely resembles what the layperson would consider a call. |
| 63 | * A Connection is created when a user dials and it is "owned" by one of the |
| 64 | * three Call instances. Which of the three Calls owns the Connection changes |
| 65 | * as the Connection goes between ACTIVE, HOLD, RINGING, and other states. |
| 66 | * |
| 67 | * This class models a new Call class from Connection objects received from |
| 68 | * the telephony layer. We use Connection references as identifiers for a call; |
| 69 | * new reference = new call. |
| 70 | * |
| 71 | * TODO(klp): Create a new Call class to replace the simple call Id ints |
| 72 | * being used currently. |
| 73 | * |
| 74 | * The new Call models are parcellable for transfer via the CallHandlerService |
| 75 | * API. |
| 76 | */ |
| 77 | public class CallModeler extends Handler { |
| 78 | |
| 79 | private static final String TAG = CallModeler.class.getSimpleName(); |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 80 | private static final boolean DBG = |
| 81 | (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 82 | |
| 83 | private static final int CALL_ID_START_VALUE = 1; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 84 | |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 85 | private final CallStateMonitor mCallStateMonitor; |
| 86 | private final CallManager mCallManager; |
| 87 | private final HashMap<Connection, Call> mCallMap = Maps.newHashMap(); |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 88 | private final HashMap<Connection, Call> mConfCallMap = Maps.newHashMap(); |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 89 | private final AtomicInteger mNextCallId = new AtomicInteger(CALL_ID_START_VALUE); |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 90 | private final ArrayList<Listener> mListeners = new ArrayList<Listener>(); |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 91 | private RejectWithTextMessageManager mRejectWithTextMessageManager; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 92 | |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 93 | public CallModeler(CallStateMonitor callStateMonitor, CallManager callManager, |
| 94 | RejectWithTextMessageManager rejectWithTextMessageManager) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 95 | mCallStateMonitor = callStateMonitor; |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 96 | mCallManager = callManager; |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 97 | mRejectWithTextMessageManager = rejectWithTextMessageManager; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 98 | |
| 99 | mCallStateMonitor.addListener(this); |
| 100 | } |
| 101 | |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 102 | @Override |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 103 | public void handleMessage(Message msg) { |
| 104 | switch(msg.what) { |
| 105 | case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION: |
| 106 | onNewRingingConnection((AsyncResult) msg.obj); |
| 107 | break; |
| 108 | case CallStateMonitor.PHONE_DISCONNECT: |
| 109 | onDisconnect((AsyncResult) msg.obj); |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 110 | break; |
| 111 | case CallStateMonitor.PHONE_STATE_CHANGED: |
| 112 | onPhoneStateChanged((AsyncResult) msg.obj); |
| 113 | break; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 114 | default: |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 119 | public void addListener(Listener listener) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 120 | Preconditions.checkNotNull(listener); |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 121 | Preconditions.checkNotNull(mListeners); |
Christine Chen | 4748abd | 2013-08-07 15:44:15 -0700 | [diff] [blame] | 122 | if (!mListeners.contains(listener)) { |
| 123 | mListeners.add(listener); |
| 124 | } |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public List<Call> getFullList() { |
| 128 | final List<Call> retval = Lists.newArrayList(); |
| 129 | doUpdate(true, retval); |
| 130 | return retval; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 133 | public CallResult getCallWithId(int callId) { |
| 134 | // max 8 connections, so this should be fast even through we are traversing the entire map. |
| 135 | for (Entry<Connection, Call> entry : mCallMap.entrySet()) { |
| 136 | if (entry.getValue().getCallId() == callId) { |
| 137 | return new CallResult(entry.getValue(), entry.getKey()); |
| 138 | } |
| 139 | } |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 140 | |
| 141 | for (Entry<Connection, Call> entry : mConfCallMap.entrySet()) { |
| 142 | if (entry.getValue().getCallId() == callId) { |
| 143 | if (entry.getValue().getChildCallIds().size() == 0) { |
| 144 | return null; |
| 145 | } |
| 146 | final CallResult child = getCallWithId(entry.getValue().getChildCallIds().first()); |
| 147 | return new CallResult(entry.getValue(), child.getActionableCall(), |
| 148 | child.getConnection()); |
| 149 | } |
| 150 | } |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 151 | return null; |
| 152 | } |
| 153 | |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 154 | public boolean hasOutstandingActiveCall() { |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 155 | return hasOutstandingActiveCallInternal(mCallMap) || |
| 156 | hasOutstandingActiveCallInternal(mConfCallMap); |
| 157 | } |
| 158 | |
| 159 | private static boolean hasOutstandingActiveCallInternal(HashMap<Connection, Call> map) { |
| 160 | for (Call call : map.values()) { |
| 161 | final int state = call.getState(); |
| 162 | if (Call.State.ACTIVE == state) { |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 170 | private void onNewRingingConnection(AsyncResult r) { |
| 171 | final Connection conn = (Connection) r.result; |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 172 | final Call call = getCallFromMap(mCallMap, conn, true); |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 173 | |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 174 | updateCallFromConnection(call, conn, false); |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 175 | call.setState(Call.State.INCOMING); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 176 | |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 177 | for (int i = 0; i < mListeners.size(); ++i) { |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 178 | if (call != null) { |
| 179 | mListeners.get(i).onIncoming(call, |
| 180 | mRejectWithTextMessageManager.loadCannedResponses()); |
| 181 | } |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | private void onDisconnect(AsyncResult r) { |
| 186 | final Connection conn = (Connection) r.result; |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 187 | final Call call = getCallFromMap(mCallMap, conn, false); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 188 | |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 189 | if (call != null) { |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 190 | final boolean wasConferenced = call.getState() == State.CONFERENCED; |
| 191 | |
| 192 | updateCallFromConnection(call, conn, false); |
| 193 | call.setState(Call.State.DISCONNECTED); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 194 | |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 195 | for (int i = 0; i < mListeners.size(); ++i) { |
| 196 | mListeners.get(i).onDisconnect(call); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 197 | } |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 198 | |
| 199 | // If it was a conferenced call, we need to run the entire update |
| 200 | // to make the proper changes to parent conference calls. |
| 201 | if (wasConferenced) { |
| 202 | onPhoneStateChanged(null); |
| 203 | } |
| 204 | |
| 205 | mCallMap.remove(conn); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 206 | } |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 207 | |
| 208 | // TODO(klp): Do a final check to see if there are any active calls. |
| 209 | // If there are not, totally cancel all calls |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Called when the phone state changes. |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 214 | */ |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 215 | private void onPhoneStateChanged(AsyncResult r) { |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 216 | final List<Call> updatedCalls = Lists.newArrayList(); |
| 217 | doUpdate(false, updatedCalls); |
| 218 | |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 219 | for (int i = 0; i < mListeners.size(); ++i) { |
| 220 | mListeners.get(i).onUpdate(updatedCalls, false); |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * Go through the Calls from CallManager and return the list of calls that were updated. |
| 227 | * Or, the full list if requested. |
| 228 | */ |
| 229 | private void doUpdate(boolean fullUpdate, List<Call> out) { |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 230 | final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList(); |
| 231 | telephonyCalls.addAll(mCallManager.getRingingCalls()); |
| 232 | telephonyCalls.addAll(mCallManager.getForegroundCalls()); |
| 233 | telephonyCalls.addAll(mCallManager.getBackgroundCalls()); |
| 234 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 235 | // Cycle through all the Connections on all the Calls. Update our Call objects |
| 236 | // to reflect any new state and send the updated Call objects to the handler service. |
| 237 | for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) { |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 238 | |
| 239 | for (Connection connection : telephonyCall.getConnections()) { |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 240 | // new connections return a Call with INVALID state, which does not translate to |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 241 | // a state in the internal.telephony.Call object. This ensures that staleness |
| 242 | // check below fails and we always add the item to the update list if it is new. |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 243 | final Call call = getCallFromMap(mCallMap, connection, true); |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 244 | |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 245 | boolean changed = updateCallFromConnection(call, connection, false); |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 246 | if (fullUpdate || changed) { |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 247 | out.add(call); |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 250 | |
| 251 | // We do a second loop to address conference call scenarios. We do this as a separate |
| 252 | // loop to ensure all child calls are up to date before we start updating the parent |
| 253 | // conference calls. |
| 254 | for (Connection connection : telephonyCall.getConnections()) { |
| 255 | updateForConferenceCalls(connection, out); |
| 256 | } |
| 257 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 258 | } |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 261 | /** |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 262 | * Checks to see if the connection is the first connection in a conference call. |
| 263 | * If it is a conference call, we will create a new Conference Call object or |
| 264 | * update the existing conference call object for that connection. |
| 265 | * If it is not a conference call but a previous associated conference call still exists, |
| 266 | * we mark it as idle and remove it from the map. |
| 267 | * In both cases above, we add the Calls to be updated to the UI. |
| 268 | * @param connection The connection object to check. |
| 269 | * @param updatedCalls List of 'updated' calls that will be sent to the UI. |
| 270 | */ |
| 271 | private boolean updateForConferenceCalls(Connection connection, List<Call> updatedCalls) { |
| 272 | // We consider this connection a conference connection if the call it |
| 273 | // belongs to is a multiparty call AND it is the first connection. |
| 274 | final boolean isConferenceCallConnection = isPartOfLiveConferenceCall(connection) && |
| 275 | connection.getCall().getEarliestConnection() == connection; |
| 276 | |
| 277 | boolean changed = false; |
| 278 | |
| 279 | // If this connection is the main connection for the conference call, then create or update |
| 280 | // a Call object for that conference call. |
| 281 | if (isConferenceCallConnection) { |
| 282 | final Call confCall = getCallFromMap(mConfCallMap, connection, true); |
| 283 | changed = updateCallFromConnection(confCall, connection, true); |
| 284 | |
| 285 | if (changed) { |
| 286 | updatedCalls.add(confCall); |
| 287 | } |
| 288 | |
| 289 | if (DBG) Log.d(TAG, "Updating a conference call: " + confCall); |
| 290 | |
| 291 | // It is possible that through a conference call split, there may be lingering conference |
| 292 | // calls where this connection was the main connection. We clean those up here. |
| 293 | } else { |
| 294 | final Call oldConfCall = getCallFromMap(mConfCallMap, connection, false); |
| 295 | |
| 296 | // We found a conference call for this connection, which is no longer a conference call. |
| 297 | // Kill it! |
| 298 | if (oldConfCall != null) { |
| 299 | if (DBG) Log.d(TAG, "Cleaning up an old conference call: " + oldConfCall); |
| 300 | mConfCallMap.remove(connection); |
| 301 | oldConfCall.setState(State.IDLE); |
| 302 | changed = true; |
| 303 | |
| 304 | // add to the list of calls to update |
| 305 | updatedCalls.add(oldConfCall); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return changed; |
| 310 | } |
| 311 | |
| 312 | /** |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 313 | * Updates the Call properties to match the state of the connection object |
| 314 | * that it represents. |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 315 | * @param call The call object to update. |
| 316 | * @param connection The connection object from which to update call. |
| 317 | * @param isForConference There are slight differences in how we populate data for conference |
| 318 | * calls. This boolean tells us which method to use. |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 319 | */ |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 320 | private boolean updateCallFromConnection(Call call, Connection connection, |
| 321 | boolean isForConference) { |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 322 | boolean changed = false; |
| 323 | |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 324 | final int newState = translateStateFromTelephony(connection, isForConference); |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 325 | |
| 326 | if (call.getState() != newState) { |
| 327 | call.setState(newState); |
| 328 | changed = true; |
| 329 | } |
| 330 | |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 331 | final Call.DisconnectCause newDisconnectCause = |
| 332 | translateDisconnectCauseFromTelephony(connection.getDisconnectCause()); |
| 333 | if (call.getDisconnectCause() != newDisconnectCause) { |
| 334 | call.setDisconnectCause(newDisconnectCause); |
| 335 | changed = true; |
| 336 | } |
| 337 | |
Santos Cordon | bbe8ecf | 2013-08-13 15:26:18 -0700 | [diff] [blame] | 338 | final long oldConnectTime = call.getConnectTime(); |
| 339 | if (oldConnectTime != connection.getConnectTime()) { |
| 340 | call.setConnectTime(connection.getConnectTime()); |
| 341 | changed = true; |
| 342 | } |
| 343 | |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 344 | if (!isForConference) { |
| 345 | final String oldNumber = call.getNumber(); |
| 346 | if (TextUtils.isEmpty(oldNumber) || !oldNumber.equals(connection.getAddress())) { |
| 347 | call.setNumber(connection.getAddress()); |
| 348 | changed = true; |
| 349 | } |
| 350 | |
| 351 | final int newNumberPresentation = connection.getNumberPresentation(); |
| 352 | if (call.getNumberPresentation() != newNumberPresentation) { |
| 353 | call.setNumberPresentation(newNumberPresentation); |
| 354 | changed = true; |
| 355 | } |
| 356 | |
| 357 | final int newCnapNamePresentation = connection.getCnapNamePresentation(); |
| 358 | if (call.getCnapNamePresentation() != newCnapNamePresentation) { |
| 359 | call.setCnapNamePresentation(newCnapNamePresentation); |
| 360 | changed = true; |
| 361 | } |
| 362 | |
| 363 | final String oldCnapName = call.getCnapName(); |
| 364 | if (TextUtils.isEmpty(oldCnapName) || !oldCnapName.equals(connection.getCnapName())) { |
| 365 | call.setCnapName(connection.getCnapName()); |
| 366 | changed = true; |
| 367 | } |
| 368 | } else { |
| 369 | |
| 370 | // update the list of children by: |
| 371 | // 1) Saving the old set |
| 372 | // 2) Removing all children |
| 373 | // 3) Adding the correct children into the Call |
| 374 | // 4) Comparing the new children set with the old children set |
| 375 | ImmutableSortedSet<Integer> oldSet = call.getChildCallIds(); |
| 376 | call.removeAllChildren(); |
| 377 | |
| 378 | if (connection.getCall() != null) { |
| 379 | for (Connection childConn : connection.getCall().getConnections()) { |
| 380 | final Call childCall = getCallFromMap(mCallMap, childConn, false); |
| 381 | if (childCall != null && childConn.isAlive()) { |
| 382 | call.addChildId(childCall.getCallId()); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | changed |= oldSet.equals(call.getChildCallIds()); |
| 387 | } |
| 388 | |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 389 | /** |
| 390 | * !!! Uses values from connection and call collected above so this part must be last !!! |
| 391 | */ |
| 392 | final int newCapabilities = getCapabilitiesFor(connection, call); |
Santos Cordon | 26e7b24 | 2013-08-07 21:15:45 -0700 | [diff] [blame] | 393 | if (call.getCapabilities() != newCapabilities) { |
| 394 | call.setCapabilities(newCapabilities); |
| 395 | changed = true; |
| 396 | } |
| 397 | |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 398 | return changed; |
| 399 | } |
| 400 | |
Santos Cordon | 26e7b24 | 2013-08-07 21:15:45 -0700 | [diff] [blame] | 401 | /** |
| 402 | * Returns a mask of capabilities for the connection such as merge, hold, etc. |
| 403 | */ |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 404 | private int getCapabilitiesFor(Connection connection, Call call) { |
| 405 | final boolean callIsActive = (call.getState() == Call.State.ACTIVE); |
| 406 | final Phone phone = connection.getCall().getPhone(); |
| 407 | |
| 408 | final boolean canHold = TelephonyCapabilities.supportsAnswerAndHold(phone); |
| 409 | boolean canAddCall = false; |
| 410 | boolean canMergeCall = false; |
| 411 | boolean canSwapCall = false; |
| 412 | |
| 413 | // only applies to active calls |
| 414 | if (callIsActive) { |
| 415 | canAddCall = PhoneUtils.okToAddCall(mCallManager); |
| 416 | canMergeCall = PhoneUtils.okToMergeCalls(mCallManager); |
| 417 | canSwapCall = PhoneUtils.okToSwapCalls(mCallManager); |
| 418 | } |
| 419 | |
| 420 | // special rules section! |
| 421 | // CDMA always has Add |
| 422 | if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) { |
| 423 | canAddCall = true; |
| 424 | } else { |
| 425 | // if neither merge nor add is on...then allow add |
| 426 | canAddCall |= !(canAddCall || canMergeCall); |
| 427 | } |
| 428 | |
Santos Cordon | 26e7b24 | 2013-08-07 21:15:45 -0700 | [diff] [blame] | 429 | int retval = 0x0; |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 430 | if (canHold) { |
Santos Cordon | 26e7b24 | 2013-08-07 21:15:45 -0700 | [diff] [blame] | 431 | retval |= Capabilities.HOLD; |
| 432 | } |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 433 | if (canAddCall) { |
| 434 | retval |= Capabilities.ADD_CALL; |
| 435 | } |
| 436 | if (canMergeCall) { |
| 437 | retval |= Capabilities.MERGE_CALLS; |
| 438 | } |
| 439 | if (canSwapCall) { |
| 440 | retval |= Capabilities.SWAP_CALLS; |
| 441 | } |
Santos Cordon | 26e7b24 | 2013-08-07 21:15:45 -0700 | [diff] [blame] | 442 | |
| 443 | return retval; |
| 444 | } |
| 445 | |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 446 | /** |
| 447 | * Returns true if the Connection is part of a multiparty call. |
| 448 | * We do this by checking the isMultiparty() method of the telephony.Call object and also |
| 449 | * checking to see if more than one of it's children is alive. |
| 450 | */ |
| 451 | private boolean isPartOfLiveConferenceCall(Connection connection) { |
| 452 | if (connection.getCall() != null && connection.getCall().isMultiparty()) { |
| 453 | int count = 0; |
| 454 | for (Connection currConn : connection.getCall().getConnections()) { |
| 455 | if (currConn.isAlive()) { |
| 456 | count++; |
| 457 | if (count >= 2) { |
| 458 | return true; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | private int translateStateFromTelephony(Connection connection, boolean isForConference) { |
| 467 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 468 | int retval = State.IDLE; |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 469 | switch (connection.getState()) { |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 470 | case ACTIVE: |
| 471 | retval = State.ACTIVE; |
| 472 | break; |
| 473 | case INCOMING: |
| 474 | retval = State.INCOMING; |
| 475 | break; |
| 476 | case DIALING: |
| 477 | case ALERTING: |
| 478 | retval = State.DIALING; |
| 479 | break; |
| 480 | case WAITING: |
| 481 | retval = State.CALL_WAITING; |
| 482 | break; |
| 483 | case HOLDING: |
| 484 | retval = State.ONHOLD; |
| 485 | break; |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 486 | case DISCONNECTED: |
| 487 | case DISCONNECTING: |
| 488 | retval = State.DISCONNECTED; |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 489 | default: |
| 490 | } |
| 491 | |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 492 | // If we are dealing with a potential child call (not the parent conference call), |
| 493 | // the check to see if we have to set the state to CONFERENCED. |
| 494 | if (!isForConference) { |
| 495 | |
| 496 | // if the connection is part of a multiparty call, and it is live, |
| 497 | // annotate it with CONFERENCED state instead. |
| 498 | if (isPartOfLiveConferenceCall(connection) && connection.isAlive()) { |
| 499 | return State.CONFERENCED; |
| 500 | } |
| 501 | } |
| 502 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 503 | return retval; |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 506 | private final ImmutableMap<Connection.DisconnectCause, Call.DisconnectCause> CAUSE_MAP = |
| 507 | ImmutableMap.<Connection.DisconnectCause, Call.DisconnectCause>builder() |
| 508 | .put(Connection.DisconnectCause.BUSY, Call.DisconnectCause.BUSY) |
| 509 | .put(Connection.DisconnectCause.CALL_BARRED, Call.DisconnectCause.CALL_BARRED) |
| 510 | .put(Connection.DisconnectCause.CDMA_ACCESS_BLOCKED, |
| 511 | Call.DisconnectCause.CDMA_ACCESS_BLOCKED) |
| 512 | .put(Connection.DisconnectCause.CDMA_ACCESS_FAILURE, |
| 513 | Call.DisconnectCause.CDMA_ACCESS_FAILURE) |
| 514 | .put(Connection.DisconnectCause.CDMA_DROP, Call.DisconnectCause.CDMA_DROP) |
| 515 | .put(Connection.DisconnectCause.CDMA_INTERCEPT, Call.DisconnectCause.CDMA_INTERCEPT) |
| 516 | .put(Connection.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE, |
| 517 | Call.DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE) |
| 518 | .put(Connection.DisconnectCause.CDMA_NOT_EMERGENCY, |
| 519 | Call.DisconnectCause.CDMA_NOT_EMERGENCY) |
| 520 | .put(Connection.DisconnectCause.CDMA_PREEMPTED, Call.DisconnectCause.CDMA_PREEMPTED) |
| 521 | .put(Connection.DisconnectCause.CDMA_REORDER, Call.DisconnectCause.CDMA_REORDER) |
| 522 | .put(Connection.DisconnectCause.CDMA_RETRY_ORDER, |
| 523 | Call.DisconnectCause.CDMA_RETRY_ORDER) |
| 524 | .put(Connection.DisconnectCause.CDMA_SO_REJECT, Call.DisconnectCause.CDMA_SO_REJECT) |
| 525 | .put(Connection.DisconnectCause.CONGESTION, Call.DisconnectCause.CONGESTION) |
| 526 | .put(Connection.DisconnectCause.CS_RESTRICTED, Call.DisconnectCause.CS_RESTRICTED) |
| 527 | .put(Connection.DisconnectCause.CS_RESTRICTED_EMERGENCY, |
| 528 | Call.DisconnectCause.CS_RESTRICTED_EMERGENCY) |
| 529 | .put(Connection.DisconnectCause.CS_RESTRICTED_NORMAL, |
| 530 | Call.DisconnectCause.CS_RESTRICTED_NORMAL) |
| 531 | .put(Connection.DisconnectCause.ERROR_UNSPECIFIED, |
| 532 | Call.DisconnectCause.ERROR_UNSPECIFIED) |
| 533 | .put(Connection.DisconnectCause.FDN_BLOCKED, Call.DisconnectCause.FDN_BLOCKED) |
| 534 | .put(Connection.DisconnectCause.ICC_ERROR, Call.DisconnectCause.ICC_ERROR) |
| 535 | .put(Connection.DisconnectCause.INCOMING_MISSED, |
| 536 | Call.DisconnectCause.INCOMING_MISSED) |
| 537 | .put(Connection.DisconnectCause.INCOMING_REJECTED, |
| 538 | Call.DisconnectCause.INCOMING_REJECTED) |
| 539 | .put(Connection.DisconnectCause.INVALID_CREDENTIALS, |
| 540 | Call.DisconnectCause.INVALID_CREDENTIALS) |
| 541 | .put(Connection.DisconnectCause.INVALID_NUMBER, |
| 542 | Call.DisconnectCause.INVALID_NUMBER) |
| 543 | .put(Connection.DisconnectCause.LIMIT_EXCEEDED, Call.DisconnectCause.LIMIT_EXCEEDED) |
| 544 | .put(Connection.DisconnectCause.LOCAL, Call.DisconnectCause.LOCAL) |
| 545 | .put(Connection.DisconnectCause.LOST_SIGNAL, Call.DisconnectCause.LOST_SIGNAL) |
| 546 | .put(Connection.DisconnectCause.MMI, Call.DisconnectCause.MMI) |
| 547 | .put(Connection.DisconnectCause.NORMAL, Call.DisconnectCause.NORMAL) |
| 548 | .put(Connection.DisconnectCause.NOT_DISCONNECTED, |
| 549 | Call.DisconnectCause.NOT_DISCONNECTED) |
| 550 | .put(Connection.DisconnectCause.NUMBER_UNREACHABLE, |
| 551 | Call.DisconnectCause.NUMBER_UNREACHABLE) |
| 552 | .put(Connection.DisconnectCause.OUT_OF_NETWORK, Call.DisconnectCause.OUT_OF_NETWORK) |
| 553 | .put(Connection.DisconnectCause.OUT_OF_SERVICE, Call.DisconnectCause.OUT_OF_SERVICE) |
| 554 | .put(Connection.DisconnectCause.POWER_OFF, Call.DisconnectCause.POWER_OFF) |
| 555 | .put(Connection.DisconnectCause.SERVER_ERROR, Call.DisconnectCause.SERVER_ERROR) |
| 556 | .put(Connection.DisconnectCause.SERVER_UNREACHABLE, |
| 557 | Call.DisconnectCause.SERVER_UNREACHABLE) |
| 558 | .put(Connection.DisconnectCause.TIMED_OUT, Call.DisconnectCause.TIMED_OUT) |
| 559 | .put(Connection.DisconnectCause.UNOBTAINABLE_NUMBER, |
| 560 | Call.DisconnectCause.UNOBTAINABLE_NUMBER) |
| 561 | .build(); |
| 562 | |
| 563 | private Call.DisconnectCause translateDisconnectCauseFromTelephony( |
| 564 | Connection.DisconnectCause causeSource) { |
| 565 | |
| 566 | if (CAUSE_MAP.containsKey(causeSource)) { |
| 567 | return CAUSE_MAP.get(causeSource); |
| 568 | } |
| 569 | |
| 570 | return Call.DisconnectCause.UNKNOWN; |
| 571 | } |
| 572 | |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 573 | /** |
Santos Cordon | e38b1ff | 2013-08-07 12:12:16 -0700 | [diff] [blame] | 574 | * Gets an existing callId for a connection, or creates one if none exists. |
| 575 | * This function does NOT set any of the Connection data onto the Call class. |
| 576 | * A separate call to updateCallFromConnection must be made for that purpose. |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 577 | */ |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 578 | private Call getCallFromMap(HashMap<Connection, Call> map, Connection conn, |
| 579 | boolean createIfMissing) { |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 580 | Call call = null; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 581 | |
| 582 | // Find the call id or create if missing and requested. |
| 583 | if (conn != null) { |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 584 | if (map.containsKey(conn)) { |
| 585 | call = map.get(conn); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 586 | } else if (createIfMissing) { |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 587 | call = createNewCall(); |
| 588 | map.put(conn, call); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 591 | return call; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /** |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 595 | * Creates a brand new connection for the call. |
| 596 | */ |
| 597 | private Call createNewCall() { |
| 598 | int callId; |
| 599 | int newNextCallId; |
| 600 | do { |
| 601 | callId = mNextCallId.get(); |
| 602 | |
| 603 | // protect against overflow |
| 604 | newNextCallId = (callId == Integer.MAX_VALUE ? |
| 605 | CALL_ID_START_VALUE : callId + 1); |
| 606 | |
| 607 | // Keep looping if the change was not atomic OR the value is already taken. |
| 608 | // The call to containsValue() is linear, however, most devices support a |
| 609 | // maximum of 7 connections so it's not expensive. |
| 610 | } while (!mNextCallId.compareAndSet(callId, newNextCallId)); |
| 611 | |
| 612 | return new Call(callId); |
| 613 | } |
| 614 | |
| 615 | /** |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 616 | * Listener interface for changes to Calls. |
| 617 | */ |
| 618 | public interface Listener { |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 619 | void onDisconnect(Call call); |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 620 | void onIncoming(Call call, ArrayList<String> textReponses); |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 621 | void onUpdate(List<Call> calls, boolean fullUpdate); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 622 | } |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 623 | |
| 624 | /** |
| 625 | * Result class for accessing a call by connection. |
| 626 | */ |
| 627 | public static class CallResult { |
| 628 | public Call mCall; |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 629 | public Call mActionableCall; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 630 | public Connection mConnection; |
| 631 | |
| 632 | private CallResult(Call call, Connection connection) { |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 633 | this(call, call, connection); |
| 634 | } |
| 635 | |
| 636 | private CallResult(Call call, Call actionableCall, Connection connection) { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 637 | mCall = call; |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 638 | mActionableCall = actionableCall; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 639 | mConnection = connection; |
| 640 | } |
| 641 | |
| 642 | public Call getCall() { |
| 643 | return mCall; |
| 644 | } |
| 645 | |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame^] | 646 | // The call that should be used for call actions like hanging up. |
| 647 | public Call getActionableCall() { |
| 648 | return mActionableCall; |
| 649 | } |
| 650 | |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 651 | public Connection getConnection() { |
| 652 | return mConnection; |
| 653 | } |
| 654 | } |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 655 | } |