Search in sources :

Example 56 with NetworkState

use of android.net.NetworkState in project android_frameworks_base by AOSPA.

the class ConnectivityService method getAllNetworkState.

@Override
public NetworkState[] getAllNetworkState() {
    // Require internal since we're handing out IMSI details
    enforceConnectivityInternalPermission();
    final ArrayList<NetworkState> result = Lists.newArrayList();
    for (Network network : getAllNetworks()) {
        final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
        if (nai != null) {
            result.add(nai.getNetworkState());
        }
    }
    return result.toArray(new NetworkState[result.size()]);
}
Also used : NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) Network(android.net.Network) NetworkState(android.net.NetworkState)

Example 57 with NetworkState

use of android.net.NetworkState in project android_frameworks_base by crdroidandroid.

the class ConnectivityService method getNetworkInfo.

@Override
public NetworkInfo getNetworkInfo(int networkType) {
    enforceAccessPermission();
    final int uid = Binder.getCallingUid();
    if (getVpnUnderlyingNetworks(uid) != null) {
        // A VPN is active, so we may need to return one of its underlying networks. This
        // information is not available in LegacyTypeTracker, so we have to get it from
        // getUnfilteredActiveNetworkState.
        final NetworkState state = getUnfilteredActiveNetworkState(uid);
        if (state.networkInfo != null && state.networkInfo.getType() == networkType) {
            filterNetworkStateForUid(state, uid, false);
            return state.networkInfo;
        }
    }
    final NetworkState state = getFilteredNetworkState(networkType, uid, false);
    return state.networkInfo;
}
Also used : NetworkState(android.net.NetworkState)

Example 58 with NetworkState

use of android.net.NetworkState in project android_frameworks_base by crdroidandroid.

the class ConnectivityService method getFilteredNetworkState.

private NetworkState getFilteredNetworkState(int networkType, int uid, boolean ignoreBlocked) {
    if (mLegacyTypeTracker.isTypeSupported(networkType)) {
        final NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
        final NetworkState state;
        if (nai != null) {
            state = nai.getNetworkState();
            state.networkInfo.setType(networkType);
        } else {
            final NetworkInfo info = new NetworkInfo(networkType, 0, getNetworkTypeName(networkType), "");
            info.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null);
            info.setIsAvailable(true);
            state = new NetworkState(info, new LinkProperties(), new NetworkCapabilities(), null, null, null);
        }
        filterNetworkStateForUid(state, uid, ignoreBlocked);
        return state;
    } else {
        return NetworkState.EMPTY;
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) NetworkState(android.net.NetworkState) LinkProperties(android.net.LinkProperties) NetworkCapabilities(android.net.NetworkCapabilities)

Example 59 with NetworkState

use of android.net.NetworkState in project android_frameworks_base by ParanoidAndroid.

the class NetworkPolicyManagerService method updateNetworkRulesLocked.

/**
     * Examine all connected {@link NetworkState}, looking for
     * {@link NetworkPolicy} that need to be enforced. When matches found, set
     * remaining quota based on usage cycle and historical stats.
     */
