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