Tyler Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 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; |
Tyler Gunn | 6f9ceb2 | 2017-04-06 08:47:01 -0700 | [diff] [blame] | 22 | import android.os.Bundle; |
Tyler Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 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; |
Tyler Gunn | 115c06e | 2017-03-02 09:29:07 -0800 | [diff] [blame] | 33 | import java.util.Optional; |
Tyler Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 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 { |
| 40 | public void onCreateIncomingConnectionFailed(ConnectionRequest request) {}; |
| 41 | public void onCreateOutgoingConnectionFailed(ConnectionRequest request) {}; |
| 42 | public void onConnectionListChanged() {}; |
Tyler Gunn | f4f0539 | 2018-03-26 18:57:59 +0000 | [diff] [blame^] | 43 | public void onConnectionServiceFocusLost() {}; |
| 44 | public void onConnectionServiceFocusGained() {}; |
Tyler Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | public static String SELF_MANAGED_ACCOUNT_1 = "1"; |
| 48 | public static String SELF_MANAGED_ACCOUNT_2 = "2"; |
Tyler Gunn | 2b17f23 | 2017-03-08 08:51:00 -0800 | [diff] [blame] | 49 | public static String SELF_MANAGED_NAME_1 = "SuperCall"; |
| 50 | public static String SELF_MANAGED_NAME_2 = "Mega Call"; |
Tyler Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 51 | |
| 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 Gunn | 115c06e | 2017-03-02 09:29:07 -0800 | [diff] [blame] | 73 | 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 Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 92 | public void setListener(Listener listener) { |
| 93 | mListener = listener; |
| 94 | } |
| 95 | |
| 96 | public void registerPhoneAccounts(Context context) { |
Tyler Gunn | 2b17f23 | 2017-03-08 08:51:00 -0800 | [diff] [blame] | 97 | registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_1, SELF_MANAGED_ADDRESS_1, |
Tyler Gunn | 6f6f1c5 | 2017-04-17 18:22:04 -0700 | [diff] [blame] | 98 | SELF_MANAGED_NAME_1, true /* areCallsLogged */); |
Tyler Gunn | 2b17f23 | 2017-03-08 08:51:00 -0800 | [diff] [blame] | 99 | registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_2, SELF_MANAGED_ADDRESS_2, |
Tyler Gunn | 6f6f1c5 | 2017-04-17 18:22:04 -0700 | [diff] [blame] | 100 | SELF_MANAGED_NAME_2, false /* areCallsLogged */); |
Tyler Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Tyler Gunn | 6f6f1c5 | 2017-04-17 18:22:04 -0700 | [diff] [blame] | 103 | public void registerPhoneAccount(Context context, String id, Uri address, String name, |
| 104 | boolean areCallsLogged) { |
Tyler Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 105 | PhoneAccountHandle handle = new PhoneAccountHandle(COMPONENT_NAME, id); |
| 106 | mPhoneAccounts.put(id, handle); |
Tyler Gunn | 6f9ceb2 | 2017-04-06 08:47:01 -0700 | [diff] [blame] | 107 | Bundle extras = new Bundle(); |
| 108 | extras.putBoolean(PhoneAccount.EXTRA_SUPPORTS_HANDOVER_TO, true); |
Tyler Gunn | 6f6f1c5 | 2017-04-17 18:22:04 -0700 | [diff] [blame] | 109 | if (areCallsLogged) { |
| 110 | extras.putBoolean(PhoneAccount.EXTRA_LOG_SELF_MANAGED_CALLS, true); |
| 111 | } |
Tyler Gunn | 2b17f23 | 2017-03-08 08:51:00 -0800 | [diff] [blame] | 112 | PhoneAccount.Builder builder = PhoneAccount.builder(handle, name) |
Tyler Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 113 | .addSupportedUriScheme(PhoneAccount.SCHEME_TEL) |
| 114 | .addSupportedUriScheme(PhoneAccount.SCHEME_SIP) |
| 115 | .setAddress(address) |
| 116 | .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED | |
| 117 | PhoneAccount.CAPABILITY_VIDEO_CALLING | |
Tyler Gunn | 2b17f23 | 2017-03-08 08:51:00 -0800 | [diff] [blame] | 118 | PhoneAccount.CAPABILITY_SUPPORTS_VIDEO_CALLING) |
Tyler Gunn | 6f9ceb2 | 2017-04-06 08:47:01 -0700 | [diff] [blame] | 119 | .setExtras(extras) |
Tyler Gunn | 2b17f23 | 2017-03-08 08:51:00 -0800 | [diff] [blame] | 120 | .setShortDescription(name); |
Tyler Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 121 | |
| 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 Gunn | f4f0539 | 2018-03-26 18:57:59 +0000 | [diff] [blame^] | 141 | 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 Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 153 | 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 Gunn | 115c06e | 2017-03-02 09:29:07 -0800 | [diff] [blame] | 175 | 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 Gunn | 9517c96 | 2017-01-26 07:24:08 -0800 | [diff] [blame] | 182 | public void notifyCallModified() { |
| 183 | if (mListener != null) { |
| 184 | mListener.onConnectionListChanged(); |
| 185 | } |
| 186 | } |
| 187 | } |