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 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 19 | import android.net.Uri; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 20 | import android.os.Bundle; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 21 | |
| 22 | import android.provider.CallLog.Calls; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 23 | import android.telecom.AudioState; |
| 24 | import android.telecom.CallState; |
| 25 | import android.telecom.GatewayInfo; |
| 26 | import android.telecom.ParcelableConference; |
| 27 | import android.telecom.PhoneAccountHandle; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 28 | import android.telecom.PhoneCapabilities; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 29 | import android.telephony.DisconnectCause; |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 30 | import android.telephony.TelephonyManager; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 31 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 32 | import com.android.internal.util.ArrayUtils; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 33 | import com.google.common.collect.ImmutableCollection; |
| 34 | import com.google.common.collect.ImmutableList; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 35 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 36 | import java.util.Arrays; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 37 | import java.util.Collections; |
Santos Cordon | 7cdb909 | 2014-07-24 21:33:33 -0700 | [diff] [blame] | 38 | import java.util.List; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 39 | import java.util.Objects; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 40 | import java.util.Set; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 41 | import java.util.concurrent.ConcurrentHashMap; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 42 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Singleton. |
| 45 | * |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 46 | * 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] | 47 | * access from other packages specifically refraining from passing the CallsManager instance |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 48 | * beyond the com.android.server.telecom package boundary. |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 49 | */ |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 50 | public final class CallsManager extends Call.ListenerBase { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 51 | |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 52 | // TODO: Consider renaming this CallsManagerPlugin. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 53 | interface CallsManagerListener { |
| 54 | void onCallAdded(Call call); |
| 55 | void onCallRemoved(Call call); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 56 | void onCallStateChanged(Call call, int oldState, int newState); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 57 | void onConnectionServiceChanged( |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 58 | Call call, |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 59 | ConnectionServiceWrapper oldService, |
| 60 | ConnectionServiceWrapper newService); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 61 | void onIncomingCallAnswered(Call call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 62 | void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 63 | void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 64 | void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState); |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 65 | void onRingbackRequested(Call call, boolean ringback); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 66 | void onIsConferencedChanged(Call call); |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 67 | void onIsVoipAudioModeChanged(Call call); |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 68 | void onVideoStateChanged(Call call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 71 | private static final CallsManager INSTANCE = new CallsManager(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 72 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 73 | private static final int MAXIMUM_LIVE_CALLS = 1; |
| 74 | private static final int MAXIMUM_HOLD_CALLS = 1; |
| 75 | private static final int MAXIMUM_RINGING_CALLS = 1; |
| 76 | private static final int MAXIMUM_OUTGOING_CALLS = 1; |
| 77 | |
| 78 | private static final int[] LIVE_CALL_STATES = |
| 79 | {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE}; |
| 80 | |
| 81 | private static final int[] OUTGOING_CALL_STATES = |
| 82 | {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING}; |
| 83 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 84 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 85 | * The main call repository. Keeps an instance of all live calls. New incoming and outgoing |
| 86 | * calls are added to the map and removed when the calls move to the disconnected state. |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 87 | * |
| 88 | * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is |
| 89 | * load factor before resizing, 1 means we only expect a single thread to |
| 90 | * access the map so make only a single shard |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 91 | */ |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 92 | private final Set<Call> mCalls = Collections.newSetFromMap( |
| 93 | new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1)); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 94 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 95 | private final ConnectionServiceRepository mConnectionServiceRepository = |
| 96 | new ConnectionServiceRepository(); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 97 | private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(); |
| 98 | private final InCallController mInCallController = new InCallController(); |
| 99 | private final CallAudioManager mCallAudioManager; |
| 100 | private final Ringer mRinger; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 101 | // For this set initial table size to 16 because we add 13 listeners in |
| 102 | // the CallsManager constructor. |
| 103 | private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap( |
| 104 | new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1)); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 105 | private final HeadsetMediaButton mHeadsetMediaButton; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 106 | private final WiredHeadsetManager mWiredHeadsetManager; |
| 107 | private final TtyManager mTtyManager; |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 108 | private final ProximitySensorManager mProximitySensorManager; |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 109 | private final PhoneStateBroadcaster mPhoneStateBroadcaster; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 110 | private final CallLogManager mCallLogManager; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 113 | * The call the user is currently interacting with. This is the call that should have audio |
| 114 | * focus and be visible in the in-call UI. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 115 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 116 | private Call mForegroundCall; |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 117 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 118 | /** Singleton accessor. */ |
| 119 | static CallsManager getInstance() { |
| 120 | return INSTANCE; |
| 121 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 122 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 123 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 124 | * Initializes the required Telecom components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 125 | */ |
| 126 | private CallsManager() { |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 127 | TelecomApp app = TelecomApp.getInstance(); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 128 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 129 | StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this); |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 130 | mWiredHeadsetManager = new WiredHeadsetManager(app); |
| 131 | mCallAudioManager = new CallAudioManager(app, statusBarNotifier, mWiredHeadsetManager); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 132 | InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 133 | mRinger = new Ringer(mCallAudioManager, this, playerFactory, app); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 134 | mHeadsetMediaButton = new HeadsetMediaButton(app, this); |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 135 | mTtyManager = new TtyManager(app, mWiredHeadsetManager); |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 136 | mProximitySensorManager = new ProximitySensorManager(app); |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 137 | mPhoneStateBroadcaster = new PhoneStateBroadcaster(); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 138 | mCallLogManager = new CallLogManager(app); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 139 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 140 | mListeners.add(statusBarNotifier); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 141 | mListeners.add(mCallLogManager); |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 142 | mListeners.add(mPhoneStateBroadcaster); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 143 | mListeners.add(mInCallController); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 144 | mListeners.add(mRinger); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 145 | mListeners.add(new RingbackPlayer(this, playerFactory)); |
| 146 | mListeners.add(new InCallToneMonitor(playerFactory, this)); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 147 | mListeners.add(mCallAudioManager); |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 148 | mListeners.add(app.getMissedCallNotifier()); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 149 | mListeners.add(mDtmfLocalTonePlayer); |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 150 | mListeners.add(mHeadsetMediaButton); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 151 | mListeners.add(RespondViaSmsManager.getInstance()); |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 152 | mListeners.add(mProximitySensorManager); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 155 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 156 | public void onSuccessfulOutgoingCall(Call call, int callState) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 157 | Log.v(this, "onSuccessfulOutgoingCall, %s", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 158 | |
Tyler Gunn | cde2e50 | 2014-08-12 14:35:58 -0700 | [diff] [blame] | 159 | setCallState(call, callState); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 160 | if (!mCalls.contains(call)) { |
| 161 | // Call was not added previously in startOutgoingCall due to it being a potential MMI |
| 162 | // code, so add it now. |
| 163 | addCall(call); |
| 164 | } |
| 165 | |
| 166 | // The call's ConnectionService has been updated. |
| 167 | for (CallsManagerListener listener : mListeners) { |
| 168 | listener.onConnectionServiceChanged(call, null, call.getConnectionService()); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 169 | } |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 170 | |
| 171 | markCallAsDialing(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 175 | public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) { |
| 176 | Log.v(this, "onFailedOutgoingCall, call: %s", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 177 | |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 178 | // TODO: Replace disconnect cause with more specific disconnect causes. |
| 179 | markCallAsDisconnected(call, errorCode, errorMsg); |
| 180 | } |
| 181 | |
| 182 | @Override |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 183 | public void onSuccessfulIncomingCall(Call incomingCall) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 184 | Log.d(this, "onSuccessfulIncomingCall"); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 185 | setCallState(incomingCall, CallState.RINGING); |
| 186 | |
| 187 | if (hasMaximumRingingCalls()) { |
| 188 | incomingCall.reject(false, null); |
| 189 | // since the call was not added to the list of calls, we have to call the missed |
| 190 | // call notifier and the call logger manually. |
| 191 | TelecomApp.getInstance().getMissedCallNotifier() |
| 192 | .showMissedCallNotification(incomingCall); |
| 193 | mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE); |
| 194 | } else { |
| 195 | addCall(incomingCall); |
| 196 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void onFailedIncomingCall(Call call) { |
Tyler Gunn | cde2e50 | 2014-08-12 14:35:58 -0700 | [diff] [blame] | 201 | setCallState(call, CallState.DISCONNECTED); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 202 | call.removeListener(this); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 205 | @Override |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 206 | public void onRingbackRequested(Call call, boolean ringback) { |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 207 | for (CallsManagerListener listener : mListeners) { |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 208 | listener.onRingbackRequested(call, ringback); |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 212 | @Override |
| 213 | public void onPostDialWait(Call call, String remaining) { |
| 214 | mInCallController.onPostDialWait(call, remaining); |
| 215 | } |
| 216 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 217 | @Override |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 218 | public void onParentChanged(Call call) { |
| 219 | for (CallsManagerListener listener : mListeners) { |
| 220 | listener.onIsConferencedChanged(call); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void onChildrenChanged(Call call) { |
| 226 | for (CallsManagerListener listener : mListeners) { |
| 227 | listener.onIsConferencedChanged(call); |
| 228 | } |
| 229 | } |
| 230 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 231 | @Override |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 232 | public void onIsVoipAudioModeChanged(Call call) { |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 233 | for (CallsManagerListener listener : mListeners) { |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 234 | listener.onIsVoipAudioModeChanged(call); |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 238 | @Override |
| 239 | public void onVideoStateChanged(Call call) { |
| 240 | for (CallsManagerListener listener : mListeners) { |
| 241 | listener.onVideoStateChanged(call); |
| 242 | } |
| 243 | } |
| 244 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 245 | ImmutableCollection<Call> getCalls() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 246 | return ImmutableList.copyOf(mCalls); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | Call getForegroundCall() { |
| 250 | return mForegroundCall; |
| 251 | } |
| 252 | |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 253 | Ringer getRinger() { |
| 254 | return mRinger; |
| 255 | } |
| 256 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 257 | InCallController getInCallController() { |
| 258 | return mInCallController; |
| 259 | } |
| 260 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 261 | boolean hasEmergencyCall() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 262 | for (Call call : mCalls) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 263 | if (call.isEmergencyCall()) { |
| 264 | return true; |
| 265 | } |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 270 | AudioState getAudioState() { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 271 | return mCallAudioManager.getAudioState(); |
| 272 | } |
| 273 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 274 | boolean isTtySupported() { |
| 275 | return mTtyManager.isTtySupported(); |
| 276 | } |
| 277 | |
| 278 | int getCurrentTtyMode() { |
| 279 | return mTtyManager.getCurrentTtyMode(); |
| 280 | } |
| 281 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 282 | /** |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 283 | * Starts the process to attach the call to a connection service. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 284 | * |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 285 | * @param phoneAccountHandle The phone account which contains the component name of the |
| 286 | * connection service to use for this call. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 287 | * @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] | 288 | */ |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 289 | void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 290 | Log.d(this, "processIncomingCallIntent"); |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 291 | Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER); |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 292 | Call call = new Call( |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 293 | mConnectionServiceRepository, |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 294 | handle, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 295 | null /* gatewayInfo */, |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 296 | null /* connectionManagerPhoneAccount */, |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 297 | phoneAccountHandle, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 298 | true /* isIncoming */, |
| 299 | false /* isConference */); |
| 300 | |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 301 | call.setExtras(extras); |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 302 | // TODO: Move this to be a part of addCall() |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 303 | call.addListener(this); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 304 | call.startCreateConnection(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 307 | /** |
| 308 | * Kicks off the first steps to creating an outgoing call so that InCallUI can launch. |
| 309 | * |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 310 | * @param handle Handle to connect the call with. |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 311 | * @param phoneAccountHandle The phone account which contains the component name of the |
| 312 | * connection service to use for this call. |
| 313 | * @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] | 314 | */ |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 315 | Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 316 | TelecomApp app = TelecomApp.getInstance(); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 317 | |
| 318 | // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call |
| 319 | // as if a phoneAccount was not specified (does the default behavior instead). |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 320 | // 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] | 321 | if (phoneAccountHandle != null) { |
| 322 | List<PhoneAccountHandle> enabledAccounts = |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 323 | app.getPhoneAccountRegistrar().getEnabledPhoneAccounts(handle.getScheme()); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 324 | if (!enabledAccounts.contains(phoneAccountHandle)) { |
| 325 | phoneAccountHandle = null; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (phoneAccountHandle == null) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 330 | // No preset account, check if default exists that supports the URI scheme for the |
| 331 | // handle. |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 332 | PhoneAccountHandle defaultAccountHandle = |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 333 | app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount( |
| 334 | handle.getScheme()); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 335 | if (defaultAccountHandle != null) { |
| 336 | phoneAccountHandle = defaultAccountHandle; |
| 337 | } |
| 338 | } |
| 339 | |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 340 | // Create a call with original handle. The handle may be changed when the call is attached |
| 341 | // to a connection service, but in most cases will remain the same. |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 342 | Call call = new Call( |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 343 | mConnectionServiceRepository, |
| 344 | handle, |
| 345 | null /* gatewayInfo */, |
| 346 | null /* connectionManagerPhoneAccount */, |
| 347 | phoneAccountHandle, |
| 348 | false /* isIncoming */, |
| 349 | false /* isConference */); |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 350 | call.setExtras(extras); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 351 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 352 | boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle()); |
| 353 | |
| 354 | // Do not support any more live calls. Our options are to move a call to hold, disconnect |
| 355 | // a call, or cancel this call altogether. |
| 356 | if (!makeRoomForOutgoingCall(call, isEmergencyCall)) { |
| 357 | // just cancel at this point. |
| 358 | return null; |
| 359 | } |
| 360 | |
| 361 | if (phoneAccountHandle == null && !isEmergencyCall) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 362 | // This is the state where the user is expected to select an account |
| 363 | call.setState(CallState.PRE_DIAL_WAIT); |
| 364 | } else { |
| 365 | call.setState(CallState.CONNECTING); |
| 366 | } |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 367 | |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 368 | if (!isPotentialMMICode(handle)) { |
| 369 | addCall(call); |
| 370 | } else { |
| 371 | call.addListener(this); |
| 372 | } |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 373 | |
| 374 | return call; |
| 375 | } |
| 376 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 377 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 378 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 379 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 380 | * @param handle Handle to connect the call with. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 381 | * @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] | 382 | * actual dialed handle via a gateway provider. May be null. |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 383 | * @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] | 384 | * @param videoState The desired video state for the outgoing call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 385 | */ |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 386 | void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn, |
| 387 | int videoState) { |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 388 | if (call == null) { |
| 389 | // don't do anything if the call no longer exists |
| 390 | Log.i(this, "Canceling unknown call."); |
Santos Cordon | b7af09e | 2014-08-06 04:30:01 -0700 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | |
Nancy Chen | 201b437 | 2014-09-08 14:18:24 -0700 | [diff] [blame] | 394 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress(); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 395 | |
| 396 | if (gatewayInfo == null) { |
Santos Cordon | b58f453 | 2014-05-29 11:59:06 -0700 | [diff] [blame] | 397 | 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] | 398 | } else { |
| 399 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 400 | Log.pii(uriHandle), Log.pii(handle)); |
| 401 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 402 | |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 403 | call.setHandle(uriHandle); |
| 404 | call.setGatewayInfo(gatewayInfo); |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 405 | call.setStartWithSpeakerphoneOn(speakerphoneOn); |
Tyler Gunn | c4abd91 | 2014-07-08 14:22:10 -0700 | [diff] [blame] | 406 | call.setVideoState(videoState); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 407 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 408 | TelecomApp app = TelecomApp.getInstance(); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 409 | boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle()); |
| 410 | if (isEmergencyCall) { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 411 | // Emergency -- CreateConnectionProcessor will choose accounts automatically |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 412 | call.setTargetPhoneAccount(null); |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 413 | } |
Nancy Chen | d9de92c | 2014-07-21 16:09:13 -0700 | [diff] [blame] | 414 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 415 | if (call.getTargetPhoneAccount() != null || isEmergencyCall) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 416 | // If the account has been set, proceed to place the outgoing call. |
| 417 | // Otherwise the connection will be initiated when the account is set by the user. |
Nancy Chen | d9de92c | 2014-07-21 16:09:13 -0700 | [diff] [blame] | 418 | call.startCreateConnection(); |
Nancy Chen | d9de92c | 2014-07-21 16:09:13 -0700 | [diff] [blame] | 419 | } |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 423 | * Attempts to start a conference call for the specified call. |
| 424 | * |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 425 | * @param call The call to conference. |
| 426 | * @param otherCall The other call to conference with. |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 427 | */ |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 428 | void conference(Call call, Call otherCall) { |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 429 | call.conferenceWith(otherCall); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 433 | * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call |
| 434 | * 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] | 435 | * the user opting to answer said call. |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 436 | * |
| 437 | * @param call The call to answer. |
| 438 | * @param videoState The video state in which to answer the call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 439 | */ |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 440 | void answerCall(Call call, int videoState) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 441 | if (!mCalls.contains(call)) { |
| 442 | Log.i(this, "Request to answer a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 443 | } else { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 444 | // 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] | 445 | // STATE_DIALING, put it on hold before answering the call. |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 446 | if (mForegroundCall != null && mForegroundCall != call && |
| 447 | (mForegroundCall.isActive() || |
| 448 | mForegroundCall.getState() == CallState.DIALING)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 449 | if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) { |
| 450 | // This call does not support hold. If it is from a different connection |
| 451 | // service, then disconnect it, otherwise allow the connection service to |
| 452 | // figure out the right states. |
| 453 | if (mForegroundCall.getConnectionService() != call.getConnectionService()) { |
| 454 | mForegroundCall.disconnect(); |
| 455 | } |
| 456 | } else { |
| 457 | Log.v(this, "Holding active/dialing call %s before answering incoming call %s.", |
| 458 | mForegroundCall, call); |
| 459 | mForegroundCall.hold(); |
| 460 | } |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 461 | // TODO: Wait until we get confirmation of the active call being |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 462 | // on-hold before answering the new call. |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 463 | // TODO: Import logic from CallManager.acceptCall() |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 466 | for (CallsManagerListener listener : mListeners) { |
| 467 | listener.onIncomingCallAnswered(call); |
| 468 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 469 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 470 | // 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] | 471 | // {@link #markCallAsActive}. |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 472 | call.answer(videoState); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 473 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 477 | * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call |
| 478 | * 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] | 479 | * the user opting to reject said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 480 | */ |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 481 | void rejectCall(Call call, boolean rejectWithMessage, String textMessage) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 482 | if (!mCalls.contains(call)) { |
| 483 | Log.i(this, "Request to reject a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 484 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 485 | for (CallsManagerListener listener : mListeners) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 486 | listener.onIncomingCallRejected(call, rejectWithMessage, textMessage); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 487 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 488 | call.reject(rejectWithMessage, textMessage); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 489 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 493 | * Instructs Telecom to play the specified DTMF tone within the specified call. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 494 | * |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 495 | * @param digit The DTMF digit to play. |
| 496 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 497 | void playDtmfTone(Call call, char digit) { |
| 498 | if (!mCalls.contains(call)) { |
| 499 | 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] | 500 | } else { |
| 501 | call.playDtmfTone(digit); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 502 | mDtmfLocalTonePlayer.playTone(call, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 507 | * Instructs Telecom to stop the currently playing DTMF tone, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 508 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 509 | void stopDtmfTone(Call call) { |
| 510 | if (!mCalls.contains(call)) { |
| 511 | 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] | 512 | } else { |
| 513 | call.stopDtmfTone(); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 514 | mDtmfLocalTonePlayer.stopTone(call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 519 | * 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] | 520 | */ |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 521 | void postDialContinue(Call call, boolean proceed) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 522 | if (!mCalls.contains(call)) { |
| 523 | 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] | 524 | } else { |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 525 | call.postDialContinue(proceed); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 530 | * 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] | 531 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 532 | * the user hitting the end-call button. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 533 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 534 | void disconnectCall(Call call) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 535 | Log.v(this, "disconnectCall %s", call); |
| 536 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 537 | if (!mCalls.contains(call)) { |
| 538 | Log.w(this, "Unknown call (%s) asked to disconnect", call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 539 | } else { |
| 540 | call.disconnect(); |
| 541 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 542 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 543 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 544 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 545 | * Instructs Telecom to disconnect all calls. |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 546 | */ |
| 547 | void disconnectAllCalls() { |
| 548 | Log.v(this, "disconnectAllCalls"); |
| 549 | |
| 550 | for (Call call : mCalls) { |
| 551 | disconnectCall(call); |
| 552 | } |
| 553 | } |
| 554 | |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 555 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 556 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 557 | * 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] | 558 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 559 | * the user hitting the hold button during an active call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 560 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 561 | void holdCall(Call call) { |
| 562 | if (!mCalls.contains(call)) { |
| 563 | 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] | 564 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 565 | Log.d(this, "Putting call on hold: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 566 | call.hold(); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 571 | * 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] | 572 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 573 | * by the user hitting the hold button during a held call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 574 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 575 | void unholdCall(Call call) { |
| 576 | if (!mCalls.contains(call)) { |
| 577 | 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] | 578 | } else { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 579 | Log.d(this, "unholding call: (%s)", call); |
Sai Cheemalapati | 45b3e3f | 2014-08-15 09:33:00 -0700 | [diff] [blame] | 580 | for (Call c : mCalls) { |
| 581 | if (c != null && c.isAlive() && c != call) { |
| 582 | c.hold(); |
| 583 | } |
| 584 | } |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 585 | call.unhold(); |
| 586 | } |
| 587 | } |
| 588 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 589 | /** Called by the in-call UI to change the mute state. */ |
| 590 | void mute(boolean shouldMute) { |
| 591 | mCallAudioManager.mute(shouldMute); |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Called by the in-call UI to change the audio route, for example to change from earpiece to |
| 596 | * speaker phone. |
| 597 | */ |
| 598 | void setAudioRoute(int route) { |
| 599 | mCallAudioManager.setAudioRoute(route); |
| 600 | } |
| 601 | |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 602 | /** Called by the in-call UI to turn the proximity sensor on. */ |
| 603 | void turnOnProximitySensor() { |
| 604 | mProximitySensorManager.turnOn(); |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Called by the in-call UI to turn the proximity sensor off. |
| 609 | * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise, |
| 610 | * the screen will be kept off until the proximity sensor goes negative. |
| 611 | */ |
| 612 | void turnOffProximitySensor(boolean screenOnImmediately) { |
| 613 | mProximitySensorManager.turnOff(screenOnImmediately); |
| 614 | } |
| 615 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 616 | void phoneAccountSelected(Call call, PhoneAccountHandle account) { |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 617 | if (!mCalls.contains(call)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 618 | Log.i(this, "Attempted to add account to unknown call %s", call); |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 619 | } else { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 620 | // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and |
| 621 | // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the |
| 622 | // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without |
| 623 | // respecting a rewritten number or a canceled number. This is unlikely since |
| 624 | // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting |
| 625 | // a phone account from the in-call UI. |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 626 | call.setTargetPhoneAccount(account); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 627 | |
| 628 | // Note: emergency calls never go through account selection dialog so they never |
| 629 | // arrive here. |
| 630 | if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) { |
| 631 | call.startCreateConnection(); |
| 632 | } else { |
| 633 | call.disconnect(); |
| 634 | } |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 638 | /** Called when the audio state changes. */ |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 639 | void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 640 | Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 641 | for (CallsManagerListener listener : mListeners) { |
| 642 | listener.onAudioStateChanged(oldAudioState, newAudioState); |
| 643 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 646 | void markCallAsRinging(Call call) { |
| 647 | setCallState(call, CallState.RINGING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 650 | void markCallAsDialing(Call call) { |
| 651 | setCallState(call, CallState.DIALING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 654 | void markCallAsActive(Call call) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 655 | if (call.getConnectTimeMillis() == 0) { |
| 656 | call.setConnectTimeMillis(System.currentTimeMillis()); |
| 657 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 658 | setCallState(call, CallState.ACTIVE); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 659 | |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 660 | if (call.getStartWithSpeakerphoneOn()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 661 | setAudioRoute(AudioState.ROUTE_SPEAKER); |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 662 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 665 | void markCallAsOnHold(Call call) { |
| 666 | setCallState(call, CallState.ON_HOLD); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 669 | /** |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 670 | * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the |
| 671 | * last live call, then also disconnect from the in-call controller. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 672 | * |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 673 | * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}. |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 674 | * @param disconnectMessage Optional message about the disconnect. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 675 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 676 | void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) { |
| 677 | call.setDisconnectCause(disconnectCause, disconnectMessage); |
| 678 | setCallState(call, CallState.DISCONNECTED); |
Sailesh Nepal | 77da19e | 2014-07-02 21:31:16 -0700 | [diff] [blame] | 679 | removeCall(call); |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 682 | /** |
Ihab Awad | a02bef5 | 2014-08-13 18:18:42 -0700 | [diff] [blame] | 683 | * Removes an existing disconnected call, and notifies the in-call app. |
| 684 | */ |
| 685 | void markCallAsRemoved(Call call) { |
| 686 | removeCall(call); |
| 687 | } |
| 688 | |
| 689 | /** |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 690 | * 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] | 691 | * service binder disconnects unexpectedly. |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 692 | * |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 693 | * @param service The connection service that disconnected. |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 694 | */ |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 695 | void handleConnectionServiceDeath(ConnectionServiceWrapper service) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 696 | if (service != null) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 697 | for (Call call : mCalls) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 698 | if (call.getConnectionService() == service) { |
| 699 | markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null); |
| 700 | } |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 705 | boolean hasAnyCalls() { |
| 706 | return !mCalls.isEmpty(); |
| 707 | } |
| 708 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 709 | boolean hasActiveOrHoldingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 710 | return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | boolean hasRingingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 714 | return getFirstCallWithState(CallState.RINGING) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 717 | boolean onMediaButton(int type) { |
| 718 | if (hasAnyCalls()) { |
| 719 | if (HeadsetMediaButton.SHORT_PRESS == type) { |
| 720 | Call ringingCall = getFirstCallWithState(CallState.RINGING); |
| 721 | if (ringingCall == null) { |
| 722 | mCallAudioManager.toggleMute(); |
| 723 | return true; |
| 724 | } else { |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 725 | ringingCall.answer(ringingCall.getVideoState()); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | } else if (HeadsetMediaButton.LONG_PRESS == type) { |
| 729 | Log.d(this, "handleHeadsetHook: longpress -> hangup"); |
| 730 | Call callToHangup = getFirstCallWithState( |
| 731 | CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD); |
| 732 | if (callToHangup != null) { |
| 733 | callToHangup.disconnect(); |
| 734 | return true; |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | return false; |
| 739 | } |
| 740 | |
| 741 | /** |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 742 | * Checks to see if the specified call is the only high-level call and if so, enable the |
| 743 | * "Add-call" button. We allow you to add a second call but not a third or beyond. |
| 744 | * |
| 745 | * @param call The call to test for add-call. |
| 746 | * @return Whether the add-call feature should be enabled for the call. |
| 747 | */ |
| 748 | protected boolean isAddCallCapable(Call call) { |
| 749 | if (call.getParentCall() != null) { |
| 750 | // Never true for child calls. |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | // Loop through all the other calls and there exists a top level (has no parent) call |
| 755 | // that is not the specified call, return false. |
| 756 | for (Call otherCall : mCalls) { |
| 757 | if (call != otherCall && otherCall.getParentCall() == null) { |
| 758 | return false; |
| 759 | } |
| 760 | } |
| 761 | return true; |
| 762 | } |
| 763 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 764 | Call getFirstCallWithState(int... states) { |
| 765 | return getFirstCallWithState(null, states); |
| 766 | } |
| 767 | |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 768 | /** |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 769 | * Returns the first call that it finds with the given states. The states are treated as having |
| 770 | * priority order so that any call with the first state will be returned before any call with |
| 771 | * states listed later in the parameter list. |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 772 | * |
| 773 | * @param callToSkip Call that this method should skip while searching |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 774 | */ |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 775 | Call getFirstCallWithState(Call callToSkip, int... states) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 776 | for (int currentState : states) { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 777 | // check the foreground first |
| 778 | if (mForegroundCall != null && mForegroundCall.getState() == currentState) { |
| 779 | return mForegroundCall; |
| 780 | } |
| 781 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 782 | for (Call call : mCalls) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 783 | if (Objects.equals(callToSkip, call)) { |
| 784 | continue; |
| 785 | } |
| 786 | |
| 787 | // Only operate on top-level calls |
| 788 | if (call.getParentCall() != null) { |
| 789 | continue; |
| 790 | } |
| 791 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 792 | if (currentState == call.getState()) { |
| 793 | return call; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | return null; |
| 798 | } |
| 799 | |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 800 | Call createConferenceCall( |
| 801 | PhoneAccountHandle phoneAccount, |
| 802 | ParcelableConference parcelableConference) { |
| 803 | Call call = new Call( |
| 804 | mConnectionServiceRepository, |
| 805 | null /* handle */, |
| 806 | null /* gatewayInfo */, |
| 807 | null /* connectionManagerPhoneAccount */, |
| 808 | phoneAccount, |
| 809 | false /* isIncoming */, |
| 810 | true /* isConference */); |
| 811 | |
| 812 | setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState())); |
Yorke Lee | 03f5128 | 2014-09-11 09:49:26 -0700 | [diff] [blame] | 813 | if (call.getState() == CallState.ACTIVE) { |
| 814 | call.setConnectTimeMillis(System.currentTimeMillis()); |
| 815 | } |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 816 | call.setCallCapabilities(parcelableConference.getCapabilities()); |
| 817 | |
| 818 | // TODO: Move this to be a part of addCall() |
| 819 | call.addListener(this); |
| 820 | addCall(call); |
| 821 | return call; |
| 822 | } |
| 823 | |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 824 | /** |
| 825 | * @return the call state currently tracked by {@link PhoneStateBroadcaster} |
| 826 | */ |
| 827 | int getCallState() { |
| 828 | return mPhoneStateBroadcaster.getCallState(); |
| 829 | } |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 830 | |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 831 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 832 | * Adds the specified call to the main list of live calls. |
| 833 | * |
| 834 | * @param call The call to add. |
| 835 | */ |
| 836 | private void addCall(Call call) { |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 837 | Log.v(this, "addCall(%s)", call); |
| 838 | |
| 839 | call.addListener(this); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 840 | mCalls.add(call); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 841 | |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 842 | // TODO: Update mForegroundCall prior to invoking |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 843 | // onCallAdded for calls which immediately take the foreground (like the first call). |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 844 | for (CallsManagerListener listener : mListeners) { |
| 845 | listener.onCallAdded(call); |
| 846 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 847 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 848 | } |
| 849 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 850 | private void removeCall(Call call) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 851 | Log.v(this, "removeCall(%s)", call); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 852 | |
Santos Cordon | 672321e | 2014-08-21 14:38:58 -0700 | [diff] [blame] | 853 | call.setParentCall(null); // need to clean up parent relationship before destroying. |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 854 | call.removeListener(this); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 855 | call.clearConnectionService(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 856 | |
| 857 | boolean shouldNotify = false; |
| 858 | if (mCalls.contains(call)) { |
| 859 | mCalls.remove(call); |
| 860 | shouldNotify = true; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 861 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 862 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 863 | // Only broadcast changes for calls that are being tracked. |
| 864 | if (shouldNotify) { |
| 865 | for (CallsManagerListener listener : mListeners) { |
| 866 | listener.onCallRemoved(call); |
| 867 | } |
| 868 | updateForegroundCall(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 871 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 872 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 873 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 874 | * |
| 875 | * @param call The call. |
| 876 | * @param newState The new state of the call. |
| 877 | */ |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 878 | private void setCallState(Call call, int newState) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 879 | if (call == null) { |
| 880 | return; |
| 881 | } |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 882 | int oldState = call.getState(); |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 883 | Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState), |
| 884 | CallState.toString(newState), call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 885 | if (newState != oldState) { |
| 886 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 887 | // 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] | 888 | // sense (e.g., STATE_ACTIVE -> STATE_RINGING). |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 889 | // TODO: Consider putting a stop to the above and turning CallState |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 890 | // into a well-defined state machine. |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 891 | // TODO: Define expected state transitions here, and log when an |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 892 | // unexpected transition occurs. |
| 893 | call.setState(newState); |
| 894 | |
| 895 | // Only broadcast state change for calls that are being tracked. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 896 | if (mCalls.contains(call)) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 897 | for (CallsManagerListener listener : mListeners) { |
| 898 | listener.onCallStateChanged(call, oldState, newState); |
| 899 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 900 | updateForegroundCall(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 906 | * 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] | 907 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 908 | private void updateForegroundCall() { |
| 909 | Call newForegroundCall = null; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 910 | for (Call call : mCalls) { |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 911 | // TODO: Foreground-ness needs to be explicitly set. No call, regardless |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 912 | // of its state will be foreground by default and instead the connection service should |
| 913 | // 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] | 914 | // the call should play audio and listen to microphone if it wants. |
| 915 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 916 | // Only top-level calls can be in foreground |
| 917 | if (call.getParentCall() != null) { |
| 918 | continue; |
| 919 | } |
| 920 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 921 | // Active calls have priority. |
| 922 | if (call.isActive()) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 923 | newForegroundCall = call; |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 924 | break; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 925 | } |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 926 | |
| 927 | if (call.isAlive() || call.getState() == CallState.RINGING) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 928 | newForegroundCall = call; |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 929 | // 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] | 930 | } |
| 931 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 932 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 933 | if (newForegroundCall != mForegroundCall) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 934 | Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 935 | Call oldForegroundCall = mForegroundCall; |
| 936 | mForegroundCall = newForegroundCall; |
| 937 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 938 | for (CallsManagerListener listener : mListeners) { |
| 939 | listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
| 940 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 941 | } |
| 942 | } |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 943 | |
| 944 | private boolean isPotentialMMICode(Uri handle) { |
| 945 | return (handle != null && handle.getSchemeSpecificPart() != null |
| 946 | && handle.getSchemeSpecificPart().contains("#")); |
| 947 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame^] | 948 | |
| 949 | private int getNumCallsWithState(int... states) { |
| 950 | int count = 0; |
| 951 | for (int state : states) { |
| 952 | for (Call call : mCalls) { |
| 953 | if (call.getState() == state) { |
| 954 | count++; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | return count; |
| 959 | } |
| 960 | |
| 961 | private boolean hasMaximumLiveCalls() { |
| 962 | return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES); |
| 963 | } |
| 964 | |
| 965 | private boolean hasMaximumHoldingCalls() { |
| 966 | return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD); |
| 967 | } |
| 968 | |
| 969 | private boolean hasMaximumRingingCalls() { |
| 970 | return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING); |
| 971 | } |
| 972 | |
| 973 | private boolean hasMaximumOutgoingCalls() { |
| 974 | return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES); |
| 975 | } |
| 976 | |
| 977 | private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) { |
| 978 | if (hasMaximumLiveCalls()) { |
| 979 | // NOTE: If the amount of live calls changes beyond 1, this logic will probably |
| 980 | // have to change. |
| 981 | Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES); |
| 982 | |
| 983 | if (hasMaximumHoldingCalls()) { |
| 984 | // There is no more room for any more calls, unless it's an emergency. |
| 985 | if (isEmergency) { |
| 986 | // Kill the current active call, this is easier then trying to disconnect a |
| 987 | // holding call and hold an active call. |
| 988 | liveCall.disconnect(); |
| 989 | return true; |
| 990 | } |
| 991 | return false; // No more room! |
| 992 | } |
| 993 | |
| 994 | // We have room for at least one more holding call at this point. |
| 995 | |
| 996 | // First thing, if we are trying to make a call with the same phone account as the live |
| 997 | // call, then allow it so that the connection service can make its own decision about |
| 998 | // how to handle the new call relative to the current one. |
| 999 | if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) { |
| 1000 | return true; |
| 1001 | } else if (call.getTargetPhoneAccount() == null) { |
| 1002 | // Without a phone account, we can't say reliably that the call will fail. |
| 1003 | // If the user chooses the same phone account as the live call, then it's |
| 1004 | // still possible that the call can be made (like with CDMA calls not supporting |
| 1005 | // hold but they still support adding a call by going immediately into conference |
| 1006 | // mode). Return true here and we'll run this code again after user chooses an |
| 1007 | // account. |
| 1008 | return true; |
| 1009 | } |
| 1010 | |
| 1011 | // Try to hold the live call before attempting the new outgoing call. |
| 1012 | if (liveCall.can(PhoneCapabilities.HOLD)) { |
| 1013 | liveCall.hold(); |
| 1014 | return true; |
| 1015 | } |
| 1016 | |
| 1017 | // The live call cannot be held so we're out of luck here. There's no room. |
| 1018 | return false; |
| 1019 | } |
| 1020 | return true; |
| 1021 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 1022 | } |