Search in sources :

Example 96 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testTimedoutAfterUnregisteredNetworkRequest.

/**
     * Validate that when a network request is unregistered (cancelled) the time-out for that
     * request doesn't trigger the onUnavailable() callback.
     */
@SmallTest
public void testTimedoutAfterUnregisteredNetworkRequest() {
    NetworkRequest nr = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build();
    final TestNetworkCallback networkCallback = new TestNetworkCallback();
    final int timeoutMs = 10;
    mCm.requestNetwork(nr, networkCallback, timeoutMs);
    // remove request
    mCm.unregisterNetworkCallback(networkCallback);
    // pass timeout and validate that no callbacks
    // Note: doesn't validate that nothing called from CS since even if called the CM already
    // unregisters the callback and won't pass it through!
    sleepFor(15);
    networkCallback.assertNoCallback();
    // create a network satisfying request - validate that request not triggered
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    networkCallback.assertNoCallback();
}
Also used : NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 97 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testMMSonWiFi.

@SmallTest
public void testMMSonWiFi() throws Exception {
    // Test bringing up cellular without MMS NetworkRequest gets reaped
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.addCapability(NET_CAPABILITY_MMS);
    ConditionVariable cv = mCellNetworkAgent.getDisconnectedCV();
    mCellNetworkAgent.connectWithoutInternet();
    waitFor(cv);
    mService.waitForIdle();
    assertEquals(0, mCm.getAllNetworks().length);
    verifyNoNetwork();
    // Test bringing up validated WiFi.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    cv = waitForConnectivityBroadcasts(1);
    mWiFiNetworkAgent.connect(true);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_WIFI);
    // Register MMS NetworkRequest
    NetworkRequest.Builder builder = new NetworkRequest.Builder();
    builder.addCapability(NetworkCapabilities.NET_CAPABILITY_MMS);
    final TestNetworkCallback networkCallback = new TestNetworkCallback();
    mCm.requestNetwork(builder.build(), networkCallback);
    // Test bringing up unvalidated cellular with MMS
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.addCapability(NET_CAPABILITY_MMS);
    mCellNetworkAgent.connectWithoutInternet();
    networkCallback.expectAvailableCallbacks(mCellNetworkAgent);
    verifyActiveNetwork(TRANSPORT_WIFI);
    // Test releasing NetworkRequest disconnects cellular with MMS
    cv = mCellNetworkAgent.getDisconnectedCV();
    mCm.unregisterNetworkCallback(networkCallback);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_WIFI);
}
Also used : ConditionVariable(android.os.ConditionVariable) NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 98 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testRequestBenchmark.

