blob: 8518adcf083cee845a366539ca0bc6226e5bb9bf [file] [log] [blame]
Tyler Gunn9517c962017-01-26 07:24:08 -08001/*
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;
Tyler Gunn6f9ceb22017-04-06 08:47:01 -070022import android.os.Bundle;
Tyler Gunn9517c962017-01-26 07:24:08 -080023import android.telecom.ConnectionRequest;
24import android.telecom.Log;
25import android.telecom.PhoneAccount;
26import android.telecom.PhoneAccountHandle;
27import android.telecom.TelecomManager;
28import android.util.ArrayMap;
29
30import java.util.ArrayList;
31import java.util.List;
32import java.util.Map;
Tyler Gunn115c06e2017-03-02 09:29:07 -080033import java.util.Optional;
Tyler Gunn9517c962017-01-26 07:24:08 -080034
35/**
36 * Manages the list of {@link SelfManagedConnection} active in the sample third-party calling app.
37 */
38public class SelfManagedCallList {
39 public abstract static class Listener {
40 public void onCreateIncomingConnectionFailed(ConnectionRequest request) {};
41 public void onCreateOutgoingConnectionFailed(ConnectionRequest request) {};
42 public void onConnectionListChanged() {};
Tyler Gunnf4f05392018-03-26 18:57:59 +000043 public void onConnectionServiceFocusLost() {};
44 public void onConnectionServiceFocusGained() {};
Tyler Gunn9517c962017-01-26 07:24:08 -080045 }
46
47 public static String SELF_MANAGED_ACCOUNT_1 = "1";
48 public static String SELF_MANAGED_ACCOUNT_2 = "2";
Tyler Gunn2b17f232017-03-08 08:51:00 -080049 public static String SELF_MANAGED_NAME_1 = "SuperCall";
50 public static String SELF_MANAGED_NAME_2 = "Mega Call";
Tyler Gunn9517c962017-01-26 07:24:08 -080051
52 private static SelfManagedCallList sInstance;
53 private static ComponentName COMPONENT_NAME = new ComponentName(
54 SelfManagedCallList.class.getPackage().getName(),
55 SelfManagedConnectionService.class.getName());
56 private static Uri SELF_MANAGED_ADDRESS_1 = Uri.fromParts(PhoneAccount.SCHEME_TEL, "555-1212",
57 "");
58 private static Uri SELF_MANAGED_ADDRESS_2 = Uri.fromParts(PhoneAccount.SCHEME_SIP,
59 "me@test.org", "");
60 private static Map<String, PhoneAccountHandle> mPhoneAccounts = new ArrayMap();
61
62 public static SelfManagedCallList getInstance() {
63 if (sInstance == null) {
64 sInstance = new SelfManagedCallList();
65 }
66 return sInstance;
67 }
68
69 private Listener mListener;
70
71 private List<SelfManagedConnection> mConnections = new ArrayList<>();
72
Tyler Gunn115c06e2017-03-02 09:29:07 -080073 private SelfManagedConnection.Listener mConnectionListener =
74 new SelfManagedConnection.Listener() {
75 @Override
76 public void onConnectionStateChanged(SelfManagedConnection connection) {
77 notifyCallModified();
78 }
79
80 @Override
81 public void onConnectionRemoved(SelfManagedConnection connection) {
82 removeConnection(connection);
83 notifyCallModified();
84 }
85 };
86
87 public SelfManagedConnection.Listener getConnectionListener() {
88 return mConnectionListener;
89 }
90
91
Tyler Gunn9517c962017-01-26 07:24:08 -080092 public void setListener(Listener listener) {
93 mListener = listener;
94 }
95
96 public void registerPhoneAccounts(Context context) {
Tyler Gunn2b17f232017-03-08 08:51:00 -080097 registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_1, SELF_MANAGED_ADDRESS_1,
Tyler Gunn6f6f1c52017-04-17 18:22:04 -070098 SELF_MANAGED_NAME_1, true /* areCallsLogged */);
Tyler Gunn2b17f232017-03-08 08:51:00 -080099 registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_2, SELF_MANAGED_ADDRESS_2,
Tyler Gunn6f6f1c52017-04-17 18:22:04 -0700100 SELF_MANAGED_NAME_2, false /* areCallsLogged */);
Tyler Gunn9517c962017-01-26 07:24:08 -0800101 }
102
Tyler Gunn6f6f1c52017-04-17 18:22:04 -0700103 public void registerPhoneAccount(Context context, String id, Uri address, String name,
104 boolean areCallsLogged) {
Tyler Gunn9517c962017-01-26 07:24:08 -0800105 PhoneAccountHandle handle = new PhoneAccountHandle(COMPONENT_NAME, id);
106 mPhoneAccounts.put(id, handle);
Tyler Gunn6f9ceb22017-04-06 08:47:01 -0700107 Bundle extras = new Bundle();
108 extras.putBoolean(PhoneAccount.EXTRA_SUPPORTS_HANDOVER_TO, true);
Tyler Gunn6f6f1c52017-04-17 18:22:04 -0700109 if (areCallsLogged) {
110 extras.putBoolean(PhoneAccount.EXTRA_LOG_SELF_MANAGED_CALLS, true);
111 }
Tyler Gunn2b17f232017-03-08 08:51:00 -0800112 PhoneAccount.Builder builder = PhoneAccount.builder(handle, name)
Tyler Gunn9517c962017-01-26 07:24:08 -0800113 .addSupportedUriScheme(PhoneAccount.SCHEME_TEL)
114 .addSupportedUriScheme(PhoneAccount.SCHEME_SIP)
115 .setAddress(address)
116 .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED |
117 PhoneAccount.CAPABILITY_VIDEO_CALLING |
Tyler Gunn2b17f232017-03-08 08:51:00 -0800118 PhoneAccount.CAPABILITY_SUPPORTS_VIDEO_CALLING)
Tyler Gunn6f9ceb22017-04-06 08:47:01 -0700119 .setExtras(extras)
Tyler Gunn2b17f232017-03-08 08:51:00 -0800120 .setShortDescription(name);
Tyler Gunn9517c962017-01-26 07:24:08 -0800121
122 TelecomManager.from(context).registerPhoneAccount(builder.build());
123 }
124
125 public PhoneAccountHandle getPhoneAccountHandle(String id) {
126 return mPhoneAccounts.get(id);
127 }
128
129 public void notifyCreateIncomingConnectionFailed(ConnectionRequest request) {
130 if (mListener != null) {
131 mListener.onCreateIncomingConnectionFailed(request);
132 }
133 }
134
135 public void notifyCreateOutgoingConnectionFailed(ConnectionRequest request) {
136 if (mListener != null) {
137 mListener.onCreateOutgoingConnectionFailed(request);
138 }
139 }
140
Tyler Gunnf4f05392018-03-26 18:57:59 +0000141 public void notifyConnectionServiceFocusGained() {
142 if (mListener != null) {
143 mListener.onConnectionServiceFocusGained();
144 }
145 }
146
147 public void notifyConnectionServiceFocusLost() {
148 if (mListener != null) {
149 mListener.onConnectionServiceFocusLost();
150 }
151 }
152
Tyler Gunn9517c962017-01-26 07:24:08 -0800153 public void addConnection(SelfManagedConnection connection) {
154 Log.i(this, "addConnection %s", connection);
155 mConnections.add(connection);
156 if (mListener != null) {
157 Log.i(this, "addConnection calling onConnectionListChanged %s", connection);
158 mListener.onConnectionListChanged();
159 }
160 }
161
162 public void removeConnection(SelfManagedConnection connection) {
163 Log.i(this, "removeConnection %s", connection);
164 mConnections.remove(connection);
165 if (mListener != null) {
166 Log.i(this, "removeConnection calling onConnectionListChanged %s", connection);
167 mListener.onConnectionListChanged();
168 }
169 }
170
171 public List<SelfManagedConnection> getConnections() {
172 return mConnections;
173 }
174
Tyler Gunn115c06e2017-03-02 09:29:07 -0800175 public SelfManagedConnection getConnectionById(int callId) {
176 Optional<SelfManagedConnection> foundOptional = mConnections.stream()
177 .filter((c) -> {return c.getCallId() == callId;})
178 .findFirst();
179 return foundOptional.orElse(null);
180 }
181
Tyler Gunn9517c962017-01-26 07:24:08 -0800182 public void notifyCallModified() {
183 if (mListener != null) {
184 mListener.onConnectionListChanged();
185 }
186 }
187}