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 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 20 | import android.net.Uri; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 21 | import android.os.Bundle; |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 22 | import android.os.Handler; |
Ihab Awad | d1d63e1 | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 23 | import android.os.Looper; |
Rekha Kumar | 032f8e4 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 24 | import android.os.SystemProperties; |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 25 | import android.os.Trace; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 26 | import android.provider.CallLog.Calls; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 27 | import android.telecom.AudioState; |
| 28 | import android.telecom.CallState; |
Tyler Gunn | d92e737 | 2015-01-09 15:59:45 -0800 | [diff] [blame] | 29 | import android.telecom.Conference; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 30 | import android.telecom.Connection; |
Andrew Lee | 701dc00 | 2014-09-11 21:29:12 -0700 | [diff] [blame] | 31 | import android.telecom.DisconnectCause; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 32 | import android.telecom.GatewayInfo; |
| 33 | import android.telecom.ParcelableConference; |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 34 | import android.telecom.ParcelableConnection; |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 35 | import android.telecom.PhoneAccount; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 36 | import android.telecom.PhoneAccountHandle; |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 37 | import android.telecom.TelecomManager; |
Andrew Lee | 4550623 | 2014-10-22 17:54:33 -0700 | [diff] [blame] | 38 | import android.telecom.VideoProfile; |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 39 | import android.telephony.PhoneNumberUtils; |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 40 | import android.telephony.TelephonyManager; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 41 | |
Ihab Awad | a365390 | 2015-01-23 13:44:46 -0800 | [diff] [blame] | 42 | import com.android.internal.annotations.VisibleForTesting; |
Rekha Kumar | 032f8e4 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 43 | import com.android.internal.telephony.PhoneConstants; |
| 44 | import com.android.internal.telephony.TelephonyProperties; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 45 | import com.android.internal.util.IndentingPrintWriter; |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 46 | |
Santos Cordon | 4bc0245 | 2014-11-19 15:51:12 -0800 | [diff] [blame] | 47 | import java.util.Collection; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 48 | import java.util.Collections; |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 49 | import java.util.HashSet; |
Santos Cordon | 7cdb909 | 2014-07-24 21:33:33 -0700 | [diff] [blame] | 50 | import java.util.List; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 51 | import java.util.Objects; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 52 | import java.util.Set; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 53 | import java.util.concurrent.ConcurrentHashMap; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 54 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Singleton. |
| 57 | * |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 58 | * 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] | 59 | * access from other packages specifically refraining from passing the CallsManager instance |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 60 | * beyond the com.android.server.telecom package boundary. |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 61 | */ |
Ihab Awad | b63b57e | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 62 | @VisibleForTesting |
| 63 | public class CallsManager extends Call.ListenerBase { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 64 | |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 65 | // TODO: Consider renaming this CallsManagerPlugin. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 66 | interface CallsManagerListener { |
| 67 | void onCallAdded(Call call); |
| 68 | void onCallRemoved(Call call); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 69 | void onCallStateChanged(Call call, int oldState, int newState); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 70 | void onConnectionServiceChanged( |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 71 | Call call, |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 72 | ConnectionServiceWrapper oldService, |
| 73 | ConnectionServiceWrapper newService); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 74 | void onIncomingCallAnswered(Call call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 75 | void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 76 | void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 77 | void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState); |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 78 | void onRingbackRequested(Call call, boolean ringback); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 79 | void onIsConferencedChanged(Call call); |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 80 | void onIsVoipAudioModeChanged(Call call); |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 81 | void onVideoStateChanged(Call call); |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 82 | void onCanAddCallChanged(boolean canAddCall); |
Rekha Kumar | 032f8e4 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 83 | void onCallSubstateChanged(Call call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 86 | private static final String TAG = "CallsManager"; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 87 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 88 | private static final int MAXIMUM_LIVE_CALLS = 1; |
| 89 | private static final int MAXIMUM_HOLD_CALLS = 1; |
| 90 | private static final int MAXIMUM_RINGING_CALLS = 1; |
| 91 | private static final int MAXIMUM_OUTGOING_CALLS = 1; |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 92 | private static final int MAXIMUM_TOP_LEVEL_CALLS = 2; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 93 | |
| 94 | private static final int[] OUTGOING_CALL_STATES = |
| 95 | {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING}; |
| 96 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 97 | private static final int[] LIVE_CALL_STATES = |
| 98 | {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE}; |
| 99 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 100 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 101 | * The main call repository. Keeps an instance of all live calls. New incoming and outgoing |
| 102 | * 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] | 103 | * |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 104 | * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is |
| 105 | * load factor before resizing, 1 means we only expect a single thread to |
| 106 | * access the map so make only a single shard |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 107 | */ |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 108 | private final Set<Call> mCalls = Collections.newSetFromMap( |
| 109 | new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1)); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 110 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 111 | private final ConnectionServiceRepository mConnectionServiceRepository; |
| 112 | private final DtmfLocalTonePlayer mDtmfLocalTonePlayer; |
| 113 | private final InCallController mInCallController; |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 114 | private final CallAudioManager mCallAudioManager; |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 115 | private RespondViaSmsManager mRespondViaSmsManager; |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 116 | private final Ringer mRinger; |
Santos Cordon | 61e6f35 | 2014-12-03 02:53:34 -0800 | [diff] [blame] | 117 | private final InCallWakeLockController mInCallWakeLockController; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 118 | // For this set initial table size to 16 because we add 13 listeners in |
| 119 | // the CallsManager constructor. |
| 120 | private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap( |
| 121 | new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1)); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 122 | private final HeadsetMediaButton mHeadsetMediaButton; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 123 | private final WiredHeadsetManager mWiredHeadsetManager; |
| 124 | private final TtyManager mTtyManager; |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 125 | private final ProximitySensorManager mProximitySensorManager; |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 126 | private final PhoneStateBroadcaster mPhoneStateBroadcaster; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 127 | private final CallLogManager mCallLogManager; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 128 | private final Context mContext; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 129 | private final TelecomSystem.SyncRoot mLock; |
| 130 | private final ContactsAsyncHelper mContactsAsyncHelper; |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 131 | private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 132 | private final PhoneAccountRegistrar mPhoneAccountRegistrar; |
| 133 | private final MissedCallNotifier mMissedCallNotifier; |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 134 | private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>(); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 135 | private final Set<Call> mPendingCallsToDisconnect = new HashSet<>(); |
| 136 | /* Handler tied to thread in which CallManager was initialized. */ |
Ihab Awad | d1d63e1 | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 137 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 138 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 139 | private boolean mCanAddCall = true; |
| 140 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 141 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 142 | * The call the user is currently interacting with. This is the call that should have audio |
| 143 | * focus and be visible in the in-call UI. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 144 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 145 | private Call mForegroundCall; |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 146 | |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 147 | private Runnable mStopTone; |
| 148 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 149 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 150 | * Initializes the required Telecom components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 151 | */ |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 152 | CallsManager( |
| 153 | Context context, |
| 154 | TelecomSystem.SyncRoot lock, |
| 155 | ContactsAsyncHelper contactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 156 | CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 157 | MissedCallNotifier missedCallNotifier, |
| 158 | PhoneAccountRegistrar phoneAccountRegistrar, |
| 159 | HeadsetMediaButtonFactory headsetMediaButtonFactory, |
| 160 | ProximitySensorManagerFactory proximitySensorManagerFactory, |
| 161 | InCallWakeLockControllerFactory inCallWakeLockControllerFactory) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 162 | mContext = context; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 163 | mLock = lock; |
| 164 | mContactsAsyncHelper = contactsAsyncHelper; |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 165 | mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 166 | mPhoneAccountRegistrar = phoneAccountRegistrar; |
| 167 | mMissedCallNotifier = missedCallNotifier; |
| 168 | StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this); |
| 169 | mWiredHeadsetManager = new WiredHeadsetManager(context); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 170 | mCallAudioManager = new CallAudioManager( |
| 171 | context, statusBarNotifier, mWiredHeadsetManager, this); |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 172 | InCallTonePlayer.Factory playerFactory = |
| 173 | new InCallTonePlayer.Factory(mCallAudioManager, lock); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 174 | mRinger = new Ringer(mCallAudioManager, this, playerFactory, context); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 175 | mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 176 | mTtyManager = new TtyManager(context, mWiredHeadsetManager); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 177 | mProximitySensorManager = proximitySensorManagerFactory.create(context, this); |
| 178 | mPhoneStateBroadcaster = new PhoneStateBroadcaster(this); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 179 | mCallLogManager = new CallLogManager(context); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 180 | mInCallController = new InCallController(context, mLock, this); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 181 | mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 182 | mConnectionServiceRepository = |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 183 | new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 184 | mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 185 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 186 | mListeners.add(statusBarNotifier); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 187 | mListeners.add(mCallLogManager); |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 188 | mListeners.add(mPhoneStateBroadcaster); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 189 | mListeners.add(mInCallController); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 190 | mListeners.add(mRinger); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 191 | mListeners.add(new RingbackPlayer(this, playerFactory)); |
| 192 | mListeners.add(new InCallToneMonitor(playerFactory, this)); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 193 | mListeners.add(mCallAudioManager); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 194 | mListeners.add(missedCallNotifier); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 195 | mListeners.add(mDtmfLocalTonePlayer); |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 196 | mListeners.add(mHeadsetMediaButton); |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 197 | mListeners.add(mProximitySensorManager); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 198 | |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 199 | mMissedCallNotifier.updateOnStartup( |
| 200 | mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 203 | public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) { |
| 204 | if (mRespondViaSmsManager != null) { |
| 205 | mListeners.remove(mRespondViaSmsManager); |
| 206 | } |
| 207 | mRespondViaSmsManager = respondViaSmsManager; |
| 208 | mListeners.add(respondViaSmsManager); |
| 209 | } |
| 210 | |
| 211 | public RespondViaSmsManager getRespondViaSmsManager() { |
| 212 | return mRespondViaSmsManager; |
| 213 | } |
| 214 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 215 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 216 | public void onSuccessfulOutgoingCall(Call call, int callState) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 217 | Log.v(this, "onSuccessfulOutgoingCall, %s", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 218 | |
Tyler Gunn | cde2e50 | 2014-08-12 14:35:58 -0700 | [diff] [blame] | 219 | setCallState(call, callState); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 220 | if (!mCalls.contains(call)) { |
| 221 | // Call was not added previously in startOutgoingCall due to it being a potential MMI |
| 222 | // code, so add it now. |
| 223 | addCall(call); |
| 224 | } |
| 225 | |
| 226 | // The call's ConnectionService has been updated. |
| 227 | for (CallsManagerListener listener : mListeners) { |
| 228 | listener.onConnectionServiceChanged(call, null, call.getConnectionService()); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 229 | } |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 230 | |
| 231 | markCallAsDialing(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Andrew Lee | 701dc00 | 2014-09-11 21:29:12 -0700 | [diff] [blame] | 235 | public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) { |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 236 | Log.v(this, "onFailedOutgoingCall, call: %s", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 237 | |
Andrew Lee | e6288a7 | 2014-10-01 13:50:02 -0700 | [diff] [blame] | 238 | markCallAsRemoved(call); |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 242 | public void onSuccessfulIncomingCall(Call incomingCall) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 243 | Log.d(this, "onSuccessfulIncomingCall"); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 244 | setCallState(incomingCall, CallState.RINGING); |
| 245 | |
| 246 | if (hasMaximumRingingCalls()) { |
| 247 | incomingCall.reject(false, null); |
| 248 | // since the call was not added to the list of calls, we have to call the missed |
| 249 | // call notifier and the call logger manually. |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 250 | mMissedCallNotifier.showMissedCallNotification(incomingCall); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 251 | mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE); |
| 252 | } else { |
| 253 | addCall(incomingCall); |
| 254 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void onFailedIncomingCall(Call call) { |
Tyler Gunn | cde2e50 | 2014-08-12 14:35:58 -0700 | [diff] [blame] | 259 | setCallState(call, CallState.DISCONNECTED); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 260 | call.removeListener(this); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 263 | @Override |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 264 | public void onSuccessfulUnknownCall(Call call, int callState) { |
| 265 | setCallState(call, callState); |
| 266 | Log.i(this, "onSuccessfulUnknownCall for call %s", call); |
| 267 | addCall(call); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void onFailedUnknownCall(Call call) { |
| 272 | Log.i(this, "onFailedUnknownCall for call %s", call); |
| 273 | setCallState(call, CallState.DISCONNECTED); |
| 274 | call.removeListener(this); |
| 275 | } |
| 276 | |
| 277 | @Override |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 278 | public void onRingbackRequested(Call call, boolean ringback) { |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 279 | for (CallsManagerListener listener : mListeners) { |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 280 | listener.onRingbackRequested(call, ringback); |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 284 | @Override |
| 285 | public void onPostDialWait(Call call, String remaining) { |
| 286 | mInCallController.onPostDialWait(call, remaining); |
| 287 | } |
| 288 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 289 | @Override |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 290 | public void onPostDialChar(final Call call, char nextChar) { |
| 291 | if (PhoneNumberUtils.is12Key(nextChar)) { |
| 292 | // Play tone if it is one of the dialpad digits, canceling out the previously queued |
| 293 | // up stopTone runnable since playing a new tone automatically stops the previous tone. |
| 294 | if (mStopTone != null) { |
| 295 | mHandler.removeCallbacks(mStopTone); |
| 296 | } |
| 297 | |
| 298 | mDtmfLocalTonePlayer.playTone(call, nextChar); |
| 299 | |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 300 | // TODO: Create a LockedRunnable class that does the synchronization automatically. |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 301 | mStopTone = new Runnable() { |
| 302 | @Override |
| 303 | public void run() { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 304 | synchronized (mLock) { |
| 305 | // Set a timeout to stop the tone in case there isn't another tone to follow. |
| 306 | mDtmfLocalTonePlayer.stopTone(call); |
| 307 | } |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 308 | } |
| 309 | }; |
| 310 | mHandler.postDelayed( |
| 311 | mStopTone, |
| 312 | Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver())); |
| 313 | } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT || |
| 314 | nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) { |
| 315 | // Stop the tone if a tone is playing, removing any other stopTone callbacks since |
| 316 | // the previous tone is being stopped anyway. |
| 317 | if (mStopTone != null) { |
| 318 | mHandler.removeCallbacks(mStopTone); |
| 319 | } |
| 320 | mDtmfLocalTonePlayer.stopTone(call); |
| 321 | } else { |
| 322 | Log.w(this, "onPostDialChar: invalid value %d", nextChar); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | @Override |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 327 | public void onParentChanged(Call call) { |
Santos Cordon | 66fe882 | 2014-10-10 16:10:58 -0700 | [diff] [blame] | 328 | // parent-child relationship affects which call should be foreground, so do an update. |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 329 | updateCallsManagerState(); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 330 | for (CallsManagerListener listener : mListeners) { |
| 331 | listener.onIsConferencedChanged(call); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public void onChildrenChanged(Call call) { |
Santos Cordon | 66fe882 | 2014-10-10 16:10:58 -0700 | [diff] [blame] | 337 | // parent-child relationship affects which call should be foreground, so do an update. |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 338 | updateCallsManagerState(); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 339 | for (CallsManagerListener listener : mListeners) { |
| 340 | listener.onIsConferencedChanged(call); |
| 341 | } |
| 342 | } |
| 343 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 344 | @Override |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 345 | public void onIsVoipAudioModeChanged(Call call) { |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 346 | for (CallsManagerListener listener : mListeners) { |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 347 | listener.onIsVoipAudioModeChanged(call); |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 351 | @Override |
| 352 | public void onVideoStateChanged(Call call) { |
| 353 | for (CallsManagerListener listener : mListeners) { |
| 354 | listener.onVideoStateChanged(call); |
| 355 | } |
| 356 | } |
| 357 | |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 358 | @Override |
| 359 | public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) { |
| 360 | mPendingCallsToDisconnect.add(call); |
| 361 | mHandler.postDelayed(new Runnable() { |
| 362 | @Override |
| 363 | public void run() { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 364 | synchronized (mLock) { |
| 365 | if (mPendingCallsToDisconnect.remove(call)) { |
| 366 | Log.i(this, "Delayed disconnection of call: %s", call); |
| 367 | call.disconnect(); |
| 368 | } |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver())); |
| 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
Santos Cordon | 4bc0245 | 2014-11-19 15:51:12 -0800 | [diff] [blame] | 376 | Collection<Call> getCalls() { |
| 377 | return Collections.unmodifiableCollection(mCalls); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Rekha Kumar | 032f8e4 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 380 | public void onCallSubstateChanged(Call call) { |
| 381 | for (CallsManagerListener listener : mListeners) { |
| 382 | listener.onCallSubstateChanged(call); |
| 383 | } |
| 384 | } |
| 385 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 386 | Call getForegroundCall() { |
| 387 | return mForegroundCall; |
| 388 | } |
| 389 | |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 390 | Ringer getRinger() { |
| 391 | return mRinger; |
| 392 | } |
| 393 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 394 | InCallController getInCallController() { |
| 395 | return mInCallController; |
| 396 | } |
| 397 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 398 | boolean hasEmergencyCall() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 399 | for (Call call : mCalls) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 400 | if (call.isEmergencyCall()) { |
| 401 | return true; |
| 402 | } |
| 403 | } |
| 404 | return false; |
| 405 | } |
| 406 | |
Andrew Lee | 4550623 | 2014-10-22 17:54:33 -0700 | [diff] [blame] | 407 | boolean hasVideoCall() { |
| 408 | for (Call call : mCalls) { |
| 409 | if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) { |
| 410 | return true; |
| 411 | } |
| 412 | } |
| 413 | return false; |
| 414 | } |
| 415 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 416 | AudioState getAudioState() { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 417 | return mCallAudioManager.getAudioState(); |
| 418 | } |
| 419 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 420 | boolean isTtySupported() { |
| 421 | return mTtyManager.isTtySupported(); |
| 422 | } |
| 423 | |
| 424 | int getCurrentTtyMode() { |
| 425 | return mTtyManager.getCurrentTtyMode(); |
| 426 | } |
| 427 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 428 | void addListener(CallsManagerListener listener) { |
| 429 | mListeners.add(listener); |
| 430 | } |
| 431 | |
| 432 | void removeListener(CallsManagerListener listener) { |
| 433 | mListeners.remove(listener); |
| 434 | } |
| 435 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 436 | /** |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 437 | * Starts the process to attach the call to a connection service. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 438 | * |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 439 | * @param phoneAccountHandle The phone account which contains the component name of the |
| 440 | * connection service to use for this call. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 441 | * @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] | 442 | */ |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 443 | void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 444 | Log.d(this, "processIncomingCallIntent"); |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 445 | Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER); |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 446 | Call call = new Call( |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 447 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 448 | this, |
Ihab Awad | d1d63e1 | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 449 | mLock, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 450 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 451 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 452 | mCallerInfoAsyncQueryFactory, |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 453 | handle, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 454 | null /* gatewayInfo */, |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 455 | null /* connectionManagerPhoneAccount */, |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 456 | phoneAccountHandle, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 457 | true /* isIncoming */, |
| 458 | false /* isConference */); |
| 459 | |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 460 | call.setExtras(extras); |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 461 | // TODO: Move this to be a part of addCall() |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 462 | call.addListener(this); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 463 | call.startCreateConnection(mPhoneAccountRegistrar); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 466 | void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
| 467 | Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE); |
| 468 | Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle)); |
| 469 | Call call = new Call( |
| 470 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 471 | this, |
Ihab Awad | d1d63e1 | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 472 | mLock, |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 473 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 474 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 475 | mCallerInfoAsyncQueryFactory, |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 476 | handle, |
| 477 | null /* gatewayInfo */, |
| 478 | null /* connectionManagerPhoneAccount */, |
| 479 | phoneAccountHandle, |
| 480 | // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach |
| 481 | // to the existing connection instead of trying to create a new one. |
| 482 | true /* isIncoming */, |
| 483 | false /* isConference */); |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 484 | call.setIsUnknown(true); |
| 485 | call.setExtras(extras); |
| 486 | call.addListener(this); |
| 487 | call.startCreateConnection(mPhoneAccountRegistrar); |
| 488 | } |
| 489 | |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 490 | private Call getNewOutgoingCall(Uri handle) { |
| 491 | // First check to see if we can reuse any of the calls that are waiting to disconnect. |
| 492 | // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information. |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 493 | Call reusedCall = null; |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 494 | for (Call pendingCall : mPendingCallsToDisconnect) { |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 495 | if (reusedCall == null && Objects.equals(pendingCall.getHandle(), handle)) { |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 496 | mPendingCallsToDisconnect.remove(pendingCall); |
| 497 | Log.i(this, "Reusing disconnected call %s", pendingCall); |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 498 | reusedCall = pendingCall; |
| 499 | } else { |
| 500 | pendingCall.disconnect(); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 501 | } |
| 502 | } |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 503 | if (reusedCall != null) { |
| 504 | return reusedCall; |
| 505 | } |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 506 | |
| 507 | // Create a call with original handle. The handle may be changed when the call is attached |
| 508 | // to a connection service, but in most cases will remain the same. |
| 509 | return new Call( |
| 510 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 511 | this, |
Ihab Awad | d1d63e1 | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 512 | mLock, |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 513 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 514 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 515 | mCallerInfoAsyncQueryFactory, |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 516 | handle, |
| 517 | null /* gatewayInfo */, |
| 518 | null /* connectionManagerPhoneAccount */, |
| 519 | null /* phoneAccountHandle */, |
| 520 | false /* isIncoming */, |
| 521 | false /* isConference */); |
| 522 | } |
| 523 | |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 524 | /** |
| 525 | * Kicks off the first steps to creating an outgoing call so that InCallUI can launch. |
| 526 | * |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 527 | * @param handle Handle to connect the call with. |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 528 | * @param phoneAccountHandle The phone account which contains the component name of the |
| 529 | * connection service to use for this call. |
| 530 | * @param extras The optional extras Bundle passed with the intent used for the incoming call. |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 531 | */ |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 532 | Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 533 | Call call = getNewOutgoingCall(handle); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 534 | |
| 535 | List<PhoneAccountHandle> accounts = |
| 536 | mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme()); |
| 537 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 538 | Log.v(this, "startOutgoingCall found accounts = " + accounts); |
| 539 | |
Nancy Chen | bc95db9 | 2014-11-24 13:19:18 -0800 | [diff] [blame] | 540 | if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) { |
| 541 | // If there is an ongoing call, use the same phone account to place this new call. |
| 542 | phoneAccountHandle = mForegroundCall.getTargetPhoneAccount(); |
| 543 | } |
| 544 | |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 545 | // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call |
| 546 | // as if a phoneAccount was not specified (does the default behavior instead). |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 547 | // 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] | 548 | if (phoneAccountHandle != null) { |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 549 | if (!accounts.contains(phoneAccountHandle)) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 550 | phoneAccountHandle = null; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (phoneAccountHandle == null) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 555 | // No preset account, check if default exists that supports the URI scheme for the |
| 556 | // handle. |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 557 | PhoneAccountHandle defaultAccountHandle = |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 558 | mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount( |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 559 | handle.getScheme()); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 560 | if (defaultAccountHandle != null) { |
| 561 | phoneAccountHandle = defaultAccountHandle; |
| 562 | } |
| 563 | } |
| 564 | |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 565 | call.setTargetPhoneAccount(phoneAccountHandle); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 566 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 567 | boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext, |
| 568 | call.getHandle()); |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 569 | boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 570 | |
| 571 | // Do not support any more live calls. Our options are to move a call to hold, disconnect |
| 572 | // a call, or cancel this call altogether. |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 573 | if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 574 | // just cancel at this point. |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 575 | Log.i(this, "No remaining room for outgoing call: %s", call); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 576 | if (mCalls.contains(call)) { |
| 577 | // This call can already exist if it is a reused call, |
| 578 | // See {@link #getNewOutgoingCall}. |
| 579 | call.disconnect(); |
| 580 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 581 | return null; |
| 582 | } |
| 583 | |
Nancy Chen | 8d92f45 | 2014-10-20 21:43:44 -0700 | [diff] [blame] | 584 | boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 && |
| 585 | !isEmergencyCall; |
| 586 | |
| 587 | if (needsAccountSelection) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 588 | // This is the state where the user is expected to select an account |
| 589 | call.setState(CallState.PRE_DIAL_WAIT); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 590 | extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 591 | } else { |
| 592 | call.setState(CallState.CONNECTING); |
| 593 | } |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 594 | |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 595 | call.setExtras(extras); |
| 596 | |
Tyler Gunn | 504eb83 | 2014-10-13 14:15:27 -0700 | [diff] [blame] | 597 | // Do not add the call if it is a potential MMI code. |
Nancy Chen | 8d92f45 | 2014-10-20 21:43:44 -0700 | [diff] [blame] | 598 | if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) { |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 599 | call.addListener(this); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 600 | } else if (!mCalls.contains(call)) { |
| 601 | // We check if mCalls already contains the call because we could potentially be reusing |
| 602 | // a call which was previously added (See {@link #getNewOutgoingCall}). |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 603 | addCall(call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 604 | } |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 605 | |
| 606 | return call; |
| 607 | } |
| 608 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 609 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 610 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 611 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 612 | * @param handle Handle to connect the call with. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 613 | * @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] | 614 | * actual dialed handle via a gateway provider. May be null. |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 615 | * @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] | 616 | * @param videoState The desired video state for the outgoing call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 617 | */ |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 618 | void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn, |
| 619 | int videoState) { |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 620 | if (call == null) { |
| 621 | // don't do anything if the call no longer exists |
| 622 | Log.i(this, "Canceling unknown call."); |
Santos Cordon | b7af09e | 2014-08-06 04:30:01 -0700 | [diff] [blame] | 623 | return; |
| 624 | } |
| 625 | |
Nancy Chen | 201b437 | 2014-09-08 14:18:24 -0700 | [diff] [blame] | 626 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress(); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 627 | |
| 628 | if (gatewayInfo == null) { |
Santos Cordon | b58f453 | 2014-05-29 11:59:06 -0700 | [diff] [blame] | 629 | 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] | 630 | } else { |
| 631 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 632 | Log.pii(uriHandle), Log.pii(handle)); |
| 633 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 634 | |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 635 | call.setHandle(uriHandle); |
| 636 | call.setGatewayInfo(gatewayInfo); |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 637 | call.setStartWithSpeakerphoneOn(speakerphoneOn); |
Tyler Gunn | c4abd91 | 2014-07-08 14:22:10 -0700 | [diff] [blame] | 638 | call.setVideoState(videoState); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 639 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 640 | boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext, |
| 641 | call.getHandle()); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 642 | if (isEmergencyCall) { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 643 | // Emergency -- CreateConnectionProcessor will choose accounts automatically |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 644 | call.setTargetPhoneAccount(null); |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 645 | } |
Nancy Chen | d9de92c | 2014-07-21 16:09:13 -0700 | [diff] [blame] | 646 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 647 | if (call.getTargetPhoneAccount() != null || isEmergencyCall) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 648 | // If the account has been set, proceed to place the outgoing call. |
| 649 | // 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] | 650 | call.startCreateConnection(mPhoneAccountRegistrar); |
Nancy Chen | d9de92c | 2014-07-21 16:09:13 -0700 | [diff] [blame] | 651 | } |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /** |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 655 | * Attempts to start a conference call for the specified call. |
| 656 | * |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 657 | * @param call The call to conference. |
| 658 | * @param otherCall The other call to conference with. |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 659 | */ |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 660 | void conference(Call call, Call otherCall) { |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 661 | call.conferenceWith(otherCall); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 665 | * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call |
| 666 | * 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] | 667 | * the user opting to answer said call. |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 668 | * |
| 669 | * @param call The call to answer. |
| 670 | * @param videoState The video state in which to answer the call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 671 | */ |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 672 | void answerCall(Call call, int videoState) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 673 | if (!mCalls.contains(call)) { |
| 674 | Log.i(this, "Request to answer a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 675 | } else { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 676 | // 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] | 677 | // STATE_DIALING, put it on hold before answering the call. |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 678 | if (mForegroundCall != null && mForegroundCall != call && |
| 679 | (mForegroundCall.isActive() || |
| 680 | mForegroundCall.getState() == CallState.DIALING)) { |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 681 | if (0 == (mForegroundCall.getConnectionCapabilities() |
| 682 | & Connection.CAPABILITY_HOLD)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 683 | // This call does not support hold. If it is from a different connection |
| 684 | // service, then disconnect it, otherwise allow the connection service to |
| 685 | // figure out the right states. |
| 686 | if (mForegroundCall.getConnectionService() != call.getConnectionService()) { |
| 687 | mForegroundCall.disconnect(); |
| 688 | } |
| 689 | } else { |
Andrew Lee | e94a8f1 | 2014-12-19 16:19:51 -0800 | [diff] [blame] | 690 | Call heldCall = getHeldCall(); |
| 691 | if (heldCall != null) { |
| 692 | Log.v(this, "Disconnecting held call %s before holding active call.", |
| 693 | heldCall); |
| 694 | heldCall.disconnect(); |
| 695 | } |
| 696 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 697 | Log.v(this, "Holding active/dialing call %s before answering incoming call %s.", |
| 698 | mForegroundCall, call); |
| 699 | mForegroundCall.hold(); |
| 700 | } |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 701 | // TODO: Wait until we get confirmation of the active call being |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 702 | // on-hold before answering the new call. |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 703 | // TODO: Import logic from CallManager.acceptCall() |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 706 | for (CallsManagerListener listener : mListeners) { |
| 707 | listener.onIncomingCallAnswered(call); |
| 708 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 709 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 710 | // 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] | 711 | // {@link #markCallAsActive}. |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 712 | call.answer(videoState); |
Rekha Kumar | 032f8e4 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 713 | if (VideoProfile.VideoState.isVideo(videoState) && |
| 714 | !mWiredHeadsetManager.isPluggedIn() && |
| 715 | !mCallAudioManager.isBluetoothDeviceAvailable() && |
| 716 | isSpeakerEnabledForVideoCalls()) { |
| 717 | call.setStartWithSpeakerphoneOn(true); |
| 718 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 719 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Rekha Kumar | 032f8e4 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 722 | private static boolean isSpeakerEnabledForVideoCalls() { |
| 723 | return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT, |
| 724 | PhoneConstants.AUDIO_OUTPUT_DEFAULT) == |
| 725 | PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER); |
| 726 | } |
| 727 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 728 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 729 | * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call |
| 730 | * 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] | 731 | * the user opting to reject said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 732 | */ |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 733 | void rejectCall(Call call, boolean rejectWithMessage, String textMessage) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 734 | if (!mCalls.contains(call)) { |
| 735 | Log.i(this, "Request to reject a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 736 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 737 | for (CallsManagerListener listener : mListeners) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 738 | listener.onIncomingCallRejected(call, rejectWithMessage, textMessage); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 739 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 740 | call.reject(rejectWithMessage, textMessage); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 741 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 745 | * Instructs Telecom to play the specified DTMF tone within the specified call. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 746 | * |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 747 | * @param digit The DTMF digit to play. |
| 748 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 749 | void playDtmfTone(Call call, char digit) { |
| 750 | if (!mCalls.contains(call)) { |
| 751 | 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] | 752 | } else { |
| 753 | call.playDtmfTone(digit); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 754 | mDtmfLocalTonePlayer.playTone(call, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 759 | * Instructs Telecom to stop the currently playing DTMF tone, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 760 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 761 | void stopDtmfTone(Call call) { |
| 762 | if (!mCalls.contains(call)) { |
| 763 | 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] | 764 | } else { |
| 765 | call.stopDtmfTone(); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 766 | mDtmfLocalTonePlayer.stopTone(call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
| 770 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 771 | * 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] | 772 | */ |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 773 | void postDialContinue(Call call, boolean proceed) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 774 | if (!mCalls.contains(call)) { |
| 775 | 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] | 776 | } else { |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 777 | call.postDialContinue(proceed); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
| 781 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 782 | * 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] | 783 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 784 | * the user hitting the end-call button. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 785 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 786 | void disconnectCall(Call call) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 787 | Log.v(this, "disconnectCall %s", call); |
| 788 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 789 | if (!mCalls.contains(call)) { |
| 790 | Log.w(this, "Unknown call (%s) asked to disconnect", call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 791 | } else { |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 792 | mLocallyDisconnectingCalls.add(call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 793 | call.disconnect(); |
| 794 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 795 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 796 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 797 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 798 | * Instructs Telecom to disconnect all calls. |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 799 | */ |
| 800 | void disconnectAllCalls() { |
| 801 | Log.v(this, "disconnectAllCalls"); |
| 802 | |
| 803 | for (Call call : mCalls) { |
| 804 | disconnectCall(call); |
| 805 | } |
| 806 | } |
| 807 | |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 808 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 809 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 810 | * 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] | 811 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 812 | * the user hitting the hold button during an active call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 813 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 814 | void holdCall(Call call) { |
| 815 | if (!mCalls.contains(call)) { |
| 816 | 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] | 817 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 818 | Log.d(this, "Putting call on hold: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 819 | call.hold(); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 824 | * 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] | 825 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 826 | * by the user hitting the hold button during a held call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 827 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 828 | void unholdCall(Call call) { |
| 829 | if (!mCalls.contains(call)) { |
| 830 | 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] | 831 | } else { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 832 | Log.d(this, "unholding call: (%s)", call); |
Sai Cheemalapati | 45b3e3f | 2014-08-15 09:33:00 -0700 | [diff] [blame] | 833 | for (Call c : mCalls) { |
Tyler Gunn | e44adb4 | 2015-01-02 10:55:35 -0800 | [diff] [blame] | 834 | // Only attempt to hold parent calls and not the individual children. |
| 835 | if (c != null && c.isAlive() && c != call && c.getParentCall() == null) { |
Sai Cheemalapati | 45b3e3f | 2014-08-15 09:33:00 -0700 | [diff] [blame] | 836 | c.hold(); |
| 837 | } |
| 838 | } |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 839 | call.unhold(); |
| 840 | } |
| 841 | } |
| 842 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 843 | /** Called by the in-call UI to change the mute state. */ |
| 844 | void mute(boolean shouldMute) { |
| 845 | mCallAudioManager.mute(shouldMute); |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Called by the in-call UI to change the audio route, for example to change from earpiece to |
| 850 | * speaker phone. |
| 851 | */ |
| 852 | void setAudioRoute(int route) { |
| 853 | mCallAudioManager.setAudioRoute(route); |
| 854 | } |
| 855 | |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 856 | /** Called by the in-call UI to turn the proximity sensor on. */ |
| 857 | void turnOnProximitySensor() { |
| 858 | mProximitySensorManager.turnOn(); |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * Called by the in-call UI to turn the proximity sensor off. |
| 863 | * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise, |
| 864 | * the screen will be kept off until the proximity sensor goes negative. |
| 865 | */ |
| 866 | void turnOffProximitySensor(boolean screenOnImmediately) { |
| 867 | mProximitySensorManager.turnOff(screenOnImmediately); |
| 868 | } |
| 869 | |
Nancy Chen | f5e5d3c | 2014-10-21 18:45:56 -0700 | [diff] [blame] | 870 | void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) { |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 871 | if (!mCalls.contains(call)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 872 | Log.i(this, "Attempted to add account to unknown call %s", call); |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 873 | } else { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 874 | // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and |
| 875 | // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the |
| 876 | // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without |
| 877 | // respecting a rewritten number or a canceled number. This is unlikely since |
| 878 | // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting |
| 879 | // a phone account from the in-call UI. |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 880 | call.setTargetPhoneAccount(account); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 881 | |
| 882 | // Note: emergency calls never go through account selection dialog so they never |
| 883 | // arrive here. |
| 884 | if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 885 | call.startCreateConnection(mPhoneAccountRegistrar); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 886 | } else { |
| 887 | call.disconnect(); |
| 888 | } |
Nancy Chen | f5e5d3c | 2014-10-21 18:45:56 -0700 | [diff] [blame] | 889 | |
| 890 | if (setDefault) { |
| 891 | mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account); |
| 892 | } |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 896 | /** Called when the audio state changes. */ |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 897 | void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 898 | Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 899 | for (CallsManagerListener listener : mListeners) { |
| 900 | listener.onAudioStateChanged(oldAudioState, newAudioState); |
| 901 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 904 | void markCallAsRinging(Call call) { |
| 905 | setCallState(call, CallState.RINGING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 906 | } |
| 907 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 908 | void markCallAsDialing(Call call) { |
| 909 | setCallState(call, CallState.DIALING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 912 | void markCallAsActive(Call call) { |
| 913 | setCallState(call, CallState.ACTIVE); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 914 | |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 915 | if (call.getStartWithSpeakerphoneOn()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 916 | setAudioRoute(AudioState.ROUTE_SPEAKER); |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 917 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 918 | } |
| 919 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 920 | void markCallAsOnHold(Call call) { |
| 921 | setCallState(call, CallState.ON_HOLD); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 924 | /** |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 925 | * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the |
| 926 | * last live call, then also disconnect from the in-call controller. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 927 | * |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 928 | * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 929 | */ |
Andrew Lee | 701dc00 | 2014-09-11 21:29:12 -0700 | [diff] [blame] | 930 | void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) { |
| 931 | call.setDisconnectCause(disconnectCause); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 932 | setCallState(call, CallState.DISCONNECTED); |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 935 | /** |
Ihab Awad | a02bef5 | 2014-08-13 18:18:42 -0700 | [diff] [blame] | 936 | * Removes an existing disconnected call, and notifies the in-call app. |
| 937 | */ |
| 938 | void markCallAsRemoved(Call call) { |
| 939 | removeCall(call); |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 940 | if (mLocallyDisconnectingCalls.contains(call)) { |
| 941 | mLocallyDisconnectingCalls.remove(call); |
| 942 | if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) { |
| 943 | mForegroundCall.unhold(); |
| 944 | } |
| 945 | } |
Ihab Awad | a02bef5 | 2014-08-13 18:18:42 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /** |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 949 | * 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] | 950 | * service binder disconnects unexpectedly. |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 951 | * |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 952 | * @param service The connection service that disconnected. |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 953 | */ |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 954 | void handleConnectionServiceDeath(ConnectionServiceWrapper service) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 955 | if (service != null) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 956 | for (Call call : mCalls) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 957 | if (call.getConnectionService() == service) { |
Yorke Lee | 2af16b6 | 2014-11-11 17:36:57 -0800 | [diff] [blame] | 958 | if (call.getState() != CallState.DISCONNECTED) { |
| 959 | markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR)); |
| 960 | } |
| 961 | markCallAsRemoved(call); |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 962 | } |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 967 | boolean hasAnyCalls() { |
| 968 | return !mCalls.isEmpty(); |
| 969 | } |
| 970 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 971 | boolean hasActiveOrHoldingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 972 | return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | boolean hasRingingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 976 | return getFirstCallWithState(CallState.RINGING) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 979 | boolean onMediaButton(int type) { |
| 980 | if (hasAnyCalls()) { |
| 981 | if (HeadsetMediaButton.SHORT_PRESS == type) { |
| 982 | Call ringingCall = getFirstCallWithState(CallState.RINGING); |
| 983 | if (ringingCall == null) { |
| 984 | mCallAudioManager.toggleMute(); |
| 985 | return true; |
| 986 | } else { |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 987 | ringingCall.answer(ringingCall.getVideoState()); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 988 | return true; |
| 989 | } |
| 990 | } else if (HeadsetMediaButton.LONG_PRESS == type) { |
| 991 | Log.d(this, "handleHeadsetHook: longpress -> hangup"); |
| 992 | Call callToHangup = getFirstCallWithState( |
| 993 | CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD); |
| 994 | if (callToHangup != null) { |
| 995 | callToHangup.disconnect(); |
| 996 | return true; |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | return false; |
| 1001 | } |
| 1002 | |
| 1003 | /** |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1004 | * Returns true if telecom supports adding another top-level call. |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1005 | */ |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1006 | boolean canAddCall() { |
Andrew Lee | 113ad62 | 2014-12-01 18:33:10 -0800 | [diff] [blame] | 1007 | if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) { |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1011 | int count = 0; |
| 1012 | for (Call call : mCalls) { |
| 1013 | if (call.isEmergencyCall()) { |
| 1014 | // We never support add call if one of the calls is an emergency call. |
| 1015 | return false; |
| 1016 | } else if (call.getParentCall() == null) { |
| 1017 | count++; |
| 1018 | } |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1019 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1020 | // We do not check states for canAddCall. We treat disconnected calls the same |
| 1021 | // and wait until they are removed instead. If we didn't count disconnected calls, |
| 1022 | // we could put InCallServices into a state where they are showing two calls but |
| 1023 | // also support add-call. Technically it's right, but overall looks better (UI-wise) |
| 1024 | // and acts better if we wait until the call is removed. |
| 1025 | if (count >= MAXIMUM_TOP_LEVEL_CALLS) { |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1026 | return false; |
| 1027 | } |
| 1028 | } |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
Ihab Awad | a365390 | 2015-01-23 13:44:46 -0800 | [diff] [blame] | 1032 | @VisibleForTesting |
| 1033 | public Call getRingingCall() { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 1034 | return getFirstCallWithState(CallState.RINGING); |
| 1035 | } |
| 1036 | |
| 1037 | Call getActiveCall() { |
| 1038 | return getFirstCallWithState(CallState.ACTIVE); |
| 1039 | } |
| 1040 | |
Nancy Chen | 05a9e40 | 2014-09-26 14:14:32 -0700 | [diff] [blame] | 1041 | Call getDialingCall() { |
| 1042 | return getFirstCallWithState(CallState.DIALING); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | Call getHeldCall() { |
| 1046 | return getFirstCallWithState(CallState.ON_HOLD); |
| 1047 | } |
| 1048 | |
Santos Cordon | c0ca76e | 2014-10-21 15:54:19 -0700 | [diff] [blame] | 1049 | int getNumHeldCalls() { |
| 1050 | int count = 0; |
| 1051 | for (Call call : mCalls) { |
| 1052 | if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) { |
| 1053 | count++; |
| 1054 | } |
| 1055 | } |
| 1056 | return count; |
| 1057 | } |
| 1058 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1059 | Call getFirstCallWithState(int... states) { |
| 1060 | return getFirstCallWithState(null, states); |
| 1061 | } |
| 1062 | |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1063 | /** |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1064 | * Returns the first call that it finds with the given states. The states are treated as having |
| 1065 | * priority order so that any call with the first state will be returned before any call with |
| 1066 | * states listed later in the parameter list. |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1067 | * |
| 1068 | * @param callToSkip Call that this method should skip while searching |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1069 | */ |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1070 | Call getFirstCallWithState(Call callToSkip, int... states) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1071 | for (int currentState : states) { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 1072 | // check the foreground first |
| 1073 | if (mForegroundCall != null && mForegroundCall.getState() == currentState) { |
| 1074 | return mForegroundCall; |
| 1075 | } |
| 1076 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1077 | for (Call call : mCalls) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1078 | if (Objects.equals(callToSkip, call)) { |
| 1079 | continue; |
| 1080 | } |
| 1081 | |
| 1082 | // Only operate on top-level calls |
| 1083 | if (call.getParentCall() != null) { |
| 1084 | continue; |
| 1085 | } |
| 1086 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1087 | if (currentState == call.getState()) { |
| 1088 | return call; |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | return null; |
| 1093 | } |
| 1094 | |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1095 | Call createConferenceCall( |
| 1096 | PhoneAccountHandle phoneAccount, |
| 1097 | ParcelableConference parcelableConference) { |
Tyler Gunn | d92e737 | 2015-01-09 15:59:45 -0800 | [diff] [blame] | 1098 | |
| 1099 | // If the parceled conference specifies a connect time, use it; otherwise default to 0, |
| 1100 | // which is the default value for new Calls. |
| 1101 | long connectTime = |
| 1102 | parcelableConference.getConnectTimeMillis() == |
| 1103 | Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 : |
| 1104 | parcelableConference.getConnectTimeMillis(); |
| 1105 | |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1106 | Call call = new Call( |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1107 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 1108 | this, |
Ihab Awad | d1d63e1 | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 1109 | mLock, |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1110 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 1111 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 1112 | mCallerInfoAsyncQueryFactory, |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1113 | null /* handle */, |
| 1114 | null /* gatewayInfo */, |
| 1115 | null /* connectionManagerPhoneAccount */, |
| 1116 | phoneAccount, |
| 1117 | false /* isIncoming */, |
Tyler Gunn | d92e737 | 2015-01-09 15:59:45 -0800 | [diff] [blame] | 1118 | true /* isConference */, |
| 1119 | connectTime); |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1120 | |
| 1121 | setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState())); |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1122 | call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities()); |
Rekha Kumar | 032f8e4 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 1123 | call.setVideoState(parcelableConference.getVideoState()); |
| 1124 | call.setVideoProvider(parcelableConference.getVideoProvider()); |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1125 | |
| 1126 | // TODO: Move this to be a part of addCall() |
| 1127 | call.addListener(this); |
| 1128 | addCall(call); |
| 1129 | return call; |
| 1130 | } |
| 1131 | |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 1132 | /** |
| 1133 | * @return the call state currently tracked by {@link PhoneStateBroadcaster} |
| 1134 | */ |
| 1135 | int getCallState() { |
| 1136 | return mPhoneStateBroadcaster.getCallState(); |
| 1137 | } |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 1138 | |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 1139 | /** |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1140 | * Retrieves the {@link PhoneAccountRegistrar}. |
| 1141 | * |
| 1142 | * @return The {@link PhoneAccountRegistrar}. |
| 1143 | */ |
| 1144 | PhoneAccountRegistrar getPhoneAccountRegistrar() { |
| 1145 | return mPhoneAccountRegistrar; |
| 1146 | } |
| 1147 | |
| 1148 | /** |
| 1149 | * Retrieves the {@link MissedCallNotifier} |
| 1150 | * @return The {@link MissedCallNotifier}. |
| 1151 | */ |
| 1152 | MissedCallNotifier getMissedCallNotifier() { |
| 1153 | return mMissedCallNotifier; |
| 1154 | } |
| 1155 | |
| 1156 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1157 | * Adds the specified call to the main list of live calls. |
| 1158 | * |
| 1159 | * @param call The call to add. |
| 1160 | */ |
| 1161 | private void addCall(Call call) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1162 | Trace.beginSection("addCall"); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1163 | Log.v(this, "addCall(%s)", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1164 | call.addListener(this); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1165 | mCalls.add(call); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1166 | |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1167 | // TODO: Update mForegroundCall prior to invoking |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1168 | // onCallAdded for calls which immediately take the foreground (like the first call). |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1169 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1170 | if (Log.SYSTRACE_DEBUG) { |
| 1171 | Trace.beginSection(listener.getClass().toString() + " addCall"); |
| 1172 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1173 | listener.onCallAdded(call); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1174 | if (Log.SYSTRACE_DEBUG) { |
| 1175 | Trace.endSection(); |
| 1176 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1177 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1178 | updateCallsManagerState(); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1179 | Trace.endSection(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1182 | private void removeCall(Call call) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1183 | Trace.beginSection("removeCall"); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 1184 | Log.v(this, "removeCall(%s)", call); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 1185 | |
Santos Cordon | 672321e | 2014-08-21 14:38:58 -0700 | [diff] [blame] | 1186 | call.setParentCall(null); // need to clean up parent relationship before destroying. |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 1187 | call.removeListener(this); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 1188 | call.clearConnectionService(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1189 | |
| 1190 | boolean shouldNotify = false; |
| 1191 | if (mCalls.contains(call)) { |
| 1192 | mCalls.remove(call); |
| 1193 | shouldNotify = true; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1194 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1195 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1196 | // Only broadcast changes for calls that are being tracked. |
| 1197 | if (shouldNotify) { |
| 1198 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1199 | if (Log.SYSTRACE_DEBUG) { |
| 1200 | Trace.beginSection(listener.getClass().toString() + " onCallRemoved"); |
| 1201 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1202 | listener.onCallRemoved(call); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1203 | if (Log.SYSTRACE_DEBUG) { |
| 1204 | Trace.endSection(); |
| 1205 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1206 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1207 | updateCallsManagerState(); |
Yorke Lee | 604a90f | 2014-10-20 12:13:32 -0700 | [diff] [blame] | 1208 | } |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1209 | Trace.endSection(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1210 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 1211 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1212 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 1213 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1214 | * |
| 1215 | * @param call The call. |
| 1216 | * @param newState The new state of the call. |
| 1217 | */ |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1218 | private void setCallState(Call call, int newState) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 1219 | if (call == null) { |
| 1220 | return; |
| 1221 | } |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1222 | int oldState = call.getState(); |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 1223 | Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState), |
| 1224 | CallState.toString(newState), call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1225 | if (newState != oldState) { |
| 1226 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 1227 | // 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] | 1228 | // sense (e.g., STATE_ACTIVE -> STATE_RINGING). |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1229 | // TODO: Consider putting a stop to the above and turning CallState |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1230 | // into a well-defined state machine. |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1231 | // TODO: Define expected state transitions here, and log when an |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1232 | // unexpected transition occurs. |
| 1233 | call.setState(newState); |
| 1234 | |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1235 | Trace.beginSection("onCallStateChanged"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1236 | // Only broadcast state change for calls that are being tracked. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1237 | if (mCalls.contains(call)) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1238 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1239 | if (Log.SYSTRACE_DEBUG) { |
| 1240 | Trace.beginSection(listener.getClass().toString() + " onCallStateChanged"); |
| 1241 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1242 | listener.onCallStateChanged(call, oldState, newState); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1243 | if (Log.SYSTRACE_DEBUG) { |
| 1244 | Trace.endSection(); |
| 1245 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1246 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1247 | updateCallsManagerState(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1248 | } |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1249 | Trace.endSection(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1254 | * Checks which call should be visible to the user and have audio focus. |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 1255 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1256 | private void updateForegroundCall() { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1257 | Trace.beginSection("updateForegroundCall"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1258 | Call newForegroundCall = null; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1259 | for (Call call : mCalls) { |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1260 | // TODO: Foreground-ness needs to be explicitly set. No call, regardless |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 1261 | // of its state will be foreground by default and instead the connection service should |
| 1262 | // be notified when its calls enter and exit foreground state. Foreground will mean that |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1263 | // the call should play audio and listen to microphone if it wants. |
| 1264 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1265 | // Only top-level calls can be in foreground |
| 1266 | if (call.getParentCall() != null) { |
| 1267 | continue; |
| 1268 | } |
| 1269 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1270 | // Active calls have priority. |
| 1271 | if (call.isActive()) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1272 | newForegroundCall = call; |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 1273 | break; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1274 | } |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1275 | |
| 1276 | if (call.isAlive() || call.getState() == CallState.RINGING) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1277 | newForegroundCall = call; |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1278 | // Don't break in case there's an active call that has priority. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1279 | } |
| 1280 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1281 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1282 | if (newForegroundCall != mForegroundCall) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1283 | Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1284 | Call oldForegroundCall = mForegroundCall; |
| 1285 | mForegroundCall = newForegroundCall; |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 1286 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1287 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1288 | if (Log.SYSTRACE_DEBUG) { |
| 1289 | Trace.beginSection(listener.getClass().toString() + " updateForegroundCall"); |
| 1290 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1291 | listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1292 | if (Log.SYSTRACE_DEBUG) { |
| 1293 | Trace.endSection(); |
| 1294 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1295 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1296 | } |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1297 | Trace.endSection(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1298 | } |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1299 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1300 | private void updateCanAddCall() { |
| 1301 | boolean newCanAddCall = canAddCall(); |
| 1302 | if (newCanAddCall != mCanAddCall) { |
| 1303 | mCanAddCall = newCanAddCall; |
| 1304 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1305 | if (Log.SYSTRACE_DEBUG) { |
| 1306 | Trace.beginSection(listener.getClass().toString() + " updateCanAddCall"); |
| 1307 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1308 | listener.onCanAddCallChanged(mCanAddCall); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1309 | if (Log.SYSTRACE_DEBUG) { |
| 1310 | Trace.endSection(); |
| 1311 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | private void updateCallsManagerState() { |
| 1317 | updateForegroundCall(); |
| 1318 | updateCanAddCall(); |
| 1319 | } |
| 1320 | |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1321 | private boolean isPotentialMMICode(Uri handle) { |
| 1322 | return (handle != null && handle.getSchemeSpecificPart() != null |
| 1323 | && handle.getSchemeSpecificPart().contains("#")); |
| 1324 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1325 | |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 1326 | /** |
| 1327 | * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are |
| 1328 | * MMI codes which can be dialed when one or more calls are in progress. |
| 1329 | * <P> |
| 1330 | * Checks for numbers formatted similar to the MMI codes defined in: |
| 1331 | * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)} |
| 1332 | * and |
| 1333 | * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)} |
| 1334 | * |
| 1335 | * @param handle The URI to call. |
| 1336 | * @return {@code True} if the URI represents a number which could be an in-call MMI code. |
| 1337 | */ |
| 1338 | private boolean isPotentialInCallMMICode(Uri handle) { |
| 1339 | if (handle != null && handle.getSchemeSpecificPart() != null && |
| 1340 | handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) { |
| 1341 | |
| 1342 | String dialedNumber = handle.getSchemeSpecificPart(); |
| 1343 | return (dialedNumber.equals("0") || |
| 1344 | (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) || |
| 1345 | (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) || |
| 1346 | dialedNumber.equals("3") || |
| 1347 | dialedNumber.equals("4") || |
| 1348 | dialedNumber.equals("5")); |
| 1349 | } |
| 1350 | return false; |
| 1351 | } |
| 1352 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1353 | private int getNumCallsWithState(int... states) { |
| 1354 | int count = 0; |
| 1355 | for (int state : states) { |
| 1356 | for (Call call : mCalls) { |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1357 | if (call.getParentCall() == null && call.getState() == state) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1358 | count++; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | return count; |
| 1363 | } |
| 1364 | |
| 1365 | private boolean hasMaximumLiveCalls() { |
| 1366 | return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES); |
| 1367 | } |
| 1368 | |
| 1369 | private boolean hasMaximumHoldingCalls() { |
| 1370 | return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD); |
| 1371 | } |
| 1372 | |
| 1373 | private boolean hasMaximumRingingCalls() { |
| 1374 | return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING); |
| 1375 | } |
| 1376 | |
| 1377 | private boolean hasMaximumOutgoingCalls() { |
| 1378 | return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES); |
| 1379 | } |
| 1380 | |
| 1381 | private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) { |
| 1382 | if (hasMaximumLiveCalls()) { |
| 1383 | // NOTE: If the amount of live calls changes beyond 1, this logic will probably |
| 1384 | // have to change. |
| 1385 | Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES); |
Anju Mathapati | c88574c | 2014-12-16 14:46:19 +0530 | [diff] [blame] | 1386 | Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " + |
| 1387 | liveCall); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1388 | |
Santos Cordon | d176650 | 2014-09-26 15:45:39 -0700 | [diff] [blame] | 1389 | if (call == liveCall) { |
| 1390 | // If the call is already the foreground call, then we are golden. |
| 1391 | // This can happen after the user selects an account in the PRE_DIAL_WAIT |
| 1392 | // state since the call was already populated into the list. |
| 1393 | return true; |
| 1394 | } |
| 1395 | |
Andrew Lee | e659518 | 2014-09-23 18:43:05 -0700 | [diff] [blame] | 1396 | if (hasMaximumOutgoingCalls()) { |
| 1397 | // Disconnect the current outgoing call if it's not an emergency call. If the user |
| 1398 | // tries to make two outgoing calls to different emergency call numbers, we will try |
| 1399 | // to connect the first outgoing call. |
| 1400 | if (isEmergency) { |
| 1401 | Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES); |
| 1402 | if (!outgoingCall.isEmergencyCall()) { |
| 1403 | outgoingCall.disconnect(); |
| 1404 | return true; |
| 1405 | } |
| 1406 | } |
| 1407 | return false; |
| 1408 | } |
| 1409 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1410 | if (hasMaximumHoldingCalls()) { |
| 1411 | // There is no more room for any more calls, unless it's an emergency. |
| 1412 | if (isEmergency) { |
| 1413 | // Kill the current active call, this is easier then trying to disconnect a |
| 1414 | // holding call and hold an active call. |
| 1415 | liveCall.disconnect(); |
| 1416 | return true; |
| 1417 | } |
| 1418 | return false; // No more room! |
| 1419 | } |
| 1420 | |
| 1421 | // We have room for at least one more holding call at this point. |
| 1422 | |
| 1423 | // First thing, if we are trying to make a call with the same phone account as the live |
| 1424 | // call, then allow it so that the connection service can make its own decision about |
| 1425 | // how to handle the new call relative to the current one. |
| 1426 | if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) { |
| 1427 | return true; |
| 1428 | } else if (call.getTargetPhoneAccount() == null) { |
| 1429 | // Without a phone account, we can't say reliably that the call will fail. |
| 1430 | // If the user chooses the same phone account as the live call, then it's |
| 1431 | // still possible that the call can be made (like with CDMA calls not supporting |
| 1432 | // hold but they still support adding a call by going immediately into conference |
| 1433 | // mode). Return true here and we'll run this code again after user chooses an |
| 1434 | // account. |
| 1435 | return true; |
| 1436 | } |
| 1437 | |
| 1438 | // Try to hold the live call before attempting the new outgoing call. |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1439 | if (liveCall.can(Connection.CAPABILITY_HOLD)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1440 | liveCall.hold(); |
| 1441 | return true; |
| 1442 | } |
| 1443 | |
| 1444 | // The live call cannot be held so we're out of luck here. There's no room. |
| 1445 | return false; |
| 1446 | } |
| 1447 | return true; |
| 1448 | } |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1449 | |
| 1450 | /** |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 1451 | * Creates a new call for an existing connection. |
| 1452 | * |
| 1453 | * @param callId The id of the new call. |
| 1454 | * @param connection The connection information. |
| 1455 | * @return The new call. |
| 1456 | */ |
| 1457 | Call createCallForExistingConnection(String callId, ParcelableConnection connection) { |
| 1458 | Call call = new Call( |
| 1459 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 1460 | this, |
Ihab Awad | d1d63e1 | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 1461 | mLock, |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 1462 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 1463 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame^] | 1464 | mCallerInfoAsyncQueryFactory, |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 1465 | connection.getHandle() /* handle */, |
| 1466 | null /* gatewayInfo */, |
| 1467 | null /* connectionManagerPhoneAccount */, |
| 1468 | connection.getPhoneAccount(), /* targetPhoneAccountHandle */ |
| 1469 | false /* isIncoming */, |
| 1470 | false /* isConference */); |
| 1471 | |
| 1472 | setCallState(call, Call.getStateFromConnectionState(connection.getState())); |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1473 | call.setConnectionCapabilities(connection.getConnectionCapabilities()); |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 1474 | call.setCallerDisplayName(connection.getCallerDisplayName(), |
| 1475 | connection.getCallerDisplayNamePresentation()); |
| 1476 | |
| 1477 | call.addListener(this); |
| 1478 | addCall(call); |
| 1479 | |
| 1480 | return call; |
| 1481 | } |
| 1482 | |
| 1483 | /** |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1484 | * Dumps the state of the {@link CallsManager}. |
| 1485 | * |
| 1486 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 1487 | */ |
| 1488 | public void dump(IndentingPrintWriter pw) { |
| 1489 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1490 | if (mCalls != null) { |
| 1491 | pw.println("mCalls: "); |
| 1492 | pw.increaseIndent(); |
| 1493 | for (Call call : mCalls) { |
| 1494 | pw.println(call); |
| 1495 | } |
| 1496 | pw.decreaseIndent(); |
| 1497 | } |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 1498 | pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall)); |
| 1499 | |
| 1500 | if (mCallAudioManager != null) { |
| 1501 | pw.println("mCallAudioManager:"); |
| 1502 | pw.increaseIndent(); |
| 1503 | mCallAudioManager.dump(pw); |
| 1504 | pw.decreaseIndent(); |
| 1505 | } |
| 1506 | |
| 1507 | if (mTtyManager != null) { |
| 1508 | pw.println("mTtyManager:"); |
| 1509 | pw.increaseIndent(); |
| 1510 | mTtyManager.dump(pw); |
| 1511 | pw.decreaseIndent(); |
| 1512 | } |
| 1513 | |
| 1514 | if (mInCallController != null) { |
| 1515 | pw.println("mInCallController:"); |
| 1516 | pw.increaseIndent(); |
| 1517 | mInCallController.dump(pw); |
| 1518 | pw.decreaseIndent(); |
| 1519 | } |
| 1520 | |
| 1521 | if (mConnectionServiceRepository != null) { |
| 1522 | pw.println("mConnectionServiceRepository:"); |
| 1523 | pw.increaseIndent(); |
| 1524 | mConnectionServiceRepository.dump(pw); |
| 1525 | pw.decreaseIndent(); |
| 1526 | } |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1527 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 1528 | } |