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