Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 18 | |
Santos Cordon | f0f99f3 | 2016-02-18 16:13:57 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Tyler Gunn | eaaf074 | 2016-09-15 15:36:38 -0700 | [diff] [blame] | 20 | import android.app.NotificationManager; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Tony Mak | a993094 | 2016-01-15 10:57:14 +0000 | [diff] [blame] | 22 | import android.content.pm.UserInfo; |
Xueren Zhang | c0e8e9d | 2015-03-06 14:16:55 +0100 | [diff] [blame] | 23 | import android.content.Intent; |
Hall Liu | fbed016 | 2016-01-14 18:06:29 -0800 | [diff] [blame] | 24 | import android.media.AudioManager; |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 26 | import android.os.Bundle; |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 27 | import android.os.Handler; |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 28 | import android.os.Looper; |
Tony Mak | a993094 | 2016-01-15 10:57:14 +0000 | [diff] [blame] | 29 | import android.os.Process; |
Sailesh Nepal | 703a1af | 2016-04-14 20:10:12 -0700 | [diff] [blame] | 30 | import android.os.SystemClock; |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 31 | import android.os.SystemProperties; |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 32 | import android.os.SystemVibrator; |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 33 | import android.os.Trace; |
Tony Mak | 578a4e6 | 2015-11-23 11:18:51 +0000 | [diff] [blame] | 34 | import android.os.UserHandle; |
Tony Mak | a993094 | 2016-01-15 10:57:14 +0000 | [diff] [blame] | 35 | import android.os.UserManager; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 36 | import android.provider.CallLog.Calls; |
Hall Liu | f1422e7 | 2016-01-27 11:07:07 -0800 | [diff] [blame] | 37 | import android.provider.Settings; |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 38 | import android.telecom.CallAudioState; |
Tyler Gunn | d92e737 | 2015-01-09 15:59:45 -0800 | [diff] [blame] | 39 | import android.telecom.Conference; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 40 | import android.telecom.Connection; |
Andrew Lee | 701dc00 | 2014-09-11 21:29:12 -0700 | [diff] [blame] | 41 | import android.telecom.DisconnectCause; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 42 | import android.telecom.GatewayInfo; |
| 43 | import android.telecom.ParcelableConference; |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 44 | import android.telecom.ParcelableConnection; |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 45 | import android.telecom.PhoneAccount; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 46 | import android.telecom.PhoneAccountHandle; |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 47 | import android.telecom.TelecomManager; |
Andrew Lee | 4550623 | 2014-10-22 17:54:33 -0700 | [diff] [blame] | 48 | import android.telecom.VideoProfile; |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 49 | import android.telephony.PhoneNumberUtils; |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 50 | import android.telephony.TelephonyManager; |
Yorke Lee | 8d3f269 | 2015-05-08 11:44:39 -0700 | [diff] [blame] | 51 | import android.text.TextUtils; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 52 | |
Ihab Awad | a365390 | 2015-01-23 13:44:46 -0800 | [diff] [blame] | 53 | import com.android.internal.annotations.VisibleForTesting; |
Abhijith Shastry | cf27f68 | 2016-03-01 18:28:44 -0800 | [diff] [blame] | 54 | import com.android.internal.telephony.AsyncEmergencyContactNotifier; |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 55 | import com.android.internal.telephony.PhoneConstants; |
| 56 | import com.android.internal.telephony.TelephonyProperties; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 57 | import com.android.internal.util.IndentingPrintWriter; |
Santos Cordon | 501b9b3 | 2016-03-07 14:40:07 -0800 | [diff] [blame] | 58 | import com.android.server.telecom.TelecomServiceImpl.DefaultDialerManagerAdapter; |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 59 | import com.android.server.telecom.callfiltering.AsyncBlockCheckFilter; |
| 60 | import com.android.server.telecom.callfiltering.BlockCheckerAdapter; |
| 61 | import com.android.server.telecom.callfiltering.CallFilterResultCallback; |
| 62 | import com.android.server.telecom.callfiltering.CallFilteringResult; |
| 63 | import com.android.server.telecom.callfiltering.CallScreeningServiceFilter; |
| 64 | import com.android.server.telecom.callfiltering.DirectToVoicemailCallFilter; |
| 65 | import com.android.server.telecom.callfiltering.IncomingCallFilter; |
Xueren Zhang | c0e8e9d | 2015-03-06 14:16:55 +0100 | [diff] [blame] | 66 | import com.android.server.telecom.components.ErrorDialogActivity; |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 67 | |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 68 | import java.util.ArrayList; |
Santos Cordon | 4bc0245 | 2014-11-19 15:51:12 -0800 | [diff] [blame] | 69 | import java.util.Collection; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 70 | import java.util.Collections; |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 71 | import java.util.HashMap; |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 72 | import java.util.HashSet; |
Brad Ebinger | e680e66 | 2016-08-26 11:45:43 -0700 | [diff] [blame] | 73 | import java.util.Iterator; |
Santos Cordon | 7cdb909 | 2014-07-24 21:33:33 -0700 | [diff] [blame] | 74 | import java.util.List; |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 75 | import java.util.Map; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 76 | import java.util.Objects; |
Tyler Gunn | 9b618b8 | 2016-10-17 15:54:35 -0700 | [diff] [blame] | 77 | import java.util.Optional; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 78 | import java.util.Set; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 79 | import java.util.concurrent.ConcurrentHashMap; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 80 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Singleton. |
| 83 | * |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 84 | * NOTE: by design most APIs are package private, use the relevant adapter/s to allow |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 85 | * access from other packages specifically refraining from passing the CallsManager instance |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 86 | * beyond the com.android.server.telecom package boundary. |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 87 | */ |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 88 | @VisibleForTesting |
Sailesh Nepal | ae92595 | 2016-01-24 18:56:58 -0800 | [diff] [blame] | 89 | public class CallsManager extends Call.ListenerBase |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 90 | implements VideoProviderProxy.Listener, CallFilterResultCallback { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 91 | |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 92 | // TODO: Consider renaming this CallsManagerPlugin. |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 93 | @VisibleForTesting |
| 94 | public interface CallsManagerListener { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 95 | void onCallAdded(Call call); |
| 96 | void onCallRemoved(Call call); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 97 | void onCallStateChanged(Call call, int oldState, int newState); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 98 | void onConnectionServiceChanged( |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 99 | Call call, |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 100 | ConnectionServiceWrapper oldService, |
| 101 | ConnectionServiceWrapper newService); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 102 | void onIncomingCallAnswered(Call call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 103 | void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage); |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 104 | void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState); |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 105 | void onRingbackRequested(Call call, boolean ringback); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 106 | void onIsConferencedChanged(Call call); |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 107 | void onIsVoipAudioModeChanged(Call call); |
Hall Liu | 9696c21 | 2016-06-24 16:09:02 -0700 | [diff] [blame] | 108 | void onVideoStateChanged(Call call, int previousVideoState, int newVideoState); |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 109 | void onCanAddCallChanged(boolean canAddCall); |
Tyler Gunn | 86014fc | 2015-06-12 14:31:25 -0700 | [diff] [blame] | 110 | void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile); |
Tyler Gunn | db82191 | 2016-02-16 14:35:25 -0800 | [diff] [blame] | 111 | void onHoldToneRequested(Call call); |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 112 | void onExternalCallChanged(Call call, boolean isExternalCall); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 115 | private static final String TAG = "CallsManager"; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 116 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 117 | private static final int MAXIMUM_LIVE_CALLS = 1; |
| 118 | private static final int MAXIMUM_HOLD_CALLS = 1; |
| 119 | private static final int MAXIMUM_RINGING_CALLS = 1; |
Roshan Pius | 4de4a89 | 2015-08-24 11:06:58 -0700 | [diff] [blame] | 120 | private static final int MAXIMUM_DIALING_CALLS = 1; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 121 | private static final int MAXIMUM_OUTGOING_CALLS = 1; |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 122 | private static final int MAXIMUM_TOP_LEVEL_CALLS = 2; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 123 | |
| 124 | private static final int[] OUTGOING_CALL_STATES = |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 125 | {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING, |
| 126 | CallState.PULLING}; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 127 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 128 | private static final int[] LIVE_CALL_STATES = |
Santos Cordon | f0f99f3 | 2016-02-18 16:13:57 -0800 | [diff] [blame] | 129 | {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING, |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 130 | CallState.PULLING, CallState.ACTIVE}; |
| 131 | |
Tyler Gunn | 8452be0 | 2015-09-17 09:57:02 -0700 | [diff] [blame] | 132 | public static final String TELECOM_CALL_ID_PREFIX = "TC@"; |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 133 | |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 134 | // Maps call technologies in PhoneConstants to those in Analytics. |
| 135 | private static final Map<Integer, Integer> sAnalyticsTechnologyMap; |
| 136 | static { |
| 137 | sAnalyticsTechnologyMap = new HashMap<>(5); |
| 138 | sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_CDMA, Analytics.CDMA_PHONE); |
| 139 | sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_GSM, Analytics.GSM_PHONE); |
| 140 | sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_IMS, Analytics.IMS_PHONE); |
| 141 | sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_SIP, Analytics.SIP_PHONE); |
| 142 | sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_THIRD_PARTY, |
| 143 | Analytics.THIRD_PARTY_PHONE); |
| 144 | } |
| 145 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 146 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 147 | * The main call repository. Keeps an instance of all live calls. New incoming and outgoing |
| 148 | * calls are added to the map and removed when the calls move to the disconnected state. |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 149 | * |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 150 | * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is |
| 151 | * load factor before resizing, 1 means we only expect a single thread to |
| 152 | * access the map so make only a single shard |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 153 | */ |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 154 | private final Set<Call> mCalls = Collections.newSetFromMap( |
| 155 | new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1)); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 156 | |
Tyler Gunn | 8452be0 | 2015-09-17 09:57:02 -0700 | [diff] [blame] | 157 | /** |
| 158 | * The current telecom call ID. Used when creating new instances of {@link Call}. Should |
| 159 | * only be accessed using the {@link #getNextCallId()} method which synchronizes on the |
| 160 | * {@link #mLock} sync root. |
| 161 | */ |
| 162 | private int mCallId = 0; |
| 163 | |
Santos Cordon | f0f99f3 | 2016-02-18 16:13:57 -0800 | [diff] [blame] | 164 | /** |
| 165 | * Stores the current foreground user. |
| 166 | */ |
| 167 | private UserHandle mCurrentUserHandle = UserHandle.of(ActivityManager.getCurrentUser()); |
| 168 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 169 | private final ConnectionServiceRepository mConnectionServiceRepository; |
| 170 | private final DtmfLocalTonePlayer mDtmfLocalTonePlayer; |
| 171 | private final InCallController mInCallController; |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 172 | private final CallAudioManager mCallAudioManager; |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 173 | private RespondViaSmsManager mRespondViaSmsManager; |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 174 | private final Ringer mRinger; |
Santos Cordon | 61e6f35 | 2014-12-03 02:53:34 -0800 | [diff] [blame] | 175 | private final InCallWakeLockController mInCallWakeLockController; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 176 | // For this set initial table size to 16 because we add 13 listeners in |
| 177 | // the CallsManager constructor. |
| 178 | private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap( |
| 179 | new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1)); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 180 | private final HeadsetMediaButton mHeadsetMediaButton; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 181 | private final WiredHeadsetManager mWiredHeadsetManager; |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 182 | private final BluetoothManager mBluetoothManager; |
Santos Cordon | a04bdca | 2015-03-04 16:57:55 -0800 | [diff] [blame] | 183 | private final DockManager mDockManager; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 184 | private final TtyManager mTtyManager; |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 185 | private final ProximitySensorManager mProximitySensorManager; |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 186 | private final PhoneStateBroadcaster mPhoneStateBroadcaster; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 187 | private final CallLogManager mCallLogManager; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 188 | private final Context mContext; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 189 | private final TelecomSystem.SyncRoot mLock; |
| 190 | private final ContactsAsyncHelper mContactsAsyncHelper; |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 191 | private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 192 | private final PhoneAccountRegistrar mPhoneAccountRegistrar; |
| 193 | private final MissedCallNotifier mMissedCallNotifier; |
Hall Liu | 5b70c1c | 2016-03-03 18:42:57 -0800 | [diff] [blame] | 194 | private final CallerInfoLookupHelper mCallerInfoLookupHelper; |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 195 | private final DefaultDialerManagerAdapter mDefaultDialerManagerAdapter; |
| 196 | private final Timeouts.Adapter mTimeoutsAdapter; |
Brad Ebinger | 6e8f3d7 | 2016-06-20 11:35:42 -0700 | [diff] [blame] | 197 | private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter; |
Tyler Gunn | eaaf074 | 2016-09-15 15:36:38 -0700 | [diff] [blame] | 198 | private final NotificationManager mNotificationManager; |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 199 | private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>(); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 200 | private final Set<Call> mPendingCallsToDisconnect = new HashSet<>(); |
| 201 | /* Handler tied to thread in which CallManager was initialized. */ |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 202 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 203 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 204 | private boolean mCanAddCall = true; |
| 205 | |
Brad Ebinger | 3da5764 | 2016-04-04 17:32:19 -0700 | [diff] [blame] | 206 | private TelephonyManager.MultiSimVariants mRadioSimVariants = null; |
| 207 | |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 208 | private Runnable mStopTone; |
| 209 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 210 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 211 | * Initializes the required Telecom components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 212 | */ |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 213 | CallsManager( |
| 214 | Context context, |
| 215 | TelecomSystem.SyncRoot lock, |
| 216 | ContactsAsyncHelper contactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 217 | CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 218 | MissedCallNotifier missedCallNotifier, |
| 219 | PhoneAccountRegistrar phoneAccountRegistrar, |
| 220 | HeadsetMediaButtonFactory headsetMediaButtonFactory, |
| 221 | ProximitySensorManagerFactory proximitySensorManagerFactory, |
Hall Liu | 8fb1fb7 | 2015-10-22 15:24:40 -0700 | [diff] [blame] | 222 | InCallWakeLockControllerFactory inCallWakeLockControllerFactory, |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 223 | CallAudioManager.AudioServiceFactory audioServiceFactory, |
| 224 | BluetoothManager bluetoothManager, |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 225 | WiredHeadsetManager wiredHeadsetManager, |
Santos Cordon | 501b9b3 | 2016-03-07 14:40:07 -0800 | [diff] [blame] | 226 | SystemStateProvider systemStateProvider, |
Brad Ebinger | 7ade5e2 | 2016-04-05 18:43:50 -0700 | [diff] [blame] | 227 | DefaultDialerManagerAdapter defaultDialerAdapter, |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 228 | Timeouts.Adapter timeoutsAdapter, |
Brad Ebinger | 6e8f3d7 | 2016-06-20 11:35:42 -0700 | [diff] [blame] | 229 | AsyncRingtonePlayer asyncRingtonePlayer, |
Tyler Gunn | eaaf074 | 2016-09-15 15:36:38 -0700 | [diff] [blame] | 230 | PhoneNumberUtilsAdapter phoneNumberUtilsAdapter, |
| 231 | InterruptionFilterProxy interruptionFilterProxy) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 232 | mContext = context; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 233 | mLock = lock; |
Brad Ebinger | 6e8f3d7 | 2016-06-20 11:35:42 -0700 | [diff] [blame] | 234 | mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 235 | mContactsAsyncHelper = contactsAsyncHelper; |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 236 | mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 237 | mPhoneAccountRegistrar = phoneAccountRegistrar; |
| 238 | mMissedCallNotifier = missedCallNotifier; |
| 239 | StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this); |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 240 | mWiredHeadsetManager = wiredHeadsetManager; |
| 241 | mBluetoothManager = bluetoothManager; |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 242 | mDefaultDialerManagerAdapter = defaultDialerAdapter; |
Santos Cordon | a04bdca | 2015-03-04 16:57:55 -0800 | [diff] [blame] | 243 | mDockManager = new DockManager(context); |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 244 | mTimeoutsAdapter = timeoutsAdapter; |
Hall Liu | 5b70c1c | 2016-03-03 18:42:57 -0800 | [diff] [blame] | 245 | mCallerInfoLookupHelper = new CallerInfoLookupHelper(context, mCallerInfoAsyncQueryFactory, |
Hall Liu | 4659827 | 2016-04-05 12:58:51 -0700 | [diff] [blame] | 246 | mContactsAsyncHelper, mLock); |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 247 | |
| 248 | mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(); |
Tyler Gunn | eaaf074 | 2016-09-15 15:36:38 -0700 | [diff] [blame] | 249 | mNotificationManager = (NotificationManager) context.getSystemService( |
| 250 | Context.NOTIFICATION_SERVICE); |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 251 | CallAudioRouteStateMachine callAudioRouteStateMachine = new CallAudioRouteStateMachine( |
| 252 | context, |
| 253 | this, |
| 254 | bluetoothManager, |
| 255 | wiredHeadsetManager, |
| 256 | statusBarNotifier, |
Hall Liu | a3e9dda | 2015-12-15 17:52:50 -0800 | [diff] [blame] | 257 | audioServiceFactory, |
Tyler Gunn | eaaf074 | 2016-09-15 15:36:38 -0700 | [diff] [blame] | 258 | interruptionFilterProxy, |
Hall Liu | a3e9dda | 2015-12-15 17:52:50 -0800 | [diff] [blame] | 259 | CallAudioRouteStateMachine.doesDeviceSupportEarpieceRoute() |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 260 | ); |
Hall Liu | 2985570 | 2015-12-11 17:42:03 -0800 | [diff] [blame] | 261 | callAudioRouteStateMachine.initialize(); |
| 262 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 263 | CallAudioRoutePeripheralAdapter callAudioRoutePeripheralAdapter = |
| 264 | new CallAudioRoutePeripheralAdapter( |
| 265 | callAudioRouteStateMachine, |
| 266 | bluetoothManager, |
| 267 | wiredHeadsetManager, |
| 268 | mDockManager); |
| 269 | |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 270 | InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory( |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 271 | callAudioRoutePeripheralAdapter, lock); |
| 272 | |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 273 | SystemSettingsUtil systemSettingsUtil = new SystemSettingsUtil(); |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 274 | RingtoneFactory ringtoneFactory = new RingtoneFactory(this, context); |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 275 | SystemVibrator systemVibrator = new SystemVibrator(context); |
Santos Cordon | 501b9b3 | 2016-03-07 14:40:07 -0800 | [diff] [blame] | 276 | mInCallController = new InCallController( |
Hall Liu | 28b82f0 | 2016-07-26 17:38:56 -0700 | [diff] [blame] | 277 | context, mLock, this, systemStateProvider, defaultDialerAdapter, mTimeoutsAdapter); |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 278 | mRinger = new Ringer(playerFactory, context, systemSettingsUtil, asyncRingtonePlayer, |
Santos Cordon | f0f99f3 | 2016-02-18 16:13:57 -0800 | [diff] [blame] | 279 | ringtoneFactory, systemVibrator, mInCallController); |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 280 | |
| 281 | mCallAudioManager = new CallAudioManager(callAudioRouteStateMachine, |
Hall Liu | fbed016 | 2016-01-14 18:06:29 -0800 | [diff] [blame] | 282 | this,new CallAudioModeStateMachine((AudioManager) |
| 283 | mContext.getSystemService(Context.AUDIO_SERVICE)), |
| 284 | playerFactory, mRinger, new RingbackPlayer(playerFactory), mDtmfLocalTonePlayer); |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 285 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 286 | mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 287 | mTtyManager = new TtyManager(context, mWiredHeadsetManager); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 288 | mProximitySensorManager = proximitySensorManagerFactory.create(context, this); |
| 289 | mPhoneStateBroadcaster = new PhoneStateBroadcaster(this); |
Ta-wei Yen | 982c0bd | 2016-04-14 13:59:54 -0700 | [diff] [blame] | 290 | mCallLogManager = new CallLogManager(context, phoneAccountRegistrar, mMissedCallNotifier); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 291 | mConnectionServiceRepository = |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 292 | new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 293 | mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 294 | |
Tyler Gunn | ae694b1 | 2015-11-11 17:46:03 -0800 | [diff] [blame] | 295 | mListeners.add(mInCallWakeLockController); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 296 | mListeners.add(statusBarNotifier); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 297 | mListeners.add(mCallLogManager); |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 298 | mListeners.add(mPhoneStateBroadcaster); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 299 | mListeners.add(mInCallController); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 300 | mListeners.add(mCallAudioManager); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 301 | mListeners.add(missedCallNotifier); |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 302 | mListeners.add(mHeadsetMediaButton); |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 303 | mListeners.add(mProximitySensorManager); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 304 | |
Tony Mak | a993094 | 2016-01-15 10:57:14 +0000 | [diff] [blame] | 305 | // There is no USER_SWITCHED broadcast for user 0, handle it here explicitly. |
| 306 | final UserManager userManager = UserManager.get(mContext); |
| 307 | // Don't load missed call if it is run in split user model. |
| 308 | if (userManager.isPrimaryUser()) { |
| 309 | onUserSwitch(Process.myUserHandle()); |
| 310 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 313 | public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) { |
| 314 | if (mRespondViaSmsManager != null) { |
| 315 | mListeners.remove(mRespondViaSmsManager); |
| 316 | } |
| 317 | mRespondViaSmsManager = respondViaSmsManager; |
| 318 | mListeners.add(respondViaSmsManager); |
| 319 | } |
| 320 | |
| 321 | public RespondViaSmsManager getRespondViaSmsManager() { |
| 322 | return mRespondViaSmsManager; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Hall Liu | 5b70c1c | 2016-03-03 18:42:57 -0800 | [diff] [blame] | 325 | public CallerInfoLookupHelper getCallerInfoLookupHelper() { |
| 326 | return mCallerInfoLookupHelper; |
| 327 | } |
| 328 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 329 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 330 | public void onSuccessfulOutgoingCall(Call call, int callState) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 331 | Log.v(this, "onSuccessfulOutgoingCall, %s", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 332 | |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 333 | setCallState(call, callState, "successful outgoing call"); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 334 | if (!mCalls.contains(call)) { |
| 335 | // Call was not added previously in startOutgoingCall due to it being a potential MMI |
| 336 | // code, so add it now. |
| 337 | addCall(call); |
| 338 | } |
| 339 | |
| 340 | // The call's ConnectionService has been updated. |
| 341 | for (CallsManagerListener listener : mListeners) { |
| 342 | listener.onConnectionServiceChanged(call, null, call.getConnectionService()); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 343 | } |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 344 | |
| 345 | markCallAsDialing(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
Andrew Lee | 701dc00 | 2014-09-11 21:29:12 -0700 | [diff] [blame] | 349 | public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) { |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 350 | Log.v(this, "onFailedOutgoingCall, call: %s", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 351 | |
Andrew Lee | e6288a7 | 2014-10-01 13:50:02 -0700 | [diff] [blame] | 352 | markCallAsRemoved(call); |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | @Override |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 356 | public void onSuccessfulIncomingCall(Call incomingCall) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 357 | Log.d(this, "onSuccessfulIncomingCall"); |
Hall Liu | ddf3f9c | 2016-08-30 13:38:52 -0700 | [diff] [blame] | 358 | if (incomingCall.hasProperty(Connection.PROPERTY_EMERGENCY_CALLBACK_MODE)) { |
| 359 | Log.i(this, "Skipping call filtering due to ECBM"); |
| 360 | onCallFilteringComplete(incomingCall, new CallFilteringResult(true, false, true, true)); |
| 361 | return; |
| 362 | } |
| 363 | |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 364 | List<IncomingCallFilter.CallFilter> filters = new ArrayList<>(); |
| 365 | filters.add(new DirectToVoicemailCallFilter(mCallerInfoLookupHelper)); |
| 366 | filters.add(new AsyncBlockCheckFilter(mContext, new BlockCheckerAdapter())); |
| 367 | filters.add(new CallScreeningServiceFilter(mContext, this, mPhoneAccountRegistrar, |
| 368 | mDefaultDialerManagerAdapter, |
| 369 | new ParcelableCallUtils.Converter(), mLock)); |
| 370 | new IncomingCallFilter(mContext, this, incomingCall, mLock, |
| 371 | mTimeoutsAdapter, filters).performFiltering(); |
Sailesh Nepal | ae92595 | 2016-01-24 18:56:58 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 375 | public void onCallFilteringComplete(Call incomingCall, CallFilteringResult result) { |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 376 | // Only set the incoming call as ringing if it isn't already disconnected. It is possible |
Tyler Gunn | e6276e1 | 2015-10-22 20:48:50 -0700 | [diff] [blame] | 377 | // that the connection service disconnected the call before it was even added to Telecom, in |
| 378 | // which case it makes no sense to set it back to a ringing state. |
| 379 | if (incomingCall.getState() != CallState.DISCONNECTED && |
| 380 | incomingCall.getState() != CallState.DISCONNECTING) { |
| 381 | setCallState(incomingCall, CallState.RINGING, |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 382 | result.shouldAllowCall ? "successful incoming call" : "blocking call"); |
Tyler Gunn | e6276e1 | 2015-10-22 20:48:50 -0700 | [diff] [blame] | 383 | } else { |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 384 | Log.i(this, "onCallFilteringCompleted: call already disconnected."); |
Tyler Gunn | 2ecfd88 | 2016-09-16 15:32:11 -0700 | [diff] [blame] | 385 | return; |
Tyler Gunn | e6276e1 | 2015-10-22 20:48:50 -0700 | [diff] [blame] | 386 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 387 | |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 388 | if (result.shouldAllowCall) { |
Sailesh Nepal | ae92595 | 2016-01-24 18:56:58 -0800 | [diff] [blame] | 389 | if (hasMaximumRingingCalls()) { |
Wei Liu | 13791b9 | 2016-10-04 17:46:44 -0700 | [diff] [blame] | 390 | if (shouldSilenceInsteadOfReject(incomingCall)) { |
| 391 | incomingCall.silence(); |
| 392 | } else { |
| 393 | Log.i(this, "onCallFilteringCompleted: Call rejected! " + |
| 394 | "Exceeds maximum number of ringing calls."); |
| 395 | rejectCallAndLog(incomingCall); |
| 396 | } |
Sailesh Nepal | ae92595 | 2016-01-24 18:56:58 -0800 | [diff] [blame] | 397 | } else if (hasMaximumDialingCalls()) { |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 398 | Log.i(this, "onCallFilteringCompleted: Call rejected! Exceeds maximum number of " + |
Sailesh Nepal | ae92595 | 2016-01-24 18:56:58 -0800 | [diff] [blame] | 399 | "dialing calls."); |
| 400 | rejectCallAndLog(incomingCall); |
| 401 | } else { |
| 402 | addCall(incomingCall); |
| 403 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 404 | } else { |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 405 | if (result.shouldReject) { |
| 406 | Log.i(this, "onCallFilteringCompleted: blocked call, rejecting."); |
Sailesh Nepal | ae92595 | 2016-01-24 18:56:58 -0800 | [diff] [blame] | 407 | incomingCall.reject(false, null); |
| 408 | } |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 409 | if (result.shouldAddToCallLog) { |
Sailesh Nepal | ae92595 | 2016-01-24 18:56:58 -0800 | [diff] [blame] | 410 | Log.i(this, "onCallScreeningCompleted: blocked call, adding to call log."); |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 411 | if (result.shouldShowNotification) { |
Ta-wei Yen | 982c0bd | 2016-04-14 13:59:54 -0700 | [diff] [blame] | 412 | Log.w(this, "onCallScreeningCompleted: blocked call, showing notification."); |
| 413 | } |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 414 | mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE, |
| 415 | result.shouldShowNotification); |
| 416 | } else if (result.shouldShowNotification) { |
Sailesh Nepal | ae92595 | 2016-01-24 18:56:58 -0800 | [diff] [blame] | 417 | Log.i(this, "onCallScreeningCompleted: blocked call, showing notification."); |
| 418 | mMissedCallNotifier.showMissedCallNotification(incomingCall); |
| 419 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 420 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Wei Liu | 13791b9 | 2016-10-04 17:46:44 -0700 | [diff] [blame] | 423 | /** |
| 424 | * Whether allow (silence rather than reject) the incoming call if it has a different source |
| 425 | * (connection service) from the existing ringing call when reaching maximum ringing calls. |
| 426 | */ |
| 427 | private boolean shouldSilenceInsteadOfReject(Call incomingCall) { |
| 428 | if (!mContext.getResources().getBoolean( |
| 429 | R.bool.silence_incoming_when_different_service_and_maximum_ringing)) { |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | Call ringingCall = null; |
| 434 | |
| 435 | for (Call call : mCalls) { |
| 436 | // Only operate on top-level calls |
| 437 | if (call.getParentCall() != null) { |
| 438 | continue; |
| 439 | } |
| 440 | |
| 441 | if (call.isExternalCall()) { |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | if (CallState.RINGING == call.getState() && |
| 446 | call.getConnectionService() == incomingCall.getConnectionService()) { |
| 447 | return false; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | return true; |
| 452 | } |
| 453 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 454 | @Override |
| 455 | public void onFailedIncomingCall(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 456 | setCallState(call, CallState.DISCONNECTED, "failed incoming call"); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 457 | call.removeListener(this); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 460 | @Override |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 461 | public void onSuccessfulUnknownCall(Call call, int callState) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 462 | setCallState(call, callState, "successful unknown call"); |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 463 | Log.i(this, "onSuccessfulUnknownCall for call %s", call); |
| 464 | addCall(call); |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public void onFailedUnknownCall(Call call) { |
| 469 | Log.i(this, "onFailedUnknownCall for call %s", call); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 470 | setCallState(call, CallState.DISCONNECTED, "failed unknown call"); |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 471 | call.removeListener(this); |
| 472 | } |
| 473 | |
| 474 | @Override |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 475 | public void onRingbackRequested(Call call, boolean ringback) { |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 476 | for (CallsManagerListener listener : mListeners) { |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 477 | listener.onRingbackRequested(call, ringback); |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 481 | @Override |
| 482 | public void onPostDialWait(Call call, String remaining) { |
| 483 | mInCallController.onPostDialWait(call, remaining); |
| 484 | } |
| 485 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 486 | @Override |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 487 | public void onPostDialChar(final Call call, char nextChar) { |
| 488 | if (PhoneNumberUtils.is12Key(nextChar)) { |
| 489 | // Play tone if it is one of the dialpad digits, canceling out the previously queued |
| 490 | // up stopTone runnable since playing a new tone automatically stops the previous tone. |
| 491 | if (mStopTone != null) { |
Brad Ebinger | e62e9e8 | 2016-02-01 18:26:40 -0800 | [diff] [blame] | 492 | mHandler.removeCallbacks(mStopTone.getRunnableToCancel()); |
| 493 | mStopTone.cancel(); |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | mDtmfLocalTonePlayer.playTone(call, nextChar); |
| 497 | |
Brad Ebinger | f5e0666 | 2016-08-25 16:16:27 -0700 | [diff] [blame] | 498 | mStopTone = new Runnable("CM.oPDC", mLock) { |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 499 | @Override |
Brad Ebinger | e62e9e8 | 2016-02-01 18:26:40 -0800 | [diff] [blame] | 500 | public void loggedRun() { |
Brad Ebinger | f5e0666 | 2016-08-25 16:16:27 -0700 | [diff] [blame] | 501 | // Set a timeout to stop the tone in case there isn't another tone to |
| 502 | // follow. |
| 503 | mDtmfLocalTonePlayer.stopTone(call); |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 504 | } |
| 505 | }; |
Brad Ebinger | e62e9e8 | 2016-02-01 18:26:40 -0800 | [diff] [blame] | 506 | mHandler.postDelayed(mStopTone.prepare(), |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 507 | Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver())); |
| 508 | } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT || |
| 509 | nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) { |
| 510 | // Stop the tone if a tone is playing, removing any other stopTone callbacks since |
| 511 | // the previous tone is being stopped anyway. |
| 512 | if (mStopTone != null) { |
Brad Ebinger | e62e9e8 | 2016-02-01 18:26:40 -0800 | [diff] [blame] | 513 | mHandler.removeCallbacks(mStopTone.getRunnableToCancel()); |
| 514 | mStopTone.cancel(); |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 515 | } |
| 516 | mDtmfLocalTonePlayer.stopTone(call); |
| 517 | } else { |
| 518 | Log.w(this, "onPostDialChar: invalid value %d", nextChar); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | @Override |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 523 | public void onParentChanged(Call call) { |
Santos Cordon | 66fe882 | 2014-10-10 16:10:58 -0700 | [diff] [blame] | 524 | // parent-child relationship affects which call should be foreground, so do an update. |
Hall Liu | fc130b2 | 2016-06-15 17:54:48 -0700 | [diff] [blame] | 525 | updateCanAddCall(); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 526 | for (CallsManagerListener listener : mListeners) { |
| 527 | listener.onIsConferencedChanged(call); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public void onChildrenChanged(Call call) { |
Santos Cordon | 66fe882 | 2014-10-10 16:10:58 -0700 | [diff] [blame] | 533 | // parent-child relationship affects which call should be foreground, so do an update. |
Hall Liu | fc130b2 | 2016-06-15 17:54:48 -0700 | [diff] [blame] | 534 | updateCanAddCall(); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 535 | for (CallsManagerListener listener : mListeners) { |
| 536 | listener.onIsConferencedChanged(call); |
| 537 | } |
| 538 | } |
| 539 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 540 | @Override |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 541 | public void onIsVoipAudioModeChanged(Call call) { |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 542 | for (CallsManagerListener listener : mListeners) { |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 543 | listener.onIsVoipAudioModeChanged(call); |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 547 | @Override |
Hall Liu | 9696c21 | 2016-06-24 16:09:02 -0700 | [diff] [blame] | 548 | public void onVideoStateChanged(Call call, int previousVideoState, int newVideoState) { |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 549 | for (CallsManagerListener listener : mListeners) { |
Hall Liu | 9696c21 | 2016-06-24 16:09:02 -0700 | [diff] [blame] | 550 | listener.onVideoStateChanged(call, previousVideoState, newVideoState); |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 554 | @Override |
Hall Liu | ad18dc9 | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 555 | public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call, |
| 556 | long disconnectionTimeout) { |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 557 | mPendingCallsToDisconnect.add(call); |
Brad Ebinger | f5e0666 | 2016-08-25 16:16:27 -0700 | [diff] [blame] | 558 | mHandler.postDelayed(new Runnable("CM.oCVNOCB", mLock) { |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 559 | @Override |
Brad Ebinger | e62e9e8 | 2016-02-01 18:26:40 -0800 | [diff] [blame] | 560 | public void loggedRun() { |
Brad Ebinger | f5e0666 | 2016-08-25 16:16:27 -0700 | [diff] [blame] | 561 | if (mPendingCallsToDisconnect.remove(call)) { |
| 562 | Log.i(this, "Delayed disconnection of call: %s", call); |
| 563 | call.disconnect(); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
Hall Liu | ad18dc9 | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 566 | }.prepare(), disconnectionTimeout); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 567 | |
| 568 | return true; |
| 569 | } |
| 570 | |
Tyler Gunn | 86014fc | 2015-06-12 14:31:25 -0700 | [diff] [blame] | 571 | /** |
| 572 | * Handles changes to the {@link Connection.VideoProvider} for a call. Adds the |
| 573 | * {@link CallsManager} as a listener for the {@link VideoProviderProxy} which is created |
| 574 | * in {@link Call#setVideoProvider(IVideoProvider)}. This allows the {@link CallsManager} to |
| 575 | * respond to callbacks from the {@link VideoProviderProxy}. |
| 576 | * |
| 577 | * @param call The call. |
| 578 | */ |
| 579 | @Override |
| 580 | public void onVideoCallProviderChanged(Call call) { |
| 581 | VideoProviderProxy videoProviderProxy = call.getVideoProviderProxy(); |
| 582 | |
| 583 | if (videoProviderProxy == null) { |
| 584 | return; |
| 585 | } |
| 586 | |
| 587 | videoProviderProxy.addListener(this); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Handles session modification requests received via the {@link TelecomVideoCallCallback} for |
| 592 | * a call. Notifies listeners of the {@link CallsManager.CallsManagerListener} of the session |
| 593 | * modification request. |
| 594 | * |
| 595 | * @param call The call. |
| 596 | * @param videoProfile The {@link VideoProfile}. |
| 597 | */ |
| 598 | @Override |
| 599 | public void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile) { |
| 600 | int videoState = videoProfile != null ? videoProfile.getVideoState() : |
| 601 | VideoProfile.STATE_AUDIO_ONLY; |
| 602 | Log.v(TAG, "onSessionModifyRequestReceived : videoProfile = " + VideoProfile |
| 603 | .videoStateToString(videoState)); |
| 604 | |
| 605 | for (CallsManagerListener listener : mListeners) { |
| 606 | listener.onSessionModifyRequestReceived(call, videoProfile); |
| 607 | } |
| 608 | } |
| 609 | |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 610 | public Collection<Call> getCalls() { |
Santos Cordon | 4bc0245 | 2014-11-19 15:51:12 -0800 | [diff] [blame] | 611 | return Collections.unmodifiableCollection(mCalls); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Tyler Gunn | db82191 | 2016-02-16 14:35:25 -0800 | [diff] [blame] | 614 | /** |
| 615 | * Play or stop a call hold tone for a call. Triggered via |
| 616 | * {@link Connection#sendConnectionEvent(String)} when the |
| 617 | * {@link Connection#EVENT_ON_HOLD_TONE_START} event or |
| 618 | * {@link Connection#EVENT_ON_HOLD_TONE_STOP} event is passed through to the |
| 619 | * |
| 620 | * @param call The call which requested the hold tone. |
| 621 | */ |
| 622 | @Override |
| 623 | public void onHoldToneRequested(Call call) { |
| 624 | for (CallsManagerListener listener : mListeners) { |
| 625 | listener.onHoldToneRequested(call); |
| 626 | } |
| 627 | } |
| 628 | |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 629 | @VisibleForTesting |
| 630 | public Call getForegroundCall() { |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 631 | if (mCallAudioManager == null) { |
| 632 | // Happens when getForegroundCall is called before full initialization. |
| 633 | return null; |
| 634 | } |
| 635 | return mCallAudioManager.getForegroundCall(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Santos Cordon | f0f99f3 | 2016-02-18 16:13:57 -0800 | [diff] [blame] | 638 | public UserHandle getCurrentUserHandle() { |
| 639 | return mCurrentUserHandle; |
| 640 | } |
| 641 | |
Hall Liu | 9ecbb1c | 2016-04-14 14:35:48 -0700 | [diff] [blame] | 642 | public CallAudioManager getCallAudioManager() { |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 643 | return mCallAudioManager; |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 646 | InCallController getInCallController() { |
| 647 | return mInCallController; |
| 648 | } |
| 649 | |
Santos Cordon | f0f99f3 | 2016-02-18 16:13:57 -0800 | [diff] [blame] | 650 | @VisibleForTesting |
| 651 | public boolean hasEmergencyCall() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 652 | for (Call call : mCalls) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 653 | if (call.isEmergencyCall()) { |
| 654 | return true; |
| 655 | } |
| 656 | } |
| 657 | return false; |
| 658 | } |
| 659 | |
Roshan Pius | 669f90b | 2015-09-02 16:21:39 -0700 | [diff] [blame] | 660 | boolean hasOnlyDisconnectedCalls() { |
| 661 | for (Call call : mCalls) { |
| 662 | if (!call.isDisconnected()) { |
| 663 | return false; |
| 664 | } |
| 665 | } |
| 666 | return true; |
| 667 | } |
| 668 | |
Andrew Lee | 4550623 | 2014-10-22 17:54:33 -0700 | [diff] [blame] | 669 | boolean hasVideoCall() { |
| 670 | for (Call call : mCalls) { |
Tyler Gunn | 467b64f | 2015-06-09 13:48:47 -0700 | [diff] [blame] | 671 | if (VideoProfile.isVideo(call.getVideoState())) { |
Andrew Lee | 4550623 | 2014-10-22 17:54:33 -0700 | [diff] [blame] | 672 | return true; |
| 673 | } |
| 674 | } |
| 675 | return false; |
| 676 | } |
| 677 | |
Hall Liu | 28b82f0 | 2016-07-26 17:38:56 -0700 | [diff] [blame] | 678 | @VisibleForTesting |
| 679 | public CallAudioState getAudioState() { |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 680 | return mCallAudioManager.getCallAudioState(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 683 | boolean isTtySupported() { |
| 684 | return mTtyManager.isTtySupported(); |
| 685 | } |
| 686 | |
| 687 | int getCurrentTtyMode() { |
| 688 | return mTtyManager.getCurrentTtyMode(); |
| 689 | } |
| 690 | |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 691 | @VisibleForTesting |
| 692 | public void addListener(CallsManagerListener listener) { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 693 | mListeners.add(listener); |
| 694 | } |
| 695 | |
| 696 | void removeListener(CallsManagerListener listener) { |
| 697 | mListeners.remove(listener); |
| 698 | } |
| 699 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 700 | /** |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 701 | * Starts the process to attach the call to a connection service. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 702 | * |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 703 | * @param phoneAccountHandle The phone account which contains the component name of the |
| 704 | * connection service to use for this call. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 705 | * @param extras The optional extras Bundle passed with the intent used for the incoming call. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 706 | */ |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 707 | void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 708 | Log.d(this, "processIncomingCallIntent"); |
Yorke Lee | 69fa897 | 2015-06-08 11:25:32 -0700 | [diff] [blame] | 709 | Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS); |
| 710 | if (handle == null) { |
| 711 | // Required for backwards compatibility |
| 712 | handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER); |
| 713 | } |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 714 | Call call = new Call( |
Tyler Gunn | 8452be0 | 2015-09-17 09:57:02 -0700 | [diff] [blame] | 715 | getNextCallId(), |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 716 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 717 | this, |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 718 | mLock, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 719 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 720 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 721 | mCallerInfoAsyncQueryFactory, |
Brad Ebinger | 6e8f3d7 | 2016-06-20 11:35:42 -0700 | [diff] [blame] | 722 | mPhoneNumberUtilsAdapter, |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 723 | handle, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 724 | null /* gatewayInfo */, |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 725 | null /* connectionManagerPhoneAccount */, |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 726 | phoneAccountHandle, |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 727 | Call.CALL_DIRECTION_INCOMING /* callDirection */, |
| 728 | false /* forceAttachToExistingConnection */, |
Tony Mak | 578a4e6 | 2015-11-23 11:18:51 +0000 | [diff] [blame] | 729 | false /* isConference */ |
| 730 | ); |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 731 | |
| 732 | call.initAnalytics(); |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 733 | if (getForegroundCall() != null) { |
| 734 | getForegroundCall().getAnalytics().setCallIsInterrupted(true); |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 735 | call.getAnalytics().setCallIsAdditional(true); |
| 736 | } |
| 737 | |
Sailesh Nepal | 703a1af | 2016-04-14 20:10:12 -0700 | [diff] [blame] | 738 | setIntentExtrasAndStartTime(call, extras); |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 739 | // TODO: Move this to be a part of addCall() |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 740 | call.addListener(this); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 741 | call.startCreateConnection(mPhoneAccountRegistrar); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 744 | void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
| 745 | Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE); |
| 746 | Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle)); |
| 747 | Call call = new Call( |
Tyler Gunn | 8452be0 | 2015-09-17 09:57:02 -0700 | [diff] [blame] | 748 | getNextCallId(), |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 749 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 750 | this, |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 751 | mLock, |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 752 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 753 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 754 | mCallerInfoAsyncQueryFactory, |
Brad Ebinger | 6e8f3d7 | 2016-06-20 11:35:42 -0700 | [diff] [blame] | 755 | mPhoneNumberUtilsAdapter, |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 756 | handle, |
| 757 | null /* gatewayInfo */, |
| 758 | null /* connectionManagerPhoneAccount */, |
| 759 | phoneAccountHandle, |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 760 | Call.CALL_DIRECTION_UNKNOWN /* callDirection */, |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 761 | // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach |
| 762 | // to the existing connection instead of trying to create a new one. |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 763 | true /* forceAttachToExistingConnection */, |
Tony Mak | 578a4e6 | 2015-11-23 11:18:51 +0000 | [diff] [blame] | 764 | false /* isConference */ |
| 765 | ); |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 766 | call.initAnalytics(); |
| 767 | |
Sailesh Nepal | 703a1af | 2016-04-14 20:10:12 -0700 | [diff] [blame] | 768 | setIntentExtrasAndStartTime(call, extras); |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 769 | call.addListener(this); |
| 770 | call.startCreateConnection(mPhoneAccountRegistrar); |
| 771 | } |
| 772 | |
Yorke Lee | 8d3f269 | 2015-05-08 11:44:39 -0700 | [diff] [blame] | 773 | private boolean areHandlesEqual(Uri handle1, Uri handle2) { |
| 774 | if (handle1 == null || handle2 == null) { |
| 775 | return handle1 == handle2; |
| 776 | } |
| 777 | |
| 778 | if (!TextUtils.equals(handle1.getScheme(), handle2.getScheme())) { |
| 779 | return false; |
| 780 | } |
| 781 | |
| 782 | final String number1 = PhoneNumberUtils.normalizeNumber(handle1.getSchemeSpecificPart()); |
| 783 | final String number2 = PhoneNumberUtils.normalizeNumber(handle2.getSchemeSpecificPart()); |
| 784 | return TextUtils.equals(number1, number2); |
| 785 | } |
| 786 | |
Brad Ebinger | b1e3d75 | 2015-11-09 17:54:17 -0800 | [diff] [blame] | 787 | private Call reuseOutgoingCall(Uri handle) { |
| 788 | // Check to see if we can reuse any of the calls that are waiting to disconnect. |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 789 | // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information. |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 790 | Call reusedCall = null; |
Brad Ebinger | e680e66 | 2016-08-26 11:45:43 -0700 | [diff] [blame] | 791 | for (Iterator<Call> callIter = mPendingCallsToDisconnect.iterator(); callIter.hasNext();) { |
| 792 | Call pendingCall = callIter.next(); |
Yorke Lee | 8d3f269 | 2015-05-08 11:44:39 -0700 | [diff] [blame] | 793 | if (reusedCall == null && areHandlesEqual(pendingCall.getHandle(), handle)) { |
Brad Ebinger | e680e66 | 2016-08-26 11:45:43 -0700 | [diff] [blame] | 794 | callIter.remove(); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 795 | Log.i(this, "Reusing disconnected call %s", pendingCall); |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 796 | reusedCall = pendingCall; |
| 797 | } else { |
Yorke Lee | 8d3f269 | 2015-05-08 11:44:39 -0700 | [diff] [blame] | 798 | Log.i(this, "Not reusing disconnected call %s", pendingCall); |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 799 | pendingCall.disconnect(); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Brad Ebinger | b1e3d75 | 2015-11-09 17:54:17 -0800 | [diff] [blame] | 803 | return reusedCall; |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 804 | } |
| 805 | |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 806 | /** |
| 807 | * Kicks off the first steps to creating an outgoing call so that InCallUI can launch. |
| 808 | * |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 809 | * @param handle Handle to connect the call with. |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 810 | * @param phoneAccountHandle The phone account which contains the component name of the |
| 811 | * connection service to use for this call. |
| 812 | * @param extras The optional extras Bundle passed with the intent used for the incoming call. |
Tony Mak | 578a4e6 | 2015-11-23 11:18:51 +0000 | [diff] [blame] | 813 | * @param initiatingUser {@link UserHandle} of user that place the outgoing call. |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 814 | */ |
Tony Mak | 578a4e6 | 2015-11-23 11:18:51 +0000 | [diff] [blame] | 815 | Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras, |
| 816 | UserHandle initiatingUser) { |
Brad Ebinger | b1e3d75 | 2015-11-09 17:54:17 -0800 | [diff] [blame] | 817 | boolean isReusedCall = true; |
| 818 | Call call = reuseOutgoingCall(handle); |
| 819 | |
| 820 | // Create a call with original handle. The handle may be changed when the call is attached |
| 821 | // to a connection service, but in most cases will remain the same. |
| 822 | if (call == null) { |
| 823 | call = new Call(getNextCallId(), mContext, |
| 824 | this, |
| 825 | mLock, |
| 826 | mConnectionServiceRepository, |
| 827 | mContactsAsyncHelper, |
| 828 | mCallerInfoAsyncQueryFactory, |
Brad Ebinger | 6e8f3d7 | 2016-06-20 11:35:42 -0700 | [diff] [blame] | 829 | mPhoneNumberUtilsAdapter, |
Brad Ebinger | b1e3d75 | 2015-11-09 17:54:17 -0800 | [diff] [blame] | 830 | handle, |
| 831 | null /* gatewayInfo */, |
| 832 | null /* connectionManagerPhoneAccount */, |
| 833 | null /* phoneAccountHandle */, |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 834 | Call.CALL_DIRECTION_OUTGOING /* callDirection */, |
| 835 | false /* forceAttachToExistingConnection */, |
Tony Mak | 578a4e6 | 2015-11-23 11:18:51 +0000 | [diff] [blame] | 836 | false /* isConference */ |
| 837 | ); |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 838 | call.initAnalytics(); |
| 839 | |
Hall Liu | 874c0f8 | 2016-04-29 18:13:18 -0700 | [diff] [blame] | 840 | call.setInitiatingUser(initiatingUser); |
| 841 | |
Brad Ebinger | b1e3d75 | 2015-11-09 17:54:17 -0800 | [diff] [blame] | 842 | isReusedCall = false; |
| 843 | } |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 844 | |
Tyler Gunn | 228f95e | 2016-03-03 16:24:45 -0800 | [diff] [blame] | 845 | // Set the video state on the call early so that when it is added to the InCall UI the UI |
| 846 | // knows to configure itself as a video call immediately. |
| 847 | if (extras != null) { |
Tyler Gunn | f815638 | 2016-03-30 08:35:29 -0700 | [diff] [blame] | 848 | int videoState = extras.getInt(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, |
| 849 | VideoProfile.STATE_AUDIO_ONLY); |
| 850 | |
| 851 | // If this is an emergency video call, we need to check if the phone account supports |
| 852 | // emergency video calling. |
Tyler Gunn | 10274f7 | 2016-07-26 15:38:16 -0700 | [diff] [blame] | 853 | // Also, ensure we don't try to place an outgoing call with video if video is not |
| 854 | // supported. |
| 855 | if (VideoProfile.isVideo(videoState)) { |
Tyler Gunn | f815638 | 2016-03-30 08:35:29 -0700 | [diff] [blame] | 856 | PhoneAccount account = |
| 857 | mPhoneAccountRegistrar.getPhoneAccount(phoneAccountHandle, initiatingUser); |
| 858 | |
Tyler Gunn | 10274f7 | 2016-07-26 15:38:16 -0700 | [diff] [blame] | 859 | if (call.isEmergencyCall() && account != null && |
Tyler Gunn | f815638 | 2016-03-30 08:35:29 -0700 | [diff] [blame] | 860 | !account.hasCapabilities(PhoneAccount.CAPABILITY_EMERGENCY_VIDEO_CALLING)) { |
| 861 | // Phone account doesn't support emergency video calling, so fallback to |
| 862 | // audio-only now to prevent the InCall UI from setting up video surfaces |
| 863 | // needlessly. |
| 864 | Log.i(this, "startOutgoingCall - emergency video calls not supported; " + |
| 865 | "falling back to audio-only"); |
| 866 | videoState = VideoProfile.STATE_AUDIO_ONLY; |
Tyler Gunn | 10274f7 | 2016-07-26 15:38:16 -0700 | [diff] [blame] | 867 | } else if (account != null && |
| 868 | !account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) { |
| 869 | // Phone account doesn't support video calling, so fallback to audio-only. |
| 870 | Log.i(this, "startOutgoingCall - video calls not supported; fallback to " + |
| 871 | "audio-only."); |
| 872 | videoState = VideoProfile.STATE_AUDIO_ONLY; |
Tyler Gunn | f815638 | 2016-03-30 08:35:29 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | call.setVideoState(videoState); |
Omkar Kolangade | 005612a | 2015-08-25 11:08:18 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Brad Ebinger | 3da5764 | 2016-04-04 17:32:19 -0700 | [diff] [blame] | 879 | List<PhoneAccountHandle> accounts = constructPossiblePhoneAccounts(handle, initiatingUser); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 880 | Log.v(this, "startOutgoingCall found accounts = " + accounts); |
| 881 | |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 882 | // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call |
| 883 | // as if a phoneAccount was not specified (does the default behavior instead). |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 884 | // Note: We will not attempt to dial with a requested phoneAccount if it is disabled. |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 885 | if (phoneAccountHandle != null) { |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 886 | if (!accounts.contains(phoneAccountHandle)) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 887 | phoneAccountHandle = null; |
| 888 | } |
| 889 | } |
| 890 | |
Brad Ebinger | e6c481a | 2016-05-12 14:13:26 -0700 | [diff] [blame] | 891 | if (phoneAccountHandle == null && accounts.size() > 0) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 892 | // No preset account, check if default exists that supports the URI scheme for the |
Brad Ebinger | 3da5764 | 2016-04-04 17:32:19 -0700 | [diff] [blame] | 893 | // handle and verify it can be used. |
| 894 | if(accounts.size() > 1) { |
| 895 | PhoneAccountHandle defaultPhoneAccountHandle = |
| 896 | mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(handle.getScheme(), |
| 897 | initiatingUser); |
| 898 | if (defaultPhoneAccountHandle != null && |
| 899 | accounts.contains(defaultPhoneAccountHandle)) { |
| 900 | phoneAccountHandle = defaultPhoneAccountHandle; |
| 901 | } |
| 902 | } else { |
| 903 | // Use the only PhoneAccount that is available |
| 904 | phoneAccountHandle = accounts.get(0); |
| 905 | } |
| 906 | |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 909 | call.setTargetPhoneAccount(phoneAccountHandle); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 910 | |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 911 | boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 912 | |
| 913 | // Do not support any more live calls. Our options are to move a call to hold, disconnect |
Brad Ebinger | b1e3d75 | 2015-11-09 17:54:17 -0800 | [diff] [blame] | 914 | // a call, or cancel this call altogether. If a call is being reused, then it has already |
| 915 | // passed the makeRoomForOutgoingCall check once and will fail the second time due to the |
| 916 | // call transitioning into the CONNECTING state. |
| 917 | if (!isPotentialInCallMMICode && (!isReusedCall && |
| 918 | !makeRoomForOutgoingCall(call, call.isEmergencyCall()))) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 919 | // just cancel at this point. |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 920 | Log.i(this, "No remaining room for outgoing call: %s", call); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 921 | if (mCalls.contains(call)) { |
| 922 | // This call can already exist if it is a reused call, |
Brad Ebinger | b1e3d75 | 2015-11-09 17:54:17 -0800 | [diff] [blame] | 923 | // See {@link #reuseOutgoingCall}. |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 924 | call.disconnect(); |
| 925 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 926 | return null; |
| 927 | } |
| 928 | |
Nancy Chen | 8d92f45 | 2014-10-20 21:43:44 -0700 | [diff] [blame] | 929 | boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 && |
Tyler Gunn | 6ffe531 | 2015-08-12 08:19:20 -0700 | [diff] [blame] | 930 | !call.isEmergencyCall(); |
Nancy Chen | 8d92f45 | 2014-10-20 21:43:44 -0700 | [diff] [blame] | 931 | |
| 932 | if (needsAccountSelection) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 933 | // This is the state where the user is expected to select an account |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 934 | call.setState(CallState.SELECT_PHONE_ACCOUNT, "needs account selection"); |
Pawit Pornkitprasan | 1b2fd28 | 2015-03-15 08:33:32 +0700 | [diff] [blame] | 935 | // Create our own instance to modify (since extras may be Bundle.EMPTY) |
| 936 | extras = new Bundle(extras); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 937 | extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 938 | } else { |
Roshan Pius | 781d025 | 2015-06-25 14:13:07 -0700 | [diff] [blame] | 939 | call.setState( |
| 940 | CallState.CONNECTING, |
| 941 | phoneAccountHandle == null ? "no-handle" : phoneAccountHandle.toString()); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 942 | } |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 943 | |
Sailesh Nepal | 703a1af | 2016-04-14 20:10:12 -0700 | [diff] [blame] | 944 | setIntentExtrasAndStartTime(call, extras); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 945 | |
Tyler Gunn | 504eb83 | 2014-10-13 14:15:27 -0700 | [diff] [blame] | 946 | // Do not add the call if it is a potential MMI code. |
Nancy Chen | 8d92f45 | 2014-10-20 21:43:44 -0700 | [diff] [blame] | 947 | if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) { |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 948 | call.addListener(this); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 949 | } else if (!mCalls.contains(call)) { |
| 950 | // We check if mCalls already contains the call because we could potentially be reusing |
Brad Ebinger | b1e3d75 | 2015-11-09 17:54:17 -0800 | [diff] [blame] | 951 | // a call which was previously added (See {@link #reuseOutgoingCall}). |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 952 | addCall(call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 953 | } |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 954 | |
| 955 | return call; |
| 956 | } |
| 957 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 958 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 959 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 960 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 961 | * @param handle Handle to connect the call with. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 962 | * @param gatewayInfo Optional gateway information that can be used to route the call to the |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 963 | * actual dialed handle via a gateway provider. May be null. |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 964 | * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects. |
Tyler Gunn | c4abd91 | 2014-07-08 14:22:10 -0700 | [diff] [blame] | 965 | * @param videoState The desired video state for the outgoing call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 966 | */ |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 967 | @VisibleForTesting |
| 968 | public void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, |
| 969 | boolean speakerphoneOn, int videoState) { |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 970 | if (call == null) { |
| 971 | // don't do anything if the call no longer exists |
| 972 | Log.i(this, "Canceling unknown call."); |
Santos Cordon | b7af09e | 2014-08-06 04:30:01 -0700 | [diff] [blame] | 973 | return; |
| 974 | } |
| 975 | |
Nancy Chen | 201b437 | 2014-09-08 14:18:24 -0700 | [diff] [blame] | 976 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress(); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 977 | |
| 978 | if (gatewayInfo == null) { |
Santos Cordon | b58f453 | 2014-05-29 11:59:06 -0700 | [diff] [blame] | 979 | Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle)); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 980 | } else { |
| 981 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 982 | Log.pii(uriHandle), Log.pii(handle)); |
| 983 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 984 | |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 985 | call.setHandle(uriHandle); |
| 986 | call.setGatewayInfo(gatewayInfo); |
Tyler Gunn | b492f4c | 2015-12-15 08:15:43 -0800 | [diff] [blame] | 987 | |
| 988 | final boolean useSpeakerWhenDocked = mContext.getResources().getBoolean( |
| 989 | R.bool.use_speaker_when_docked); |
John Eckerdal | 6348600 | 2015-11-20 13:17:29 +0100 | [diff] [blame] | 990 | final boolean useSpeakerForDock = isSpeakerphoneEnabledForDock(); |
Hall Liu | 9696c21 | 2016-06-24 16:09:02 -0700 | [diff] [blame] | 991 | final boolean useSpeakerForVideoCall = isSpeakerphoneAutoEnabledForVideoCalls(videoState); |
Tyler Gunn | b492f4c | 2015-12-15 08:15:43 -0800 | [diff] [blame] | 992 | |
| 993 | // Auto-enable speakerphone if the originating intent specified to do so, if the call |
| 994 | // is a video call, of if using speaker when docked |
| 995 | call.setStartWithSpeakerphoneOn(speakerphoneOn || useSpeakerForVideoCall |
John Eckerdal | 6348600 | 2015-11-20 13:17:29 +0100 | [diff] [blame] | 996 | || (useSpeakerWhenDocked && useSpeakerForDock)); |
Tyler Gunn | c4abd91 | 2014-07-08 14:22:10 -0700 | [diff] [blame] | 997 | call.setVideoState(videoState); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 998 | |
Santos Cordon | a04bdca | 2015-03-04 16:57:55 -0800 | [diff] [blame] | 999 | if (speakerphoneOn) { |
| 1000 | Log.i(this, "%s Starting with speakerphone as requested", call); |
John Eckerdal | 6348600 | 2015-11-20 13:17:29 +0100 | [diff] [blame] | 1001 | } else if (useSpeakerWhenDocked && useSpeakerForDock) { |
Santos Cordon | a04bdca | 2015-03-04 16:57:55 -0800 | [diff] [blame] | 1002 | Log.i(this, "%s Starting with speakerphone because car is docked.", call); |
Tyler Gunn | b492f4c | 2015-12-15 08:15:43 -0800 | [diff] [blame] | 1003 | } else if (useSpeakerForVideoCall) { |
| 1004 | Log.i(this, "%s Starting with speakerphone because its a video call.", call); |
Santos Cordon | a04bdca | 2015-03-04 16:57:55 -0800 | [diff] [blame] | 1005 | } |
Bryce Lee | 88f900a | 2015-11-06 08:51:22 -0800 | [diff] [blame] | 1006 | |
Tyler Gunn | 6ffe531 | 2015-08-12 08:19:20 -0700 | [diff] [blame] | 1007 | if (call.isEmergencyCall()) { |
Abhijith Shastry | cf27f68 | 2016-03-01 18:28:44 -0800 | [diff] [blame] | 1008 | new AsyncEmergencyContactNotifier(mContext).execute(); |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 1009 | } |
Nancy Chen | d9de92c | 2014-07-21 16:09:13 -0700 | [diff] [blame] | 1010 | |
Bryce Lee | 6afefa4 | 2015-10-25 22:25:53 -0700 | [diff] [blame] | 1011 | final boolean requireCallCapableAccountByHandle = mContext.getResources().getBoolean( |
| 1012 | com.android.internal.R.bool.config_requireCallCapableAccountForHandle); |
| 1013 | |
Tyler Gunn | 6ffe531 | 2015-08-12 08:19:20 -0700 | [diff] [blame] | 1014 | if (call.getTargetPhoneAccount() != null || call.isEmergencyCall()) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 1015 | // If the account has been set, proceed to place the outgoing call. |
| 1016 | // Otherwise the connection will be initiated when the account is set by the user. |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1017 | call.startCreateConnection(mPhoneAccountRegistrar); |
Bryce Lee | 6afefa4 | 2015-10-25 22:25:53 -0700 | [diff] [blame] | 1018 | } else if (mPhoneAccountRegistrar.getCallCapablePhoneAccounts( |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 1019 | requireCallCapableAccountByHandle ? call.getHandle().getScheme() : null, false, |
| 1020 | call.getInitiatingUser()).isEmpty()) { |
Bryce Lee | de0ac37 | 2015-10-23 16:06:00 -0700 | [diff] [blame] | 1021 | // If there are no call capable accounts, disconnect the call. |
| 1022 | markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.CANCELED, |
| 1023 | "No registered PhoneAccounts")); |
| 1024 | markCallAsRemoved(call); |
Nancy Chen | d9de92c | 2014-07-21 16:09:13 -0700 | [diff] [blame] | 1025 | } |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /** |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 1029 | * Attempts to start a conference call for the specified call. |
| 1030 | * |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 1031 | * @param call The call to conference. |
| 1032 | * @param otherCall The other call to conference with. |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 1033 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1034 | @VisibleForTesting |
| 1035 | public void conference(Call call, Call otherCall) { |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1036 | call.conferenceWith(otherCall); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1040 | * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call |
| 1041 | * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1042 | * the user opting to answer said call. |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 1043 | * |
| 1044 | * @param call The call to answer. |
| 1045 | * @param videoState The video state in which to answer the call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1046 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1047 | @VisibleForTesting |
| 1048 | public void answerCall(Call call, int videoState) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1049 | if (!mCalls.contains(call)) { |
| 1050 | Log.i(this, "Request to answer a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 1051 | } else { |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 1052 | Call foregroundCall = getForegroundCall(); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1053 | // If the foreground call is not the ringing call and it is currently isActive() or |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1054 | // STATE_DIALING, put it on hold before answering the call. |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 1055 | if (foregroundCall != null && foregroundCall != call && |
| 1056 | (foregroundCall.isActive() || |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 1057 | foregroundCall.getState() == CallState.DIALING || |
| 1058 | foregroundCall.getState() == CallState.PULLING)) { |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 1059 | if (0 == (foregroundCall.getConnectionCapabilities() |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1060 | & Connection.CAPABILITY_HOLD)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1061 | // This call does not support hold. If it is from a different connection |
| 1062 | // service, then disconnect it, otherwise allow the connection service to |
| 1063 | // figure out the right states. |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 1064 | if (foregroundCall.getConnectionService() != call.getConnectionService()) { |
| 1065 | foregroundCall.disconnect(); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } else { |
Andrew Lee | e94a8f1 | 2014-12-19 16:19:51 -0800 | [diff] [blame] | 1068 | Call heldCall = getHeldCall(); |
| 1069 | if (heldCall != null) { |
| 1070 | Log.v(this, "Disconnecting held call %s before holding active call.", |
| 1071 | heldCall); |
| 1072 | heldCall.disconnect(); |
| 1073 | } |
| 1074 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1075 | Log.v(this, "Holding active/dialing call %s before answering incoming call %s.", |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 1076 | foregroundCall, call); |
| 1077 | foregroundCall.hold(); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1078 | } |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1079 | // TODO: Wait until we get confirmation of the active call being |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1080 | // on-hold before answering the new call. |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1081 | // TODO: Import logic from CallManager.acceptCall() |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1084 | for (CallsManagerListener listener : mListeners) { |
| 1085 | listener.onIncomingCallAnswered(call); |
| 1086 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1087 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 1088 | // We do not update the UI until we get confirmation of the answer() through |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1089 | // {@link #markCallAsActive}. |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 1090 | call.answer(videoState); |
Hall Liu | 9696c21 | 2016-06-24 16:09:02 -0700 | [diff] [blame] | 1091 | if (isSpeakerphoneAutoEnabledForVideoCalls(videoState)) { |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 1092 | call.setStartWithSpeakerphoneOn(true); |
| 1093 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 1094 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
Tyler Gunn | b360f0d | 2015-12-01 14:34:03 -0800 | [diff] [blame] | 1097 | /** |
| 1098 | * Determines if the speakerphone should be automatically enabled for the call. Speakerphone |
| 1099 | * should be enabled if the call is a video call and bluetooth or the wired headset are not in |
| 1100 | * use. |
| 1101 | * |
| 1102 | * @param videoState The video state of the call. |
| 1103 | * @return {@code true} if the speakerphone should be enabled. |
| 1104 | */ |
Hall Liu | 9696c21 | 2016-06-24 16:09:02 -0700 | [diff] [blame] | 1105 | public boolean isSpeakerphoneAutoEnabledForVideoCalls(int videoState) { |
Tyler Gunn | b360f0d | 2015-12-01 14:34:03 -0800 | [diff] [blame] | 1106 | return VideoProfile.isVideo(videoState) && |
| 1107 | !mWiredHeadsetManager.isPluggedIn() && |
| 1108 | !mBluetoothManager.isBluetoothAvailable() && |
| 1109 | isSpeakerEnabledForVideoCalls(); |
| 1110 | } |
| 1111 | |
| 1112 | /** |
John Eckerdal | 6348600 | 2015-11-20 13:17:29 +0100 | [diff] [blame] | 1113 | * Determines if the speakerphone should be enabled for when docked. Speakerphone |
| 1114 | * should be enabled if the device is docked and bluetooth or the wired headset are |
| 1115 | * not in use. |
| 1116 | * |
| 1117 | * @return {@code true} if the speakerphone should be enabled for the dock. |
| 1118 | */ |
| 1119 | private boolean isSpeakerphoneEnabledForDock() { |
| 1120 | return mDockManager.isDocked() && |
| 1121 | !mWiredHeadsetManager.isPluggedIn() && |
| 1122 | !mBluetoothManager.isBluetoothAvailable(); |
| 1123 | } |
| 1124 | |
| 1125 | /** |
Tyler Gunn | b360f0d | 2015-12-01 14:34:03 -0800 | [diff] [blame] | 1126 | * Determines if the speakerphone should be automatically enabled for video calls. |
| 1127 | * |
| 1128 | * @return {@code true} if the speakerphone should automatically be enabled. |
| 1129 | */ |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 1130 | private static boolean isSpeakerEnabledForVideoCalls() { |
| 1131 | return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT, |
| 1132 | PhoneConstants.AUDIO_OUTPUT_DEFAULT) == |
| 1133 | PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER); |
| 1134 | } |
| 1135 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1136 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1137 | * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call |
| 1138 | * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1139 | * the user opting to reject said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1140 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1141 | @VisibleForTesting |
| 1142 | public void rejectCall(Call call, boolean rejectWithMessage, String textMessage) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1143 | if (!mCalls.contains(call)) { |
| 1144 | Log.i(this, "Request to reject a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 1145 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1146 | for (CallsManagerListener listener : mListeners) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 1147 | listener.onIncomingCallRejected(call, rejectWithMessage, textMessage); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1148 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 1149 | call.reject(rejectWithMessage, textMessage); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 1150 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1154 | * Instructs Telecom to play the specified DTMF tone within the specified call. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1155 | * |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1156 | * @param digit The DTMF digit to play. |
| 1157 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1158 | @VisibleForTesting |
| 1159 | public void playDtmfTone(Call call, char digit) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1160 | if (!mCalls.contains(call)) { |
| 1161 | Log.i(this, "Request to play DTMF in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1162 | } else { |
| 1163 | call.playDtmfTone(digit); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 1164 | mDtmfLocalTonePlayer.playTone(call, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1169 | * Instructs Telecom to stop the currently playing DTMF tone, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1170 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1171 | @VisibleForTesting |
| 1172 | public void stopDtmfTone(Call call) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1173 | if (!mCalls.contains(call)) { |
| 1174 | Log.i(this, "Request to stop DTMF in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1175 | } else { |
| 1176 | call.stopDtmfTone(); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 1177 | mDtmfLocalTonePlayer.stopTone(call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1182 | * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1183 | */ |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 1184 | void postDialContinue(Call call, boolean proceed) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1185 | if (!mCalls.contains(call)) { |
| 1186 | Log.i(this, "Request to continue post-dial string in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1187 | } else { |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 1188 | call.postDialContinue(proceed); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1193 | * Instructs Telecom to disconnect the specified call. Intended to be invoked by the |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1194 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 1195 | * the user hitting the end-call button. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1196 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1197 | @VisibleForTesting |
| 1198 | public void disconnectCall(Call call) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 1199 | Log.v(this, "disconnectCall %s", call); |
| 1200 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1201 | if (!mCalls.contains(call)) { |
| 1202 | Log.w(this, "Unknown call (%s) asked to disconnect", call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 1203 | } else { |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 1204 | mLocallyDisconnectingCalls.add(call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 1205 | call.disconnect(); |
| 1206 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1207 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1208 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1209 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1210 | * Instructs Telecom to disconnect all calls. |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 1211 | */ |
| 1212 | void disconnectAllCalls() { |
| 1213 | Log.v(this, "disconnectAllCalls"); |
| 1214 | |
| 1215 | for (Call call : mCalls) { |
| 1216 | disconnectCall(call); |
| 1217 | } |
| 1218 | } |
| 1219 | |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 1220 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 1221 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1222 | * Instructs Telecom to put the specified call on hold. Intended to be invoked by the |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1223 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 1224 | * the user hitting the hold button during an active call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1225 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1226 | @VisibleForTesting |
| 1227 | public void holdCall(Call call) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1228 | if (!mCalls.contains(call)) { |
| 1229 | Log.w(this, "Unknown call (%s) asked to be put on hold", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1230 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1231 | Log.d(this, "Putting call on hold: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1232 | call.hold(); |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1237 | * Instructs Telecom to release the specified call from hold. Intended to be invoked by |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1238 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 1239 | * by the user hitting the hold button during a held call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1240 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1241 | @VisibleForTesting |
| 1242 | public void unholdCall(Call call) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1243 | if (!mCalls.contains(call)) { |
| 1244 | Log.w(this, "Unknown call (%s) asked to be removed from hold", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1245 | } else { |
Hall Liu | 874c0f8 | 2016-04-29 18:13:18 -0700 | [diff] [blame] | 1246 | boolean otherCallHeld = false; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 1247 | Log.d(this, "unholding call: (%s)", call); |
Sai Cheemalapati | 45b3e3f | 2014-08-15 09:33:00 -0700 | [diff] [blame] | 1248 | for (Call c : mCalls) { |
Tyler Gunn | e44adb4 | 2015-01-02 10:55:35 -0800 | [diff] [blame] | 1249 | // Only attempt to hold parent calls and not the individual children. |
| 1250 | if (c != null && c.isAlive() && c != call && c.getParentCall() == null) { |
Hall Liu | 874c0f8 | 2016-04-29 18:13:18 -0700 | [diff] [blame] | 1251 | otherCallHeld = true; |
| 1252 | Log.event(c, Log.Events.SWAP); |
Sai Cheemalapati | 45b3e3f | 2014-08-15 09:33:00 -0700 | [diff] [blame] | 1253 | c.hold(); |
| 1254 | } |
| 1255 | } |
Hall Liu | 874c0f8 | 2016-04-29 18:13:18 -0700 | [diff] [blame] | 1256 | if (otherCallHeld) { |
| 1257 | Log.event(call, Log.Events.SWAP); |
| 1258 | } |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1259 | call.unhold(); |
| 1260 | } |
| 1261 | } |
| 1262 | |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 1263 | @Override |
Tyler Gunn | 961694a | 2016-03-21 16:01:40 -0700 | [diff] [blame] | 1264 | public void onExtrasChanged(Call c, int source, Bundle extras) { |
| 1265 | if (source != Call.SOURCE_CONNECTION_SERVICE) { |
| 1266 | return; |
| 1267 | } |
| 1268 | handleCallTechnologyChange(c); |
Brad Ebinger | baf52ba | 2016-03-21 16:38:32 -0700 | [diff] [blame] | 1269 | handleChildAddressChange(c); |
Hall Liu | fc130b2 | 2016-06-15 17:54:48 -0700 | [diff] [blame] | 1270 | updateCanAddCall(); |
Tyler Gunn | 961694a | 2016-03-21 16:01:40 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Brad Ebinger | 3da5764 | 2016-04-04 17:32:19 -0700 | [diff] [blame] | 1273 | // Construct the list of possible PhoneAccounts that the outgoing call can use based on the |
| 1274 | // active calls in CallsManager. If any of the active calls are on a SIM based PhoneAccount, |
| 1275 | // then include only that SIM based PhoneAccount and any non-SIM PhoneAccounts, such as SIP. |
| 1276 | private List<PhoneAccountHandle> constructPossiblePhoneAccounts(Uri handle, UserHandle user) { |
| 1277 | if (handle == null) { |
| 1278 | return Collections.emptyList(); |
| 1279 | } |
| 1280 | List<PhoneAccountHandle> allAccounts = |
| 1281 | mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme(), false, user); |
| 1282 | // First check the Radio SIM Technology |
| 1283 | if(mRadioSimVariants == null) { |
| 1284 | TelephonyManager tm = (TelephonyManager) mContext.getSystemService( |
| 1285 | Context.TELEPHONY_SERVICE); |
| 1286 | // Cache Sim Variants |
| 1287 | mRadioSimVariants = tm.getMultiSimConfiguration(); |
| 1288 | } |
| 1289 | // Only one SIM PhoneAccount can be active at one time for DSDS. Only that SIM PhoneAccount |
| 1290 | // Should be available if a call is already active on the SIM account. |
| 1291 | if(mRadioSimVariants != TelephonyManager.MultiSimVariants.DSDA) { |
| 1292 | List<PhoneAccountHandle> simAccounts = |
| 1293 | mPhoneAccountRegistrar.getSimPhoneAccountsOfCurrentUser(); |
| 1294 | PhoneAccountHandle ongoingCallAccount = null; |
| 1295 | for (Call c : mCalls) { |
| 1296 | if (!c.isDisconnected() && !c.isNew() && simAccounts.contains( |
| 1297 | c.getTargetPhoneAccount())) { |
| 1298 | ongoingCallAccount = c.getTargetPhoneAccount(); |
| 1299 | break; |
| 1300 | } |
| 1301 | } |
| 1302 | if (ongoingCallAccount != null) { |
| 1303 | // Remove all SIM accounts that are not the active SIM from the list. |
| 1304 | simAccounts.remove(ongoingCallAccount); |
| 1305 | allAccounts.removeAll(simAccounts); |
| 1306 | } |
| 1307 | } |
| 1308 | return allAccounts; |
| 1309 | } |
| 1310 | |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 1311 | /** |
| 1312 | * Informs listeners (notably {@link CallAudioManager} of a change to the call's external |
| 1313 | * property. |
| 1314 | * . |
| 1315 | * @param call The call whose external property changed. |
| 1316 | * @param isExternalCall {@code True} if the call is now external, {@code false} otherwise. |
| 1317 | */ |
| 1318 | @Override |
| 1319 | public void onExternalCallChanged(Call call, boolean isExternalCall) { |
| 1320 | Log.v(this, "onConnectionPropertiesChanged: %b", isExternalCall); |
| 1321 | for (CallsManagerListener listener : mListeners) { |
| 1322 | listener.onExternalCallChanged(call, isExternalCall); |
| 1323 | } |
| 1324 | } |
| 1325 | |
Tyler Gunn | 961694a | 2016-03-21 16:01:40 -0700 | [diff] [blame] | 1326 | private void handleCallTechnologyChange(Call call) { |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 1327 | if (call.getExtras() != null |
| 1328 | && call.getExtras().containsKey(TelecomManager.EXTRA_CALL_TECHNOLOGY_TYPE)) { |
| 1329 | |
| 1330 | Integer analyticsCallTechnology = sAnalyticsTechnologyMap.get( |
| 1331 | call.getExtras().getInt(TelecomManager.EXTRA_CALL_TECHNOLOGY_TYPE)); |
| 1332 | if (analyticsCallTechnology == null) { |
| 1333 | analyticsCallTechnology = Analytics.THIRD_PARTY_PHONE; |
| 1334 | } |
| 1335 | call.getAnalytics().addCallTechnology(analyticsCallTechnology); |
| 1336 | } |
| 1337 | } |
| 1338 | |
Brad Ebinger | baf52ba | 2016-03-21 16:38:32 -0700 | [diff] [blame] | 1339 | public void handleChildAddressChange(Call call) { |
| 1340 | if (call.getExtras() != null |
| 1341 | && call.getExtras().containsKey(Connection.EXTRA_CHILD_ADDRESS)) { |
| 1342 | |
| 1343 | String viaNumber = call.getExtras().getString(Connection.EXTRA_CHILD_ADDRESS); |
| 1344 | call.setViaNumber(viaNumber); |
| 1345 | } |
| 1346 | } |
| 1347 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 1348 | /** Called by the in-call UI to change the mute state. */ |
| 1349 | void mute(boolean shouldMute) { |
| 1350 | mCallAudioManager.mute(shouldMute); |
| 1351 | } |
| 1352 | |
| 1353 | /** |
| 1354 | * Called by the in-call UI to change the audio route, for example to change from earpiece to |
| 1355 | * speaker phone. |
| 1356 | */ |
| 1357 | void setAudioRoute(int route) { |
| 1358 | mCallAudioManager.setAudioRoute(route); |
| 1359 | } |
| 1360 | |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 1361 | /** Called by the in-call UI to turn the proximity sensor on. */ |
| 1362 | void turnOnProximitySensor() { |
| 1363 | mProximitySensorManager.turnOn(); |
| 1364 | } |
| 1365 | |
| 1366 | /** |
| 1367 | * Called by the in-call UI to turn the proximity sensor off. |
| 1368 | * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise, |
| 1369 | * the screen will be kept off until the proximity sensor goes negative. |
| 1370 | */ |
| 1371 | void turnOffProximitySensor(boolean screenOnImmediately) { |
| 1372 | mProximitySensorManager.turnOff(screenOnImmediately); |
| 1373 | } |
| 1374 | |
Nancy Chen | f5e5d3c | 2014-10-21 18:45:56 -0700 | [diff] [blame] | 1375 | void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) { |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 1376 | if (!mCalls.contains(call)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1377 | Log.i(this, "Attempted to add account to unknown call %s", call); |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 1378 | } else { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1379 | call.setTargetPhoneAccount(account); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1380 | |
Hall Liu | 4b1759d | 2016-02-02 18:17:40 -0800 | [diff] [blame] | 1381 | if (!call.isNewOutgoingCallIntentBroadcastDone()) { |
| 1382 | return; |
| 1383 | } |
| 1384 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1385 | // Note: emergency calls never go through account selection dialog so they never |
| 1386 | // arrive here. |
| 1387 | if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1388 | call.startCreateConnection(mPhoneAccountRegistrar); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1389 | } else { |
| 1390 | call.disconnect(); |
| 1391 | } |
Nancy Chen | f5e5d3c | 2014-10-21 18:45:56 -0700 | [diff] [blame] | 1392 | |
| 1393 | if (setDefault) { |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1394 | mPhoneAccountRegistrar |
| 1395 | .setUserSelectedOutgoingPhoneAccount(account, call.getInitiatingUser()); |
Nancy Chen | f5e5d3c | 2014-10-21 18:45:56 -0700 | [diff] [blame] | 1396 | } |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 1400 | /** Called when the audio state changes. */ |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 1401 | @VisibleForTesting |
| 1402 | public void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState |
| 1403 | newAudioState) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 1404 | Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1405 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 1406 | listener.onCallAudioStateChanged(oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1407 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1410 | void markCallAsRinging(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 1411 | setCallState(call, CallState.RINGING, "ringing set explicitly"); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1414 | void markCallAsDialing(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 1415 | setCallState(call, CallState.DIALING, "dialing set explicitly"); |
Santos Cordon | d6782cd | 2015-04-16 09:57:50 -0700 | [diff] [blame] | 1416 | maybeMoveToSpeakerPhone(call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 1419 | void markCallAsPulling(Call call) { |
| 1420 | setCallState(call, CallState.PULLING, "pulling set explicitly"); |
| 1421 | maybeMoveToSpeakerPhone(call); |
| 1422 | } |
| 1423 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1424 | void markCallAsActive(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 1425 | setCallState(call, CallState.ACTIVE, "active set explicitly"); |
Santos Cordon | d6782cd | 2015-04-16 09:57:50 -0700 | [diff] [blame] | 1426 | maybeMoveToSpeakerPhone(call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1429 | void markCallAsOnHold(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 1430 | setCallState(call, CallState.ON_HOLD, "on-hold set explicitly"); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 1433 | /** |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1434 | * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the |
| 1435 | * last live call, then also disconnect from the in-call controller. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 1436 | * |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 1437 | * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 1438 | */ |
Andrew Lee | 701dc00 | 2014-09-11 21:29:12 -0700 | [diff] [blame] | 1439 | void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) { |
| 1440 | call.setDisconnectCause(disconnectCause); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 1441 | setCallState(call, CallState.DISCONNECTED, "disconnected set explicitly"); |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1444 | /** |
Ihab Awad | a02bef5 | 2014-08-13 18:18:42 -0700 | [diff] [blame] | 1445 | * Removes an existing disconnected call, and notifies the in-call app. |
| 1446 | */ |
| 1447 | void markCallAsRemoved(Call call) { |
| 1448 | removeCall(call); |
Tyler Gunn | 7d58ba5 | 2016-07-25 15:05:48 -0700 | [diff] [blame] | 1449 | Call foregroundCall = mCallAudioManager.getPossiblyHeldForegroundCall(); |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 1450 | if (mLocallyDisconnectingCalls.contains(call)) { |
| 1451 | mLocallyDisconnectingCalls.remove(call); |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 1452 | if (foregroundCall != null && foregroundCall.getState() == CallState.ON_HOLD) { |
| 1453 | foregroundCall.unhold(); |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 1454 | } |
Tyler Gunn | 7d58ba5 | 2016-07-25 15:05:48 -0700 | [diff] [blame] | 1455 | } else if (foregroundCall != null && |
| 1456 | !foregroundCall.can(Connection.CAPABILITY_SUPPORT_HOLD) && |
| 1457 | foregroundCall.getState() == CallState.ON_HOLD) { |
| 1458 | |
| 1459 | // The new foreground call is on hold, however the carrier does not display the hold |
| 1460 | // button in the UI. Therefore, we need to auto unhold the held call since the user has |
| 1461 | // no means of unholding it themselves. |
| 1462 | Log.i(this, "Auto-unholding held foreground call (call doesn't support hold)"); |
| 1463 | foregroundCall.unhold(); |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 1464 | } |
Ihab Awad | a02bef5 | 2014-08-13 18:18:42 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | /** |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 1468 | * Cleans up any calls currently associated with the specified connection service when the |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 1469 | * service binder disconnects unexpectedly. |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 1470 | * |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 1471 | * @param service The connection service that disconnected. |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 1472 | */ |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 1473 | void handleConnectionServiceDeath(ConnectionServiceWrapper service) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 1474 | if (service != null) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 1475 | for (Call call : mCalls) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 1476 | if (call.getConnectionService() == service) { |
Yorke Lee | 2af16b6 | 2014-11-11 17:36:57 -0800 | [diff] [blame] | 1477 | if (call.getState() != CallState.DISCONNECTED) { |
| 1478 | markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR)); |
| 1479 | } |
| 1480 | markCallAsRemoved(call); |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 1481 | } |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 1486 | /** |
| 1487 | * Determines if the {@link CallsManager} has any non-external calls. |
| 1488 | * |
| 1489 | * @return {@code True} if there are any non-external calls, {@code false} otherwise. |
| 1490 | */ |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1491 | boolean hasAnyCalls() { |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 1492 | if (mCalls.isEmpty()) { |
| 1493 | return false; |
| 1494 | } |
| 1495 | |
| 1496 | for (Call call : mCalls) { |
| 1497 | if (!call.isExternalCall()) { |
| 1498 | return true; |
| 1499 | } |
| 1500 | } |
| 1501 | return false; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 1504 | boolean hasActiveOrHoldingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 1505 | return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | boolean hasRingingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 1509 | return getFirstCallWithState(CallState.RINGING) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1512 | boolean onMediaButton(int type) { |
| 1513 | if (hasAnyCalls()) { |
| 1514 | if (HeadsetMediaButton.SHORT_PRESS == type) { |
| 1515 | Call ringingCall = getFirstCallWithState(CallState.RINGING); |
| 1516 | if (ringingCall == null) { |
| 1517 | mCallAudioManager.toggleMute(); |
| 1518 | return true; |
| 1519 | } else { |
Hall Liu | 3777a7e | 2016-07-25 14:46:48 -0700 | [diff] [blame] | 1520 | ringingCall.answer(VideoProfile.STATE_AUDIO_ONLY); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1521 | return true; |
| 1522 | } |
| 1523 | } else if (HeadsetMediaButton.LONG_PRESS == type) { |
| 1524 | Log.d(this, "handleHeadsetHook: longpress -> hangup"); |
| 1525 | Call callToHangup = getFirstCallWithState( |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 1526 | CallState.RINGING, CallState.DIALING, CallState.PULLING, CallState.ACTIVE, |
| 1527 | CallState.ON_HOLD); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1528 | if (callToHangup != null) { |
| 1529 | callToHangup.disconnect(); |
| 1530 | return true; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
| 1537 | /** |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1538 | * Returns true if telecom supports adding another top-level call. |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1539 | */ |
Hall Liu | 28b82f0 | 2016-07-26 17:38:56 -0700 | [diff] [blame] | 1540 | @VisibleForTesting |
| 1541 | public boolean canAddCall() { |
Hall Liu | f1422e7 | 2016-01-27 11:07:07 -0800 | [diff] [blame] | 1542 | boolean isDeviceProvisioned = Settings.Global.getInt(mContext.getContentResolver(), |
| 1543 | Settings.Global.DEVICE_PROVISIONED, 0) != 0; |
| 1544 | if (!isDeviceProvisioned) { |
| 1545 | Log.d(TAG, "Device not provisioned, canAddCall is false."); |
| 1546 | return false; |
| 1547 | } |
| 1548 | |
Andrew Lee | 113ad62 | 2014-12-01 18:33:10 -0800 | [diff] [blame] | 1549 | if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) { |
| 1550 | return false; |
| 1551 | } |
| 1552 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1553 | int count = 0; |
| 1554 | for (Call call : mCalls) { |
| 1555 | if (call.isEmergencyCall()) { |
| 1556 | // We never support add call if one of the calls is an emergency call. |
| 1557 | return false; |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 1558 | } else if (call.isExternalCall()) { |
| 1559 | // External calls don't count. |
| 1560 | continue; |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1561 | } else if (call.getParentCall() == null) { |
| 1562 | count++; |
| 1563 | } |
Hall Liu | fc130b2 | 2016-06-15 17:54:48 -0700 | [diff] [blame] | 1564 | Bundle extras = call.getExtras(); |
| 1565 | if (extras != null) { |
Hall Liu | 140c840 | 2016-07-06 16:30:46 -0700 | [diff] [blame] | 1566 | if (extras.getBoolean(Connection.EXTRA_DISABLE_ADD_CALL, false)) { |
| 1567 | return false; |
| 1568 | } |
Hall Liu | fc130b2 | 2016-06-15 17:54:48 -0700 | [diff] [blame] | 1569 | } |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1570 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1571 | // We do not check states for canAddCall. We treat disconnected calls the same |
| 1572 | // and wait until they are removed instead. If we didn't count disconnected calls, |
| 1573 | // we could put InCallServices into a state where they are showing two calls but |
| 1574 | // also support add-call. Technically it's right, but overall looks better (UI-wise) |
| 1575 | // and acts better if we wait until the call is removed. |
| 1576 | if (count >= MAXIMUM_TOP_LEVEL_CALLS) { |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1577 | return false; |
| 1578 | } |
| 1579 | } |
Hall Liu | fc130b2 | 2016-06-15 17:54:48 -0700 | [diff] [blame] | 1580 | |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1581 | return true; |
| 1582 | } |
| 1583 | |
Ihab Awad | a365390 | 2015-01-23 13:44:46 -0800 | [diff] [blame] | 1584 | @VisibleForTesting |
P.Y. Laligand | d35be75 | 2015-03-06 14:12:43 -0800 | [diff] [blame] | 1585 | public Call getRingingCall() { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 1586 | return getFirstCallWithState(CallState.RINGING); |
| 1587 | } |
| 1588 | |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1589 | @VisibleForTesting |
| 1590 | public Call getActiveCall() { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 1591 | return getFirstCallWithState(CallState.ACTIVE); |
| 1592 | } |
| 1593 | |
Nancy Chen | 05a9e40 | 2014-09-26 14:14:32 -0700 | [diff] [blame] | 1594 | Call getDialingCall() { |
| 1595 | return getFirstCallWithState(CallState.DIALING); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1598 | @VisibleForTesting |
| 1599 | public Call getHeldCall() { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 1600 | return getFirstCallWithState(CallState.ON_HOLD); |
| 1601 | } |
| 1602 | |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1603 | @VisibleForTesting |
| 1604 | public int getNumHeldCalls() { |
Santos Cordon | c0ca76e | 2014-10-21 15:54:19 -0700 | [diff] [blame] | 1605 | int count = 0; |
| 1606 | for (Call call : mCalls) { |
| 1607 | if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) { |
| 1608 | count++; |
| 1609 | } |
| 1610 | } |
| 1611 | return count; |
| 1612 | } |
| 1613 | |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 1614 | @VisibleForTesting |
| 1615 | public Call getOutgoingCall() { |
Roshan Pius | 7d7cf27 | 2015-08-28 11:10:56 -0700 | [diff] [blame] | 1616 | return getFirstCallWithState(OUTGOING_CALL_STATES); |
| 1617 | } |
| 1618 | |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 1619 | @VisibleForTesting |
| 1620 | public Call getFirstCallWithState(int... states) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1621 | return getFirstCallWithState(null, states); |
| 1622 | } |
| 1623 | |
Brad Ebinger | 6e8f3d7 | 2016-06-20 11:35:42 -0700 | [diff] [blame] | 1624 | @VisibleForTesting |
| 1625 | public PhoneNumberUtilsAdapter getPhoneNumberUtilsAdapter() { |
| 1626 | return mPhoneNumberUtilsAdapter; |
| 1627 | } |
| 1628 | |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1629 | /** |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1630 | * Returns the first call that it finds with the given states. The states are treated as having |
| 1631 | * priority order so that any call with the first state will be returned before any call with |
| 1632 | * states listed later in the parameter list. |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1633 | * |
| 1634 | * @param callToSkip Call that this method should skip while searching |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1635 | */ |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1636 | Call getFirstCallWithState(Call callToSkip, int... states) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1637 | for (int currentState : states) { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 1638 | // check the foreground first |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 1639 | Call foregroundCall = getForegroundCall(); |
| 1640 | if (foregroundCall != null && foregroundCall.getState() == currentState) { |
| 1641 | return foregroundCall; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1644 | for (Call call : mCalls) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1645 | if (Objects.equals(callToSkip, call)) { |
| 1646 | continue; |
| 1647 | } |
| 1648 | |
| 1649 | // Only operate on top-level calls |
| 1650 | if (call.getParentCall() != null) { |
| 1651 | continue; |
| 1652 | } |
| 1653 | |
Tyler Gunn | f15dc33 | 2016-06-07 16:01:41 -0700 | [diff] [blame] | 1654 | if (call.isExternalCall()) { |
| 1655 | continue; |
| 1656 | } |
| 1657 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1658 | if (currentState == call.getState()) { |
| 1659 | return call; |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | return null; |
| 1664 | } |
| 1665 | |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1666 | Call createConferenceCall( |
Tyler Gunn | 8452be0 | 2015-09-17 09:57:02 -0700 | [diff] [blame] | 1667 | String callId, |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1668 | PhoneAccountHandle phoneAccount, |
| 1669 | ParcelableConference parcelableConference) { |
Tyler Gunn | d92e737 | 2015-01-09 15:59:45 -0800 | [diff] [blame] | 1670 | |
| 1671 | // If the parceled conference specifies a connect time, use it; otherwise default to 0, |
| 1672 | // which is the default value for new Calls. |
| 1673 | long connectTime = |
| 1674 | parcelableConference.getConnectTimeMillis() == |
| 1675 | Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 : |
| 1676 | parcelableConference.getConnectTimeMillis(); |
| 1677 | |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1678 | Call call = new Call( |
Tyler Gunn | 8452be0 | 2015-09-17 09:57:02 -0700 | [diff] [blame] | 1679 | callId, |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1680 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 1681 | this, |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 1682 | mLock, |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1683 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 1684 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 1685 | mCallerInfoAsyncQueryFactory, |
Brad Ebinger | 6e8f3d7 | 2016-06-20 11:35:42 -0700 | [diff] [blame] | 1686 | mPhoneNumberUtilsAdapter, |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1687 | null /* handle */, |
| 1688 | null /* gatewayInfo */, |
| 1689 | null /* connectionManagerPhoneAccount */, |
| 1690 | phoneAccount, |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 1691 | Call.CALL_DIRECTION_UNDEFINED /* callDirection */, |
| 1692 | false /* forceAttachToExistingConnection */, |
Tyler Gunn | d92e737 | 2015-01-09 15:59:45 -0800 | [diff] [blame] | 1693 | true /* isConference */, |
| 1694 | connectTime); |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1695 | |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 1696 | setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()), |
| 1697 | "new conference call"); |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1698 | call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities()); |
Tyler Gunn | 571d5e6 | 2016-03-15 15:55:18 -0700 | [diff] [blame] | 1699 | call.setConnectionProperties(parcelableConference.getConnectionProperties()); |
Tyler Gunn | 13e8a69 | 2016-08-03 18:38:00 -0700 | [diff] [blame] | 1700 | call.setVideoState(parcelableConference.getVideoState()); |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 1701 | call.setVideoProvider(parcelableConference.getVideoProvider()); |
Andrew Lee | 57412d3 | 2015-04-14 13:38:44 -0700 | [diff] [blame] | 1702 | call.setStatusHints(parcelableConference.getStatusHints()); |
Tyler Gunn | 961694a | 2016-03-21 16:01:40 -0700 | [diff] [blame] | 1703 | call.putExtras(Call.SOURCE_CONNECTION_SERVICE, parcelableConference.getExtras()); |
Tyler Gunn | 9b618b8 | 2016-10-17 15:54:35 -0700 | [diff] [blame] | 1704 | // In case this Conference was added via a ConnectionManager, keep track of the original |
| 1705 | // Connection ID as created by the originating ConnectionService. |
| 1706 | Bundle extras = parcelableConference.getExtras(); |
| 1707 | if (extras != null && extras.containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) { |
| 1708 | call.setOriginalConnectionId(extras.getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID)); |
| 1709 | } |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1710 | |
| 1711 | // TODO: Move this to be a part of addCall() |
| 1712 | call.addListener(this); |
| 1713 | addCall(call); |
| 1714 | return call; |
| 1715 | } |
| 1716 | |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 1717 | /** |
| 1718 | * @return the call state currently tracked by {@link PhoneStateBroadcaster} |
| 1719 | */ |
| 1720 | int getCallState() { |
| 1721 | return mPhoneStateBroadcaster.getCallState(); |
| 1722 | } |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 1723 | |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 1724 | /** |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1725 | * Retrieves the {@link PhoneAccountRegistrar}. |
| 1726 | * |
| 1727 | * @return The {@link PhoneAccountRegistrar}. |
| 1728 | */ |
| 1729 | PhoneAccountRegistrar getPhoneAccountRegistrar() { |
| 1730 | return mPhoneAccountRegistrar; |
| 1731 | } |
| 1732 | |
| 1733 | /** |
| 1734 | * Retrieves the {@link MissedCallNotifier} |
| 1735 | * @return The {@link MissedCallNotifier}. |
| 1736 | */ |
| 1737 | MissedCallNotifier getMissedCallNotifier() { |
| 1738 | return mMissedCallNotifier; |
| 1739 | } |
| 1740 | |
| 1741 | /** |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 1742 | * Reject an incoming call and manually add it to the Call Log. |
| 1743 | * @param incomingCall Incoming call that has been rejected |
| 1744 | */ |
| 1745 | private void rejectCallAndLog(Call incomingCall) { |
Tyler Gunn | 165ee75 | 2016-09-06 13:40:10 -0700 | [diff] [blame] | 1746 | if (incomingCall.getConnectionService() != null) { |
| 1747 | // Only reject the call if it has not already been destroyed. If a call ends while |
| 1748 | // incoming call filtering is taking place, it is possible that the call has already |
| 1749 | // been destroyed, and as such it will be impossible to send the reject to the |
| 1750 | // associated ConnectionService. |
| 1751 | incomingCall.reject(false, null); |
| 1752 | } else { |
| 1753 | Log.i(this, "rejectCallAndLog - call already destroyed."); |
| 1754 | } |
| 1755 | |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 1756 | // Since the call was not added to the list of calls, we have to call the missed |
| 1757 | // call notifier and the call logger manually. |
| 1758 | // Do we need missed call notification for direct to Voicemail calls? |
Ta-wei Yen | 982c0bd | 2016-04-14 13:59:54 -0700 | [diff] [blame] | 1759 | mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE, |
| 1760 | true /*showNotificationForMissedCall*/); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1764 | * Adds the specified call to the main list of live calls. |
| 1765 | * |
| 1766 | * @param call The call to add. |
| 1767 | */ |
| 1768 | private void addCall(Call call) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1769 | Trace.beginSection("addCall"); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1770 | Log.v(this, "addCall(%s)", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1771 | call.addListener(this); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1772 | mCalls.add(call); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1773 | |
Sailesh Nepal | 703a1af | 2016-04-14 20:10:12 -0700 | [diff] [blame] | 1774 | // Specifies the time telecom finished routing the call. This is used by the dialer for |
| 1775 | // analytics. |
| 1776 | Bundle extras = call.getIntentExtras(); |
| 1777 | extras.putLong(TelecomManager.EXTRA_CALL_TELECOM_ROUTING_END_TIME_MILLIS, |
| 1778 | SystemClock.elapsedRealtime()); |
| 1779 | |
Hall Liu | fc130b2 | 2016-06-15 17:54:48 -0700 | [diff] [blame] | 1780 | updateCanAddCall(); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1781 | // onCallAdded for calls which immediately take the foreground (like the first call). |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1782 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1783 | if (Log.SYSTRACE_DEBUG) { |
| 1784 | Trace.beginSection(listener.getClass().toString() + " addCall"); |
| 1785 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1786 | listener.onCallAdded(call); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1787 | if (Log.SYSTRACE_DEBUG) { |
| 1788 | Trace.endSection(); |
| 1789 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1790 | } |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1791 | Trace.endSection(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1794 | private void removeCall(Call call) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1795 | Trace.beginSection("removeCall"); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 1796 | Log.v(this, "removeCall(%s)", call); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 1797 | |
Santos Cordon | 672321e | 2014-08-21 14:38:58 -0700 | [diff] [blame] | 1798 | call.setParentCall(null); // need to clean up parent relationship before destroying. |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 1799 | call.removeListener(this); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 1800 | call.clearConnectionService(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1801 | |
| 1802 | boolean shouldNotify = false; |
| 1803 | if (mCalls.contains(call)) { |
| 1804 | mCalls.remove(call); |
| 1805 | shouldNotify = true; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1806 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1807 | |
Santos Cordon | 2685dab | 2015-04-17 17:12:09 -0700 | [diff] [blame] | 1808 | call.destroy(); |
| 1809 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1810 | // Only broadcast changes for calls that are being tracked. |
| 1811 | if (shouldNotify) { |
Hall Liu | fc130b2 | 2016-06-15 17:54:48 -0700 | [diff] [blame] | 1812 | updateCanAddCall(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1813 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1814 | if (Log.SYSTRACE_DEBUG) { |
| 1815 | Trace.beginSection(listener.getClass().toString() + " onCallRemoved"); |
| 1816 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1817 | listener.onCallRemoved(call); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1818 | if (Log.SYSTRACE_DEBUG) { |
| 1819 | Trace.endSection(); |
| 1820 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1821 | } |
Yorke Lee | 604a90f | 2014-10-20 12:13:32 -0700 | [diff] [blame] | 1822 | } |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1823 | Trace.endSection(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1824 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 1825 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1826 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 1827 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1828 | * |
| 1829 | * @param call The call. |
| 1830 | * @param newState The new state of the call. |
| 1831 | */ |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 1832 | private void setCallState(Call call, int newState, String tag) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 1833 | if (call == null) { |
| 1834 | return; |
| 1835 | } |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1836 | int oldState = call.getState(); |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 1837 | Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState), |
| 1838 | CallState.toString(newState), call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1839 | if (newState != oldState) { |
| 1840 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 1841 | // us that the call is in a particular state, we allow it even if it doesn't make |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1842 | // sense (e.g., STATE_ACTIVE -> STATE_RINGING). |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1843 | // TODO: Consider putting a stop to the above and turning CallState |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1844 | // into a well-defined state machine. |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1845 | // TODO: Define expected state transitions here, and log when an |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1846 | // unexpected transition occurs. |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 1847 | call.setState(newState, tag); |
Xueren Zhang | c0e8e9d | 2015-03-06 14:16:55 +0100 | [diff] [blame] | 1848 | maybeShowErrorDialogOnDisconnect(call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1849 | |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1850 | Trace.beginSection("onCallStateChanged"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1851 | // Only broadcast state change for calls that are being tracked. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1852 | if (mCalls.contains(call)) { |
Hall Liu | fc130b2 | 2016-06-15 17:54:48 -0700 | [diff] [blame] | 1853 | updateCanAddCall(); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1854 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1855 | if (Log.SYSTRACE_DEBUG) { |
| 1856 | Trace.beginSection(listener.getClass().toString() + " onCallStateChanged"); |
| 1857 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1858 | listener.onCallStateChanged(call, oldState, newState); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1859 | if (Log.SYSTRACE_DEBUG) { |
| 1860 | Trace.endSection(); |
| 1861 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1862 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1863 | } |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1864 | Trace.endSection(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1868 | private void updateCanAddCall() { |
| 1869 | boolean newCanAddCall = canAddCall(); |
| 1870 | if (newCanAddCall != mCanAddCall) { |
| 1871 | mCanAddCall = newCanAddCall; |
| 1872 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1873 | if (Log.SYSTRACE_DEBUG) { |
| 1874 | Trace.beginSection(listener.getClass().toString() + " updateCanAddCall"); |
| 1875 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1876 | listener.onCanAddCallChanged(mCanAddCall); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1877 | if (Log.SYSTRACE_DEBUG) { |
| 1878 | Trace.endSection(); |
| 1879 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1884 | private boolean isPotentialMMICode(Uri handle) { |
| 1885 | return (handle != null && handle.getSchemeSpecificPart() != null |
| 1886 | && handle.getSchemeSpecificPart().contains("#")); |
| 1887 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1888 | |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 1889 | /** |
| 1890 | * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are |
| 1891 | * MMI codes which can be dialed when one or more calls are in progress. |
| 1892 | * <P> |
| 1893 | * Checks for numbers formatted similar to the MMI codes defined in: |
Amit Mahajan | 8c4e22a | 2015-12-14 13:24:15 -0800 | [diff] [blame] | 1894 | * {@link com.android.internal.telephony.Phone#handleInCallMmiCommands(String)} |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 1895 | * |
| 1896 | * @param handle The URI to call. |
| 1897 | * @return {@code True} if the URI represents a number which could be an in-call MMI code. |
| 1898 | */ |
| 1899 | private boolean isPotentialInCallMMICode(Uri handle) { |
| 1900 | if (handle != null && handle.getSchemeSpecificPart() != null && |
Xueren Zhang | c0e8e9d | 2015-03-06 14:16:55 +0100 | [diff] [blame] | 1901 | handle.getScheme() != null && |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 1902 | handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) { |
| 1903 | |
| 1904 | String dialedNumber = handle.getSchemeSpecificPart(); |
| 1905 | return (dialedNumber.equals("0") || |
| 1906 | (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) || |
| 1907 | (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) || |
| 1908 | dialedNumber.equals("3") || |
| 1909 | dialedNumber.equals("4") || |
| 1910 | dialedNumber.equals("5")); |
| 1911 | } |
| 1912 | return false; |
| 1913 | } |
| 1914 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1915 | private int getNumCallsWithState(int... states) { |
| 1916 | int count = 0; |
| 1917 | for (int state : states) { |
| 1918 | for (Call call : mCalls) { |
Tyler Gunn | f15dc33 | 2016-06-07 16:01:41 -0700 | [diff] [blame] | 1919 | if (call.getParentCall() == null && call.getState() == state && |
| 1920 | !call.isExternalCall()) { |
| 1921 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1922 | count++; |
| 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | return count; |
| 1927 | } |
| 1928 | |
| 1929 | private boolean hasMaximumLiveCalls() { |
| 1930 | return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES); |
| 1931 | } |
| 1932 | |
| 1933 | private boolean hasMaximumHoldingCalls() { |
| 1934 | return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD); |
| 1935 | } |
| 1936 | |
| 1937 | private boolean hasMaximumRingingCalls() { |
| 1938 | return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING); |
| 1939 | } |
| 1940 | |
| 1941 | private boolean hasMaximumOutgoingCalls() { |
| 1942 | return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES); |
| 1943 | } |
| 1944 | |
Roshan Pius | 4de4a89 | 2015-08-24 11:06:58 -0700 | [diff] [blame] | 1945 | private boolean hasMaximumDialingCalls() { |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 1946 | return MAXIMUM_DIALING_CALLS <= getNumCallsWithState(CallState.DIALING, CallState.PULLING); |
Roshan Pius | 4de4a89 | 2015-08-24 11:06:58 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1949 | private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) { |
| 1950 | if (hasMaximumLiveCalls()) { |
| 1951 | // NOTE: If the amount of live calls changes beyond 1, this logic will probably |
| 1952 | // have to change. |
Tony Mak | f5a32e4 | 2016-02-02 12:10:26 +0000 | [diff] [blame] | 1953 | Call liveCall = getFirstCallWithState(LIVE_CALL_STATES); |
Anju Mathapati | c88574c | 2014-12-16 14:46:19 +0530 | [diff] [blame] | 1954 | Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " + |
| 1955 | liveCall); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1956 | |
Santos Cordon | d176650 | 2014-09-26 15:45:39 -0700 | [diff] [blame] | 1957 | if (call == liveCall) { |
| 1958 | // If the call is already the foreground call, then we are golden. |
Santos Cordon | 9269451 | 2015-04-23 14:47:28 -0700 | [diff] [blame] | 1959 | // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT |
Santos Cordon | d176650 | 2014-09-26 15:45:39 -0700 | [diff] [blame] | 1960 | // state since the call was already populated into the list. |
| 1961 | return true; |
| 1962 | } |
| 1963 | |
Andrew Lee | e659518 | 2014-09-23 18:43:05 -0700 | [diff] [blame] | 1964 | if (hasMaximumOutgoingCalls()) { |
Yorke Lee | 867907d | 2015-06-03 12:24:43 -0700 | [diff] [blame] | 1965 | Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES); |
| 1966 | if (isEmergency && !outgoingCall.isEmergencyCall()) { |
| 1967 | // Disconnect the current outgoing call if it's not an emergency call. If the |
| 1968 | // user tries to make two outgoing calls to different emergency call numbers, |
| 1969 | // we will try to connect the first outgoing call. |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 1970 | call.getAnalytics().setCallIsAdditional(true); |
| 1971 | outgoingCall.getAnalytics().setCallIsInterrupted(true); |
Yorke Lee | 867907d | 2015-06-03 12:24:43 -0700 | [diff] [blame] | 1972 | outgoingCall.disconnect(); |
| 1973 | return true; |
| 1974 | } |
| 1975 | if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) { |
| 1976 | // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT} |
| 1977 | // state, just disconnect it since the user has explicitly started a new call. |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 1978 | call.getAnalytics().setCallIsAdditional(true); |
| 1979 | outgoingCall.getAnalytics().setCallIsInterrupted(true); |
Yorke Lee | 867907d | 2015-06-03 12:24:43 -0700 | [diff] [blame] | 1980 | outgoingCall.disconnect(); |
| 1981 | return true; |
Andrew Lee | e659518 | 2014-09-23 18:43:05 -0700 | [diff] [blame] | 1982 | } |
| 1983 | return false; |
| 1984 | } |
| 1985 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1986 | if (hasMaximumHoldingCalls()) { |
| 1987 | // There is no more room for any more calls, unless it's an emergency. |
| 1988 | if (isEmergency) { |
| 1989 | // Kill the current active call, this is easier then trying to disconnect a |
| 1990 | // holding call and hold an active call. |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 1991 | call.getAnalytics().setCallIsAdditional(true); |
| 1992 | liveCall.getAnalytics().setCallIsInterrupted(true); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1993 | liveCall.disconnect(); |
| 1994 | return true; |
| 1995 | } |
| 1996 | return false; // No more room! |
| 1997 | } |
| 1998 | |
| 1999 | // We have room for at least one more holding call at this point. |
| 2000 | |
Tyler Gunn | e4cd916 | 2015-08-11 15:36:04 -0700 | [diff] [blame] | 2001 | // TODO: Remove once b/23035408 has been corrected. |
| 2002 | // If the live call is a conference, it will not have a target phone account set. This |
| 2003 | // means the check to see if the live call has the same target phone account as the new |
| 2004 | // call will not cause us to bail early. As a result, we'll end up holding the |
| 2005 | // ongoing conference call. However, the ConnectionService is already doing that. This |
| 2006 | // has caused problems with some carriers. As a workaround until b/23035408 is |
| 2007 | // corrected, we will try and get the target phone account for one of the conference's |
| 2008 | // children and use that instead. |
| 2009 | PhoneAccountHandle liveCallPhoneAccount = liveCall.getTargetPhoneAccount(); |
| 2010 | if (liveCallPhoneAccount == null && liveCall.isConference() && |
| 2011 | !liveCall.getChildCalls().isEmpty()) { |
| 2012 | liveCallPhoneAccount = getFirstChildPhoneAccount(liveCall); |
| 2013 | Log.i(this, "makeRoomForOutgoingCall: using child call PhoneAccount = " + |
| 2014 | liveCallPhoneAccount); |
| 2015 | } |
| 2016 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 2017 | // First thing, if we are trying to make a call with the same phone account as the live |
| 2018 | // call, then allow it so that the connection service can make its own decision about |
| 2019 | // how to handle the new call relative to the current one. |
Tyler Gunn | e4cd916 | 2015-08-11 15:36:04 -0700 | [diff] [blame] | 2020 | if (Objects.equals(liveCallPhoneAccount, call.getTargetPhoneAccount())) { |
| 2021 | Log.i(this, "makeRoomForOutgoingCall: phoneAccount matches."); |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 2022 | call.getAnalytics().setCallIsAdditional(true); |
| 2023 | liveCall.getAnalytics().setCallIsInterrupted(true); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 2024 | return true; |
| 2025 | } else if (call.getTargetPhoneAccount() == null) { |
| 2026 | // Without a phone account, we can't say reliably that the call will fail. |
| 2027 | // If the user chooses the same phone account as the live call, then it's |
| 2028 | // still possible that the call can be made (like with CDMA calls not supporting |
| 2029 | // hold but they still support adding a call by going immediately into conference |
| 2030 | // mode). Return true here and we'll run this code again after user chooses an |
| 2031 | // account. |
| 2032 | return true; |
| 2033 | } |
| 2034 | |
| 2035 | // Try to hold the live call before attempting the new outgoing call. |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 2036 | if (liveCall.can(Connection.CAPABILITY_HOLD)) { |
Tyler Gunn | e4cd916 | 2015-08-11 15:36:04 -0700 | [diff] [blame] | 2037 | Log.i(this, "makeRoomForOutgoingCall: holding live call."); |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 2038 | call.getAnalytics().setCallIsAdditional(true); |
| 2039 | liveCall.getAnalytics().setCallIsInterrupted(true); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 2040 | liveCall.hold(); |
| 2041 | return true; |
| 2042 | } |
| 2043 | |
| 2044 | // The live call cannot be held so we're out of luck here. There's no room. |
| 2045 | return false; |
| 2046 | } |
| 2047 | return true; |
| 2048 | } |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 2049 | |
| 2050 | /** |
Tyler Gunn | e4cd916 | 2015-08-11 15:36:04 -0700 | [diff] [blame] | 2051 | * Given a call, find the first non-null phone account handle of its children. |
| 2052 | * |
| 2053 | * @param parentCall The parent call. |
| 2054 | * @return The first non-null phone account handle of the children, or {@code null} if none. |
| 2055 | */ |
| 2056 | private PhoneAccountHandle getFirstChildPhoneAccount(Call parentCall) { |
| 2057 | for (Call childCall : parentCall.getChildCalls()) { |
| 2058 | PhoneAccountHandle childPhoneAccount = childCall.getTargetPhoneAccount(); |
| 2059 | if (childPhoneAccount != null) { |
| 2060 | return childPhoneAccount; |
| 2061 | } |
| 2062 | } |
| 2063 | return null; |
| 2064 | } |
| 2065 | |
| 2066 | /** |
Santos Cordon | d6782cd | 2015-04-16 09:57:50 -0700 | [diff] [blame] | 2067 | * Checks to see if the call should be on speakerphone and if so, set it. |
| 2068 | */ |
| 2069 | private void maybeMoveToSpeakerPhone(Call call) { |
| 2070 | if (call.getStartWithSpeakerphoneOn()) { |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 2071 | setAudioRoute(CallAudioState.ROUTE_SPEAKER); |
Santos Cordon | d6782cd | 2015-04-16 09:57:50 -0700 | [diff] [blame] | 2072 | call.setStartWithSpeakerphoneOn(false); |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | /** |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 2077 | * Creates a new call for an existing connection. |
| 2078 | * |
| 2079 | * @param callId The id of the new call. |
| 2080 | * @param connection The connection information. |
| 2081 | * @return The new call. |
| 2082 | */ |
| 2083 | Call createCallForExistingConnection(String callId, ParcelableConnection connection) { |
Hall Liu | 4b9e556 | 2016-07-14 15:34:56 -0700 | [diff] [blame] | 2084 | boolean isDowngradedConference = (connection.getConnectionProperties() |
| 2085 | & Connection.PROPERTY_IS_DOWNGRADED_CONFERENCE) != 0; |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 2086 | Call call = new Call( |
Tyler Gunn | 234d542 | 2015-12-08 14:27:49 -0800 | [diff] [blame] | 2087 | callId, |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 2088 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 2089 | this, |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 2090 | mLock, |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 2091 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 2092 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 2093 | mCallerInfoAsyncQueryFactory, |
Brad Ebinger | 6e8f3d7 | 2016-06-20 11:35:42 -0700 | [diff] [blame] | 2094 | mPhoneNumberUtilsAdapter, |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 2095 | connection.getHandle() /* handle */, |
| 2096 | null /* gatewayInfo */, |
| 2097 | null /* connectionManagerPhoneAccount */, |
| 2098 | connection.getPhoneAccount(), /* targetPhoneAccountHandle */ |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 2099 | Call.CALL_DIRECTION_UNDEFINED /* callDirection */, |
| 2100 | false /* forceAttachToExistingConnection */, |
Hall Liu | 4b9e556 | 2016-07-14 15:34:56 -0700 | [diff] [blame] | 2101 | isDowngradedConference /* isConference */, |
Roshan Pius | 084ab08 | 2015-07-15 12:17:04 -0700 | [diff] [blame] | 2102 | connection.getConnectTimeMillis() /* connectTimeMillis */); |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 2103 | |
Hall Liu | 3258720 | 2015-11-18 11:10:08 -0800 | [diff] [blame] | 2104 | call.initAnalytics(); |
| 2105 | call.getAnalytics().setCreatedFromExistingConnection(true); |
| 2106 | |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 2107 | setCallState(call, Call.getStateFromConnectionState(connection.getState()), |
| 2108 | "existing connection"); |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 2109 | call.setConnectionCapabilities(connection.getConnectionCapabilities()); |
Tyler Gunn | 571d5e6 | 2016-03-15 15:55:18 -0700 | [diff] [blame] | 2110 | call.setConnectionProperties(connection.getConnectionProperties()); |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 2111 | call.setCallerDisplayName(connection.getCallerDisplayName(), |
| 2112 | connection.getCallerDisplayNamePresentation()); |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 2113 | call.addListener(this); |
Tyler Gunn | 9b618b8 | 2016-10-17 15:54:35 -0700 | [diff] [blame] | 2114 | |
| 2115 | // In case this connection was added via a ConnectionManager, keep track of the original |
| 2116 | // Connection ID as created by the originating ConnectionService. |
| 2117 | Bundle extras = connection.getExtras(); |
| 2118 | if (extras != null && extras.containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) { |
| 2119 | call.setOriginalConnectionId(extras.getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID)); |
| 2120 | } |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 2121 | addCall(call); |
| 2122 | |
| 2123 | return call; |
| 2124 | } |
| 2125 | |
| 2126 | /** |
Tyler Gunn | 9b618b8 | 2016-10-17 15:54:35 -0700 | [diff] [blame] | 2127 | * Determines whether Telecom already knows about a Connection added via the |
| 2128 | * {@link android.telecom.ConnectionService#addExistingConnection(PhoneAccountHandle, |
| 2129 | * Connection)} API via a ConnectionManager. |
| 2130 | * |
| 2131 | * See {@link Connection#EXTRA_ORIGINAL_CONNECTION_ID}. |
| 2132 | * @param originalConnectionId The new connection ID to check. |
| 2133 | * @return {@code true} if this connection is already known by Telecom. |
| 2134 | */ |
| 2135 | Call getAlreadyAddedConnection(String originalConnectionId) { |
| 2136 | Optional<Call> existingCall = mCalls.stream() |
| 2137 | .filter(call -> originalConnectionId.equals(call.getOriginalConnectionId()) || |
| 2138 | originalConnectionId.equals(call.getId())) |
| 2139 | .findFirst(); |
| 2140 | |
| 2141 | if (existingCall.isPresent()) { |
| 2142 | Log.i(this, "isExistingConnectionAlreadyAdded - call %s already added with id %s", |
| 2143 | originalConnectionId, existingCall.get().getId()); |
| 2144 | return existingCall.get(); |
| 2145 | } |
| 2146 | |
| 2147 | return null; |
| 2148 | } |
| 2149 | |
| 2150 | /** |
Tyler Gunn | 8452be0 | 2015-09-17 09:57:02 -0700 | [diff] [blame] | 2151 | * @return A new unique telecom call Id. |
| 2152 | */ |
| 2153 | private String getNextCallId() { |
| 2154 | synchronized(mLock) { |
| 2155 | return TELECOM_CALL_ID_PREFIX + (++mCallId); |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | /** |
Tony Mak | a993094 | 2016-01-15 10:57:14 +0000 | [diff] [blame] | 2160 | * Callback when foreground user is switched. We will reload missed call in all profiles |
| 2161 | * including the user itself. There may be chances that profiles are not started yet. |
| 2162 | */ |
| 2163 | void onUserSwitch(UserHandle userHandle) { |
Santos Cordon | f0f99f3 | 2016-02-18 16:13:57 -0800 | [diff] [blame] | 2164 | mCurrentUserHandle = userHandle; |
Tony Mak | a993094 | 2016-01-15 10:57:14 +0000 | [diff] [blame] | 2165 | mMissedCallNotifier.setCurrentUserHandle(userHandle); |
| 2166 | final UserManager userManager = UserManager.get(mContext); |
| 2167 | List<UserInfo> profiles = userManager.getEnabledProfiles(userHandle.getIdentifier()); |
| 2168 | for (UserInfo profile : profiles) { |
| 2169 | reloadMissedCallsOfUser(profile.getUserHandle()); |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | /** |
| 2174 | * Because there may be chances that profiles are not started yet though its parent user is |
| 2175 | * switched, we reload missed calls of profile that are just started here. |
| 2176 | */ |
| 2177 | void onUserStarting(UserHandle userHandle) { |
| 2178 | if (UserUtil.isProfile(mContext, userHandle)) { |
| 2179 | reloadMissedCallsOfUser(userHandle); |
| 2180 | } |
| 2181 | } |
| 2182 | |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 2183 | public TelecomSystem.SyncRoot getLock() { |
| 2184 | return mLock; |
| 2185 | } |
| 2186 | |
Tony Mak | a993094 | 2016-01-15 10:57:14 +0000 | [diff] [blame] | 2187 | private void reloadMissedCallsOfUser(UserHandle userHandle) { |
| 2188 | mMissedCallNotifier.reloadFromDatabase( |
| 2189 | mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory, userHandle); |
| 2190 | } |
| 2191 | |
| 2192 | /** |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 2193 | * Dumps the state of the {@link CallsManager}. |
| 2194 | * |
| 2195 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 2196 | */ |
| 2197 | public void dump(IndentingPrintWriter pw) { |
| 2198 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 2199 | if (mCalls != null) { |
| 2200 | pw.println("mCalls: "); |
| 2201 | pw.increaseIndent(); |
| 2202 | for (Call call : mCalls) { |
| 2203 | pw.println(call); |
| 2204 | } |
| 2205 | pw.decreaseIndent(); |
| 2206 | } |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 2207 | |
| 2208 | if (mCallAudioManager != null) { |
| 2209 | pw.println("mCallAudioManager:"); |
| 2210 | pw.increaseIndent(); |
| 2211 | mCallAudioManager.dump(pw); |
| 2212 | pw.decreaseIndent(); |
| 2213 | } |
| 2214 | |
| 2215 | if (mTtyManager != null) { |
| 2216 | pw.println("mTtyManager:"); |
| 2217 | pw.increaseIndent(); |
| 2218 | mTtyManager.dump(pw); |
| 2219 | pw.decreaseIndent(); |
| 2220 | } |
| 2221 | |
| 2222 | if (mInCallController != null) { |
| 2223 | pw.println("mInCallController:"); |
| 2224 | pw.increaseIndent(); |
| 2225 | mInCallController.dump(pw); |
| 2226 | pw.decreaseIndent(); |
| 2227 | } |
| 2228 | |
| 2229 | if (mConnectionServiceRepository != null) { |
| 2230 | pw.println("mConnectionServiceRepository:"); |
| 2231 | pw.increaseIndent(); |
| 2232 | mConnectionServiceRepository.dump(pw); |
| 2233 | pw.decreaseIndent(); |
| 2234 | } |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 2235 | } |
Xueren Zhang | c0e8e9d | 2015-03-06 14:16:55 +0100 | [diff] [blame] | 2236 | |
| 2237 | /** |
| 2238 | * For some disconnected causes, we show a dialog when it's a mmi code or potential mmi code. |
| 2239 | * |
| 2240 | * @param call The call. |
| 2241 | */ |
| 2242 | private void maybeShowErrorDialogOnDisconnect(Call call) { |
| 2243 | if (call.getState() == CallState.DISCONNECTED && (isPotentialMMICode(call.getHandle()) |
| 2244 | || isPotentialInCallMMICode(call.getHandle()))) { |
| 2245 | DisconnectCause disconnectCause = call.getDisconnectCause(); |
| 2246 | if (!TextUtils.isEmpty(disconnectCause.getDescription()) && (disconnectCause.getCode() |
| 2247 | == DisconnectCause.ERROR)) { |
| 2248 | Intent errorIntent = new Intent(mContext, ErrorDialogActivity.class); |
| 2249 | errorIntent.putExtra(ErrorDialogActivity.ERROR_MESSAGE_STRING_EXTRA, |
| 2250 | disconnectCause.getDescription()); |
| 2251 | errorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2252 | mContext.startActivityAsUser(errorIntent, UserHandle.CURRENT); |
| 2253 | } |
| 2254 | } |
| 2255 | } |
Sailesh Nepal | 703a1af | 2016-04-14 20:10:12 -0700 | [diff] [blame] | 2256 | |
| 2257 | private void setIntentExtrasAndStartTime(Call call, Bundle extras) { |
| 2258 | // Create our own instance to modify (since extras may be Bundle.EMPTY) |
| 2259 | extras = new Bundle(extras); |
| 2260 | |
| 2261 | // Specifies the time telecom began routing the call. This is used by the dialer for |
| 2262 | // analytics. |
| 2263 | extras.putLong(TelecomManager.EXTRA_CALL_TELECOM_ROUTING_START_TIME_MILLIS, |
| 2264 | SystemClock.elapsedRealtime()); |
| 2265 | |
| 2266 | call.setIntentExtras(extras); |
| 2267 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 2268 | } |