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