Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f0ffb60c authored by Chris Weir's avatar Chris Weir
Browse files

Add support for tracking PANS data usage

Adds support for tracking oemManagedNetwork types (OEM_PAID and
OEM_PRIVATE).

Bug: 174485293
Test: atest NetworkTemplateTest NetworkStatsCollectionTest
NetworkStatsObserversTest NetworkStatsServiceTest NetworkIdentityTest
Test: verify that the bytes go up in PansTest app only when
`ping -I eth2 8.8.8.8` is run as root.

Change-Id: I78660a835f16265ba8e44bb84332c6633dd72b84
parent 9379af7c
Loading
Loading
Loading
Loading
+53 −4
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.net;

import static android.net.ConnectivityManager.TYPE_WIFI;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.net.wifi.WifiInfo;
@@ -41,6 +42,22 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {

    public static final int SUBTYPE_COMBINED = -1;

    /**
     * Network has no {@code NetworkCapabilities#NET_CAPABILITY_OEM_*}.
     * @hide
     */
    public static final int OEM_NONE = 0x0;
    /**
     * Network has {@link NetworkCapabilities#NET_CAPABILITY_OEM_PAID}.
     * @hide
     */
    public static final int OEM_PAID = 0x1;
    /**
     * Network has {@link NetworkCapabilities#NET_CAPABILITY_OEM_PRIVATE}.
     * @hide
     */
    public static final int OEM_PRIVATE = 0x2;

    final int mType;
    final int mSubType;
    final String mSubscriberId;
@@ -48,10 +65,11 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
    final boolean mRoaming;
    final boolean mMetered;
    final boolean mDefaultNetwork;
    final int mOemManaged;

    public NetworkIdentity(
            int type, int subType, String subscriberId, String networkId, boolean roaming,
            boolean metered, boolean defaultNetwork) {
            boolean metered, boolean defaultNetwork, int oemManaged) {
        mType = type;
        mSubType = subType;
        mSubscriberId = subscriberId;
@@ -59,12 +77,13 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        mRoaming = roaming;
        mMetered = metered;
        mDefaultNetwork = defaultNetwork;
        mOemManaged = oemManaged;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming, mMetered,
                mDefaultNetwork);
                mDefaultNetwork, mOemManaged);
    }

    @Override
@@ -75,7 +94,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
                    && Objects.equals(mSubscriberId, ident.mSubscriberId)
                    && Objects.equals(mNetworkId, ident.mNetworkId)
                    && mMetered == ident.mMetered
                    && mDefaultNetwork == ident.mDefaultNetwork;
                    && mDefaultNetwork == ident.mDefaultNetwork
                    && mOemManaged == ident.mOemManaged;
        }
        return false;
    }
@@ -102,6 +122,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        }
        builder.append(", metered=").append(mMetered);
        builder.append(", defaultNetwork=").append(mDefaultNetwork);
        // TODO(180557699): Print a human readable string for OEM managed state.
        builder.append(", oemManaged=").append(mOemManaged);
        return builder.append("}").toString();
    }

@@ -120,6 +142,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        proto.write(NetworkIdentityProto.ROAMING, mRoaming);
        proto.write(NetworkIdentityProto.METERED, mMetered);
        proto.write(NetworkIdentityProto.DEFAULT_NETWORK, mDefaultNetwork);
        proto.write(NetworkIdentityProto.OEM_MANAGED_NETWORK, mOemManaged);

        proto.end(start);
    }
@@ -152,6 +175,10 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        return mDefaultNetwork;
    }

    public int getOemManaged() {
        return mOemManaged;
    }

    /**
     * Build a {@link NetworkIdentity} from the given {@link NetworkState} and {@code subType},
     * assuming that any mobile networks are using the current IMSI. The subType if applicable,
@@ -171,6 +198,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {

        subscriberId = state.subscriberId;

        final int oemManaged = getOemBitfield(state.networkCapabilities);

        if (legacyType == TYPE_WIFI) {
            if (state.networkCapabilities.getSsid() != null) {
                networkId = state.networkCapabilities.getSsid();
@@ -185,7 +214,24 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        }

        return new NetworkIdentity(legacyType, subType, subscriberId, networkId, roaming, metered,
                defaultNetwork);
                defaultNetwork, oemManaged);
    }

    /**
     * Builds a bitfield of {@code NetworkIdentity.OEM_*} based on {@link NetworkCapabilities}.
     * @hide
     */
    public static int getOemBitfield(NetworkCapabilities nc) {
        int oemManaged = OEM_NONE;

        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID)) {
            oemManaged |= OEM_PAID;
        }
        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE)) {
            oemManaged |= OEM_PRIVATE;
        }

        return oemManaged;
    }

    @Override
