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:
@@ -54,6 +54,7 @@ import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
import android.os.ConditionVariable;
|
||||
import android.os.IBinder;
|
||||
import android.os.SystemClock;
|
||||
import android.system.Os;
|
||||
@@ -264,6 +265,9 @@ public final class CtsNetUtils {
|
||||
Log.w(TAG, "connect failed with " + error + "; waiting before retry");
|
||||
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 {
|
||||
@@ -696,16 +700,28 @@ public final class CtsNetUtils {
|
||||
* {@code onAvailable}.
|
||||
*/
|
||||
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 mUnavailableLatch = new CountDownLatch(1);
|
||||
|
||||
public Network currentNetwork;
|
||||
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 {
|
||||
return mAvailableLatch.await(CONNECTIVITY_CHANGE_TIMEOUT_SECS, TimeUnit.SECONDS)
|
||||
? currentNetwork : null;
|
||||
final long timeoutMs = TimeUnit.SECONDS.toMillis(CONNECTIVITY_CHANGE_TIMEOUT_SECS);
|
||||
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 {
|
||||
@@ -717,17 +733,17 @@ public final class CtsNetUtils {
|
||||
return mUnavailableLatch.await(2, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onAvailable(Network network) {
|
||||
currentNetwork = network;
|
||||
mAvailableLatch.countDown();
|
||||
mAvailableCv.open();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLost(Network network) {
|
||||
lastLostNetwork = network;
|
||||
if (network.equals(currentNetwork)) {
|
||||
mAvailableCv.close();
|
||||
currentNetwork = null;
|
||||
}
|
||||
mLostLatch.countDown();
|
||||
|
||||
Reference in New Issue
Block a user