private void updateNetworkRulesLocked() {
    if (LOGV)
        Slog.v(TAG, "updateIfacesLocked()");
    final NetworkState[] states;
    try {
        states = mConnManager.getAllNetworkState();
    } catch (RemoteException e) {
        // ignored; service lives in system_server
        return;
    }
    // first, derive identity for all connected networks, which can be used
    // to match against templates.
    final HashMap<NetworkIdentity, String> networks = Maps.newHashMap();
    for (NetworkState state : states) {
        // stash identity and iface away for later use
        if (state.networkInfo.isConnected()) {
            final String iface = state.linkProperties.getInterfaceName();
            final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state);
            networks.put(ident, iface);
        }
    }
    // build list of rules and ifaces to enforce them against
    mNetworkRules.clear();
    final ArrayList<String> ifaceList = Lists.newArrayList();
    for (NetworkPolicy policy : mNetworkPolicy.values()) {
        // collect all active ifaces that match this template
        ifaceList.clear();
        for (Map.Entry<NetworkIdentity, String> entry : networks.entrySet()) {
            final NetworkIdentity ident = entry.getKey();
            if (policy.template.matches(ident)) {
                final String iface = entry.getValue();
                ifaceList.add(iface);
            }
        }
        if (ifaceList.size() > 0) {
            final String[] ifaces = ifaceList.toArray(new String[ifaceList.size()]);
            mNetworkRules.put(policy, ifaces);
        }
    }
    long lowestRule = Long.MAX_VALUE;
    final HashSet<String> newMeteredIfaces = Sets.newHashSet();
    // apply each policy that we found ifaces for; compute remaining data
    // based on current cycle and historical stats, and push to kernel.
    final long currentTime = currentTimeMillis();
    for (NetworkPolicy policy : mNetworkRules.keySet()) {
        final String[] ifaces = mNetworkRules.get(policy);
        final long start;
        final long totalBytes;
        if (policy.hasCycle()) {
            start = computeLastCycleBoundary(currentTime, policy);
            totalBytes = getTotalBytes(policy.template, start, currentTime);
        } else {
            start = Long.MAX_VALUE;
            totalBytes = 0;
        }
        if (LOGD) {
            Slog.d(TAG, "applying policy " + policy.toString() + " to ifaces " + Arrays.toString(ifaces));
        }
        final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED;
        final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED;
        if (hasLimit || policy.metered) {
            final long quotaBytes;
            if (!hasLimit) {
                // metered network, but no policy limit; we still need to
                // restrict apps, so push really high quota.
                quotaBytes = Long.MAX_VALUE;
            } else if (policy.lastLimitSnooze >= start) {
                // snoozing past quota, but we still need to restrict apps,
                // so push really high quota.
                quotaBytes = Long.MAX_VALUE;
            } else {
                // remaining "quota" bytes are based on total usage in
                // current cycle. kernel doesn't like 0-byte rules, so we
                // set 1-byte quota and disable the radio later.
                quotaBytes = Math.max(1, policy.limitBytes - totalBytes);
            }
            if (ifaces.length > 1) {
                // TODO: switch to shared quota once NMS supports
                Slog.w(TAG, "shared quota unsupported; generating rule for each iface");
            }
            for (String iface : ifaces) {
                removeInterfaceQuota(iface);
                setInterfaceQuota(iface, quotaBytes);
                newMeteredIfaces.add(iface);
            }
        }
        // keep track of lowest warning or limit of active policies
        if (hasWarning && policy.warningBytes < lowestRule) {
            lowestRule = policy.warningBytes;
        }
        if (hasLimit && policy.limitBytes < lowestRule) {
            lowestRule = policy.limitBytes;
        }
    }
    mHandler.obtainMessage(MSG_ADVISE_PERSIST_THRESHOLD, lowestRule).sendToTarget();
    // remove quota on any trailing interfaces
    for (String iface : mMeteredIfaces) {
        if (!newMeteredIfaces.contains(iface)) {
            removeInterfaceQuota(iface);
        }
    }
    mMeteredIfaces = newMeteredIfaces;
    final String[] meteredIfaces = mMeteredIfaces.toArray(new String[mMeteredIfaces.size()]);
    mHandler.obtainMessage(MSG_METERED_IFACES_CHANGED, meteredIfaces).sendToTarget();
}
Also used : NetworkIdentity(android.net.NetworkIdentity) NetworkPolicy(android.net.NetworkPolicy) NetworkState(android.net.NetworkState) RemoteException(android.os.RemoteException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 60 with NetworkState

use of android.net.NetworkState in project android_frameworks_base by ParanoidAndroid.

the class NetworkStatsService method updateIfacesLocked.

/**
     * Inspect all current {@link NetworkState} to derive mapping from {@code
     * iface} to {@link NetworkStatsHistory}. When multiple {@link NetworkInfo}
     * are active on a single {@code iface}, they are combined under a single
     * {@link NetworkIdentitySet}.
     */
private void updateIfacesLocked() {
    if (!mSystemReady)
        return;
    if (LOGV)
        Slog.v(TAG, "updateIfacesLocked()");
    // take one last stats snapshot before updating iface mapping. this
    // isn't perfect, since the kernel may already be counting traffic from
    // the updated network.
    // poll, but only persist network stats to keep codepath fast. UID stats
    // will be persisted during next alarm poll event.
    performPollLocked(FLAG_PERSIST_NETWORK);
    final NetworkState[] states;
    final LinkProperties activeLink;
    try {
        states = mConnManager.getAllNetworkState();
        activeLink = mConnManager.getActiveLinkProperties();
    } catch (RemoteException e) {
        // ignored; service lives in system_server
        return;
    }
    mActiveIface = activeLink != null ? activeLink.getInterfaceName() : null;
    // rebuild active interfaces based on connected networks
    mActiveIfaces.clear();
    for (NetworkState state : states) {
        if (state.networkInfo.isConnected()) {
            // collect networks under their parent interfaces
            final String iface = state.linkProperties.getInterfaceName();
            NetworkIdentitySet ident = mActiveIfaces.get(iface);
            if (ident == null) {
                ident = new NetworkIdentitySet();
                mActiveIfaces.put(iface, ident);
            }
            ident.add(NetworkIdentity.buildNetworkIdentity(mContext, state));
            // remember any ifaces associated with mobile networks
            if (isNetworkTypeMobile(state.networkInfo.getType()) && iface != null) {
                if (!contains(mMobileIfaces, iface)) {
                    mMobileIfaces = appendElement(String.class, mMobileIfaces, iface);
                }
            }
        }
    }
}
Also used : NetworkState(android.net.NetworkState) RemoteException(android.os.RemoteException) LinkProperties(android.net.LinkProperties)

Aggregations

NetworkState (android.net.NetworkState)101 LinkProperties (android.net.LinkProperties)36 NetworkInfo (android.net.NetworkInfo)26 NetworkPolicy (android.net.NetworkPolicy)21 RemoteException (android.os.RemoteException)18 NetworkStats (android.net.NetworkStats)15 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)15 NetworkIdentity (android.net.NetworkIdentity)11 Intent (android.content.Intent)10 ArraySet (android.util.ArraySet)10 Suppress (android.test.suitebuilder.annotation.Suppress)9 NetworkCapabilities (android.net.NetworkCapabilities)8 Network (android.net.Network)5 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)5 Pair (android.util.Pair)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)3 Matchers.anyString (org.mockito.Matchers.anyString)2 NetworkStateTracker (android.net.NetworkStateTracker)1 HashMap (java.util.HashMap)1