@@ -209,6 +255,9 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        if (res == 0) {
            res = Boolean.compare(mDefaultNetwork, another.mDefaultNetwork);
        }
        if (res == 0) {
            res = Integer.compare(mOemManaged, another.mOemManaged);
        }
        return res;
    }
}
+42 −6
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static android.net.ConnectivityManager.TYPE_PROXY;
import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.ConnectivityManager.TYPE_WIFI_P2P;
import static android.net.ConnectivityManager.TYPE_WIMAX;
import static android.net.NetworkIdentity.OEM_NONE;
import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
@@ -99,6 +100,22 @@ public class NetworkTemplate implements Parcelable {
     */
    public static final int NETWORK_TYPE_5G_NSA = -2;

    /**
     * Value to match both OEM managed and unmanaged networks (all networks).
     * @hide
     */
    public static final int OEM_MANAGED_ALL = -1;
    /**
     * Value to match networks which are not OEM managed.
     * @hide
     */
    public static final int OEM_MANAGED_NO = OEM_NONE;
    /**
     * Value to match any OEM managed network.
     * @hide
     */
    public static final int OEM_MANAGED_YES = -2;

    private static boolean isKnownMatchRule(final int rule) {
        switch (rule) {
            case MATCH_MOBILE:
@@ -151,10 +168,10 @@ public class NetworkTemplate implements Parcelable {
            @NetworkType int ratType) {
        if (TextUtils.isEmpty(subscriberId)) {
            return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null, null,
                    METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType);
                    METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL);
        }
        return new NetworkTemplate(MATCH_MOBILE, subscriberId, new String[]{subscriberId}, null,
                METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType);
                METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL);
    }

    /**
@@ -235,6 +252,9 @@ public class NetworkTemplate implements Parcelable {
    private final int mDefaultNetwork;
    private final int mSubType;

    // Bitfield containing OEM network properties{@code NetworkIdentity#OEM_*}.
    private final int mOemManaged;

    @UnsupportedAppUsage
    public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
        this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
@@ -243,11 +263,12 @@ public class NetworkTemplate implements Parcelable {
    public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
            String networkId) {
        this(matchRule, subscriberId, matchSubscriberIds, networkId, METERED_ALL, ROAMING_ALL,
                DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL);
                DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL);
    }

    public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
            String networkId, int metered, int roaming, int defaultNetwork, int subType) {
            String networkId, int metered, int roaming, int defaultNetwork, int subType,
            int oemManaged) {
        mMatchRule = matchRule;
        mSubscriberId = subscriberId;
        mMatchSubscriberIds = matchSubscriberIds;
@@ -256,6 +277,7 @@ public class NetworkTemplate implements Parcelable {
        mRoaming = roaming;
        mDefaultNetwork = defaultNetwork;
        mSubType = subType;
        mOemManaged = oemManaged;

        if (!isKnownMatchRule(matchRule)) {
            Log.e(TAG, "Unknown network template rule " + matchRule
@@ -272,6 +294,7 @@ public class NetworkTemplate implements Parcelable {
        mRoaming = in.readInt();
        mDefaultNetwork = in.readInt();
        mSubType = in.readInt();
        mOemManaged = in.readInt();
    }

    @Override
@@ -284,6 +307,7 @@ public class NetworkTemplate implements Parcelable {
        dest.writeInt(mRoaming);
        dest.writeInt(mDefaultNetwork);
        dest.writeInt(mSubType);
        dest.writeInt(mOemManaged);
    }

    @Override
@@ -319,13 +343,16 @@ public class NetworkTemplate implements Parcelable {
        if (mSubType != NETWORK_TYPE_ALL) {
            builder.append(", subType=").append(mSubType);
        }
        if (mOemManaged != OEM_MANAGED_ALL) {
            builder.append(", oemManaged=").append(mOemManaged);
        }
        return builder.toString();
    }

    @Override
    public int hashCode() {
        return Objects.hash(mMatchRule, mSubscriberId, mNetworkId, mMetered, mRoaming,
                mDefaultNetwork, mSubType);
                mDefaultNetwork, mSubType, mOemManaged);
    }

    @Override
@@ -338,7 +365,8 @@ public class NetworkTemplate implements Parcelable {
                    && mMetered == other.mMetered
                    && mRoaming == other.mRoaming
                    && mDefaultNetwork == other.mDefaultNetwork
                    && mSubType == other.mSubType;
                    && mSubType == other.mSubType
                    && mOemManaged == other.mOemManaged;
        }
        return false;
    }
@@ -384,6 +412,7 @@ public class NetworkTemplate implements Parcelable {
        if (!matchesMetered(ident)) return false;
        if (!matchesRoaming(ident)) return false;
        if (!matchesDefaultNetwork(ident)) return false;
        if (!matchesOemNetwork(ident)) return false;

        switch (mMatchRule) {
            case MATCH_MOBILE:
@@ -425,6 +454,13 @@ public class NetworkTemplate implements Parcelable {
            || (mDefaultNetwork == DEFAULT_NETWORK_NO && !ident.mDefaultNetwork);
    }

    private boolean matchesOemNetwork(NetworkIdentity ident) {
        return (mOemManaged == OEM_MANAGED_ALL)
            || (mOemManaged == OEM_MANAGED_YES
                    && ident.mOemManaged != OEM_NONE)
            || (mOemManaged == ident.mOemManaged);
    }

    private boolean matchesCollapsedRatType(NetworkIdentity ident) {
        return mSubType == NETWORK_TYPE_ALL
                || getCollapsedRatType(mSubType) == getCollapsedRatType(ident.mSubType);
+2 −0
Original line number Diff line number Diff line
@@ -80,6 +80,8 @@ message NetworkIdentityProto {
    optional bool metered = 5;

    optional bool default_network = 6;

    optional int32 oem_managed_network = 7;
}

// Corresponds to NetworkStatsRecorder.
+4 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.net.NetworkPolicy.LIMIT_DISABLED;
import static android.net.NetworkPolicy.WARNING_DISABLED;
import static android.net.NetworkTemplate.NETWORK_TYPE_ALL;
import static android.net.NetworkTemplate.OEM_MANAGED_ALL;
import static android.provider.Settings.Global.NETWORK_DEFAULT_DAILY_MULTIPATH_QUOTA_BYTES;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;

@@ -226,7 +227,7 @@ public class MultipathPolicyTracker {
            mNetworkTemplate = new NetworkTemplate(
                    NetworkTemplate.MATCH_MOBILE, subscriberId, new String[] { subscriberId },
                    null, NetworkStats.METERED_ALL, NetworkStats.ROAMING_ALL,
                    NetworkStats.DEFAULT_NETWORK_NO, NETWORK_TYPE_ALL);
                    NetworkStats.DEFAULT_NETWORK_NO, NETWORK_TYPE_ALL, OEM_MANAGED_ALL);
            mUsageCallback = new UsageCallback() {
                @Override
                public void onThresholdReached(int networkType, String subscriberId) {
@@ -274,7 +275,8 @@ public class MultipathPolicyTracker {
                    null /* networkId, unused for matching mobile networks */,
                    !nc.hasCapability(NET_CAPABILITY_NOT_ROAMING),
                    !nc.hasCapability(NET_CAPABILITY_NOT_METERED),
                    false /* defaultNetwork, templates should have DEFAULT_NETWORK_ALL */);
                    false /* defaultNetwork, templates should have DEFAULT_NETWORK_ALL */,
                    OEM_MANAGED_ALL);
        }

        private long getRemainingDailyBudget(long limitBytes,
+11 −2
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
    private static final int VERSION_ADD_NETWORK_ID = 3;
    private static final int VERSION_ADD_METERED = 4;
    private static final int VERSION_ADD_DEFAULT_NETWORK = 5;
    private static final int VERSION_ADD_OEM_MANAGED_NETWORK = 6;

    public NetworkIdentitySet() {
    }
@@ -84,13 +85,20 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
                defaultNetwork = true;
            }

            final int oemNetCapabilities;
            if (version >= VERSION_ADD_OEM_MANAGED_NETWORK) {
                oemNetCapabilities = in.readInt();
            } else {
                oemNetCapabilities = NetworkIdentity.OEM_NONE;
            }

            add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered,
                    defaultNetwork));
                    defaultNetwork, oemNetCapabilities));
        }
    }

    public void writeToStream(DataOutput out) throws IOException {
        out.writeInt(VERSION_ADD_DEFAULT_NETWORK);
        out.writeInt(VERSION_ADD_OEM_MANAGED_NETWORK);
        out.writeInt(size());
        for (NetworkIdentity ident : this) {
            out.writeInt(ident.getType());
@@ -100,6 +108,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
            out.writeBoolean(ident.getRoaming());
            out.writeBoolean(ident.getMetered());
            out.writeBoolean(ident.getDefaultNetwork());
            out.writeInt(ident.getOemManaged());
        }
    }

Loading