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