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 17 package com.android.server.telecom.testapps; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.telecom.ConnectionRequest; 24 import android.telecom.Log; 25 import android.telecom.PhoneAccount; 26 import android.telecom.PhoneAccountHandle; 27 import android.telecom.TelecomManager; 28 import android.util.ArrayMap; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.util.Map; 33 import java.util.Optional; 34 35 /** 36 * Manages the list of {@link SelfManagedConnection} active in the sample third-party calling app. 37 */ 38 public class SelfManagedCallList { 39 public abstract static class Listener { onCreateIncomingConnectionFailed(ConnectionRequest request)40 public void onCreateIncomingConnectionFailed(ConnectionRequest request) {}; onCreateOutgoingConnectionFailed(ConnectionRequest request)41 public void onCreateOutgoingConnectionFailed(ConnectionRequest request) {}; onConnectionListChanged()42 public void onConnectionListChanged() {}; onConnectionServiceFocusLost()43 public void onConnectionServiceFocusLost() {}; onConnectionServiceFocusGained()44 public void onConnectionServiceFocusGained() {}; 45 } 46 47 public static String SELF_MANAGED_ACCOUNT_1 = "1"; 48 public static String SELF_MANAGED_ACCOUNT_2 = "2"; 49 public static String SELF_MANAGED_ACCOUNT_3 = "3"; 50 public static String SELF_MANAGED_NAME_1 = "SuperCall"; 51 public static String SELF_MANAGED_NAME_2 = "Mega Call"; 52 public static String SELF_MANAGED_NAME_3 = "SM Call"; 53 public static String CUSTOM_URI_SCHEME = "custom"; 54 55 private static SelfManagedCallList sInstance; 56 private static ComponentName COMPONENT_NAME = new ComponentName( 57 SelfManagedCallList.class.getPackage().getName(), 58 SelfManagedConnectionService.class.getName()); 59 private static Uri SELF_MANAGED_ADDRESS_1 = Uri.fromParts(PhoneAccount.SCHEME_TEL, "555-1212", 60 ""); 61 private static Uri SELF_MANAGED_ADDRESS_2 = Uri.fromParts(PhoneAccount.SCHEME_SIP, 62 "me@test.org", ""); 63 private static Map<String, PhoneAccountHandle> mPhoneAccounts = new ArrayMap(); 64 getInstance()65 public static SelfManagedCallList getInstance() { 66 if (sInstance == null) { 67 sInstance = new SelfManagedCallList(); 68 } 69 return sInstance; 70 } 71 72 private Listener mListener; 73 74 private List<SelfManagedConnection> mConnections = new ArrayList<>(); 75 76 private SelfManagedConnection.Listener mConnectionListener = 77 new SelfManagedConnection.Listener() { 78 @Override 79 public void onConnectionStateChanged(SelfManagedConnection connection) { 80 notifyCallModified(); 81 } 82 83 @Override 84 public void onConnectionRemoved(SelfManagedConnection connection) { 85 removeConnection(connection); 86 notifyCallModified(); 87 } 88 }; 89 getConnectionListener()90 public SelfManagedConnection.Listener getConnectionListener() { 91 return mConnectionListener; 92 } 93 94 setListener(Listener listener)95 public void setListener(Listener listener) { 96 mListener = listener; 97 } 98 registerPhoneAccounts(Context context)99 public void registerPhoneAccounts(Context context) { 100 registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_1, SELF_MANAGED_ADDRESS_1, 101 SELF_MANAGED_NAME_1, true /* areCallsLogged */); 102 registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_2, SELF_MANAGED_ADDRESS_2, 103 SELF_MANAGED_NAME_2, false /* areCallsLogged */); 104 registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_3, SELF_MANAGED_ADDRESS_1, 105 SELF_MANAGED_NAME_3, true /* areCallsLogged */); 106 } 107 registerPhoneAccount(Context context, String id, Uri address, String name, boolean areCallsLogged)108 public void registerPhoneAccount(Context context, String id, Uri address, String name, 109 boolean areCallsLogged) { 110 PhoneAccountHandle handle = new PhoneAccountHandle(COMPONENT_NAME, id); 111 mPhoneAccounts.put(id, handle); 112 Bundle extras = new Bundle(); 113 extras.putBoolean(PhoneAccount.EXTRA_SUPPORTS_HANDOVER_TO, true); 114 if (areCallsLogged) { 115 extras.putBoolean(PhoneAccount.EXTRA_LOG_SELF_MANAGED_CALLS, true); 116 } 117 if (id.equals(SELF_MANAGED_ACCOUNT_3)) { 118 extras.putBoolean(PhoneAccount.EXTRA_ADD_SELF_MANAGED_CALLS_TO_INCALLSERVICE, true); 119 } 120 PhoneAccount.Builder builder = PhoneAccount.builder(handle, name) 121 .addSupportedUriScheme(PhoneAccount.SCHEME_TEL) 122 .addSupportedUriScheme(PhoneAccount.SCHEME_SIP) 123 .addSupportedUriScheme(CUSTOM_URI_SCHEME) 124 .setAddress(address) 125 .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED | 126 PhoneAccount.CAPABILITY_VIDEO_CALLING | 127 PhoneAccount.CAPABILITY_SUPPORTS_VIDEO_CALLING) 128 .setExtras(extras) 129 .setShortDescription(name); 130 131 TelecomManager.from(context).registerPhoneAccount(builder.build()); 132 } 133 getPhoneAccountHandle(String id)134 public PhoneAccountHandle getPhoneAccountHandle(String id) { 135 return mPhoneAccounts.get(id); 136 } 137 notifyCreateIncomingConnectionFailed(ConnectionRequest request)138 public void notifyCreateIncomingConnectionFailed(ConnectionRequest request) { 139 if (mListener != null) { 140 mListener.onCreateIncomingConnectionFailed(request); 141 } 142 } 143 notifyCreateOutgoingConnectionFailed(ConnectionRequest request)144 public void notifyCreateOutgoingConnectionFailed(ConnectionRequest request) { 145 if (mListener != null) { 146 mListener.onCreateOutgoingConnectionFailed(request); 147 } 148 } 149 notifyConnectionServiceFocusGained()150 public void notifyConnectionServiceFocusGained() { 151 if (mListener != null) { 152 mListener.onConnectionServiceFocusGained(); 153 } 154 } 155 notifyConnectionServiceFocusLost()156 public void notifyConnectionServiceFocusLost() { 157 if (mListener != null) { 158 mListener.onConnectionServiceFocusLost(); 159 } 160 } 161 addConnection(SelfManagedConnection connection)162 public void addConnection(SelfManagedConnection connection) { 163 Log.i(this, "addConnection %s", connection); 164 mConnections.add(connection); 165 if (mListener != null) { 166 Log.i(this, "addConnection calling onConnectionListChanged %s", connection); 167 mListener.onConnectionListChanged(); 168 } 169 } 170 removeConnection(SelfManagedConnection connection)171 public void removeConnection(SelfManagedConnection connection) { 172 Log.i(this, "removeConnection %s", connection); 173 mConnections.remove(connection); 174 if (mListener != null) { 175 Log.i(this, "removeConnection calling onConnectionListChanged %s", connection); 176 mListener.onConnectionListChanged(); 177 } 178 } 179 getConnections()180 public List<SelfManagedConnection> getConnections() { 181 return mConnections; 182 } 183 getConnectionById(int callId)184 public SelfManagedConnection getConnectionById(int callId) { 185 Optional<SelfManagedConnection> foundOptional = mConnections.stream() 186 .filter((c) -> {return c.getCallId() == callId;}) 187 .findFirst(); 188 return foundOptional.orElse(null); 189 } 190 notifyCallModified()191 public void notifyCallModified() { 192 if (mListener != null) { 193 mListener.onConnectionListChanged(); 194 } 195 } 196 } 197