Debian Codegraph @

core/java/android/telephony/TelephonyRegistryManager.java

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