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.cts.verifier.telecom;
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.PhoneAccount;
24 import android.telecom.PhoneAccountHandle;
25 import android.telecom.TelecomManager;
26 
27 import com.android.compatibility.common.util.ApiTest;
28 import com.android.cts.verifier.PassFailButtons;
29 
30 /**
31  * Utilities class for dealing with the Telecom CTS Verifier PhoneAccounts.
32  */
33 @ApiTest(apis={"android.Telecom.PhoneAccount"})
34 public class PhoneAccountUtils {
35     public static final String TEST_PHONE_ACCOUNT_ID = "test";
36     public static final String TEST_PHONE_ACCOUNT_LABEL = "CTS Verifier Test";
37     public static final Uri TEST_PHONE_ACCOUNT_ADDRESS = Uri.parse("sip:test@android.com");
38 
39     public static final PhoneAccountHandle TEST_PHONE_ACCOUNT_HANDLE =
40             new PhoneAccountHandle(new ComponentName(
41                     PassFailButtons.class.getPackage().getName(),
42                     CtsConnectionService.class.getName()), TEST_PHONE_ACCOUNT_ID);
43     public static final PhoneAccount TEST_PHONE_ACCOUNT = new PhoneAccount.Builder(
44             TEST_PHONE_ACCOUNT_HANDLE, TEST_PHONE_ACCOUNT_LABEL)
45             .setAddress(TEST_PHONE_ACCOUNT_ADDRESS)
46             .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)
47             .build();
48 
49     public static final String TEST_SELF_MAANGED_PHONE_ACCOUNT_ID = "selfMgdTest";
50     public static final String TEST_SELF_MANAGED_PHONE_ACCOUNT_LABEL = "CTSVerifier";
51     public static final Uri TEST_SELF_MANAGED_PHONE_ACCOUNT_ADDRESS =
52             Uri.parse("sip:sekf@android.com");
53     public static final PhoneAccountHandle TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE =
54             new PhoneAccountHandle(new ComponentName(
55                     PassFailButtons.class.getPackage().getName(),
56                     CtsConnectionService.class.getName()), TEST_SELF_MAANGED_PHONE_ACCOUNT_ID);
57     public static final Bundle TEST_SELF_MANAGED_PHONE_ACCOUNT_EXTRA;
58     static {
59         TEST_SELF_MANAGED_PHONE_ACCOUNT_EXTRA = new Bundle();
TEST_SELF_MANAGED_PHONE_ACCOUNT_EXTRA.putBoolean( PhoneAccount.EXTRA_ADD_SELF_MANAGED_CALLS_TO_INCALLSERVICE, true)60         TEST_SELF_MANAGED_PHONE_ACCOUNT_EXTRA.putBoolean(
61                 PhoneAccount.EXTRA_ADD_SELF_MANAGED_CALLS_TO_INCALLSERVICE, true);
62     }
63 
64     public static final PhoneAccount TEST_SELF_MANAGED_PHONE_ACCOUNT = new PhoneAccount.Builder(
65             TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE, TEST_SELF_MANAGED_PHONE_ACCOUNT_LABEL)
66             .setAddress(TEST_SELF_MANAGED_PHONE_ACCOUNT_ADDRESS)
67             .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
68             .setExtras(TEST_SELF_MANAGED_PHONE_ACCOUNT_EXTRA)
69             .build();
70 
71     public static final String TEST_SELF_MAANGED_PHONE_ACCOUNT2_ID = "selfMgdTest2";
72     public static final String TEST_SELF_MANAGED_PHONE_ACCOUNT2_LABEL = "CTSVerifier2";
73 
74     public static final PhoneAccountHandle TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE_2 =
75         new PhoneAccountHandle(new ComponentName(
76                     PassFailButtons.class.getPackage().getName(),
77                     CtsSelfManagedConnectionService.class.getName()),
78                 TEST_SELF_MAANGED_PHONE_ACCOUNT2_ID);
79     public static final PhoneAccount TEST_SELF_MANAGED_PHONE_ACCOUNT_2 = new PhoneAccount.Builder(
80             TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE_2, TEST_SELF_MANAGED_PHONE_ACCOUNT2_LABEL)
81             .setAddress(TEST_SELF_MANAGED_PHONE_ACCOUNT_ADDRESS)
82             .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
83             .build();
84 
85     /**
86      * Registers the test phone account.
87      * @param context The context.
88      */
registerTestPhoneAccount(Context context)89     public static void registerTestPhoneAccount(Context context) {
90         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
91                 Context.TELECOM_SERVICE);
92         telecomManager.registerPhoneAccount(TEST_PHONE_ACCOUNT);
93     }
94 
95     /**
96      * Retrieves the test phone account, or null if not registered.
97      * @param context The context.
98      * @return The Phone Account.
99      */
getPhoneAccount(Context context)100     public static PhoneAccount getPhoneAccount(Context context) {
101         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
102                 Context.TELECOM_SERVICE);
103         return telecomManager.getPhoneAccount(TEST_PHONE_ACCOUNT_HANDLE);
104     }
105 
106     /**
107      * Retrieves a specific phone account
108      * @param context the context
109      * @param pah phoneAccountHandle request
110      * @return PhoneAccount corresponding to the phoneAccountHandle
111      */
getSpecificPhoneAccount(Context context, PhoneAccountHandle pah)112     public static PhoneAccount getSpecificPhoneAccount(Context context, PhoneAccountHandle pah) {
113         TelecomManager telecomManager = context.getSystemService(TelecomManager.class);
114         if (telecomManager == null) return null;
115         return telecomManager.getPhoneAccount(pah);
116     }
117 
118     /**
119      * Unregisters the test phone account.
120      * @param context The context.
121      */
unRegisterTestPhoneAccount(Context context)122     public static void unRegisterTestPhoneAccount(Context context) {
123         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
124                 Context.TELECOM_SERVICE);
125         telecomManager.unregisterPhoneAccount(TEST_PHONE_ACCOUNT_HANDLE);
126     }
127 
128     /**
129      * Registers the test self-managed phone accounts.
130      * @param context The context.
131      */
registerTestSelfManagedPhoneAccount(Context context)132     public static void registerTestSelfManagedPhoneAccount(Context context) {
133         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
134                 Context.TELECOM_SERVICE);
135         telecomManager.registerPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT);
136         telecomManager.registerPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_2);
137     }
138 
139     /**
140      * Unregisters the test self-managed phone accounts.
141      * @param context The context.
142      */
unRegisterTestSelfManagedPhoneAccount(Context context)143     public static void unRegisterTestSelfManagedPhoneAccount(Context context) {
144         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
145                 Context.TELECOM_SERVICE);
146         telecomManager.unregisterPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE);
147         telecomManager.unregisterPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE_2);
148     }
149 
150     /**
151      * Retrieves the test phone account, or null if not registered.
152      * @param context The context.
153      * @return The Phone Account.
154      */
getSelfManagedPhoneAccount(Context context)155     public static PhoneAccount getSelfManagedPhoneAccount(Context context) {
156         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
157                 Context.TELECOM_SERVICE);
158         return telecomManager.getPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE);
159     }
160 
161     /**
162      * Gets the default outgoing phone account, or null if none selected.
163      * @param context The context.
164      * @return The PhoneAccountHandle
165      */
getDefaultOutgoingPhoneAccount(Context context)166     public static PhoneAccountHandle getDefaultOutgoingPhoneAccount(Context context) {
167         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
168                 Context.TELECOM_SERVICE);
169         return telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL);
170     }
171 
172     /**
173      * Retrieves the test phone account, or null if not registered.
174      * @param context The context.
175      * @return The Phone Account.
176      */
getSelfManagedPhoneAccount2(Context context)177     public static PhoneAccount getSelfManagedPhoneAccount2(Context context) {
178         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
179                 Context.TELECOM_SERVICE);
180         return telecomManager.getPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE_2);
181     }
182 
183     /**
184      * cleans up all ongoing cts connections
185      * @param context the context
186      */
cleanupConnectionServices(Context context)187     public static void cleanupConnectionServices(Context context) {
188         CtsSelfManagedConnectionService ctsSelfConnSvr =
189                 CtsSelfManagedConnectionService.getConnectionService();
190         if (ctsSelfConnSvr != null) {
191             ctsSelfConnSvr.getConnections()
192                     .stream()
193                     .forEach((c) -> {
194                         c.onDisconnect();
195                     });
196         }
197 
198         CtsConnectionService ctsConnectionService =
199                 CtsConnectionService.getConnectionService();
200         if (ctsConnectionService != null) {
201             ctsConnectionService.getConnections()
202                     .stream()
203                     .forEach((c) -> {
204                         c.onDisconnect();
205                     });
206         }
207         PhoneAccountUtils.unRegisterTestSelfManagedPhoneAccount(context);
208     }
209 }
210