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