Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.telecomm; |
| 18 | |
Santos Cordon | 5c6fa95 | 2014-07-20 17:47:12 -0700 | [diff] [blame] | 19 | import android.annotation.SdkConstant; |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 20 | import android.app.PendingIntent; |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 21 | import android.app.Service; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
Santos Cordon | 5c6fa95 | 2014-07-20 17:47:12 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 24 | import android.net.Uri; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 25 | import android.os.Handler; |
| 26 | import android.os.IBinder; |
| 27 | import android.os.Looper; |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 28 | import android.os.Message; |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 29 | import android.telephony.DisconnectCause; |
Andrew Lee | 1418576 | 2014-07-25 09:41:56 -0700 | [diff] [blame] | 30 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 31 | import com.android.internal.os.SomeArgs; |
| 32 | import com.android.internal.telecomm.IConnectionService; |
| 33 | import com.android.internal.telecomm.IConnectionServiceAdapter; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 34 | import com.android.internal.telecomm.RemoteServiceCallback; |
| 35 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 37 | import java.util.Collection; |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 38 | import java.util.Collections; |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 39 | import java.util.HashMap; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 40 | import java.util.List; |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 41 | import java.util.Map; |
| 42 | |
| 43 | /** |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 44 | * A {@link android.app.Service} that provides telephone connections to processes running on an |
| 45 | * Android device. |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 46 | */ |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 47 | public abstract class ConnectionService extends Service { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 48 | |
Santos Cordon | 5c6fa95 | 2014-07-20 17:47:12 -0700 | [diff] [blame] | 49 | /** |
| 50 | * The {@link Intent} that must be declared as handled by the service. |
| 51 | */ |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 52 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
Santos Cordon | 5c6fa95 | 2014-07-20 17:47:12 -0700 | [diff] [blame] | 53 | public static final String SERVICE_INTERFACE = "android.telecomm.ConnectionService"; |
| 54 | |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 55 | // Flag controlling whether PII is emitted into the logs |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 56 | private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 57 | |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 58 | private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1; |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 59 | private static final int MSG_CREATE_CONNECTION = 2; |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 60 | private static final int MSG_ABORT = 3; |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 61 | private static final int MSG_ANSWER = 4; |
| 62 | private static final int MSG_REJECT = 5; |
| 63 | private static final int MSG_DISCONNECT = 6; |
| 64 | private static final int MSG_HOLD = 7; |
| 65 | private static final int MSG_UNHOLD = 8; |
| 66 | private static final int MSG_ON_AUDIO_STATE_CHANGED = 9; |
| 67 | private static final int MSG_PLAY_DTMF_TONE = 10; |
| 68 | private static final int MSG_STOP_DTMF_TONE = 11; |
| 69 | private static final int MSG_CONFERENCE = 12; |
| 70 | private static final int MSG_SPLIT_FROM_CONFERENCE = 13; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 71 | private static final int MSG_ON_POST_DIAL_CONTINUE = 14; |
| 72 | private static final int MSG_ON_PHONE_ACCOUNT_CLICKED = 15; |
| 73 | private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16; |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 74 | |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 75 | private final Map<String, Connection> mConnectionById = new HashMap<>(); |
| 76 | private final Map<Connection, String> mIdByConnection = new HashMap<>(); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 77 | private final RemoteConnectionManager mRemoteConnectionManager = new RemoteConnectionManager(); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 78 | |
Ihab Awad | 9c3f188 | 2014-06-30 21:17:13 -0700 | [diff] [blame] | 79 | private boolean mAreAccountsInitialized = false; |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 80 | private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>(); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 81 | private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter(); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 82 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 83 | private final IBinder mBinder = new IConnectionService.Stub() { |
| 84 | @Override |
| 85 | public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) { |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 86 | mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget(); |
| 87 | } |
| 88 | |
| 89 | public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) { |
| 90 | mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget(); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 94 | public void createConnection( |
| 95 | PhoneAccountHandle connectionManagerPhoneAccount, |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 96 | String id, |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 97 | ConnectionRequest request, |
| 98 | boolean isIncoming) { |
| 99 | SomeArgs args = SomeArgs.obtain(); |
| 100 | args.arg1 = connectionManagerPhoneAccount; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 101 | args.arg2 = id; |
| 102 | args.arg3 = request; |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 103 | args.argi1 = isIncoming ? 1 : 0; |
| 104 | mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget(); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void abort(String callId) { |
| 109 | mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget(); |
| 110 | } |
| 111 | |
| 112 | @Override |
Andrew Lee | 8da4c3c | 2014-07-16 10:11:42 -0700 | [diff] [blame] | 113 | public void answer(String callId, int videoState) { |
| 114 | SomeArgs args = SomeArgs.obtain(); |
| 115 | args.arg1 = callId; |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 116 | args.argi1 = videoState; |
Andrew Lee | 8da4c3c | 2014-07-16 10:11:42 -0700 | [diff] [blame] | 117 | mHandler.obtainMessage(MSG_ANSWER, args).sendToTarget(); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void reject(String callId) { |
| 122 | mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget(); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void disconnect(String callId) { |
| 127 | mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void hold(String callId) { |
| 132 | mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget(); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void unhold(String callId) { |
| 137 | mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget(); |
| 138 | } |
| 139 | |
| 140 | @Override |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 141 | public void onAudioStateChanged(String callId, AudioState audioState) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 142 | SomeArgs args = SomeArgs.obtain(); |
| 143 | args.arg1 = callId; |
| 144 | args.arg2 = audioState; |
| 145 | mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void playDtmfTone(String callId, char digit) { |
| 150 | mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget(); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void stopDtmfTone(String callId) { |
| 155 | mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget(); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void conference(String conferenceCallId, String callId) { |
| 160 | SomeArgs args = SomeArgs.obtain(); |
| 161 | args.arg1 = conferenceCallId; |
| 162 | args.arg2 = callId; |
| 163 | mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget(); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void splitFromConference(String callId) { |
| 168 | mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void onPostDialContinue(String callId, boolean proceed) { |
| 173 | SomeArgs args = SomeArgs.obtain(); |
| 174 | args.arg1 = callId; |
| 175 | args.argi1 = proceed ? 1 : 0; |
| 176 | mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget(); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void onPhoneAccountClicked(String callId) { |
| 181 | mHandler.obtainMessage(MSG_ON_PHONE_ACCOUNT_CLICKED, callId).sendToTarget(); |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
| 186 | @Override |
| 187 | public void handleMessage(Message msg) { |
| 188 | switch (msg.what) { |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 189 | case MSG_ADD_CONNECTION_SERVICE_ADAPTER: |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 190 | mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj); |
| 191 | onAdapterAttached(); |
| 192 | break; |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 193 | case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER: |
| 194 | mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj); |
| 195 | break; |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 196 | case MSG_CREATE_CONNECTION: { |
| 197 | SomeArgs args = (SomeArgs) msg.obj; |
| 198 | try { |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 199 | final PhoneAccountHandle connectionManagerPhoneAccount = |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 200 | (PhoneAccountHandle) args.arg1; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 201 | final String id = (String) args.arg2; |
| 202 | final ConnectionRequest request = (ConnectionRequest) args.arg3; |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 203 | final boolean isIncoming = args.argi1 == 1; |
| 204 | if (!mAreAccountsInitialized) { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 205 | Log.d(this, "Enqueueing pre-init request %s", id); |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 206 | mPreInitializationConnectionRequests.add(new Runnable() { |
| 207 | @Override |
| 208 | public void run() { |
| 209 | createConnection( |
| 210 | connectionManagerPhoneAccount, |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 211 | id, |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 212 | request, |
| 213 | isIncoming); |
| 214 | } |
| 215 | }); |
| 216 | } else { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 217 | createConnection( |
| 218 | connectionManagerPhoneAccount, |
| 219 | id, |
| 220 | request, |
| 221 | isIncoming); |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 222 | } |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 223 | } finally { |
| 224 | args.recycle(); |
| 225 | } |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 226 | break; |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 227 | } |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 228 | case MSG_ABORT: |
| 229 | abort((String) msg.obj); |
| 230 | break; |
Andrew Lee | 8da4c3c | 2014-07-16 10:11:42 -0700 | [diff] [blame] | 231 | case MSG_ANSWER: { |
| 232 | SomeArgs args = (SomeArgs) msg.obj; |
| 233 | try { |
| 234 | String callId = (String) args.arg1; |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 235 | int videoState = args.argi1; |
Andrew Lee | 8da4c3c | 2014-07-16 10:11:42 -0700 | [diff] [blame] | 236 | answer(callId, videoState); |
| 237 | } finally { |
| 238 | args.recycle(); |
| 239 | } |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 240 | break; |
Andrew Lee | 8da4c3c | 2014-07-16 10:11:42 -0700 | [diff] [blame] | 241 | } |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 242 | case MSG_REJECT: |
| 243 | reject((String) msg.obj); |
| 244 | break; |
| 245 | case MSG_DISCONNECT: |
| 246 | disconnect((String) msg.obj); |
| 247 | break; |
| 248 | case MSG_HOLD: |
| 249 | hold((String) msg.obj); |
| 250 | break; |
| 251 | case MSG_UNHOLD: |
| 252 | unhold((String) msg.obj); |
| 253 | break; |
| 254 | case MSG_ON_AUDIO_STATE_CHANGED: { |
| 255 | SomeArgs args = (SomeArgs) msg.obj; |
| 256 | try { |
| 257 | String callId = (String) args.arg1; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 258 | AudioState audioState = (AudioState) args.arg2; |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 259 | onAudioStateChanged(callId, audioState); |
| 260 | } finally { |
| 261 | args.recycle(); |
| 262 | } |
| 263 | break; |
| 264 | } |
| 265 | case MSG_PLAY_DTMF_TONE: |
| 266 | playDtmfTone((String) msg.obj, (char) msg.arg1); |
| 267 | break; |
| 268 | case MSG_STOP_DTMF_TONE: |
| 269 | stopDtmfTone((String) msg.obj); |
| 270 | break; |
| 271 | case MSG_CONFERENCE: { |
| 272 | SomeArgs args = (SomeArgs) msg.obj; |
| 273 | try { |
| 274 | String conferenceCallId = (String) args.arg1; |
| 275 | String callId = (String) args.arg2; |
| 276 | conference(conferenceCallId, callId); |
| 277 | } finally { |
| 278 | args.recycle(); |
| 279 | } |
| 280 | break; |
| 281 | } |
| 282 | case MSG_SPLIT_FROM_CONFERENCE: |
| 283 | splitFromConference((String) msg.obj); |
| 284 | break; |
| 285 | case MSG_ON_POST_DIAL_CONTINUE: { |
| 286 | SomeArgs args = (SomeArgs) msg.obj; |
| 287 | try { |
| 288 | String callId = (String) args.arg1; |
| 289 | boolean proceed = (args.argi1 == 1); |
| 290 | onPostDialContinue(callId, proceed); |
| 291 | } finally { |
| 292 | args.recycle(); |
| 293 | } |
| 294 | break; |
| 295 | } |
| 296 | case MSG_ON_PHONE_ACCOUNT_CLICKED: |
Evan Charlton | 8c8a062 | 2014-07-20 12:31:00 -0700 | [diff] [blame] | 297 | onPhoneAccountHandleClicked((String) msg.obj); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 298 | break; |
| 299 | default: |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | }; |
| 304 | |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 305 | private final Connection.Listener mConnectionListener = new Connection.Listener() { |
| 306 | @Override |
| 307 | public void onStateChanged(Connection c, int state) { |
| 308 | String id = mIdByConnection.get(c); |
Ihab Awad | 42b30e1 | 2014-05-22 09:49:34 -0700 | [diff] [blame] | 309 | Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state)); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 310 | switch (state) { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 311 | case Connection.STATE_ACTIVE: |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 312 | mAdapter.setActive(id); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 313 | break; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 314 | case Connection.STATE_DIALING: |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 315 | mAdapter.setDialing(id); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 316 | break; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 317 | case Connection.STATE_DISCONNECTED: |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 318 | // Handled in onDisconnected() |
| 319 | break; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 320 | case Connection.STATE_HOLDING: |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 321 | mAdapter.setOnHold(id); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 322 | break; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 323 | case Connection.STATE_NEW: |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 324 | // Nothing to tell Telecomm |
| 325 | break; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 326 | case Connection.STATE_RINGING: |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 327 | mAdapter.setRinging(id); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public void onDisconnected(Connection c, int cause, String message) { |
| 334 | String id = mIdByConnection.get(c); |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 335 | Log.d(this, "Adapter set disconnected %d %s", cause, message); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 336 | mAdapter.setDisconnected(id, cause, message); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Override |
Tyler Gunn | aa07df8 | 2014-07-17 07:50:22 -0700 | [diff] [blame] | 340 | public void onVideoStateChanged(Connection c, int videoState) { |
| 341 | String id = mIdByConnection.get(c); |
| 342 | Log.d(this, "Adapter set video state %d", videoState); |
| 343 | mAdapter.setVideoState(id, videoState); |
| 344 | } |
| 345 | |
| 346 | @Override |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 347 | public void onHandleChanged(Connection c, Uri handle, int presentation) { |
| 348 | String id = mIdByConnection.get(c); |
| 349 | mAdapter.setHandle(id, handle, presentation); |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public void onCallerDisplayNameChanged( |
| 354 | Connection c, String callerDisplayName, int presentation) { |
| 355 | String id = mIdByConnection.get(c); |
| 356 | mAdapter.setCallerDisplayName(id, callerDisplayName, presentation); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | @Override |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 360 | public void onDestroyed(Connection c) { |
| 361 | removeConnection(c); |
| 362 | } |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 363 | |
| 364 | @Override |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 365 | public void onPostDialWait(Connection c, String remaining) { |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame] | 366 | String id = mIdByConnection.get(c); |
| 367 | Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 368 | mAdapter.onPostDialWait(id, remaining); |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Override |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 372 | public void onRequestingRingback(Connection c, boolean ringback) { |
| 373 | String id = mIdByConnection.get(c); |
| 374 | Log.d(this, "Adapter onRingback %b", ringback); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 375 | mAdapter.setRequestingRingback(id, ringback); |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 376 | } |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 377 | |
| 378 | @Override |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 379 | public void onCallCapabilitiesChanged(Connection c, int capabilities) { |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 380 | String id = mIdByConnection.get(c); |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 381 | Log.d(this, "capabilities: parcelableconnection: %s", |
| 382 | PhoneCapabilities.toString(capabilities)); |
| 383 | mAdapter.setCallCapabilities(id, capabilities); |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 386 | @Override |
| 387 | public void onParentConnectionChanged(Connection c, Connection parent) { |
| 388 | String id = mIdByConnection.get(c); |
| 389 | String parentId = parent == null ? null : mIdByConnection.get(parent); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 390 | mAdapter.setIsConferenced(id, parentId); |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 391 | } |
Andrew Lee | 5ffbe8b | 2014-06-20 16:29:33 -0700 | [diff] [blame] | 392 | |
| 393 | @Override |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 394 | public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) { |
Andrew Lee | 5ffbe8b | 2014-06-20 16:29:33 -0700 | [diff] [blame] | 395 | String id = mIdByConnection.get(c); |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 396 | mAdapter.setVideoProvider(id, videoProvider); |
Andrew Lee | 5ffbe8b | 2014-06-20 16:29:33 -0700 | [diff] [blame] | 397 | } |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 398 | |
| 399 | @Override |
Sailesh Nepal | 001bbbb | 2014-07-15 14:40:39 -0700 | [diff] [blame] | 400 | public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) { |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 401 | String id = mIdByConnection.get(c); |
| 402 | mAdapter.setAudioModeIsVoip(id, isVoip); |
| 403 | } |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 404 | |
| 405 | @Override |
Sailesh Nepal | 001bbbb | 2014-07-15 14:40:39 -0700 | [diff] [blame] | 406 | public void onStatusHintsChanged(Connection c, StatusHints statusHints) { |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 407 | String id = mIdByConnection.get(c); |
| 408 | mAdapter.setStatusHints(id, statusHints); |
| 409 | } |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 410 | |
| 411 | @Override |
| 412 | public void onStartActivityFromInCall(Connection c, PendingIntent intent) { |
| 413 | String id = mIdByConnection.get(c); |
| 414 | mAdapter.startActivityFromInCall(id, intent); |
| 415 | } |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 416 | |
| 417 | @Override |
| 418 | public void onConferenceableConnectionsChanged( |
| 419 | Connection connection, List<Connection> conferenceableConnections) { |
| 420 | String id = mIdByConnection.get(connection); |
| 421 | List<String> conferenceableCallIds = new ArrayList<>(conferenceableConnections.size()); |
| 422 | for (Connection c : conferenceableConnections) { |
| 423 | if (mIdByConnection.containsKey(c)) { |
| 424 | conferenceableCallIds.add(mIdByConnection.get(c)); |
| 425 | } |
| 426 | } |
| 427 | Collections.sort(conferenceableCallIds); |
| 428 | mAdapter.setConferenceableConnections(id, conferenceableCallIds); |
| 429 | } |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 430 | }; |
| 431 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 432 | /** {@inheritDoc} */ |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 433 | @Override |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 434 | public final IBinder onBind(Intent intent) { |
| 435 | return mBinder; |
| 436 | } |
| 437 | |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 438 | /** |
| 439 | * This can be used by telecomm to either create a new outgoing call or attach to an existing |
| 440 | * incoming call. In either case, telecomm will cycle through a set of services and call |
| 441 | * createConnection util a connection service cancels the process or completes it successfully. |
| 442 | */ |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 443 | private void createConnection( |
| 444 | final PhoneAccountHandle callManagerAccount, |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 445 | final String callId, |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 446 | final ConnectionRequest request, |
| 447 | boolean isIncoming) { |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 448 | Log.d(this, "call %s", request); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 449 | |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 450 | final Connection createdConnection; |
| 451 | if (isIncoming) { |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 452 | createdConnection = onCreateIncomingConnection(callManagerAccount, request); |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 453 | } else { |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 454 | createdConnection = onCreateOutgoingConnection(callManagerAccount, request); |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 455 | } |
Sailesh Nepal | 506e386 | 2014-06-25 13:35:14 -0700 | [diff] [blame] | 456 | |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 457 | if (createdConnection != null) { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 458 | Log.d(this, "adapter handleCreateConnectionSuccessful %s", callId); |
| 459 | if (createdConnection.getState() == Connection.STATE_INITIALIZING) { |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 460 | // Wait for the connection to become initialized. |
| 461 | createdConnection.addConnectionListener(new Connection.Listener() { |
| 462 | @Override |
| 463 | public void onStateChanged(Connection c, int state) { |
| 464 | switch (state) { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 465 | case Connection.STATE_FAILED: |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 466 | Log.d(this, "Connection (%s) failed (%d: %s)", request, |
| 467 | c.getFailureCode(), c.getFailureMessage()); |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 468 | Log.d(this, "adapter handleCreateConnectionFailed %s", |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 469 | callId); |
| 470 | mAdapter.handleCreateConnectionFailed( |
| 471 | callId, |
| 472 | request, |
| 473 | c.getFailureCode(), |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 474 | c.getFailureMessage()); |
| 475 | break; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 476 | case Connection.STATE_CANCELED: |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 477 | Log.d(this, "adapter handleCreateConnectionCanceled %s", |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 478 | callId); |
| 479 | mAdapter.handleCreateConnectionCancelled(callId, request); |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 480 | break; |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 481 | case Connection.STATE_INITIALIZING: |
| 482 | Log.d(this, "State changed to STATE_INITIALIZING; ignoring"); |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 483 | return; // Don't want to stop listening on this state transition. |
| 484 | default: |
| 485 | Log.d(this, "Connection created in state %s", |
| 486 | Connection.stateToString(state)); |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 487 | connectionCreated(callId, request, createdConnection); |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 488 | break; |
| 489 | } |
| 490 | c.removeConnectionListener(this); |
| 491 | } |
| 492 | }); |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 493 | } else if (createdConnection.getState() == Connection.STATE_CANCELED) { |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 494 | // Tell telecomm not to attempt any more services. |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 495 | mAdapter.handleCreateConnectionCancelled(callId, request); |
| 496 | } else if (createdConnection.getState() == Connection.STATE_FAILED) { |
| 497 | mAdapter.handleCreateConnectionFailed( |
| 498 | callId, |
| 499 | request, |
| 500 | createdConnection.getFailureCode(), |
Sailesh Nepal | 4e7ec71 | 2014-07-28 10:25:27 -0700 | [diff] [blame] | 501 | createdConnection.getFailureMessage()); |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 502 | } else { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 503 | connectionCreated(callId, request, createdConnection); |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 504 | } |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 505 | } else { |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 506 | // Tell telecomm to try a different service. |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 507 | Log.d(this, "adapter handleCreateConnectionFailed %s", callId); |
| 508 | mAdapter.handleCreateConnectionFailed( |
| 509 | callId, |
| 510 | request, |
| 511 | DisconnectCause.ERROR_UNSPECIFIED, |
| 512 | null); |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 513 | } |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 516 | private void connectionCreated( |
| 517 | String callId, |
| 518 | ConnectionRequest request, |
| 519 | Connection connection) { |
| 520 | addConnection(callId, connection); |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 521 | Uri handle = connection.getHandle(); |
| 522 | String number = handle == null ? "null" : handle.getSchemeSpecificPart(); |
| 523 | Log.v(this, "connectionCreated, parcelableconnection: %s, %d, %s", |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 524 | Connection.toLogSafePhoneNumber(number), |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 525 | connection.getState(), |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 526 | PhoneCapabilities.toString(connection.getCallCapabilities())); |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 527 | |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 528 | Log.d(this, "adapter handleCreateConnectionSuccessful %s", callId); |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 529 | mAdapter.handleCreateConnectionSuccessful( |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 530 | callId, |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 531 | request, |
| 532 | new ParcelableConnection( |
| 533 | request.getAccountHandle(), |
| 534 | connection.getState(), |
| 535 | connection.getCallCapabilities(), |
| 536 | connection.getHandle(), |
| 537 | connection.getHandlePresentation(), |
| 538 | connection.getCallerDisplayName(), |
| 539 | connection.getCallerDisplayNamePresentation(), |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 540 | connection.getVideoProvider() == null ? |
| 541 | null : connection.getVideoProvider().getInterface(), |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 542 | connection.getVideoState())); |
| 543 | } |
| 544 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 545 | private void abort(String callId) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 546 | Log.d(this, "abort %s", callId); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 547 | findConnectionForAction(callId, "abort").onAbort(); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Andrew Lee | 8da4c3c | 2014-07-16 10:11:42 -0700 | [diff] [blame] | 550 | private void answer(String callId, int videoState) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 551 | Log.d(this, "answer %s", callId); |
Andrew Lee | 8da4c3c | 2014-07-16 10:11:42 -0700 | [diff] [blame] | 552 | findConnectionForAction(callId, "answer").onAnswer(videoState); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 555 | private void reject(String callId) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 556 | Log.d(this, "reject %s", callId); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 557 | findConnectionForAction(callId, "reject").onReject(); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 560 | private void disconnect(String callId) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 561 | Log.d(this, "disconnect %s", callId); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 562 | findConnectionForAction(callId, "disconnect").onDisconnect(); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 565 | private void hold(String callId) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 566 | Log.d(this, "hold %s", callId); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 567 | findConnectionForAction(callId, "hold").onHold(); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 570 | private void unhold(String callId) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 571 | Log.d(this, "unhold %s", callId); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 572 | findConnectionForAction(callId, "unhold").onUnhold(); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 575 | private void onAudioStateChanged(String callId, AudioState audioState) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 576 | Log.d(this, "onAudioStateChanged %s %s", callId, audioState); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 577 | findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState); |
| 578 | } |
| 579 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 580 | private void playDtmfTone(String callId, char digit) { |
| 581 | Log.d(this, "playDtmfTone %s %c", callId, digit); |
| 582 | findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit); |
| 583 | } |
| 584 | |
| 585 | private void stopDtmfTone(String callId) { |
| 586 | Log.d(this, "stopDtmfTone %s", callId); |
| 587 | findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone(); |
| 588 | } |
| 589 | |
| 590 | private void conference(final String conferenceCallId, String callId) { |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 591 | Log.d(this, "conference %s, %s", conferenceCallId, callId); |
Santos Cordon | 980acb9 | 2014-05-31 10:31:19 -0700 | [diff] [blame] | 592 | |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 593 | Connection connection = findConnectionForAction(callId, "conference"); |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 594 | if (connection == Connection.getNullConnection()) { |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 595 | Log.w(this, "Connection missing in conference request %s.", callId); |
| 596 | return; |
Santos Cordon | 980acb9 | 2014-05-31 10:31:19 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 599 | onCreateConferenceConnection(conferenceCallId, connection, |
| 600 | new Response<String, Connection>() { |
| 601 | /** ${inheritDoc} */ |
| 602 | @Override |
| 603 | public void onResult(String ignored, Connection... result) { |
| 604 | Log.d(this, "onCreateConference.Response %s", (Object[]) result); |
| 605 | if (result != null && result.length == 1) { |
| 606 | Connection conferenceConnection = result[0]; |
| 607 | if (!mIdByConnection.containsKey(conferenceConnection)) { |
| 608 | Log.v(this, "sending new conference call %s", conferenceCallId); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 609 | mAdapter.addConferenceCall(conferenceCallId); |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 610 | addConnection(conferenceCallId, conferenceConnection); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /** ${inheritDoc} */ |
| 616 | @Override |
| 617 | public void onError(String request, int code, String reason) { |
| 618 | // no-op |
| 619 | } |
| 620 | }); |
Santos Cordon | 980acb9 | 2014-05-31 10:31:19 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 623 | private void splitFromConference(String callId) { |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 624 | Log.d(this, "splitFromConference(%s)", callId); |
Santos Cordon | 980acb9 | 2014-05-31 10:31:19 -0700 | [diff] [blame] | 625 | |
| 626 | Connection connection = findConnectionForAction(callId, "splitFromConference"); |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 627 | if (connection == Connection.getNullConnection()) { |
Santos Cordon | 980acb9 | 2014-05-31 10:31:19 -0700 | [diff] [blame] | 628 | Log.w(this, "Connection missing in conference request %s.", callId); |
| 629 | return; |
| 630 | } |
| 631 | |
Santos Cordon | 8abea42 | 2014-08-06 04:46:17 -0700 | [diff] [blame] | 632 | // TODO: Find existing conference call and invoke split(connection). |
Santos Cordon | 980acb9 | 2014-05-31 10:31:19 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 635 | private void onPostDialContinue(String callId, boolean proceed) { |
Evan Charlton | 6dea4ac | 2014-06-03 14:07:13 -0700 | [diff] [blame] | 636 | Log.d(this, "onPostDialContinue(%s)", callId); |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 637 | findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed); |
Evan Charlton | 6dea4ac | 2014-06-03 14:07:13 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Evan Charlton | 8c8a062 | 2014-07-20 12:31:00 -0700 | [diff] [blame] | 640 | private void onPhoneAccountHandleClicked(String callId) { |
Sailesh Nepal | 2bed956 | 2014-07-02 21:26:12 -0700 | [diff] [blame] | 641 | Log.d(this, "onPhoneAccountClicked %s", callId); |
| 642 | findConnectionForAction(callId, "onPhoneAccountClicked").onPhoneAccountClicked(); |
| 643 | } |
| 644 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 645 | private void onAdapterAttached() { |
Ihab Awad | 9c3f188 | 2014-06-30 21:17:13 -0700 | [diff] [blame] | 646 | if (mAreAccountsInitialized) { |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 647 | // No need to query again if we already did it. |
| 648 | return; |
| 649 | } |
| 650 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 651 | mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() { |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 652 | @Override |
| 653 | public void onResult( |
| 654 | final List<ComponentName> componentNames, |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 655 | final List<IBinder> services) { |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 656 | mHandler.post(new Runnable() { |
| 657 | @Override public void run() { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 658 | for (int i = 0; i < componentNames.size() && i < services.size(); i++) { |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 659 | mRemoteConnectionManager.addConnectionService( |
| 660 | componentNames.get(i), |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 661 | IConnectionService.Stub.asInterface(services.get(i))); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 662 | } |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 663 | onAccountsInitialized(); |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 664 | Log.d(this, "remote connection services found: " + services); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 665 | } |
| 666 | }); |
| 667 | } |
| 668 | |
| 669 | @Override |
| 670 | public void onError() { |
| 671 | mHandler.post(new Runnable() { |
| 672 | @Override public void run() { |
Ihab Awad | 9c3f188 | 2014-06-30 21:17:13 -0700 | [diff] [blame] | 673 | mAreAccountsInitialized = true; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 674 | } |
| 675 | }); |
| 676 | } |
| 677 | }); |
| 678 | } |
| 679 | |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 680 | /** |
| 681 | * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an |
| 682 | * incoming request. This is used to attach to existing incoming calls. |
| 683 | * |
| 684 | * @param connectionManagerPhoneAccount See description at |
| 685 | * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}. |
| 686 | * @param request Details about the incoming call. |
| 687 | * @return The {@code Connection} object to satisfy this call, or {@code null} to |
| 688 | * not handle the call. |
| 689 | */ |
| 690 | public final RemoteConnection createRemoteIncomingConnection( |
| 691 | PhoneAccountHandle connectionManagerPhoneAccount, |
| 692 | ConnectionRequest request) { |
| 693 | return mRemoteConnectionManager.createRemoteConnection( |
| 694 | connectionManagerPhoneAccount, request, true); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /** |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 698 | * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an |
| 699 | * outgoing request. This is used to initiate new outgoing calls. |
| 700 | * |
| 701 | * @param connectionManagerPhoneAccount See description at |
| 702 | * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}. |
| 703 | * @param request Details about the incoming call. |
| 704 | * @return The {@code Connection} object to satisfy this call, or {@code null} to |
| 705 | * not handle the call. |
| 706 | */ |
| 707 | public final RemoteConnection createRemoteOutgoingConnection( |
| 708 | PhoneAccountHandle connectionManagerPhoneAccount, |
| 709 | ConnectionRequest request) { |
| 710 | return mRemoteConnectionManager.createRemoteConnection( |
| 711 | connectionManagerPhoneAccount, request, false); |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Returns all the active {@code Connection}s for which this {@code ConnectionService} |
| 716 | * has taken responsibility. |
| 717 | * |
| 718 | * @return A collection of {@code Connection}s created by this {@code ConnectionService}. |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 719 | */ |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame] | 720 | public final Collection<Connection> getAllConnections() { |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 721 | return mConnectionById.values(); |
| 722 | } |
| 723 | |
| 724 | /** |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 725 | * Create a {@code Connection} given an incoming request. This is used to attach to existing |
| 726 | * incoming calls. |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame] | 727 | * |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 728 | * @param connectionManagerPhoneAccount See description at |
| 729 | * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}. |
| 730 | * @param request Details about the incoming call. |
| 731 | * @return The {@code Connection} object to satisfy this call, or {@code null} to |
| 732 | * not handle the call. |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 733 | */ |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 734 | public Connection onCreateIncomingConnection( |
| 735 | PhoneAccountHandle connectionManagerPhoneAccount, |
| 736 | ConnectionRequest request) { |
| 737 | return null; |
| 738 | } |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 739 | |
| 740 | /** |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 741 | * Create a {@code Connection} given an outgoing request. This is used to initiate new |
| 742 | * outgoing calls. |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 743 | * |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 744 | * @param connectionManagerPhoneAccount The connection manager account to use for managing |
| 745 | * this call. |
| 746 | * <p> |
| 747 | * If this parameter is not {@code null}, it means that this {@code ConnectionService} |
| 748 | * has registered one or more {@code PhoneAccount}s having |
| 749 | * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain |
| 750 | * one of these {@code PhoneAccount}s, while the {@code request} will contain another |
| 751 | * (usually but not always distinct) {@code PhoneAccount} to be used for actually |
| 752 | * making the connection. |
| 753 | * <p> |
| 754 | * If this parameter is {@code null}, it means that this {@code ConnectionService} is |
| 755 | * being asked to make a direct connection. The |
| 756 | * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be |
| 757 | * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for |
| 758 | * making the connection. |
| 759 | * @param request Details about the outgoing call. |
| 760 | * @return The {@code Connection} object to satisfy this call, or the result of an invocation |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 761 | * of {@link Connection#createFailedConnection(int, String)} to not handle the call. |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 762 | */ |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 763 | public Connection onCreateOutgoingConnection( |
| 764 | PhoneAccountHandle connectionManagerPhoneAccount, |
| 765 | ConnectionRequest request) { |
| 766 | return null; |
| 767 | } |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 768 | |
| 769 | /** |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 770 | * Returns a new or existing conference connection when the the user elects to convert the |
| 771 | * specified connection into a conference call. The specified connection can be any connection |
| 772 | * which had previously specified itself as conference-capable including both simple connections |
| 773 | * and connections previously returned from this method. |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 774 | * <p> |
| 775 | * TODO: To be refactored out with conference call re-engineering<br/> |
| 776 | * TODO: Also remove class {@link Response} once this method is removed |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 777 | * |
| 778 | * @param connection The connection from which the user opted to start a conference call. |
| 779 | * @param token The token to be passed into the response callback. |
| 780 | * @param callback The callback for providing the potentially-new conference connection. |
| 781 | */ |
Santos Cordon | f295110 | 2014-07-20 19:06:29 -0700 | [diff] [blame] | 782 | public void onCreateConferenceConnection( |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 783 | String token, |
| 784 | Connection connection, |
| 785 | Response<String, Connection> callback) {} |
| 786 | |
| 787 | /** |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 788 | * Notifies that a connection has been added to this connection service and sent to Telecomm. |
| 789 | * |
| 790 | * @param connection The connection which was added. |
| 791 | */ |
Santos Cordon | f295110 | 2014-07-20 19:06:29 -0700 | [diff] [blame] | 792 | public void onConnectionAdded(Connection connection) {} |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 793 | |
| 794 | /** |
| 795 | * Notified that a connection has been removed from this connection service. |
| 796 | * |
| 797 | * @param connection The connection which was removed. |
| 798 | */ |
Santos Cordon | f295110 | 2014-07-20 19:06:29 -0700 | [diff] [blame] | 799 | public void onConnectionRemoved(Connection connection) {} |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 800 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 801 | private void onAccountsInitialized() { |
| 802 | mAreAccountsInitialized = true; |
| 803 | for (Runnable r : mPreInitializationConnectionRequests) { |
| 804 | r.run(); |
| 805 | } |
| 806 | mPreInitializationConnectionRequests.clear(); |
| 807 | } |
| 808 | |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 809 | private void addConnection(String callId, Connection connection) { |
| 810 | mConnectionById.put(callId, connection); |
| 811 | mIdByConnection.put(connection, callId); |
| 812 | connection.addConnectionListener(mConnectionListener); |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 813 | onConnectionAdded(connection); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | private void removeConnection(Connection connection) { |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 817 | String id = mIdByConnection.get(connection); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 818 | connection.removeConnectionListener(mConnectionListener); |
| 819 | mConnectionById.remove(mIdByConnection.get(connection)); |
| 820 | mIdByConnection.remove(connection); |
Santos Cordon | b693998 | 2014-06-04 20:20:58 -0700 | [diff] [blame] | 821 | onConnectionRemoved(connection); |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 822 | mAdapter.removeCall(id); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | private Connection findConnectionForAction(String callId, String action) { |
| 826 | if (mConnectionById.containsKey(callId)) { |
| 827 | return mConnectionById.get(callId); |
| 828 | } |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 829 | Log.w(this, "%s - Cannot find Connection %s", action, callId); |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame^] | 830 | return Connection.getNullConnection(); |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Santos Cordon | 980acb9 | 2014-05-31 10:31:19 -0700 | [diff] [blame] | 833 | } |