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