• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.telephony;
18 
19 import android.Manifest;
20 import android.annotation.CallbackExecutor;
21 import android.annotation.IntDef;
22 import android.annotation.NonNull;
23 import android.annotation.RequiresPermission;
24 import android.annotation.SystemApi;
25 import android.compat.annotation.ChangeId;
26 import android.os.Binder;
27 import android.os.Build;
28 import android.telephony.emergency.EmergencyNumber;
29 import android.telephony.ims.ImsReasonInfo;
30 import android.telephony.ims.MediaQualityStatus;
31 import android.telephony.ims.MediaThreshold;
32 import android.util.Log;
33 
34 import com.android.internal.annotations.VisibleForTesting;
35 import com.android.internal.telephony.IPhoneStateListener;
36 
37 import dalvik.system.VMRuntime;
38 
39 import java.lang.annotation.Retention;
40 import java.lang.annotation.RetentionPolicy;
41 import java.lang.ref.WeakReference;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.concurrent.Executor;
45 
46 /**
47  * A callback class for monitoring changes in specific telephony states
48  * on the device, including service state, signal strength, message
49  * waiting indicator (voicemail), and others.
50  * <p>
51  * To register a callback, use a {@link TelephonyCallback} which implements interfaces regarding
52  * EVENT_*. For example,
53  * FakeServiceStateCallback extends {@link TelephonyCallback} implements
54  * {@link TelephonyCallback.ServiceStateListener}.
55  * <p>
56  * Then override the methods for the state that you wish to receive updates for, and
57  * pass the executor and your TelephonyCallback object to
58  * {@link TelephonyManager#registerTelephonyCallback}.
59  * Methods are called when the state changes, as well as once on initial registration.
60  * <p>
61  * Note that access to some telephony information is
62  * permission-protected. Your application won't receive updates for protected
63  * information unless it has the appropriate permissions declared in
64  * its manifest file. Where permissions apply, they are noted in the
65  * appropriate sub-interfaces.
66  */
67 public class TelephonyCallback {
68     private static final String LOG_TAG = "TelephonyCallback";
69     /**
70      * Experiment flag to set the per-pid registration limit for TelephonyCallback
71      *
72      * Limit on registrations of {@link TelephonyCallback}s on a per-pid basis. When this limit is
73      * exceeded, any calls to {@link TelephonyManager#registerTelephonyCallback} will fail with an
74      * {@link IllegalStateException}.
75      *
76      * {@link android.os.Process#PHONE_UID}, {@link android.os.Process#SYSTEM_UID}, and the uid that
77      * TelephonyRegistry runs under are exempt from this limit.
78      *
79      * If the value of the flag is less than 1, enforcement of the limit will be disabled.
80      * @hide
81      */
82     public static final String FLAG_PER_PID_REGISTRATION_LIMIT =
83             "phone_state_listener_per_pid_registration_limit";
84 
85     /**
86      * Default value for the per-pid registration limit.
87      * See {@link #FLAG_PER_PID_REGISTRATION_LIMIT}.
88      * @hide
89      */
90     public static final int DEFAULT_PER_PID_REGISTRATION_LIMIT = 50;
91 
92     /**
93      * This change enables a limit on the number of {@link TelephonyCallback} objects any process
94      * may register via {@link TelephonyManager#registerTelephonyCallback}. The default limit is 50,
95      * which may change via remote device config updates.
96      *
97      * This limit is enforced via an {@link IllegalStateException} thrown from
98      * {@link TelephonyManager#registerTelephonyCallback} when the offending process attempts to
99      * register one too many callbacks.
100      *
101      * @hide
102      */
103     @ChangeId
104     public static final long PHONE_STATE_LISTENER_LIMIT_CHANGE_ID = 150880553L;
105 
106     /**
107      * Event for changes to the network service state (cellular).
108      *
109      * <p>Requires {@link Manifest.permission#ACCESS_FINE_LOCATION} or {@link
110      * Manifest.permission#ACCESS_COARSE_LOCATION} depending on the accuracy of the location info
111      * listeners want to get.
112      *
113      * @hide
114      * @see ServiceStateListener#onServiceStateChanged
115      * @see ServiceState
116      */
117     @SystemApi
118     public static final int EVENT_SERVICE_STATE_CHANGED = 1;
119 
120     /**
121      * Event for changes to the network signal strength (cellular).
122      *
123      * @hide
124      * @see SignalStrengthsListener#onSignalStrengthsChanged
125      */
126     @SystemApi
127     public static final int EVENT_SIGNAL_STRENGTH_CHANGED = 2;
128 
129     /**
130      * Event for changes to the message-waiting indicator.
131      * <p>
132      * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE} or that
133      * the calling app has carrier privileges (see
134      * {@link TelephonyManager#hasCarrierPrivileges}).
135      * <p>
136      * Example: The status bar uses this to determine when to display the
137      * voicemail icon.
138      *
139      * @hide
140      * @see MessageWaitingIndicatorListener#onMessageWaitingIndicatorChanged
141      */
142     @SystemApi
143     @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
144     public static final int EVENT_MESSAGE_WAITING_INDICATOR_CHANGED = 3;
145 
146     /**
147      * Event for changes to the call-forwarding indicator.
148      * <p>
149      * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE} or that
150      * the calling app has carrier privileges (see
151      * {@link TelephonyManager#hasCarrierPrivileges}).
152      *
153      * @hide
154      * @see CallForwardingIndicatorListener#onCallForwardingIndicatorChanged
155      */
156     @SystemApi
157     @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
158     public static final int EVENT_CALL_FORWARDING_INDICATOR_CHANGED = 4;
159 
160     /**
161      * Event for changes to the device's cell location. Note that
162      * this will result in frequent listeners to the listener.
163      * <p>
164      * If you need regular location updates but want more control over
165      * the update interval or location precision, you can set up a callback
166      * through the {@link android.location.LocationManager location manager}
167      * instead.
168      *
169      * @hide
170      * @see CellLocationListener#onCellLocationChanged
171      */
172     @SystemApi
173     @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
174     public static final int EVENT_CELL_LOCATION_CHANGED = 5;
175 
176     /**
177      * Event for changes to the device call state.
178      * <p>
179      * Handles callbacks to {@link CallStateListener#onCallStateChanged(int)}.
180      * <p>
181      * Note: This is different from the legacy {@link #EVENT_LEGACY_CALL_STATE_CHANGED} listener
182      * which can include the phone number of the caller.  We purposely do not include the phone
183      * number as that information is not required for call state listeners going forward.
184      * @hide
185      */
186     @SystemApi
187     public static final int EVENT_CALL_STATE_CHANGED = 6;
188 
189     /**
190      * Event for changes to the data connection state (cellular).
191      *
192      * @hide
193      * @see DataConnectionStateListener#onDataConnectionStateChanged
194      */
195     @SystemApi
196     public static final int EVENT_DATA_CONNECTION_STATE_CHANGED = 7;
197 
198     /**
199      * Event for changes to the direction of data traffic on the data
200      * connection (cellular).
201      * <p>
202      * Example: The status bar uses this to display the appropriate
203      * data-traffic icon.
204      *
205      * @hide
206      * @see DataActivityListener#onDataActivity
207      */
208     @SystemApi
209     public static final int EVENT_DATA_ACTIVITY_CHANGED = 8;
210 
211     /**
212      * Event for changes to the network signal strengths (cellular).
213      * <p>
214      * Example: The status bar uses this to control the signal-strength
215      * icon.
216      *
217      * @hide
218      * @see SignalStrengthsListener#onSignalStrengthsChanged
219      */
220     @SystemApi
221     public static final int EVENT_SIGNAL_STRENGTHS_CHANGED = 9;
222 
223     /**
224      * Event for changes of the network signal strengths (cellular) always reported from modem,
225      * even in some situations such as the screen of the device is off.
226      *
227      * @hide
228      * @see TelephonyManager#setSignalStrengthUpdateRequest
229      */
230     @SystemApi
231     public static final int EVENT_ALWAYS_REPORTED_SIGNAL_STRENGTH_CHANGED = 10;
232 
233     /**
234      * Event for changes to observed cell info.
235      *
236      * @hide
237      * @see CellInfoListener#onCellInfoChanged
238      */
239     @SystemApi
240     @RequiresPermission(allOf = {
241             Manifest.permission.READ_PHONE_STATE,
242             Manifest.permission.ACCESS_FINE_LOCATION
243     })    public static final int EVENT_CELL_INFO_CHANGED = 11;
244 
245     /**
246      * Event for {@link android.telephony.Annotation.PreciseCallStates} of ringing,
247      * background and foreground calls.
248      *
249      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
250      * or the calling app has carrier privileges
251      * (see {@link TelephonyManager#hasCarrierPrivileges}).
252      *
253      * @hide
254      * @see PreciseCallStateListener#onPreciseCallStateChanged
255      */
256     @SystemApi
257     @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
258     public static final int EVENT_PRECISE_CALL_STATE_CHANGED = 12;
259 
260     /**
261      * Event for {@link PreciseDataConnectionState} on the data connection (cellular).
262      *
263      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
264      * or the calling app has carrier privileges
265      * (see {@link TelephonyManager#hasCarrierPrivileges}).
266      *
267      * @hide
268      * @see PreciseDataConnectionStateListener#onPreciseDataConnectionStateChanged
269      */
270     @SystemApi
271     @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
272     public static final int EVENT_PRECISE_DATA_CONNECTION_STATE_CHANGED = 13;
273 
274     /**
275      * Event for real time info for all data connections (cellular)).
276      *
277      * @hide
278      * @see PhoneStateListener#onDataConnectionRealTimeInfoChanged(DataConnectionRealTimeInfo)
279      * @deprecated Use {@link TelephonyManager#requestModemActivityInfo}
280      */
281     @Deprecated
282     @SystemApi
283     @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
284     public static final int EVENT_DATA_CONNECTION_REAL_TIME_INFO_CHANGED = 14;
285 
286     /**
287      * Event for OEM hook raw event
288      *
289      * @hide
290      * @see PhoneStateListener#onOemHookRawEvent
291      */
292     @SystemApi
293     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
294     public static final int EVENT_OEM_HOOK_RAW = 15;
295 
296     /**
297      * Event for changes to the SRVCC state of the active call.
298      *
299      * @hide
300      * @see SrvccStateListener#onSrvccStateChanged
301      */
302     @SystemApi
303     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
304     public static final int EVENT_SRVCC_STATE_CHANGED = 16;
305 
306     /**
307      * Event for carrier network changes indicated by a carrier app.
308      *
309      * @hide
310      * @see android.service.carrier.CarrierService#notifyCarrierNetworkChange(boolean)
311      * @see CarrierNetworkListener#onCarrierNetworkChange
312      */
313     @SystemApi
314     public static final int EVENT_CARRIER_NETWORK_CHANGED = 17;
315 
316     /**
317      * Event for changes to the sim voice activation state
318      *
319      * @hide
320      * @see TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATING
321      * @see TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATED
322      * @see TelephonyManager#SIM_ACTIVATION_STATE_DEACTIVATED
323      * @see TelephonyManager#SIM_ACTIVATION_STATE_RESTRICTED
324      * @see TelephonyManager#SIM_ACTIVATION_STATE_UNKNOWN
325      * <p>
326      * Example: TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATED indicates voice service has been
327      * fully activated
328      * @see VoiceActivationStateListener#onVoiceActivationStateChanged
329      */
330     @SystemApi
331     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
332     public static final int EVENT_VOICE_ACTIVATION_STATE_CHANGED = 18;
333 
334     /**
335      * Event for changes to the sim data activation state
336      *
337      * @hide
338      * @see TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATING
339      * @see TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATED
340      * @see TelephonyManager#SIM_ACTIVATION_STATE_DEACTIVATED
341      * @see TelephonyManager#SIM_ACTIVATION_STATE_RESTRICTED
342      * @see TelephonyManager#SIM_ACTIVATION_STATE_UNKNOWN
343      * <p>
344      * Example: TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATED indicates data service has been
345      * fully activated
346      * @see DataActivationStateListener#onDataActivationStateChanged
347      */
348     @SystemApi
349     public static final int EVENT_DATA_ACTIVATION_STATE_CHANGED = 19;
350 
351     /**
352      * Event for changes to the user mobile data state
353      *
354      * @hide
355      * @see UserMobileDataStateListener#onUserMobileDataStateChanged
356      */
357     @SystemApi
358     public static final int EVENT_USER_MOBILE_DATA_STATE_CHANGED = 20;
359 
360     /**
361      * Event for display info changed event.
362      *
363      * @hide
364      * @see DisplayInfoListener#onDisplayInfoChanged
365      */
366     @SystemApi
367     public static final int EVENT_DISPLAY_INFO_CHANGED = 21;
368 
369     /**
370      * Event for changes to the phone capability.
371      *
372      * @hide
373      * @see PhoneCapabilityListener#onPhoneCapabilityChanged
374      */
375     @SystemApi
376     public static final int EVENT_PHONE_CAPABILITY_CHANGED = 22;
377 
378     /**
379      * Event for changes to active data subscription ID. Active data subscription is
380      * the current subscription used to setup Cellular Internet data. The data is only active on the
381      * subscription at a time, even it is multi-SIM mode. For example, it could be the current
382      * active opportunistic subscription in use, or the subscription user selected as default data
383      * subscription in DSDS mode.
384      *
385      * <p>Requires permission {@link android.Manifest.permission#READ_PHONE_STATE} or the calling
386      * app has carrier privileges (see {@link TelephonyManager#hasCarrierPrivileges}).
387      *
388      * @hide
389      * @see ActiveDataSubscriptionIdListener#onActiveDataSubscriptionIdChanged
390      */
391     @SystemApi
392     @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
393     public static final int EVENT_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGED = 23;
394 
395     /**
396      * Event for changes to the radio power state.
397      *
398      * @hide
399      * @see RadioPowerStateListener#onRadioPowerStateChanged
400      */
401     @SystemApi
402     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
403     public static final int EVENT_RADIO_POWER_STATE_CHANGED = 24;
404 
405     /**
406      * Event for changes to emergency number list based on all active subscriptions.
407      *
408      * <p>Requires permission {@link android.Manifest.permission#READ_PHONE_STATE} or the calling
409      * app has carrier privileges (see {@link TelephonyManager#hasCarrierPrivileges}).
410      *
411      * @hide
412      * @see EmergencyNumberListListener#onEmergencyNumberListChanged
413      */
414     @SystemApi
415     @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
416     public static final int EVENT_EMERGENCY_NUMBER_LIST_CHANGED = 25;
417 
418     /**
419      * Event for call disconnect causes which contains {@link DisconnectCause} and
420      * {@link PreciseDisconnectCause}.
421      *
422      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
423      * or the calling app has carrier privileges
424      * (see {@link TelephonyManager#hasCarrierPrivileges}).
425      *
426      * @hide
427      * @see CallDisconnectCauseListener#onCallDisconnectCauseChanged
428      */
429     @SystemApi
430     @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
431     public static final int EVENT_CALL_DISCONNECT_CAUSE_CHANGED = 26;
432 
433     /**
434      * Event for changes to the call attributes of a currently active call.
435      *
436      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
437      * or the calling app has carrier privileges
438      * (see {@link TelephonyManager#hasCarrierPrivileges}).
439      *
440      * @hide
441      * @see CallAttributesListener#onCallAttributesChanged
442      */
443     @SystemApi
444     @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
445     public static final int EVENT_CALL_ATTRIBUTES_CHANGED = 27;
446 
447     /**
448      * Event for IMS call disconnect causes which contains
449      * {@link android.telephony.ims.ImsReasonInfo}
450      *
451      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
452      * or the calling app has carrier privileges
453      * (see {@link TelephonyManager#hasCarrierPrivileges}).
454      *
455      * @hide
456      * @see ImsCallDisconnectCauseListener#onImsCallDisconnectCauseChanged(ImsReasonInfo)
457      */
458     @SystemApi
459     @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
460     public static final int EVENT_IMS_CALL_DISCONNECT_CAUSE_CHANGED = 28;
461 
462     /**
463      * Event for the emergency number placed from an outgoing call.
464      *
465      * @hide
466      * @see OutgoingEmergencyCallListener#onOutgoingEmergencyCall
467      */
468     @SystemApi
469     @RequiresPermission(Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION)
470     public static final int EVENT_OUTGOING_EMERGENCY_CALL = 29;
471 
472     /**
473      * Event for the emergency number placed from an outgoing SMS.
474      *
475      * @hide
476      * @see OutgoingEmergencySmsListener#onOutgoingEmergencySms
477      */
478     @SystemApi
479     @RequiresPermission(Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION)
480     public static final int EVENT_OUTGOING_EMERGENCY_SMS = 30;
481 
482     /**
483      * Event for registration failures.
484      * <p>
485      * Event for indications that a registration procedure has failed in either the CS or PS
486      * domain. This indication does not necessarily indicate a change of service state, which should
487      * be tracked via {@link #EVENT_SERVICE_STATE_CHANGED}.
488      *
489      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE} or
490      * the calling app has carrier privileges (see {@link TelephonyManager#hasCarrierPrivileges}).
491      *
492      * <p>Requires the {@link Manifest.permission#ACCESS_FINE_LOCATION} permission in case that
493      * listener want to get location info in {@link CellIdentity} regardless of whether the calling
494      * app has carrier privileges.
495      *
496      * @hide
497      * @see RegistrationFailedListener#onRegistrationFailed
498      */
499     @SystemApi
500     @RequiresPermission(allOf = {
501             Manifest.permission.READ_PRECISE_PHONE_STATE,
502             Manifest.permission.ACCESS_FINE_LOCATION
503     })
504     public static final int EVENT_REGISTRATION_FAILURE = 31;
505 
506     /**
507      * Event for Barring Information for the current registered / camped cell.
508      *
509      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE} or
510      * the calling app has carrier privileges (see {@link TelephonyManager#hasCarrierPrivileges}).
511      *
512      * <p>Requires the {@link Manifest.permission#ACCESS_FINE_LOCATION} permission in case that
513      * listener want to get {@link BarringInfo} which includes location info in {@link CellIdentity}
514      * regardless of whether the calling app has carrier privileges.
515      *
516      * @hide
517      * @see BarringInfoListener#onBarringInfoChanged
518      */
519     @SystemApi
520     @RequiresPermission(allOf = {
521             Manifest.permission.READ_PRECISE_PHONE_STATE,
522             Manifest.permission.ACCESS_FINE_LOCATION
523     })
524     public static final int EVENT_BARRING_INFO_CHANGED = 32;
525 
526     /**
527      * Event for changes to the physical channel configuration.
528      *
529      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
530      * or the calling app has carrier privileges
531      * (see {@link TelephonyManager#hasCarrierPrivileges}).
532      *
533      * @hide
534      * @see PhysicalChannelConfigListener#onPhysicalChannelConfigChanged
535      */
536     @SystemApi
537     @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
538     public static final int EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED = 33;
539 
540 
541     /**
542      * Event for changes to the data enabled.
543      * <p>
544      * Event for indications that the enabled status of current data has changed.
545      *
546      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
547      * or the calling app has carrier privileges
548      * (see {@link TelephonyManager#hasCarrierPrivileges}).
549      *
550      * @hide
551      * @see DataEnabledListener#onDataEnabledChanged
552      */
553     @SystemApi
554     @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
555     public static final int EVENT_DATA_ENABLED_CHANGED = 34;
556 
557     /**
558      * Event for changes to allowed network list based on all active subscriptions.
559      *
560      * @hide
561      * @see AllowedNetworkTypesListener#onAllowedNetworkTypesChanged
562      */
563     @SystemApi
564     @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
565     public static final int EVENT_ALLOWED_NETWORK_TYPE_LIST_CHANGED = 35;
566 
567     /**
568      * Event for changes to the legacy call state changed listener implemented by
569      * {@link PhoneStateListener#onCallStateChanged(int, String)}.  This listener variant is similar
570      * to the new {@link CallStateListener#onCallStateChanged(int)} with the important distinction
571      * that it CAN provide the phone number associated with a call.
572      *
573      * @hide
574      */
575     @SystemApi
576     @RequiresPermission(android.Manifest.permission.READ_CALL_LOG)
577     public static final int EVENT_LEGACY_CALL_STATE_CHANGED = 36;
578 
579 
580     /**
581      * Event for changes to the link capacity estimate (LCE)
582      *
583      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
584      *
585      * @see LinkCapacityEstimateChangedListener#onLinkCapacityEstimateChanged
586      *
587      * @hide
588      */
589     @SystemApi
590     @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
591     public static final int EVENT_LINK_CAPACITY_ESTIMATE_CHANGED = 37;
592 
593     /**
594      * Event to norify the Anbr information from Radio to Ims.
595      *
596      * @see ImsCallSessionImplBase#callSessionNotifyAnbr.
597      *
598      * @hide
599      */
600     public static final int EVENT_TRIGGER_NOTIFY_ANBR = 38;
601 
602     /**
603      * Event for changes to the media quality status
604      *
605      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
606      *
607      * @see MediaQualityStatusChangedListener#onMediaQualityStatusChanged
608      *
609      * @hide
610      */
611     @SystemApi
612     @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
613     public static final int EVENT_MEDIA_QUALITY_STATUS_CHANGED = 39;
614 
615 
616     /**
617      * Event for changes to the Emergency callback mode
618      *
619      * <p>Requires permission {@link android.Manifest.permission#READ_PRIVILEGED_PHONE_STATE}
620      *
621      * @see EmergencyCallbackModeListener#onCallbackModeStarted(int)
622      * @see EmergencyCallbackModeListener#onCallbackModeStopped(int, int)
623      *
624      * @hide
625      */
626     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
627     public static final int EVENT_EMERGENCY_CALLBACK_MODE_CHANGED = 40;
628 
629     /**
630      * @hide
631      */
632     @IntDef(prefix = {"EVENT_"}, value = {
633             EVENT_SERVICE_STATE_CHANGED,
634             EVENT_SIGNAL_STRENGTH_CHANGED,
635             EVENT_MESSAGE_WAITING_INDICATOR_CHANGED,
636             EVENT_CALL_FORWARDING_INDICATOR_CHANGED,
637             EVENT_CELL_LOCATION_CHANGED,
638             EVENT_CALL_STATE_CHANGED,
639             EVENT_DATA_CONNECTION_STATE_CHANGED,
640             EVENT_DATA_ACTIVITY_CHANGED,
641             EVENT_SIGNAL_STRENGTHS_CHANGED,
642             EVENT_ALWAYS_REPORTED_SIGNAL_STRENGTH_CHANGED,
643             EVENT_CELL_INFO_CHANGED,
644             EVENT_PRECISE_CALL_STATE_CHANGED,
645             EVENT_PRECISE_DATA_CONNECTION_STATE_CHANGED,
646             EVENT_DATA_CONNECTION_REAL_TIME_INFO_CHANGED,
647             EVENT_OEM_HOOK_RAW,
648             EVENT_SRVCC_STATE_CHANGED,
649             EVENT_CARRIER_NETWORK_CHANGED,
650             EVENT_VOICE_ACTIVATION_STATE_CHANGED,
651             EVENT_DATA_ACTIVATION_STATE_CHANGED,
652             EVENT_USER_MOBILE_DATA_STATE_CHANGED,
653             EVENT_DISPLAY_INFO_CHANGED,
654             EVENT_PHONE_CAPABILITY_CHANGED,
655             EVENT_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGED,
656             EVENT_RADIO_POWER_STATE_CHANGED,
657             EVENT_EMERGENCY_NUMBER_LIST_CHANGED,
658             EVENT_CALL_DISCONNECT_CAUSE_CHANGED,
659             EVENT_CALL_ATTRIBUTES_CHANGED,
660             EVENT_IMS_CALL_DISCONNECT_CAUSE_CHANGED,
661             EVENT_OUTGOING_EMERGENCY_CALL,
662             EVENT_OUTGOING_EMERGENCY_SMS,
663             EVENT_REGISTRATION_FAILURE,
664             EVENT_BARRING_INFO_CHANGED,
665             EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED,
666             EVENT_DATA_ENABLED_CHANGED,
667             EVENT_ALLOWED_NETWORK_TYPE_LIST_CHANGED,
668             EVENT_LEGACY_CALL_STATE_CHANGED,
669             EVENT_LINK_CAPACITY_ESTIMATE_CHANGED,
670             EVENT_TRIGGER_NOTIFY_ANBR,
671             EVENT_MEDIA_QUALITY_STATUS_CHANGED,
672             EVENT_EMERGENCY_CALLBACK_MODE_CHANGED
673     })
674     @Retention(RetentionPolicy.SOURCE)
675     public @interface TelephonyEvent {
676     }
677 
678     /**
679      * @hide
680      */
681     //TODO: The maxTargetSdk should be S if the build time tool updates it.
682     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
683     public IPhoneStateListener callback;
684 
685     /**
686      * @hide
687      */
init(@onNull @allbackExecutor Executor executor)688     public void init(@NonNull @CallbackExecutor Executor executor) {
689         if (executor == null) {
690             throw new IllegalArgumentException("TelephonyCallback Executor must be non-null");
691         }
692         callback = new IPhoneStateListenerStub(this, executor);
693     }
694 
695     /**
696      * Interface for service state listener.
697      */
698     public interface ServiceStateListener {
699         /**
700          * Callback invoked when device service state changes on the registered subscription.
701          * Note, the registration subscription ID comes from {@link TelephonyManager} object
702          * which registers TelephonyCallback by
703          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
704          * If this TelephonyManager object was created with
705          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
706          * subscription ID. Otherwise, this callback applies to
707          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
708          * <p>
709          * The instance of {@link ServiceState} passed as an argument here will have various
710          * levels of location information stripped from it depending on the location permissions
711          * that your app holds.
712          * Only apps holding the {@link Manifest.permission#ACCESS_FINE_LOCATION} permission will
713          * receive all the information in {@link ServiceState}, otherwise the cellIdentity
714          * will be null if apps only holding the {@link Manifest.permission#ACCESS_COARSE_LOCATION}
715          * permission. Network operator name in long/short alphanumeric format and numeric id will
716          * be null if apps holding neither {@link android.Manifest.permission#ACCESS_FINE_LOCATION}
717          *
718          * @see ServiceState#STATE_EMERGENCY_ONLY
719          * @see ServiceState#STATE_IN_SERVICE
720          * @see ServiceState#STATE_OUT_OF_SERVICE
721          * @see ServiceState#STATE_POWER_OFF
722          */
onServiceStateChanged(@onNull ServiceState serviceState)723         void onServiceStateChanged(@NonNull ServiceState serviceState);
724     }
725 
726     /**
727      * Interface for message waiting indicator listener.
728      */
729     public interface MessageWaitingIndicatorListener {
730         /**
731          * Callback invoked when the message-waiting indicator changes on the registered
732          * subscription.
733          * Note, the registration subscription ID comes from {@link TelephonyManager} object by
734          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
735          * If this TelephonyManager object was created with
736          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
737          * subscription ID. Otherwise, this callback applies to
738          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
739          *
740          * The calling app should have carrier privileges
741          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
742          * {@link android.Manifest.permission#READ_PHONE_STATE}.
743          *
744          */
745         @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
onMessageWaitingIndicatorChanged(boolean mwi)746         void onMessageWaitingIndicatorChanged(boolean mwi);
747     }
748 
749     /**
750      * Interface for call-forwarding indicator listener.
751      */
752     public interface CallForwardingIndicatorListener {
753         /**
754          * Callback invoked when the call-forwarding indicator changes on the registered
755          * subscription.
756          * Note, the registration subscription ID comes from {@link TelephonyManager} object
757          * which registers TelephonyCallback by
758          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
759          * If this TelephonyManager object was created with
760          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
761          * subscription ID. Otherwise, this callback applies to
762          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
763          *
764          * The calling app should have carrier privileges
765          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
766          * {@link android.Manifest.permission#READ_PHONE_STATE}.
767          *
768          */
769         @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
onCallForwardingIndicatorChanged(boolean cfi)770         void onCallForwardingIndicatorChanged(boolean cfi);
771     }
772 
773     /**
774      * Interface for device cell location listener.
775      */
776     public interface CellLocationListener {
777         /**
778          * Callback invoked when device cell location changes on the registered subscription.
779          * Note, the registration subscription ID comes from {@link TelephonyManager} object
780          * which registers TelephonyCallback by
781          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
782          * If this TelephonyManager object was created with
783          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
784          * subscription ID. Otherwise, this callback applies to
785          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
786          */
787         @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
onCellLocationChanged(@onNull CellLocation location)788         void onCellLocationChanged(@NonNull CellLocation location);
789     }
790 
791     /**
792      * Interface for call state listener.
793      */
794     public interface CallStateListener {
795         /**
796          * Callback invoked when device call state changes.
797          * <p>
798          * Reports the state of Telephony (mobile) calls on the device for the registered
799          * subscription.
800          * <p>
801          * Note: the registration subscription ID comes from {@link TelephonyManager} object
802          * which registers TelephonyCallback by
803          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
804          * If this TelephonyManager object was created with
805          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
806          * subscription ID. Otherwise, this callback applies to
807          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
808          * <p>
809          * Note: The state returned here may differ from that returned by
810          * {@link TelephonyManager#getCallState()}. Receivers of this callback should be aware that
811          * calling {@link TelephonyManager#getCallState()} from within this callback may return a
812          * different state than the callback reports.
813          *
814          * @param state the current call state
815          */
816         @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
onCallStateChanged(@nnotation.CallState int state)817         void onCallStateChanged(@Annotation.CallState int state);
818     }
819 
820     /**
821      * Interface for data connection state listener.
822      */
823     public interface DataConnectionStateListener {
824         /**
825          * Callback invoked when connection state changes on the registered subscription.
826          * Note, the registration subscription ID comes from {@link TelephonyManager} object
827          * which registers TelephonyCallback by
828          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
829          * If this TelephonyManager object was created with
830          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
831          * subscription ID. Otherwise, this callback applies to
832          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
833          *
834          * @param state       is the current state of data connection.
835          * @param networkType is the current network type of data connection.
836          * @see TelephonyManager#DATA_DISCONNECTED
837          * @see TelephonyManager#DATA_CONNECTING
838          * @see TelephonyManager#DATA_CONNECTED
839          * @see TelephonyManager#DATA_SUSPENDED
840          * @see TelephonyManager#DATA_HANDOVER_IN_PROGRESS
841          */
onDataConnectionStateChanged(@elephonyManager.DataState int state, @Annotation.NetworkType int networkType)842         void onDataConnectionStateChanged(@TelephonyManager.DataState int state,
843                 @Annotation.NetworkType int networkType);
844     }
845 
846     /**
847      * Interface for data activity state listener.
848      */
849     public interface DataActivityListener {
850         /**
851          * Callback invoked when data activity state changes on the registered subscription.
852          * Note, the registration subscription ID comes from {@link TelephonyManager} object
853          * which registers TelephonyCallback by
854          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
855          * If this TelephonyManager object was created with
856          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
857          * subscription ID. Otherwise, this callback applies to
858          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
859          *
860          * @see TelephonyManager#DATA_ACTIVITY_NONE
861          * @see TelephonyManager#DATA_ACTIVITY_IN
862          * @see TelephonyManager#DATA_ACTIVITY_OUT
863          * @see TelephonyManager#DATA_ACTIVITY_INOUT
864          * @see TelephonyManager#DATA_ACTIVITY_DORMANT
865          */
onDataActivity(@nnotation.DataActivityType int direction)866         void onDataActivity(@Annotation.DataActivityType int direction);
867     }
868 
869     /**
870      * Interface for network signal strengths listener.
871      */
872     public interface SignalStrengthsListener {
873         /**
874          * Callback invoked when network signal strengths changes on the registered subscription.
875          * Note, the registration subscription ID comes from {@link TelephonyManager} object
876          * which registers TelephonyCallback by
877          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
878          * If this TelephonyManager object was created with
879          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
880          * subscription ID. Otherwise, this callback applies to
881          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
882          */
onSignalStrengthsChanged(@onNull SignalStrength signalStrength)883         void onSignalStrengthsChanged(@NonNull SignalStrength signalStrength);
884     }
885 
886     /**
887      * Interface for cell info listener.
888      */
889     public interface CellInfoListener {
890         /**
891          * Callback invoked when a observed cell info has changed or new cells have been added
892          * or removed on the registered subscription.
893          * Note, the registration subscription ID s from {@link TelephonyManager} object
894          * which registers TelephonyCallback by
895          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
896          * If this TelephonyManager object was created with
897          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
898          * subscription ID. Otherwise, this callback applies to
899          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
900          *
901          * @param cellInfo is the list of currently visible cells.
902          */
903         @RequiresPermission(allOf = {
904                 Manifest.permission.READ_PHONE_STATE,
905                 Manifest.permission.ACCESS_FINE_LOCATION
906         })
onCellInfoChanged(@onNull List<CellInfo> cellInfo)907         void onCellInfoChanged(@NonNull List<CellInfo> cellInfo);
908     }
909 
910     /**
911      * Interface for precise device call state listener.
912      *
913      * @hide
914      */
915     @SystemApi
916     public interface PreciseCallStateListener {
917         /**
918          * Callback invoked when precise device call state changes on the registered subscription.
919          * Note, the registration subscription ID comes from {@link TelephonyManager} object
920          * which registers TelephonyCallback by
921          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
922          * If this TelephonyManager object was created with
923          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
924          * subscription ID. Otherwise, this callback applies to
925          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
926          *
927          * The calling app should have carrier privileges
928          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
929          * {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}.
930          *
931          * @param callState {@link PreciseCallState}
932          */
933         @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
onPreciseCallStateChanged(@onNull PreciseCallState callState)934         void onPreciseCallStateChanged(@NonNull PreciseCallState callState);
935     }
936 
937     /**
938      * Interface for call disconnect cause listener.
939      */
940     public interface CallDisconnectCauseListener {
941         /**
942          * Callback invoked when call disconnect cause changes on the registered subscription.
943          * Note, the registration subscription ID comes from {@link TelephonyManager} object
944          * which registers TelephonyCallback by
945          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
946          * If this TelephonyManager object was created with
947          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
948          * subscription ID. Otherwise, this callback applies to
949          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
950          *
951          * @param disconnectCause        {@link DisconnectCause}.
952          * @param preciseDisconnectCause {@link PreciseDisconnectCause}.
953          */
954         @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
onCallDisconnectCauseChanged(@nnotation.DisconnectCauses int disconnectCause, @Annotation.PreciseDisconnectCauses int preciseDisconnectCause)955         void onCallDisconnectCauseChanged(@Annotation.DisconnectCauses int disconnectCause,
956                 @Annotation.PreciseDisconnectCauses int preciseDisconnectCause);
957     }
958 
959     /**
960      * Interface for IMS call disconnect cause listener.
961      */
962     public interface ImsCallDisconnectCauseListener {
963         /**
964          * Callback invoked when IMS call disconnect cause changes on the registered subscription.
965          * Note, the registration subscription ID comes from {@link TelephonyManager} object
966          * which registers TelephonyCallback by
967          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
968          * If this TelephonyManager object was created with
969          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
970          * subscription ID. Otherwise, this callback applies to
971          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
972          *
973          * The calling app should have carrier privileges
974          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
975          * {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}.
976          *
977          * @param imsReasonInfo {@link ImsReasonInfo} contains details on why IMS call failed.
978          */
979         @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
onImsCallDisconnectCauseChanged(@onNull ImsReasonInfo imsReasonInfo)980         void onImsCallDisconnectCauseChanged(@NonNull ImsReasonInfo imsReasonInfo);
981     }
982 
983     /**
984      * Interface for precise data connection state listener.
985      */
986     public interface PreciseDataConnectionStateListener {
987         /**
988          * Callback providing update about the default/internet data connection on the registered
989          * subscription.
990          * <p>
991          * Note, the registration subscription ID comes from {@link TelephonyManager} object
992          * which registers TelephonyCallback by
993          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
994          * If this TelephonyManager object was created with
995          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
996          * subscription ID. Otherwise, this callback applies to
997          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
998          *
999          * The calling app should have carrier privileges
1000          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
1001          * {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}.
1002          *
1003          * @param dataConnectionState {@link PreciseDataConnectionState}
1004          */
1005         @RequiresPermission(android.Manifest.permission.READ_PRECISE_PHONE_STATE)
onPreciseDataConnectionStateChanged( @onNull PreciseDataConnectionState dataConnectionState)1006         void onPreciseDataConnectionStateChanged(
1007             @NonNull PreciseDataConnectionState dataConnectionState);
1008     }
1009 
1010     /**
1011      * Interface for Single Radio Voice Call Continuity listener.
1012      *
1013      * @hide
1014      */
1015     @SystemApi
1016     public interface SrvccStateListener {
1017         /**
1018          * Callback invoked when there has been a change in the Single Radio Voice Call Continuity
1019          * (SRVCC) state for the currently active call on the registered subscription.
1020          * <p>
1021          * Note, the registration subscription ID comes from {@link TelephonyManager} object
1022          * which registers TelephonyCallback by
1023          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
1024          * If this TelephonyManager object was created with
1025          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1026          * subscription ID. Otherwise, this callback applies to
1027          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1028          */
1029         @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
onSrvccStateChanged(@nnotation.SrvccState int srvccState)1030         void onSrvccStateChanged(@Annotation.SrvccState int srvccState);
1031     }
1032 
1033     /**
1034      * Interface for SIM voice activation state listener.
1035      *
1036      * @hide
1037      */
1038     @SystemApi
1039     public interface VoiceActivationStateListener {
1040         /**
1041          * Callback invoked when the SIM voice activation state has changed on the registered
1042          * subscription.
1043          * Note, the registration subscription ID comes from {@link TelephonyManager} object
1044          * which registers TelephonyCallback by
1045          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
1046          * If this TelephonyManager object was created with
1047          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1048          * subscription ID. Otherwise, this callback applies to
1049          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1050          *
1051          * @param state is the current SIM voice activation state
1052          */
1053         @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
onVoiceActivationStateChanged(@nnotation.SimActivationState int state)1054         void onVoiceActivationStateChanged(@Annotation.SimActivationState int state);
1055 
1056     }
1057 
1058     /**
1059      * Interface for SIM data activation state listener.
1060      */
1061     public interface DataActivationStateListener {
1062         /**
1063          * Callback invoked when the SIM data activation state has changed on the registered
1064          * subscription.
1065          * Note, the registration subscription ID comes from {@link TelephonyManager} object
1066          * which registers TelephonyCallback by
1067          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
1068          * If this TelephonyManager object was created with
1069          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1070          * subscription ID. Otherwise, this callback applies to
1071          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1072          *
1073          * @param state is the current SIM data activation state
1074          */
onDataActivationStateChanged(@nnotation.SimActivationState int state)1075         void onDataActivationStateChanged(@Annotation.SimActivationState int state);
1076     }
1077 
1078     /**
1079      * Interface for user mobile data state listener.
1080      */
1081     public interface UserMobileDataStateListener {
1082         /**
1083          * Callback invoked when the user mobile data state has changed on the registered
1084          * subscription.
1085          * Note, the registration subscription ID comes from {@link TelephonyManager} object
1086          * which registers TelephonyCallback by
1087          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
1088          * If this TelephonyManager object was created with
1089          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1090          * subscription ID. Otherwise, this callback applies to
1091          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1092          *
1093          * @param enabled indicates whether the current user mobile data state is enabled or
1094          *                disabled.
1095          */
onUserMobileDataStateChanged(boolean enabled)1096         void onUserMobileDataStateChanged(boolean enabled);
1097     }
1098 
1099     /**
1100      * Interface for display info listener.
1101      */
1102     public interface DisplayInfoListener {
1103         /**
1104          * Callback invoked when the display info has changed on the registered subscription.
1105          * <p> The {@link TelephonyDisplayInfo} contains status information shown to the user
1106          * based on carrier policy.
1107          *
1108          * @param telephonyDisplayInfo The display information.
1109          */
onDisplayInfoChanged(@onNull TelephonyDisplayInfo telephonyDisplayInfo)1110         void onDisplayInfoChanged(@NonNull TelephonyDisplayInfo telephonyDisplayInfo);
1111     }
1112 
1113     /**
1114      * Interface for the current emergency number list listener.
1115      */
1116     public interface EmergencyNumberListListener {
1117         /**
1118          * Callback invoked when the current emergency number list has changed on the registered
1119          * subscription.
1120          * <p>
1121          * Note, the registered subscription is associated with {@link TelephonyManager} object
1122          * on which
1123          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}
1124          * was called.
1125          * If this TelephonyManager object was created with
1126          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1127          * given subscription ID. Otherwise, this callback applies to
1128          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1129          *
1130          * The calling app should have carrier privileges
1131          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
1132          * {@link android.Manifest.permission#READ_PHONE_STATE}.
1133          *
1134          * @param emergencyNumberList Map associating all active subscriptions on the device with
1135          *                            the list of emergency numbers originating from that
1136          *                            subscription.
1137          *                            If there are no active subscriptions, the map will contain a
1138          *                            single entry with
1139          *                            {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} as
1140          *                            the key and a list of emergency numbers as the value. If no
1141          *                            emergency number information is available, the value will be
1142          *                            empty.
1143          */
1144         @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
onEmergencyNumberListChanged(@onNull Map<Integer, List<EmergencyNumber>> emergencyNumberList)1145         void onEmergencyNumberListChanged(@NonNull Map<Integer,
1146                 List<EmergencyNumber>> emergencyNumberList);
1147     }
1148 
1149     /**
1150      * Interface for outgoing emergency call listener.
1151      *
1152      * @hide
1153      */
1154     @SystemApi
1155     public interface OutgoingEmergencyCallListener {
1156         /**
1157          * Callback invoked when an outgoing call is placed to an emergency number.
1158          * <p>
1159          * This method will be called when an emergency call is placed on any subscription
1160          * (including the no-SIM case), regardless of which subscription this callback was
1161          * registered on.
1162          * <p>
1163          *
1164          * @param placedEmergencyNumber The {@link EmergencyNumber} the emergency call was
1165          *                              placed to.
1166          * @param subscriptionId        The subscription ID used to place the emergency call. If the
1167          *                              emergency call was placed without a valid subscription
1168          *                              (e.g. when there are no SIM cards in the device), this
1169          *                              will be
1170          *                              equal to
1171          *                              {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID}.
1172          */
1173         @RequiresPermission(Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION)
onOutgoingEmergencyCall(@onNull EmergencyNumber placedEmergencyNumber, int subscriptionId)1174         void onOutgoingEmergencyCall(@NonNull EmergencyNumber placedEmergencyNumber,
1175                 int subscriptionId);
1176     }
1177 
1178     /**
1179      * Interface for outgoing emergency sms listener.
1180      *
1181      * @hide
1182      */
1183     @SystemApi
1184     public interface OutgoingEmergencySmsListener {
1185         /**
1186          * Smsback invoked when an outgoing sms is sent to an emergency number.
1187          * <p>
1188          * This method will be called when an emergency sms is sent on any subscription,
1189          * regardless of which subscription this callback was registered on.
1190          *
1191          * @param sentEmergencyNumber The {@link EmergencyNumber} the emergency sms was sent to.
1192          * @param subscriptionId      The subscription ID used to send the emergency sms.
1193          */
1194         @RequiresPermission(Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION)
onOutgoingEmergencySms(@onNull EmergencyNumber sentEmergencyNumber, int subscriptionId)1195         void onOutgoingEmergencySms(@NonNull EmergencyNumber sentEmergencyNumber,
1196                 int subscriptionId);
1197     }
1198 
1199     /**
1200      * Interface for phone capability listener.
1201      *
1202      * @hide
1203      */
1204     @SystemApi
1205     public interface PhoneCapabilityListener {
1206         /**
1207          * Callback invoked when phone capability changes.
1208          * Note, this callback triggers regardless of registered subscription.
1209          *
1210          * @param capability the new phone capability
1211          */
onPhoneCapabilityChanged(@onNull PhoneCapability capability)1212         void onPhoneCapabilityChanged(@NonNull PhoneCapability capability);
1213     }
1214 
1215     /**
1216      * Interface for active data subscription ID listener.
1217      */
1218     public interface ActiveDataSubscriptionIdListener {
1219         /**
1220          * Callback invoked when active data subscription ID changes.
1221          * Note, this callback triggers regardless of registered subscription.
1222          *
1223          * @param subId current subscription used to setup Cellular Internet data. The data is
1224          *              only active on the subscription at a time, even it is multi-SIM mode.
1225          *              For example, it could be the current active opportunistic subscription
1226          *              in use, or the subscription user selected as default data subscription in
1227          *              DSDS mode.
1228          *
1229          * The calling app should have carrier privileges
1230          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
1231          * {@link android.Manifest.permission#READ_PHONE_STATE}.
1232          *
1233          */
1234         @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
onActiveDataSubscriptionIdChanged(int subId)1235         void onActiveDataSubscriptionIdChanged(int subId);
1236     }
1237 
1238     /**
1239      * Interface for modem radio power state listener.
1240      *
1241      * @hide
1242      */
1243     @SystemApi
1244     public interface RadioPowerStateListener {
1245         /**
1246          * Callback invoked when modem radio power state changes on the registered subscription.
1247          * Note, the registration subscription ID comes from {@link TelephonyManager} object
1248          * which registers TelephonyCallback by
1249          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
1250          * If this TelephonyManager object was created with
1251          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1252          * subscription ID. Otherwise, this callback applies to
1253          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1254          *
1255          * @param state the modem radio power state
1256          */
1257         @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
onRadioPowerStateChanged(@nnotation.RadioPowerState int state)1258         void onRadioPowerStateChanged(@Annotation.RadioPowerState int state);
1259     }
1260 
1261     /**
1262      * Interface for carrier network listener.
1263      */
1264     public interface CarrierNetworkListener {
1265         /**
1266          * Callback invoked when telephony has received notice from a carrier
1267          * app that a network action that could result in connectivity loss
1268          * has been requested by an app using
1269          * {@link android.service.carrier.CarrierService#notifyCarrierNetworkChange(boolean)}
1270          * <p>
1271          * This is optional and is only used to allow the system to provide alternative UI while
1272          * telephony is performing an action that may result in intentional, temporary network
1273          * lack of connectivity.
1274          * <p>
1275          * Note, this callback is pinned to the registered subscription and will be invoked when
1276          * the notifying carrier app has carrier privilege rule on the registered
1277          * subscription. {@link android.telephony.TelephonyManager#hasCarrierPrivileges}
1278          *
1279          * @param active If the carrier network change is or shortly will be active,
1280          *               {@code true} indicate that showing alternative UI, {@code false} otherwise.
1281          */
onCarrierNetworkChange(boolean active)1282         void onCarrierNetworkChange(boolean active);
1283     }
1284 
1285     /**
1286      * Interface for registration failures listener.
1287      */
1288     public interface RegistrationFailedListener {
1289         /**
1290          * Report that Registration or a Location/Routing/Tracking Area update has failed.
1291          *
1292          * <p>Indicate whenever a registration procedure, including a location, routing, or tracking
1293          * area update fails. This includes procedures that do not necessarily result in a change of
1294          * the modem's registration status. If the modem's registration status changes, that is
1295          * reflected in the onNetworkStateChanged() and subsequent
1296          * get{Voice/Data}RegistrationState().
1297          *
1298          * <p>Because registration failures are ephemeral, this callback is not sticky.
1299          * Registrants will not receive the most recent past value when registering.
1300          *
1301          * The calling app should have carrier privileges
1302          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
1303          * {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE} and
1304          * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
1305          *
1306          * If the calling app doesn't have {@link android.Manifest.permission#ACCESS_FINE_LOCATION},
1307          * it will receive {@link CellIdentity} without location-sensitive information included.
1308          *
1309          * @param cellIdentity        the CellIdentity, which must include the globally unique
1310          *                            identifier
1311          *                            for the cell (for example, all components of the CGI or ECGI).
1312          * @param chosenPlmn          a 5 or 6 digit alphanumeric PLMN (MCC|MNC) among those
1313          *                            broadcast by the
1314          *                            cell that was chosen for the failed registration attempt.
1315          * @param domain              DOMAIN_CS, DOMAIN_PS or both in case of a combined procedure.
1316          * @param causeCode           the primary failure cause code of the procedure.
1317          *                            For GSM/UMTS (MM), values are in TS 24.008 Sec 10.5.95
1318          *                            For GSM/UMTS (GMM), values are in TS 24.008 Sec 10.5.147
1319          *                            For LTE (EMM), cause codes are TS 24.301 Sec 9.9.3.9
1320          *                            For NR (5GMM), cause codes are TS 24.501 Sec 9.11.3.2
1321          *                            Integer.MAX_VALUE if this value is unused.
1322          * @param additionalCauseCode the cause code of any secondary/combined procedure
1323          *                            if appropriate. For UMTS, if a combined attach succeeds for
1324          *                            PS only, then the GMM cause code shall be included as an
1325          *                            additionalCauseCode. For LTE (ESM), cause codes are in
1326          *                            TS 24.301 9.9.4.4. Integer.MAX_VALUE if this value is unused.
1327          */
1328         @RequiresPermission(allOf = {
1329                 Manifest.permission.READ_PRECISE_PHONE_STATE,
1330                 Manifest.permission.ACCESS_FINE_LOCATION
1331         })
onRegistrationFailed(@onNull CellIdentity cellIdentity, @NonNull String chosenPlmn, @NetworkRegistrationInfo.Domain int domain, int causeCode, int additionalCauseCode)1332         void onRegistrationFailed(@NonNull CellIdentity cellIdentity, @NonNull String chosenPlmn,
1333                 @NetworkRegistrationInfo.Domain int domain, int causeCode, int additionalCauseCode);
1334     }
1335 
1336     /**
1337      * Interface for the current allowed network type list listener. This list involves values of
1338      * allowed network type for each of reasons.
1339      *
1340      * @hide
1341      */
1342     @SystemApi
1343     public interface AllowedNetworkTypesListener {
1344         /**
1345          * Callback invoked when the current allowed network type list has changed on the
1346          * registered subscription for a specified reason.
1347          * Note, the registered subscription is associated with {@link TelephonyManager} object
1348          * on which {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}
1349          * was called.
1350          * If this TelephonyManager object was created with
1351          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1352          * given subscription ID. Otherwise, this callback applies to
1353          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1354          *
1355          * @param reason an allowed network type reasons.
1356          * @see TelephonyManager#ALLOWED_NETWORK_TYPES_REASON_USER
1357          * @see TelephonyManager#ALLOWED_NETWORK_TYPES_REASON_POWER
1358          * @see TelephonyManager#ALLOWED_NETWORK_TYPES_REASON_CARRIER
1359          * @see TelephonyManager#ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
1360          *
1361          * @param allowedNetworkType an allowed network type bitmask value. (for example,
1362          * the long bitmask value is {{@link TelephonyManager#NETWORK_TYPE_BITMASK_NR}|
1363          * {@link TelephonyManager#NETWORK_TYPE_BITMASK_LTE}})
1364          *
1365          * For example:
1366          * If the latest allowed network type is changed by user, then the system
1367          * notifies the {@link TelephonyManager#ALLOWED_NETWORK_TYPES_REASON_USER} and
1368          * long type value}.
1369          */
1370         @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
onAllowedNetworkTypesChanged(@elephonyManager.AllowedNetworkTypesReason int reason, @TelephonyManager.NetworkTypeBitMask long allowedNetworkType)1371         void onAllowedNetworkTypesChanged(@TelephonyManager.AllowedNetworkTypesReason int reason,
1372                 @TelephonyManager.NetworkTypeBitMask long allowedNetworkType);
1373     }
1374 
1375     /**
1376      * Interface for call attributes listener.
1377      *
1378      * @hide
1379      */
1380     @SystemApi
1381     public interface CallAttributesListener {
1382         /**
1383          * Callback invoked when the call attributes changes on the active call on the registered
1384          * subscription. If the user swaps between a foreground and background call the call
1385          * attributes will be reported for the active call only.
1386          * Note, the registration subscription ID comes from {@link TelephonyManager} object
1387          * which registers TelephonyCallback by
1388          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
1389          * If this TelephonyManager object was created with
1390          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1391          * subscription ID. Otherwise, this callback applies to
1392          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1393          *
1394          * The calling app should have carrier privileges
1395          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
1396          * {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}.
1397          *
1398          * @param callAttributes the call attributes
1399          * @deprecated Use onCallStatesChanged({@link List<CallState>}) to get each of call
1400          *          state for all ongoing calls on the subscription.
1401          */
1402         @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
1403         @Deprecated
onCallAttributesChanged(@onNull CallAttributes callAttributes)1404         default void onCallAttributesChanged(@NonNull CallAttributes callAttributes) {
1405             Log.w(LOG_TAG, "onCallAttributesChanged(List<CallState>) should be "
1406                     + "overridden.");
1407         }
1408 
1409         /**
1410          * Callback invoked when the call attributes changes on the ongoing calls on the registered
1411          * subscription. If there are 1 foreground and 1 background call, Two {@link CallState}
1412          * will be passed.
1413          * Note, the registration subscription ID comes from {@link TelephonyManager} object
1414          * which registers TelephonyCallback by
1415          * {@link TelephonyManager#registerTelephonyCallback(Executor, TelephonyCallback)}.
1416          * If this TelephonyManager object was created with
1417          * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1418          * subscription ID. Otherwise, this callback applies to
1419          * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1420          * In the event that there are no active(state is not
1421          * {@link PreciseCallState#PRECISE_CALL_STATE_IDLE}) calls, this API will report empty list.
1422          *
1423          * The calling app should have carrier privileges
1424          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
1425          * {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}.
1426          *
1427          * @param callStateList the list of call states for each ongoing call. If there are
1428          *                           a active call and a holding call, 1 call attributes for
1429          *                           {@link PreciseCallState#PRECISE_CALL_STATE_ACTIVE}  and another
1430          *                           for {@link PreciseCallState#PRECISE_CALL_STATE_HOLDING}
1431          *                           will be in this list.
1432          */
1433         // Added as default for backward compatibility
1434         @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
onCallStatesChanged(@onNull List<CallState> callStateList)1435         default void onCallStatesChanged(@NonNull List<CallState> callStateList) {
1436             if (callStateList.size() > 0) {
1437                 int foregroundCallState = PreciseCallState.PRECISE_CALL_STATE_IDLE;
1438                 int backgroundCallState = PreciseCallState.PRECISE_CALL_STATE_IDLE;
1439                 int ringingCallState = PreciseCallState.PRECISE_CALL_STATE_IDLE;
1440                 for (CallState cs : callStateList) {
1441                     switch (cs.getCallClassification()) {
1442                         case CallState.CALL_CLASSIFICATION_FOREGROUND:
1443                             foregroundCallState = cs.getCallState();
1444                             break;
1445                         case CallState.CALL_CLASSIFICATION_BACKGROUND:
1446                             backgroundCallState = cs.getCallState();
1447                             break;
1448                         case CallState.CALL_CLASSIFICATION_RINGING:
1449                             ringingCallState = cs.getCallState();
1450                             break;
1451                         default:
1452                             break;
1453                     }
1454                 }
1455                 onCallAttributesChanged(new CallAttributes(
1456                         new PreciseCallState(
1457                                 ringingCallState, foregroundCallState, backgroundCallState,
1458                                 DisconnectCause.NOT_VALID, PreciseDisconnectCause.NOT_VALID),
1459                         callStateList.get(0).getNetworkType(),
1460                         callStateList.get(0).getCallQuality()));
1461             } else {
1462                 onCallAttributesChanged(new CallAttributes(
1463                         new PreciseCallState(PreciseCallState.PRECISE_CALL_STATE_IDLE,
1464                                 PreciseCallState.PRECISE_CALL_STATE_IDLE,
1465                                 PreciseCallState.PRECISE_CALL_STATE_IDLE,
1466                                 DisconnectCause.NOT_VALID, PreciseDisconnectCause.NOT_VALID),
1467                         TelephonyManager.NETWORK_TYPE_UNKNOWN, new CallQuality()));
1468             }
1469         }
1470     }
1471 
1472     /**
1473      * Interface for barring information listener.
1474      */
1475     public interface BarringInfoListener {
1476         /**
1477          * Report updated barring information for the current camped/registered cell.
1478          *
1479          * <p>Barring info is provided for all services applicable to the current camped/registered
1480          * cell, for the registered PLMN and current access class/access category.
1481          *
1482          * The calling app should have carrier privileges
1483          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
1484          * {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE} and
1485          * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
1486          *
1487          * If the calling app doesn't have {@link android.Manifest.permission#ACCESS_FINE_LOCATION},
1488          * it will receive {@link BarringInfo} including {@link CellIdentity} without
1489          * location-sensitive information included.
1490          *
1491          * @param barringInfo for all services on the current cell.
1492          * @see android.telephony.BarringInfo
1493          */
1494         @RequiresPermission(allOf = {
1495                 Manifest.permission.READ_PRECISE_PHONE_STATE,
1496                 Manifest.permission.ACCESS_FINE_LOCATION
1497         })
onBarringInfoChanged(@onNull BarringInfo barringInfo)1498         void onBarringInfoChanged(@NonNull BarringInfo barringInfo);
1499     }
1500 
1501     /**
1502      * Interface for current physical channel configuration listener.
1503      */
1504     public interface PhysicalChannelConfigListener {
1505         /**
1506          * Callback invoked when the current physical channel configuration has changed
1507          *
1508          * The calling app should have carrier privileges
1509          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
1510          * {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}.
1511          *
1512          * @param configs List of the current {@link PhysicalChannelConfig}s
1513          */
1514         @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
onPhysicalChannelConfigChanged(@onNull List<PhysicalChannelConfig> configs)1515         void onPhysicalChannelConfigChanged(@NonNull List<PhysicalChannelConfig> configs);
1516     }
1517 
1518     /**
1519      * Interface for data enabled listener.
1520      *
1521      * @hide
1522      */
1523     @SystemApi
1524     public interface DataEnabledListener {
1525         /**
1526          * Callback invoked when the data enabled changes.
1527          *
1528          * The calling app should have carrier privileges
1529          * (see {@link TelephonyManager#hasCarrierPrivileges}) if it does not have the
1530          * {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}.
1531          *
1532          * @param enabled {@code true} if data is enabled, otherwise disabled.
1533          * @param reason  Reason for data enabled/disabled.
1534          *                See {@link TelephonyManager.DataEnabledChangedReason}.
1535          */
1536         @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
onDataEnabledChanged(boolean enabled, @TelephonyManager.DataEnabledChangedReason int reason)1537         void onDataEnabledChanged(boolean enabled,
1538                 @TelephonyManager.DataEnabledChangedReason int reason);
1539     }
1540 
1541     /**
1542      * Interface for link capacity estimate changed listener.
1543      *
1544      * @hide
1545      */
1546     @SystemApi
1547     public interface LinkCapacityEstimateChangedListener {
1548         /**
1549          * Callback invoked when the link capacity estimate (LCE) changes
1550          *
1551          * @param linkCapacityEstimateList a list of {@link LinkCapacityEstimate}
1552          * The list size is at least 1.
1553          * In case of a dual connected network, the list size could be 2.
1554          * Use {@link LinkCapacityEstimate#getType()} to get the type of each element.
1555          */
1556         @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
onLinkCapacityEstimateChanged( @onNull List<LinkCapacityEstimate> linkCapacityEstimateList)1557         void onLinkCapacityEstimateChanged(
1558                 @NonNull List<LinkCapacityEstimate> linkCapacityEstimateList);
1559     }
1560 
1561     /**
1562      * Interface for media quality status changed listener.
1563      *
1564      * @hide
1565      */
1566     @SystemApi
1567     public interface MediaQualityStatusChangedListener {
1568         /**
1569          * Callback invoked when the media quality status of IMS call changes. This call back
1570          * means current media quality status crosses at least one of threshold values in {@link
1571          * MediaThreshold}. Listener needs to get quality information & check whether it crossed
1572          * listener's threshold.
1573          *
1574          * <p/> Currently thresholds for this indication can be configurable by CARRIER_CONFIG
1575          * {@link CarrierConfigManager#KEY_VOICE_RTP_THRESHOLDS_PACKET_LOSS_RATE_INT}
1576          * {@link CarrierConfigManager#KEY_VOICE_RTP_THRESHOLDS_INACTIVITY_TIME_IN_MILLIS_INT}
1577          * {@link CarrierConfigManager#KEY_VOICE_RTP_THRESHOLDS_JITTER_INT}
1578          *
1579          * @param mediaQualityStatus The media quality status currently measured.
1580          */
1581         @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
onMediaQualityStatusChanged(@onNull MediaQualityStatus mediaQualityStatus)1582         void onMediaQualityStatusChanged(@NonNull MediaQualityStatus mediaQualityStatus);
1583     }
1584 
1585     /**
1586      * Interface for emergency callback mode listener.
1587      *
1588      * @hide
1589      */
1590     public interface EmergencyCallbackModeListener {
1591         /**
1592          * Indicates that Callback Mode has been started.
1593          * <p>
1594          * This method will be called when an emergency sms/emergency call is sent
1595          * and the callback mode is supported by the carrier.
1596          * If an emergency SMS is transmitted during callback mode for SMS, this API will be called
1597          * once again with TelephonyManager#EMERGENCY_CALLBACK_MODE_SMS.
1598          *
1599          * @param type for callback mode entry
1600          *             See {@link TelephonyManager.EmergencyCallbackModeType}.
1601          * @see TelephonyManager#EMERGENCY_CALLBACK_MODE_CALL
1602          * @see TelephonyManager#EMERGENCY_CALLBACK_MODE_SMS
1603          */
1604         @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
onCallBackModeStarted(@elephonyManager.EmergencyCallbackModeType int type)1605         void onCallBackModeStarted(@TelephonyManager.EmergencyCallbackModeType int type);
1606 
1607         /**
1608          * Indicates that Callback Mode has been stopped.
1609          * <p>
1610          * This method will be called when the callback mode timer expires or when
1611          * a normal call/SMS is sent
1612          *
1613          * @param type for callback mode entry
1614          * @see TelephonyManager#EMERGENCY_CALLBACK_MODE_CALL
1615          * @see TelephonyManager#EMERGENCY_CALLBACK_MODE_SMS
1616          *
1617          * @param reason for changing callback mode
1618          *
1619          * @see TelephonyManager#STOP_REASON_UNKNOWN
1620          * @see TelephonyManager#STOP_REASON_OUTGOING_NORMAL_CALL_INITIATED
1621          * @see TelephonyManager#STOP_REASON_NORMAL_SMS_SENT
1622          * @see TelephonyManager#STOP_REASON_OUTGOING_EMERGENCY_CALL_INITIATED
1623          * @see TelephonyManager#STOP_REASON_EMERGENCY_SMS_SENT
1624          * @see TelephonyManager#STOP_REASON_TIMER_EXPIRED
1625          * @see TelephonyManager#STOP_REASON_USER_ACTION
1626          */
1627         @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
onCallBackModeStopped(@elephonyManager.EmergencyCallbackModeType int type, @TelephonyManager.EmergencyCallbackModeStopReason int reason)1628         void onCallBackModeStopped(@TelephonyManager.EmergencyCallbackModeType int type,
1629                 @TelephonyManager.EmergencyCallbackModeStopReason int reason);
1630     }
1631 
1632     /**
1633      * The callback methods need to be called on the handler thread where
1634      * this object was created.  If the binder did that for us it'd be nice.
1635      * <p>
1636      * Using a static class and weak reference here to avoid memory leak caused by the
1637      * IPhoneState.Stub callback retaining references to the outside TelephonyCallback:
1638      * even caller has been destroyed and "un-registered" the TelephonyCallback, it is still not
1639      * eligible for GC given the references coming from:
1640      * Native Stack --> TelephonyCallback --> Context (Activity).
1641      * memory of caller's context will be collected after GC from service side get triggered
1642      */
1643     private static class IPhoneStateListenerStub extends IPhoneStateListener.Stub {
1644         private WeakReference<TelephonyCallback> mTelephonyCallbackWeakRef;
1645         private Executor mExecutor;
1646 
IPhoneStateListenerStub(TelephonyCallback telephonyCallback, Executor executor)1647         IPhoneStateListenerStub(TelephonyCallback telephonyCallback, Executor executor) {
1648             mTelephonyCallbackWeakRef = new WeakReference<TelephonyCallback>(telephonyCallback);
1649             mExecutor = executor;
1650         }
1651 
onServiceStateChanged(ServiceState serviceState)1652         public void onServiceStateChanged(ServiceState serviceState) {
1653             ServiceStateListener listener = (ServiceStateListener) mTelephonyCallbackWeakRef.get();
1654             if (listener == null) return;
1655 
1656             Binder.withCleanCallingIdentity(
1657                     () -> mExecutor.execute(() -> listener.onServiceStateChanged(serviceState)));
1658         }
1659 
onSignalStrengthChanged(int asu)1660         public void onSignalStrengthChanged(int asu) {
1661             // default implementation empty
1662         }
1663 
onMessageWaitingIndicatorChanged(boolean mwi)1664         public void onMessageWaitingIndicatorChanged(boolean mwi) {
1665             MessageWaitingIndicatorListener listener =
1666                     (MessageWaitingIndicatorListener) mTelephonyCallbackWeakRef.get();
1667             if (listener == null) return;
1668 
1669             Binder.withCleanCallingIdentity(
1670                     () -> mExecutor.execute(() -> listener.onMessageWaitingIndicatorChanged(mwi)));
1671         }
1672 
onCallForwardingIndicatorChanged(boolean cfi)1673         public void onCallForwardingIndicatorChanged(boolean cfi) {
1674             CallForwardingIndicatorListener listener =
1675                     (CallForwardingIndicatorListener) mTelephonyCallbackWeakRef.get();
1676             if (listener == null) return;
1677 
1678             Binder.withCleanCallingIdentity(
1679                     () -> mExecutor.execute(() -> listener.onCallForwardingIndicatorChanged(cfi)));
1680         }
1681 
onCellLocationChanged(CellIdentity cellIdentity)1682         public void onCellLocationChanged(CellIdentity cellIdentity) {
1683             // There is no system/public API to create an CellIdentity in system server,
1684             // so the server pass a null to indicate an empty initial location.
1685             CellLocation location =
1686                     cellIdentity == null ? CellLocation.getEmpty() : cellIdentity.asCellLocation();
1687             CellLocationListener listener = (CellLocationListener) mTelephonyCallbackWeakRef.get();
1688             if (listener == null) return;
1689 
1690             Binder.withCleanCallingIdentity(
1691                     () -> mExecutor.execute(() -> listener.onCellLocationChanged(location)));
1692         }
1693 
onLegacyCallStateChanged(int state, String incomingNumber)1694         public void onLegacyCallStateChanged(int state, String incomingNumber) {
1695             // Not used for TelephonyCallback; part of the AIDL which is used by both the legacy
1696             // PhoneStateListener and TelephonyCallback.
1697         }
1698 
onCallStateChanged(int state)1699         public void onCallStateChanged(int state) {
1700             CallStateListener listener = (CallStateListener) mTelephonyCallbackWeakRef.get();
1701             if (listener == null) return;
1702 
1703             Binder.withCleanCallingIdentity(
1704                     () -> mExecutor.execute(() -> listener.onCallStateChanged(state)));
1705         }
1706 
onDataConnectionStateChanged(int state, int networkType)1707         public void onDataConnectionStateChanged(int state, int networkType) {
1708             DataConnectionStateListener listener =
1709                     (DataConnectionStateListener) mTelephonyCallbackWeakRef.get();
1710             if (listener == null) return;
1711 
1712             if (state == TelephonyManager.DATA_DISCONNECTING
1713                     && VMRuntime.getRuntime().getTargetSdkVersion() < Build.VERSION_CODES.R) {
1714                 Binder.withCleanCallingIdentity(
1715                         () -> mExecutor.execute(() ->
1716                                 listener.onDataConnectionStateChanged(
1717                                         TelephonyManager.DATA_CONNECTED, networkType)));
1718             } else {
1719                 Binder.withCleanCallingIdentity(
1720                         () -> mExecutor.execute(() ->
1721                                 listener.onDataConnectionStateChanged(state, networkType)));
1722             }
1723         }
1724 
onDataActivity(int direction)1725         public void onDataActivity(int direction) {
1726             DataActivityListener listener = (DataActivityListener) mTelephonyCallbackWeakRef.get();
1727             if (listener == null) return;
1728 
1729             Binder.withCleanCallingIdentity(
1730                     () -> mExecutor.execute(() -> listener.onDataActivity(direction)));
1731         }
1732 
onSignalStrengthsChanged(SignalStrength signalStrength)1733         public void onSignalStrengthsChanged(SignalStrength signalStrength) {
1734             SignalStrengthsListener listener =
1735                     (SignalStrengthsListener) mTelephonyCallbackWeakRef.get();
1736             if (listener == null) return;
1737 
1738             Binder.withCleanCallingIdentity(
1739                     () -> mExecutor.execute(() -> listener.onSignalStrengthsChanged(
1740                             signalStrength)));
1741         }
1742 
onCellInfoChanged(List<CellInfo> cellInfo)1743         public void onCellInfoChanged(List<CellInfo> cellInfo) {
1744             CellInfoListener listener = (CellInfoListener) mTelephonyCallbackWeakRef.get();
1745             if (listener == null) return;
1746 
1747             Binder.withCleanCallingIdentity(
1748                     () -> mExecutor.execute(() -> listener.onCellInfoChanged(cellInfo)));
1749         }
1750 
onPreciseCallStateChanged(PreciseCallState callState)1751         public void onPreciseCallStateChanged(PreciseCallState callState) {
1752             PreciseCallStateListener listener =
1753                     (PreciseCallStateListener) mTelephonyCallbackWeakRef.get();
1754             if (listener == null) return;
1755 
1756             Binder.withCleanCallingIdentity(
1757                     () -> mExecutor.execute(() -> listener.onPreciseCallStateChanged(callState)));
1758         }
1759 
onCallDisconnectCauseChanged(int disconnectCause, int preciseDisconnectCause)1760         public void onCallDisconnectCauseChanged(int disconnectCause, int preciseDisconnectCause) {
1761             CallDisconnectCauseListener listener =
1762                     (CallDisconnectCauseListener) mTelephonyCallbackWeakRef.get();
1763             if (listener == null) return;
1764 
1765             Binder.withCleanCallingIdentity(
1766                     () -> mExecutor.execute(() -> listener.onCallDisconnectCauseChanged(
1767                             disconnectCause, preciseDisconnectCause)));
1768         }
1769 
onPreciseDataConnectionStateChanged( PreciseDataConnectionState dataConnectionState)1770         public void onPreciseDataConnectionStateChanged(
1771                 PreciseDataConnectionState dataConnectionState) {
1772             PreciseDataConnectionStateListener listener =
1773                     (PreciseDataConnectionStateListener) mTelephonyCallbackWeakRef.get();
1774             if (listener == null) return;
1775 
1776             Binder.withCleanCallingIdentity(
1777                     () -> mExecutor.execute(
1778                             () -> listener.onPreciseDataConnectionStateChanged(
1779                                     dataConnectionState)));
1780         }
1781 
onDataConnectionRealTimeInfoChanged(DataConnectionRealTimeInfo dcRtInfo)1782         public void onDataConnectionRealTimeInfoChanged(DataConnectionRealTimeInfo dcRtInfo) {
1783             // default implementation empty
1784         }
1785 
onSrvccStateChanged(int state)1786         public void onSrvccStateChanged(int state) {
1787             SrvccStateListener listener = (SrvccStateListener) mTelephonyCallbackWeakRef.get();
1788             if (listener == null) return;
1789 
1790             Binder.withCleanCallingIdentity(
1791                     () -> mExecutor.execute(() -> listener.onSrvccStateChanged(state)));
1792         }
1793 
onVoiceActivationStateChanged(int activationState)1794         public void onVoiceActivationStateChanged(int activationState) {
1795             VoiceActivationStateListener listener =
1796                     (VoiceActivationStateListener) mTelephonyCallbackWeakRef.get();
1797             if (listener == null) return;
1798 
1799             Binder.withCleanCallingIdentity(
1800                     () -> mExecutor.execute(
1801                             () -> listener.onVoiceActivationStateChanged(activationState)));
1802         }
1803 
onDataActivationStateChanged(int activationState)1804         public void onDataActivationStateChanged(int activationState) {
1805             DataActivationStateListener listener =
1806                     (DataActivationStateListener) mTelephonyCallbackWeakRef.get();
1807             if (listener == null) return;
1808 
1809             Binder.withCleanCallingIdentity(
1810                     () -> mExecutor.execute(
1811                             () -> listener.onDataActivationStateChanged(activationState)));
1812         }
1813 
onUserMobileDataStateChanged(boolean enabled)1814         public void onUserMobileDataStateChanged(boolean enabled) {
1815             UserMobileDataStateListener listener =
1816                     (UserMobileDataStateListener) mTelephonyCallbackWeakRef.get();
1817             if (listener == null) return;
1818 
1819             Binder.withCleanCallingIdentity(
1820                     () -> mExecutor.execute(
1821                             () -> listener.onUserMobileDataStateChanged(enabled)));
1822         }
1823 
onDisplayInfoChanged(TelephonyDisplayInfo telephonyDisplayInfo)1824         public void onDisplayInfoChanged(TelephonyDisplayInfo telephonyDisplayInfo) {
1825             DisplayInfoListener listener = (DisplayInfoListener)mTelephonyCallbackWeakRef.get();
1826             if (listener == null) return;
1827 
1828             Binder.withCleanCallingIdentity(
1829                     () -> mExecutor.execute(
1830                             () -> listener.onDisplayInfoChanged(telephonyDisplayInfo)));
1831         }
1832 
onOemHookRawEvent(byte[] rawData)1833         public void onOemHookRawEvent(byte[] rawData) {
1834             // default implementation empty
1835         }
1836 
onCarrierNetworkChange(boolean active)1837         public void onCarrierNetworkChange(boolean active) {
1838             CarrierNetworkListener listener =
1839                     (CarrierNetworkListener) mTelephonyCallbackWeakRef.get();
1840             if (listener == null) return;
1841 
1842             Binder.withCleanCallingIdentity(
1843                     () -> mExecutor.execute(() -> listener.onCarrierNetworkChange(active)));
1844         }
1845 
onEmergencyNumberListChanged(Map emergencyNumberList)1846         public void onEmergencyNumberListChanged(Map emergencyNumberList) {
1847             EmergencyNumberListListener listener =
1848                     (EmergencyNumberListListener) mTelephonyCallbackWeakRef.get();
1849             if (listener == null) return;
1850 
1851             Binder.withCleanCallingIdentity(
1852                     () -> mExecutor.execute(
1853                             () -> listener.onEmergencyNumberListChanged(emergencyNumberList)));
1854         }
1855 
onOutgoingEmergencyCall(@onNull EmergencyNumber placedEmergencyNumber, int subscriptionId)1856         public void onOutgoingEmergencyCall(@NonNull EmergencyNumber placedEmergencyNumber,
1857             int subscriptionId) {
1858             OutgoingEmergencyCallListener listener =
1859                     (OutgoingEmergencyCallListener) mTelephonyCallbackWeakRef.get();
1860             if (listener == null) return;
1861 
1862             Binder.withCleanCallingIdentity(
1863                     () -> mExecutor.execute(
1864                             () -> listener.onOutgoingEmergencyCall(placedEmergencyNumber,
1865                                     subscriptionId)));
1866         }
1867 
onOutgoingEmergencySms(@onNull EmergencyNumber sentEmergencyNumber, int subscriptionId)1868         public void onOutgoingEmergencySms(@NonNull EmergencyNumber sentEmergencyNumber,
1869             int subscriptionId) {
1870             OutgoingEmergencySmsListener listener =
1871                     (OutgoingEmergencySmsListener) mTelephonyCallbackWeakRef.get();
1872             if (listener == null) return;
1873 
1874             Binder.withCleanCallingIdentity(
1875                     () -> mExecutor.execute(
1876                             () -> listener.onOutgoingEmergencySms(sentEmergencyNumber,
1877                                     subscriptionId)));
1878         }
1879 
onPhoneCapabilityChanged(PhoneCapability capability)1880         public void onPhoneCapabilityChanged(PhoneCapability capability) {
1881             PhoneCapabilityListener listener =
1882                     (PhoneCapabilityListener) mTelephonyCallbackWeakRef.get();
1883             if (listener == null) return;
1884 
1885             Binder.withCleanCallingIdentity(
1886                     () -> mExecutor.execute(() -> listener.onPhoneCapabilityChanged(capability)));
1887         }
1888 
onRadioPowerStateChanged(@nnotation.RadioPowerState int state)1889         public void onRadioPowerStateChanged(@Annotation.RadioPowerState int state) {
1890             RadioPowerStateListener listener =
1891                     (RadioPowerStateListener) mTelephonyCallbackWeakRef.get();
1892             if (listener == null) return;
1893 
1894             Binder.withCleanCallingIdentity(
1895                     () -> mExecutor.execute(() -> listener.onRadioPowerStateChanged(state)));
1896         }
1897 
onCallStatesChanged(List<CallState> callStateList)1898         public void onCallStatesChanged(List<CallState> callStateList) {
1899             CallAttributesListener listener =
1900                     (CallAttributesListener) mTelephonyCallbackWeakRef.get();
1901             if (listener == null) return;
1902 
1903             Binder.withCleanCallingIdentity(
1904                     () -> mExecutor.execute(() -> listener.onCallStatesChanged(callStateList)));
1905         }
1906 
onActiveDataSubIdChanged(int subId)1907         public void onActiveDataSubIdChanged(int subId) {
1908             ActiveDataSubscriptionIdListener listener =
1909                     (ActiveDataSubscriptionIdListener) mTelephonyCallbackWeakRef.get();
1910             if (listener == null) return;
1911 
1912             Binder.withCleanCallingIdentity(
1913                     () -> mExecutor.execute(() -> listener.onActiveDataSubscriptionIdChanged(
1914                             subId)));
1915         }
1916 
onImsCallDisconnectCauseChanged(ImsReasonInfo disconnectCause)1917         public void onImsCallDisconnectCauseChanged(ImsReasonInfo disconnectCause) {
1918             ImsCallDisconnectCauseListener listener =
1919                     (ImsCallDisconnectCauseListener) mTelephonyCallbackWeakRef.get();
1920             if (listener == null) return;
1921 
1922             Binder.withCleanCallingIdentity(
1923                     () -> mExecutor.execute(
1924                             () -> listener.onImsCallDisconnectCauseChanged(disconnectCause)));
1925         }
1926 
onRegistrationFailed(@onNull CellIdentity cellIdentity, @NonNull String chosenPlmn, int domain, int causeCode, int additionalCauseCode)1927         public void onRegistrationFailed(@NonNull CellIdentity cellIdentity,
1928             @NonNull String chosenPlmn, int domain, int causeCode, int additionalCauseCode) {
1929             RegistrationFailedListener listener =
1930                     (RegistrationFailedListener) mTelephonyCallbackWeakRef.get();
1931             if (listener == null) return;
1932 
1933             Binder.withCleanCallingIdentity(
1934                     () -> mExecutor.execute(() -> listener.onRegistrationFailed(
1935                             cellIdentity, chosenPlmn, domain, causeCode, additionalCauseCode)));
1936             // default implementation empty
1937         }
1938 
onBarringInfoChanged(BarringInfo barringInfo)1939         public void onBarringInfoChanged(BarringInfo barringInfo) {
1940             BarringInfoListener listener = (BarringInfoListener) mTelephonyCallbackWeakRef.get();
1941             if (listener == null) return;
1942 
1943             Binder.withCleanCallingIdentity(
1944                     () -> mExecutor.execute(() -> listener.onBarringInfoChanged(barringInfo)));
1945         }
1946 
onPhysicalChannelConfigChanged(List<PhysicalChannelConfig> configs)1947         public void onPhysicalChannelConfigChanged(List<PhysicalChannelConfig> configs) {
1948             PhysicalChannelConfigListener listener =
1949                     (PhysicalChannelConfigListener) mTelephonyCallbackWeakRef.get();
1950             if (listener == null) return;
1951 
1952             Binder.withCleanCallingIdentity(
1953                     () -> mExecutor.execute(() -> listener.onPhysicalChannelConfigChanged(
1954                             configs)));
1955         }
1956 
onDataEnabledChanged(boolean enabled, @TelephonyManager.DataEnabledReason int reason)1957         public void onDataEnabledChanged(boolean enabled,
1958             @TelephonyManager.DataEnabledReason int reason) {
1959             DataEnabledListener listener =
1960                     (DataEnabledListener) mTelephonyCallbackWeakRef.get();
1961             if (listener == null) return;
1962 
1963             Binder.withCleanCallingIdentity(
1964                     () -> mExecutor.execute(() -> listener.onDataEnabledChanged(
1965                             enabled, reason)));
1966         }
1967 
onAllowedNetworkTypesChanged(int reason, long allowedNetworkType)1968         public void onAllowedNetworkTypesChanged(int reason, long allowedNetworkType) {
1969             AllowedNetworkTypesListener listener =
1970                     (AllowedNetworkTypesListener) mTelephonyCallbackWeakRef.get();
1971             if (listener == null) return;
1972 
1973             Binder.withCleanCallingIdentity(
1974                     () -> mExecutor.execute(
1975                             () -> listener.onAllowedNetworkTypesChanged(reason,
1976                                     allowedNetworkType)));
1977         }
1978 
onLinkCapacityEstimateChanged( List<LinkCapacityEstimate> linkCapacityEstimateList)1979         public void onLinkCapacityEstimateChanged(
1980                 List<LinkCapacityEstimate> linkCapacityEstimateList) {
1981             LinkCapacityEstimateChangedListener listener =
1982                     (LinkCapacityEstimateChangedListener) mTelephonyCallbackWeakRef.get();
1983             if (listener == null) return;
1984 
1985             Binder.withCleanCallingIdentity(
1986                     () -> mExecutor.execute(() -> listener.onLinkCapacityEstimateChanged(
1987                             linkCapacityEstimateList)));
1988         }
1989 
onMediaQualityStatusChanged( MediaQualityStatus mediaQualityStatus)1990         public void onMediaQualityStatusChanged(
1991                 MediaQualityStatus mediaQualityStatus) {
1992             MediaQualityStatusChangedListener listener =
1993                     (MediaQualityStatusChangedListener) mTelephonyCallbackWeakRef.get();
1994             if (listener == null) return;
1995 
1996             Binder.withCleanCallingIdentity(
1997                     () -> mExecutor.execute(() -> listener.onMediaQualityStatusChanged(
1998                             mediaQualityStatus)));
1999         }
2000 
onCallBackModeStarted(@elephonyManager.EmergencyCallbackModeType int type)2001         public void onCallBackModeStarted(@TelephonyManager.EmergencyCallbackModeType int type) {
2002             EmergencyCallbackModeListener listener =
2003                     (EmergencyCallbackModeListener) mTelephonyCallbackWeakRef.get();
2004             Log.d(LOG_TAG, "onCallBackModeStarted:type=" + type + ", listener=" + listener);
2005             if (listener == null) return;
2006 
2007             Binder.withCleanCallingIdentity(
2008                     () -> mExecutor.execute(() -> listener.onCallBackModeStarted(type)));
2009         }
2010 
onCallBackModeStopped(@elephonyManager.EmergencyCallbackModeType int type, @TelephonyManager.EmergencyCallbackModeStopReason int reason)2011         public void onCallBackModeStopped(@TelephonyManager.EmergencyCallbackModeType int type,
2012                 @TelephonyManager.EmergencyCallbackModeStopReason int reason) {
2013             EmergencyCallbackModeListener listener =
2014                     (EmergencyCallbackModeListener) mTelephonyCallbackWeakRef.get();
2015             Log.d(LOG_TAG, "onCallBackModeStopped:type=" + type
2016                     + ", reason=" + reason + ", listener=" + listener);
2017             if (listener == null) return;
2018 
2019             Binder.withCleanCallingIdentity(
2020                     () -> mExecutor.execute(() -> listener.onCallBackModeStopped(type, reason)));
2021         }
2022     }
2023 }
2024