Reset device state correctly after calling subscriptionEnabled APIs.

Test: atest SubscriptionManagerTest#testSetAndCheckSubscriptionEnabled
followed by atest SubscriptionManagerTest#testSanity
Bug: 158892113

Merged-in: I719076828f3f0e9840acb6caae6a8b57e15a696e
Change-Id: I719076828f3f0e9840acb6caae6a8b57e15a696e
(cherry picked from commit 0b50e71e5e5286b1fe37d6996ef9c6f9403cf240)
diff --git a/tests/tests/telephony/current/src/android/telephony/cts/SubscriptionManagerTest.java b/tests/tests/telephony/current/src/android/telephony/cts/SubscriptionManagerTest.java
index ce2c3e4..174af37 100644
--- a/tests/tests/telephony/current/src/android/telephony/cts/SubscriptionManagerTest.java
+++ b/tests/tests/telephony/current/src/android/telephony/cts/SubscriptionManagerTest.java
@@ -69,6 +69,7 @@
 import java.util.concurrent.Executor;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
@@ -690,8 +691,10 @@
         boolean enabled = executeWithShellPermissionAndDefault(false, mSm,
                 (sm) -> sm.isSubscriptionEnabled(mSubId));
 
+        AtomicBoolean waitForIsEnabledValue = new AtomicBoolean(!enabled);
         // wait for the first call to take effect
-        CountDownLatch subscriptionEnabledLatch = new CountDownLatch(1);
+        Object lock = new Object();
+        AtomicBoolean setSubscriptionEnabledCallCompleted = new AtomicBoolean(false);
         TestThread t = new TestThread(new Runnable() {
             @Override
             public void run() {
@@ -701,9 +704,13 @@
                         new SubscriptionManager.OnSubscriptionsChangedListener() {
                             @Override
                             public void onSubscriptionsChanged() {
-                                if (executeWithShellPermissionAndDefault(enabled, mSm,
-                                        (sm) -> sm.isSubscriptionEnabled(mSubId)) != enabled) {
-                                    subscriptionEnabledLatch.countDown();
+                                boolean waitForValue = waitForIsEnabledValue.get();
+                                if (executeWithShellPermissionAndDefault(!waitForValue, mSm,
+                                        (sm) -> sm.isSubscriptionEnabled(mSubId)) == waitForValue) {
+                                    synchronized (lock) {
+                                        setSubscriptionEnabledCallCompleted.set(true);
+                                        lock.notifyAll();
+                                    }
                                 }
                             }
                         };
@@ -720,20 +727,43 @@
             executeWithShellPermissionAndDefault(false, mSm,
                     (sm) -> sm.setSubscriptionEnabled(mSubId, !enabled));
 
-            boolean setSubscriptionEnabledCallComplete =
-                    subscriptionEnabledLatch.await(5000, TimeUnit.MILLISECONDS);
-            if (!setSubscriptionEnabledCallComplete) {
+            synchronized (lock) {
+                if (!setSubscriptionEnabledCallCompleted.get()) {
+                    lock.wait(5000);
+                }
+            }
+            if (!setSubscriptionEnabledCallCompleted.get()) {
                 // not treating this as test failure as it may be due to UX confirmation or may not
                 // be supported
                 Log.e(TAG, "setSubscriptionEnabled() did not complete");
                 return;
             }
 
+            // switch back to the original value
+            waitForIsEnabledValue.set(enabled);
+            setSubscriptionEnabledCallCompleted.set(false);
             executeWithShellPermissionAndDefault(false, mSm,
                     (sm) -> sm.setSubscriptionEnabled(mSubId, enabled));
+
+            // wait to make sure device is left in the same state after the test as it was before
+            // the test
+            synchronized (lock) {
+                if (!setSubscriptionEnabledCallCompleted.get()) {
+                    // longer wait time on purpose as re-enabling can take a longer time
+                    lock.wait(50000);
+                }
+            }
+            if (!setSubscriptionEnabledCallCompleted.get()) {
+                // treat this as failure because it worked the first time
+                fail("setSubscriptionEnabled() did not work second time");
+            }
         } catch (InterruptedException e) {
-            // ignore
+            fail("InterruptedException");
         }
+
+        // Reset default data subId as it may have been changed as part of the calls above
+        ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(mSm,
+                (sm) -> sm.setDefaultDataSubId(mSubId));
     }
 
     @Test