summaryrefslogtreecommitdiffstats
path: root/telecomm/java/android/telecomm/ConnectionService.java
blob: d6970e2bd6b8afd86aa6db3cbc15a490d17d4383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.telecomm;

import android.content.ComponentName;
import android.net.Uri;
import android.os.Bundle;

import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.telephony.DisconnectCause;

import com.android.internal.telecomm.ICallService;
import com.android.internal.telecomm.RemoteServiceCallback;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A {@link android.app.Service} that provides telephone connections to
 * processes running on an Android device.
 */
public abstract class ConnectionService extends CallService {
    // Flag controlling whether PII is emitted into the logs
    private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
    private static final Connection NULL_CONNECTION = new Connection() {};

    // Mappings from Connections to IDs as understood by the current CallService implementation
    private final Map<String, Connection> mConnectionById = new HashMap<>();
    private final Map<Connection, String> mIdByConnection = new HashMap<>();
    private final RemoteConnectionManager mRemoteConnectionManager = new RemoteConnectionManager();
    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private SimpleResponse<Uri, List<PhoneAccount>> mAccountLookupResponse;
    private Uri mAccountLookupHandle;
    private boolean mAreAccountsInitialized = false;

    /**
     * A callback for providing the resuilt of creating a connection.
     */
    public interface OutgoingCallResponse<CONNECTION> {
        /**
         * Tells Telecomm that an attempt to place the specified outgoing call succeeded.
         *
         * @param request The original request.
         * @param connection The connection.
         */
        void onSuccess(ConnectionRequest request, CONNECTION connection);

        /**
         * Tells Telecomm that an attempt to place the specified outgoing call failed.
         *
         * @param request The original request.
         * @param code An integer code indicating the reason for failure.
         * @param msg A message explaining the reason for failure.
         */
        void onFailure(ConnectionRequest request, int code, String msg);

        /**
         * Tells Telecomm to cancel the call.
         *
         * @param request The original request.
         */
        void onCancel(ConnectionRequest request);
    }

    private final Connection.Listener mConnectionListener = new Connection.Listener() {
        @Override
        public void onStateChanged(Connection c, int state) {
            String id = mIdByConnection.get(c);
            Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
            switch (state) {
                case Connection.State.ACTIVE:
                    getAdapter().setActive(id);
                    break;
                case Connection.State.DIALING:
                    getAdapter().setDialing(id);
                    break;
                case Connection.State.DISCONNECTED:
                    // Handled in onDisconnected()
                    break;
                case Connection.State.HOLDING:
                    getAdapter().setOnHold(id);
                    break;
                case Connection.State.NEW:
                    // Nothing to tell Telecomm
                    break;
                case Connection.State.RINGING:
                    getAdapter().setRinging(id);
                    break;
            }
        }

        /** {@inheritDoc} */
        @Override
        public void onFeaturesChanged(Connection c, int features) {
            String id = mIdByConnection.get(c);
            Log.d(this, "Adapter set features %d", features);
            getAdapter().setFeatures(id, features);
        }

        @Override
        public void onDisconnected(Connection c, int cause, String message) {
            String id = mIdByConnection.get(c);
            Log.d(this, "Adapter set disconnected %d %s", cause, message);
            getAdapter().setDisconnected(id, cause, message);
        }

        @Override
        public void onHandleChanged(Connection c, Uri newHandle) {
            // TODO: Unsupported yet
        }

        @Override
        public void onAudioStateChanged(Connection c, CallAudioState state) {
            // TODO: Unsupported yet
        }

        @Override
        public void onSignalChanged(Connection c, Bundle details) {
            // TODO: Unsupported yet
        }

        @Override
        public void onDestroyed(Connection c) {
            removeConnection(c);
        }

        @Override
        public final void onPostDialWait(Connection c, String remaining) {
            String id = mIdByConnection.get(c);
            Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
            getAdapter().onPostDialWait(id, remaining);
        }

        @Override
        public void onRequestingRingback(Connection c, boolean ringback) {
            String id = mIdByConnection.get(c);
            Log.d(this, "Adapter onRingback %b", ringback);
            getAdapter().setRequestingRingback(id, ringback);
        }

        @Override
        public void onConferenceCapableChanged(Connection c, boolean isConferenceCapable) {
            String id = mIdByConnection.get(c);
            getAdapter().setCanConference(id, isConferenceCapable);
        }

        /** ${inheritDoc} */
        @Override
        public void onParentConnectionChanged(Connection c, Connection parent) {
            String id = mIdByConnection.get(c);
            String parentId = parent == null ? null : mIdByConnection.get(parent);
            getAdapter().setIsConferenced(id, parentId);
        }

        @Override
        public void onSetCallVideoProvider(Connection c, CallVideoProvider callVideoProvider) {
            String id = mIdByConnection.get(c);
            getAdapter().setCallVideoProvider(id, callVideoProvider);
        }
    };

