OSDN Git Service

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