1 /* 2 * Copyright (C) 2019 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 package android.telephony; 17 18 import android.annotation.CallbackExecutor; 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.RequiresPermission; 22 import android.compat.Compatibility; 23 import android.compat.annotation.ChangeId; 24 import android.compat.annotation.EnabledAfter; 25 import android.content.Context; 26 import android.os.Binder; 27 import android.os.Build; 28 import android.os.RemoteException; 29 import android.os.ServiceManager; 30 import android.telephony.Annotation.CallState; 31 import android.telephony.Annotation.DataActivityType; 32 import android.telephony.Annotation.DisconnectCauses; 33 import android.telephony.Annotation.NetworkType; 34 import android.telephony.Annotation.PreciseCallStates; 35 import android.telephony.Annotation.PreciseDisconnectCauses; 36 import android.telephony.Annotation.RadioPowerState; 37 import android.telephony.Annotation.SimActivationState; 38 import android.telephony.Annotation.SrvccState; 39 import android.telephony.emergency.EmergencyNumber; 40 import android.telephony.ims.ImsReasonInfo; 41 import android.util.ArraySet; 42 import android.util.Log; 43 44 import com.android.internal.telephony.IOnSubscriptionsChangedListener; 45 import com.android.internal.telephony.ITelephonyRegistry; 46 47 import java.util.HashMap; 48 import java.util.List; 49 import java.util.Map; 50 import java.util.Set; 51 import java.util.concurrent.Executor; 52 53 /** 54 * A centralized place to notify telephony related status changes, e.g, {@link ServiceState} update 55 * or {@link PhoneCapability} changed. This might trigger callback from applications side through 56 * {@link android.telephony.PhoneStateListener} 57 * 58 * TODO: limit API access to only carrier apps with certain permissions or apps running on 59 * privileged UID. 60 * 61 * @hide 62 */ 63 public class TelephonyRegistryManager { 64 65 private static final String TAG = "TelephonyRegistryManager"; 66 private static ITelephonyRegistry sRegistry; 67 private final Context mContext; 68 69 /** 70 * A mapping between {@link SubscriptionManager.OnSubscriptionsChangedListener} and 71 * its callback IOnSubscriptionsChangedListener. 72 */ 73 private final Map<SubscriptionManager.OnSubscriptionsChangedListener, 74 IOnSubscriptionsChangedListener> mSubscriptionChangedListenerMap = new HashMap<>(); 75 /** 76 * A mapping between {@link SubscriptionManager.OnOpportunisticSubscriptionsChangedListener} and 77 * its callback IOnSubscriptionsChangedListener. 78 */ 79 private final Map<SubscriptionManager.OnOpportunisticSubscriptionsChangedListener, 80 IOnSubscriptionsChangedListener> mOpportunisticSubscriptionChangedListenerMap 81 = new HashMap<>(); 82 83 84 /** @hide **/ TelephonyRegistryManager(@onNull Context context)85 public TelephonyRegistryManager(@NonNull Context context) { 86 mContext = context; 87 if (sRegistry == null) { 88 sRegistry = ITelephonyRegistry.Stub.asInterface( 89 ServiceManager.getService("telephony.registry")); 90 } 91 } 92 93 /** 94 * Register for changes to the list of active {@link SubscriptionInfo} records or to the 95 * individual records themselves. When a change occurs the onSubscriptionsChanged method of 96 * the listener will be invoked immediately if there has been a notification. The 97 * onSubscriptionChanged method will also be triggered once initially when calling this 98 * function. 99 * 100 * @param listener an instance of {@link SubscriptionManager.OnSubscriptionsChangedListener} 101 * with onSubscriptionsChanged overridden. 102 * @param executor the executor that will execute callbacks. 103 */ addOnSubscriptionsChangedListener( @onNull SubscriptionManager.OnSubscriptionsChangedListener listener, @NonNull Executor executor)104 public void addOnSubscriptionsChangedListener( 105 @NonNull SubscriptionManager.OnSubscriptionsChangedListener listener, 106 @NonNull Executor executor) { 107 if (mSubscriptionChangedListenerMap.get(listener) != null) { 108 Log.d(TAG, "addOnSubscriptionsChangedListener listener already present"); 109 return; 110 } 111 IOnSubscriptionsChangedListener callback = new IOnSubscriptionsChangedListener.Stub() { 112 @Override 113 public void onSubscriptionsChanged () { 114 final long identity = Binder.clearCallingIdentity(); 115 try { 116 executor.execute(() -> listener.onSubscriptionsChanged()); 117 } finally { 118 Binder.restoreCallingIdentity(identity); 119 } 120 } 121 }; 122 mSubscriptionChangedListenerMap.put(listener, callback); 123 try { 124 sRegistry.addOnSubscriptionsChangedListener(mContext.getOpPackageName(), 125 mContext.getAttributionTag(), callback); 126 } catch (RemoteException ex) { 127 // system server crash 128 } 129 } 130 131 /** 132 * Unregister the {@link SubscriptionManager.OnSubscriptionsChangedListener}. This is not 133 * strictly necessary as the listener will automatically be unregistered if an attempt to 134 * invoke the listener fails. 135 * 136 * @param listener that is to be unregistered. 137 */ removeOnSubscriptionsChangedListener( @onNull SubscriptionManager.OnSubscriptionsChangedListener listener)138 public void removeOnSubscriptionsChangedListener( 139 @NonNull SubscriptionManager.OnSubscriptionsChangedListener listener) { 140 if (mSubscriptionChangedListenerMap.get(listener) == null) { 141 return; 142 } 143 try { 144 sRegistry.removeOnSubscriptionsChangedListener(mContext.getOpPackageName(), 145 mSubscriptionChangedListenerMap.get(listener)); 146 mSubscriptionChangedListenerMap.remove(listener); 147 } catch (RemoteException ex) { 148 // system server crash 149 } 150 } 151 152 /** 153 * Register for changes to the list of opportunistic subscription records or to the 154 * individual records themselves. When a change occurs the onOpportunisticSubscriptionsChanged 155 * method of the listener will be invoked immediately if there has been a notification. 156 * 157 * @param listener an instance of 158 * {@link SubscriptionManager.OnOpportunisticSubscriptionsChangedListener} with 159 * onOpportunisticSubscriptionsChanged overridden. 160 * @param executor an Executor that will execute callbacks. 161 */ addOnOpportunisticSubscriptionsChangedListener( @onNull SubscriptionManager.OnOpportunisticSubscriptionsChangedListener listener, @NonNull Executor executor)162 public void addOnOpportunisticSubscriptionsChangedListener( 163 @NonNull SubscriptionManager.OnOpportunisticSubscriptionsChangedListener listener, 164 @NonNull Executor executor) { 165 if (mOpportunisticSubscriptionChangedListenerMap.get(listener) != null) { 166 Log.d(TAG, "addOnOpportunisticSubscriptionsChangedListener listener already present"); 167 return; 168 } 169 /** 170 * The callback methods need to be called on the executor thread where 171 * this object was created. If the binder did that for us it'd be nice. 172 */ 173 IOnSubscriptionsChangedListener callback = new IOnSubscriptionsChangedListener.Stub() { 174 @Override 175 public void onSubscriptionsChanged() { 176 final long identity = Binder.clearCallingIdentity(); 177 try { 178 Log.d(TAG, "onOpportunisticSubscriptionsChanged callback received."); 179 executor.execute(() -> listener.onOpportunisticSubscriptionsChanged()); 180 } finally { 181 Binder.restoreCallingIdentity(identity); 182 } 183 } 184 }; 185 mOpportunisticSubscriptionChangedListenerMap.put(listener, callback); 186 try { 187 sRegistry.addOnOpportunisticSubscriptionsChangedListener(mContext.getOpPackageName(), 188 mContext.getAttributionTag(), callback); 189 } catch (RemoteException ex) { 190 // system server crash 191 } 192 } 193 194 /** 195 * Unregister the {@link SubscriptionManager.OnOpportunisticSubscriptionsChangedListener} 196 * that is currently listening opportunistic subscriptions change. This is not strictly 197 * necessary as the listener will automatically be unregistered if an attempt to invoke the 198 * listener fails. 199 * 200 * @param listener that is to be unregistered. 201 */ removeOnOpportunisticSubscriptionsChangedListener( @onNull SubscriptionManager.OnOpportunisticSubscriptionsChangedListener listener)202 public void removeOnOpportunisticSubscriptionsChangedListener( 203 @NonNull SubscriptionManager.OnOpportunisticSubscriptionsChangedListener listener) { 204 if (mOpportunisticSubscriptionChangedListenerMap.get(listener) == null) { 205 return; 206 } 207 try { 208 sRegistry.removeOnSubscriptionsChangedListener(mContext.getOpPackageName(), 209 mOpportunisticSubscriptionChangedListenerMap.get(listener)); 210 mOpportunisticSubscriptionChangedListenerMap.remove(listener); 211 } catch (RemoteException ex) { 212 // system server crash 213 } 214 } 215 216 /** 217 * To check the SDK version for {@link #listenFromListener}. 218 */ 219 @ChangeId 220 @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.P) 221 private static final long LISTEN_CODE_CHANGE = 147600208L; 222 223 /** 224 * Listen for incoming subscriptions 225 * @param subId Subscription ID 226 * @param pkg Package name 227 * @param featureId Feature ID 228 * @param listener Listener providing callback 229 * @param events Events 230 * @param notifyNow Whether to notify instantly 231 */ listenFromListener(int subId, @NonNull String pkg, @NonNull String featureId, @NonNull PhoneStateListener listener, @NonNull int events, boolean notifyNow)232 public void listenFromListener(int subId, @NonNull String pkg, @NonNull String featureId, 233 @NonNull PhoneStateListener listener, @NonNull int events, boolean notifyNow) { 234 if (listener == null) { 235 throw new IllegalStateException("telephony service is null."); 236 } 237 238 try { 239 int[] eventsList = getEventsFromBitmask(events).stream().mapToInt(i -> i).toArray(); 240 // subId from PhoneStateListener is deprecated Q on forward, use the subId from 241 // TelephonyManager instance. Keep using subId from PhoneStateListener for pre-Q. 242 if (Compatibility.isChangeEnabled(LISTEN_CODE_CHANGE)) { 243 // Since mSubId in PhoneStateListener is deprecated from Q on forward, this is 244 // the only place to set mSubId and its for "informational" only. 245 listener.mSubId = (eventsList.length == 0) 246 ? SubscriptionManager.INVALID_SUBSCRIPTION_ID : subId; 247 } else if (listener.mSubId != null) { 248 subId = listener.mSubId; 249 } 250 sRegistry.listenWithEventList( 251 subId, pkg, featureId, listener.callback, eventsList, notifyNow); 252 } catch (RemoteException e) { 253 throw e.rethrowFromSystemServer(); 254 } 255 } 256 257 /** 258 * Listen for incoming subscriptions 259 * @param subId Subscription ID 260 * @param pkg Package name 261 * @param featureId Feature ID 262 * @param telephonyCallback Listener providing callback 263 * @param events List events 264 * @param notifyNow Whether to notify instantly 265 */ listenFromCallback(int subId, @NonNull String pkg, @NonNull String featureId, @NonNull TelephonyCallback telephonyCallback, @NonNull int[] events, boolean notifyNow)266 private void listenFromCallback(int subId, @NonNull String pkg, @NonNull String featureId, 267 @NonNull TelephonyCallback telephonyCallback, @NonNull int[] events, 268 boolean notifyNow) { 269 try { 270 sRegistry.listenWithEventList( 271 subId, pkg, featureId, telephonyCallback.callback, events, notifyNow); 272 } catch (RemoteException e) { 273 throw e.rethrowFromSystemServer(); 274 } 275 } 276 277 /** 278 * Informs the system of an intentional upcoming carrier network change by a carrier app. 279 * This call only used to allow the system to provide alternative UI while telephony is 280 * performing an action that may result in intentional, temporary network lack of connectivity. 281 * <p> 282 * Based on the active parameter passed in, this method will either show or hide the alternative 283 * UI. There is no timeout associated with showing this UX, so a carrier app must be sure to 284 * call with active set to false sometime after calling with it set to {@code true}. 285 * <p> 286 * Requires Permission: calling app has carrier privileges. 287 * 288 * @param active Whether the carrier network change is or shortly will be 289 * active. Set this value to true to begin showing alternative UI and false to stop. 290 * @see TelephonyManager#hasCarrierPrivileges 291 */ notifyCarrierNetworkChange(boolean active)292 public void notifyCarrierNetworkChange(boolean active) { 293 try { 294 sRegistry.notifyCarrierNetworkChange(active); 295 } catch (RemoteException ex) { 296 // system server crash 297 } 298 } 299 300 /** 301 * Notify call state changed on certain subscription. 302 * 303 * @param slotIndex for which call state changed. Can be derived from subId except when subId is 304 * invalid. 305 * @param subId for which call state changed. 306 * @param state latest call state. e.g, offhook, ringing 307 * @param incomingNumber incoming phone number. 308 */ notifyCallStateChanged(int slotIndex, int subId, @CallState int state, @Nullable String incomingNumber)309 public void notifyCallStateChanged(int slotIndex, int subId, @CallState int state, 310 @Nullable String incomingNumber) { 311 try { 312 sRegistry.notifyCallState(slotIndex, subId, state, incomingNumber); 313 } catch (RemoteException ex) { 314 // system server crash 315 } 316 } 317 318 /** 319 * Notify call state changed on all subscriptions. 320 * 321 * @param state latest call state. e.g, offhook, ringing 322 * @param incomingNumber incoming phone number. 323 * @hide 324 */ 325 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) notifyCallStateChangedForAllSubscriptions(@allState int state, @Nullable String incomingNumber)326 public void notifyCallStateChangedForAllSubscriptions(@CallState int state, 327 @Nullable String incomingNumber) { 328 try { 329 sRegistry.notifyCallStateForAllSubs(state, incomingNumber); 330 } catch (RemoteException ex) { 331 // system server crash 332 } 333 } 334 335 /** 336 * Notify {@link SubscriptionInfo} change. 337 * @hide 338 */ notifySubscriptionInfoChanged()339 public void notifySubscriptionInfoChanged() { 340 try { 341 sRegistry.notifySubscriptionInfoChanged(); 342 } catch (RemoteException ex) { 343 // system server crash 344 } 345 } 346 347 /** 348 * Notify opportunistic {@link SubscriptionInfo} change. 349 * @hide 350 */ notifyOpportunisticSubscriptionInfoChanged()351 public void notifyOpportunisticSubscriptionInfoChanged() { 352 try { 353 sRegistry.notifyOpportunisticSubscriptionInfoChanged(); 354 } catch (RemoteException ex) { 355 // system server crash 356 } 357 } 358 359 /** 360 * Notify {@link ServiceState} update on certain subscription. 361 * 362 * @param slotIndex for which the service state changed. Can be derived from subId except 363 * subId is invalid. 364 * @param subId for which the service state changed. 365 * @param state service state e.g, in service, out of service or roaming status. 366 */ notifyServiceStateChanged(int slotIndex, int subId, @NonNull ServiceState state)367 public void notifyServiceStateChanged(int slotIndex, int subId, @NonNull ServiceState state) { 368 try { 369 sRegistry.notifyServiceStateForPhoneId(slotIndex, subId, state); 370 } catch (RemoteException ex) { 371 // system server crash 372 } 373 } 374 375 /** 376 * Notify {@link SignalStrength} update on certain subscription. 377 * 378 * @param slotIndex for which the signalstrength changed. Can be derived from subId except when 379 * subId is invalid. 380 * @param subId for which the signalstrength changed. 381 * @param signalStrength e.g, signalstrength level {@see SignalStrength#getLevel()} 382 */ notifySignalStrengthChanged(int slotIndex, int subId, @NonNull SignalStrength signalStrength)383 public void notifySignalStrengthChanged(int slotIndex, int subId, 384 @NonNull SignalStrength signalStrength) { 385 try { 386 sRegistry.notifySignalStrengthForPhoneId(slotIndex, subId, signalStrength); 387 } catch (RemoteException ex) { 388 // system server crash 389 } 390 } 391 392 /** 393 * Notify changes to the message-waiting indicator on certain subscription. e.g, The status bar 394 * uses message waiting indicator to determine when to display the voicemail icon. 395 * 396 * @param slotIndex for which message waiting indicator changed. Can be derived from subId 397 * except when subId is invalid. 398 * @param subId for which message waiting indicator changed. 399 * @param msgWaitingInd {@code true} indicates there is message-waiting indicator, {@code false} 400 * otherwise. 401 */ notifyMessageWaitingChanged(int slotIndex, int subId, boolean msgWaitingInd)402 public void notifyMessageWaitingChanged(int slotIndex, int subId, boolean msgWaitingInd) { 403 try { 404 sRegistry.notifyMessageWaitingChangedForPhoneId(slotIndex, subId, msgWaitingInd); 405 } catch (RemoteException ex) { 406 // system process is dead 407 } 408 } 409 410 /** 411 * Notify changes to the call-forwarding status on certain subscription. 412 * 413 * @param subId for which call forwarding status changed. 414 * @param callForwardInd {@code true} indicates there is call forwarding, {@code false} 415 * otherwise. 416 */ notifyCallForwardingChanged(int subId, boolean callForwardInd)417 public void notifyCallForwardingChanged(int subId, boolean callForwardInd) { 418 try { 419 sRegistry.notifyCallForwardingChangedForSubscriber(subId, callForwardInd); 420 } catch (RemoteException ex) { 421 // system process is dead 422 } 423 } 424 425 /** 426 * Notify changes to activity state changes on certain subscription. 427 * 428 * @param subId for which data activity state changed. 429 * @param dataActivityType indicates the latest data activity type e.g, {@link 430 * TelephonyManager#DATA_ACTIVITY_IN} 431 */ notifyDataActivityChanged(int subId, @DataActivityType int dataActivityType)432 public void notifyDataActivityChanged(int subId, @DataActivityType int dataActivityType) { 433 try { 434 sRegistry.notifyDataActivityForSubscriber(subId, dataActivityType); 435 } catch (RemoteException ex) { 436 // system process is dead 437 } 438 } 439 440 /** 441 * Notify changes to default (Internet) data connection state on certain subscription. 442 * 443 * @param slotIndex for which data connections state changed. Can be derived from subId except 444 * when subId is invalid. 445 * @param subId for which data connection state changed. 446 * @param preciseState the PreciseDataConnectionState 447 * 448 * @see PreciseDataConnectionState 449 * @see TelephonyManager#DATA_DISCONNECTED 450 */ notifyDataConnectionForSubscriber(int slotIndex, int subId, @NonNull PreciseDataConnectionState preciseState)451 public void notifyDataConnectionForSubscriber(int slotIndex, int subId, 452 @NonNull PreciseDataConnectionState preciseState) { 453 try { 454 sRegistry.notifyDataConnectionForSubscriber( 455 slotIndex, subId, preciseState); 456 } catch (RemoteException ex) { 457 // system process is dead 458 } 459 } 460 461 /** 462 * Notify {@link CallQuality} change on certain subscription. 463 * 464 * @param slotIndex for which call quality state changed. Can be derived from subId except when 465 * subId is invalid. 466 * @param subId for which call quality state changed. 467 * @param callQuality Information about call quality e.g, call quality level 468 * @param networkType associated with this data connection. e.g, LTE 469 */ notifyCallQualityChanged(int slotIndex, int subId, @NonNull CallQuality callQuality, @NetworkType int networkType)470 public void notifyCallQualityChanged(int slotIndex, int subId, @NonNull CallQuality callQuality, 471 @NetworkType int networkType) { 472 try { 473 sRegistry.notifyCallQualityChanged(callQuality, slotIndex, subId, networkType); 474 } catch (RemoteException ex) { 475 // system process is dead 476 } 477 } 478 479 /** 480 * Notify emergency number list changed on certain subscription. 481 * 482 * @param slotIndex for which emergency number list changed. Can be derived from subId except 483 * when subId is invalid. 484 * @param subId for which emergency number list changed. 485 */ notifyEmergencyNumberList( int slotIndex, int subId)486 public void notifyEmergencyNumberList( int slotIndex, int subId) { 487 try { 488 sRegistry.notifyEmergencyNumberList(slotIndex, subId); 489 } catch (RemoteException ex) { 490 // system process is dead 491 } 492 } 493 494 /** 495 * Notify outgoing emergency call. 496 * @param phoneId Sender phone ID. 497 * @param subId Sender subscription ID. 498 * @param emergencyNumber Emergency number. 499 */ notifyOutgoingEmergencyCall(int phoneId, int subId, @NonNull EmergencyNumber emergencyNumber)500 public void notifyOutgoingEmergencyCall(int phoneId, int subId, 501 @NonNull EmergencyNumber emergencyNumber) { 502 try { 503 sRegistry.notifyOutgoingEmergencyCall(phoneId, subId, emergencyNumber); 504 } catch (RemoteException ex) { 505 // system process is dead 506 } 507 } 508 509 /** 510 * Notify outgoing emergency SMS. 511 * @param phoneId Sender phone ID. 512 * @param subId Sender subscription ID. 513 * @param emergencyNumber Emergency number. 514 */ notifyOutgoingEmergencySms(int phoneId, int subId, @NonNull EmergencyNumber emergencyNumber)515 public void notifyOutgoingEmergencySms(int phoneId, int subId, 516 @NonNull EmergencyNumber emergencyNumber) { 517 try { 518 sRegistry.notifyOutgoingEmergencySms(phoneId, subId, emergencyNumber); 519 } catch (RemoteException ex) { 520 // system process is dead 521 } 522 } 523 524 /** 525 * Notify radio power state changed on certain subscription. 526 * 527 * @param slotIndex for which radio power state changed. Can be derived from subId except when 528 * subId is invalid. 529 * @param subId for which radio power state changed. 530 * @param radioPowerState the current modem radio state. 531 */ notifyRadioPowerStateChanged(int slotIndex, int subId, @RadioPowerState int radioPowerState)532 public void notifyRadioPowerStateChanged(int slotIndex, int subId, 533 @RadioPowerState int radioPowerState) { 534 try { 535 sRegistry.notifyRadioPowerStateChanged(slotIndex, subId, radioPowerState); 536 } catch (RemoteException ex) { 537 // system process is dead 538 } 539 } 540 541 /** 542 * Notify {@link PhoneCapability} changed. 543 * 544 * @param phoneCapability the capability of the modem group. 545 */ notifyPhoneCapabilityChanged(@onNull PhoneCapability phoneCapability)546 public void notifyPhoneCapabilityChanged(@NonNull PhoneCapability phoneCapability) { 547 try { 548 sRegistry.notifyPhoneCapabilityChanged(phoneCapability); 549 } catch (RemoteException ex) { 550 // system process is dead 551 } 552 } 553 554 /** 555 * Sim activation type: voice 556 * @see #notifyVoiceActivationStateChanged 557 * @hide 558 */ 559 public static final int SIM_ACTIVATION_TYPE_VOICE = 0; 560 /** 561 * Sim activation type: data 562 * @see #notifyDataActivationStateChanged 563 * @hide 564 */ 565 public static final int SIM_ACTIVATION_TYPE_DATA = 1; 566 567 /** 568 * Notify data activation state changed on certain subscription. 569 * @see TelephonyManager#getDataActivationState() 570 * 571 * @param slotIndex for which data activation state changed. Can be derived from subId except 572 * when subId is invalid. 573 * @param subId for which data activation state changed. 574 * @param activationState sim activation state e.g, activated. 575 */ notifyDataActivationStateChanged(int slotIndex, int subId, @SimActivationState int activationState)576 public void notifyDataActivationStateChanged(int slotIndex, int subId, 577 @SimActivationState int activationState) { 578 try { 579 sRegistry.notifySimActivationStateChangedForPhoneId(slotIndex, subId, 580 SIM_ACTIVATION_TYPE_DATA, activationState); 581 } catch (RemoteException ex) { 582 // system process is dead 583 } 584 } 585 586 /** 587 * Notify voice activation state changed on certain subscription. 588 * @see TelephonyManager#getVoiceActivationState() 589 * 590 * @param slotIndex for which voice activation state changed. Can be derived from subId except 591 * subId is invalid. 592 * @param subId for which voice activation state changed. 593 * @param activationState sim activation state e.g, activated. 594 */ notifyVoiceActivationStateChanged(int slotIndex, int subId, @SimActivationState int activationState)595 public void notifyVoiceActivationStateChanged(int slotIndex, int subId, 596 @SimActivationState int activationState) { 597 try { 598 sRegistry.notifySimActivationStateChangedForPhoneId(slotIndex, subId, 599 SIM_ACTIVATION_TYPE_VOICE, activationState); 600 } catch (RemoteException ex) { 601 // system process is dead 602 } 603 } 604 605 /** 606 * Notify User mobile data state changed on certain subscription. e.g, mobile data is enabled 607 * or disabled. 608 * 609 * @param slotIndex for which mobile data state has changed. Can be derived from subId except 610 * when subId is invalid. 611 * @param subId for which mobile data state has changed. 612 * @param state {@code true} indicates mobile data is enabled/on. {@code false} otherwise. 613 */ notifyUserMobileDataStateChanged(int slotIndex, int subId, boolean state)614 public void notifyUserMobileDataStateChanged(int slotIndex, int subId, boolean state) { 615 try { 616 sRegistry.notifyUserMobileDataStateChangedForPhoneId(slotIndex, subId, state); 617 } catch (RemoteException ex) { 618 // system process is dead 619 } 620 } 621 622 /** 623 * Notify display info changed. 624 * 625 * @param slotIndex The SIM slot index for which display info has changed. Can be 626 * derived from {@code subscriptionId} except when {@code subscriptionId} is invalid, such as 627 * when the device is in emergency-only mode. 628 * @param subscriptionId Subscription id for which display network info has changed. 629 * @param telephonyDisplayInfo The display info. 630 */ notifyDisplayInfoChanged(int slotIndex, int subscriptionId, @NonNull TelephonyDisplayInfo telephonyDisplayInfo)631 public void notifyDisplayInfoChanged(int slotIndex, int subscriptionId, 632 @NonNull TelephonyDisplayInfo telephonyDisplayInfo) { 633 try { 634 sRegistry.notifyDisplayInfoChanged(slotIndex, subscriptionId, telephonyDisplayInfo); 635 } catch (RemoteException ex) { 636 // system process is dead 637 } 638 } 639 640 /** 641 * Notify IMS call disconnect causes which contains {@link android.telephony.ims.ImsReasonInfo}. 642 * 643 * @param subId for which ims call disconnect. 644 * @param imsReasonInfo the reason for ims call disconnect. 645 */ notifyImsDisconnectCause(int subId, @NonNull ImsReasonInfo imsReasonInfo)646 public void notifyImsDisconnectCause(int subId, @NonNull ImsReasonInfo imsReasonInfo) { 647 try { 648 sRegistry.notifyImsDisconnectCause(subId, imsReasonInfo); 649 } catch (RemoteException ex) { 650 // system process is dead 651 } 652 } 653 654 /** 655 * Notify single Radio Voice Call Continuity (SRVCC) state change for the currently active call 656 * on certain subscription. 657 * 658 * @param subId for which srvcc state changed. 659 * @param state srvcc state 660 */ notifySrvccStateChanged(int subId, @SrvccState int state)661 public void notifySrvccStateChanged(int subId, @SrvccState int state) { 662 try { 663 sRegistry.notifySrvccStateChanged(subId, state); 664 } catch (RemoteException ex) { 665 // system process is dead 666 } 667 } 668 669 /** 670 * Notify precise call state changed on certain subscription, including foreground, background 671 * and ringcall states. 672 * 673 * @param slotIndex for which precise call state changed. Can be derived from subId except when 674 * subId is invalid. 675 * @param subId for which precise call state changed. 676 * @param ringCallPreciseState ringCall state. 677 * @param foregroundCallPreciseState foreground call state. 678 * @param backgroundCallPreciseState background call state. 679 */ notifyPreciseCallState(int slotIndex, int subId, @PreciseCallStates int ringCallPreciseState, @PreciseCallStates int foregroundCallPreciseState, @PreciseCallStates int backgroundCallPreciseState)680 public void notifyPreciseCallState(int slotIndex, int subId, 681 @PreciseCallStates int ringCallPreciseState, 682 @PreciseCallStates int foregroundCallPreciseState, 683 @PreciseCallStates int backgroundCallPreciseState) { 684 try { 685 sRegistry.notifyPreciseCallState(slotIndex, subId, ringCallPreciseState, 686 foregroundCallPreciseState, backgroundCallPreciseState); 687 } catch (RemoteException ex) { 688 // system process is dead 689 } 690 } 691 692 /** 693 * Notify call disconnect causes which contains {@link DisconnectCause} and {@link 694 * android.telephony.PreciseDisconnectCause}. 695 * 696 * @param slotIndex for which call disconnected. Can be derived from subId except when subId is 697 * invalid. 698 * @param subId for which call disconnected. 699 * @param cause {@link DisconnectCause} for the disconnected call. 700 * @param preciseCause {@link android.telephony.PreciseDisconnectCause} for the disconnected 701 * call. 702 */ notifyDisconnectCause(int slotIndex, int subId, @DisconnectCauses int cause, @PreciseDisconnectCauses int preciseCause)703 public void notifyDisconnectCause(int slotIndex, int subId, @DisconnectCauses int cause, 704 @PreciseDisconnectCauses int preciseCause) { 705 try { 706 sRegistry.notifyDisconnectCause(slotIndex, subId, cause, preciseCause); 707 } catch (RemoteException ex) { 708 // system process is dead 709 } 710 } 711 712 /** 713 * Notify {@link android.telephony.CellLocation} changed. 714 * 715 * <p>To be compatible with {@link TelephonyRegistry}, use {@link CellIdentity} which is 716 * parcelable, and convert to CellLocation in client code. 717 */ notifyCellLocation(int subId, @NonNull CellIdentity cellLocation)718 public void notifyCellLocation(int subId, @NonNull CellIdentity cellLocation) { 719 try { 720 sRegistry.notifyCellLocationForSubscriber(subId, cellLocation); 721 } catch (RemoteException ex) { 722 // system process is dead 723 } 724 } 725 726 /** 727 * Notify {@link CellInfo} changed on certain subscription. e.g, when an observed cell info has 728 * changed or new cells have been added or removed on the given subscription. 729 * 730 * @param subId for which cellinfo changed. 731 * @param cellInfo A list of cellInfo associated with the given subscription. 732 */ notifyCellInfoChanged(int subId, @NonNull List<CellInfo> cellInfo)733 public void notifyCellInfoChanged(int subId, @NonNull List<CellInfo> cellInfo) { 734 try { 735 sRegistry.notifyCellInfoForSubscriber(subId, cellInfo); 736 } catch (RemoteException ex) { 737 738 } 739 } 740 741 /** 742 * Notify that the active data subscription ID has changed. 743 * @param activeDataSubId The new subscription ID for active data 744 */ notifyActiveDataSubIdChanged(int activeDataSubId)745 public void notifyActiveDataSubIdChanged(int activeDataSubId) { 746 try { 747 sRegistry.notifyActiveDataSubIdChanged(activeDataSubId); 748 } catch (RemoteException ex) { 749 750 } 751 } 752 753 /** 754 * Report that Registration or a Location/Routing/Tracking Area update has failed. 755 * 756 * @param slotIndex for which call disconnected. Can be derived from subId except when subId is 757 * invalid. 758 * @param subId for which cellinfo changed. 759 * @param cellIdentity the CellIdentity, which must include the globally unique identifier 760 * for the cell (for example, all components of the CGI or ECGI). 761 * @param chosenPlmn a 5 or 6 digit alphanumeric PLMN (MCC|MNC) among those broadcast by the 762 * cell that was chosen for the failed registration attempt. 763 * @param domain DOMAIN_CS, DOMAIN_PS or both in case of a combined procedure. 764 * @param causeCode the primary failure cause code of the procedure. 765 * For GSM/UMTS (MM), values are in TS 24.008 Sec 10.5.95 766 * For GSM/UMTS (GMM), values are in TS 24.008 Sec 10.5.147 767 * For LTE (EMM), cause codes are TS 24.301 Sec 9.9.3.9 768 * For NR (5GMM), cause codes are TS 24.501 Sec 9.11.3.2 769 * Integer.MAX_VALUE if this value is unused. 770 * @param additionalCauseCode the cause code of any secondary/combined procedure if appropriate. 771 * For UMTS, if a combined attach succeeds for PS only, then the GMM cause code shall be 772 * included as an additionalCauseCode. For LTE (ESM), cause codes are in 773 * TS 24.301 9.9.4.4. Integer.MAX_VALUE if this value is unused. 774 */ notifyRegistrationFailed(int slotIndex, int subId, @NonNull CellIdentity cellIdentity, @NonNull String chosenPlmn, int domain, int causeCode, int additionalCauseCode)775 public void notifyRegistrationFailed(int slotIndex, int subId, 776 @NonNull CellIdentity cellIdentity, @NonNull String chosenPlmn, 777 int domain, int causeCode, int additionalCauseCode) { 778 try { 779 sRegistry.notifyRegistrationFailed(slotIndex, subId, cellIdentity, 780 chosenPlmn, domain, causeCode, additionalCauseCode); 781 } catch (RemoteException ex) { 782 } 783 } 784 785 /** 786 * Notify {@link BarringInfo} has changed for a specific subscription. 787 * 788 * @param slotIndex for the phone object that got updated barring info. 789 * @param subId for which the BarringInfo changed. 790 * @param barringInfo updated BarringInfo. 791 */ notifyBarringInfoChanged( int slotIndex, int subId, @NonNull BarringInfo barringInfo)792 public void notifyBarringInfoChanged( 793 int slotIndex, int subId, @NonNull BarringInfo barringInfo) { 794 try { 795 sRegistry.notifyBarringInfoChanged(slotIndex, subId, barringInfo); 796 } catch (RemoteException ex) { 797 // system server crash 798 } 799 } 800 801 /** 802 * Notify {@link PhysicalChannelConfig} has changed for a specific subscription. 803 * 804 * @param slotIndex for which physical channel configs changed. 805 * @param subId the subId 806 * @param configs a list of {@link PhysicalChannelConfig}, the configs of physical channel. 807 */ notifyPhysicalChannelConfigForSubscriber(int slotIndex, int subId, List<PhysicalChannelConfig> configs)808 public void notifyPhysicalChannelConfigForSubscriber(int slotIndex, int subId, 809 List<PhysicalChannelConfig> configs) { 810 try { 811 sRegistry.notifyPhysicalChannelConfigForSubscriber(slotIndex, subId, configs); 812 } catch (RemoteException ex) { 813 // system server crash 814 } 815 } 816 817 /** 818 * Notify that the data enabled has changed. 819 * 820 * @param enabled True if data is enabled, otherwise disabled. 821 * @param reason Reason for data enabled/disabled. See {@code REASON_*} in 822 * {@link TelephonyManager}. 823 */ notifyDataEnabled(int slotIndex, int subId, boolean enabled, @TelephonyManager.DataEnabledReason int reason)824 public void notifyDataEnabled(int slotIndex, int subId, boolean enabled, 825 @TelephonyManager.DataEnabledReason int reason) { 826 try { 827 sRegistry.notifyDataEnabled(slotIndex, subId, enabled, reason); 828 } catch (RemoteException ex) { 829 // system server crash 830 } 831 } 832 833 /** 834 * Notify the allowed network types has changed for a specific subscription and the specific 835 * reason. 836 * @param slotIndex for which allowed network types changed. 837 * @param subId for which allowed network types changed. 838 * @param reason an allowed network type reasons. 839 * @param allowedNetworkType an allowed network type bitmask value. 840 */ notifyAllowedNetworkTypesChanged(int slotIndex, int subId, int reason, long allowedNetworkType)841 public void notifyAllowedNetworkTypesChanged(int slotIndex, int subId, 842 int reason, long allowedNetworkType) { 843 try { 844 sRegistry.notifyAllowedNetworkTypesChanged(slotIndex, subId, reason, 845 allowedNetworkType); 846 } catch (RemoteException ex) { 847 // system process is dead 848 } 849 } 850 851 /** 852 * Notify that the link capacity estimate has changed. 853 * @param slotIndex for the phone object that gets the updated link capacity estimate 854 * @param subId for subscription that gets the updated link capacity estimate 855 * @param linkCapacityEstimateList a list of {@link LinkCapacityEstimate} 856 */ notifyLinkCapacityEstimateChanged(int slotIndex, int subId, List<LinkCapacityEstimate> linkCapacityEstimateList)857 public void notifyLinkCapacityEstimateChanged(int slotIndex, int subId, 858 List<LinkCapacityEstimate> linkCapacityEstimateList) { 859 try { 860 sRegistry.notifyLinkCapacityEstimateChanged(slotIndex, subId, linkCapacityEstimateList); 861 } catch (RemoteException ex) { 862 // system server crash 863 } 864 } 865 getEventsFromCallback( @onNull TelephonyCallback telephonyCallback)866 public @NonNull Set<Integer> getEventsFromCallback( 867 @NonNull TelephonyCallback telephonyCallback) { 868 Set<Integer> eventList = new ArraySet<>(); 869 870 if (telephonyCallback instanceof TelephonyCallback.ServiceStateListener) { 871 eventList.add(TelephonyCallback.EVENT_SERVICE_STATE_CHANGED); 872 } 873 874 if (telephonyCallback instanceof TelephonyCallback.MessageWaitingIndicatorListener) { 875 eventList.add(TelephonyCallback.EVENT_MESSAGE_WAITING_INDICATOR_CHANGED); 876 } 877 878 if (telephonyCallback instanceof TelephonyCallback.CallForwardingIndicatorListener) { 879 eventList.add(TelephonyCallback.EVENT_CALL_FORWARDING_INDICATOR_CHANGED); 880 } 881 882 if (telephonyCallback instanceof TelephonyCallback.CellLocationListener) { 883 eventList.add(TelephonyCallback.EVENT_CELL_LOCATION_CHANGED); 884 } 885 886 // Note: Legacy PhoneStateListeners use EVENT_LEGACY_CALL_STATE_CHANGED 887 if (telephonyCallback instanceof TelephonyCallback.CallStateListener) { 888 eventList.add(TelephonyCallback.EVENT_CALL_STATE_CHANGED); 889 } 890 891 if (telephonyCallback instanceof TelephonyCallback.DataConnectionStateListener) { 892 eventList.add(TelephonyCallback.EVENT_DATA_CONNECTION_STATE_CHANGED); 893 } 894 895 if (telephonyCallback instanceof TelephonyCallback.DataActivityListener) { 896 eventList.add(TelephonyCallback.EVENT_DATA_ACTIVITY_CHANGED); 897 } 898 899 if (telephonyCallback instanceof TelephonyCallback.SignalStrengthsListener) { 900 eventList.add(TelephonyCallback.EVENT_SIGNAL_STRENGTHS_CHANGED); 901 } 902 903 if (telephonyCallback instanceof TelephonyCallback.CellInfoListener) { 904 eventList.add(TelephonyCallback.EVENT_CELL_INFO_CHANGED); 905 } 906 907 if (telephonyCallback instanceof TelephonyCallback.PreciseCallStateListener) { 908 eventList.add(TelephonyCallback.EVENT_PRECISE_CALL_STATE_CHANGED); 909 } 910 911 if (telephonyCallback instanceof TelephonyCallback.CallDisconnectCauseListener) { 912 eventList.add(TelephonyCallback.EVENT_CALL_DISCONNECT_CAUSE_CHANGED); 913 } 914 915 if (telephonyCallback instanceof TelephonyCallback.ImsCallDisconnectCauseListener) { 916 eventList.add(TelephonyCallback.EVENT_IMS_CALL_DISCONNECT_CAUSE_CHANGED); 917 } 918 919 if (telephonyCallback instanceof TelephonyCallback.PreciseDataConnectionStateListener) { 920 eventList.add(TelephonyCallback.EVENT_PRECISE_DATA_CONNECTION_STATE_CHANGED); 921 } 922 923 if (telephonyCallback instanceof TelephonyCallback.SrvccStateListener) { 924 eventList.add(TelephonyCallback.EVENT_SRVCC_STATE_CHANGED); 925 } 926 927 if (telephonyCallback instanceof TelephonyCallback.VoiceActivationStateListener) { 928 eventList.add(TelephonyCallback.EVENT_VOICE_ACTIVATION_STATE_CHANGED); 929 } 930 931 if (telephonyCallback instanceof TelephonyCallback.DataActivationStateListener) { 932 eventList.add(TelephonyCallback.EVENT_DATA_ACTIVATION_STATE_CHANGED); 933 } 934 935 if (telephonyCallback instanceof TelephonyCallback.UserMobileDataStateListener) { 936 eventList.add(TelephonyCallback.EVENT_USER_MOBILE_DATA_STATE_CHANGED); 937 } 938 939 if (telephonyCallback instanceof TelephonyCallback.DisplayInfoListener) { 940 eventList.add(TelephonyCallback.EVENT_DISPLAY_INFO_CHANGED); 941 } 942 943 if (telephonyCallback instanceof TelephonyCallback.EmergencyNumberListListener) { 944 eventList.add(TelephonyCallback.EVENT_EMERGENCY_NUMBER_LIST_CHANGED); 945 } 946 947 if (telephonyCallback instanceof TelephonyCallback.OutgoingEmergencyCallListener) { 948 eventList.add(TelephonyCallback.EVENT_OUTGOING_EMERGENCY_CALL); 949 } 950 951 if (telephonyCallback instanceof TelephonyCallback.OutgoingEmergencySmsListener) { 952 eventList.add(TelephonyCallback.EVENT_OUTGOING_EMERGENCY_SMS); 953 } 954 955 if (telephonyCallback instanceof TelephonyCallback.PhoneCapabilityListener) { 956 eventList.add(TelephonyCallback.EVENT_PHONE_CAPABILITY_CHANGED); 957 } 958 959 if (telephonyCallback instanceof TelephonyCallback.ActiveDataSubscriptionIdListener) { 960 eventList.add(TelephonyCallback.EVENT_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGED); 961 } 962 963 if (telephonyCallback instanceof TelephonyCallback.RadioPowerStateListener) { 964 eventList.add(TelephonyCallback.EVENT_RADIO_POWER_STATE_CHANGED); 965 } 966 967 if (telephonyCallback instanceof TelephonyCallback.CarrierNetworkListener) { 968 eventList.add(TelephonyCallback.EVENT_CARRIER_NETWORK_CHANGED); 969 } 970 971 if (telephonyCallback instanceof TelephonyCallback.RegistrationFailedListener) { 972 eventList.add(TelephonyCallback.EVENT_REGISTRATION_FAILURE); 973 } 974 975 if (telephonyCallback instanceof TelephonyCallback.CallAttributesListener) { 976 eventList.add(TelephonyCallback.EVENT_CALL_ATTRIBUTES_CHANGED); 977 } 978 979 if (telephonyCallback instanceof TelephonyCallback.BarringInfoListener) { 980 eventList.add(TelephonyCallback.EVENT_BARRING_INFO_CHANGED); 981 } 982 983 if (telephonyCallback instanceof TelephonyCallback.PhysicalChannelConfigListener) { 984 eventList.add(TelephonyCallback.EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED); 985 } 986 987 if (telephonyCallback instanceof TelephonyCallback.DataEnabledListener) { 988 eventList.add(TelephonyCallback.EVENT_DATA_ENABLED_CHANGED); 989 } 990 991 if (telephonyCallback instanceof TelephonyCallback.AllowedNetworkTypesListener) { 992 eventList.add(TelephonyCallback.EVENT_ALLOWED_NETWORK_TYPE_LIST_CHANGED); 993 } 994 995 if (telephonyCallback instanceof TelephonyCallback.LinkCapacityEstimateChangedListener) { 996 eventList.add(TelephonyCallback.EVENT_LINK_CAPACITY_ESTIMATE_CHANGED); 997 } 998 999 return eventList; 1000 } 1001 getEventsFromBitmask(int eventMask)1002 private @NonNull Set<Integer> getEventsFromBitmask(int eventMask) { 1003 1004 Set<Integer> eventList = new ArraySet<>(); 1005 1006 if ((eventMask & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) { 1007 eventList.add(TelephonyCallback.EVENT_SERVICE_STATE_CHANGED); 1008 } 1009 1010 if ((eventMask & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) { 1011 eventList.add(TelephonyCallback.EVENT_SIGNAL_STRENGTH_CHANGED); 1012 } 1013 1014 if ((eventMask & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) { 1015 eventList.add(TelephonyCallback.EVENT_MESSAGE_WAITING_INDICATOR_CHANGED); 1016 } 1017 1018 if ((eventMask & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) { 1019 eventList.add(TelephonyCallback.EVENT_CALL_FORWARDING_INDICATOR_CHANGED); 1020 } 1021 1022 if ((eventMask & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) { 1023 eventList.add(TelephonyCallback.EVENT_CELL_LOCATION_CHANGED); 1024 } 1025 1026 // Note: Legacy call state listeners can get the phone number which is not provided in the 1027 // new version in TelephonyCallback. 1028 if ((eventMask & PhoneStateListener.LISTEN_CALL_STATE) != 0) { 1029 eventList.add(TelephonyCallback.EVENT_LEGACY_CALL_STATE_CHANGED); 1030 } 1031 1032 if ((eventMask & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) { 1033 eventList.add(TelephonyCallback.EVENT_DATA_CONNECTION_STATE_CHANGED); 1034 } 1035 1036 if ((eventMask & PhoneStateListener.LISTEN_DATA_ACTIVITY) != 0) { 1037 eventList.add(TelephonyCallback.EVENT_DATA_ACTIVITY_CHANGED); 1038 } 1039 1040 if ((eventMask & PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) != 0) { 1041 eventList.add(TelephonyCallback.EVENT_SIGNAL_STRENGTHS_CHANGED); 1042 } 1043 1044 if ((eventMask & PhoneStateListener.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH) != 0) { 1045 eventList.add(TelephonyCallback.EVENT_ALWAYS_REPORTED_SIGNAL_STRENGTH_CHANGED); 1046 } 1047 1048 if ((eventMask & PhoneStateListener.LISTEN_CELL_INFO) != 0) { 1049 eventList.add(TelephonyCallback.EVENT_CELL_INFO_CHANGED); 1050 } 1051 1052 if ((eventMask & PhoneStateListener.LISTEN_PRECISE_CALL_STATE) != 0) { 1053 eventList.add(TelephonyCallback.EVENT_PRECISE_CALL_STATE_CHANGED); 1054 } 1055 1056 if ((eventMask & PhoneStateListener.LISTEN_PRECISE_DATA_CONNECTION_STATE) != 0) { 1057 eventList.add(TelephonyCallback.EVENT_PRECISE_DATA_CONNECTION_STATE_CHANGED); 1058 } 1059 1060 if ((eventMask & PhoneStateListener.LISTEN_DATA_CONNECTION_REAL_TIME_INFO) != 0) { 1061 eventList.add(TelephonyCallback.EVENT_DATA_CONNECTION_REAL_TIME_INFO_CHANGED); 1062 } 1063 1064 if ((eventMask & PhoneStateListener.LISTEN_OEM_HOOK_RAW_EVENT) != 0) { 1065 eventList.add(TelephonyCallback.EVENT_OEM_HOOK_RAW); 1066 } 1067 1068 if ((eventMask & PhoneStateListener.LISTEN_SRVCC_STATE_CHANGED) != 0) { 1069 eventList.add(TelephonyCallback.EVENT_SRVCC_STATE_CHANGED); 1070 } 1071 1072 if ((eventMask & PhoneStateListener.LISTEN_CARRIER_NETWORK_CHANGE) != 0) { 1073 eventList.add(TelephonyCallback.EVENT_CARRIER_NETWORK_CHANGED); 1074 } 1075 1076 if ((eventMask & PhoneStateListener.LISTEN_VOICE_ACTIVATION_STATE) != 0) { 1077 eventList.add(TelephonyCallback.EVENT_VOICE_ACTIVATION_STATE_CHANGED); 1078 } 1079 1080 if ((eventMask & PhoneStateListener.LISTEN_DATA_ACTIVATION_STATE) != 0) { 1081 eventList.add(TelephonyCallback.EVENT_DATA_ACTIVATION_STATE_CHANGED); 1082 } 1083 1084 if ((eventMask & PhoneStateListener.LISTEN_USER_MOBILE_DATA_STATE) != 0) { 1085 eventList.add(TelephonyCallback.EVENT_USER_MOBILE_DATA_STATE_CHANGED); 1086 } 1087 1088 if ((eventMask & PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED) != 0) { 1089 eventList.add(TelephonyCallback.EVENT_DISPLAY_INFO_CHANGED); 1090 } 1091 1092 if ((eventMask & PhoneStateListener.LISTEN_PHONE_CAPABILITY_CHANGE) != 0) { 1093 eventList.add(TelephonyCallback.EVENT_PHONE_CAPABILITY_CHANGED); 1094 } 1095 1096 if ((eventMask & PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE) != 0) { 1097 eventList.add(TelephonyCallback.EVENT_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGED); 1098 } 1099 1100 if ((eventMask & PhoneStateListener.LISTEN_RADIO_POWER_STATE_CHANGED) != 0) { 1101 eventList.add(TelephonyCallback.EVENT_RADIO_POWER_STATE_CHANGED); 1102 } 1103 1104 if ((eventMask & PhoneStateListener.LISTEN_EMERGENCY_NUMBER_LIST) != 0) { 1105 eventList.add(TelephonyCallback.EVENT_EMERGENCY_NUMBER_LIST_CHANGED); 1106 } 1107 1108 if ((eventMask & PhoneStateListener.LISTEN_CALL_DISCONNECT_CAUSES) != 0) { 1109 eventList.add(TelephonyCallback.EVENT_CALL_DISCONNECT_CAUSE_CHANGED); 1110 } 1111 1112 if ((eventMask & PhoneStateListener.LISTEN_CALL_ATTRIBUTES_CHANGED) != 0) { 1113 eventList.add(TelephonyCallback.EVENT_CALL_ATTRIBUTES_CHANGED); 1114 } 1115 1116 if ((eventMask & PhoneStateListener.LISTEN_IMS_CALL_DISCONNECT_CAUSES) != 0) { 1117 eventList.add(TelephonyCallback.EVENT_IMS_CALL_DISCONNECT_CAUSE_CHANGED); 1118 } 1119 1120 if ((eventMask & PhoneStateListener.LISTEN_OUTGOING_EMERGENCY_CALL) != 0) { 1121 eventList.add(TelephonyCallback.EVENT_OUTGOING_EMERGENCY_CALL); 1122 } 1123 1124 if ((eventMask & PhoneStateListener.LISTEN_OUTGOING_EMERGENCY_SMS) != 0) { 1125 eventList.add(TelephonyCallback.EVENT_OUTGOING_EMERGENCY_SMS); 1126 } 1127 1128 if ((eventMask & PhoneStateListener.LISTEN_REGISTRATION_FAILURE) != 0) { 1129 eventList.add(TelephonyCallback.EVENT_REGISTRATION_FAILURE); 1130 } 1131 1132 if ((eventMask & PhoneStateListener.LISTEN_BARRING_INFO) != 0) { 1133 eventList.add(TelephonyCallback.EVENT_BARRING_INFO_CHANGED); 1134 } 1135 return eventList; 1136 1137 } 1138 1139 /** 1140 * Registers a callback object to receive notification of changes in specified telephony states. 1141 * <p> 1142 * To register a callback, pass a {@link TelephonyCallback} which implements 1143 * interfaces of events. For example, 1144 * FakeServiceStateCallback extends {@link TelephonyCallback} implements 1145 * {@link TelephonyCallback.ServiceStateListener}. 1146 * 1147 * At registration, and when a specified telephony state changes, the telephony manager invokes 1148 * the appropriate callback method on the callback object and passes the current (updated) 1149 * values. 1150 * <p> 1151 * 1152 * If this TelephonyManager object has been created with 1153 * {@link TelephonyManager#createForSubscriptionId}, applies to the given subId. 1154 * Otherwise, applies to {@link SubscriptionManager#getDefaultSubscriptionId()}. 1155 * To register events for multiple subIds, pass a separate callback object to 1156 * each TelephonyManager object created with {@link TelephonyManager#createForSubscriptionId}. 1157 * 1158 * Note: if you call this method while in the middle of a binder transaction, you <b>must</b> 1159 * call {@link android.os.Binder#clearCallingIdentity()} before calling this method. A 1160 * {@link SecurityException} will be thrown otherwise. 1161 * 1162 * This API should be used sparingly -- large numbers of callbacks will cause system 1163 * instability. If a process has registered too many callbacks without unregistering them, it 1164 * may encounter an {@link IllegalStateException} when trying to register more callbacks. 1165 * 1166 * @param callback The {@link TelephonyCallback} object to register. 1167 */ registerTelephonyCallback(@onNull @allbackExecutor Executor executor, int subId, String pkgName, String attributionTag, @NonNull TelephonyCallback callback, boolean notifyNow)1168 public void registerTelephonyCallback(@NonNull @CallbackExecutor Executor executor, 1169 int subId, String pkgName, String attributionTag, @NonNull TelephonyCallback callback, 1170 boolean notifyNow) { 1171 if (callback == null) { 1172 throw new IllegalStateException("telephony service is null."); 1173 } 1174 callback.init(executor); 1175 listenFromCallback(subId, pkgName, attributionTag, callback, 1176 getEventsFromCallback(callback).stream().mapToInt(i -> i).toArray(), notifyNow); 1177 } 1178 1179 /** 1180 * Unregister an existing {@link TelephonyCallback}. 1181 * 1182 * @param callback The {@link TelephonyCallback} object to unregister. 1183 */ unregisterTelephonyCallback(int subId, String pkgName, String attributionTag, @NonNull TelephonyCallback callback, boolean notifyNow)1184 public void unregisterTelephonyCallback(int subId, String pkgName, String attributionTag, 1185 @NonNull TelephonyCallback callback, boolean notifyNow) { 1186 listenFromCallback(subId, pkgName, attributionTag, callback, new int[0], notifyNow); 1187 } 1188 } 1189