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 // Disable suppressing blocking. 1046 TelephonyUtils.endBlockSuppression(InstrumentationRegistry.getInstrumentation()); 1047 } 1048 1049 private ActiveDataSubscriptionIdListener mActiveDataSubscriptionIdCallback; 1050 1051 private class ActiveDataSubscriptionIdListener extends TelephonyCallback 1052 implements TelephonyCallback.ActiveDataSubscriptionIdListener { 1053 @Override onActiveDataSubscriptionIdChanged(int subId)1054 public void onActiveDataSubscriptionIdChanged(int subId) { 1055 synchronized (mLock) { 1056 mOnActiveDataSubscriptionIdChanged = true; 1057 mLock.notify(); 1058 } 1059 } 1060 } 1061 1062 @Test testOnActiveDataSubscriptionIdChangedByRegisterTelephonyCallback()1063 public void testOnActiveDataSubscriptionIdChangedByRegisterTelephonyCallback() 1064 throws Throwable { 1065 if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) { 1066 Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE"); 1067 return; 1068 } 1069 assertFalse(mOnActiveDataSubscriptionIdChanged); 1070 1071 mHandler.post(() -> { 1072 mActiveDataSubscriptionIdCallback = 1073 new ActiveDataSubscriptionIdListener(); 1074 registerTelephonyCallback(mActiveDataSubscriptionIdCallback); 1075 }); 1076 synchronized (mLock) { 1077 if (!mOnActiveDataSubscriptionIdChanged) { 1078 mLock.wait(WAIT_TIME); 1079 } 1080 } 1081 1082 assertTrue(mOnActiveDataSubscriptionIdChanged); 1083 1084 // Test unregister 1085 unRegisterTelephonyCallback(mOnActiveDataSubscriptionIdChanged, 1086 mActiveDataSubscriptionIdCallback); 1087 } 1088 1089 private BarringInfoListener mBarringInfoCallback; 1090 1091 private class BarringInfoListener extends TelephonyCallback 1092 implements TelephonyCallback.BarringInfoListener { 1093 @Override onBarringInfoChanged(BarringInfo barringInfo)1094 public void onBarringInfoChanged(BarringInfo barringInfo) { 1095 synchronized (mLock) { 1096 mOnBarringInfoChangedCalled = true; 1097 mBarringInfo = barringInfo; 1098 mLock.notify(); 1099 } 1100 } 1101 } 1102 1103 @Test testOnBarringInfoChangedByRegisterTelephonyCallback()1104 public void testOnBarringInfoChangedByRegisterTelephonyCallback() throws Throwable { 1105 if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) { 1106 Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE"); 1107 return; 1108 } 1109 1110 assertFalse(mOnBarringInfoChangedCalled); 1111 mHandler.post(() -> { 1112 mBarringInfoCallback = new BarringInfoListener(); 1113 registerTelephonyCallbackWithPermission(mBarringInfoCallback); 1114 }); 1115 1116 synchronized (mLock) { 1117 if (!mOnBarringInfoChangedCalled) { 1118 mLock.wait(WAIT_TIME); 1119 } 1120 } 1121 assertTrue(mOnBarringInfoChangedCalled); 1122 1123 assertBarringInfoSane(mBarringInfo); 1124 1125 // Test unregister 1126 unRegisterTelephonyCallback(mOnBarringInfoChangedCalled, mBarringInfoCallback); 1127 } 1128 1129 private static final int[] sBarringServiceInfoTypes = new int[]{ 1130 BarringInfo.BARRING_SERVICE_TYPE_CS_SERVICE, 1131 BarringInfo.BARRING_SERVICE_TYPE_PS_SERVICE, 1132 BarringInfo.BARRING_SERVICE_TYPE_CS_VOICE, 1133 BarringInfo.BARRING_SERVICE_TYPE_MO_SIGNALLING, 1134 BarringInfo.BARRING_SERVICE_TYPE_MO_DATA, 1135 BarringInfo.BARRING_SERVICE_TYPE_CS_FALLBACK, 1136 BarringInfo.BARRING_SERVICE_TYPE_MMTEL_VOICE, 1137 BarringInfo.BARRING_SERVICE_TYPE_MMTEL_VIDEO, 1138 BarringInfo.BARRING_SERVICE_TYPE_EMERGENCY, 1139 BarringInfo.BARRING_SERVICE_TYPE_SMS 1140 }; 1141 assertBarringInfoSane(BarringInfo barringInfo)1142 private static void assertBarringInfoSane(BarringInfo barringInfo) { 1143 assertNotNull(barringInfo); 1144 1145 // Flags to track whether we have had unknown and known barring types reported 1146 boolean hasBarringTypeUnknown = false; 1147 boolean hasBarringTypeKnown = false; 1148 1149 for (int bsiType : sBarringServiceInfoTypes) { 1150 BarringInfo.BarringServiceInfo bsi = barringInfo.getBarringServiceInfo(bsiType); 1151 assertNotNull(bsi); 1152 switch (bsi.getBarringType()) { 1153 case BarringInfo.BarringServiceInfo.BARRING_TYPE_UNKNOWN: 1154 hasBarringTypeUnknown = true; 1155 assertFalse(bsi.isConditionallyBarred()); 1156 assertEquals(0, bsi.getConditionalBarringFactor()); 1157 assertEquals(0, bsi.getConditionalBarringTimeSeconds()); 1158 assertFalse(bsi.isBarred()); 1159 break; 1160 1161 case BarringInfo.BarringServiceInfo.BARRING_TYPE_NONE: 1162 hasBarringTypeKnown = true; 1163 // Unless conditional barring is active, all conditional barring fields 1164 // should be "unset". 1165 assertFalse(bsi.isConditionallyBarred()); 1166 assertEquals(0, bsi.getConditionalBarringFactor()); 1167 assertEquals(0, bsi.getConditionalBarringTimeSeconds()); 1168 assertFalse(bsi.isBarred()); 1169 break; 1170 1171 case BarringInfo.BarringServiceInfo.BARRING_TYPE_UNCONDITIONAL: 1172 hasBarringTypeKnown = true; 1173 // Unless conditional barring is active, all conditional barring fields 1174 // should be "unset". 1175 assertFalse(bsi.isConditionallyBarred()); 1176 assertEquals(0, bsi.getConditionalBarringFactor()); 1177 assertEquals(0, bsi.getConditionalBarringTimeSeconds()); 1178 assertTrue(bsi.isBarred()); 1179 break; 1180 1181 case BarringInfo.BarringServiceInfo.BARRING_TYPE_CONDITIONAL: 1182 hasBarringTypeKnown = true; 1183 // If conditional barring is active, then the barring time and factor must 1184 // be known (set), but the device may or may not be barred at the moment, 1185 // so isConditionallyBarred() can be either true or false (hence not checked). 1186 assertNotEquals(0, bsi.getConditionalBarringFactor()); 1187 assertNotEquals(0, bsi.getConditionalBarringTimeSeconds()); 1188 assertEquals(bsi.isBarred(), bsi.isConditionallyBarred()); 1189 break; 1190 } 1191 } 1192 // If any barring type is unknown, then barring is not supported so all must be 1193 // unknown. If any type is known, then all that are not reported are assumed to 1194 // be not barred. 1195 assertNotEquals(hasBarringTypeUnknown, hasBarringTypeKnown); 1196 } 1197 1198 private RegistrationFailedListener mRegistrationFailedCallback; 1199 1200 private class RegistrationFailedListener extends TelephonyCallback 1201 implements TelephonyCallback.RegistrationFailedListener { 1202 @Override onRegistrationFailed(CellIdentity cid, String chosenPlmn, int domain, int causeCode, int additionalCauseCode)1203 public void onRegistrationFailed(CellIdentity cid, String chosenPlmn, 1204 int domain, int causeCode, int additionalCauseCode) { 1205 synchronized (mLock) { 1206 mOnRegistrationFailedCalled = true; 1207 mLock.notify(); 1208 } 1209 } 1210 } 1211 1212 @Test testOnRegistrationFailedByRegisterTelephonyCallback()1213 public void testOnRegistrationFailedByRegisterTelephonyCallback() throws Throwable { 1214 if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) { 1215 Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE"); 1216 return; 1217 } 1218 1219 assertFalse(mOnBarringInfoChangedCalled); 1220 mHandler.post(() -> { 1221 mRegistrationFailedCallback = new RegistrationFailedListener(); 1222 registerTelephonyCallbackWithPermission(mRegistrationFailedCallback); 1223 1224 }); 1225 1226 synchronized (mLock) { 1227 if (!mOnBarringInfoChangedCalled) { 1228 mLock.wait(WAIT_TIME); 1229 } 1230 } 1231 1232 // Assert that in the WAIT_TIME interval, the listener wasn't invoked. While this is 1233 // **technically** a flaky test, in practice this flake should happen approximately never 1234 // as it would mean that a registered phone is failing to reselect during CTS at this 1235 // exact moment. 1236 // 1237 // What the test is verifying is that there is no "auto" callback for registration 1238 // failure because unlike other PSL registrants, this one is not called upon registration. 1239 assertFalse(mOnRegistrationFailedCalled); 1240 1241 // Test unregister 1242 unRegisterTelephonyCallback(mOnRegistrationFailedCalled, mRegistrationFailedCallback); 1243 } 1244 1245 private PhysicalChannelConfigListener mPhysicalChannelConfigCallback; 1246 1247 private class PhysicalChannelConfigListener extends TelephonyCallback 1248 implements TelephonyCallback.PhysicalChannelConfigListener { 1249 @Override onPhysicalChannelConfigChanged( @onNull List<PhysicalChannelConfig> configs)1250 public void onPhysicalChannelConfigChanged( 1251 @NonNull List<PhysicalChannelConfig> configs) { 1252 synchronized (mLock) { 1253 mOnPhysicalChannelConfigCalled = true; 1254 mLock.notify(); 1255 } 1256 } 1257 } 1258 1259 @Test testOnPhysicalChannelConfigChanged()1260 public void testOnPhysicalChannelConfigChanged() throws Throwable { 1261 if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) { 1262 Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE"); 1263 return; 1264 } 1265 1266 assertFalse(mOnPhysicalChannelConfigCalled); 1267 mHandler.post(() -> { 1268 mPhysicalChannelConfigCallback = 1269 new PhysicalChannelConfigListener(); 1270 registerTelephonyCallbackWithPermission(mPhysicalChannelConfigCallback); 1271 }); 1272 1273 synchronized (mLock) { 1274 while (!mOnPhysicalChannelConfigCalled) { 1275 mLock.wait(WAIT_TIME); 1276 } 1277 } 1278 assertTrue(mOnPhysicalChannelConfigCalled); 1279 1280 // Test unregister 1281 unRegisterTelephonyCallback(mOnPhysicalChannelConfigCalled, 1282 mPhysicalChannelConfigCallback); 1283 } 1284 1285 private DataEnabledListener mDataEnabledCallback; 1286 1287 private class DataEnabledListener extends TelephonyCallback 1288 implements TelephonyCallback.DataEnabledListener { 1289 @Override onDataEnabledChanged(boolean enabled, @DataEnabledReason int reason)1290 public void onDataEnabledChanged(boolean enabled, @DataEnabledReason int reason) { 1291 synchronized (mLock) { 1292 mOnDataEnabledChangedCalled = true; 1293 mLock.notify(); 1294 } 1295 } 1296 } 1297 1298 @Test testOnDataEnabledChangedByRegisterTelephonyCallback()1299 public void testOnDataEnabledChangedByRegisterTelephonyCallback() throws Throwable { 1300 if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) { 1301 Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE"); 1302 return; 1303 } 1304 1305 assertFalse(mOnDataEnabledChangedCalled); 1306 mHandler.post(() -> { 1307 mDataEnabledCallback = new DataEnabledListener(); 1308 registerTelephonyCallbackWithPermission(mDataEnabledCallback); 1309 }); 1310 1311 synchronized (mLock) { 1312 while (!mOnDataEnabledChangedCalled) { 1313 mLock.wait(WAIT_TIME); 1314 } 1315 } 1316 assertTrue(mOnDataEnabledChangedCalled); 1317 1318 // Test unregister 1319 unRegisterTelephonyCallback(mOnDataEnabledChangedCalled, mDataEnabledCallback); 1320 } 1321 1322 private AllowedNetworkTypesListener mAllowedNetworkTypesCallback; 1323 1324 private class AllowedNetworkTypesListener extends TelephonyCallback 1325 implements TelephonyCallback.AllowedNetworkTypesListener { 1326 @Override onAllowedNetworkTypesChanged(int reason, long allowedNetworkType)1327 public void onAllowedNetworkTypesChanged(int reason, long allowedNetworkType) { 1328 synchronized (mLock) { 1329 Log.d(TAG, "onAllowedNetworkTypesChanged"); 1330 mAllowedNetworkTypeReason = reason; 1331 mAllowedNetworkTypeValue = allowedNetworkType; 1332 mOnAllowedNetworkTypesChangedCalled = true; 1333 1334 mLock.notify(); 1335 } 1336 } 1337 } 1338 1339 @Test testOnAllowedNetworkTypesChangedByRegisterPhoneStateListener()1340 public void testOnAllowedNetworkTypesChangedByRegisterPhoneStateListener() throws Throwable { 1341 if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) { 1342 Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE"); 1343 return; 1344 } 1345 long originalAllowedNetworkTypeUser = ShellIdentityUtils.invokeMethodWithShellPermissions( 1346 mTelephonyManager, (tm) -> { 1347 return tm.getAllowedNetworkTypesForReason( 1348 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER); 1349 }); 1350 assertFalse(mOnAllowedNetworkTypesChangedCalled); 1351 1352 mHandler.post(() -> { 1353 mAllowedNetworkTypesCallback = new AllowedNetworkTypesListener(); 1354 registerTelephonyCallbackWithPermission(mAllowedNetworkTypesCallback); 1355 ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn( 1356 mTelephonyManager, 1357 (tm) -> tm.setAllowedNetworkTypesForReason( 1358 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER, 1359 TelephonyManager.NETWORK_TYPE_BITMASK_NR)); 1360 }); 1361 1362 synchronized (mLock) { 1363 if (!mOnAllowedNetworkTypesChangedCalled) { 1364 mLock.wait(WAIT_TIME); 1365 } 1366 } 1367 1368 long allowedNetworkTypeUser = ShellIdentityUtils.invokeMethodWithShellPermissions( 1369 mTelephonyManager, (tm) -> { 1370 return tm.getAllowedNetworkTypesForReason( 1371 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER); 1372 }); 1373 1374 assertEquals(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER, mAllowedNetworkTypeReason); 1375 assertEquals(allowedNetworkTypeUser, mAllowedNetworkTypeValue); 1376 // Test unregister 1377 unRegisterTelephonyCallback(mOnAllowedNetworkTypesChangedCalled, 1378 mAllowedNetworkTypesCallback); 1379 1380 // Recover the allowed network type user settings. 1381 ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn( 1382 mTelephonyManager, 1383 (tm) -> tm.setAllowedNetworkTypesForReason( 1384 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER, 1385 originalAllowedNetworkTypeUser)); 1386 } 1387 1388 private LinkCapacityEstimateChangedListener mLinkCapacityEstimateChangedListener; 1389 1390 private class LinkCapacityEstimateChangedListener extends TelephonyCallback 1391 implements TelephonyCallback.LinkCapacityEstimateChangedListener { 1392 @Override onLinkCapacityEstimateChanged( List<LinkCapacityEstimate> linkCapacityEstimateList)1393 public void onLinkCapacityEstimateChanged( 1394 List<LinkCapacityEstimate> linkCapacityEstimateList) { 1395 synchronized (mLock) { 1396 int lceType = linkCapacityEstimateList.get(0).getType(); 1397 if (lceType == LinkCapacityEstimate.LCE_TYPE_COMBINED 1398 || lceType == LinkCapacityEstimate.LCE_TYPE_PRIMARY 1399 || lceType == LinkCapacityEstimate.LCE_TYPE_SECONDARY) { 1400 mOnLinkCapacityEstimateChangedCalled = true; 1401 } 1402 mLock.notify(); 1403 } 1404 } 1405 } 1406 1407 @Test testOnLinkCapacityEstimateChangedByRegisterPhoneStateListener()1408 public void testOnLinkCapacityEstimateChangedByRegisterPhoneStateListener() throws Throwable { 1409 if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) { 1410 Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE"); 1411 return; 1412 } 1413 1414 assertFalse(mOnLinkCapacityEstimateChangedCalled); 1415 mHandler.post(() -> { 1416 mLinkCapacityEstimateChangedListener = new LinkCapacityEstimateChangedListener(); 1417 registerTelephonyCallbackWithPermission(mLinkCapacityEstimateChangedListener); 1418 }); 1419 1420 synchronized (mLock) { 1421 while (!mOnLinkCapacityEstimateChangedCalled) { 1422 mLock.wait(WAIT_TIME); 1423 } 1424 } 1425 assertTrue(mOnLinkCapacityEstimateChangedCalled); 1426 1427 // Test unregister 1428 unRegisterTelephonyCallback(mOnLinkCapacityEstimateChangedCalled, 1429 mLinkCapacityEstimateChangedListener); 1430 } 1431 } 1432