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