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