    /** @hide */
    @Override
    protected final void call(final CallInfo callInfo) {
        Log.d(this, "call %s", callInfo);
        onCreateConnections(
                new ConnectionRequest(
                        callInfo.getId(),
                        callInfo.getHandle(),
                        callInfo.getExtras()),
                new OutgoingCallResponse<Connection>() {
                    @Override
                    public void onSuccess(ConnectionRequest request, Connection connection) {
                        Log.d(this, "adapter handleSuccessfulOutgoingCall %s",
                                callInfo.getId());
                        getAdapter().handleSuccessfulOutgoingCall(callInfo.getId());
                        addConnection(callInfo.getId(), connection);
                    }

                    @Override
                    public void onFailure(ConnectionRequest request, int code, String msg) {
                        getAdapter().handleFailedOutgoingCall(request, code, msg);
                    }

                    @Override
                    public void onCancel(ConnectionRequest request) {
                        getAdapter().cancelOutgoingCall(callInfo.getId());
                    }
                }
        );
    }

    /** @hide */
    @Override
    protected final void abort(String callId) {
        Log.d(this, "abort %s", callId);
        findConnectionForAction(callId, "abort").abort();
    }

    /** @hide */
    @Override
    protected final void setIncomingCallId(final String callId, Bundle extras) {
        Log.d(this, "setIncomingCallId %s %s", callId, extras);
        onCreateIncomingConnection(
                new ConnectionRequest(
                        callId,
                        null,  // TODO: Can we obtain this from "extras"?
                        extras),
                new Response<ConnectionRequest, Connection>() {
                    @Override
                    public void onResult(ConnectionRequest request, Connection... result) {
                        if (result != null && result.length != 1) {
                            Log.d(this, "adapter handleFailedOutgoingCall %s", callId);
                            getAdapter().handleFailedOutgoingCall(
                                    request,
                                    DisconnectCause.ERROR_UNSPECIFIED,
                                    "Created " + result.length + " Connections, expected 1");
                            for (Connection c : result) {
                                c.abort();
                            }
                        } else {
                            addConnection(callId, result[0]);
                            Log.d(this, "adapter notifyIncomingCall %s", callId);
                            // TODO: Uri.EMPTY is because CallInfo crashes when Parceled with a
                            // null URI ... need to fix that at its cause!
                            getAdapter().notifyIncomingCall(new CallInfo(
                                    callId,
                                    connectionStateToCallState(result[0].getState()),
                                    request.getHandle() /* result[0].getHandle() == null
                                            ? Uri.EMPTY : result[0].getHandle() */));
                        }
                    }

                    @Override
                    public void onError(ConnectionRequest request, int code, String msg) {
                        Log.d(this, "adapter failed setIncomingCallId %s %d %s",
                                request, code, msg);
                    }
                }
        );
    }

    /** @hide */
    @Override
    protected final void answer(String callId) {
        Log.d(this, "answer %s", callId);
        findConnectionForAction(callId, "answer").answer();
    }

    /** @hide */
    @Override
    protected final void reject(String callId) {
        Log.d(this, "reject %s", callId);
        findConnectionForAction(callId, "reject").reject();
    }

    /** @hide */
    @Override
    protected final void disconnect(String callId) {
        Log.d(this, "disconnect %s", callId);
        findConnectionForAction(callId, "disconnect").disconnect();
    }

    /** @hide */
    @Override
    protected final void hold(String callId) {
        Log.d(this, "hold %s", callId);
        findConnectionForAction(callId, "hold").hold();
    }

    /** @hide */
    @Override
    protected final void unhold(String callId) {
        Log.d(this, "unhold %s", callId);
        findConnectionForAction(callId, "unhold").unhold();
    }

    /** @hide */
    @Override
    protected final void playDtmfTone(String callId, char digit) {
        Log.d(this, "playDtmfTone %s %c", callId, digit);
        findConnectionForAction(callId, "playDtmfTone").playDtmfTone(digit);
    }

