Snap for 4793185 from 14b3c50f80ec06dbe21bf3980dd254582cbd8814 to pi-release

Change-Id: I3236dfcdc7856a0b42131c2ca9b54b58b5bd8ee4
diff --git a/tests/cts/net/src/android/net/wifi/rtt/cts/TestBase.java b/tests/cts/net/src/android/net/wifi/rtt/cts/TestBase.java
index adbe0f6..57ea2a5 100644
--- a/tests/cts/net/src/android/net/wifi/rtt/cts/TestBase.java
+++ b/tests/cts/net/src/android/net/wifi/rtt/cts/TestBase.java
@@ -20,10 +20,9 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.PackageManager;
 import android.location.LocationManager;
-import android.net.NetworkInfo;
 import android.net.wifi.ScanResult;
-import android.net.wifi.SupplicantState;
 import android.net.wifi.WifiManager;
 import android.net.wifi.rtt.RangingResult;
 import android.net.wifi.rtt.RangingResultCallback;
@@ -45,9 +44,6 @@
 public class TestBase extends AndroidTestCase {
     protected static final String TAG = "WifiRttCtsTests";
 
-    // wait for Wi-Fi to enable and/or connect
-    private static final int WAIT_FOR_WIFI_ENABLE_SECS = 10;
-
     // wait for Wi-Fi RTT to become available
     private static final int WAIT_FOR_RTT_CHANGE_SECS = 10;
 
@@ -59,10 +55,6 @@
     private LocationManager mLocationManager;
     private WifiManager.WifiLock mWifiLock;
 
-    protected IntentFilter mRttIntent;
-    protected IntentFilter mWifiStateIntent;
-    protected IntentFilter mWifiConnectedIntent;
-
     private final HandlerThread mHandlerThread = new HandlerThread("SingleDeviceTest");
     protected final Executor mExecutor;
     {
@@ -75,10 +67,8 @@
      * should be tested if the feature is supported on the current device.
      */
     static boolean shouldTestWifiRtt(Context context) {
-        // TODO b/74457054: enable when t/31350604 resolved
-        return false;
-//        final PackageManager pm = context.getPackageManager();
-//        return pm.hasSystemFeature(PackageManager.FEATURE_WIFI_RTT);
+        final PackageManager pm = context.getPackageManager();
+        return pm.hasSystemFeature(PackageManager.FEATURE_WIFI_RTT);
     }
 
     @Override
@@ -98,43 +88,22 @@
                 Context.WIFI_RTT_RANGING_SERVICE);
         assertNotNull("Wi-Fi RTT Manager", mWifiRttManager);
 
-        mRttIntent = new IntentFilter();
-        mRttIntent.addAction(WifiRttManager.ACTION_WIFI_RTT_STATE_CHANGED);
-
-        mWifiStateIntent = new IntentFilter();
-        mWifiStateIntent.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
-
-        mWifiConnectedIntent = new IntentFilter();
-        mWifiConnectedIntent.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
-
         mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
         assertNotNull("Wi-Fi Manager", mWifiManager);
         mWifiLock = mWifiManager.createWifiLock(TAG);
         mWifiLock.acquire();
-        assertTrue("Location scans must be enabled", mWifiManager.isScanAlwaysAvailable());
         if (!mWifiManager.isWifiEnabled()) {
             mWifiManager.setWifiEnabled(true);
-            WifiEnableBroadcastReceiver receiver = new WifiEnableBroadcastReceiver(true);
-            mContext.registerReceiver(receiver, mWifiStateIntent);
-            receiver.waitForDesiredState();
-            mContext.unregisterReceiver(receiver);
         }
 
-        if (!mWifiManager.getConnectionInfo().getSupplicantState().equals(
-                SupplicantState.COMPLETED)) {
-            WifiConnectedBroadcastReceiver receiver = new WifiConnectedBroadcastReceiver();
-            mContext.registerReceiver(receiver, mWifiConnectedIntent);
-            receiver.waitForConnected();
-            mContext.unregisterReceiver(receiver);
-        }
-
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(WifiRttManager.ACTION_WIFI_RTT_STATE_CHANGED);
+        WifiRttBroadcastReceiver receiver = new WifiRttBroadcastReceiver();
+        mContext.registerReceiver(receiver, intentFilter);
         if (!mWifiRttManager.isAvailable()) {
-            WifiRttBroadcastReceiver receiver = new WifiRttBroadcastReceiver();
-            mContext.registerReceiver(receiver, mRttIntent);
             assertTrue("Timeout waiting for Wi-Fi RTT to change status",
                     receiver.waitForStateChange());
             assertTrue("Wi-Fi RTT is not available (should be)", mWifiRttManager.isAvailable());
-            mContext.unregisterReceiver(receiver);
         }
     }
 
