1 /*
2  * Copyright (C) 2008 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.NonNull;
21 import android.annotation.RequiresPermission;
22 import android.annotation.SystemApi;
23 import android.annotation.TestApi;
24 import android.compat.annotation.ChangeId;
25 import android.compat.annotation.UnsupportedAppUsage;
26 import android.os.Binder;
27 import android.os.Build;
28 import android.os.Handler;
29 import android.os.HandlerExecutor;
30 import android.os.Looper;
31 import android.telephony.Annotation.CallState;
32 import android.telephony.Annotation.RadioPowerState;
33 import android.telephony.Annotation.SimActivationState;
34 import android.telephony.Annotation.SrvccState;
35 import android.telephony.emergency.EmergencyNumber;
36 import android.telephony.ims.ImsReasonInfo;
37 
38 import com.android.internal.annotations.VisibleForTesting;
39 import com.android.internal.telephony.IPhoneStateListener;
40 
41 import dalvik.system.VMRuntime;
42 
43 import java.lang.ref.WeakReference;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.concurrent.Executor;
47 
48 /**
49  * A listener class for monitoring changes in specific telephony states
50  * on the device, including service state, signal strength, message
51  * waiting indicator (voicemail), and others.
52  * <p>
53  * Override the methods for the state that you wish to receive updates for, and
54  * pass your PhoneStateListener object, along with bitwise-or of the LISTEN_
55  * flags to {@link TelephonyManager#listen TelephonyManager.listen()}. Methods are
56  * called when the state changes, as well as once on initial registration.
57  * <p>
58  * Note that access to some telephony information is
59  * permission-protected. Your application won't receive updates for protected
60  * information unless it has the appropriate permissions declared in
61  * its manifest file. Where permissions apply, they are noted in the
62  * appropriate LISTEN_ flags.
63  */
64 public class PhoneStateListener {
65     private static final String LOG_TAG = "PhoneStateListener";
66     private static final boolean DBG = false; // STOPSHIP if true
67 
68     /**
69      * Experiment flag to set the per-pid registration limit for PhoneStateListeners
70      *
71      * Limit on registrations of {@link PhoneStateListener}s on a per-pid
72      * basis. When this limit is exceeded, any calls to {@link TelephonyManager#listen} will fail
73      * with an {@link IllegalStateException}.
74      *
75      * {@link android.os.Process#PHONE_UID}, {@link android.os.Process#SYSTEM_UID}, and the uid that
76      * TelephonyRegistry runs under are exempt from this limit.
77      *
78      * If the value of the flag is less than 1, enforcement of the limit will be disabled.
79      * @hide
80      */
81     public static final String FLAG_PER_PID_REGISTRATION_LIMIT =
82             "phone_state_listener_per_pid_registration_limit";
83 
84     /**
85      * Default value for the per-pid registation limit.
86      * See {@link #FLAG_PER_PID_REGISTRATION_LIMIT}.
87      * @hide
88      */
89     public static final int DEFAULT_PER_PID_REGISTRATION_LIMIT = 50;
90 
91     /**
92      * This change enables a limit on the number of {@link PhoneStateListener} objects any process
93      * may register via {@link TelephonyManager#listen}. The default limit is 50, which may change
94      * via remote device config updates.
95      *
96      * This limit is enforced via an {@link IllegalStateException} thrown from
97      * {@link TelephonyManager#listen} when the offending process attempts to register one too many
98      * listeners.
99      *
100      * @hide
101      */
102     @ChangeId
103     public static final long PHONE_STATE_LISTENER_LIMIT_CHANGE_ID = 150880553L;
104 
105     /**
106      * Stop listening for updates.
107      *
108      * The PhoneStateListener is not tied to any subscription and unregistered for any update.
109      */
110     public static final int LISTEN_NONE = 0;
111 
112     /**
113      *  Listen for changes to the network service state (cellular).
114      *
115      *  @see #onServiceStateChanged
116      *  @see ServiceState
117      */
118     public static final int LISTEN_SERVICE_STATE                            = 0x00000001;
119 
120     /**
121      * Listen for changes to the network signal strength (cellular).
122      * {@more}
123      *
124      * @see #onSignalStrengthChanged
125      *
126      * @deprecated by {@link #LISTEN_SIGNAL_STRENGTHS}
127      */
128     @Deprecated
129     public static final int LISTEN_SIGNAL_STRENGTH                          = 0x00000002;
130 
131     /**
132      * Listen for changes to the message-waiting indicator.
133      * {@more}
134      * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE
135      * READ_PHONE_STATE} or that the calling app has carrier privileges (see
136      * {@link TelephonyManager#hasCarrierPrivileges}).
137      * <p>
138      * Example: The status bar uses this to determine when to display the
139      * voicemail icon.
140      *
141      * @see #onMessageWaitingIndicatorChanged
142      */
143     public static final int LISTEN_MESSAGE_WAITING_INDICATOR                = 0x00000004;
144 
145     /**
146      * Listen for changes to the call-forwarding indicator.
147      * {@more}
148      * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE
149      * READ_PHONE_STATE} or that the calling app has carrier privileges (see
150      * {@link TelephonyManager#hasCarrierPrivileges}).
151      *
152      * @see #onCallForwardingIndicatorChanged
153      */
154     public static final int LISTEN_CALL_FORWARDING_INDICATOR                = 0x00000008;
155 
156     /**
157      * Listen for changes to the device's cell location. Note that
158      * this will result in frequent callbacks to the listener.
159      * {@more}
160      * Requires Permission: {@link android.Manifest.permission#ACCESS_FINE_LOCATION
161      * ACCESS_FINE_LOCATION}
162      * <p>
163      * If you need regular location updates but want more control over
164      * the update interval or location precision, you can set up a listener
165      * through the {@link android.location.LocationManager location manager}
166      * instead.
167      *
168      * @see #onCellLocationChanged
169      */
170     public static final int LISTEN_CELL_LOCATION                            = 0x00000010;
171 
172     /**
173      * Listen for changes to the device call state.
174      * {@more}
175      *
176      * @see #onCallStateChanged
177      */
178     public static final int LISTEN_CALL_STATE                               = 0x00000020;
179 
180     /**
181      * Listen for changes to the data connection state (cellular).
182      *
183      * @see #onDataConnectionStateChanged
184      */
185     public static final int LISTEN_DATA_CONNECTION_STATE                    = 0x00000040;
186 
187     /**
188      * Listen for changes to the direction of data traffic on the data
189      * connection (cellular).
190      * {@more}
191      * Example: The status bar uses this to display the appropriate
192      * data-traffic icon.
193      *
194      * @see #onDataActivity
195      */
196     public static final int LISTEN_DATA_ACTIVITY                            = 0x00000080;
197 
198     /**
199      * Listen for changes to the network signal strengths (cellular).
200      * <p>
201      * Example: The status bar uses this to control the signal-strength
202      * icon.
203      *
204      * @see #onSignalStrengthsChanged
205      */
206     public static final int LISTEN_SIGNAL_STRENGTHS                         = 0x00000100;
207 
208     /**
209      * Listen for changes of the network signal strengths (cellular) always reported from modem,
210      * even in some situations such as the screen of the device is off.
211      *
212      * @see #onSignalStrengthsChanged
213      *
214      * @hide
215      */
216     @RequiresPermission(android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH)
217     public static final int LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH          = 0x00000200;
218 
219     /**
220      * Listen for changes to observed cell info.
221      *
222      * Listening to this event requires the {@link Manifest.permission#ACCESS_FINE_LOCATION}
223      * permission.
224      *
225      * @see #onCellInfoChanged
226      */
227     public static final int LISTEN_CELL_INFO = 0x00000400;
228 
229     /**
230      * Listen for {@link android.telephony.Annotation.PreciseCallStates} of ringing,
231      * background and foreground calls.
232      *
233      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
234      * or the calling app has carrier privileges
235      * (see {@link TelephonyManager#hasCarrierPrivileges}).
236      *
237      * @hide
238      */
239     @RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
240     @SystemApi
241     public static final int LISTEN_PRECISE_CALL_STATE                       = 0x00000800;
242 
243     /**
244      * Listen for {@link PreciseDataConnectionState} on the data connection (cellular).
245      *
246      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
247      * or the calling app has carrier privileges
248      * (see {@link TelephonyManager#hasCarrierPrivileges}).
249      *
250      * @see #onPreciseDataConnectionStateChanged
251      */
252     @RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
253     public static final int LISTEN_PRECISE_DATA_CONNECTION_STATE            = 0x00001000;
254 
255     /**
256      * Listen for real time info for all data connections (cellular)).
257      * {@more}
258      * Requires Permission: {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE
259      * READ_PRECISE_PHONE_STATE}
260      * @see #onDataConnectionRealTimeInfoChanged(DataConnectionRealTimeInfo)
261      *
262      * @deprecated Use {@link TelephonyManager#getModemActivityInfo()}
263      * @hide
264      */
265     @Deprecated
266     public static final int LISTEN_DATA_CONNECTION_REAL_TIME_INFO           = 0x00002000;
267 
268     /**
269      * Listen for changes to the SRVCC state of the active call.
270      *
271      * <p>Requires permission {@link android.Manifest.permission#READ_PRIVILEGED_PHONE_STATE}
272      *
273      * @see #onServiceStateChanged(ServiceState)
274      * @hide
275      */
276     @SystemApi
277     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
278     public static final int LISTEN_SRVCC_STATE_CHANGED                     = 0x00004000;
279 
280     /**
281      * Listen for OEM hook raw event
282      *
283      * @see #onOemHookRawEvent
284      * @hide
285      * @deprecated OEM needs a vendor-extension hal and their apps should use that instead
286      */
287     @Deprecated
288     public static final int LISTEN_OEM_HOOK_RAW_EVENT                       = 0x00008000;
289 
290     /**
291      * Listen for carrier network changes indicated by a carrier app.
292      *
293      * @see #onCarrierNetworkRequest
294      * @see TelephonyManager#notifyCarrierNetworkChange(boolean)
295      * @hide
296      */
297     public static final int LISTEN_CARRIER_NETWORK_CHANGE                   = 0x00010000;
298 
299     /**
300      * Listen for changes to the sim voice activation state
301      *
302      * <p>Requires permission {@link android.Manifest.permission#READ_PRIVILEGED_PHONE_STATE}
303      *
304      * @see TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATING
305      * @see TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATED
306      * @see TelephonyManager#SIM_ACTIVATION_STATE_DEACTIVATED
307      * @see TelephonyManager#SIM_ACTIVATION_STATE_RESTRICTED
308      * @see TelephonyManager#SIM_ACTIVATION_STATE_UNKNOWN
309      * {@more}
310      * Example: TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATED indicates voice service has been
311      * fully activated
312      *
313      * @see #onVoiceActivationStateChanged
314      * @hide
315      */
316     @SystemApi
317     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
318     public static final int LISTEN_VOICE_ACTIVATION_STATE                   = 0x00020000;
319 
320     /**
321      * Listen for changes to the sim data activation state
322      * @see TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATING
323      * @see TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATED
324      * @see TelephonyManager#SIM_ACTIVATION_STATE_DEACTIVATED
325      * @see TelephonyManager#SIM_ACTIVATION_STATE_RESTRICTED
326      * @see TelephonyManager#SIM_ACTIVATION_STATE_UNKNOWN
327      * {@more}
328      * Example: TelephonyManager#SIM_ACTIVATION_STATE_ACTIVATED indicates data service has been
329      * fully activated
330      *
331      * @see #onDataActivationStateChanged
332      * @hide
333      */
334     public static final int LISTEN_DATA_ACTIVATION_STATE                   = 0x00040000;
335 
336     /**
337      *  Listen for changes to the user mobile data state
338      *
339      *  @see #onUserMobileDataStateChanged
340      */
341     public static final int LISTEN_USER_MOBILE_DATA_STATE                  = 0x00080000;
342 
343     /**
344      *  Listen for display info changed event.
345      *
346      *  Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE
347      *  READ_PHONE_STATE} or that the calling app has carrier privileges (see
348      *  {@link TelephonyManager#hasCarrierPrivileges}).
349      *
350      *  @see #onDisplayInfoChanged
351      */
352     public static final int LISTEN_DISPLAY_INFO_CHANGED = 0x00100000;
353 
354     /**
355      *  Listen for changes to the phone capability.
356      *
357      *  @see #onPhoneCapabilityChanged
358      *  @hide
359      */
360     public static final int LISTEN_PHONE_CAPABILITY_CHANGE                 = 0x00200000;
361 
362     /**
363      *  Listen for changes to active data subId. Active data subscription is
364      *  the current subscription used to setup Cellular Internet data. For example,
365      *  it could be the current active opportunistic subscription in use, or the
366      *  subscription user selected as default data subscription in DSDS mode.
367      *
368      *  @see #onActiveDataSubscriptionIdChanged
369      */
370     public static final int LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE = 0x00400000;
371 
372     /**
373      *  Listen for changes to the radio power state.
374      *
375      * <p>Requires permission {@link android.Manifest.permission#READ_PRIVILEGED_PHONE_STATE}
376      *
377      *  @see #onRadioPowerStateChanged
378      *  @hide
379      */
380     @SystemApi
381     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
382     public static final int LISTEN_RADIO_POWER_STATE_CHANGED               = 0x00800000;
383 
384     /**
385      * Listen for changes to emergency number list based on all active subscriptions.
386      *
387      * <p>Requires permission {@link android.Manifest.permission#READ_PHONE_STATE} or the calling
388      * app has carrier privileges (see {@link TelephonyManager#hasCarrierPrivileges}).
389      */
390     public static final int LISTEN_EMERGENCY_NUMBER_LIST                   = 0x01000000;
391 
392     /**
393      * Listen for call disconnect causes which contains {@link DisconnectCause} and
394      * {@link PreciseDisconnectCause}.
395      *
396      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
397      * or the calling app has carrier privileges
398      * (see {@link TelephonyManager#hasCarrierPrivileges}).
399      *
400      */
401     @RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
402     public static final int LISTEN_CALL_DISCONNECT_CAUSES                  = 0x02000000;
403 
404     /**
405      * Listen for changes to the call attributes of a currently active call.
406      *
407      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
408      * or the calling app has carrier privileges
409      * (see {@link TelephonyManager#hasCarrierPrivileges}).
410      *
411      * @see #onCallAttributesChanged
412      * @hide
413      */
414     @SystemApi
415     @RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
416     public static final int LISTEN_CALL_ATTRIBUTES_CHANGED                 = 0x04000000;
417 
418     /**
419      * Listen for IMS call disconnect causes which contains
420      * {@link android.telephony.ims.ImsReasonInfo}
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      * @see #onImsCallDisconnectCauseChanged(ImsReasonInfo)
427      */
428     @RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
429     public static final int LISTEN_IMS_CALL_DISCONNECT_CAUSES              = 0x08000000;
430 
431     /**
432      * Listen for the emergency number placed from an outgoing call.
433      *
434      * <p>Requires permission {@link android.Manifest.permission#READ_ACTIVE_EMERGENCY_SESSION}
435      *
436      * @see #onOutgoingEmergencyCall
437      * @hide
438      */
439     @SystemApi
440     @TestApi
441     @RequiresPermission(Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION)
442     public static final int LISTEN_OUTGOING_EMERGENCY_CALL                  = 0x10000000;
443 
444     /**
445      * Listen for the emergency number placed from an outgoing SMS.
446      *
447      * <p>Requires permission {@link android.Manifest.permission#READ_ACTIVE_EMERGENCY_SESSION}
448      *
449      * @see #onOutgoingEmergencySms
450      * @hide
451      */
452     @SystemApi
453     @TestApi
454     @RequiresPermission(Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION)
455     public static final int LISTEN_OUTGOING_EMERGENCY_SMS                   = 0x20000000;
456 
457     /**
458      * Listen for Registration Failures.
459      *
460      * Listen for indications that a registration procedure has failed in either the CS or PS
461      * domain. This indication does not necessarily indicate a change of service state, which should
462      * be tracked via {@link #LISTEN_SERVICE_STATE}.
463      *
464      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE} or
465      * the calling app has carrier privileges (see {@link TelephonyManager#hasCarrierPrivileges}).
466      *
467      * <p>Also requires the {@link Manifest.permission#ACCESS_FINE_LOCATION} permission, regardless
468      * of whether the calling app has carrier privileges.
469      *
470      * @see #onRegistrationFailed
471      */
472     @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
473     public static final int LISTEN_REGISTRATION_FAILURE = 0x40000000;
474 
475     /**
476      * Listen for Barring Information for the current registered / camped cell.
477      *
478      * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE} or
479      * the calling app has carrier privileges (see {@link TelephonyManager#hasCarrierPrivileges}).
480      *
481      * <p>Also requires the {@link Manifest.permission#ACCESS_FINE_LOCATION} permission, regardless
482      * of whether the calling app has carrier privileges.
483      *
484      * @see #onBarringInfoChanged
485      */
486     @RequiresPermission(Manifest.permission.READ_PRECISE_PHONE_STATE)
487     public static final int LISTEN_BARRING_INFO = 0x80000000;
488 
489     /*
490      * Subscription used to listen to the phone state changes
491      * @hide
492      */
493     /** @hide */
494     @UnsupportedAppUsage
495     protected Integer mSubId;
496 
497     /**
498      * @hide
499      */
500     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
501     @UnsupportedAppUsage
502     public final IPhoneStateListener callback;
503 
504     /**
505      * Create a PhoneStateListener for the Phone with the default subscription.
506      * This class requires Looper.myLooper() not return null.
507      */
PhoneStateListener()508     public PhoneStateListener() {
509         this(null, Looper.myLooper());
510     }
511 
512     /**
513      * Create a PhoneStateListener for the Phone with the default subscription
514      * using a particular non-null Looper.
515      * @hide
516      */
517     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
PhoneStateListener(Looper looper)518     public PhoneStateListener(Looper looper) {
519         this(null, looper);
520     }
521 
522     /**
523      * Create a PhoneStateListener for the Phone using the specified subscription.
524      * This class requires Looper.myLooper() not return null. To supply your
525      * own non-null Looper use PhoneStateListener(int subId, Looper looper) below.
526      * @hide
527      */
528     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
PhoneStateListener(Integer subId)529     public PhoneStateListener(Integer subId) {
530         this(subId, Looper.myLooper());
531         if (subId != null && VMRuntime.getRuntime().getTargetSdkVersion()
532                 >= Build.VERSION_CODES.Q) {
533             throw new IllegalArgumentException("PhoneStateListener with subId: "
534                     + subId + " is not supported, use default constructor");
535         }
536     }
537     /**
538      * Create a PhoneStateListener for the Phone using the specified subscription
539      * and non-null Looper.
540      * @hide
541      */
542     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
PhoneStateListener(Integer subId, Looper looper)543     public PhoneStateListener(Integer subId, Looper looper) {
544         this(subId, new HandlerExecutor(new Handler(looper)));
545         if (subId != null && VMRuntime.getRuntime().getTargetSdkVersion()
546                 >= Build.VERSION_CODES.Q) {
547             throw new IllegalArgumentException("PhoneStateListener with subId: "
548                     + subId + " is not supported, use default constructor");
549         }
550     }
551 
552     /**
553      * Create a PhoneStateListener for the Phone using the specified Executor
554      *
555      * <p>Create a PhoneStateListener with a specified Executor for handling necessary callbacks.
556      * The Executor must not be null.
557      *
558      * @param executor a non-null Executor that will execute callbacks for the PhoneStateListener.
559      */
PhoneStateListener(@onNull Executor executor)560     public PhoneStateListener(@NonNull Executor executor) {
561         this(null, executor);
562     }
563 
PhoneStateListener(Integer subId, Executor e)564     private PhoneStateListener(Integer subId, Executor e) {
565         if (e == null) {
566             throw new IllegalArgumentException("PhoneStateListener Executor must be non-null");
567         }
568         mSubId = subId;
569         callback = new IPhoneStateListenerStub(this, e);
570     }
571 
572     /**
573      * Callback invoked when device service state changes on the registered subscription.
574      * Note, the registration subId comes from {@link TelephonyManager} object which registers
575      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
576      * If this TelephonyManager object was created with
577      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
578      * subId. Otherwise, this callback applies to
579      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
580      *
581      * The instance of {@link ServiceState} passed as an argument here will have various levels of
582      * location information stripped from it depending on the location permissions that your app
583      * holds. Only apps holding the {@link Manifest.permission#ACCESS_FINE_LOCATION} permission will
584      * receive all the information in {@link ServiceState}.
585      *
586      * @see ServiceState#STATE_EMERGENCY_ONLY
587      * @see ServiceState#STATE_IN_SERVICE
588      * @see ServiceState#STATE_OUT_OF_SERVICE
589      * @see ServiceState#STATE_POWER_OFF
590      */
onServiceStateChanged(ServiceState serviceState)591     public void onServiceStateChanged(ServiceState serviceState) {
592         // default implementation empty
593     }
594 
595     /**
596      * Callback invoked when network signal strength changes on the registered subscription.
597      * Note, the registration subId comes from {@link TelephonyManager} object which registers
598      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
599      * If this TelephonyManager object was created with
600      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
601      * subId. Otherwise, this callback applies to
602      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
603      *
604      * @see ServiceState#STATE_EMERGENCY_ONLY
605      * @see ServiceState#STATE_IN_SERVICE
606      * @see ServiceState#STATE_OUT_OF_SERVICE
607      * @see ServiceState#STATE_POWER_OFF
608      * @deprecated Use {@link #onSignalStrengthsChanged(SignalStrength)}
609      */
610     @Deprecated
onSignalStrengthChanged(int asu)611     public void onSignalStrengthChanged(int asu) {
612         // default implementation empty
613     }
614 
615     /**
616      * Callback invoked when the message-waiting indicator changes on the registered subscription.
617      * Note, the registration subId comes from {@link TelephonyManager} object which registers
618      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
619      * If this TelephonyManager object was created with
620      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
621      * subId. Otherwise, this callback applies to
622      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
623      */
onMessageWaitingIndicatorChanged(boolean mwi)624     public void onMessageWaitingIndicatorChanged(boolean mwi) {
625         // default implementation empty
626     }
627 
628     /**
629      * Callback invoked when the call-forwarding indicator changes on the registered subscription.
630      * Note, the registration subId comes from {@link TelephonyManager} object which registers
631      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
632      * If this TelephonyManager object was created with
633      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
634      * subId. Otherwise, this callback applies to
635      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
636      */
onCallForwardingIndicatorChanged(boolean cfi)637     public void onCallForwardingIndicatorChanged(boolean cfi) {
638         // default implementation empty
639     }
640 
641     /**
642      * Callback invoked when device cell location changes on the registered subscription.
643      * Note, the registration subId comes from {@link TelephonyManager} object which registers
644      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
645      * If this TelephonyManager object was created with
646      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
647      * subId. Otherwise, this callback applies to
648      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
649      */
onCellLocationChanged(CellLocation location)650     public void onCellLocationChanged(CellLocation location) {
651         // default implementation empty
652     }
653 
654     /**
655      * Callback invoked when device call state changes.
656      * <p>
657      * Reports the state of Telephony (mobile) calls on the device for the registered subscription.
658      * <p>
659      * Note: the registration subId comes from {@link TelephonyManager} object which registers
660      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
661      * If this TelephonyManager object was created with
662      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
663      * subId. Otherwise, this callback applies to
664      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
665      * <p>
666      * Note: The state returned here may differ from that returned by
667      * {@link TelephonyManager#getCallState()}. Receivers of this callback should be aware that
668      * calling {@link TelephonyManager#getCallState()} from within this callback may return a
669      * different state than the callback reports.
670      *
671      * @param state call state
672      * @param phoneNumber call phone number. If application does not have
673      * {@link android.Manifest.permission#READ_CALL_LOG READ_CALL_LOG} permission or carrier
674      * privileges (see {@link TelephonyManager#hasCarrierPrivileges}), an empty string will be
675      * passed as an argument.
676      */
onCallStateChanged(@allState int state, String phoneNumber)677     public void onCallStateChanged(@CallState int state, String phoneNumber) {
678         // default implementation empty
679     }
680 
681     /**
682      * Callback invoked when connection state changes on the registered subscription.
683      * Note, the registration subId comes from {@link TelephonyManager} object which registers
684      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
685      * If this TelephonyManager object was created with
686      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
687      * subId. Otherwise, this callback applies to
688      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
689      *
690      * @see TelephonyManager#DATA_DISCONNECTED
691      * @see TelephonyManager#DATA_CONNECTING
692      * @see TelephonyManager#DATA_CONNECTED
693      * @see TelephonyManager#DATA_SUSPENDED
694      */
onDataConnectionStateChanged(int state)695     public void onDataConnectionStateChanged(int state) {
696         // default implementation empty
697     }
698 
699     /**
700      * same as above, but with the network type.  Both called.
701      */
onDataConnectionStateChanged(int state, int networkType)702     public void onDataConnectionStateChanged(int state, int networkType) {
703     }
704 
705     /**
706      * Callback invoked when data activity state changes on the registered subscription.
707      * Note, the registration subId comes from {@link TelephonyManager} object which registers
708      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
709      * If this TelephonyManager object was created with
710      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
711      * subId. Otherwise, this callback applies to
712      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
713      *
714      * @see TelephonyManager#DATA_ACTIVITY_NONE
715      * @see TelephonyManager#DATA_ACTIVITY_IN
716      * @see TelephonyManager#DATA_ACTIVITY_OUT
717      * @see TelephonyManager#DATA_ACTIVITY_INOUT
718      * @see TelephonyManager#DATA_ACTIVITY_DORMANT
719      */
onDataActivity(int direction)720     public void onDataActivity(int direction) {
721         // default implementation empty
722     }
723 
724     /**
725      * Callback invoked when network signal strengths changes on the registered subscription.
726      * Note, the registration subId comes from {@link TelephonyManager} object which registers
727      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
728      * If this TelephonyManager object was created with
729      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
730      * subId. Otherwise, this callback applies to
731      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
732      */
onSignalStrengthsChanged(SignalStrength signalStrength)733     public void onSignalStrengthsChanged(SignalStrength signalStrength) {
734         // default implementation empty
735     }
736 
737     /**
738      * Callback invoked when a observed cell info has changed or new cells have been added
739      * or removed on the registered subscription.
740      * Note, the registration subId s from {@link TelephonyManager} object which registers
741      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
742      * If this TelephonyManager object was created with
743      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
744      * subId. Otherwise, this callback applies to
745      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
746      *
747      * @param cellInfo is the list of currently visible cells.
748      */
onCellInfoChanged(List<CellInfo> cellInfo)749     public void onCellInfoChanged(List<CellInfo> cellInfo) {
750     }
751 
752     /**
753      * Callback invoked when precise device call state changes on the registered subscription.
754      * Note, the registration subId comes from {@link TelephonyManager} object which registers
755      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
756      * If this TelephonyManager object was created with
757      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
758      * subId. Otherwise, this callback applies to
759      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
760      * @param callState {@link PreciseCallState}
761      * @hide
762      */
763     @RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
764     @SystemApi
onPreciseCallStateChanged(@onNull PreciseCallState callState)765     public void onPreciseCallStateChanged(@NonNull PreciseCallState callState) {
766         // default implementation empty
767     }
768 
769     /**
770      * Callback invoked when call disconnect cause changes on the registered subscription.
771      * Note, the registration subId comes from {@link TelephonyManager} object which registers
772      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
773      * If this TelephonyManager object was created with
774      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
775      * subId. Otherwise, this callback applies to
776      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
777      *
778      * @param disconnectCause {@link DisconnectCause}.
779      * @param preciseDisconnectCause {@link PreciseDisconnectCause}.
780      *
781      */
782     @RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
onCallDisconnectCauseChanged(int disconnectCause, int preciseDisconnectCause)783     public void onCallDisconnectCauseChanged(int disconnectCause, int preciseDisconnectCause) {
784         // default implementation empty
785     }
786 
787     /**
788      * Callback invoked when Ims call disconnect cause changes on the registered subscription.
789      * Note, the registration subId comes from {@link TelephonyManager} object which registers
790      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
791      * If this TelephonyManager object was created with
792      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
793      * subId. Otherwise, this callback applies to
794      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
795      *
796      * @param imsReasonInfo {@link ImsReasonInfo} contains details on why IMS call failed.
797      *
798      */
799     @RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
onImsCallDisconnectCauseChanged(@onNull ImsReasonInfo imsReasonInfo)800     public void onImsCallDisconnectCauseChanged(@NonNull ImsReasonInfo imsReasonInfo) {
801         // default implementation empty
802     }
803 
804     /**
805      * Callback providing update about the default/internet data connection on the registered
806      * subscription.
807      *
808      * Note, the registration subId comes from {@link TelephonyManager} object which registers
809      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
810      * If this TelephonyManager object was created with
811      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
812      * subId. Otherwise, this callback applies to
813      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
814      *
815      * <p>Requires permission {@link android.Manifest.permission#MODIFY_PHONE_STATE}
816      * or the calling app has carrier privileges
817      * (see {@link TelephonyManager#hasCarrierPrivileges}).
818      *
819      * @param dataConnectionState {@link PreciseDataConnectionState}
820      */
821     @RequiresPermission((android.Manifest.permission.MODIFY_PHONE_STATE))
onPreciseDataConnectionStateChanged( @onNull PreciseDataConnectionState dataConnectionState)822     public void onPreciseDataConnectionStateChanged(
823             @NonNull PreciseDataConnectionState dataConnectionState) {
824         // default implementation empty
825     }
826 
827     /**
828      * Callback invoked when data connection real time info changes on the registered subscription.
829      * Note, the registration subId comes from {@link TelephonyManager} object which registers
830      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
831      * If this TelephonyManager object was created with
832      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
833      * subId. Otherwise, this callback applies to
834      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
835      *
836      * @hide
837      */
838     @UnsupportedAppUsage
onDataConnectionRealTimeInfoChanged( DataConnectionRealTimeInfo dcRtInfo)839     public void onDataConnectionRealTimeInfoChanged(
840             DataConnectionRealTimeInfo dcRtInfo) {
841         // default implementation empty
842     }
843 
844     /**
845      * Callback invoked when there has been a change in the Single Radio Voice Call Continuity
846      * (SRVCC) state for the currently active call on the registered subscription.
847      *
848      * Note, the registration subId comes from {@link TelephonyManager} object which registers
849      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
850      * If this TelephonyManager object was created with
851      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
852      * subId. Otherwise, this callback applies to
853      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
854      *
855      * @hide
856      */
857     @SystemApi
onSrvccStateChanged(@rvccState int srvccState)858     public void onSrvccStateChanged(@SrvccState int srvccState) {
859 
860     }
861 
862     /**
863      * Callback invoked when the SIM voice activation state has changed on the registered
864      * subscription.
865      * Note, the registration subId comes from {@link TelephonyManager} object which registers
866      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
867      * If this TelephonyManager object was created with
868      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
869      * subId. Otherwise, this callback applies to
870      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
871      *
872      * @param state is the current SIM voice activation state
873      * @hide
874      */
875     @SystemApi
onVoiceActivationStateChanged(@imActivationState int state)876     public void onVoiceActivationStateChanged(@SimActivationState int state) {
877     }
878 
879     /**
880      * Callback invoked when the SIM data activation state has changed on the registered
881      * subscription.
882      * Note, the registration subId comes from {@link TelephonyManager} object which registers
883      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
884      * If this TelephonyManager object was created with
885      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
886      * subId. Otherwise, this callback applies to
887      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
888      *
889      * @param state is the current SIM data activation state
890      * @hide
891      */
onDataActivationStateChanged(@imActivationState int state)892     public void onDataActivationStateChanged(@SimActivationState int state) {
893     }
894 
895     /**
896      * Callback invoked when the user mobile data state has changed on the registered subscription.
897      * Note, the registration subId comes from {@link TelephonyManager} object which registers
898      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
899      * If this TelephonyManager object was created with
900      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
901      * subId. Otherwise, this callback applies to
902      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
903      *
904      * @param enabled indicates whether the current user mobile data state is enabled or disabled.
905      */
onUserMobileDataStateChanged(boolean enabled)906     public void onUserMobileDataStateChanged(boolean enabled) {
907         // default implementation empty
908     }
909 
910     /**
911      * Callback invoked when the display info has changed on the registered subscription.
912      * <p> The {@link TelephonyDisplayInfo} contains status information shown to the user based on
913      * carrier policy.
914      *
915      * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling
916      * app has carrier privileges (see {@link TelephonyManager#hasCarrierPrivileges}).
917      *
918      * @param telephonyDisplayInfo The display information.
919      */
920     @RequiresPermission((android.Manifest.permission.READ_PHONE_STATE))
onDisplayInfoChanged(@onNull TelephonyDisplayInfo telephonyDisplayInfo)921     public void onDisplayInfoChanged(@NonNull TelephonyDisplayInfo telephonyDisplayInfo) {
922         // default implementation empty
923     }
924 
925     /**
926      * Callback invoked when the current emergency number list has changed on the registered
927      * subscription.
928      * Note, the registration subId comes from {@link TelephonyManager} object which registers
929      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
930      * If this TelephonyManager object was created with
931      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
932      * subId. Otherwise, this callback applies to
933      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
934      *
935      * @param emergencyNumberList Map including the key as the active subscription ID
936      *                           (Note: if there is no active subscription, the key is
937      *                           {@link SubscriptionManager#getDefaultSubscriptionId})
938      *                           and the value as the list of {@link EmergencyNumber};
939      *                           null if this information is not available.
940      * @hide
941      */
onEmergencyNumberListChanged( @onNull Map<Integer, List<EmergencyNumber>> emergencyNumberList)942     public void onEmergencyNumberListChanged(
943             @NonNull Map<Integer, List<EmergencyNumber>> emergencyNumberList) {
944         // default implementation empty
945     }
946 
947     /**
948      * Callback invoked when an outgoing call is placed to an emergency number.
949      *
950      * @param placedEmergencyNumber the emergency number {@link EmergencyNumber} the call is placed
951      *                              to.
952      * @hide
953      */
954     @SystemApi
955     @TestApi
onOutgoingEmergencyCall(@onNull EmergencyNumber placedEmergencyNumber)956     public void onOutgoingEmergencyCall(@NonNull EmergencyNumber placedEmergencyNumber) {
957         // default implementation empty
958     }
959 
960     /**
961      * Callback invoked when an outgoing SMS is placed to an emergency number.
962      *
963      * @param sentEmergencyNumber the emergency number {@link EmergencyNumber} the SMS is sent to.
964      * @hide
965      */
966     @SystemApi
967     @TestApi
onOutgoingEmergencySms(@onNull EmergencyNumber sentEmergencyNumber)968     public void onOutgoingEmergencySms(@NonNull EmergencyNumber sentEmergencyNumber) {
969         // default implementation empty
970     }
971 
972     /**
973      * Callback invoked when OEM hook raw event is received on the registered subscription.
974      * Note, the registration subId comes from {@link TelephonyManager} object which registers
975      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
976      * If this TelephonyManager object was created with
977      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
978      * subId. Otherwise, this callback applies to
979      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
980      *
981      * Requires the READ_PRIVILEGED_PHONE_STATE permission.
982      * @param rawData is the byte array of the OEM hook raw data.
983      * @hide
984      */
985     @UnsupportedAppUsage
onOemHookRawEvent(byte[] rawData)986     public void onOemHookRawEvent(byte[] rawData) {
987         // default implementation empty
988     }
989 
990     /**
991      * Callback invoked when phone capability changes.
992      * Note, this callback triggers regardless of registered subscription.
993      *
994      * @param capability the new phone capability
995      * @hide
996      */
onPhoneCapabilityChanged(PhoneCapability capability)997     public void onPhoneCapabilityChanged(PhoneCapability capability) {
998         // default implementation empty
999     }
1000 
1001     /**
1002      * Callback invoked when active data subId changes.
1003      * Note, this callback triggers regardless of registered subscription.
1004      *
1005      * Requires the READ_PHONE_STATE permission.
1006      * @param subId current subscription used to setup Cellular Internet data.
1007      *              For example, it could be the current active opportunistic subscription in use,
1008      *              or the subscription user selected as default data subscription in DSDS mode.
1009      */
onActiveDataSubscriptionIdChanged(int subId)1010     public void onActiveDataSubscriptionIdChanged(int subId) {
1011         // default implementation empty
1012     }
1013 
1014     /**
1015      * Callback invoked when the call attributes changes on the registered subscription.
1016      * Note, the registration subId comes from {@link TelephonyManager} object which registers
1017      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
1018      * If this TelephonyManager object was created with
1019      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1020      * subId. Otherwise, this callback applies to
1021      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1022      *
1023      * Requires the READ_PRECISE_PHONE_STATE permission.
1024      * @param callAttributes the call attributes
1025      * @hide
1026      */
1027     @SystemApi
onCallAttributesChanged(@onNull CallAttributes callAttributes)1028     public void onCallAttributesChanged(@NonNull CallAttributes callAttributes) {
1029         // default implementation empty
1030     }
1031 
1032     /**
1033      * Callback invoked when modem radio power state changes on the registered subscription.
1034      * Note, the registration subId comes from {@link TelephonyManager} object which registers
1035      * PhoneStateListener by {@link TelephonyManager#listen(PhoneStateListener, int)}.
1036      * If this TelephonyManager object was created with
1037      * {@link TelephonyManager#createForSubscriptionId(int)}, then the callback applies to the
1038      * subId. Otherwise, this callback applies to
1039      * {@link SubscriptionManager#getDefaultSubscriptionId()}.
1040      *
1041      * @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE}
1042      * @param state the modem radio power state
1043      * @hide
1044      */
1045     @SystemApi
onRadioPowerStateChanged(@adioPowerState int state)1046     public void onRadioPowerStateChanged(@RadioPowerState int state) {
1047         // default implementation empty
1048     }
1049 
1050     /**
1051      * Callback invoked when telephony has received notice from a carrier
1052      * app that a network action that could result in connectivity loss
1053      * has been requested by an app using
1054      * {@link android.telephony.TelephonyManager#notifyCarrierNetworkChange(boolean)}
1055      *
1056      * Note, this callback is pinned to the registered subscription and will be invoked when
1057      * the notifying carrier app has carrier privilege rule on the registered
1058      * subscription. {@link android.telephony.TelephonyManager#hasCarrierPrivileges}
1059      *
1060      * @param active Whether the carrier network change is or shortly
1061      *               will be active. This value is true to indicate
1062      *               showing alternative UI and false to stop.
1063      *
1064      * @hide
1065      */
onCarrierNetworkChange(boolean active)1066     public void onCarrierNetworkChange(boolean active) {
1067         // default implementation empty
1068     }
1069 
1070     /**
1071      * Report that Registration or a Location/Routing/Tracking Area update has failed.
1072      *
1073      * <p>Indicate whenever a registration procedure, including a location, routing, or tracking
1074      * area update fails. This includes procedures that do not necessarily result in a change of
1075      * the modem's registration status. If the modem's registration status changes, that is
1076      * reflected in the onNetworkStateChanged() and subsequent get{Voice/Data}RegistrationState().
1077      *
1078      * <p>Because registration failures are ephemeral, this callback is not sticky.
1079      * Registrants will not receive the most recent past value when registering.
1080      *
1081      * @param cellIdentity the CellIdentity, which must include the globally unique identifier
1082      *        for the cell (for example, all components of the CGI or ECGI).
1083      * @param chosenPlmn a 5 or 6 digit alphanumeric PLMN (MCC|MNC) among those broadcast by the
1084      *         cell that was chosen for the failed registration attempt.
1085      * @param domain DOMAIN_CS, DOMAIN_PS or both in case of a combined procedure.
1086      * @param causeCode the primary failure cause code of the procedure.
1087      *        For GSM/UMTS (MM), values are in TS 24.008 Sec 10.5.95
1088      *        For GSM/UMTS (GMM), values are in TS 24.008 Sec 10.5.147
1089      *        For LTE (EMM), cause codes are TS 24.301 Sec 9.9.3.9
1090      *        For NR (5GMM), cause codes are TS 24.501 Sec 9.11.3.2
1091      *        Integer.MAX_VALUE if this value is unused.
1092      * @param additionalCauseCode the cause code of any secondary/combined procedure if appropriate.
1093      *        For UMTS, if a combined attach succeeds for PS only, then the GMM cause code shall be
1094      *        included as an additionalCauseCode. For LTE (ESM), cause codes are in
1095      *        TS 24.301 9.9.4.4. Integer.MAX_VALUE if this value is unused.
1096      */
onRegistrationFailed(@onNull CellIdentity cellIdentity, @NonNull String chosenPlmn, int domain, int causeCode, int additionalCauseCode)1097     public void onRegistrationFailed(@NonNull CellIdentity cellIdentity, @NonNull String chosenPlmn,
1098             int domain, int causeCode, int additionalCauseCode) {
1099         // default implementation empty
1100     }
1101 
1102     /**
1103      * Report updated barring information for the current camped/registered cell.
1104      *
1105      * <p>Barring info is provided for all services applicable to the current camped/registered
1106      * cell, for the registered PLMN and current access class/access category.
1107      *
1108      * @param barringInfo for all services on the current cell.
1109      *
1110      * @see android.telephony.BarringInfo
1111      */
onBarringInfoChanged(@onNull BarringInfo barringInfo)1112     public void onBarringInfoChanged(@NonNull BarringInfo barringInfo) {
1113         // default implementation empty
1114     }
1115 
1116     /**
1117      * The callback methods need to be called on the handler thread where
1118      * this object was created.  If the binder did that for us it'd be nice.
1119      *
1120      * Using a static class and weak reference here to avoid memory leak caused by the
1121      * IPhoneStateListener.Stub callback retaining references to the outside PhoneStateListeners:
1122      * even caller has been destroyed and "un-registered" the PhoneStateListener, it is still not
1123      * eligible for GC given the references coming from:
1124      * Native Stack --> PhoneStateListener --> Context (Activity).
1125      * memory of caller's context will be collected after GC from service side get triggered
1126      */
1127     private static class IPhoneStateListenerStub extends IPhoneStateListener.Stub {
1128         private WeakReference<PhoneStateListener> mPhoneStateListenerWeakRef;
1129         private Executor mExecutor;
1130 
IPhoneStateListenerStub(PhoneStateListener phoneStateListener, Executor executor)1131         IPhoneStateListenerStub(PhoneStateListener phoneStateListener, Executor executor) {
1132             mPhoneStateListenerWeakRef = new WeakReference<PhoneStateListener>(phoneStateListener);
1133             mExecutor = executor;
1134         }
1135 
onServiceStateChanged(ServiceState serviceState)1136         public void onServiceStateChanged(ServiceState serviceState) {
1137             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1138             if (psl == null) return;
1139 
1140             Binder.withCleanCallingIdentity(
1141                     () -> mExecutor.execute(() -> psl.onServiceStateChanged(serviceState)));
1142         }
1143 
onSignalStrengthChanged(int asu)1144         public void onSignalStrengthChanged(int asu) {
1145             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1146             if (psl == null) return;
1147 
1148             Binder.withCleanCallingIdentity(
1149                     () -> mExecutor.execute(() -> psl.onSignalStrengthChanged(asu)));
1150         }
1151 
onMessageWaitingIndicatorChanged(boolean mwi)1152         public void onMessageWaitingIndicatorChanged(boolean mwi) {
1153             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1154             if (psl == null) return;
1155 
1156             Binder.withCleanCallingIdentity(
1157                     () -> mExecutor.execute(() -> psl.onMessageWaitingIndicatorChanged(mwi)));
1158         }
1159 
onCallForwardingIndicatorChanged(boolean cfi)1160         public void onCallForwardingIndicatorChanged(boolean cfi) {
1161             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1162             if (psl == null) return;
1163 
1164             Binder.withCleanCallingIdentity(
1165                     () -> mExecutor.execute(() -> psl.onCallForwardingIndicatorChanged(cfi)));
1166         }
1167 
onCellLocationChanged(CellIdentity cellIdentity)1168         public void onCellLocationChanged(CellIdentity cellIdentity) {
1169             // There is no system/public API to create an CellIdentity in system server,
1170             // so the server pass a null to indicate an empty initial location.
1171             CellLocation location =
1172                     cellIdentity == null ? CellLocation.getEmpty() : cellIdentity.asCellLocation();
1173             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1174             if (psl == null) return;
1175 
1176             Binder.withCleanCallingIdentity(
1177                     () -> mExecutor.execute(() -> psl.onCellLocationChanged(location)));
1178         }
1179 
onCallStateChanged(int state, String incomingNumber)1180         public void onCallStateChanged(int state, String incomingNumber) {
1181             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1182             if (psl == null) return;
1183 
1184             Binder.withCleanCallingIdentity(
1185                     () -> mExecutor.execute(() -> psl.onCallStateChanged(state, incomingNumber)));
1186         }
1187 
onDataConnectionStateChanged(int state, int networkType)1188         public void onDataConnectionStateChanged(int state, int networkType) {
1189             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1190             if (psl == null) return;
1191 
1192             if (state == TelephonyManager.DATA_DISCONNECTING
1193                     && VMRuntime.getRuntime().getTargetSdkVersion() < Build.VERSION_CODES.R) {
1194                 Binder.withCleanCallingIdentity(() -> mExecutor.execute(
1195                         () -> {
1196                             psl.onDataConnectionStateChanged(
1197                                     TelephonyManager.DATA_CONNECTED, networkType);
1198                             psl.onDataConnectionStateChanged(TelephonyManager.DATA_CONNECTED);
1199                         }));
1200             } else {
1201                 Binder.withCleanCallingIdentity(() -> mExecutor.execute(
1202                         () -> {
1203                             psl.onDataConnectionStateChanged(state, networkType);
1204                             psl.onDataConnectionStateChanged(state);
1205                         }));
1206             }
1207         }
1208 
onDataActivity(int direction)1209         public void onDataActivity(int direction) {
1210             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1211             if (psl == null) return;
1212 
1213             Binder.withCleanCallingIdentity(
1214                     () -> mExecutor.execute(() -> psl.onDataActivity(direction)));
1215         }
1216 
onSignalStrengthsChanged(SignalStrength signalStrength)1217         public void onSignalStrengthsChanged(SignalStrength signalStrength) {
1218             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1219             if (psl == null) return;
1220 
1221             Binder.withCleanCallingIdentity(
1222                     () -> mExecutor.execute(() -> psl.onSignalStrengthsChanged(signalStrength)));
1223         }
1224 
onCellInfoChanged(List<CellInfo> cellInfo)1225         public void onCellInfoChanged(List<CellInfo> cellInfo) {
1226             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1227             if (psl == null) return;
1228 
1229             Binder.withCleanCallingIdentity(
1230                     () -> mExecutor.execute(() -> psl.onCellInfoChanged(cellInfo)));
1231         }
1232 
onPreciseCallStateChanged(PreciseCallState callState)1233         public void onPreciseCallStateChanged(PreciseCallState callState) {
1234             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1235             if (psl == null) return;
1236 
1237             Binder.withCleanCallingIdentity(
1238                     () -> mExecutor.execute(() -> psl.onPreciseCallStateChanged(callState)));
1239         }
1240 
onCallDisconnectCauseChanged(int disconnectCause, int preciseDisconnectCause)1241         public void onCallDisconnectCauseChanged(int disconnectCause, int preciseDisconnectCause) {
1242             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1243             if (psl == null) return;
1244 
1245             Binder.withCleanCallingIdentity(
1246                     () -> mExecutor.execute(() -> psl.onCallDisconnectCauseChanged(
1247                             disconnectCause, preciseDisconnectCause)));
1248         }
1249 
onPreciseDataConnectionStateChanged( PreciseDataConnectionState dataConnectionState)1250         public void onPreciseDataConnectionStateChanged(
1251                 PreciseDataConnectionState dataConnectionState) {
1252             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1253             if (psl == null) return;
1254 
1255             Binder.withCleanCallingIdentity(
1256                     () -> mExecutor.execute(
1257                             () -> psl.onPreciseDataConnectionStateChanged(dataConnectionState)));
1258         }
1259 
onDataConnectionRealTimeInfoChanged(DataConnectionRealTimeInfo dcRtInfo)1260         public void onDataConnectionRealTimeInfoChanged(DataConnectionRealTimeInfo dcRtInfo) {
1261             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1262             if (psl == null) return;
1263 
1264             Binder.withCleanCallingIdentity(
1265                     () -> mExecutor.execute(
1266                             () -> psl.onDataConnectionRealTimeInfoChanged(dcRtInfo)));
1267         }
1268 
onSrvccStateChanged(int state)1269         public void onSrvccStateChanged(int state) {
1270             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1271             if (psl == null) return;
1272 
1273             Binder.withCleanCallingIdentity(
1274                     () -> mExecutor.execute(() -> psl.onSrvccStateChanged(state)));
1275         }
1276 
onVoiceActivationStateChanged(int activationState)1277         public void onVoiceActivationStateChanged(int activationState) {
1278             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1279             if (psl == null) return;
1280 
1281             Binder.withCleanCallingIdentity(
1282                     () -> mExecutor.execute(
1283                             () -> psl.onVoiceActivationStateChanged(activationState)));
1284         }
1285 
onDataActivationStateChanged(int activationState)1286         public void onDataActivationStateChanged(int activationState) {
1287             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1288             if (psl == null) return;
1289 
1290             Binder.withCleanCallingIdentity(
1291                     () -> mExecutor.execute(
1292                             () -> psl.onDataActivationStateChanged(activationState)));
1293         }
1294 
onUserMobileDataStateChanged(boolean enabled)1295         public void onUserMobileDataStateChanged(boolean enabled) {
1296             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1297             if (psl == null) return;
1298 
1299             Binder.withCleanCallingIdentity(
1300                     () -> mExecutor.execute(
1301                             () -> psl.onUserMobileDataStateChanged(enabled)));
1302         }
1303 
onDisplayInfoChanged(TelephonyDisplayInfo telephonyDisplayInfo)1304         public void onDisplayInfoChanged(TelephonyDisplayInfo telephonyDisplayInfo) {
1305             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1306             if (psl == null) return;
1307 
1308             Binder.withCleanCallingIdentity(
1309                     () -> mExecutor.execute(
1310                             () -> psl.onDisplayInfoChanged(telephonyDisplayInfo)));
1311         }
1312 
onOemHookRawEvent(byte[] rawData)1313         public void onOemHookRawEvent(byte[] rawData) {
1314             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1315             if (psl == null) return;
1316 
1317             Binder.withCleanCallingIdentity(
1318                     () -> mExecutor.execute(() -> psl.onOemHookRawEvent(rawData)));
1319         }
1320 
onCarrierNetworkChange(boolean active)1321         public void onCarrierNetworkChange(boolean active) {
1322             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1323             if (psl == null) return;
1324 
1325             Binder.withCleanCallingIdentity(
1326                     () -> mExecutor.execute(() -> psl.onCarrierNetworkChange(active)));
1327         }
1328 
onEmergencyNumberListChanged(Map emergencyNumberList)1329         public void onEmergencyNumberListChanged(Map emergencyNumberList) {
1330             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1331             if (psl == null) return;
1332 
1333             Binder.withCleanCallingIdentity(
1334                     () -> mExecutor.execute(
1335                             () -> psl.onEmergencyNumberListChanged(emergencyNumberList)));
1336         }
1337 
onOutgoingEmergencyCall(@onNull EmergencyNumber placedEmergencyNumber)1338         public void onOutgoingEmergencyCall(@NonNull EmergencyNumber placedEmergencyNumber) {
1339             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1340             if (psl == null) return;
1341 
1342             Binder.withCleanCallingIdentity(
1343                     () -> mExecutor.execute(
1344                             () -> psl.onOutgoingEmergencyCall(placedEmergencyNumber)));
1345         }
1346 
onOutgoingEmergencySms(@onNull EmergencyNumber sentEmergencyNumber)1347         public void onOutgoingEmergencySms(@NonNull EmergencyNumber sentEmergencyNumber) {
1348             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1349             if (psl == null) return;
1350 
1351             Binder.withCleanCallingIdentity(
1352                     () -> mExecutor.execute(
1353                             () -> psl.onOutgoingEmergencySms(sentEmergencyNumber)));
1354         }
1355 
onPhoneCapabilityChanged(PhoneCapability capability)1356         public void onPhoneCapabilityChanged(PhoneCapability capability) {
1357             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1358             if (psl == null) return;
1359 
1360             Binder.withCleanCallingIdentity(
1361                     () -> mExecutor.execute(() -> psl.onPhoneCapabilityChanged(capability)));
1362         }
1363 
onRadioPowerStateChanged(@adioPowerState int state)1364         public void onRadioPowerStateChanged(@RadioPowerState int state) {
1365             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1366             if (psl == null) return;
1367 
1368             Binder.withCleanCallingIdentity(
1369                     () -> mExecutor.execute(() -> psl.onRadioPowerStateChanged(state)));
1370         }
1371 
onCallAttributesChanged(CallAttributes callAttributes)1372         public void onCallAttributesChanged(CallAttributes callAttributes) {
1373             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1374             if (psl == null) return;
1375 
1376             Binder.withCleanCallingIdentity(
1377                     () -> mExecutor.execute(() -> psl.onCallAttributesChanged(callAttributes)));
1378         }
1379 
onActiveDataSubIdChanged(int subId)1380         public void onActiveDataSubIdChanged(int subId) {
1381             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1382             if (psl == null) return;
1383 
1384             Binder.withCleanCallingIdentity(
1385                     () -> mExecutor.execute(() -> psl.onActiveDataSubscriptionIdChanged(subId)));
1386         }
1387 
onImsCallDisconnectCauseChanged(ImsReasonInfo disconnectCause)1388         public void onImsCallDisconnectCauseChanged(ImsReasonInfo disconnectCause) {
1389             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1390             if (psl == null) return;
1391 
1392             Binder.withCleanCallingIdentity(
1393                     () -> mExecutor.execute(
1394                             () -> psl.onImsCallDisconnectCauseChanged(disconnectCause)));
1395 
1396         }
1397 
onRegistrationFailed(@onNull CellIdentity cellIdentity, @NonNull String chosenPlmn, int domain, int causeCode, int additionalCauseCode)1398         public void onRegistrationFailed(@NonNull CellIdentity cellIdentity,
1399                 @NonNull String chosenPlmn, int domain,
1400                 int causeCode, int additionalCauseCode) {
1401             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1402             if (psl == null) return;
1403 
1404             Binder.withCleanCallingIdentity(
1405                     () -> mExecutor.execute(() -> psl.onRegistrationFailed(
1406                             cellIdentity, chosenPlmn, domain, causeCode, additionalCauseCode)));
1407             // default implementation empty
1408         }
1409 
onBarringInfoChanged(BarringInfo barringInfo)1410         public void onBarringInfoChanged(BarringInfo barringInfo) {
1411             PhoneStateListener psl = mPhoneStateListenerWeakRef.get();
1412             if (psl == null) return;
1413 
1414             Binder.withCleanCallingIdentity(
1415                     () -> mExecutor.execute(() -> psl.onBarringInfoChanged(barringInfo)));
1416         }
1417     }
1418 
1419 
log(String s)1420     private void log(String s) {
1421         Rlog.d(LOG_TAG, s);
1422     }
1423 }
1424