summaryrefslogtreecommitdiff
path: root/testapps/src/com/android/server/telecom/testapps/SelfManagedConnectionService.java
blob: e6e35d87d47f3399e0af46a4b27986e17c52cfc3 (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
/*
 * Copyright (C) 2017 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 com.android.server.telecom.testapps;

import android.content.Intent;
import android.os.Bundle;
import android.telecom.Connection;
import android.telecom.ConnectionRequest;
import android.telecom.ConnectionService;
import android.telecom.Log;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;

import java.util.Random;

/**
 * Sample implementation of the self-managed {@link ConnectionService} API.
 * <p>
 * See {@link android.telecom} for more information on self-managed {@link ConnectionService}s.
 */
public class SelfManagedConnectionService extends ConnectionService {
    public static final String EXTRA_HOLDABLE = "com.android.server.telecom.testapps.HOLDABLE";
    private static final String[] TEST_NAMES = {"Tom Smith", "Jane Appleseed", "Joseph Engleton",
            "Claudia McPherson", "Chris P. Bacon", "Seymour Butz", "Hugh Mungus", "Anita Bath"};
    private final SelfManagedCallList mCallList = SelfManagedCallList.getInstance();

    @Override
    public Connection onCreateOutgoingConnection(
            PhoneAccountHandle connectionManagerAccount,
            final ConnectionRequest request) {

        return createSelfManagedConnection(request, false, false /* isHandover */);
    }

    @Override
    public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
            ConnectionRequest request) {
        return createSelfManagedConnection(request, true, false /* isHandover */);
    }

    @Override
    public Connection onCreateOutgoingHandoverConnection(PhoneAccountHandle fromPhoneAccountHandle,
            ConnectionRequest request) {
        return createSelfManagedConnection(request, false, true /* isHandover */);
    }

    @Override
    public Connection onCreateIncomingHandoverConnection(PhoneAccountHandle fromPhoneAccountHandle,
            ConnectionRequest request) {
        return createSelfManagedConnection(request, true, true /* isHandover */);
    }

    @Override
    public void onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
                                                 ConnectionRequest request) {
        mCallList.notifyCreateIncomingConnectionFailed(request);
    }

    @Override
    public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
                                                 ConnectionRequest request) {
        mCallList.notifyCreateOutgoingConnectionFailed(request);
    }

    @Override
    public void onConnectionServiceFocusLost() {
        mCallList.notifyConnectionServiceFocusLost();
        connectionServiceFocusReleased();
    }

    @Override
    public void onConnectionServiceFocusGained() {
        mCallList.notifyConnectionServiceFocusGained();
    }

    private Connection createSelfManagedConnection(ConnectionRequest request, boolean isIncoming,
            boolean isHandover) {
        SelfManagedConnection connection = new SelfManagedConnection(mCallList,
                getApplicationContext(), isIncoming);
        connection.setListener(mCallList.getConnectionListener());
        connection.setConnectionProperties(Connection.PROPERTY_SELF_MANAGED);
        connection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED);
        // Purposely do not set the audio mode to voip since we expect this to be the default:
        // connection.setAudioModeIsVoip(true);
        connection.setVideoState(request.getVideoState());
        Random random = new Random();
        connection.setCallerDisplayName(TEST_NAMES[random.nextInt(TEST_NAMES.length)],
                TelecomManager.PRESENTATION_ALLOWED);
        connection.setExtras(request.getExtras());
        if (isIncoming) {
            connection.setIsIncomingCallUiShowing(request.shouldShowIncomingCallUi());
            connection.setRinging();
        } else {
            connection.setDialing();
        }
        Bundle requestExtras = request.getExtras();
        if (requestExtras != null) {
            boolean isHoldable = requestExtras.getBoolean(EXTRA_HOLDABLE, false);
            Log.i(this, "createConnection: isHandover=%b, handoverFrom=%s, holdable=%b",
                    isHandover,
                    requestExtras.getString(TelecomManager.EXTRA_HANDOVER_FROM_PHONE_ACCOUNT),
                    isHoldable);
            connection.setIsHandover(isHandover);
            if (isHoldable) {
                connection.setConnectionCapabilities(connection.getConnectionCapabilities() |
                        Connection.CAPABILITY_HOLD | Connection.CAPABILITY_SUPPORT_HOLD);
            }
            if (!isIncoming && connection.isHandover()) {
                Intent intent = new Intent(Intent.ACTION_MAIN, null);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClass(this, HandoverActivity.class);
                intent.putExtra(HandoverActivity.EXTRA_CALL_ID, connection.getCallId());
                startActivity(intent);
            } else {
                Log.i(this, "Handover incoming call created.");
            }
        }

        // Track the phone account handle which created this connection so we can distinguish them
        // in the sample call list later.
        Bundle moreExtras = new Bundle();
        moreExtras.putParcelable(SelfManagedConnection.EXTRA_PHONE_ACCOUNT_HANDLE,
                request.getAccountHandle());
        connection.putExtras(moreExtras);
        connection.setVideoState(request.getVideoState());
        Log.i(this, "createSelfManagedConnection %s", connection);
        mCallList.addConnection(connection);
        return connection;
    }
}