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