Merge "Wait for next network in waitForAvailable" am: e9f65535e0

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1760698

Change-Id: I3390ce0ad504e7367840ef13b9311e889a9798b1
This commit is contained in:
Remi NGUYEN VAN
2021-07-19 10:06:02 +00:00
committed by Automerger Merge Worker

View File

@@ -54,6 +54,7 @@ import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Binder; import android.os.Binder;
import android.os.Build; import android.os.Build;
import android.os.ConditionVariable;
import android.os.IBinder; import android.os.IBinder;
import android.os.SystemClock; import android.os.SystemClock;
import android.system.Os; import android.system.Os;
@@ -264,6 +265,9 @@ public final class CtsNetUtils {
Log.w(TAG, "connect failed with " + error + "; waiting before retry"); Log.w(TAG, "connect failed with " + error + "; waiting before retry");
SystemClock.sleep(WIFI_CONNECT_INTERVAL_MS); SystemClock.sleep(WIFI_CONNECT_INTERVAL_MS);
} }
fail("Failed to connect to " + config.SSID
+ " after " + MAX_WIFI_CONNECT_RETRIES + "retries");
} }
private static class ConnectWifiListener implements WifiManager.ActionListener { private static class ConnectWifiListener implements WifiManager.ActionListener {
@@ -696,16 +700,28 @@ public final class CtsNetUtils {
* {@code onAvailable}. * {@code onAvailable}.
*/ */
public static class TestNetworkCallback extends ConnectivityManager.NetworkCallback { public static class TestNetworkCallback extends ConnectivityManager.NetworkCallback {
private final CountDownLatch mAvailableLatch = new CountDownLatch(1); private final ConditionVariable mAvailableCv = new ConditionVariable(false);
private final CountDownLatch mLostLatch = new CountDownLatch(1); private final CountDownLatch mLostLatch = new CountDownLatch(1);
private final CountDownLatch mUnavailableLatch = new CountDownLatch(1); private final CountDownLatch mUnavailableLatch = new CountDownLatch(1);
public Network currentNetwork; public Network currentNetwork;
public Network lastLostNetwork; public Network lastLostNetwork;
/**
* Wait for a network to be available.
*
* If onAvailable was previously called but was followed by onLost, this will wait for the
* next available network.
*/
public Network waitForAvailable() throws InterruptedException { public Network waitForAvailable() throws InterruptedException {
return mAvailableLatch.await(CONNECTIVITY_CHANGE_TIMEOUT_SECS, TimeUnit.SECONDS) final long timeoutMs = TimeUnit.SECONDS.toMillis(CONNECTIVITY_CHANGE_TIMEOUT_SECS);
? currentNetwork : null; while (mAvailableCv.block(timeoutMs)) {
final Network n = currentNetwork;
if (n != null) return n;
Log.w(TAG, "onAvailable called but network was lost before it could be returned."
+ " Waiting for the next call to onAvailable.");
}
return null;
} }
public Network waitForLost() throws InterruptedException { public Network waitForLost() throws InterruptedException {
@@ -717,17 +733,17 @@ public final class CtsNetUtils {
return mUnavailableLatch.await(2, TimeUnit.SECONDS); return mUnavailableLatch.await(2, TimeUnit.SECONDS);
} }
@Override @Override
public void onAvailable(Network network) { public void onAvailable(Network network) {
currentNetwork = network; currentNetwork = network;
mAvailableLatch.countDown(); mAvailableCv.open();
} }
@Override @Override
public void onLost(Network network) { public void onLost(Network network) {
lastLostNetwork = network; lastLostNetwork = network;
if (network.equals(currentNetwork)) { if (network.equals(currentNetwork)) {
mAvailableCv.close();
currentNetwork = null; currentNetwork = null;
} }
mLostLatch.countDown(); mLostLatch.countDown();