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.telecom.PhoneAccount;
23 import android.telecom.PhoneAccountHandle;
24 import android.telecom.TelecomManager;
25 
26 import com.android.cts.verifier.PassFailButtons;
27 
28 /**
29  * Utilities class for dealing with the Telecom CTS Verifier PhoneAccounts.
30  */
31 public class PhoneAccountUtils {
32     public static final String TEST_PHONE_ACCOUNT_ID = "test";
33     public static final String TEST_PHONE_ACCOUNT_LABEL = "CTS Verifier Test";
34     public static final Uri TEST_PHONE_ACCOUNT_ADDRESS = Uri.parse("sip:test@android.com");
35 
36     public static final PhoneAccountHandle TEST_PHONE_ACCOUNT_HANDLE =
37             new PhoneAccountHandle(new ComponentName(
38                     PassFailButtons.class.getPackage().getName(),
39                     CtsConnectionService.class.getName()), TEST_PHONE_ACCOUNT_ID);
40     public static final PhoneAccount TEST_PHONE_ACCOUNT = new PhoneAccount.Builder(
41             TEST_PHONE_ACCOUNT_HANDLE, TEST_PHONE_ACCOUNT_LABEL)
42             .setAddress(TEST_PHONE_ACCOUNT_ADDRESS)
43             .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)
44             .build();
45 
46     public static final String TEST_SELF_MAANGED_PHONE_ACCOUNT_ID = "selfMgdTest";
47     public static final String TEST_SELF_MANAGED_PHONE_ACCOUNT_LABEL = "CTSVerifier";
48     public static final Uri TEST_SELF_MANAGED_PHONE_ACCOUNT_ADDRESS =
49             Uri.parse("sip:sekf@android.com");
50     public static final PhoneAccountHandle TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE =
51             new PhoneAccountHandle(new ComponentName(
52                     PassFailButtons.class.getPackage().getName(),
53                     CtsConnectionService.class.getName()), TEST_SELF_MAANGED_PHONE_ACCOUNT_ID);
54     public static final PhoneAccount TEST_SELF_MANAGED_PHONE_ACCOUNT = new PhoneAccount.Builder(
55             TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE, TEST_SELF_MANAGED_PHONE_ACCOUNT_LABEL)
56             .setAddress(TEST_SELF_MANAGED_PHONE_ACCOUNT_ADDRESS)
57             .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
58             .build();
59 
60     public static final String TEST_SELF_MAANGED_PHONE_ACCOUNT2_ID = "selfMgdTest2";
61     public static final String TEST_SELF_MANAGED_PHONE_ACCOUNT2_LABEL = "CTSVerifier2";
62 
63     public static final PhoneAccountHandle TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE_2 =
64         new PhoneAccountHandle(new ComponentName(
65                     PassFailButtons.class.getPackage().getName(),
66                     CtsSelfManagedConnectionService.class.getName()),
67                 TEST_SELF_MAANGED_PHONE_ACCOUNT2_ID);
68     public static final PhoneAccount TEST_SELF_MANAGED_PHONE_ACCOUNT_2 = new PhoneAccount.Builder(
69             TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE_2, TEST_SELF_MANAGED_PHONE_ACCOUNT2_LABEL)
70             .setAddress(TEST_SELF_MANAGED_PHONE_ACCOUNT_ADDRESS)
71             .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
72             .build();
73 
74     /**
75      * Registers the test phone account.
76      * @param context The context.
77      */
registerTestPhoneAccount(Context context)78     public static void registerTestPhoneAccount(Context context) {
79         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
80                 Context.TELECOM_SERVICE);
81         telecomManager.registerPhoneAccount(TEST_PHONE_ACCOUNT);
82     }
83 
84     /**
85      * Retrieves the test phone account, or null if not registered.
86      * @param context The context.
87      * @return The Phone Account.
88      */
getPhoneAccount(Context context)89     public static PhoneAccount getPhoneAccount(Context context) {
90         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
91                 Context.TELECOM_SERVICE);
92         return telecomManager.getPhoneAccount(TEST_PHONE_ACCOUNT_HANDLE);
93     }
94 
95     /**
96      * Unregisters the test phone account.
97      * @param context The context.
98      */
unRegisterTestPhoneAccount(Context context)99     public static void unRegisterTestPhoneAccount(Context context) {
100         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
101                 Context.TELECOM_SERVICE);
102         telecomManager.unregisterPhoneAccount(TEST_PHONE_ACCOUNT_HANDLE);
103     }
104 
105     /**
106      * Registers the test self-managed phone accounts.
107      * @param context The context.
108      */
registerTestSelfManagedPhoneAccount(Context context)109     public static void registerTestSelfManagedPhoneAccount(Context context) {
110         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
111                 Context.TELECOM_SERVICE);
112         telecomManager.registerPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT);
113         telecomManager.registerPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_2);
114     }
115 
116     /**
117      * Unregisters the test self-managed phone accounts.
118      * @param context The context.
119      */
unRegisterTestSelfManagedPhoneAccount(Context context)120     public static void unRegisterTestSelfManagedPhoneAccount(Context context) {
121         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
122                 Context.TELECOM_SERVICE);
123         telecomManager.unregisterPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE);
124         telecomManager.unregisterPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE_2);
125     }
126 
127     /**
128      * Retrieves the test phone account, or null if not registered.
129      * @param context The context.
130      * @return The Phone Account.
131      */
getSelfManagedPhoneAccount(Context context)132     public static PhoneAccount getSelfManagedPhoneAccount(Context context) {
133         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
134                 Context.TELECOM_SERVICE);
135         return telecomManager.getPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE);
136     }
137 
138     /**
139      * Gets the default outgoing phone account, or null if none selected.
140      * @param context The context.
141      * @return The PhoneAccountHandle
142      */
getDefaultOutgoingPhoneAccount(Context context)143     public static PhoneAccountHandle getDefaultOutgoingPhoneAccount(Context context) {
144         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
145                 Context.TELECOM_SERVICE);
146         return telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL);
147     }
148 
149     /**
150      * Retrieves the test phone account, or null if not registered.
151      * @param context The context.
152      * @return The Phone Account.
153      */
getSelfManagedPhoneAccount2(Context context)154     public static PhoneAccount getSelfManagedPhoneAccount2(Context context) {
155         TelecomManager telecomManager = (TelecomManager) context.getSystemService(
156                 Context.TELECOM_SERVICE);
157         return telecomManager.getPhoneAccount(TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE_2);
158     }
159 }
160