    /** @hide */
    @Override
    protected final void stopDtmfTone(String callId) {
        Log.d(this, "stopDtmfTone %s", callId);
        findConnectionForAction(callId, "stopDtmfTone").stopDtmfTone();
    }

    /** @hide */
    @Override
    protected final void onAudioStateChanged(String callId, CallAudioState audioState) {
        Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
        findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
    }

    /** @hide */
    @Override
    protected final void conference(final String conferenceCallId, String callId) {
        Log.d(this, "conference %s, %s", conferenceCallId, callId);

        Connection connection = findConnectionForAction(callId, "conference");
        if (connection == NULL_CONNECTION) {
            Log.w(this, "Connection missing in conference request %s.", callId);
            return;
        }

        onCreateConferenceConnection(conferenceCallId, connection,
                new Response<String, Connection>() {
                    /** ${inheritDoc} */
                    @Override
                    public void onResult(String ignored, Connection... result) {
                        Log.d(this, "onCreateConference.Response %s", (Object[]) result);
                        if (result != null && result.length == 1) {
                            Connection conferenceConnection = result[0];
                            if (!mIdByConnection.containsKey(conferenceConnection)) {
                                Log.v(this, "sending new conference call %s", conferenceCallId);
                                getAdapter().addConferenceCall(conferenceCallId);
                                addConnection(conferenceCallId, conferenceConnection);
                            }
                        }
                    }

                    /** ${inheritDoc} */
                    @Override
                    public void onError(String request, int code, String reason) {
                        // no-op
                    }
                });
    }

    /** @hide */
    @Override
    protected final void splitFromConference(String callId) {
        Log.d(this, "splitFromConference(%s)", callId);

        Connection connection = findConnectionForAction(callId, "splitFromConference");
        if (connection == NULL_CONNECTION) {
            Log.w(this, "Connection missing in conference request %s.", callId);
            return;
        }

        // TODO(santoscordon): Find existing conference call and invoke split(connection).
    }

    /** @hide */
    @Override
    protected final void onPostDialContinue(String callId, boolean proceed) {
        Log.d(this, "onPostDialContinue(%s)", callId);

        Connection connection = findConnectionForAction(callId, "onPostDialContinue");
        if (connection == NULL_CONNECTION) {
            Log.w(this, "Connection missing in post-dial request %s.", callId);
            return;
        }
        connection.onPostDialContinue(proceed);
    }

    /** @hide */
    @Override
    protected final void onFeaturesChanged(String callId, int features) {
        Log.d(this, "onFeaturesChanged %s %d", callId, features);
        findConnectionForAction(callId, "onFeaturesChanged").setFeatures(features);
    }

    /** @hide */
    @Override
    protected void onPhoneAccountClicked(String callId) {
        Log.d(this, "onPhoneAccountClicked %s", callId);
        findConnectionForAction(callId, "onPhoneAccountClicked").onPhoneAccountClicked();
    }