@SmallTest
public void testRequestBenchmark() throws Exception {
    // TODO: turn this unit test into a real benchmarking test.
    // Benchmarks connecting and switching performance in the presence of a large number of
    // NetworkRequests.
    // 1. File NUM_REQUESTS requests.
    // 2. Have a network connect. Wait for NUM_REQUESTS onAvailable callbacks to fire.
    // 3. Have a new network connect and outscore the previous. Wait for NUM_REQUESTS onLosing
    //    and NUM_REQUESTS onAvailable callbacks to fire.
    // See how long it took.
    final int NUM_REQUESTS = 90;
    final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
    final NetworkCallback[] callbacks = new NetworkCallback[NUM_REQUESTS];
    final CountDownLatch availableLatch = new CountDownLatch(NUM_REQUESTS);
    final CountDownLatch losingLatch = new CountDownLatch(NUM_REQUESTS);
    for (int i = 0; i < NUM_REQUESTS; i++) {
        callbacks[i] = new NetworkCallback() {

            @Override
            public void onAvailable(Network n) {
                availableLatch.countDown();
            }

            @Override
            public void onLosing(Network n, int t) {
                losingLatch.countDown();
            }
        };
    }
    final int REGISTER_TIME_LIMIT_MS = 180;
    assertTimeLimit("Registering callbacks", REGISTER_TIME_LIMIT_MS, () -> {
        for (NetworkCallback cb : callbacks) {
            mCm.registerNetworkCallback(request, cb);
        }
    });
    final int CONNECT_TIME_LIMIT_MS = 40;
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Don't request that the network validate, because otherwise connect() will block until
    // the network gets NET_CAPABILITY_VALIDATED, after all the callbacks below have fired,
    // and we won't actually measure anything.
    mCellNetworkAgent.connect(false);
    long onAvailableDispatchingDuration = durationOf(() -> {
        if (!awaitLatch(availableLatch, CONNECT_TIME_LIMIT_MS)) {
            fail(String.format("Only dispatched %d/%d onAvailable callbacks in %dms", NUM_REQUESTS - availableLatch.getCount(), NUM_REQUESTS, CONNECT_TIME_LIMIT_MS));
        }
    });
    Log.d(TAG, String.format("Connect, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, onAvailableDispatchingDuration, CONNECT_TIME_LIMIT_MS));
    final int SWITCH_TIME_LIMIT_MS = 40;
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    // Give wifi a high enough score that we'll linger cell when wifi comes up.
    mWiFiNetworkAgent.adjustScore(40);
    mWiFiNetworkAgent.connect(false);
    long onLostDispatchingDuration = durationOf(() -> {
        if (!awaitLatch(losingLatch, SWITCH_TIME_LIMIT_MS)) {
            fail(String.format("Only dispatched %d/%d onLosing callbacks in %dms", NUM_REQUESTS - losingLatch.getCount(), NUM_REQUESTS, SWITCH_TIME_LIMIT_MS));
        }
    });
    Log.d(TAG, String.format("Linger, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, onLostDispatchingDuration, SWITCH_TIME_LIMIT_MS));
    final int UNREGISTER_TIME_LIMIT_MS = 10;
    assertTimeLimit("Unregistering callbacks", UNREGISTER_TIME_LIMIT_MS, () -> {
        for (NetworkCallback cb : callbacks) {
            mCm.unregisterNetworkCallback(cb);
        }
    });
}
Also used : Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) CountDownLatch(java.util.concurrent.CountDownLatch) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 99 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class WifiManager method enableNetwork.

/**
     * Allow a previously configured network to be associated with. If
     * <code>attemptConnect</code> is true, an attempt to connect to the selected
     * network is initiated. This may result in the asynchronous delivery
     * of state change events.
     * <p>
     * <b>Note:</b> If an application's target SDK version is
     * {@link android.os.Build.VERSION_CODES#LOLLIPOP} or newer, network
     * communication may not use Wi-Fi even if Wi-Fi is connected; traffic may
     * instead be sent through another network, such as cellular data,
     * Bluetooth tethering, or Ethernet. For example, traffic will never use a
     * Wi-Fi network that does not provide Internet access (e.g. a wireless
     * printer), if another network that does offer Internet access (e.g.
     * cellular data) is available. Applications that need to ensure that their
     * network traffic uses Wi-Fi should use APIs such as
     * {@link Network#bindSocket(java.net.Socket)},
     * {@link Network#openConnection(java.net.URL)}, or
     * {@link ConnectivityManager#bindProcessToNetwork} to do so.
     *
     * Applications are not allowed to enable networks created by other
     * applications.
     *
     * @param netId the ID of the network as returned by {@link #addNetwork} or {@link
     *        #getConfiguredNetworks}.
     * @param attemptConnect The way to select a particular network to connect to is specify
     *        {@code true} for this parameter.
     * @return {@code true} if the operation succeeded
     */
public boolean enableNetwork(int netId, boolean attemptConnect) {
    final boolean pin = attemptConnect && mTargetSdkVersion < Build.VERSION_CODES.LOLLIPOP;
    if (pin) {
        NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build();
        NetworkPinner.pin(mContext, request);
    }
    boolean success;
    try {
        success = mService.enableNetwork(netId, attemptConnect);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
    if (pin && !success) {
        NetworkPinner.unpin();
    }
    return success;
}
Also used : NetworkRequest(android.net.NetworkRequest) RemoteException(android.os.RemoteException)

Example 100 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class NetworkAgentInfo method removeRequest.

/**
     * Remove the specified request from this network.
     */
public void removeRequest(int requestId) {
    NetworkRequest existing = mNetworkRequests.get(requestId);
    if (existing == null)
        return;
    updateRequestCounts(REMOVE, existing);
    mNetworkRequests.remove(requestId);
    if (existing.isRequest()) {
        unlingerRequest(existing);
    }
}
Also used : NetworkRequest(android.net.NetworkRequest)

Aggregations

NetworkRequest (android.net.NetworkRequest)158 NetworkCapabilities (android.net.NetworkCapabilities)48 SmallTest (android.test.suitebuilder.annotation.SmallTest)43 ConditionVariable (android.os.ConditionVariable)24 LargeTest (android.test.suitebuilder.annotation.LargeTest)21 RemoteException (android.os.RemoteException)20 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)18 Network (android.net.Network)16 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)15 LinkProperties (android.net.LinkProperties)14 ArrayList (java.util.ArrayList)9 ContentResolver (android.content.ContentResolver)8 HandlerThread (android.os.HandlerThread)8 MockContentResolver (android.test.mock.MockContentResolver)8 ConnectivityManager (android.net.ConnectivityManager)6 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)6 Intent (android.content.Intent)5 Bundle (android.os.Bundle)5 Message (android.os.Message)5 IBatteryStats (com.android.internal.app.IBatteryStats)5