Search in sources :

Example 96 with NetworkCapabilities

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

the class ConnectivityService method getDefaultNetworkCapabilitiesForUser.

@Override
public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
    // The basic principle is: if an app's traffic could possibly go over a
    // network, without the app doing anything multinetwork-specific,
    // (hence, by "default"), then include that network's capabilities in
    // the array.
    //
    // In the normal case, app traffic only goes over the system's default
    // network connection, so that's the only network returned.
    //
    // With a VPN in force, some app traffic may go into the VPN, and thus
    // over whatever underlying networks the VPN specifies, while other app
    // traffic may go over the system default network (e.g.: a split-tunnel
    // VPN, or an app disallowed by the VPN), so the set of networks
    // returned includes the VPN's underlying networks and the system
    // default.
    enforceAccessPermission();
    HashMap<Network, NetworkCapabilities> result = new HashMap<Network, NetworkCapabilities>();
    NetworkAgentInfo nai = getDefaultNetwork();
    NetworkCapabilities nc = getNetworkCapabilitiesInternal(nai);
    if (nc != null) {
        result.put(nai.network, nc);
    }
    if (!mLockdownEnabled) {
        synchronized (mVpns) {
            Vpn vpn = mVpns.get(userId);
            if (vpn != null) {
                Network[] networks = vpn.getUnderlyingNetworks();
                if (networks != null) {
                    for (Network network : networks) {
                        nai = getNetworkAgentInfoForNetwork(network);
                        nc = getNetworkCapabilitiesInternal(nai);
                        if (nc != null) {
                            result.put(network, nc);
                        }
                    }
                }
            }
        }
    }
    NetworkCapabilities[] out = new NetworkCapabilities[result.size()];
    out = result.values().toArray(out);
    return out;
}
Also used : HashMap(java.util.HashMap) Vpn(com.android.server.connectivity.Vpn) NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) Network(android.net.Network) NetworkCapabilities(android.net.NetworkCapabilities)

Example 97 with NetworkCapabilities

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

the class ConnectivityService method updateCapabilities.

/**
     * Update the NetworkCapabilities for {@code networkAgent} to {@code networkCapabilities}
     * augmented with any stateful capabilities implied from {@code networkAgent}
     * (e.g., validated status and captive portal status).
     *
     * @param oldScore score of the network before any of the changes that prompted us
     *                 to call this function.
     * @param nai the network having its capabilities updated.
     * @param networkCapabilities the new network capabilities.
     */
private void updateCapabilities(int oldScore, NetworkAgentInfo nai, NetworkCapabilities networkCapabilities) {
    if (nai.everConnected && !nai.networkCapabilities.equalImmutableCapabilities(networkCapabilities)) {
        Slog.wtf(TAG, "BUG: " + nai + " changed immutable capabilities: " + nai.networkCapabilities + " -> " + networkCapabilities);
    }
    // Don't modify caller's NetworkCapabilities.
    networkCapabilities = new NetworkCapabilities(networkCapabilities);
    if (nai.lastValidated) {
        networkCapabilities.addCapability(NET_CAPABILITY_VALIDATED);
    } else {
        networkCapabilities.removeCapability(NET_CAPABILITY_VALIDATED);
    }
    if (nai.lastCaptivePortalDetected) {
        networkCapabilities.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
    } else {
        networkCapabilities.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
    }
    if (nai.isBackgroundNetwork()) {
        networkCapabilities.removeCapability(NET_CAPABILITY_FOREGROUND);
    } else {
        networkCapabilities.addCapability(NET_CAPABILITY_FOREGROUND);
    }
    if (Objects.equals(nai.networkCapabilities, networkCapabilities))
        return;
    final String oldPermission = getNetworkPermission(nai.networkCapabilities);
    final String newPermission = getNetworkPermission(networkCapabilities);
    if (!Objects.equals(oldPermission, newPermission) && nai.created && !nai.isVPN()) {
        try {
            mNetd.setNetworkPermission(nai.network.netId, newPermission);
        } catch (RemoteException e) {
            loge("Exception in setNetworkPermission: " + e);
        }
    }
    final NetworkCapabilities prevNc = nai.networkCapabilities;
    synchronized (nai) {
        nai.networkCapabilities = networkCapabilities;
    }
    if (nai.getCurrentScore() == oldScore && networkCapabilities.equalRequestableCapabilities(prevNc)) {
        // If the requestable capabilities haven't changed, and the score hasn't changed, then
        // the change we're processing can't affect any requests, it can only affect the listens
        // on this network. We might have been called by rematchNetworkAndRequests when a
        // network changed foreground state.
        processListenRequests(nai, true);
    } else {
        // If the requestable capabilities have changed or the score changed, we can't have been
        // called by rematchNetworkAndRequests, so it's safe to start a rematch.
        rematchAllNetworksAndRequests(nai, oldScore);
        notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED);
    }
}
Also used : NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString) RemoteException(android.os.RemoteException) NetworkCapabilities(android.net.NetworkCapabilities)

Example 98 with NetworkCapabilities

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

the class ConnectivityService method pendingRequestForNetwork.

@Override
public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, PendingIntent operation) {
    checkNotNull(operation, "PendingIntent cannot be null.");
    networkCapabilities = new NetworkCapabilities(networkCapabilities);
    enforceNetworkRequestPermissions(networkCapabilities);
    enforceMeteredApnPolicy(networkCapabilities);
    ensureRequestableCapabilities(networkCapabilities);
    NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, nextNetworkRequestId(), NetworkRequest.Type.REQUEST);
    NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation);
    if (DBG)
        log("pendingRequest for " + nri);
    mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT, nri));
    return networkRequest;
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities)

Example 99 with NetworkCapabilities

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

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 100 with NetworkCapabilities

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

the class LingerMonitorTest method nai.

NetworkAgentInfo nai(int netId, int transport, int networkType, String networkTypeName) {
    NetworkInfo info = new NetworkInfo(networkType, 0, networkTypeName, "");
    NetworkCapabilities caps = new NetworkCapabilities();
    caps.addCapability(0);
    caps.addTransportType(transport);
    NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null, caps, 50, mCtx, null, mMisc, null, mConnService);
    nai.everValidated = true;
    return nai;
}
Also used : NetworkInfo(android.net.NetworkInfo) Network(android.net.Network) NetworkCapabilities(android.net.NetworkCapabilities)

Aggregations

NetworkCapabilities (android.net.NetworkCapabilities)114 NetworkRequest (android.net.NetworkRequest)53 Network (android.net.Network)27 RemoteException (android.os.RemoteException)25 LinkProperties (android.net.LinkProperties)23 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)20 NetworkInfo (android.net.NetworkInfo)18 ConnectivityManager (android.net.ConnectivityManager)15 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)15 SmallTest (android.test.suitebuilder.annotation.SmallTest)12 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)10 NetworkState (android.net.NetworkState)8 HandlerThread (android.os.HandlerThread)8 NetworkMisc (android.net.NetworkMisc)5 IWifiManager (android.net.wifi.IWifiManager)5 WifiManager (android.net.wifi.WifiManager)5 Bundle (android.os.Bundle)5 Message (android.os.Message)5 ServiceState (android.telephony.ServiceState)5 SignalStrength (android.telephony.SignalStrength)5