Wait for location settings change in test

When setting the location mode, use a ContentObserver to wait for the
change to propogate before returning from the method.

Fixes: 163981473
Test: atest NetworkScanApiTest
Change-Id: I00908f389f86aa82215dde74bd9778f790057d86
diff --git a/tests/tests/carrierapi/src/android/carrierapi/cts/NetworkScanApiTest.java b/tests/tests/carrierapi/src/android/carrierapi/cts/NetworkScanApiTest.java
index efb4432..017d7fa 100644
--- a/tests/tests/carrierapi/src/android/carrierapi/cts/NetworkScanApiTest.java
+++ b/tests/tests/carrierapi/src/android/carrierapi/cts/NetworkScanApiTest.java
@@ -27,6 +27,7 @@
 import android.content.Context;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
+import android.database.ContentObserver;
 import android.os.AsyncTask;
 import android.os.Handler;
 import android.os.HandlerThread;
@@ -58,6 +59,8 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 /**
@@ -83,6 +86,7 @@
     private NetworkScan mNetworkScan;
     private NetworkScanRequest mNetworkScanRequest;
     private NetworkScanCallbackImpl mNetworkScanCallback;
+    private static final int LOCATION_SETTING_CHANGE_WAIT_MS = 1000;
     private static final int MAX_CELLINFO_WAIT_MILLIS = 5000; // 5 seconds
     private static final int SCAN_SEARCH_TIME_SECONDS = 60;
     // Wait one second longer than the max scan search time to give the test time to receive the
@@ -454,6 +458,15 @@
     }
 
     private boolean getAndSetLocationSwitch(boolean enabled) {
+        CountDownLatch locationChangeLatch = new CountDownLatch(1);
+        ContentObserver settingsObserver = new ContentObserver(mHandler) {
+            @Override
+            public void onChange(boolean selfChange) {
+                locationChangeLatch.countDown();
+                super.onChange(selfChange);
+            }
+        };
+
         InstrumentationRegistry.getInstrumentation().getUiAutomation()
                 .adoptShellPermissionIdentity();
         try {
@@ -464,8 +477,22 @@
             int locationMode = enabled ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY
                     : Settings.Secure.LOCATION_MODE_OFF;
             if (locationMode != oldLocationMode) {
+                InstrumentationRegistry.getContext().getContentResolver().registerContentObserver(
+                        Settings.Secure.getUriFor(Settings.Secure.LOCATION_MODE),
+                        false, settingsObserver);
                 Settings.Secure.putInt(InstrumentationRegistry.getContext().getContentResolver(),
                         Settings.Secure.LOCATION_MODE, locationMode);
+                try {
+                    assertTrue(locationChangeLatch.await(LOCATION_SETTING_CHANGE_WAIT_MS,
+                            TimeUnit.MILLISECONDS));
+                } catch (InterruptedException e) {
+                    Log.w(NetworkScanApiTest.class.getSimpleName(),
+                            "Interrupted while waiting for location settings change. Test results"
+                            + " may not be accurate.");
+                } finally {
+                    InstrumentationRegistry.getContext().getContentResolver()
+                            .unregisterContentObserver(settingsObserver);
+                }
             }
             return oldLocationMode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
         } finally {