Search in sources :

Example 21 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 22 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 23 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 24 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)

Example 25 with NetworkRequest

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

the class ConnectivityService method handleReleaseNetworkRequest.

private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid) {
    final NetworkRequestInfo nri = getNriForAppRequest(request, callingUid, "release NetworkRequest");
    if (nri == null)
        return;
    if (VDBG || (DBG && nri.request.isRequest()))
        log("releasing " + request);
    nri.unlinkDeathRecipient();
    mNetworkRequests.remove(request);
    synchronized (mUidToNetworkRequestCount) {
        int requests = mUidToNetworkRequestCount.get(nri.mUid, 0);
        if (requests < 1) {
            Slog.wtf(TAG, "BUG: too small request count " + requests + " for UID " + nri.mUid);
        } else if (requests == 1) {
            mUidToNetworkRequestCount.removeAt(mUidToNetworkRequestCount.indexOfKey(nri.mUid));
        } else {
            mUidToNetworkRequestCount.put(nri.mUid, requests - 1);
        }
    }
    mNetworkRequestInfoLogs.log("RELEASE " + nri);
    if (nri.request.isRequest()) {
        boolean wasKept = false;
        NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId);
        if (nai != null) {
            boolean wasBackgroundNetwork = nai.isBackgroundNetwork();
            nai.removeRequest(nri.request.requestId);
            if (VDBG) {
                log(" Removing from current network " + nai.name() + ", leaving " + nai.numNetworkRequests() + " requests.");
            }
            // If there are still lingered requests on this network, don't tear it down,
            // but resume lingering instead.
            updateLingerState(nai, SystemClock.elapsedRealtime());
            if (unneeded(nai, UnneededFor.TEARDOWN)) {
                if (DBG)
                    log("no live requests for " + nai.name() + "; disconnecting");
                teardownUnneededNetwork(nai);
            } else {
                wasKept = true;
            }
            mNetworkForRequestId.remove(nri.request.requestId);
            if (!wasBackgroundNetwork && nai.isBackgroundNetwork()) {
                // Went from foreground to background.
                updateCapabilities(nai.getCurrentScore(), nai, nai.networkCapabilities);
            }
        }
        // network satisfying it, so this loop is wasteful
        for (NetworkAgentInfo otherNai : mNetworkAgentInfos.values()) {
            if (otherNai.isSatisfyingRequest(nri.request.requestId) && otherNai != nai) {
                Slog.wtf(TAG, "Request " + nri.request + " satisfied by " + otherNai.name() + ", but mNetworkAgentInfos says " + (nai != null ? nai.name() : "null"));
            }
        }
        // phantom disconnect for this type.
        if (nri.request.legacyType != TYPE_NONE && nai != null) {
            boolean doRemove = true;
            if (wasKept) {
                // same legacy type - if so, don't remove the nai
                for (int i = 0; i < nai.numNetworkRequests(); i++) {
                    NetworkRequest otherRequest = nai.requestAt(i);
                    if (otherRequest.legacyType == nri.request.legacyType && otherRequest.isRequest()) {
                        if (DBG)
                            log(" still have other legacy request - leaving");
                        doRemove = false;
                    }
                }
            }
            if (doRemove) {
                mLegacyTypeTracker.remove(nri.request.legacyType, nai, false);
            }
        }
        for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) {
            nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST, nri.request);
        }
    } else {
        // if this listen request applies and remove it.
        for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
            nai.removeRequest(nri.request.requestId);
            if (nri.request.networkCapabilities.hasSignalStrength() && nai.satisfiesImmutableCapabilitiesOf(nri.request)) {
                updateSignalStrengthThresholds(nai, "RELEASE", nri.request);
            }
        }
    }
    callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED, 0);
}
Also used : NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) 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