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