1 /*
2  * Copyright (C) 2009 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.cts;
17 
18 import static androidx.test.InstrumentationRegistry.getContext;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assume.assumeNoException;
29 import static org.junit.Assume.assumeTrue;
30 
31 import android.annotation.NonNull;
32 import android.content.Context;
33 import android.content.pm.PackageManager;
34 import android.net.ConnectivityManager;
35 import android.os.Looper;
36 import android.platform.test.annotations.RequiresFlagsEnabled;
37 import android.platform.test.flag.junit.CheckFlagsRule;
38 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
39 import android.telephony.Annotation.RadioPowerState;
40 import android.telephony.Annotation.SimActivationState;
41 import android.telephony.BarringInfo;
42 import android.telephony.CallState;
43 import android.telephony.CellIdentity;
44 import android.telephony.CellInfo;
45 import android.telephony.CellLocation;
46 import android.telephony.LinkCapacityEstimate;
47 import android.telephony.NetworkRegistrationInfo;
48 import android.telephony.PhysicalChannelConfig;
49 import android.telephony.PreciseCallState;
50 import android.telephony.PreciseDataConnectionState;
51 import android.telephony.ServiceState;
52 import android.telephony.SignalStrength;
53 import android.telephony.SmsManager;
54 import android.telephony.TelephonyCallback;
55 import android.telephony.TelephonyDisplayInfo;
56 import android.telephony.TelephonyManager;
57 import android.telephony.TelephonyManager.DataEnabledReason;
58 import android.telephony.TelephonyManager.EmergencyCallbackModeStopReason;
59 import android.telephony.TelephonyManager.EmergencyCallbackModeType;
60 import android.telephony.cts.util.TelephonyUtils;
61 import android.telephony.emergency.EmergencyNumber;
62 import android.telephony.ims.ImsReasonInfo;
63 import android.text.TextUtils;
64 import android.util.Log;
65 import android.util.Pair;
66 
67 import androidx.test.InstrumentationRegistry;
68 
69 import com.android.compatibility.common.util.ShellIdentityUtils;
70 import com.android.internal.telephony.flags.Flags;
71 
72 import org.junit.Before;
73 import org.junit.Rule;
74 import org.junit.Test;
75 
76 import java.util.Arrays;
77 import java.util.List;
78 import java.util.concurrent.Executor;
79 
80 public class TelephonyCallbackTest {
81     @Rule
82     public final CheckFlagsRule mCheckFlagsRule =
83             DeviceFlagsValueProvider.createCheckFlagsRule();
84 
85     public static final long WAIT_TIME = 1000;
86 
87     private static final String TEST_EMERGENCY_NUMBER = "998877665544332211";
88 
89     private boolean mOnActiveDataSubscriptionIdChanged;
90     private boolean mOnCallForwardingIndicatorChangedCalled;
91     private boolean mOnCallStateChangedCalled;
92     private boolean mOnCellLocationChangedCalled;
93     private boolean mOnUserMobileDataStateChanged;
94     private boolean mOnDataActivityCalled;
95     private boolean mOnDataConnectionStateChangedCalled;
96     private boolean mOnDataConnectionStateChangedWithNetworkTypeCalled;
97     private boolean mOnMessageWaitingIndicatorChangedCalled;
98     private boolean mOnCellInfoChangedCalled;
99     private boolean mOnServiceStateChangedCalled;
100     private boolean mOnPreciseCallStateChangedCalled;
101     private boolean mOnCallStatesChangedCalled;
102     private boolean mOnCallDisconnectCauseChangedCalled;
103     private boolean mOnImsCallDisconnectCauseChangedCalled;
104     private ImsReasonInfo mImsReasonInfo;
105     private EmergencyNumber mOnOutgoingSmsEmergencyNumberChanged;
106     private boolean mOnPreciseDataConnectionStateChanged;
107     private boolean mOnRadioPowerStateChangedCalled;
108     private boolean mVoiceActivationStateChangedCalled;
109     private boolean mSrvccStateChangedCalled;
110     private boolean mOnBarringInfoChangedCalled;
111     private boolean mOnRegistrationFailedCalled;
112     private boolean mOnTelephonyDisplayInfoChanged;
113     private boolean mOnPhysicalChannelConfigCalled;
114     private boolean mOnDataEnabledChangedCalled;
115     private boolean mOnLinkCapacityEstimateChangedCalled;
116     private boolean mOnEmergencyCallbackModeChangedCalled;
117     private boolean mOnCarrierRoamingNtnModeChangedCalled;
118     @RadioPowerState
119     private int mRadioPowerState;
120     @SimActivationState
121     private int mVoiceActivationState;
122     private ServiceState mServiceState;
123     private boolean mOnAllowedNetworkTypesChangedCalled;
124     private int mAllowedNetworkTypeReason = -1;
125     private long mAllowedNetworkTypeValue = -1;
126     private BarringInfo mBarringInfo;
127     private PreciseDataConnectionState mPreciseDataConnectionState;
128     private PreciseCallState mPreciseCallState;
129     private List<CallState> mCallStateList;
130     private SignalStrength mSignalStrength;
131     private TelephonyManager mTelephonyManager;
132     private final Object mLock = new Object();
133     private static final String TAG = "TelephonyCallbackTest";
134     private static ConnectivityManager mCm;
135     private PackageManager mPackageManager;
136     private static final List<Integer> DATA_CONNECTION_STATE = Arrays.asList(
137             TelephonyManager.DATA_CONNECTED,
138             TelephonyManager.DATA_DISCONNECTED,
139             TelephonyManager.DATA_CONNECTING,
140             TelephonyManager.DATA_UNKNOWN,
141             TelephonyManager.DATA_SUSPENDED
142     );
143     private static final List<Integer> PRECISE_CALL_STATE = Arrays.asList(
144             PreciseCallState.PRECISE_CALL_STATE_ACTIVE,
145             PreciseCallState.PRECISE_CALL_STATE_ALERTING,
146             PreciseCallState.PRECISE_CALL_STATE_DIALING,
147             PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED,
148             PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING,
149             PreciseCallState.PRECISE_CALL_STATE_HOLDING,
150             PreciseCallState.PRECISE_CALL_STATE_IDLE,
151             PreciseCallState.PRECISE_CALL_STATE_INCOMING,
152             PreciseCallState.PRECISE_CALL_STATE_NOT_VALID,
153             PreciseCallState.PRECISE_CALL_STATE_WAITING
154     );
155 
156     private static final List<Integer> NETWORK_TYPES = Arrays.asList(
157             TelephonyManager.NETWORK_TYPE_UNKNOWN,
158             TelephonyManager.NETWORK_TYPE_GPRS,
159             TelephonyManager.NETWORK_TYPE_EDGE,
160             TelephonyManager.NETWORK_TYPE_UMTS,
161             TelephonyManager.NETWORK_TYPE_CDMA,
162             TelephonyManager.NETWORK_TYPE_EVDO_0,
163             TelephonyManager.NETWORK_TYPE_EVDO_A,
164             TelephonyManager.NETWORK_TYPE_1xRTT,
165             TelephonyManager.NETWORK_TYPE_HSDPA,
166             TelephonyManager.NETWORK_TYPE_HSUPA,
167             TelephonyManager.NETWORK_TYPE_HSPA,
168             TelephonyManager.NETWORK_TYPE_HSPAP,
169             TelephonyManager.NETWORK_TYPE_LTE,
170             TelephonyManager.NETWORK_TYPE_EHRPD,
171             TelephonyManager.NETWORK_TYPE_GSM,
172             TelephonyManager.NETWORK_TYPE_IWLAN,
173             TelephonyManager.NETWORK_TYPE_NR
174     );
175 
176     private final Executor mSimpleExecutor = Runnable::run;
177 
178     @Before
setUp()179     public void setUp() throws Exception {
180         mPackageManager = getContext().getPackageManager();
181         assumeTrue("Skipping test that requires FEATURE_TELEPHONY",
182                 mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY));
183 
184         mTelephonyManager =
185                 (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
186         try {
187             mTelephonyManager.getHalVersion(TelephonyManager.HAL_SERVICE_RADIO);
188         } catch (IllegalStateException e) {
189             assumeNoException("Skipping tests because Telephony service is null", e);
190         }
191         mCm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
192     }
193 
194     @Test
testTelephonyCallback()195     public void testTelephonyCallback() {
196 
197         Looper.prepare();
198         new TelephonyCallback();
199     }
200 
registerTelephonyCallbackWithPermission(@onNull TelephonyCallback callback)201     private void registerTelephonyCallbackWithPermission(@NonNull TelephonyCallback callback) {
202         ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(mTelephonyManager,
203                 (tm) -> tm.registerTelephonyCallback(mSimpleExecutor, callback));
204     }
205 
registerTelephonyCallback(@onNull TelephonyCallback callback)206     private void registerTelephonyCallback(@NonNull TelephonyCallback callback) {
207         mTelephonyManager.registerTelephonyCallback(mSimpleExecutor, callback);
208     }
209 
registerTelephonyCallback(@onNull TelephonyCallback callback, boolean renounceFine, boolean renounceCoarse)210     private void registerTelephonyCallback(@NonNull TelephonyCallback callback,
211             boolean renounceFine, boolean renounceCoarse) {
212         int includeLocationData = TelephonyManager.INCLUDE_LOCATION_DATA_FINE;
213         if (renounceFine && renounceCoarse) {
214             includeLocationData = TelephonyManager.INCLUDE_LOCATION_DATA_NONE;
215         } else if (renounceFine) {
216             includeLocationData = TelephonyManager.INCLUDE_LOCATION_DATA_COARSE;
217         }
218         mTelephonyManager.registerTelephonyCallback(includeLocationData, mSimpleExecutor,
219                 callback);
220     }
221 
unRegisterTelephonyCallback(boolean condition, @NonNull TelephonyCallback callback)222     private void unRegisterTelephonyCallback(boolean condition,
223                                              @NonNull TelephonyCallback callback) throws Exception {
224         synchronized (mLock) {
225             condition = false;
226             mTelephonyManager.unregisterTelephonyCallback(callback);
227             mLock.wait(WAIT_TIME);
228 
229             assertFalse(condition);
230         }
231     }
232 
233     private ServiceStateListener mServiceStateCallback;
234 
235     private class ServiceStateListener extends TelephonyCallback
236             implements TelephonyCallback.ServiceStateListener {
237         @Override
onServiceStateChanged(ServiceState serviceState)238         public void onServiceStateChanged(ServiceState serviceState) {
239             synchronized (mLock) {
240                 mOnServiceStateChangedCalled = true;
241                 mServiceState = serviceState;
242                 mLock.notify();
243             }
244         }
245     }
246 
247     @Test
testOnServiceStateChangedByRegisterTelephonyCallback()248     public void testOnServiceStateChangedByRegisterTelephonyCallback() throws Throwable {
249         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
250             assumeTrue(mPackageManager.hasSystemFeature(
251                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
252         }
253 
254         assertFalse(mOnServiceStateChangedCalled);
255 
256 
257         mServiceStateCallback = new ServiceStateListener();
258         registerTelephonyCallback(mServiceStateCallback);
259 
260         synchronized (mLock) {
261             if (!mOnServiceStateChangedCalled) {
262                 mLock.wait(WAIT_TIME);
263             }
264         }
265 
266         assertTrue(mOnServiceStateChangedCalled);
267 
268         // Test unregister
269         unRegisterTelephonyCallback(mOnServiceStateChangedCalled, mServiceStateCallback);
270     }
271 
272     @Test
testOnServiceStateChangedByRegisterTelephonyCallbackWithLocationRenounce()273     public void testOnServiceStateChangedByRegisterTelephonyCallbackWithLocationRenounce()
274             throws Throwable {
275         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
276             assumeTrue(mPackageManager.hasSystemFeature(
277                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
278         }
279 
280         if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) {
281             Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE");
282             return;
283         }
284 
285         assertFalse(mOnServiceStateChangedCalled);
286 
287         mServiceStateCallback = new ServiceStateListener();
288         registerTelephonyCallback(mServiceStateCallback, true, true);
289 
290         synchronized (mLock) {
291             if (!mOnServiceStateChangedCalled) {
292                 mLock.wait(WAIT_TIME);
293             }
294         }
295 
296         assertTrue(mOnServiceStateChangedCalled);
297         assertServiceStateLocationSanitization(mServiceState);
298 
299         // Test unregister
300         unRegisterTelephonyCallback(mOnServiceStateChangedCalled, mServiceStateCallback);
301     }
302 
303     @Test
testOnServiceStateChangedByRegisterTelephonyCallbackWithCoarseRenounce()304     public void testOnServiceStateChangedByRegisterTelephonyCallbackWithCoarseRenounce()
305             throws Throwable {
306         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
307             assumeTrue(mPackageManager.hasSystemFeature(
308                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
309         }
310 
311         if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) {
312             Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE");
313             return;
314         }
315 
316         assertFalse(mOnServiceStateChangedCalled);
317 
318         mServiceStateCallback = new ServiceStateListener();
319         registerTelephonyCallback(mServiceStateCallback, false, true);
320 
321         synchronized (mLock) {
322             if (!mOnServiceStateChangedCalled) {
323                 mLock.wait(WAIT_TIME);
324             }
325         }
326 
327         assertTrue(mOnServiceStateChangedCalled);
328 
329         // Test unregister
330         unRegisterTelephonyCallback(mOnServiceStateChangedCalled, mServiceStateCallback);
331     }
332 
333     @Test
testOnServiceStateChangedByRegisterTelephonyCallbackWithFineOnlyRenounce()334     public void testOnServiceStateChangedByRegisterTelephonyCallbackWithFineOnlyRenounce()
335             throws Throwable {
336         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
337             assumeTrue(mPackageManager.hasSystemFeature(
338                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
339         }
340 
341         if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) {
342             Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE");
343             return;
344         }
345 
346         assertFalse(mOnServiceStateChangedCalled);
347 
348         mServiceStateCallback = new ServiceStateListener();
349         registerTelephonyCallback(mServiceStateCallback, true, false);
350 
351         synchronized (mLock) {
352             if (!mOnServiceStateChangedCalled) {
353                 mLock.wait(WAIT_TIME);
354             }
355         }
356 
357         assertTrue(mOnServiceStateChangedCalled);
358         assertServiceStateFineLocationSanitization(mServiceState);
359 
360         // Test unregister
361         unRegisterTelephonyCallback(mOnServiceStateChangedCalled, mServiceStateCallback);
362     }
363 
assertServiceStateFineLocationSanitization(ServiceState state)364     private void assertServiceStateFineLocationSanitization(ServiceState state) {
365         if (state == null) return;
366 
367         if (state.getNetworkRegistrationInfoList() != null) {
368             for (NetworkRegistrationInfo nrs : state.getNetworkRegistrationInfoList()) {
369                 assertNull(nrs.getCellIdentity());
370             }
371         }
372     }
373 
assertServiceStateLocationSanitization(ServiceState state)374     private void assertServiceStateLocationSanitization(ServiceState state) {
375         if (state == null) return;
376         assertServiceStateFineLocationSanitization(state);
377         assertTrue(TextUtils.isEmpty(state.getOperatorAlphaLong()));
378         assertTrue(TextUtils.isEmpty(state.getOperatorAlphaShort()));
379         assertTrue(TextUtils.isEmpty(state.getOperatorNumeric()));
380     }
381 
382     @Test
testOnUnRegisterFollowedByRegisterTelephonyCallback()383     public void testOnUnRegisterFollowedByRegisterTelephonyCallback() throws Throwable {
384         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
385             assumeTrue(mPackageManager.hasSystemFeature(
386                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
387         }
388 
389         assertFalse(mOnServiceStateChangedCalled);
390 
391         mServiceStateCallback = new ServiceStateListener();
392         registerTelephonyCallback(mServiceStateCallback);
393 
394         synchronized (mLock) {
395             if (!mOnServiceStateChangedCalled) {
396                 mLock.wait(WAIT_TIME);
397             }
398         }
399 
400         assertTrue(mOnServiceStateChangedCalled);
401 
402         // un-register
403         if (mServiceStateCallback != null) {
404             // un-register the listener
405             mTelephonyManager.unregisterTelephonyCallback(mServiceStateCallback);
406         }
407         // reset after un-register
408         mOnServiceStateChangedCalled = false;
409         synchronized (mLock) {
410             if (!mOnServiceStateChangedCalled) {
411                 mLock.wait(WAIT_TIME);
412             }
413         }
414         assertFalse(mOnServiceStateChangedCalled);
415 
416         // re-register the listener
417         registerTelephonyCallback(mServiceStateCallback);
418         synchronized (mLock) {
419             if (!mOnServiceStateChangedCalled) {
420                 mLock.wait(WAIT_TIME);
421             }
422         }
423 
424         assertTrue(mOnServiceStateChangedCalled);
425 
426         // Test unregister
427         unRegisterTelephonyCallback(mOnServiceStateChangedCalled, mServiceStateCallback);
428     }
429 
430     private SignalStrengthsListener mSignalStrengthsCallback;
431 
432     private class SignalStrengthsListener extends TelephonyCallback
433             implements TelephonyCallback.SignalStrengthsListener {
434         @Override
onSignalStrengthsChanged(SignalStrength signalStrength)435         public void onSignalStrengthsChanged(SignalStrength signalStrength) {
436             synchronized (mLock) {
437                 mSignalStrength = signalStrength;
438                 mLock.notify();
439             }
440         }
441     }
442 
getSignalStrength()443     private void getSignalStrength() {
444         mSignalStrength.getCdmaDbm();
445         mSignalStrength.getCdmaEcio();
446         mSignalStrength.getEvdoDbm();
447         mSignalStrength.getEvdoEcio();
448         mSignalStrength.getEvdoSnr();
449         mSignalStrength.getGsmBitErrorRate();
450         mSignalStrength.getGsmSignalStrength();
451         mSignalStrength.isGsm();
452         mSignalStrength.getLevel();
453     }
454 
455     @Test
testOnSignalStrengthsChangedByRegisterTelephonyCallback()456     public void testOnSignalStrengthsChangedByRegisterTelephonyCallback() throws Throwable {
457         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
458             assumeTrue(mPackageManager.hasSystemFeature(
459                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
460         }
461 
462         assertTrue(mSignalStrength == null);
463 
464         mSignalStrengthsCallback = new SignalStrengthsListener();
465         registerTelephonyCallback(mSignalStrengthsCallback);
466 
467         synchronized (mLock) {
468             if (mSignalStrength == null) {
469                 mLock.wait(WAIT_TIME);
470             }
471         }
472 
473         assertTrue(mSignalStrength != null);
474         // Call SignalStrength methods to make sure they do not throw any exceptions
475         getSignalStrength();
476 
477         // Test unregister
478         unRegisterTelephonyCallback(mSignalStrength == null, mSignalStrengthsCallback);
479     }
480 
481     private MessageWaitingIndicatorListener mMessageWaitingIndicatorCallback;
482 
483     private class MessageWaitingIndicatorListener extends TelephonyCallback
484             implements TelephonyCallback.MessageWaitingIndicatorListener {
485         @Override
onMessageWaitingIndicatorChanged(boolean mwi)486         public void onMessageWaitingIndicatorChanged(boolean mwi) {
487             synchronized (mLock) {
488                 mOnMessageWaitingIndicatorChangedCalled = true;
489                 mLock.notify();
490             }
491         }
492     }
493 
494     @Test
testOnMessageWaitingIndicatorChangedByRegisterTelephonyCallback()495     public void testOnMessageWaitingIndicatorChangedByRegisterTelephonyCallback()
496             throws Throwable {
497         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
498             assumeTrue(mPackageManager.hasSystemFeature(
499                     PackageManager.FEATURE_TELEPHONY_MESSAGING));
500         }
501 
502         assertFalse(mOnMessageWaitingIndicatorChangedCalled);
503 
504         mMessageWaitingIndicatorCallback = new MessageWaitingIndicatorListener();
505         registerTelephonyCallback(mMessageWaitingIndicatorCallback);
506 
507         synchronized (mLock) {
508             if (!mOnMessageWaitingIndicatorChangedCalled) {
509                 mLock.wait(WAIT_TIME);
510             }
511         }
512 
513         assertTrue(mOnMessageWaitingIndicatorChangedCalled);
514 
515         // Test unregister
516         unRegisterTelephonyCallback(mOnMessageWaitingIndicatorChangedCalled,
517                 mMessageWaitingIndicatorCallback);
518     }
519 
520     private PreciseCallStateListener mPreciseCallStateCallback;
521 
522     private class PreciseCallStateListener extends TelephonyCallback
523             implements TelephonyCallback.PreciseCallStateListener {
524         @Override
onPreciseCallStateChanged(PreciseCallState preciseCallState)525         public void onPreciseCallStateChanged(PreciseCallState preciseCallState) {
526             synchronized (mLock) {
527                 mOnPreciseCallStateChangedCalled = true;
528                 mPreciseCallState = preciseCallState;
529                 mLock.notify();
530             }
531         }
532     }
533 
534     @Test
testOnPreciseCallStateChangedByRegisterTelephonyCallback()535     public void testOnPreciseCallStateChangedByRegisterTelephonyCallback() throws Throwable {
536         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
537             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CALLING));
538         }
539 
540         assertThat(mOnPreciseCallStateChangedCalled).isFalse();
541 
542         mPreciseCallStateCallback = new PreciseCallStateListener();
543         registerTelephonyCallbackWithPermission(mPreciseCallStateCallback);
544 
545         synchronized (mLock) {
546             if (!mOnPreciseCallStateChangedCalled) {
547                 mLock.wait(WAIT_TIME);
548             }
549         }
550         Log.d(TAG, "testOnPreciseCallStateChangedByRegisterTelephonyCallback: "
551                 + mOnPreciseCallStateChangedCalled);
552 
553         assertThat(mOnPreciseCallStateChangedCalled).isTrue();
554         assertThat(mPreciseCallState.getForegroundCallState()).isIn(PRECISE_CALL_STATE);
555         assertThat(mPreciseCallState.getBackgroundCallState()).isIn(PRECISE_CALL_STATE);
556         assertThat(mPreciseCallState.getRingingCallState()).isIn(PRECISE_CALL_STATE);
557 
558         // Test unregister
559         unRegisterTelephonyCallback(mOnPreciseCallStateChangedCalled,
560                 mPreciseCallStateCallback);
561     }
562 
563     private CallAttributesListener mCallAttributesListener;
564 
565     private class CallAttributesListener extends TelephonyCallback
566             implements TelephonyCallback.CallAttributesListener {
567         @Override
onCallStatesChanged(List<CallState> callStateList)568         public void onCallStatesChanged(List<CallState> callStateList) {
569             synchronized (mLock) {
570                 mOnCallStatesChangedCalled = true;
571                 mCallStateList = callStateList;
572                 mLock.notify();
573             }
574         }
575     }
576 
577     @Test
testOnCallStatesChangedByRegisterTelephonyCallback()578     public void testOnCallStatesChangedByRegisterTelephonyCallback() throws Throwable {
579         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
580             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CALLING));
581         } else {
582             if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
583                 Log.d(TAG, "Skipping test that requires FEATURE_TELEPHONY");
584                 return;
585             }
586         }
587 
588         assertThat(mOnCallStatesChangedCalled).isFalse();
589 
590         mCallAttributesListener = new CallAttributesListener();
591         registerTelephonyCallbackWithPermission(mCallAttributesListener);
592 
593         synchronized (mLock) {
594             if (!mOnCallStatesChangedCalled) {
595                 mLock.wait(WAIT_TIME);
596             }
597         }
598         Log.d(TAG, "testOnCallStatesChangedByRegisterTelephonyCallback: "
599                 + mOnCallStatesChangedCalled);
600 
601         assertThat(mOnCallStatesChangedCalled).isTrue();
602         assertNotNull(mCallStateList);
603         if (mCallStateList.size() > 0) {
604             assertThat(mCallStateList.get(0).getCallState()).isIn(PRECISE_CALL_STATE);
605             assertThat(mCallStateList.get(0).getNetworkType()).isIn(NETWORK_TYPES);
606         }
607 
608         // Test unregister
609         unRegisterTelephonyCallback(mOnCallStatesChangedCalled,
610                 mCallAttributesListener);
611     }
612 
613     private CallDisconnectCauseListener mCallDisconnectCauseCallback;
614 
615     private class CallDisconnectCauseListener extends TelephonyCallback
616             implements TelephonyCallback.CallDisconnectCauseListener {
617         @Override
onCallDisconnectCauseChanged(int disconnectCause, int preciseDisconnectCause)618         public void onCallDisconnectCauseChanged(int disconnectCause,
619                                                  int preciseDisconnectCause) {
620             synchronized (mLock) {
621                 mOnCallDisconnectCauseChangedCalled = true;
622                 mLock.notify();
623             }
624         }
625     }
626 
627     @Test
testOnCallDisconnectCauseChangedByRegisterTelephonyCallback()628     public void testOnCallDisconnectCauseChangedByRegisterTelephonyCallback() throws Throwable {
629         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
630             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CALLING));
631         }
632 
633         assertThat(mOnCallDisconnectCauseChangedCalled).isFalse();
634 
635         mCallDisconnectCauseCallback = new CallDisconnectCauseListener();
636         registerTelephonyCallbackWithPermission(mCallDisconnectCauseCallback);
637 
638         synchronized (mLock) {
639             if (!mOnCallDisconnectCauseChangedCalled) {
640                 mLock.wait(WAIT_TIME);
641             }
642         }
643 
644         assertThat(mOnCallDisconnectCauseChangedCalled).isTrue();
645 
646         // Test unregister
647         unRegisterTelephonyCallback(mOnCallDisconnectCauseChangedCalled,
648                 mCallDisconnectCauseCallback);
649     }
650 
651     private ImsCallDisconnectCauseListener mImsCallDisconnectCauseCallback;
652 
653     private class ImsCallDisconnectCauseListener extends TelephonyCallback
654             implements TelephonyCallback.ImsCallDisconnectCauseListener {
655         @Override
onImsCallDisconnectCauseChanged(ImsReasonInfo imsReason)656         public void onImsCallDisconnectCauseChanged(ImsReasonInfo imsReason) {
657             synchronized (mLock) {
658                 mOnImsCallDisconnectCauseChangedCalled = true;
659                 mImsReasonInfo = imsReason;
660                 mLock.notify();
661             }
662         }
663     }
664 
665     @Test
testOnImsCallDisconnectCauseChangedByRegisterTelephonyCallback()666     public void testOnImsCallDisconnectCauseChangedByRegisterTelephonyCallback() throws Throwable {
667         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
668             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CALLING));
669         }
670 
671         assertThat(mOnImsCallDisconnectCauseChangedCalled).isFalse();
672 
673         mImsCallDisconnectCauseCallback = new ImsCallDisconnectCauseListener();
674         registerTelephonyCallbackWithPermission(mImsCallDisconnectCauseCallback);
675 
676         synchronized (mLock) {
677             if (!mOnImsCallDisconnectCauseChangedCalled) {
678                 mLock.wait(WAIT_TIME);
679             }
680         }
681 
682         assertThat(mOnImsCallDisconnectCauseChangedCalled).isTrue();
683         assertNotNull(mImsReasonInfo);
684 
685         // Test unregister
686         unRegisterTelephonyCallback(mOnImsCallDisconnectCauseChangedCalled,
687                 mImsCallDisconnectCauseCallback);
688     }
689 
690     private SrvccStateListener mSrvccStateCallback;
691 
692     private class SrvccStateListener extends TelephonyCallback
693             implements TelephonyCallback.SrvccStateListener {
694         @Override
onSrvccStateChanged(int state)695         public void onSrvccStateChanged(int state) {
696             synchronized (mLock) {
697                 mSrvccStateChangedCalled = true;
698                 mLock.notify();
699             }
700         }
701     }
702 
703     @Test
testOnSrvccStateChangedByRegisterTelephonyCallback()704     public void testOnSrvccStateChangedByRegisterTelephonyCallback() throws Throwable {
705         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
706             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS));
707         }
708 
709         assertThat(mSrvccStateChangedCalled).isFalse();
710 
711         mSrvccStateCallback = new SrvccStateListener();
712         registerTelephonyCallbackWithPermission(mSrvccStateCallback);
713 
714         synchronized (mLock) {
715             if (!mSrvccStateChangedCalled) {
716                 mLock.wait(WAIT_TIME);
717             }
718         }
719         Log.d(TAG, "testOnSrvccStateChangedByRegisterTelephonyCallback");
720 
721         assertThat(mSrvccStateChangedCalled).isTrue();
722 
723         // Test unregister
724         unRegisterTelephonyCallback(mSrvccStateChangedCalled, mSrvccStateCallback);
725     }
726 
727     private RadioPowerStateListener mRadioPowerStateCallback;
728 
729     private class RadioPowerStateListener extends TelephonyCallback
730             implements TelephonyCallback.RadioPowerStateListener {
731         @Override
onRadioPowerStateChanged(int state)732         public void onRadioPowerStateChanged(int state) {
733             synchronized (mLock) {
734                 mRadioPowerState = state;
735                 mOnRadioPowerStateChangedCalled = true;
736                 mLock.notify();
737             }
738         }
739     }
740 
741     @Test
testOnRadioPowerStateChangedByRegisterTelephonyCallback()742     public void testOnRadioPowerStateChangedByRegisterTelephonyCallback() throws Throwable {
743         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
744             assumeTrue(mPackageManager.hasSystemFeature(
745                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
746         }
747 
748         assertThat(mOnRadioPowerStateChangedCalled).isFalse();
749 
750         mRadioPowerStateCallback = new RadioPowerStateListener();
751         registerTelephonyCallbackWithPermission(mRadioPowerStateCallback);
752 
753         synchronized (mLock) {
754             if (!mOnRadioPowerStateChangedCalled) {
755                 mLock.wait(WAIT_TIME);
756             }
757         }
758         Log.d(TAG, "testOnRadioPowerStateChangedByRegisterTelephonyCallback: "
759                 + mRadioPowerState);
760 
761         assertThat(mTelephonyManager.getRadioPowerState()).isEqualTo(mRadioPowerState);
762 
763         // Test unregister
764         unRegisterTelephonyCallback(mOnRadioPowerStateChangedCalled,
765                 mRadioPowerStateCallback);
766     }
767 
768     private VoiceActivationStateListener mVoiceActivationStateCallback;
769 
770     private class VoiceActivationStateListener extends TelephonyCallback
771             implements TelephonyCallback.VoiceActivationStateListener {
772         @Override
onVoiceActivationStateChanged(int state)773         public void onVoiceActivationStateChanged(int state) {
774             synchronized (mLock) {
775                 mVoiceActivationState = state;
776                 mVoiceActivationStateChangedCalled = true;
777                 mLock.notify();
778             }
779         }
780     }
781 
782     @Test
testOnVoiceActivationStateChangedByRegisterTelephonyCallback()783     public void testOnVoiceActivationStateChangedByRegisterTelephonyCallback() throws Throwable {
784         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
785             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CALLING));
786         }
787 
788         assertThat(mVoiceActivationStateChangedCalled).isFalse();
789 
790         mVoiceActivationStateCallback = new VoiceActivationStateListener();
791         registerTelephonyCallbackWithPermission(mVoiceActivationStateCallback);
792 
793         synchronized (mLock) {
794             if (!mVoiceActivationStateChangedCalled) {
795                 mLock.wait(WAIT_TIME);
796             }
797         }
798         Log.d(TAG, "testOnVoiceActivationStateChangedByRegisterTelephonyCallback: "
799                 + mVoiceActivationState);
800         int state = ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
801                 (tm) -> tm.getVoiceActivationState());
802 
803         assertEquals(state, mVoiceActivationState);
804 
805         // Test unregister
806         unRegisterTelephonyCallback(mVoiceActivationStateChangedCalled,
807                 mVoiceActivationStateCallback);
808     }
809 
810     private PreciseDataConnectionStateListener mPreciseDataConnectionStateCallback;
811 
812     private class PreciseDataConnectionStateListener extends TelephonyCallback
813             implements TelephonyCallback.PreciseDataConnectionStateListener {
814         @Override
onPreciseDataConnectionStateChanged( PreciseDataConnectionState state)815         public void onPreciseDataConnectionStateChanged(
816                 PreciseDataConnectionState state) {
817             synchronized (mLock) {
818                 mOnPreciseDataConnectionStateChanged = true;
819                 mPreciseDataConnectionState = state;
820                 mLock.notify();
821             }
822         }
823     }
824 
getPreciseDataConnectionState()825     private void getPreciseDataConnectionState() {
826         // Ensure that no exceptions are thrown
827         mPreciseDataConnectionState.getNetworkType();
828         mPreciseDataConnectionState.getLinkProperties();
829         mPreciseDataConnectionState.getLastCauseCode();
830         mPreciseDataConnectionState.getLinkProperties();
831         mPreciseDataConnectionState.getApnSetting();
832         mPreciseDataConnectionState.getTransportType();
833         mPreciseDataConnectionState.getId();
834 
835         // Deprecated in R
836         assertEquals(mPreciseDataConnectionState.getDataConnectionState(),
837                 mPreciseDataConnectionState.getState());
838         assertEquals(mPreciseDataConnectionState.getDataConnectionFailCause(),
839                 mPreciseDataConnectionState.getLastCauseCode());
840 
841         // Superseded in R by getApnSetting()
842         mPreciseDataConnectionState.getDataConnectionApnTypeBitMask();
843         mPreciseDataConnectionState.getDataConnectionApn();
844     }
845 
846     @Test
testOnPreciseDataConnectionStateChangedByRegisterTelephonyCallback()847     public void testOnPreciseDataConnectionStateChangedByRegisterTelephonyCallback()
848             throws Throwable {
849         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
850             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_DATA));
851         }
852 
853         assertThat(mOnCallDisconnectCauseChangedCalled).isFalse();
854 
855         mPreciseDataConnectionStateCallback =
856                 new PreciseDataConnectionStateListener();
857         registerTelephonyCallbackWithPermission(mPreciseDataConnectionStateCallback);
858 
859         synchronized (mLock) {
860             if (!mOnPreciseDataConnectionStateChanged) {
861                 mLock.wait(WAIT_TIME);
862             }
863         }
864 
865         assertThat(mOnPreciseDataConnectionStateChanged).isTrue();
866         assertThat(mPreciseDataConnectionState.getState())
867                 .isIn(DATA_CONNECTION_STATE);
868 
869         getPreciseDataConnectionState();
870         // Test unregister
871         unRegisterTelephonyCallback(mOnPreciseDataConnectionStateChanged,
872                 mPreciseDataConnectionStateCallback);
873     }
874 
875     private DisplayInfoListener mDisplayInfoCallback;
876 
877     private class DisplayInfoListener extends TelephonyCallback
878             implements TelephonyCallback.DisplayInfoListener {
879         @Override
onDisplayInfoChanged(TelephonyDisplayInfo displayInfo)880         public void onDisplayInfoChanged(TelephonyDisplayInfo displayInfo) {
881             synchronized (mLock) {
882                 mOnTelephonyDisplayInfoChanged = true;
883                 mLock.notify();
884             }
885         }
886     }
887 
888     @Test
testOnDisplayInfoChangedByRegisterTelephonyCallback()889     public void testOnDisplayInfoChangedByRegisterTelephonyCallback() throws Exception {
890         assertThat(mOnTelephonyDisplayInfoChanged).isFalse();
891 
892         mDisplayInfoCallback = new DisplayInfoListener();
893         registerTelephonyCallback(mDisplayInfoCallback);
894 
895         synchronized (mLock) {
896             if (!mOnTelephonyDisplayInfoChanged) {
897                 mLock.wait(WAIT_TIME);
898             }
899         }
900         assertTrue(mOnTelephonyDisplayInfoChanged);
901 
902         // Test unregister
903         unRegisterTelephonyCallback(mOnTelephonyDisplayInfoChanged, mDisplayInfoCallback);
904     }
905 
906     private CallForwardingIndicatorListener mCallForwardingIndicatorCallback;
907 
908     private class CallForwardingIndicatorListener extends TelephonyCallback
909             implements TelephonyCallback.CallForwardingIndicatorListener {
910         @Override
onCallForwardingIndicatorChanged(boolean cfi)911         public void onCallForwardingIndicatorChanged(boolean cfi) {
912             synchronized (mLock) {
913                 mOnCallForwardingIndicatorChangedCalled = true;
914                 mLock.notify();
915             }
916         }
917     }
918 
919     @Test
testOnCallForwardingIndicatorChangedByRegisterTelephonyCallback()920     public void testOnCallForwardingIndicatorChangedByRegisterTelephonyCallback()
921             throws Throwable {
922         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
923             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CALLING));
924         }
925 
926         assertFalse(mOnCallForwardingIndicatorChangedCalled);
927 
928         mCallForwardingIndicatorCallback = new CallForwardingIndicatorListener();
929         registerTelephonyCallback(mCallForwardingIndicatorCallback);
930 
931         synchronized (mLock) {
932             if (!mOnCallForwardingIndicatorChangedCalled) {
933                 mLock.wait(WAIT_TIME);
934             }
935         }
936 
937         assertTrue(mOnCallForwardingIndicatorChangedCalled);
938 
939         // Test unregister
940         unRegisterTelephonyCallback(mOnCallForwardingIndicatorChangedCalled,
941                 mCallForwardingIndicatorCallback);
942     }
943 
944     private CellLocationListener mCellLocationCallback;
945 
946     private class CellLocationListener extends TelephonyCallback
947             implements TelephonyCallback.CellLocationListener {
948         @Override
onCellLocationChanged(CellLocation location)949         public void onCellLocationChanged(CellLocation location) {
950             synchronized (mLock) {
951                 mOnCellLocationChangedCalled = true;
952                 mLock.notify();
953             }
954         }
955     }
956 
957     @Test
testOnCellLocationChangedByRegisterTelephonyCallback()958     public void testOnCellLocationChangedByRegisterTelephonyCallback() throws Throwable {
959         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
960             assumeTrue(mPackageManager.hasSystemFeature(
961                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
962         }
963 
964         assertFalse(mOnCellLocationChangedCalled);
965 
966         TelephonyManagerTest.grantLocationPermissions();
967 
968         mCellLocationCallback = new CellLocationListener();
969         registerTelephonyCallback(mCellLocationCallback);
970 
971         synchronized (mLock) {
972             if (!mOnCellLocationChangedCalled) {
973                 mLock.wait(WAIT_TIME);
974             }
975         }
976 
977         assertTrue(mOnCellLocationChangedCalled);
978 
979         // Test unregister
980         unRegisterTelephonyCallback(mOnCellLocationChangedCalled, mCellLocationCallback);
981     }
982 
983     private CallStateListener mCallStateCallback;
984 
985     private class CallStateListener extends TelephonyCallback
986             implements TelephonyCallback.CallStateListener {
987         @Override
onCallStateChanged(int state)988         public void onCallStateChanged(int state) {
989             synchronized (mLock) {
990                 mOnCallStateChangedCalled = true;
991                 mLock.notify();
992             }
993         }
994     }
995 
996     @Test
testOnCallStateChangedByRegisterTelephonyCallback()997     public void testOnCallStateChangedByRegisterTelephonyCallback() throws Throwable {
998         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
999             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CALLING));
1000         }
1001 
1002         assertFalse(mOnCallStateChangedCalled);
1003 
1004         mCallStateCallback = new CallStateListener();
1005         registerTelephonyCallback(mCallStateCallback);
1006 
1007         synchronized (mLock) {
1008             if (!mOnCallStateChangedCalled) {
1009                 mLock.wait(WAIT_TIME);
1010             }
1011         }
1012 
1013         assertTrue(mOnCallStateChangedCalled);
1014 
1015         // Test unregister
1016         unRegisterTelephonyCallback(mOnCallStateChangedCalled, mCallStateCallback);
1017     }
1018 
1019     private DataConnectionStateListener mDataConnectionStateCallback;
1020 
1021     private class DataConnectionStateListener extends TelephonyCallback
1022             implements TelephonyCallback.DataConnectionStateListener {
1023         @Override
onDataConnectionStateChanged(int state, int networkType)1024         public void onDataConnectionStateChanged(int state, int networkType) {
1025             synchronized (mLock) {
1026                 mOnDataConnectionStateChangedCalled = true;
1027                 mOnDataConnectionStateChangedWithNetworkTypeCalled = true;
1028                 if (mOnDataConnectionStateChangedCalled
1029                         && mOnDataConnectionStateChangedWithNetworkTypeCalled) {
1030                     mLock.notify();
1031                 }
1032             }
1033         }
1034     }
1035 
1036     @Test
testOnDataConnectionStateChangedByRegisterTelephonyCallback()1037     public void testOnDataConnectionStateChangedByRegisterTelephonyCallback() throws Throwable {
1038         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1039             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_DATA));
1040         }
1041 
1042         assertFalse(mOnDataConnectionStateChangedCalled);
1043         assertFalse(mOnDataConnectionStateChangedWithNetworkTypeCalled);
1044 
1045         mDataConnectionStateCallback = new DataConnectionStateListener();
1046         registerTelephonyCallback(mDataConnectionStateCallback);
1047 
1048         synchronized (mLock) {
1049             if (!mOnDataConnectionStateChangedCalled ||
1050                     !mOnDataConnectionStateChangedWithNetworkTypeCalled) {
1051                 mLock.wait(WAIT_TIME);
1052             }
1053         }
1054 
1055         assertTrue(mOnDataConnectionStateChangedCalled);
1056         assertTrue(mOnDataConnectionStateChangedWithNetworkTypeCalled);
1057 
1058         // Test unregister
1059         unRegisterTelephonyCallback(mOnDataConnectionStateChangedCalled,
1060                 mDataConnectionStateCallback);
1061     }
1062 
1063     private DataActivityListener mDataActivityCallback;
1064 
1065     private class DataActivityListener extends TelephonyCallback
1066             implements TelephonyCallback.DataActivityListener {
1067         @Override
onDataActivity(int direction)1068         public void onDataActivity(int direction) {
1069             synchronized (mLock) {
1070                 mOnDataActivityCalled = true;
1071                 mLock.notify();
1072             }
1073         }
1074     }
1075 
1076     @Test
testOnDataActivityByRegisterTelephonyCallback()1077     public void testOnDataActivityByRegisterTelephonyCallback() throws Throwable {
1078         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1079             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_DATA));
1080         }
1081 
1082         assertFalse(mOnDataActivityCalled);
1083 
1084         mDataActivityCallback = new DataActivityListener();
1085         registerTelephonyCallback(mDataActivityCallback);
1086 
1087         synchronized (mLock) {
1088             if (!mOnDataActivityCalled) {
1089                 mLock.wait(WAIT_TIME);
1090             }
1091         }
1092 
1093         assertTrue(mOnDataActivityCalled);
1094 
1095         // Test unregister
1096         unRegisterTelephonyCallback(mOnDataActivityCalled, mDataActivityCallback);
1097     }
1098 
1099     private CellInfoListener mCellInfoCallback;
1100 
1101     private class CellInfoListener extends TelephonyCallback
1102             implements TelephonyCallback.CellInfoListener {
1103         @Override
onCellInfoChanged(List<CellInfo> cellInfo)1104         public void onCellInfoChanged(List<CellInfo> cellInfo) {
1105             synchronized (mLock) {
1106                 mOnCellInfoChangedCalled = true;
1107                 mLock.notify();
1108             }
1109         }
1110     }
1111 
1112     @Test
testOnCellInfoChangedByRegisterTelephonyCallback()1113     public void testOnCellInfoChangedByRegisterTelephonyCallback() throws Throwable {
1114         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1115             assumeTrue(mPackageManager.hasSystemFeature(
1116                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
1117         }
1118 
1119         assertFalse(mOnDataActivityCalled);
1120 
1121         TelephonyManagerTest.grantLocationPermissions();
1122 
1123         mCellInfoCallback = new CellInfoListener();
1124         registerTelephonyCallback(mCellInfoCallback);
1125 
1126         synchronized (mLock) {
1127             if (!mOnCellInfoChangedCalled) {
1128                 mLock.wait(WAIT_TIME);
1129             }
1130         }
1131 
1132         assertTrue(mOnCellInfoChangedCalled);
1133 
1134         // Test unregister
1135         unRegisterTelephonyCallback(mOnCellInfoChangedCalled, mCellInfoCallback);
1136     }
1137 
1138     private UserMobileDataStateListener mUserMobileDataStateCallback;
1139 
1140     private class UserMobileDataStateListener extends TelephonyCallback
1141             implements TelephonyCallback.UserMobileDataStateListener {
1142         @Override
onUserMobileDataStateChanged(boolean state)1143         public void onUserMobileDataStateChanged(boolean state) {
1144             synchronized (mLock) {
1145                 mOnUserMobileDataStateChanged = true;
1146                 mLock.notify();
1147             }
1148         }
1149     }
1150 
1151     @Test
testOnUserMobileDataStateChangedByRegisterTelephonyCallback()1152     public void testOnUserMobileDataStateChangedByRegisterTelephonyCallback() throws Throwable {
1153         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1154             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_DATA));
1155         }
1156 
1157         assertFalse(mOnUserMobileDataStateChanged);
1158 
1159         mUserMobileDataStateCallback = new UserMobileDataStateListener();
1160         registerTelephonyCallback(mUserMobileDataStateCallback);
1161 
1162         synchronized (mLock) {
1163             if (!mOnUserMobileDataStateChanged) {
1164                 mLock.wait(WAIT_TIME);
1165             }
1166         }
1167 
1168         assertTrue(mOnUserMobileDataStateChanged);
1169 
1170         // Test unregister
1171         unRegisterTelephonyCallback(mOnUserMobileDataStateChanged, mUserMobileDataStateCallback);
1172     }
1173 
1174     private OutgoingEmergencySmsListener mOutgoingEmergencySmsCallback;
1175 
1176     private class OutgoingEmergencySmsListener extends TelephonyCallback
1177             implements TelephonyCallback.OutgoingEmergencySmsListener {
1178         @Override
onOutgoingEmergencySms(EmergencyNumber emergencyNumber, int subId)1179         public void onOutgoingEmergencySms(EmergencyNumber emergencyNumber, int subId) {
1180             synchronized (mLock) {
1181                 Log.i(TAG, "onOutgoingEmergencySms: emergencyNumber=" + emergencyNumber);
1182                 mOnOutgoingSmsEmergencyNumberChanged = emergencyNumber;
1183                 mLock.notify();
1184             }
1185         }
1186     }
1187 
1188     @Test
testOnOutgoingSmsEmergencyNumberChangedByRegisterTelephonyCallback()1189     public void testOnOutgoingSmsEmergencyNumberChangedByRegisterTelephonyCallback()
1190             throws Throwable {
1191         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1192             assumeTrue(mPackageManager.hasSystemFeature(
1193                     PackageManager.FEATURE_TELEPHONY_MESSAGING));
1194         }
1195 
1196         TelephonyUtils.addTestEmergencyNumber(
1197                 InstrumentationRegistry.getInstrumentation(), TEST_EMERGENCY_NUMBER);
1198         assertNull(mOnOutgoingSmsEmergencyNumberChanged);
1199 
1200         mOutgoingEmergencySmsCallback = new OutgoingEmergencySmsListener();
1201         registerTelephonyCallbackWithPermission(mOutgoingEmergencySmsCallback);
1202         SmsManager smsManager = SmsManager.getDefault();
1203         ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(smsManager,
1204                 (sm) -> sm.sendTextMessage(
1205                         TEST_EMERGENCY_NUMBER, null,
1206                         "testOutgoingSmsListenerCtsByRegisterTelephonyCallback",
1207                         null, null));
1208         try {
1209             synchronized (mLock) {
1210                 if (mOnOutgoingSmsEmergencyNumberChanged == null) {
1211                     mLock.wait(WAIT_TIME);
1212                 }
1213             }
1214         } catch (InterruptedException e) {
1215             Log.e(TAG, "Operation interrupted.");
1216         } finally {
1217             TelephonyUtils.removeTestEmergencyNumber(
1218                     InstrumentationRegistry.getInstrumentation(), TEST_EMERGENCY_NUMBER);
1219         }
1220 
1221         assertNotNull(mOnOutgoingSmsEmergencyNumberChanged);
1222         assertEquals(mOnOutgoingSmsEmergencyNumberChanged.getNumber(), TEST_EMERGENCY_NUMBER);
1223 
1224         // Test unregister
1225         unRegisterTelephonyCallback(mOnOutgoingSmsEmergencyNumberChanged == null,
1226                 mOutgoingEmergencySmsCallback);
1227 
1228         // Disable suppressing blocking.
1229         TelephonyUtils.endBlockSuppression(InstrumentationRegistry.getInstrumentation());
1230     }
1231 
1232     private ActiveDataSubscriptionIdListener mActiveDataSubscriptionIdCallback;
1233 
1234     private class ActiveDataSubscriptionIdListener extends TelephonyCallback
1235             implements TelephonyCallback.ActiveDataSubscriptionIdListener {
1236         @Override
onActiveDataSubscriptionIdChanged(int subId)1237         public void onActiveDataSubscriptionIdChanged(int subId) {
1238             synchronized (mLock) {
1239                 mOnActiveDataSubscriptionIdChanged = true;
1240                 mLock.notify();
1241             }
1242         }
1243     }
1244 
1245     @Test
testOnActiveDataSubscriptionIdChangedByRegisterTelephonyCallback()1246     public void testOnActiveDataSubscriptionIdChangedByRegisterTelephonyCallback()
1247             throws Throwable {
1248         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1249             assumeTrue(mPackageManager.hasSystemFeature(
1250                     PackageManager.FEATURE_TELEPHONY_SUBSCRIPTION));
1251         }
1252 
1253         assertFalse(mOnActiveDataSubscriptionIdChanged);
1254 
1255         mActiveDataSubscriptionIdCallback =
1256                 new ActiveDataSubscriptionIdListener();
1257         registerTelephonyCallback(mActiveDataSubscriptionIdCallback);
1258 
1259         synchronized (mLock) {
1260             if (!mOnActiveDataSubscriptionIdChanged) {
1261                 mLock.wait(WAIT_TIME);
1262             }
1263         }
1264 
1265         assertTrue(mOnActiveDataSubscriptionIdChanged);
1266 
1267         // Test unregister
1268         unRegisterTelephonyCallback(mOnActiveDataSubscriptionIdChanged,
1269                 mActiveDataSubscriptionIdCallback);
1270     }
1271 
1272     private BarringInfoListener mBarringInfoCallback;
1273 
1274     private class BarringInfoListener extends TelephonyCallback
1275             implements TelephonyCallback.BarringInfoListener {
1276         @Override
onBarringInfoChanged(BarringInfo barringInfo)1277         public void onBarringInfoChanged(BarringInfo barringInfo) {
1278             synchronized (mLock) {
1279                 mOnBarringInfoChangedCalled = true;
1280                 mBarringInfo = barringInfo;
1281                 mLock.notify();
1282             }
1283         }
1284     }
1285 
1286     @Test
testOnBarringInfoChangedByRegisterTelephonyCallback()1287     public void testOnBarringInfoChangedByRegisterTelephonyCallback() throws Throwable {
1288         assertFalse(mOnBarringInfoChangedCalled);
1289 
1290         mBarringInfoCallback = new BarringInfoListener();
1291         registerTelephonyCallbackWithPermission(mBarringInfoCallback);
1292 
1293         synchronized (mLock) {
1294             if (!mOnBarringInfoChangedCalled) {
1295                 mLock.wait(WAIT_TIME);
1296             }
1297         }
1298         assertTrue(mOnBarringInfoChangedCalled);
1299 
1300         assertBarringInfoSane(mBarringInfo);
1301 
1302         // Test unregister
1303         unRegisterTelephonyCallback(mOnBarringInfoChangedCalled, mBarringInfoCallback);
1304     }
1305 
1306     private static final int[] sBarringServiceInfoTypes = new int[]{
1307             BarringInfo.BARRING_SERVICE_TYPE_CS_SERVICE,
1308             BarringInfo.BARRING_SERVICE_TYPE_PS_SERVICE,
1309             BarringInfo.BARRING_SERVICE_TYPE_CS_VOICE,
1310             BarringInfo.BARRING_SERVICE_TYPE_MO_SIGNALLING,
1311             BarringInfo.BARRING_SERVICE_TYPE_MO_DATA,
1312             BarringInfo.BARRING_SERVICE_TYPE_CS_FALLBACK,
1313             BarringInfo.BARRING_SERVICE_TYPE_MMTEL_VOICE,
1314             BarringInfo.BARRING_SERVICE_TYPE_MMTEL_VIDEO,
1315             BarringInfo.BARRING_SERVICE_TYPE_EMERGENCY,
1316             BarringInfo.BARRING_SERVICE_TYPE_SMS
1317     };
1318 
assertBarringInfoSane(BarringInfo barringInfo)1319     private static void assertBarringInfoSane(BarringInfo barringInfo) {
1320         assertNotNull(barringInfo);
1321 
1322         // Flags to track whether we have had unknown and known barring types reported
1323         boolean hasBarringTypeUnknown = false;
1324         boolean hasBarringTypeKnown = false;
1325 
1326         for (int bsiType : sBarringServiceInfoTypes) {
1327             BarringInfo.BarringServiceInfo bsi = barringInfo.getBarringServiceInfo(bsiType);
1328             assertNotNull(bsi);
1329             switch (bsi.getBarringType()) {
1330                 case BarringInfo.BarringServiceInfo.BARRING_TYPE_UNKNOWN:
1331                     hasBarringTypeUnknown = true;
1332                     assertFalse(bsi.isConditionallyBarred());
1333                     assertEquals(0, bsi.getConditionalBarringFactor());
1334                     assertEquals(0, bsi.getConditionalBarringTimeSeconds());
1335                     assertFalse(bsi.isBarred());
1336                     break;
1337 
1338                 case BarringInfo.BarringServiceInfo.BARRING_TYPE_NONE:
1339                     hasBarringTypeKnown = true;
1340                     // Unless conditional barring is active, all conditional barring fields
1341                     // should be "unset".
1342                     assertFalse(bsi.isConditionallyBarred());
1343                     assertEquals(0, bsi.getConditionalBarringFactor());
1344                     assertEquals(0, bsi.getConditionalBarringTimeSeconds());
1345                     assertFalse(bsi.isBarred());
1346                     break;
1347 
1348                 case BarringInfo.BarringServiceInfo.BARRING_TYPE_UNCONDITIONAL:
1349                     hasBarringTypeKnown = true;
1350                     // Unless conditional barring is active, all conditional barring fields
1351                     // should be "unset".
1352                     assertFalse(bsi.isConditionallyBarred());
1353                     assertEquals(0, bsi.getConditionalBarringFactor());
1354                     assertEquals(0, bsi.getConditionalBarringTimeSeconds());
1355                     assertTrue(bsi.isBarred());
1356                     break;
1357 
1358                 case BarringInfo.BarringServiceInfo.BARRING_TYPE_CONDITIONAL:
1359                     hasBarringTypeKnown = true;
1360                     // If conditional barring is active, then the barring time and factor must
1361                     // be known (set), but the device may or may not be barred at the moment,
1362                     // so isConditionallyBarred() can be either true or false (hence not checked).
1363                     assertNotEquals(0, bsi.getConditionalBarringFactor());
1364                     assertNotEquals(0, bsi.getConditionalBarringTimeSeconds());
1365                     assertEquals(bsi.isBarred(), bsi.isConditionallyBarred());
1366                     break;
1367             }
1368         }
1369         // If any barring type is unknown, then barring is not supported so all must be
1370         // unknown. If any type is known, then all that are not reported are assumed to
1371         // be not barred.
1372         assertNotEquals(hasBarringTypeUnknown, hasBarringTypeKnown);
1373     }
1374 
1375     private RegistrationFailedListener mRegistrationFailedCallback;
1376 
1377     private class RegistrationFailedListener extends TelephonyCallback
1378             implements TelephonyCallback.RegistrationFailedListener {
1379         @Override
onRegistrationFailed(CellIdentity cid, String chosenPlmn, int domain, int causeCode, int additionalCauseCode)1380         public void onRegistrationFailed(CellIdentity cid, String chosenPlmn,
1381                                          int domain, int causeCode, int additionalCauseCode) {
1382             synchronized (mLock) {
1383                 mOnRegistrationFailedCalled = true;
1384                 mLock.notify();
1385             }
1386         }
1387     }
1388 
1389     @Test
testOnRegistrationFailedByRegisterTelephonyCallback()1390     public void testOnRegistrationFailedByRegisterTelephonyCallback() throws Throwable {
1391 
1392         assertFalse(mOnBarringInfoChangedCalled);
1393 
1394         mRegistrationFailedCallback = new RegistrationFailedListener();
1395         registerTelephonyCallbackWithPermission(mRegistrationFailedCallback);
1396 
1397         synchronized (mLock) {
1398             if (!mOnBarringInfoChangedCalled) {
1399                 mLock.wait(WAIT_TIME);
1400             }
1401         }
1402 
1403         // Assert that in the WAIT_TIME interval, the listener wasn't invoked. While this is
1404         // **technically** a flaky test, in practice this flake should happen approximately never
1405         // as it would mean that a registered phone is failing to reselect during CTS at this
1406         // exact moment.
1407         //
1408         // What the test is verifying is that there is no "auto" callback for registration
1409         // failure because unlike other PSL registrants, this one is not called upon registration.
1410         assertFalse(mOnRegistrationFailedCalled);
1411 
1412         // Test unregister
1413         unRegisterTelephonyCallback(mOnRegistrationFailedCalled, mRegistrationFailedCallback);
1414     }
1415 
1416     private PhysicalChannelConfigListener mPhysicalChannelConfigCallback;
1417 
1418     private class PhysicalChannelConfigListener extends TelephonyCallback
1419             implements TelephonyCallback.PhysicalChannelConfigListener {
1420         @Override
onPhysicalChannelConfigChanged(@onNull List<PhysicalChannelConfig> configs)1421         public void onPhysicalChannelConfigChanged(@NonNull List<PhysicalChannelConfig> configs) {
1422             synchronized (mLock) {
1423                 mOnPhysicalChannelConfigCalled = true;
1424                 mLock.notify();
1425             }
1426         }
1427     }
1428 
1429     @Test
testOnPhysicalChannelConfigChanged()1430     public void testOnPhysicalChannelConfigChanged() throws Throwable {
1431         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1432             assumeTrue(mPackageManager.hasSystemFeature(
1433                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
1434         }
1435 
1436         Pair<Integer, Integer> networkHalVersion =
1437                 mTelephonyManager.getHalVersion(TelephonyManager.HAL_SERVICE_NETWORK);
1438         // 1.2+ or 1.6 with CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED or 2.0+
1439         boolean physicalChannelConfigSupported;
1440         if (networkHalVersion.first == 1 && networkHalVersion.second == 6) {
1441             physicalChannelConfigSupported = ShellIdentityUtils.invokeMethodWithShellPermissions(
1442                     mTelephonyManager, (tm) -> tm.isRadioInterfaceCapabilitySupported(
1443                             TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED));
1444         } else {
1445             physicalChannelConfigSupported =
1446                     networkHalVersion.first > 1 || networkHalVersion.second >= 2;
1447         }
1448         if (!physicalChannelConfigSupported) {
1449             Log.d(TAG, "Skipping test because physical channel configs are not available.");
1450             return;
1451         }
1452 
1453         assertFalse(mOnPhysicalChannelConfigCalled);
1454 
1455         mPhysicalChannelConfigCallback = new PhysicalChannelConfigListener();
1456         registerTelephonyCallbackWithPermission(mPhysicalChannelConfigCallback);
1457 
1458         synchronized (mLock) {
1459             while (!mOnPhysicalChannelConfigCalled) {
1460                 mLock.wait(WAIT_TIME);
1461             }
1462         }
1463         assertTrue(mOnPhysicalChannelConfigCalled);
1464 
1465         // Test unregister
1466         unRegisterTelephonyCallback(mOnPhysicalChannelConfigCalled,
1467                 mPhysicalChannelConfigCallback);
1468     }
1469 
1470     private DataEnabledListener mDataEnabledCallback;
1471 
1472     private class DataEnabledListener extends TelephonyCallback
1473             implements TelephonyCallback.DataEnabledListener {
1474         @Override
onDataEnabledChanged(boolean enabled, @DataEnabledReason int reason)1475         public void onDataEnabledChanged(boolean enabled, @DataEnabledReason int reason) {
1476             synchronized (mLock) {
1477                 mOnDataEnabledChangedCalled = true;
1478                 mLock.notify();
1479             }
1480         }
1481     }
1482 
1483     @Test
testOnDataEnabledChangedByRegisterTelephonyCallback()1484     public void testOnDataEnabledChangedByRegisterTelephonyCallback() throws Throwable {
1485         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1486             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_DATA));
1487         }
1488 
1489         assertFalse(mOnDataEnabledChangedCalled);
1490 
1491         mDataEnabledCallback = new DataEnabledListener();
1492         registerTelephonyCallbackWithPermission(mDataEnabledCallback);
1493 
1494         synchronized (mLock) {
1495             while (!mOnDataEnabledChangedCalled) {
1496                 mLock.wait(WAIT_TIME);
1497             }
1498         }
1499         assertTrue(mOnDataEnabledChangedCalled);
1500 
1501         // Test unregister
1502         unRegisterTelephonyCallback(mOnDataEnabledChangedCalled, mDataEnabledCallback);
1503     }
1504 
1505     private AllowedNetworkTypesListener mAllowedNetworkTypesCallback;
1506 
1507     private class AllowedNetworkTypesListener extends TelephonyCallback
1508             implements TelephonyCallback.AllowedNetworkTypesListener {
1509         @Override
onAllowedNetworkTypesChanged(int reason, long allowedNetworkType)1510         public void onAllowedNetworkTypesChanged(int reason, long allowedNetworkType) {
1511             synchronized (mLock) {
1512                 Log.d(TAG, "onAllowedNetworkTypesChanged");
1513                 mAllowedNetworkTypeReason = reason;
1514                 mAllowedNetworkTypeValue = allowedNetworkType;
1515                 mOnAllowedNetworkTypesChangedCalled = true;
1516 
1517                 mLock.notify();
1518             }
1519         }
1520     }
1521 
1522     @Test
testOnAllowedNetworkTypesChangedByRegisterPhoneStateListener()1523     public void testOnAllowedNetworkTypesChangedByRegisterPhoneStateListener() throws Throwable {
1524         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1525             assumeTrue(mPackageManager.hasSystemFeature(
1526                     PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS));
1527         }
1528 
1529         long originalAllowedNetworkTypeUser = ShellIdentityUtils.invokeMethodWithShellPermissions(
1530                 mTelephonyManager, (tm) -> {
1531                     return tm.getAllowedNetworkTypesForReason(
1532                             TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER);
1533                 });
1534         assertFalse(mOnAllowedNetworkTypesChangedCalled);
1535 
1536         long supportedNetworkTypes =
1537                 ShellIdentityUtils.invokeMethodWithShellPermissions(
1538                 mTelephonyManager, (tm) -> {
1539                         return tm.getSupportedRadioAccessFamily();
1540                     });
1541 
1542         mAllowedNetworkTypesCallback = new AllowedNetworkTypesListener();
1543         registerTelephonyCallbackWithPermission(mAllowedNetworkTypesCallback);
1544         long networkTypesToBeTested =
1545                 (supportedNetworkTypes & TelephonyManager.NETWORK_TYPE_BITMASK_NR) == 0
1546                         ? TelephonyManager.NETWORK_TYPE_BITMASK_LTE
1547                         : TelephonyManager.NETWORK_TYPE_BITMASK_NR;
1548         ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(
1549                 mTelephonyManager,
1550                 (tm) -> tm.setAllowedNetworkTypesForReason(
1551                         TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
1552                         networkTypesToBeTested));
1553 
1554         synchronized (mLock) {
1555             if (!mOnAllowedNetworkTypesChangedCalled) {
1556                 mLock.wait(WAIT_TIME);
1557             }
1558         }
1559 
1560         long allowedNetworkTypeUser = ShellIdentityUtils.invokeMethodWithShellPermissions(
1561                 mTelephonyManager, (tm) -> {
1562                     return tm.getAllowedNetworkTypesForReason(
1563                             TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER);
1564                 });
1565 
1566         assertEquals(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER, mAllowedNetworkTypeReason);
1567         assertEquals(allowedNetworkTypeUser, mAllowedNetworkTypeValue);
1568         // Test unregister
1569         unRegisterTelephonyCallback(mOnAllowedNetworkTypesChangedCalled,
1570                 mAllowedNetworkTypesCallback);
1571 
1572         // Recover the allowed network type user settings.
1573         ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(
1574                 mTelephonyManager,
1575                 (tm) -> tm.setAllowedNetworkTypesForReason(
1576                         TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
1577                         originalAllowedNetworkTypeUser));
1578     }
1579 
1580     private LinkCapacityEstimateChangedListener mLinkCapacityEstimateChangedListener;
1581 
1582     private class LinkCapacityEstimateChangedListener extends TelephonyCallback
1583             implements TelephonyCallback.LinkCapacityEstimateChangedListener {
1584         @Override
onLinkCapacityEstimateChanged( List<LinkCapacityEstimate> linkCapacityEstimateList)1585         public void onLinkCapacityEstimateChanged(
1586                 List<LinkCapacityEstimate> linkCapacityEstimateList) {
1587             synchronized (mLock) {
1588                 int lceType = linkCapacityEstimateList.get(0).getType();
1589                 if (lceType == LinkCapacityEstimate.LCE_TYPE_COMBINED
1590                         || lceType == LinkCapacityEstimate.LCE_TYPE_PRIMARY
1591                         || lceType == LinkCapacityEstimate.LCE_TYPE_SECONDARY) {
1592                     mOnLinkCapacityEstimateChangedCalled = true;
1593                 }
1594                 mLock.notify();
1595             }
1596         }
1597     }
1598 
1599     @Test
testOnLinkCapacityEstimateChangedByRegisterPhoneStateListener()1600     public void testOnLinkCapacityEstimateChangedByRegisterPhoneStateListener() throws Throwable {
1601 
1602         assertFalse(mOnLinkCapacityEstimateChangedCalled);
1603 
1604         mLinkCapacityEstimateChangedListener = new LinkCapacityEstimateChangedListener();
1605         registerTelephonyCallbackWithPermission(mLinkCapacityEstimateChangedListener);
1606 
1607         synchronized (mLock) {
1608             while (!mOnLinkCapacityEstimateChangedCalled) {
1609                 mLock.wait(WAIT_TIME);
1610             }
1611         }
1612         assertTrue(mOnLinkCapacityEstimateChangedCalled);
1613 
1614         // Test unregister
1615         unRegisterTelephonyCallback(mOnLinkCapacityEstimateChangedCalled,
1616                 mLinkCapacityEstimateChangedListener);
1617     }
1618 
1619 
1620     private EmergencyCallbackModeListener mEmergencyCallbackModeListener;
1621 
1622     private class EmergencyCallbackModeListener extends TelephonyCallback
1623             implements TelephonyCallback.EmergencyCallbackModeListener {
1624         @Override
onCallBackModeStarted(@mergencyCallbackModeType int type)1625         public void onCallBackModeStarted(@EmergencyCallbackModeType int type) {
1626 
1627         }
1628         @Override
onCallBackModeStopped(@mergencyCallbackModeType int type, @EmergencyCallbackModeStopReason int reason)1629         public void onCallBackModeStopped(@EmergencyCallbackModeType int type,
1630                 @EmergencyCallbackModeStopReason int reason) {
1631             synchronized (mLock) {
1632                 mOnEmergencyCallbackModeChangedCalled = true;
1633                 mLock.notify();
1634             }
1635         }
1636     }
1637 
1638     @Test
testOnEmergencyCallbackModeListener()1639     public void testOnEmergencyCallbackModeListener() throws Throwable {
1640         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
1641             assumeTrue(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CALLING));
1642         }
1643 
1644         if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) {
1645             Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE");
1646             return;
1647         }
1648 
1649         assertFalse(mOnEmergencyCallbackModeChangedCalled);
1650         mEmergencyCallbackModeListener = new EmergencyCallbackModeListener();
1651         registerTelephonyCallbackWithPermission(mEmergencyCallbackModeListener);
1652 
1653         synchronized (mLock) {
1654             while (!mOnEmergencyCallbackModeChangedCalled) {
1655                 mLock.wait(WAIT_TIME);
1656             }
1657         }
1658         assertTrue(mOnEmergencyCallbackModeChangedCalled);
1659 
1660         // Test unregister
1661         unRegisterTelephonyCallback(mOnEmergencyCallbackModeChangedCalled,
1662                 mEmergencyCallbackModeListener);
1663     }
1664 
1665     private CarrierRoamingNtnModeListener mCarrierRoamingNtnModeListener;
1666 
1667     private class CarrierRoamingNtnModeListener extends TelephonyCallback
1668             implements TelephonyCallback.CarrierRoamingNtnModeListener {
1669 
1670         @Override
onCarrierRoamingNtnModeChanged(boolean active)1671         public void onCarrierRoamingNtnModeChanged(boolean active) {
1672             synchronized (mLock) {
1673                 mOnCarrierRoamingNtnModeChangedCalled = true;
1674                 mLock.notify();
1675             }
1676         }
1677     }
1678 
1679     @Test
1680     @RequiresFlagsEnabled(Flags.FLAG_CARRIER_ENABLED_SATELLITE_FLAG)
testOnCarrierRoamingNtnModeListener()1681     public void testOnCarrierRoamingNtnModeListener() throws Throwable {
1682         assertFalse(mOnCarrierRoamingNtnModeChangedCalled);
1683         mCarrierRoamingNtnModeListener = new CarrierRoamingNtnModeListener();
1684         registerTelephonyCallback(mCarrierRoamingNtnModeListener);
1685 
1686         synchronized (mLock) {
1687             while (!mOnCarrierRoamingNtnModeChangedCalled) {
1688                 mLock.wait(WAIT_TIME);
1689             }
1690         }
1691         assertTrue(mOnCarrierRoamingNtnModeChangedCalled);
1692 
1693         unRegisterTelephonyCallback(mOnCarrierRoamingNtnModeChangedCalled,
1694                 mCarrierRoamingNtnModeListener);
1695     }
1696 }
1697