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