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