@@ -148,77 +117,33 @@
         super.tearDown();
     }
 
-    private class SyncBroadcastReceiver extends BroadcastReceiver {
+    class WifiRttBroadcastReceiver extends BroadcastReceiver {
         private CountDownLatch mBlocker = new CountDownLatch(1);
-        private String mAction;
-        private int mTimeout;
-
-        SyncBroadcastReceiver(String action, int timeout) {
-            mAction = action;
-            mTimeout = timeout;
-        }
 
         @Override
         public void onReceive(Context context, Intent intent) {
-            if (mAction.equals(intent.getAction())) {
+            if (WifiRttManager.ACTION_WIFI_RTT_STATE_CHANGED.equals(intent.getAction())) {
                 mBlocker.countDown();
             }
         }
 
         boolean waitForStateChange() throws InterruptedException {
-            return mBlocker.await(mTimeout, TimeUnit.SECONDS);
-        }
-    };
-
-    class WifiEnableBroadcastReceiver extends BroadcastReceiver {
-        private CountDownLatch mBlocker = new CountDownLatch(1);
-        private boolean mDesiredState;
-
-        WifiEnableBroadcastReceiver(boolean desiredState) {
-            mDesiredState = desiredState;
-        }
-
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
-                if (mWifiManager.isWifiEnabled() == mDesiredState) {
-                    mBlocker.countDown();
-                }
-            }
-        }
-
-        boolean waitForDesiredState() throws InterruptedException {
-            return mBlocker.await(WAIT_FOR_WIFI_ENABLE_SECS, TimeUnit.SECONDS);
+            return mBlocker.await(WAIT_FOR_RTT_CHANGE_SECS, TimeUnit.SECONDS);
         }
     }
 
