TelecomManager.java revision 5cf2dde3f7b2003fa832f0657b8b675ce84d77f7
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package android.telecom;
16
17import android.annotation.SystemApi;
18import android.content.ComponentName;
19import android.content.Context;
20import android.content.Intent;
21import android.net.Uri;
22import android.os.Bundle;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25import android.telephony.TelephonyManager;
26import android.text.TextUtils;
27import android.util.Log;
28
29import com.android.internal.telecom.ITelecomService;
30
31import java.util.ArrayList;
32import java.util.Collections;
33import java.util.List;
34
35/**
36 * Provides access to information about active calls and registration/call-management functionality.
37 * Apps can use methods in this class to determine the current call state.
38 * <p>
39 * Apps do not instantiate this class directly; instead, they retrieve a reference to an instance
40 * through {@link Context#getSystemService Context.getSystemService(Context.TELECOM_SERVICE)}.
41 * <p>
42 * Note that access to some telecom information is permission-protected. Your app cannot access the
43 * protected information or gain access to protected functionality unless it has the appropriate
44 * permissions declared in its manifest file. Where permissions apply, they are noted in the method
45 * descriptions.
46 */
47public class TelecomManager {
48
49    /**
50     * Activity action: Starts the UI for handing an incoming call. This intent starts the in-call
51     * UI by notifying the Telecom system that an incoming call exists for a specific call service
52     * (see {@link android.telecom.ConnectionService}). Telecom reads the Intent extras to find
53     * and bind to the appropriate {@link android.telecom.ConnectionService} which Telecom will
54     * ultimately use to control and get information about the call.
55     * <p>
56     * Input: get*Extra field {@link #EXTRA_PHONE_ACCOUNT_HANDLE} contains the component name of the
57     * {@link android.telecom.ConnectionService} that Telecom should bind to. Telecom will then
58     * ask the connection service for more information about the call prior to showing any UI.
59     */
60    public static final String ACTION_INCOMING_CALL = "android.telecom.action.INCOMING_CALL";
61
62    /**
63     * Similar to {@link #ACTION_INCOMING_CALL}, but is used only by Telephony to add a new
64     * sim-initiated MO call for carrier testing.
65     * @hide
66     */
67    public static final String ACTION_NEW_UNKNOWN_CALL = "android.telecom.action.NEW_UNKNOWN_CALL";
68
69    /**
70     * The {@link android.content.Intent} action used to configure a
71     * {@link android.telecom.ConnectionService}.
72     */
73    public static final String ACTION_CONNECTION_SERVICE_CONFIGURE =
74            "android.telecom.action.CONNECTION_SERVICE_CONFIGURE";
75
76    /**
77     * The {@link android.content.Intent} action used to show the call accessibility settings page.
78     */
79    public static final String ACTION_SHOW_CALL_ACCESSIBILITY_SETTINGS =
80            "android.telecom.action.SHOW_CALL_ACCESSIBILITY_SETTINGS";
81
82    /**
83     * The {@link android.content.Intent} action used to show the call settings page.
84     */
85    public static final String ACTION_SHOW_CALL_SETTINGS =
86            "android.telecom.action.SHOW_CALL_SETTINGS";
87
88    /**
89     * The {@link android.content.Intent} action used to show the respond via SMS settings page.
90     */
91    public static final String ACTION_SHOW_RESPOND_VIA_SMS_SETTINGS =
92            "android.telecom.action.SHOW_RESPOND_VIA_SMS_SETTINGS";
93
94    /**
95     * The {@link android.content.Intent} action used to show the settings page used to configure
96     * {@link PhoneAccount} preferences.
97     */
98    public static final String ACTION_CHANGE_PHONE_ACCOUNTS =
99            "android.telecom.action.CHANGE_PHONE_ACCOUNTS";
100
101    /**
102     * The {@link android.content.Intent} action used indicate that a new phone account was
103     * just registered.
104     * @hide
105     */
106    @SystemApi
107    public static final String ACTION_PHONE_ACCOUNT_REGISTERED =
108            "android.telecom.action.PHONE_ACCOUNT_REGISTERED";
109
110    /**
111     * Activity action: Shows a dialog asking the user whether or not they want to replace the
112     * current default Dialer with the one specified in
113     * {@link #EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME}.
114     *
115     * Usage example:
116     * <pre>
117     * Intent intent = new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER);
118     * intent.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,
119     *         getActivity().getPackageName());
120     * startActivity(intent);
121     * </pre>
122     */
123    public static final String ACTION_CHANGE_DEFAULT_DIALER =
124            "android.telecom.action.CHANGE_DEFAULT_DIALER";
125
126    /**
127     * Privileged version of {@link #ACTION_CHANGE_DEFAULT_DIALER} that doesn't require
128     * confirmation from the user via the dialog.
129     *
130     * Requires permission: {@link android.Manifest.permission#MODIFY_PHONE_STATE}
131     * @hide
132     */
133    public static final String ACTION_CHANGE_DEFAULT_DIALER_PRIVILEGED =
134            "android.telecom.action.CHANGE_DEFAULT_DIALER_PRIVILEGED";
135
136    /**
137     * Activity action: Opens the settings screen where a user can enable and disable which
138     * {@link PhoneAccount}s are allows to make and receive calls. Because a user must
139     * explicitly enable an account before the system will use it, an app may want to send the
140     * user to this setting after registering a {@link PhoneAccount}.
141     * <p>
142     * Input: get*Extra field {@link #EXTRA_PHONE_ACCOUNT_DESCRIPTION} contains a string-based
143     * reference to the {@link PhoneAccountHandle} you want to enable. get*Extra field
144     * {@link #EXTRA_ENABLE_PHONE_ACCOUNT_VALUE} contains a boolean value indicated whether
145     * the account should be enabled or disabled.
146     * <p>
147     * Requires permission: {@link android.Manifest.permission#MODIFY_PHONE_STATE}
148     * @hide
149     */
150    public static final String ACTION_ENABLE_PHONE_ACCOUNT_SETTING =
151            "android.telecom.action.ENABLE_PHONE_ACCOUNT_SETTING";
152
153    /**
154     * Extra value used to provide the package name for {@link #ACTION_CHANGE_DEFAULT_DIALER}.
155     */
156    public static final String EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME =
157            "android.telecom.extra.CHANGE_DEFAULT_DIALER_PACKAGE_NAME";
158
159    /**
160     * Optional extra for {@link android.content.Intent#ACTION_CALL} containing a boolean that
161     * determines whether the speakerphone should be automatically turned on for an outgoing call.
162     */
163    public static final String EXTRA_START_CALL_WITH_SPEAKERPHONE =
164            "android.telecom.extra.START_CALL_WITH_SPEAKERPHONE";
165
166    /**
167     * Optional extra for {@link android.content.Intent#ACTION_CALL} containing an integer that
168     * determines the desired video state for an outgoing call.
169     * Valid options:
170     * {@link VideoProfile#STATE_AUDIO_ONLY},
171     * {@link VideoProfile#STATE_BIDIRECTIONAL},
172     * {@link VideoProfile#STATE_RX_ENABLED},
173     * {@link VideoProfile#STATE_TX_ENABLED}.
174     */
175    public static final String EXTRA_START_CALL_WITH_VIDEO_STATE =
176            "android.telecom.extra.START_CALL_WITH_VIDEO_STATE";
177
178    /**
179     * The extra used with an {@link android.content.Intent#ACTION_CALL} and
180     * {@link android.content.Intent#ACTION_DIAL} {@code Intent} to specify a
181     * {@link PhoneAccountHandle} to use when making the call.
182     * <p class="note">
183     * Retrieve with {@link android.content.Intent#getParcelableExtra(String)}.
184     */
185    public static final String EXTRA_PHONE_ACCOUNT_HANDLE =
186            "android.telecom.extra.PHONE_ACCOUNT_HANDLE";
187
188    /**
189     * The extra used with {@link #ACTION_ENABLE_PHONE_ACCOUNT_SETTING} to specify a phone account
190     * as a string value. The value is of the form: "A;B" where A is the component name of the
191     * {@link PhoneAccount} (e.g.,
192     * com.android.phone/com.android.services.telephony.TelephonyConnectionService) and B is the
193     * {@link PhoneAccount} ID.
194     * @hide
195     */
196    public static final String EXTRA_PHONE_ACCOUNT_DESCRIPTION =
197            "android.telecom.extra.PHONE_ACCOUNT_DESCRIPTION";
198
199    /**
200     * Boolean extra used to specify a value for enabling and disabling a phone account.
201     * Used with {@link #ACTION_ENABLE_PHONE_ACCOUNT_SETTING}.
202     * @hide
203     */
204    public static final String EXTRA_ENABLE_PHONE_ACCOUNT_VALUE =
205            "android.telecom.extra.ENABLE_PHONE_ACCOUNT_VALUE";
206
207    /**
208     * Optional extra for {@link #ACTION_INCOMING_CALL} containing a {@link Bundle} which contains
209     * metadata about the call. This {@link Bundle} will be returned to the
210     * {@link ConnectionService}.
211     */
212    public static final String EXTRA_INCOMING_CALL_EXTRAS =
213            "android.telecom.extra.INCOMING_CALL_EXTRAS";
214
215    /**
216     * Optional extra for {@link android.content.Intent#ACTION_CALL} and
217     * {@link android.content.Intent#ACTION_DIAL} {@code Intent} containing a {@link Bundle}
218     * which contains metadata about the call. This {@link Bundle} will be saved into
219     * {@code Call.Details}.
220     */
221    public static final String EXTRA_OUTGOING_CALL_EXTRAS =
222            "android.telecom.extra.OUTGOING_CALL_EXTRAS";
223
224    /**
225     * @hide
226     */
227    public static final String EXTRA_UNKNOWN_CALL_HANDLE =
228            "android.telecom.extra.UNKNOWN_CALL_HANDLE";
229
230    /**
231     * Optional extra for {@link android.telephony.TelephonyManager#ACTION_PHONE_STATE_CHANGED}
232     * containing the disconnect code.
233     */
234    public static final String EXTRA_CALL_DISCONNECT_CAUSE =
235            "android.telecom.extra.CALL_DISCONNECT_CAUSE";
236
237    /**
238     * Optional extra for {@link android.telephony.TelephonyManager#ACTION_PHONE_STATE_CHANGED}
239     * containing the disconnect message.
240     */
241    public static final String EXTRA_CALL_DISCONNECT_MESSAGE =
242            "android.telecom.extra.CALL_DISCONNECT_MESSAGE";
243
244    /**
245     * Optional extra for {@link android.telephony.TelephonyManager#ACTION_PHONE_STATE_CHANGED}
246     * containing the component name of the associated connection service.
247     * @hide
248     */
249    @SystemApi
250    public static final String EXTRA_CONNECTION_SERVICE =
251            "android.telecom.extra.CONNECTION_SERVICE";
252
253    /**
254     * An optional {@link android.content.Intent#ACTION_CALL} intent extra denoting the
255     * package name of the app specifying an alternative gateway for the call.
256     * The value is a string.
257     *
258     * (The following comment corresponds to the all GATEWAY_* extras)
259     * An app which sends the {@link android.content.Intent#ACTION_CALL} intent can specify an
260     * alternative address to dial which is different from the one specified and displayed to
261     * the user. This alternative address is referred to as the gateway address.
262     */
263    public static final String GATEWAY_PROVIDER_PACKAGE =
264            "android.telecom.extra.GATEWAY_PROVIDER_PACKAGE";
265
266    /**
267     * An optional {@link android.content.Intent#ACTION_CALL} intent extra corresponding to the
268     * original address to dial for the call. This is used when an alternative gateway address is
269     * provided to recall the original address.
270     * The value is a {@link android.net.Uri}.
271     *
272     * (See {@link #GATEWAY_PROVIDER_PACKAGE} for details)
273     */
274    public static final String GATEWAY_ORIGINAL_ADDRESS =
275            "android.telecom.extra.GATEWAY_ORIGINAL_ADDRESS";
276
277    /**
278     * The number which the party on the other side of the line will see (and use to return the
279     * call).
280     * <p>
281     * {@link ConnectionService}s which interact with {@link RemoteConnection}s should only populate
282     * this if the {@link android.telephony.TelephonyManager#getLine1Number()} value, as that is the
283     * user's expected caller ID.
284     */
285    public static final String EXTRA_CALL_BACK_NUMBER = "android.telecom.extra.CALL_BACK_NUMBER";
286
287    /**
288     * The dual tone multi-frequency signaling character sent to indicate the dialing system should
289     * pause for a predefined period.
290     */
291    public static final char DTMF_CHARACTER_PAUSE = ',';
292
293    /**
294     * The dual-tone multi-frequency signaling character sent to indicate the dialing system should
295     * wait for user confirmation before proceeding.
296     */
297    public static final char DTMF_CHARACTER_WAIT = ';';
298
299    /**
300     * TTY (teletypewriter) mode is off.
301     *
302     * @hide
303     */
304    public static final int TTY_MODE_OFF = 0;
305
306    /**
307     * TTY (teletypewriter) mode is on. The speaker is off and the microphone is muted. The user
308     * will communicate with the remote party by sending and receiving text messages.
309     *
310     * @hide
311     */
312    public static final int TTY_MODE_FULL = 1;
313
314    /**
315     * TTY (teletypewriter) mode is in hearing carryover mode (HCO). The microphone is muted but the
316     * speaker is on. The user will communicate with the remote party by sending text messages and
317     * hearing an audible reply.
318     *
319     * @hide
320     */
321    public static final int TTY_MODE_HCO = 2;
322
323    /**
324     * TTY (teletypewriter) mode is in voice carryover mode (VCO). The speaker is off but the
325     * microphone is still on. User will communicate with the remote party by speaking and receiving
326     * text message replies.
327     *
328     * @hide
329     */
330    public static final int TTY_MODE_VCO = 3;
331
332    /**
333     * Broadcast intent action indicating that the current TTY mode has changed. An intent extra
334     * provides this state as an int.
335     *
336     * @see #EXTRA_CURRENT_TTY_MODE
337     * @hide
338     */
339    public static final String ACTION_CURRENT_TTY_MODE_CHANGED =
340            "android.telecom.action.CURRENT_TTY_MODE_CHANGED";
341
342    /**
343     * The lookup key for an int that indicates the current TTY mode.
344     * Valid modes are:
345     * - {@link #TTY_MODE_OFF}
346     * - {@link #TTY_MODE_FULL}
347     * - {@link #TTY_MODE_HCO}
348     * - {@link #TTY_MODE_VCO}
349     *
350     * @hide
351     */
352    public static final String EXTRA_CURRENT_TTY_MODE =
353            "android.telecom.intent.extra.CURRENT_TTY_MODE";
354
355    /**
356     * Broadcast intent action indicating that the TTY preferred operating mode has changed. An
357     * intent extra provides the new mode as an int.
358     *
359     * @see #EXTRA_TTY_PREFERRED_MODE
360     * @hide
361     */
362    public static final String ACTION_TTY_PREFERRED_MODE_CHANGED =
363            "android.telecom.action.TTY_PREFERRED_MODE_CHANGED";
364
365    /**
366     * The lookup key for an int that indicates preferred TTY mode. Valid modes are: -
367     * {@link #TTY_MODE_OFF} - {@link #TTY_MODE_FULL} - {@link #TTY_MODE_HCO} -
368     * {@link #TTY_MODE_VCO}
369     *
370     * @hide
371     */
372    public static final String EXTRA_TTY_PREFERRED_MODE =
373            "android.telecom.intent.extra.TTY_PREFERRED";
374
375    /**
376     * The following 4 constants define how properties such as phone numbers and names are
377     * displayed to the user.
378     */
379
380    /**
381     * Indicates that the address or number of a call is allowed to be displayed for caller ID.
382    */
383    public static final int PRESENTATION_ALLOWED = 1;
384
385    /**
386     * Indicates that the address or number of a call is blocked by the other party.
387     */
388    public static final int PRESENTATION_RESTRICTED = 2;
389
390    /**
391     * Indicates that the address or number of a call is not specified or known by the carrier.
392     */
393    public static final int PRESENTATION_UNKNOWN = 3;
394
395    /**
396     * Indicates that the address or number of a call belongs to a pay phone.
397     */
398    public static final int PRESENTATION_PAYPHONE = 4;
399
400    private static final String TAG = "TelecomManager";
401
402    private final Context mContext;
403
404    /**
405     * @hide
406     */
407    public static TelecomManager from(Context context) {
408        return (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
409    }
410
411    /**
412     * @hide
413     */
414    public TelecomManager(Context context) {
415        Context appContext = context.getApplicationContext();
416        if (appContext != null) {
417            mContext = appContext;
418        } else {
419            mContext = context;
420        }
421    }
422
423    /**
424     * Return the {@link PhoneAccount} which will be used to place outgoing calls to addresses with
425     * the specified {@code uriScheme}. This {@link PhoneAccount} will always be a member of the
426     * list which is returned from invoking {@link #getCallCapablePhoneAccounts()}. The specific
427     * account returned depends on the following priorities:
428     * <ul>
429     * <li> If the user-selected default {@link PhoneAccount} supports the specified scheme, it will
430     * be returned.
431     * </li>
432     * <li> If there exists only one {@link PhoneAccount} that supports the specified scheme, it
433     * will be returned.
434     * </li>
435     * </ul>
436     * <p>
437     * If no {@link PhoneAccount} fits the criteria above, this method will return {@code null}.
438     *
439     * @param uriScheme The URI scheme.
440     * @return The {@link PhoneAccountHandle} corresponding to the account to be used.
441     */
442    public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) {
443        try {
444            if (isServiceConnected()) {
445                return getTelecomService().getDefaultOutgoingPhoneAccount(uriScheme,
446                        mContext.getOpPackageName());
447            }
448        } catch (RemoteException e) {
449            Log.e(TAG, "Error calling ITelecomService#getDefaultOutgoingPhoneAccount", e);
450        }
451        return null;
452    }
453
454    /**
455     * Return the {@link PhoneAccount} which is the user-chosen default for making outgoing phone
456     * calls. This {@code PhoneAccount} will always be a member of the list which is returned from
457     * calling {@link #getCallCapablePhoneAccounts()}
458     * <p>
459     * Apps must be prepared for this method to return {@code null}, indicating that there currently
460     * exists no user-chosen default {@code PhoneAccount}.
461     *
462     * @return The user outgoing phone account selected by the user.
463     * @hide
464     */
465    public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
466        try {
467            if (isServiceConnected()) {
468                return getTelecomService().getUserSelectedOutgoingPhoneAccount();
469            }
470        } catch (RemoteException e) {
471            Log.e(TAG, "Error calling ITelecomService#getUserSelectedOutgoingPhoneAccount", e);
472        }
473        return null;
474    }
475
476    /**
477     * Sets the user-chosen default for making outgoing phone calls.
478     * @hide
479     */
480    public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
481        try {
482            if (isServiceConnected()) {
483                getTelecomService().setUserSelectedOutgoingPhoneAccount(accountHandle);
484            }
485        } catch (RemoteException e) {
486            Log.e(TAG, "Error calling ITelecomService#setUserSelectedOutgoingPhoneAccount");
487        }
488    }
489
490    /**
491     * Returns the current SIM call manager. Apps must be prepared for this method to return
492     * {@code null}, indicating that there currently exists no user-chosen default
493     * {@code PhoneAccount}.
494     *
495     * @return The phone account handle of the current sim call manager.
496     */
497    public PhoneAccountHandle getSimCallManager() {
498        try {
499            if (isServiceConnected()) {
500                return getTelecomService().getSimCallManager();
501            }
502        } catch (RemoteException e) {
503            Log.e(TAG, "Error calling ITelecomService#getSimCallManager");
504        }
505        return null;
506    }
507
508    /**
509     * Sets the SIM call manager to the specified phone account.
510     *
511     * @param accountHandle The phone account handle of the account to set as the sim call manager.
512     * @hide
513     */
514    public void setSimCallManager(PhoneAccountHandle accountHandle) {
515        try {
516            if (isServiceConnected()) {
517                getTelecomService().setSimCallManager(accountHandle);
518            }
519        } catch (RemoteException e) {
520            Log.e(TAG, "Error calling ITelecomService#setSimCallManager");
521        }
522    }
523
524    /**
525     * Returns the list of registered SIM call managers.
526     *
527     * @return List of registered SIM call managers.
528     * @hide
529     */
530    public List<PhoneAccountHandle> getSimCallManagers() {
531        try {
532            if (isServiceConnected()) {
533                return getTelecomService().getSimCallManagers(mContext.getOpPackageName());
534            }
535        } catch (RemoteException e) {
536            Log.e(TAG, "Error calling ITelecomService#getSimCallManagers");
537        }
538        return new ArrayList<>();
539    }
540
541    /**
542     * Returns the current connection manager. Apps must be prepared for this method to return
543     * {@code null}, indicating that there currently exists no user-chosen default
544     * {@code PhoneAccount}.
545     *
546     * @return The phone account handle of the current connection manager.
547     * @hide
548     */
549    @SystemApi
550    public PhoneAccountHandle getConnectionManager() {
551        return getSimCallManager();
552    }
553
554    /**
555     * Returns a list of the {@link PhoneAccountHandle}s which can be used to make and receive phone
556     * calls which support the specified URI scheme.
557     * <P>
558     * For example, invoking with {@code "tel"} will find all {@link PhoneAccountHandle}s which
559     * support telephone calls (e.g. URIs such as {@code tel:555-555-1212}).  Invoking with
560     * {@code "sip"} will find all {@link PhoneAccountHandle}s which support SIP calls (e.g. URIs
561     * such as {@code sip:example@sipexample.com}).
562     *
563     * @param uriScheme The URI scheme.
564     * @return A list of {@code PhoneAccountHandle} objects supporting the URI scheme.
565     * @hide
566     */
567    @SystemApi
568    public List<PhoneAccountHandle> getPhoneAccountsSupportingScheme(String uriScheme) {
569        try {
570            if (isServiceConnected()) {
571                return getTelecomService().getPhoneAccountsSupportingScheme(uriScheme,
572                        mContext.getOpPackageName());
573            }
574        } catch (RemoteException e) {
575            Log.e(TAG, "Error calling ITelecomService#getPhoneAccountsSupportingScheme", e);
576        }
577        return new ArrayList<>();
578    }
579
580
581    /**
582     * Returns a list of {@link PhoneAccountHandle}s which can be used to make and receive phone
583     * calls. The returned list includes only those accounts which have been explicitly enabled
584     * by the user.
585     *
586     * @see #EXTRA_PHONE_ACCOUNT_HANDLE
587     * @return A list of {@code PhoneAccountHandle} objects.
588     */
589    public List<PhoneAccountHandle> getCallCapablePhoneAccounts() {
590        return getCallCapablePhoneAccounts(false);
591    }
592
593    /**
594     * Returns a list of {@link PhoneAccountHandle}s including those which have not been enabled
595     * by the user.
596     *
597     * @return A list of {@code PhoneAccountHandle} objects.
598     * @hide
599     */
600    public List<PhoneAccountHandle> getCallCapablePhoneAccounts(boolean includeDisabledAccounts) {
601        try {
602            if (isServiceConnected()) {
603                return getTelecomService().getCallCapablePhoneAccounts(
604                        includeDisabledAccounts, mContext.getOpPackageName());
605            }
606        } catch (RemoteException e) {
607            Log.e(TAG, "Error calling ITelecomService#getCallCapablePhoneAccounts(" +
608                    includeDisabledAccounts + ")", e);
609        }
610        return new ArrayList<>();
611    }
612
613    /**
614     *  Returns a list of all {@link PhoneAccount}s registered for the calling package.
615     *
616     * @return A list of {@code PhoneAccountHandle} objects.
617     * @hide
618     */
619    @SystemApi
620    public List<PhoneAccountHandle> getPhoneAccountsForPackage() {
621        try {
622            if (isServiceConnected()) {
623                return getTelecomService().getPhoneAccountsForPackage(mContext.getPackageName());
624            }
625        } catch (RemoteException e) {
626            Log.e(TAG, "Error calling ITelecomService#getPhoneAccountsForPackage", e);
627        }
628        return null;
629    }
630
631    /**
632     * Return the {@link PhoneAccount} for a specified {@link PhoneAccountHandle}. Object includes
633     * resources which can be used in a user interface.
634     *
635     * @param account The {@link PhoneAccountHandle}.
636     * @return The {@link PhoneAccount} object.
637     */
638    public PhoneAccount getPhoneAccount(PhoneAccountHandle account) {
639        try {
640            if (isServiceConnected()) {
641                return getTelecomService().getPhoneAccount(account);
642            }
643        } catch (RemoteException e) {
644            Log.e(TAG, "Error calling ITelecomService#getPhoneAccount", e);
645        }
646        return null;
647    }
648
649    /**
650     * Returns a count of all {@link PhoneAccount}s.
651     *
652     * @return The count of {@link PhoneAccount}s.
653     * @hide
654     */
655    @SystemApi
656    public int getAllPhoneAccountsCount() {
657        try {
658            if (isServiceConnected()) {
659                return getTelecomService().getAllPhoneAccountsCount();
660            }
661        } catch (RemoteException e) {
662            Log.e(TAG, "Error calling ITelecomService#getAllPhoneAccountsCount", e);
663        }
664        return 0;
665    }
666
667    /**
668     * Returns a list of all {@link PhoneAccount}s.
669     *
670     * @return All {@link PhoneAccount}s.
671     * @hide
672     */
673    @SystemApi
674    public List<PhoneAccount> getAllPhoneAccounts() {
675        try {
676            if (isServiceConnected()) {
677                return getTelecomService().getAllPhoneAccounts();
678            }
679        } catch (RemoteException e) {
680            Log.e(TAG, "Error calling ITelecomService#getAllPhoneAccounts", e);
681        }
682        return Collections.EMPTY_LIST;
683    }
684
685    /**
686     * Returns a list of all {@link PhoneAccountHandle}s.
687     *
688     * @return All {@link PhoneAccountHandle}s.
689     * @hide
690     */
691    @SystemApi
692    public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
693        try {
694            if (isServiceConnected()) {
695                return getTelecomService().getAllPhoneAccountHandles();
696            }
697        } catch (RemoteException e) {
698            Log.e(TAG, "Error calling ITelecomService#getAllPhoneAccountHandles", e);
699        }
700        return Collections.EMPTY_LIST;
701    }
702
703    /**
704     * Register a {@link PhoneAccount} for use by the system. When registering
705     * {@link PhoneAccount}s, existing registrations will be overwritten if the
706     * {@link PhoneAccountHandle} matches that of a {@link PhoneAccount} which is already
707     * registered. Once registered, the {@link PhoneAccount} is listed to the user as an option
708     * when placing calls. The user may still need to enable the {@link PhoneAccount} within
709     * the phone app settings before the account is usable.
710     * <p>
711     * A {@link SecurityException} will be thrown if an app tries to register a
712     * {@link PhoneAccountHandle} where the package name specified within
713     * {@link PhoneAccountHandle#getComponentName()} does not match the package name of the app.
714     *
715     * @param account The complete {@link PhoneAccount}.
716     */
717    public void registerPhoneAccount(PhoneAccount account) {
718        try {
719            if (isServiceConnected()) {
720                getTelecomService().registerPhoneAccount(account);
721            }
722        } catch (RemoteException e) {
723            Log.e(TAG, "Error calling ITelecomService#registerPhoneAccount", e);
724        }
725    }
726
727    /**
728     * Remove a {@link PhoneAccount} registration from the system.
729     *
730     * @param accountHandle A {@link PhoneAccountHandle} for the {@link PhoneAccount} to unregister.
731     */
732    public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
733        try {
734            if (isServiceConnected()) {
735                getTelecomService().unregisterPhoneAccount(accountHandle);
736            }
737        } catch (RemoteException e) {
738            Log.e(TAG, "Error calling ITelecomService#unregisterPhoneAccount", e);
739        }
740    }
741
742    /**
743     * Remove all Accounts that belong to the calling package from the system.
744     * @hide
745     */
746    @SystemApi
747    public void clearPhoneAccounts() {
748        clearAccounts();
749    }
750    /**
751     * Remove all Accounts that belong to the calling package from the system.
752     * @deprecated Use {@link #clearPhoneAccounts()} instead.
753     * @hide
754     */
755    @SystemApi
756    public void clearAccounts() {
757        try {
758            if (isServiceConnected()) {
759                getTelecomService().clearAccounts(mContext.getPackageName());
760            }
761        } catch (RemoteException e) {
762            Log.e(TAG, "Error calling ITelecomService#clearAccounts", e);
763        }
764    }
765
766    /**
767     * Remove all Accounts that belong to the specified package from the system.
768     * @hide
769     */
770    public void clearAccountsForPackage(String packageName) {
771        try {
772            if (isServiceConnected() && !TextUtils.isEmpty(packageName)) {
773                getTelecomService().clearAccounts(packageName);
774            }
775        } catch (RemoteException e) {
776            Log.e(TAG, "Error calling ITelecomService#clearAccountsForPackage", e);
777        }
778    }
779
780
781    /**
782     * @deprecated - Use {@link TelecomManager#getDefaultDialerPackage} to directly access
783     *         the default dialer's package name instead.
784     * @hide
785     */
786    @SystemApi
787    public ComponentName getDefaultPhoneApp() {
788        try {
789            if (isServiceConnected()) {
790                return getTelecomService().getDefaultPhoneApp();
791            }
792        } catch (RemoteException e) {
793            Log.e(TAG, "RemoteException attempting to get the default phone app.", e);
794        }
795        return null;
796    }
797
798    /**
799     * Used to determine the currently selected default dialer package.
800     *
801     * @return package name for the default dialer package or null if no package has been
802     *         selected as the default dialer.
803     */
804    public String getDefaultDialerPackage() {
805        try {
806            if (isServiceConnected()) {
807                return getTelecomService().getDefaultDialerPackage();
808            }
809        } catch (RemoteException e) {
810            Log.e(TAG, "RemoteException attempting to get the default dialer package name.", e);
811        }
812        return null;
813    }
814
815    /**
816     * Used to determine the dialer package that is preloaded on the system partition.
817     *
818     * @return package name for the system dialer package or null if no system dialer is preloaded.
819     * @hide
820     */
821    public String getSystemDialerPackage() {
822        try {
823            if (isServiceConnected()) {
824                return getTelecomService().getSystemDialerPackage();
825            }
826        } catch (RemoteException e) {
827            Log.e(TAG, "RemoteException attempting to get the system dialer package name.", e);
828        }
829        return null;
830    }
831
832    /**
833     * Return whether a given phone number is the configured voicemail number for a
834     * particular phone account.
835     *
836     * @param accountHandle The handle for the account to check the voicemail number against
837     * @param number The number to look up.
838     */
839    public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
840        try {
841            if (isServiceConnected()) {
842                return getTelecomService().isVoiceMailNumber(accountHandle, number,
843                        mContext.getOpPackageName());
844            }
845        } catch (RemoteException e) {
846            Log.e(TAG, "RemoteException calling ITelecomService#isVoiceMailNumber.", e);
847        }
848        return false;
849    }
850
851    /**
852     * Return the voicemail number for a given phone account.
853     *
854     * @param accountHandle The handle for the phone account.
855     * @return The voicemail number for the phone account, and {@code null} if one has not been
856     *         configured.
857     */
858    public String getVoiceMailNumber(PhoneAccountHandle accountHandle) {
859        try {
860            if (isServiceConnected()) {
861                return getTelecomService().getVoiceMailNumber(accountHandle,
862                        mContext.getOpPackageName());
863            }
864        } catch (RemoteException e) {
865            Log.e(TAG, "RemoteException calling ITelecomService#hasVoiceMailNumber.", e);
866        }
867        return null;
868    }
869
870    /**
871     * Return the line 1 phone number for given phone account.
872     *
873     * @param accountHandle The handle for the account retrieve a number for.
874     * @return A string representation of the line 1 phone number.
875     */
876    public String getLine1Number(PhoneAccountHandle accountHandle) {
877        try {
878            if (isServiceConnected()) {
879                return getTelecomService().getLine1Number(accountHandle,
880                        mContext.getOpPackageName());
881            }
882        } catch (RemoteException e) {
883            Log.e(TAG, "RemoteException calling ITelecomService#getLine1Number.", e);
884        }
885        return null;
886    }
887
888    /**
889     * Returns whether there is an ongoing phone call (can be in dialing, ringing, active or holding
890     * states).
891     * <p>
892     * Requires permission: {@link android.Manifest.permission#READ_PHONE_STATE}
893     * </p>
894     */
895    public boolean isInCall() {
896        try {
897            if (isServiceConnected()) {
898                return getTelecomService().isInCall(mContext.getOpPackageName());
899            }
900        } catch (RemoteException e) {
901            Log.e(TAG, "RemoteException calling isInCall().", e);
902        }
903        return false;
904    }
905
906    /**
907     * Returns one of the following constants that represents the current state of Telecom:
908     *
909     * {@link TelephonyManager#CALL_STATE_RINGING}
910     * {@link TelephonyManager#CALL_STATE_OFFHOOK}
911     * {@link TelephonyManager#CALL_STATE_IDLE}
912     *
913     * Note that this API does not require the
914     * {@link android.Manifest.permission#READ_PHONE_STATE} permission. This is intentional, to
915     * preserve the behavior of {@link TelephonyManager#getCallState()}, which also did not require
916     * the permission.
917     * @hide
918     */
919    @SystemApi
920    public int getCallState() {
921        try {
922            if (isServiceConnected()) {
923                return getTelecomService().getCallState();
924            }
925        } catch (RemoteException e) {
926            Log.d(TAG, "RemoteException calling getCallState().", e);
927        }
928        return TelephonyManager.CALL_STATE_IDLE;
929    }
930
931    /**
932     * Returns whether there currently exists is a ringing incoming-call.
933     *
934     * @hide
935     */
936    @SystemApi
937    public boolean isRinging() {
938        try {
939            if (isServiceConnected()) {
940                return getTelecomService().isRinging(mContext.getOpPackageName());
941            }
942        } catch (RemoteException e) {
943            Log.e(TAG, "RemoteException attempting to get ringing state of phone app.", e);
944        }
945        return false;
946    }
947
948    /**
949     * Ends an ongoing call.
950     * TODO: L-release - need to convert all invocations of ITelecomService#endCall to use this
951     * method (clockwork & gearhead).
952     * @hide
953     */
954    @SystemApi
955    public boolean endCall() {
956        try {
957            if (isServiceConnected()) {
958                return getTelecomService().endCall();
959            }
960        } catch (RemoteException e) {
961            Log.e(TAG, "Error calling ITelecomService#endCall", e);
962        }
963        return false;
964    }
965
966    /**
967     * If there is a ringing incoming call, this method accepts the call on behalf of the user.
968     * TODO: L-release - need to convert all invocation of ITelecmmService#answerRingingCall to use
969     * this method (clockwork & gearhead).
970     *
971     * @hide
972     */
973    @SystemApi
974    public void acceptRingingCall() {
975        try {
976            if (isServiceConnected()) {
977                getTelecomService().acceptRingingCall();
978            }
979        } catch (RemoteException e) {
980            Log.e(TAG, "Error calling ITelecomService#acceptRingingCall", e);
981        }
982    }
983
984    /**
985     * Silences the ringer if a ringing call exists.
986     */
987    public void silenceRinger() {
988        try {
989            if (isServiceConnected()) {
990                getTelecomService().silenceRinger(mContext.getOpPackageName());
991            }
992        } catch (RemoteException e) {
993            Log.e(TAG, "Error calling ITelecomService#silenceRinger", e);
994        }
995    }
996
997    /**
998     * Returns whether TTY is supported on this device.
999     *
1000     * @hide
1001     */
1002    @SystemApi
1003    public boolean isTtySupported() {
1004        try {
1005            if (isServiceConnected()) {
1006                return getTelecomService().isTtySupported(mContext.getOpPackageName());
1007            }
1008        } catch (RemoteException e) {
1009            Log.e(TAG, "RemoteException attempting to get TTY supported state.", e);
1010        }
1011        return false;
1012    }
1013
1014    /**
1015     * Returns the current TTY mode of the device. For TTY to be on the user must enable it in
1016     * settings and have a wired headset plugged in.
1017     * Valid modes are:
1018     * - {@link TelecomManager#TTY_MODE_OFF}
1019     * - {@link TelecomManager#TTY_MODE_FULL}
1020     * - {@link TelecomManager#TTY_MODE_HCO}
1021     * - {@link TelecomManager#TTY_MODE_VCO}
1022     * @hide
1023     */
1024    public int getCurrentTtyMode() {
1025        try {
1026            if (isServiceConnected()) {
1027                return getTelecomService().getCurrentTtyMode(mContext.getOpPackageName());
1028            }
1029        } catch (RemoteException e) {
1030            Log.e(TAG, "RemoteException attempting to get the current TTY mode.", e);
1031        }
1032        return TTY_MODE_OFF;
1033    }
1034
1035    /**
1036     * Registers a new incoming call. A {@link ConnectionService} should invoke this method when it
1037     * has an incoming call. The specified {@link PhoneAccountHandle} must have been registered
1038     * with {@link #registerPhoneAccount}. Once invoked, this method will cause the system to bind
1039     * to the {@link ConnectionService} associated with the {@link PhoneAccountHandle} and request
1040     * additional information about the call (See
1041     * {@link ConnectionService#onCreateIncomingConnection}) before starting the incoming call UI.
1042     *
1043     * @param phoneAccount A {@link PhoneAccountHandle} registered with
1044     *            {@link #registerPhoneAccount}.
1045     * @param extras A bundle that will be passed through to
1046     *            {@link ConnectionService#onCreateIncomingConnection}.
1047     */
1048    public void addNewIncomingCall(PhoneAccountHandle phoneAccount, Bundle extras) {
1049        try {
1050            if (isServiceConnected()) {
1051                getTelecomService().addNewIncomingCall(
1052                        phoneAccount, extras == null ? new Bundle() : extras);
1053            }
1054        } catch (RemoteException e) {
1055            Log.e(TAG, "RemoteException adding a new incoming call: " + phoneAccount, e);
1056        }
1057    }
1058
1059    /**
1060     * Registers a new unknown call with Telecom. This can only be called by the system Telephony
1061     * service. This is invoked when Telephony detects a new unknown connection that was neither
1062     * a new incoming call, nor an user-initiated outgoing call.
1063     *
1064     * @param phoneAccount A {@link PhoneAccountHandle} registered with
1065     *            {@link #registerPhoneAccount}.
1066     * @param extras A bundle that will be passed through to
1067     *            {@link ConnectionService#onCreateIncomingConnection}.
1068     * @hide
1069     */
1070    @SystemApi
1071    public void addNewUnknownCall(PhoneAccountHandle phoneAccount, Bundle extras) {
1072        try {
1073            if (isServiceConnected()) {
1074                getTelecomService().addNewUnknownCall(
1075                        phoneAccount, extras == null ? new Bundle() : extras);
1076            }
1077        } catch (RemoteException e) {
1078            Log.e(TAG, "RemoteException adding a new unknown call: " + phoneAccount, e);
1079        }
1080    }
1081
1082    /**
1083     * Processes the specified dial string as an MMI code.
1084     * MMI codes are any sequence of characters entered into the dialpad that contain a "*" or "#".
1085     * Some of these sequences launch special behavior through handled by Telephony.
1086     * This method uses the default subscription.
1087     * <p>
1088     * Requires that the method-caller be set as the system dialer app.
1089     * </p>
1090     *
1091     * @param dialString The digits to dial.
1092     * @return True if the digits were processed as an MMI code, false otherwise.
1093     */
1094    public boolean handleMmi(String dialString) {
1095        ITelecomService service = getTelecomService();
1096        if (service != null) {
1097            try {
1098                return service.handlePinMmi(dialString, mContext.getOpPackageName());
1099            } catch (RemoteException e) {
1100                Log.e(TAG, "Error calling ITelecomService#handlePinMmi", e);
1101            }
1102        }
1103        return false;
1104    }
1105
1106    /**
1107     * Processes the specified dial string as an MMI code.
1108     * MMI codes are any sequence of characters entered into the dialpad that contain a "*" or "#".
1109     * Some of these sequences launch special behavior through handled by Telephony.
1110     * <p>
1111     * Requires that the method-caller be set as the system dialer app.
1112     * </p>
1113     *
1114     * @param accountHandle The handle for the account the MMI code should apply to.
1115     * @param dialString The digits to dial.
1116     * @return True if the digits were processed as an MMI code, false otherwise.
1117     */
1118    public boolean handleMmi(String dialString, PhoneAccountHandle accountHandle) {
1119        ITelecomService service = getTelecomService();
1120        if (service != null) {
1121            try {
1122                return service.handlePinMmiForPhoneAccount(accountHandle, dialString,
1123                        mContext.getOpPackageName());
1124            } catch (RemoteException e) {
1125                Log.e(TAG, "Error calling ITelecomService#handlePinMmi", e);
1126            }
1127        }
1128        return false;
1129    }
1130
1131    /**
1132     * @param accountHandle The handle for the account to derive an adn query URI for or
1133     * {@code null} to return a URI which will use the default account.
1134     * @return The URI (with the content:// scheme) specific to the specified {@link PhoneAccount}
1135     * for the the content retrieve.
1136     */
1137    public Uri getAdnUriForPhoneAccount(PhoneAccountHandle accountHandle) {
1138        ITelecomService service = getTelecomService();
1139        if (service != null && accountHandle != null) {
1140            try {
1141                return service.getAdnUriForPhoneAccount(accountHandle, mContext.getOpPackageName());
1142            } catch (RemoteException e) {
1143                Log.e(TAG, "Error calling ITelecomService#getAdnUriForPhoneAccount", e);
1144            }
1145        }
1146        return Uri.parse("content://icc/adn");
1147    }
1148
1149    /**
1150     * Removes the missed-call notification if one is present.
1151     * <p>
1152     * Requires that the method-caller be set as the system dialer app.
1153     * </p>
1154     */
1155    public void cancelMissedCallsNotification() {
1156        ITelecomService service = getTelecomService();
1157        if (service != null) {
1158            try {
1159                service.cancelMissedCallsNotification(mContext.getOpPackageName());
1160            } catch (RemoteException e) {
1161                Log.e(TAG, "Error calling ITelecomService#cancelMissedCallsNotification", e);
1162            }
1163        }
1164    }
1165
1166    /**
1167     * Brings the in-call screen to the foreground if there is an ongoing call. If there is
1168     * currently no ongoing call, then this method does nothing.
1169     * <p>
1170     * Requires that the method-caller be set as the system dialer app or have the
1171     * {@link android.Manifest.permission#READ_PHONE_STATE} permission.
1172     * </p>
1173     *
1174     * @param showDialpad Brings up the in-call dialpad as part of showing the in-call screen.
1175     */
1176    public void showInCallScreen(boolean showDialpad) {
1177        ITelecomService service = getTelecomService();
1178        if (service != null) {
1179            try {
1180                service.showInCallScreen(showDialpad, mContext.getOpPackageName());
1181            } catch (RemoteException e) {
1182                Log.e(TAG, "Error calling ITelecomService#showCallScreen", e);
1183            }
1184        }
1185    }
1186
1187    /**
1188     * Places a new outgoing call to the provided address using the system telecom service with
1189     * the specified extras.
1190     *
1191     * This method is equivalent to placing an outgoing call using {@link Intent#ACTION_CALL},
1192     * except that the outgoing call will always be sent via the system telecom service. If
1193     * method-caller is either the user selected default dialer app or preloaded system dialer
1194     * app, then emergency calls will also be allowed.
1195     *
1196     * Requires permission: {@link android.Manifest.permission#CALL_PHONE}
1197     *
1198     * Usage example:
1199     * <pre>
1200     * Uri uri = Uri.fromParts("tel", "12345", null);
1201     * Bundle extras = new Bundle();
1202     * extras.putBoolean(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, true);
1203     * telecomManager.placeCall(uri, extras);
1204     * </pre>
1205     *
1206     * @param address The address to make the call to.
1207     * @param extras Bundle of extras to use with the call.
1208     */
1209    public void placeCall(Uri address, Bundle extras) {
1210        ITelecomService service = getTelecomService();
1211        if (service != null) {
1212            if (address == null) {
1213                Log.w(TAG, "Cannot place call to empty address.");
1214            }
1215            try {
1216                service.placeCall(address, extras == null ? new Bundle() : extras,
1217                        mContext.getOpPackageName());
1218            } catch (RemoteException e) {
1219                Log.e(TAG, "Error calling ITelecomService#placeCall", e);
1220            }
1221        }
1222    }
1223
1224    /**
1225     * Enables and disables specified phone account.
1226     *
1227     * @param handle Handle to the phone account.
1228     * @param isEnabled Enable state of the phone account.
1229     * @hide
1230     */
1231    @SystemApi
1232    public void enablePhoneAccount(PhoneAccountHandle handle, boolean isEnabled) {
1233        ITelecomService service = getTelecomService();
1234        if (service != null) {
1235            try {
1236                service.enablePhoneAccount(handle, isEnabled);
1237            } catch (RemoteException e) {
1238                Log.e(TAG, "Error enablePhoneAbbount", e);
1239            }
1240        }
1241    }
1242
1243    private ITelecomService getTelecomService() {
1244        return ITelecomService.Stub.asInterface(ServiceManager.getService(Context.TELECOM_SERVICE));
1245    }
1246
1247    private boolean isServiceConnected() {
1248        boolean isConnected = getTelecomService() != null;
1249        if (!isConnected) {
1250            Log.w(TAG, "Telecom Service not found.");
1251        }
1252        return isConnected;
1253    }
1254}
1255