summaryrefslogtreecommitdiff
path: root/testapps/src/com/android/server/telecom/testapps/SelfManagedCallList.java
blob: 273b06024773b9104b564ff6f097dffba8aebe43 (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
/*
 * 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.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.telecom.ConnectionRequest;
import android.telecom.Log;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.util.ArrayMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Manages the list of {@link SelfManagedConnection} active in the sample third-party calling app.
 */
public class SelfManagedCallList {
    public abstract static class Listener {
        public void onCreateIncomingConnectionFailed(ConnectionRequest request) {};
        public void onCreateOutgoingConnectionFailed(ConnectionRequest request) {};
        public void onConnectionListChanged() {};
        public void onConnectionServiceFocusLost() {};
        public void onConnectionServiceFocusGained() {};
    }

    public static String SELF_MANAGED_ACCOUNT_1 = "1";
    public static String SELF_MANAGED_ACCOUNT_2 = "2";
    public static String SELF_MANAGED_ACCOUNT_1A = "1A";
    public static String SELF_MANAGED_ACCOUNT_3 = "3";
    public static String SELF_MANAGED_NAME_1 = "SuperCall";
    public static String SELF_MANAGED_NAME_2 = "Mega Call";
    public static String SELF_MANAGED_NAME_1A = "SM Call";
    public static String SELF_MANAGED_NAME_3 = "Sep Process";
    public static String CUSTOM_URI_SCHEME = "custom";

    private static SelfManagedCallList sInstance;
    private static ComponentName COMPONENT_NAME = new ComponentName(
            SelfManagedCallList.class.getPackage().getName(),
            SelfManagedConnectionService.class.getName());
    private static ComponentName OTHER_COMPONENT_NAME = new ComponentName(
            SelfManagedCallList.class.getPackage().getName(),
            OtherSelfManagedConnectionService.class.getName());
    private static Uri SELF_MANAGED_ADDRESS_1 = Uri.fromParts(PhoneAccount.SCHEME_TEL, "555-1212",
            "");
    private static Uri SELF_MANAGED_ADDRESS_2 = Uri.fromParts(PhoneAccount.SCHEME_SIP,
            "me@test.org", "");
    private static Uri SELF_MANAGED_ADDRESS_3 = Uri.fromParts(PhoneAccount.SCHEME_SIP,
            "hilda@test.org", "");
    private static Map<String, PhoneAccountHandle> mPhoneAccounts = new ArrayMap();

    public static SelfManagedCallList getInstance() {
        if (sInstance == null) {
            sInstance = new SelfManagedCallList();
        }
        return sInstance;
    }

    private Listener mListener;

    private List<SelfManagedConnection> mConnections = new ArrayList<>();

    private SelfManagedConnection.Listener mConnectionListener =
            new SelfManagedConnection.Listener() {
                @Override
                public void onConnectionStateChanged(SelfManagedConnection connection) {
                    notifyCallModified();
                }

                @Override
                public void onConnectionRemoved(SelfManagedConnection connection) {
                    removeConnection(connection);
                    notifyCallModified();
                }
    };

    public SelfManagedConnection.Listener getConnectionListener() {
        return mConnectionListener;
    }


    public void setListener(Listener listener) {
        mListener = listener;
    }

    public void registerPhoneAccounts(Context context) {
        registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_1, SELF_MANAGED_ADDRESS_1,
                SELF_MANAGED_NAME_1, true /* areCallsLogged */);
        registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_2, SELF_MANAGED_ADDRESS_2,
                SELF_MANAGED_NAME_2, false /* areCallsLogged */);
        registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_1A, SELF_MANAGED_ADDRESS_1,
                SELF_MANAGED_NAME_1A, true /* areCallsLogged */);
        registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_1A, SELF_MANAGED_ADDRESS_1,
                SELF_MANAGED_NAME_1A, true /* areCallsLogged */);
        registerPhoneAccount(context, OTHER_COMPONENT_NAME, SELF_MANAGED_ACCOUNT_3,
                SELF_MANAGED_ADDRESS_3, SELF_MANAGED_NAME_3, false /* areCallsLogged */);
    }

    public void registerPhoneAccount(Context context, String id, Uri address, String name,
            boolean areCallsLogged) {
        registerPhoneAccount(context, COMPONENT_NAME, id, address, name, areCallsLogged);
    }

    public void registerPhoneAccount(Context context, ComponentName componentName, String id,
            Uri address, String name, boolean areCallsLogged) {
        PhoneAccountHandle handle = new PhoneAccountHandle(componentName, id);
        mPhoneAccounts.put(id, handle);
        Bundle extras = new Bundle();
        extras.putBoolean(PhoneAccount.EXTRA_SUPPORTS_HANDOVER_TO, true);
        if (areCallsLogged) {
            extras.putBoolean(PhoneAccount.EXTRA_LOG_SELF_MANAGED_CALLS, true);
        }
        if (id.equals(SELF_MANAGED_ACCOUNT_1A)) {
            extras.putBoolean(PhoneAccount.EXTRA_ADD_SELF_MANAGED_CALLS_TO_INCALLSERVICE, true);
        }
        PhoneAccount.Builder builder = PhoneAccount.builder(handle, name)
                .addSupportedUriScheme(PhoneAccount.SCHEME_TEL)
                .addSupportedUriScheme(PhoneAccount.SCHEME_SIP)
                .addSupportedUriScheme(CUSTOM_URI_SCHEME)
                .setAddress(address)
                .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED |
                        PhoneAccount.CAPABILITY_VIDEO_CALLING |
                        PhoneAccount.CAPABILITY_SUPPORTS_VIDEO_CALLING)
                .setExtras(extras)
                .setShortDescription(name);

        TelecomManager.from(context).registerPhoneAccount(builder.build());
    }

    public PhoneAccountHandle getPhoneAccountHandle(String id) {
        return mPhoneAccounts.get(id);
    }

    public void notifyCreateIncomingConnectionFailed(ConnectionRequest request) {
        if (mListener != null) {
            mListener.onCreateIncomingConnectionFailed(request);
        }
    }

    public void notifyCreateOutgoingConnectionFailed(ConnectionRequest request) {
        if (mListener != null) {
            mListener.onCreateOutgoingConnectionFailed(request);
        }
    }

    public void notifyConnectionServiceFocusGained() {
        if (mListener != null) {
            mListener.onConnectionServiceFocusGained();
        }
    }

    public void notifyConnectionServiceFocusLost() {
        if (mListener != null) {
            mListener.onConnectionServiceFocusLost();
        }
    }

    public void addConnection(SelfManagedConnection connection) {
        Log.i(this, "addConnection %s", connection);
        mConnections.add(connection);
        if (mListener != null) {
            Log.i(this, "addConnection calling onConnectionListChanged %s", connection);
            mListener.onConnectionListChanged();
        }
    }

    public void removeConnection(SelfManagedConnection connection) {
        Log.i(this, "removeConnection %s", connection);
        mConnections.remove(connection);
        if (mListener != null) {
            Log.i(this, "removeConnection calling onConnectionListChanged %s", connection);
            mListener.onConnectionListChanged();
        }
    }

    public List<SelfManagedConnection> getConnections() {
        return mConnections;
    }

    public SelfManagedConnection getConnectionById(int callId) {
        Optional<SelfManagedConnection> foundOptional = mConnections.stream()
                .filter((c) -> {return c.getCallId() == callId;})
                .findFirst();
        return foundOptional.orElse(null);
    }

    public void notifyCallModified() {
        if (mListener != null) {
            mListener.onConnectionListChanged();
        }
    }
}