Search in sources :

Example 51 with NetworkState

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

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();
    mActiveUidIfaces.clear();
    final ArraySet<String> mobileIfaces = new ArraySet<>();
    for (NetworkState state : states) {
        if (state.networkInfo.isConnected() && (state.networkCapabilities == null || !state.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || state.networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) || hasImsNetworkCapability(state))) {
            final boolean isMobile = isNetworkTypeMobile(state.networkInfo.getType());
            final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state);
            // Traffic occurring on the base interface is always counted for
            // both total usage and UID details.
            final String baseIface = state.linkProperties.getInterfaceName();
            if (baseIface != null) {
                findOrCreateNetworkIdentitySet(mActiveIfaces, baseIface).add(ident);
                findOrCreateNetworkIdentitySet(mActiveUidIfaces, baseIface).add(ident);
                // per carrier's policy, modem will report 0 usage for VT calls.
                if (state.networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS) && !ident.getMetered()) {
                    // Copy the identify from IMS one but mark it as metered.
                    NetworkIdentity vtIdent = new NetworkIdentity(ident.getType(), ident.getSubType(), ident.getSubscriberId(), ident.getNetworkId(), ident.getRoaming(), true);
                    findOrCreateNetworkIdentitySet(mActiveIfaces, VT_INTERFACE).add(vtIdent);
                    findOrCreateNetworkIdentitySet(mActiveUidIfaces, VT_INTERFACE).add(vtIdent);
                }
                if (isMobile) {
                    mobileIfaces.add(baseIface);
                }
            }
            // Traffic occurring on stacked interfaces is usually clatd,
            // which is already accounted against its final egress interface
            // by the kernel. Thus, we only need to collect stacked
            // interface stats at the UID level.
            final List<LinkProperties> stackedLinks = state.linkProperties.getStackedLinks();
            for (LinkProperties stackedLink : stackedLinks) {
                final String stackedIface = stackedLink.getInterfaceName();
                if (stackedIface != null) {
                    findOrCreateNetworkIdentitySet(mActiveUidIfaces, stackedIface).add(ident);
                    if (isMobile) {
                        mobileIfaces.add(stackedIface);
                    }
                }
            }
        }
    }
    mMobileIfaces = mobileIfaces.toArray(new String[mobileIfaces.size()]);
}
Also used : ArraySet(android.util.ArraySet) NetworkIdentity(android.net.NetworkIdentity) NetworkState(android.net.NetworkState) RemoteException(android.os.RemoteException) LinkProperties(android.net.LinkProperties)

Example 52 with NetworkState

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

the class NetworkStatsServiceTest method buildMobile3gState.

private static NetworkState buildMobile3gState(String subscriberId, boolean isRoaming) {
    final NetworkInfo info = new NetworkInfo(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
    info.setDetailedState(DetailedState.CONNECTED, null, null);
    info.setRoaming(isRoaming);
    final LinkProperties prop = new LinkProperties();
    prop.setInterfaceName(TEST_IFACE);
    return new NetworkState(info, prop, null, null, subscriberId, null);
}
Also used : NetworkInfo(android.net.NetworkInfo) NetworkState(android.net.NetworkState) LinkProperties(android.net.LinkProperties)

Example 53 with NetworkState

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

the class NetworkStatsServiceTest method buildMobile4gState.

private static NetworkState buildMobile4gState(String iface) {
    final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
    info.setDetailedState(DetailedState.CONNECTED, null, null);
    final LinkProperties prop = new LinkProperties();
    prop.setInterfaceName(iface);
    return new NetworkState(info, prop, null, null, null, null);
}
Also used : NetworkInfo(android.net.NetworkInfo) NetworkState(android.net.NetworkState) LinkProperties(android.net.LinkProperties)

Example 54 with NetworkState

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

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 55 with NetworkState

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

the class ConnectivityService method getActiveNetworkInfoUnfiltered.

// Public because it's used by mLockdownTracker.
public NetworkInfo getActiveNetworkInfoUnfiltered() {
    enforceAccessPermission();
    final int uid = Binder.getCallingUid();
    NetworkState state = getUnfilteredActiveNetworkState(uid);
    return state.networkInfo;
}
Also used : NetworkState(android.net.NetworkState)

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