SelfManagedCallList.java revision 9517c960762f1ac4a3c558ff834fa1ace69f1e39
1/*
2 * Copyright (C) 2017 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
17package com.android.server.telecom.testapps;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.net.Uri;
22import android.telecom.ConnectionRequest;
23import android.telecom.Log;
24import android.telecom.PhoneAccount;
25import android.telecom.PhoneAccountHandle;
26import android.telecom.TelecomManager;
27import android.util.ArrayMap;
28
29import java.util.ArrayList;
30import java.util.List;
31import java.util.Map;
32
33/**
34 * Manages the list of {@link SelfManagedConnection} active in the sample third-party calling app.
35 */
36public class SelfManagedCallList {
37    public abstract static class Listener {
38        public void onCreateIncomingConnectionFailed(ConnectionRequest request) {};
39        public void onCreateOutgoingConnectionFailed(ConnectionRequest request) {};
40        public void onConnectionListChanged() {};
41    }
42
43    public static String SELF_MANAGED_ACCOUNT_1 = "1";
44    public static String SELF_MANAGED_ACCOUNT_2 = "2";
45
46    private static SelfManagedCallList sInstance;
47    private static ComponentName COMPONENT_NAME = new ComponentName(
48            SelfManagedCallList.class.getPackage().getName(),
49            SelfManagedConnectionService.class.getName());
50    private static Uri SELF_MANAGED_ADDRESS_1 = Uri.fromParts(PhoneAccount.SCHEME_TEL, "555-1212",
51            "");
52    private static Uri SELF_MANAGED_ADDRESS_2 = Uri.fromParts(PhoneAccount.SCHEME_SIP,
53            "me@test.org", "");
54    private static Map<String, PhoneAccountHandle> mPhoneAccounts = new ArrayMap();
55
56    public static SelfManagedCallList getInstance() {
57        if (sInstance == null) {
58            sInstance = new SelfManagedCallList();
59        }
60        return sInstance;
61    }
62
63    private Listener mListener;
64
65    private List<SelfManagedConnection> mConnections = new ArrayList<>();
66
67    public void setListener(Listener listener) {
68        mListener = listener;
69    }
70
71    public void registerPhoneAccounts(Context context) {
72        registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_1, SELF_MANAGED_ADDRESS_1);
73        registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_2, SELF_MANAGED_ADDRESS_2);
74    }
75
76    public void registerPhoneAccount(Context context, String id, Uri address) {
77        PhoneAccountHandle handle = new PhoneAccountHandle(COMPONENT_NAME, id);
78        mPhoneAccounts.put(id, handle);
79        PhoneAccount.Builder builder = PhoneAccount.builder(handle, id)
80                .addSupportedUriScheme(PhoneAccount.SCHEME_TEL)
81                .addSupportedUriScheme(PhoneAccount.SCHEME_SIP)
82                .setAddress(address)
83                .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED |
84                        PhoneAccount.CAPABILITY_VIDEO_CALLING |
85                        PhoneAccount.CAPABILITY_SUPPORTS_VIDEO_CALLING);
86
87        TelecomManager.from(context).registerPhoneAccount(builder.build());
88    }
89
90    public PhoneAccountHandle getPhoneAccountHandle(String id) {
91        return mPhoneAccounts.get(id);
92    }
93
94    public void notifyCreateIncomingConnectionFailed(ConnectionRequest request) {
95        if (mListener != null) {
96            mListener.onCreateIncomingConnectionFailed(request);
97        }
98    }
99
100    public void notifyCreateOutgoingConnectionFailed(ConnectionRequest request) {
101        if (mListener != null) {
102            mListener.onCreateOutgoingConnectionFailed(request);
103        }
104    }
105
106    public void addConnection(SelfManagedConnection connection) {
107        Log.i(this, "addConnection %s", connection);
108        mConnections.add(connection);
109        if (mListener != null) {
110            Log.i(this, "addConnection calling onConnectionListChanged %s", connection);
111            mListener.onConnectionListChanged();
112        }
113    }
114
115    public void removeConnection(SelfManagedConnection connection) {
116        Log.i(this, "removeConnection %s", connection);
117        mConnections.remove(connection);
118        if (mListener != null) {
119            Log.i(this, "removeConnection calling onConnectionListChanged %s", connection);
120            mListener.onConnectionListChanged();
121        }
122    }
123
124    public List<SelfManagedConnection> getConnections() {
125        return mConnections;
126    }
127
128    public void notifyCallModified() {
129        if (mListener != null) {
130            mListener.onConnectionListChanged();
131        }
132    }
133}
134