Search in sources :

Example 11 with NetworkRequest

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

the class GnssLocationProvider method handleRequestSuplConnection.

private void handleRequestSuplConnection(InetAddress address) {
    if (DEBUG) {
        String message = String.format("requestSuplConnection, state=%s, address=%s", agpsDataConnStateAsString(), address);
        Log.d(TAG, message);
    }
    if (mAGpsDataConnectionState != AGPS_DATA_CONNECTION_CLOSED) {
        return;
    }
    mAGpsDataConnectionIpAddr = address;
    mAGpsDataConnectionState = AGPS_DATA_CONNECTION_OPENING;
    NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
    requestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
    requestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_SUPL);
    NetworkRequest request = requestBuilder.build();
    mConnMgr.requestNetwork(request, mSuplConnectivityCallback);
}
Also used : NetworkRequest(android.net.NetworkRequest)

Example 12 with NetworkRequest

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

the class ConnectivityServiceTest method testSatisfiedNetworkRequestDoesNotTriggerOnUnavailable.

/**
     * Validate that a satisfied network request does not trigger onUnavailable() once the
     * time-out period expires.
     */
@SmallTest
public void testSatisfiedNetworkRequestDoesNotTriggerOnUnavailable() {
    NetworkRequest nr = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build();
    final TestNetworkCallback networkCallback = new TestNetworkCallback();
    final int timeoutMs = 150;
    mCm.requestNetwork(nr, networkCallback, timeoutMs);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    networkCallback.expectAvailableCallbacks(mWiFiNetworkAgent, false, timeoutMs);
    // pass timeout and validate that UNAVAILABLE is not called
    networkCallback.assertNoCallback();
}
Also used : NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 13 with NetworkRequest

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

the class ConnectivityServiceTest method testNetworkRequestMaximum.

@SmallTest
public void testNetworkRequestMaximum() {
    final int MAX_REQUESTS = 100;
    // Test that the limit is enforced when MAX_REQUESTS simultaneous requests are added.
    NetworkRequest networkRequest = new NetworkRequest.Builder().build();
    ArrayList<NetworkCallback> networkCallbacks = new ArrayList<NetworkCallback>();
    try {
        for (int i = 0; i < MAX_REQUESTS; i++) {
            NetworkCallback networkCallback = new NetworkCallback();
            mCm.requestNetwork(networkRequest, networkCallback);
            networkCallbacks.add(networkCallback);
        }
        fail("Registering " + MAX_REQUESTS + " NetworkRequests did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (NetworkCallback networkCallback : networkCallbacks) {
        mCm.unregisterNetworkCallback(networkCallback);
    }
    networkCallbacks.clear();
    try {
        for (int i = 0; i < MAX_REQUESTS; i++) {
            NetworkCallback networkCallback = new NetworkCallback();
            mCm.registerNetworkCallback(networkRequest, networkCallback);
            networkCallbacks.add(networkCallback);
        }
        fail("Registering " + MAX_REQUESTS + " NetworkCallbacks did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (NetworkCallback networkCallback : networkCallbacks) {
        mCm.unregisterNetworkCallback(networkCallback);
    }
    networkCallbacks.clear();
    ArrayList<PendingIntent> pendingIntents = new ArrayList<PendingIntent>();
    try {
        for (int i = 0; i < MAX_REQUESTS + 1; i++) {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a" + i), 0);
            mCm.requestNetwork(networkRequest, pendingIntent);
            pendingIntents.add(pendingIntent);
        }
        fail("Registering " + MAX_REQUESTS + " PendingIntent NetworkRequests did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (PendingIntent pendingIntent : pendingIntents) {
        mCm.unregisterNetworkCallback(pendingIntent);
    }
    pendingIntents.clear();
    try {
        for (int i = 0; i < MAX_REQUESTS + 1; i++) {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a" + i), 0);
            mCm.registerNetworkCallback(networkRequest, pendingIntent);
            pendingIntents.add(pendingIntent);
        }
        fail("Registering " + MAX_REQUESTS + " PendingIntent NetworkCallbacks did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (PendingIntent pendingIntent : pendingIntents) {
        mCm.unregisterNetworkCallback(pendingIntent);
    }
    pendingIntents.clear();
    mService.waitForIdle(5000);
    // Test that the limit is not hit when MAX_REQUESTS requests are added and removed.
    for (int i = 0; i < MAX_REQUESTS; i++) {
        NetworkCallback networkCallback = new NetworkCallback();
        mCm.requestNetwork(networkRequest, networkCallback);
        mCm.unregisterNetworkCallback(networkCallback);
    }
    mService.waitForIdle();
    for (int i = 0; i < MAX_REQUESTS; i++) {
        NetworkCallback networkCallback = new NetworkCallback();
        mCm.registerNetworkCallback(networkRequest, networkCallback);
        mCm.unregisterNetworkCallback(networkCallback);
    }
    mService.waitForIdle();
    for (int i = 0; i < MAX_REQUESTS; i++) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("b" + i), 0);
        mCm.requestNetwork(networkRequest, pendingIntent);
        mCm.unregisterNetworkCallback(pendingIntent);
    }
    mService.waitForIdle();
    for (int i = 0; i < MAX_REQUESTS; i++) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("c" + i), 0);
        mCm.registerNetworkCallback(networkRequest, pendingIntent);
        mCm.unregisterNetworkCallback(pendingIntent);
    }
}
Also used : NetworkRequest(android.net.NetworkRequest) ArrayList(java.util.ArrayList) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 14 with NetworkRequest

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

