blob: fbbc58973d7de1ce315d5e43e139b23f561225b3 [file] [log] [blame]
Nancy Chenbbf35962015-12-28 17:17:27 -08001/*
2 * Copyright (C) 2015 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 */
Gary Mai69c182a2016-12-05 13:07:03 -080016package com.android.contacts.compat.telecom;
Nancy Chenbbf35962015-12-28 17:17:27 -080017
18import android.app.Activity;
19import android.content.Intent;
Nancy Chen78384182015-12-22 17:21:13 -080020import android.net.Uri;
Nancy Chenbbf35962015-12-28 17:17:27 -080021import android.support.annotation.Nullable;
Nancy Chen78384182015-12-22 17:21:13 -080022import android.telecom.PhoneAccount;
Nancy Chenbbf35962015-12-28 17:17:27 -080023import android.telecom.PhoneAccountHandle;
24import android.telecom.TelecomManager;
25import android.telephony.PhoneNumberUtils;
26import android.telephony.TelephonyManager;
Nancy Chen78384182015-12-22 17:21:13 -080027import android.text.TextUtils;
Nancy Chenbbf35962015-12-28 17:17:27 -080028
Gary Mai69c182a2016-12-05 13:07:03 -080029import com.android.contacts.compat.CompatUtils;
Nancy Chenbbf35962015-12-28 17:17:27 -080030
Nancy Chen78384182015-12-22 17:21:13 -080031import java.util.ArrayList;
32import java.util.List;
33
Nancy Chenbbf35962015-12-28 17:17:27 -080034/**
Nancy Chen78384182015-12-22 17:21:13 -080035 * Compatibility class for {@link android.telecom.TelecomManager}.
Nancy Chenbbf35962015-12-28 17:17:27 -080036 */
37public class TelecomManagerCompat {
Nancy Chen78384182015-12-22 17:21:13 -080038 public static final String TELECOM_MANAGER_CLASS = "android.telecom.TelecomManager";
Nancy Chenbbf35962015-12-28 17:17:27 -080039 /**
40 * Places a new outgoing call to the provided address using the system telecom service with
41 * the specified intent.
42 *
43 * @param activity {@link Activity} used to start another activity for the given intent
44 * @param telecomManager the {@link TelecomManager} used to place a call, if possible
45 * @param intent the intent for the call
Nancy Chenbbf35962015-12-28 17:17:27 -080046 */
Nancy Chen78384182015-12-22 17:21:13 -080047 public static void placeCall(@Nullable Activity activity,
48 @Nullable TelecomManager telecomManager, @Nullable Intent intent) {
49 if (activity == null || telecomManager == null || intent == null) {
50 return;
51 }
Nancy Chenbbf35962015-12-28 17:17:27 -080052 if (CompatUtils.isMarshmallowCompatible()) {
53 telecomManager.placeCall(intent.getData(), intent.getExtras());
54 return;
55 }
56 activity.startActivityForResult(intent, 0);
57 }
58
59 /**
Nancy Chen78384182015-12-22 17:21:13 -080060 * Get the URI for running an adn query.
61 *
62 * @param telecomManager the {@link TelecomManager} used for method calls, if possible.
63 * @param accountHandle The handle for the account to derive an adn query URI for or
64 * {@code null} to return a URI which will use the default account.
65 * @return The URI (with the content:// scheme) specific to the specified {@link PhoneAccount}
66 * for the the content retrieve.
67 */
68 public static Uri getAdnUriForPhoneAccount(@Nullable TelecomManager telecomManager,
69 PhoneAccountHandle accountHandle) {
70 if (telecomManager != null && (CompatUtils.isMarshmallowCompatible()
71 || CompatUtils.isMethodAvailable(TELECOM_MANAGER_CLASS, "getAdnUriForPhoneAccount",
72 PhoneAccountHandle.class))) {
73 return telecomManager.getAdnUriForPhoneAccount(accountHandle);
74 }
75 return Uri.parse("content://icc/adn");
76 }
77
78 /**
79 * Returns a list of {@link PhoneAccountHandle}s which can be used to make and receive phone
80 * calls. The returned list includes only those accounts which have been explicitly enabled
81 * by the user.
82 *
83 * @param telecomManager the {@link TelecomManager} used for method calls, if possible.
84 * @return A list of PhoneAccountHandle objects.
85 */
86 public static List<PhoneAccountHandle> getCallCapablePhoneAccounts(
87 @Nullable TelecomManager telecomManager) {
88 if (telecomManager != null && (CompatUtils.isMarshmallowCompatible()
89 || CompatUtils.isMethodAvailable(TELECOM_MANAGER_CLASS,
90 "getCallCapablePhoneAccounts"))) {
91 return telecomManager.getCallCapablePhoneAccounts();
92 }
93 return new ArrayList<>();
94 }
95
96 /**
97 * Used to determine the currently selected default dialer package.
98 *
99 * @param telecomManager the {@link TelecomManager} used for method calls, if possible.
100 * @return package name for the default dialer package or null if no package has been
101 * selected as the default dialer.
102 */
103 @Nullable
104 public static String getDefaultDialerPackage(@Nullable TelecomManager telecomManager) {
105 if (telecomManager != null && CompatUtils.isDefaultDialerCompatible()) {
106 return telecomManager.getDefaultDialerPackage();
107 }
108 return null;
109 }
110
111 /**
112 * Return the {@link PhoneAccount} which will be used to place outgoing calls to addresses with
113 * the specified {@code uriScheme}. This PhoneAccount will always be a member of the
114 * list which is returned from invoking {@link TelecomManager#getCallCapablePhoneAccounts()}.
115 * The specific account returned depends on the following priorities:
116 *
117 * 1. If the user-selected default PhoneAccount supports the specified scheme, it will
118 * be returned.
119 * 2. If there exists only one PhoneAccount that supports the specified scheme, it
120 * will be returned.
121 *
122 * If no PhoneAccount fits the criteria above, this method will return {@code null}.
123 *
124 * @param telecomManager the {@link TelecomManager} used for method calls, if possible.
125 * @param uriScheme The URI scheme.
126 * @return The {@link PhoneAccountHandle} corresponding to the account to be used.
127 */
128 @Nullable
129 public static PhoneAccountHandle getDefaultOutgoingPhoneAccount(
130 @Nullable TelecomManager telecomManager, @Nullable String uriScheme) {
131 if (telecomManager != null && (CompatUtils.isMarshmallowCompatible()
132 || CompatUtils.isMethodAvailable(TELECOM_MANAGER_CLASS,
133 "getDefaultOutgoingPhoneAccount", String.class))) {
134 return telecomManager.getDefaultOutgoingPhoneAccount(uriScheme);
135 }
136 return null;
137 }
138
139 /**
Nancy Chenbbf35962015-12-28 17:17:27 -0800140 * Return the line 1 phone number for given phone account.
141 *
142 * @param telecomManager the {@link TelecomManager} to use in the event that
143 * {@link TelecomManager#getLine1Number(PhoneAccountHandle)} is available
144 * @param telephonyManager the {@link TelephonyManager} to use if TelecomManager#getLine1Number
145 * is unavailable
146 * @param phoneAccountHandle the phoneAccountHandle upon which to check the line one number
147 * @return the line one number
Nancy Chenbbf35962015-12-28 17:17:27 -0800148 */
Nancy Chen78384182015-12-22 17:21:13 -0800149 @Nullable
150 public static String getLine1Number(@Nullable TelecomManager telecomManager,
151 @Nullable TelephonyManager telephonyManager,
152 @Nullable PhoneAccountHandle phoneAccountHandle) {
153 if (telecomManager != null && CompatUtils.isMarshmallowCompatible()) {
Nancy Chenbbf35962015-12-28 17:17:27 -0800154 return telecomManager.getLine1Number(phoneAccountHandle);
155 }
Nancy Chen78384182015-12-22 17:21:13 -0800156 if (telephonyManager != null) {
157 return telephonyManager.getLine1Number();
158 }
159 return null;
Nancy Chenbbf35962015-12-28 17:17:27 -0800160 }
161
162 /**
163 * Return whether a given phone number is the configured voicemail number for a
164 * particular phone account.
165 *
Nancy Chenaefe94a2015-12-28 16:44:01 -0800166 * @param telecomManager the {@link TelecomManager} to use for checking the number.
Nancy Chenbbf35962015-12-28 17:17:27 -0800167 * @param accountHandle The handle for the account to check the voicemail number against
168 * @param number The number to look up.
Nancy Chenbbf35962015-12-28 17:17:27 -0800169 */
Nancy Chen78384182015-12-22 17:21:13 -0800170 public static boolean isVoiceMailNumber(@Nullable TelecomManager telecomManager,
Nancy Chenbbf35962015-12-28 17:17:27 -0800171 @Nullable PhoneAccountHandle accountHandle, @Nullable String number) {
Nancy Chen78384182015-12-22 17:21:13 -0800172 if (telecomManager != null && (CompatUtils.isMarshmallowCompatible()
173 || CompatUtils.isMethodAvailable(TELECOM_MANAGER_CLASS, "isVoiceMailNumber",
174 PhoneAccountHandle.class, String.class))) {
Nancy Chenbbf35962015-12-28 17:17:27 -0800175 return telecomManager.isVoiceMailNumber(accountHandle, number);
176 }
177 return PhoneNumberUtils.isVoiceMailNumber(number);
178 }
179
180 /**
Nancy Chen78384182015-12-22 17:21:13 -0800181 * Return the {@link PhoneAccount} for a specified {@link PhoneAccountHandle}. Object includes
182 * resources which can be used in a user interface.
183 *
184 * @param telecomManager the {@link TelecomManager} used for method calls, if possible.
Nancy Chenaefe94a2015-12-28 16:44:01 -0800185 * @param account The {@link PhoneAccountHandle}.
186 * @return The {@link PhoneAccount} object or null if it doesn't exist.
Nancy Chen78384182015-12-22 17:21:13 -0800187 */
188 @Nullable
189 public static PhoneAccount getPhoneAccount(@Nullable TelecomManager telecomManager,
190 @Nullable PhoneAccountHandle accountHandle) {
191 if (telecomManager != null && (CompatUtils.isMethodAvailable(
192 TELECOM_MANAGER_CLASS, "getPhoneAccount", PhoneAccountHandle.class))) {
193 return telecomManager.getPhoneAccount(accountHandle);
194 }
195 return null;
196 }
197
198 /**
199 * Return the voicemail number for a given phone account.
200 *
201 * @param telecomManager The {@link TelecomManager} object to use for retrieving the voicemail
202 * number if accountHandle is specified.
203 * @param telephonyManager The {@link TelephonyManager} object to use for retrieving the
204 * voicemail number if accountHandle is null.
205 * @param accountHandle The handle for the phone account.
206 * @return The voicemail number for the phone account, and {@code null} if one has not been
207 * configured.
208 */
209 @Nullable
210 public static String getVoiceMailNumber(@Nullable TelecomManager telecomManager,
211 @Nullable TelephonyManager telephonyManager,
212 @Nullable PhoneAccountHandle accountHandle) {
213 if (telecomManager != null && (CompatUtils.isMethodAvailable(
214 TELECOM_MANAGER_CLASS, "getVoiceMailNumber", PhoneAccountHandle.class))) {
215 return telecomManager.getVoiceMailNumber(accountHandle);
216 } else if (telephonyManager != null){
217 return telephonyManager.getVoiceMailNumber();
218 }
219 return null;
220 }
221
222 /**
223 * Processes the specified dial string as an MMI code.
224 * MMI codes are any sequence of characters entered into the dialpad that contain a "*" or "#".
225 * Some of these sequences launch special behavior through handled by Telephony.
226 *
227 * @param telecomManager The {@link TelecomManager} object to use for handling MMI.
228 * @param dialString The digits to dial.
229 * @return {@code true} if the digits were processed as an MMI code, {@code false} otherwise.
230 */
231 public static boolean handleMmi(@Nullable TelecomManager telecomManager,
232 @Nullable String dialString, @Nullable PhoneAccountHandle accountHandle) {
233 if (telecomManager == null || TextUtils.isEmpty(dialString)) {
234 return false;
235 }
236 if (CompatUtils.isMarshmallowCompatible()) {
237 return telecomManager.handleMmi(dialString, accountHandle);
238 }
239
240 Object handleMmiResult = CompatUtils.invokeMethod(
241 telecomManager,
242 "handleMmi",
243 new Class<?>[] {PhoneAccountHandle.class, String.class},
244 new Object[] {accountHandle, dialString});
245 if (handleMmiResult != null) {
246 return (boolean) handleMmiResult;
247 }
248
249 return telecomManager.handleMmi(dialString);
250 }
251
252 /**
Nancy Chenbbf35962015-12-28 17:17:27 -0800253 * Silences the ringer if a ringing call exists. Noop if {@link TelecomManager#silenceRinger()}
254 * is unavailable.
255 *
Nancy Chenaefe94a2015-12-28 16:44:01 -0800256 * @param telecomManager the TelecomManager to use to silence the ringer.
Nancy Chenbbf35962015-12-28 17:17:27 -0800257 */
Nancy Chen78384182015-12-22 17:21:13 -0800258 public static void silenceRinger(@Nullable TelecomManager telecomManager) {
259 if (telecomManager != null && (CompatUtils.isMarshmallowCompatible() || CompatUtils
260 .isMethodAvailable(TELECOM_MANAGER_CLASS, "silenceRinger"))) {
Nancy Chenbbf35962015-12-28 17:17:27 -0800261 telecomManager.silenceRinger();
262 }
263 }
Nancy Chenaefe94a2015-12-28 16:44:01 -0800264
265 /**
266 * Returns the current SIM call manager. Apps must be prepared for this method to return null,
267 * indicating that there currently exists no registered SIM call manager.
268 *
269 * @param telecomManager the {@link TelecomManager} to use to fetch the SIM call manager.
270 * @return The phone account handle of the current sim call manager.
271 */
272 @Nullable
273 public static PhoneAccountHandle getSimCallManager(TelecomManager telecomManager) {
274 if (telecomManager != null && (CompatUtils.isMarshmallowCompatible() || CompatUtils
275 .isMethodAvailable(TELECOM_MANAGER_CLASS, "getSimCallManager"))) {
276 return telecomManager.getSimCallManager();
277 }
278 return null;
279 }
Nancy Chenbbf35962015-12-28 17:17:27 -0800280}