    /** @hide */
    @Override
    protected final void onAdapterAttached(CallServiceAdapter adapter) {
        if (mAreAccountsInitialized) {
            // No need to query again if we already did it.
            return;
        }

        getAdapter().queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
            @Override
            public void onResult(
                    final List<ComponentName> componentNames,
                    final List<IBinder> callServices) {
                mHandler.post(new Runnable() {
                    @Override public void run() {
                        for (int i = 0; i < componentNames.size() && i < callServices.size(); i++) {
                            mRemoteConnectionManager.addConnectionService(
                                    componentNames.get(i),
                                    ICallService.Stub.asInterface(callServices.get(i)));
                        }
                        mAreAccountsInitialized = true;
                        Log.d(this, "remote call services found: " + callServices);
                        maybeRespondToAccountLookup();
                    }
                });
            }

            @Override
            public void onError() {
                mHandler.post(new Runnable() {
                    @Override public void run() {
                        mAreAccountsInitialized = true;
                        maybeRespondToAccountLookup();
                    }
                });
            }
        });
    }

    public final void lookupRemoteAccounts(
            Uri handle, SimpleResponse<Uri, List<PhoneAccount>> response) {
        mAccountLookupResponse = response;
        mAccountLookupHandle = handle;
        maybeRespondToAccountLookup();
    }

    public final void maybeRespondToAccountLookup() {
        if (mAreAccountsInitialized && mAccountLookupResponse != null) {
            mAccountLookupResponse.onResult(
                    mAccountLookupHandle,
                    mRemoteConnectionManager.getAccounts(mAccountLookupHandle));

            mAccountLookupHandle = null;
            mAccountLookupResponse = null;
        }
    }

    public final void createRemoteOutgoingConnection(
            ConnectionRequest request,
            OutgoingCallResponse<RemoteConnection> response) {
        mRemoteConnectionManager.createOutgoingConnection(request, response);
    }

    /**
     * Returns all connections currently associated with this connection service.
     */
    public final Collection<Connection> getAllConnections() {
        return mConnectionById.values();
    }

    /**
     * Create a Connection given a request.
     *
     * @param request Data encapsulating details of the desired Connection.
     * @param callback A callback for providing the result.
     */
    protected void onCreateConnections(
            ConnectionRequest request,
            OutgoingCallResponse<Connection> callback) {}

    /**
     * Returns a new or existing conference connection when the the user elects to convert the
     * specified connection into a conference call. The specified connection can be any connection
     * which had previously specified itself as conference-capable including both simple connections
     * and connections previously returned from this method.
     *
     * @param connection The connection from which the user opted to start a conference call.
     * @param token The token to be passed into the response callback.
     * @param callback The callback for providing the potentially-new conference connection.
     */
    protected void onCreateConferenceConnection(
            String token,
            Connection connection,
            Response<String, Connection> callback) {}

    /**
     * Create a Connection to match an incoming connection notification.
     *
     * IMPORTANT: If the incoming connection has a phone number (or other handle) that the user
     * is not supposed to be able to see (e.g. it is PRESENTATION_RESTRICTED), then a compliant
     * ConnectionService implementation MUST NOT reveal this phone number as part of the Intent
     * it sends to notify Telecomm of an incoming connection.
     *
     * @param request Data encapsulating details of the desired Connection.
     * @param callback A callback for providing the result.
     */
    protected void onCreateIncomingConnection(
            ConnectionRequest request,
            Response<ConnectionRequest, Connection> callback) {}

    /**
     * Notifies that a connection has been added to this connection service and sent to Telecomm.
     *
     * @param connection The connection which was added.
     */
    protected void onConnectionAdded(Connection connection) {}

    /**
     * Notified that a connection has been removed from this connection service.
     *
     * @param connection The connection which was removed.
     */
    protected void onConnectionRemoved(Connection connection) {}

    static String toLogSafePhoneNumber(String number) {
        // For unknown number, log empty string.
        if (number == null) {
            return "";
        }

        if (PII_DEBUG) {
            // When PII_DEBUG is true we emit PII.
            return number;
        }

        // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
        // sanitized phone numbers.
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < number.length(); i++) {
            char c = number.charAt(i);
            if (c == '-' || c == '@' || c == '.') {
                builder.append(c);
            } else {
                builder.append('x');
            }
        }
        return builder.toString();
    }

    private CallState connectionStateToCallState(int connectionState) {
        switch (connectionState) {
            case Connection.State.NEW:
                return CallState.NEW;
            case Connection.State.RINGING:
                return CallState.RINGING;
            case Connection.State.DIALING:
                return CallState.DIALING;
            case Connection.State.ACTIVE:
                return CallState.ACTIVE;
            case Connection.State.HOLDING:
                return CallState.ON_HOLD;
            case Connection.State.DISCONNECTED:
                return CallState.DISCONNECTED;
            default:
                Log.wtf(this, "Unknown Connection.State %d", connectionState);
                return CallState.NEW;
        }
    }

    private void addConnection(String callId, Connection connection) {
        mConnectionById.put(callId, connection);
        mIdByConnection.put(connection, callId);
        connection.addConnectionListener(mConnectionListener);
        onConnectionAdded(connection);
    }

    private void removeConnection(Connection connection) {
        connection.removeConnectionListener(mConnectionListener);
        mConnectionById.remove(mIdByConnection.get(connection));
        mIdByConnection.remove(connection);
        onConnectionRemoved(connection);
    }

    private Connection findConnectionForAction(String callId, String action) {
        if (mConnectionById.containsKey(callId)) {
            return mConnectionById.get(callId);
        }
        Log.w(this, "%s - Cannot find Connection %s", action, callId);
        return NULL_CONNECTION;
    }
}