-    class WifiConnectedBroadcastReceiver extends BroadcastReceiver {
+    class WifiScansBroadcastReceiver extends BroadcastReceiver {
         private CountDownLatch mBlocker = new CountDownLatch(1);
 
         @Override
         public void onReceive(Context context, Intent intent) {
-            if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
-                NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
-                if (ni.getState() == NetworkInfo.State.CONNECTED) {
-                    mBlocker.countDown();
-                }
+            if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {
+                mBlocker.countDown();
             }
         }
 
-        boolean waitForConnected() throws InterruptedException {
-            return mBlocker.await(WAIT_FOR_WIFI_ENABLE_SECS, TimeUnit.SECONDS);
-        }
-    }
-
-    class WifiRttBroadcastReceiver extends SyncBroadcastReceiver {
-        WifiRttBroadcastReceiver() {
-            super(WifiRttManager.ACTION_WIFI_RTT_STATE_CHANGED, WAIT_FOR_RTT_CHANGE_SECS);
-        }
-    }
-
-    class WifiScansBroadcastReceiver extends SyncBroadcastReceiver {
-        WifiScansBroadcastReceiver() {
-            super(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION, WAIT_FOR_SCAN_RESULTS_SECS);
+        boolean waitForStateChange() throws InterruptedException {
+            return mBlocker.await(WAIT_FOR_SCAN_RESULTS_SECS, TimeUnit.SECONDS);
         }
     }
 
@@ -281,33 +206,30 @@
     }
 
     /**
-     * Start a scan and return the test AP with the specified SSID and which supports IEEE 802.11mc.
-     * If the AP is not found re-attempts the scan maxScanRetries times (i.e. total number of
-     * scans can be maxScanRetries + 1).
+     * Start a scan and return a test AP which supports IEEE 802.11mc and which has the highest
+     * RSSI. Will perform N (parameterized) scans and get the best AP across both scans.
      *
      * Returns null if test AP is not found in the specified number of scans.
      *
-     * @param bssid The BSSID of the test AP
-     * @param maxScanRetries Maximum number of scans retries (in addition to first scan).
+     * @param numScanRetries Maximum number of scans retries (in addition to first scan).
      */
-    protected ScanResult scanForTestAp(String bssid, int maxScanRetries)
+    protected ScanResult scanForTestAp(int numScanRetries)
             throws InterruptedException {
         int scanCount = 0;
-        while (scanCount <= maxScanRetries) {
+        ScanResult bestTestAp = null;
+        while (scanCount <= numScanRetries) {
             for (ScanResult scanResult : scanAps()) {
                 if (!scanResult.is80211mcResponder()) {
                     continue;
                 }
-                if (!bssid.equals(scanResult.BSSID)) {
-                    continue;
+                if (bestTestAp == null || scanResult.level > bestTestAp.level) {
+                    bestTestAp = scanResult;
                 }
-
-                return scanResult;
             }
 
             scanCount++;
         }
 
-        return null;
+        return bestTestAp;
     }
 }
diff --git a/tests/cts/net/src/android/net/wifi/rtt/cts/WifiRttTest.java b/tests/cts/net/src/android/net/wifi/rtt/cts/WifiRttTest.java
index 8c59a57..5995bbd 100644
--- a/tests/cts/net/src/android/net/wifi/rtt/cts/WifiRttTest.java
+++ b/tests/cts/net/src/android/net/wifi/rtt/cts/WifiRttTest.java
@@ -16,9 +16,11 @@
 
 package android.net.wifi.rtt.cts;
 
+import android.content.IntentFilter;
 import android.net.wifi.ScanResult;
 import android.net.wifi.rtt.RangingRequest;
 import android.net.wifi.rtt.RangingResult;
+import android.net.wifi.rtt.WifiRttManager;
 
 import com.android.compatibility.common.util.DeviceReportLog;
 import com.android.compatibility.common.util.ResultType;
@@ -32,8 +34,8 @@
  * Wi-Fi RTT CTS test: range to all available Access Points which support IEEE 802.11mc.
  */
 public class WifiRttTest extends TestBase {
-    // Max number of scan retries to do while searching for APs supporting IEEE 802.11mc
-    private static final int MAX_NUM_SCAN_RETRIES_SEARCHING_FOR_IEEE80211MC_AP = 2;
+    // Number of scans to do while searching for APs supporting IEEE 802.11mc
+    private static final int NUM_SCANS_SEARCHING_FOR_IEEE80211MC_AP = 2;
 
     // Number of RTT measurements per AP
     private static final int NUM_OF_RTT_ITERATIONS = 10;
@@ -60,18 +62,9 @@
             return;
         }
 
-        // Find the associated AP: get BSSID and disable Wi-Fi
-        String bssid = mWifiManager.getConnectionInfo().getBSSID();
-        assertNotNull("Null BSSID - must be associated!", bssid);
-        assertTrue("Cannot disable Wi-Fi (to disassociate)", mWifiManager.setWifiEnabled(false));
-        WifiEnableBroadcastReceiver receiver = new WifiEnableBroadcastReceiver(false);
-        mContext.registerReceiver(receiver, mWifiStateIntent);
-        receiver.waitForDesiredState();
-        mContext.unregisterReceiver(receiver);
-
         // Scan for IEEE 802.11mc supporting APs
-        ScanResult testAp = scanForTestAp(bssid, MAX_NUM_SCAN_RETRIES_SEARCHING_FOR_IEEE80211MC_AP);
-        assertTrue("Cannot find test AP: bssid=" + bssid, testAp != null);
+        ScanResult testAp = scanForTestAp(NUM_SCANS_SEARCHING_FOR_IEEE80211MC_AP);
+        assertTrue("Cannot find test AP", testAp != null);
 
         // Perform RTT operations
         RangingRequest request = new RangingRequest.Builder().addAccessPoint(testAp).build();