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