the class ConnectivityServiceTest method testSatisfiedThenLostNetworkRequestDoesNotTriggerOnUnavailable.

/**
     * Validate that a satisfied network request followed by a disconnected (lost) network does
     * not trigger onUnavailable() once the time-out period expires.
     */
@SmallTest
public void testSatisfiedThenLostNetworkRequestDoesNotTriggerOnUnavailable() {
    NetworkRequest nr = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build();
    final TestNetworkCallback networkCallback = new TestNetworkCallback();
    final int requestTimeoutMs = 100;
    mCm.requestNetwork(nr, networkCallback, requestTimeoutMs);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    final int assertTimeoutMs = 150;
    networkCallback.expectAvailableCallbacks(mWiFiNetworkAgent, false, assertTimeoutMs);
    sleepFor(20);
    mWiFiNetworkAgent.disconnect();
    networkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    // pass timeout and validate that UNAVAILABLE is not called
    sleepFor(100);
    networkCallback.assertNoCallback();
}
Also used : NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 15 with NetworkRequest

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

the class ConnectivityServiceTest method testCaptivePortal.

@SmallTest
public void testCaptivePortal() {
    final TestNetworkCallback captivePortalCallback = new TestNetworkCallback();
    final NetworkRequest captivePortalRequest = new NetworkRequest.Builder().addCapability(NET_CAPABILITY_CAPTIVE_PORTAL).build();
    mCm.registerNetworkCallback(captivePortalRequest, captivePortalCallback);
    final TestNetworkCallback validatedCallback = new TestNetworkCallback();
    final NetworkRequest validatedRequest = new NetworkRequest.Builder().addCapability(NET_CAPABILITY_VALIDATED).build();
    mCm.registerNetworkCallback(validatedRequest, validatedCallback);
    // Bring up a network with a captive portal.
    // Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    String firstRedirectUrl = "http://example.com/firstPath";
    mWiFiNetworkAgent.connectWithCaptivePortal(firstRedirectUrl);
    captivePortalCallback.expectAvailableCallbacks(mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), firstRedirectUrl);
    // Take down network.
    // Expect onLost callback.
    mWiFiNetworkAgent.disconnect();
    captivePortalCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    // Bring up a network with a captive portal.
    // Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    String secondRedirectUrl = "http://example.com/secondPath";
    mWiFiNetworkAgent.connectWithCaptivePortal(secondRedirectUrl);
    captivePortalCallback.expectAvailableCallbacks(mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), secondRedirectUrl);
    // Make captive portal disappear then revalidate.
    // Expect onLost callback because network no longer provides NET_CAPABILITY_CAPTIVE_PORTAL.
    mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 204;
    mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
    captivePortalCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    // Expect NET_CAPABILITY_VALIDATED onAvailable callback.
    validatedCallback.expectAvailableCallbacks(mWiFiNetworkAgent);
    // TODO: Investigate only sending available callbacks.
    validatedCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
    // Break network connectivity.
    // Expect NET_CAPABILITY_VALIDATED onLost callback.
    mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 500;
    mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
    validatedCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
}
Also used : NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

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