Search in sources :

Example 31 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method testRequestCallbackUpdates.

@LargeTest
public void testRequestCallbackUpdates() throws Exception {
    // File a network request for mobile.
    final TestNetworkCallback cellNetworkCallback = new TestRequestUpdateCallback();
    final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    mCm.requestNetwork(cellRequest, cellNetworkCallback);
    // Bring up the mobile network.
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    // We should get onAvailable().
    cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    // We should get onCapabilitiesChanged(), when the mobile network successfully validates.
    cellNetworkCallback.expectCallback(CallbackState.NETWORK_CAPABILITIES, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    // Update LinkProperties.
    final LinkProperties lp = new LinkProperties();
    lp.setInterfaceName("foonet_data0");
    mCellNetworkAgent.sendLinkProperties(lp);
    // We should get onLinkPropertiesChanged().
    cellNetworkCallback.expectCallback(CallbackState.LINK_PROPERTIES, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    // Register a garden variety default network request.
    final TestNetworkCallback dfltNetworkCallback = new TestRequestUpdateCallback();
    mCm.registerDefaultNetworkCallback(dfltNetworkCallback);
    // Only onAvailable() is called; no other information is delivered.
    dfltNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    dfltNetworkCallback.assertNoCallback();
    // Request a NetworkCapabilities update; only the requesting callback is notified.
    mCm.requestNetworkCapabilities(dfltNetworkCallback);
    dfltNetworkCallback.expectCallback(CallbackState.NETWORK_CAPABILITIES, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    dfltNetworkCallback.assertNoCallback();
    // Request a LinkProperties update; only the requesting callback is notified.
    mCm.requestLinkProperties(dfltNetworkCallback);
    dfltNetworkCallback.expectCallback(CallbackState.LINK_PROPERTIES, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    dfltNetworkCallback.assertNoCallback();
    mCm.unregisterNetworkCallback(dfltNetworkCallback);
    mCm.unregisterNetworkCallback(cellNetworkCallback);
}
Also used : NetworkRequest(android.net.NetworkRequest) LinkProperties(android.net.LinkProperties) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 32 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityService method createInternetRequestForTransport.

private NetworkRequest createInternetRequestForTransport(int transportType, NetworkRequest.Type type) {
    NetworkCapabilities netCap = new NetworkCapabilities();
    netCap.addCapability(NET_CAPABILITY_INTERNET);
    netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED);
    if (transportType > -1) {
        netCap.addTransportType(transportType);
    }
    return new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId(), type);
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities)

Example 33 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityService method pendingListenForNetwork.

@Override
public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, PendingIntent operation) {
    checkNotNull(operation, "PendingIntent cannot be null.");
    if (!hasWifiNetworkListenPermission(networkCapabilities)) {
        enforceAccessPermission();
    }
    NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities(networkCapabilities), TYPE_NONE, nextNetworkRequestId(), NetworkRequest.Type.LISTEN);
    NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation);
    if (VDBG)
        log("pendingListenForNetwork for " + nri);
    mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri));
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities)

Example 34 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityService method listenForNetwork.

@Override
public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, Messenger messenger, IBinder binder) {
    if (!hasWifiNetworkListenPermission(networkCapabilities)) {
        enforceAccessPermission();
    }
    NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities);
    if (!ConnectivityManager.checkChangePermission(mContext)) {
        // Apps without the CHANGE_NETWORK_STATE permission can't use background networks, so
        // make all their listens include NET_CAPABILITY_FOREGROUND. That way, they will get
        // onLost and onAvailable callbacks when networks move in and out of the background.
        // There is no need to do this for requests because an app without CHANGE_NETWORK_STATE
        // can't request networks.
        nc.addCapability(NET_CAPABILITY_FOREGROUND);
    }
    NetworkRequest networkRequest = new NetworkRequest(nc, TYPE_NONE, nextNetworkRequestId(), NetworkRequest.Type.LISTEN);
    NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder);
    if (VDBG)
        log("listenForNetwork for " + nri);
    mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri));
    return networkRequest;
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities)

Example 35 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityService method teardownUnneededNetwork.

private void teardownUnneededNetwork(NetworkAgentInfo nai) {
    if (nai.numRequestNetworkRequests() != 0) {
        for (int i = 0; i < nai.numNetworkRequests(); i++) {
            NetworkRequest nr = nai.requestAt(i);
            // Ignore listening requests.
            if (nr.isListen())
                continue;
            loge("Dead network still had at least " + nr);
            break;
        }
    }
    nai.asyncChannel.disconnect();
}
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