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

Commit 7246788b authored by Junyu Lai's avatar Junyu Lai Committed by Automerger Merge Worker
Browse files

Merge changes from topics "ms56-historybuilder", "ms65.3" am: 54bc16fc

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1951478

Change-Id: I8f727a73716c3f06d8c946a7e014793c5c8046eb
parents 2c73071f 54bc16fc
Loading
Loading
Loading
Loading
+54 −15
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.net;

import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.NetworkTemplate.NETWORK_TYPE_ALL;

@@ -30,6 +31,7 @@ import android.telephony.Annotation;
import android.telephony.TelephonyManager;
import android.util.proto.ProtoOutputStream;

import com.android.net.module.util.CollectionUtils;
import com.android.net.module.util.NetworkCapabilitiesUtils;
import com.android.net.module.util.NetworkIdentityUtils;

@@ -55,7 +57,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(prefix = { "OEM_MANAGED_" }, value = {
    @IntDef(prefix = { "OEM_MANAGED_" }, flag = true, value = {
            NetworkTemplate.OEM_MANAGED_NO,
            NetworkTemplate.OEM_MANAGED_PAID,
            NetworkTemplate.OEM_MANAGED_PRIVATE
@@ -71,12 +73,14 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
     * Network has {@link NetworkCapabilities#NET_CAPABILITY_OEM_PAID}.
     * @hide
     */
    public static final int OEM_PAID = 0x1;
    public static final int OEM_PAID = 1 << 0;
    /**
     * Network has {@link NetworkCapabilities#NET_CAPABILITY_OEM_PRIVATE}.
     * @hide
     */
    public static final int OEM_PRIVATE = 0x2;
    public static final int OEM_PRIVATE = 1 << 1;

    private static final long SUPPORTED_OEM_MANAGED_TYPES = OEM_PAID | OEM_PRIVATE;

    final int mType;
    final int mRatType;
@@ -218,7 +222,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        return mRoaming;
    }

    /** Return the roaming status of this instance. */
    /** Return whether this network is roaming. */
    public boolean isRoaming() {
        return mRoaming;
    }
@@ -229,7 +233,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        return mMetered;
    }

    /** Return the meteredness of this instance. */
    /** Return whether this network is metered. */
    public boolean isMetered() {
        return mMetered;
    }
@@ -240,7 +244,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        return mDefaultNetwork;
    }

    /** Return the default network status of this instance. */
    /** Return whether this network is the default network. */
    public boolean isDefaultNetwork() {
        return mDefaultNetwork;
    }
@@ -262,7 +266,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
     *                {@link TelephonyManager#NETWORK_TYPE_UNKNOWN} if not applicable.
     *                See {@code TelephonyManager.NETWORK_TYPE_*}.
     * @hide
     * @deprecated See {@link NetworkIdentity#Builder}.
     * @deprecated See {@link NetworkIdentity.Builder}.
     */
    // TODO: Remove this after all callers are migrated to use new Api.
    @Deprecated
@@ -270,8 +274,12 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
    public static NetworkIdentity buildNetworkIdentity(Context context,
            @NonNull NetworkStateSnapshot snapshot,
            boolean defaultNetwork, @Annotation.NetworkType int ratType) {
        return new NetworkIdentity.Builder().setNetworkStateSnapshot(snapshot)
                .setDefaultNetwork(defaultNetwork).setRatType(ratType).build();
        final NetworkIdentity.Builder builder = new NetworkIdentity.Builder()
                .setNetworkStateSnapshot(snapshot).setDefaultNetwork(defaultNetwork);
        if (snapshot.getLegacyType() == TYPE_MOBILE && ratType != NETWORK_TYPE_ALL) {
            builder.setRatType(ratType);
        }
        return builder.build();
    }

    /**
@@ -323,6 +331,11 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
     * Builder class for {@link NetworkIdentity}.
     */
    public static final class Builder {
        // Need to be synchronized with ConnectivityManager.
        // TODO: Use {@link ConnectivityManager#MAX_NETWORK_TYPE} when this file is in the module.
        private static final int MAX_NETWORK_TYPE = 18; // TYPE_TEST
        private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;

        private int mType;
        private int mRatType;
        private String mSubscriberId;
@@ -374,11 +387,9 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
                        .getTransportInfo();
                if (transportInfo instanceof WifiInfo) {
                    final WifiInfo info = (WifiInfo) transportInfo;
                    if (info != null) {
                    setWifiNetworkKey(info.getNetworkKey());
                }
            }
            }
            return this;
        }

@@ -391,6 +402,12 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
         */
        @NonNull
        public Builder setType(int type) {
            // Include TYPE_NONE for compatibility, type field might not be filled by some
            // networks such as test networks.
            if ((type < MIN_NETWORK_TYPE || MAX_NETWORK_TYPE < type)
                    && type != ConnectivityManager.TYPE_NONE) {
                throw new IllegalArgumentException("Invalid network type: " + type);
            }
            mType = type;
            return this;
        }
@@ -405,6 +422,10 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
         */
        @NonNull
        public Builder setRatType(@Annotation.NetworkType int ratType) {
            if (!CollectionUtils.contains(TelephonyManager.getAllNetworkTypes(), ratType)
                    && ratType != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
                throw new IllegalArgumentException("Invalid ratType " + ratType);
            }
            mRatType = ratType;
            return this;
        }
@@ -447,7 +468,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        }

        /**
         * Set the roaming.
         * Set whether this network is roaming.
         *
         * @param roaming the roaming status of the network.
         * @return this builder.
@@ -459,7 +480,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        }

        /**
         * Set the meteredness.
         * Set whether this network is metered.
         *
         * @param metered the meteredness of the network.
         * @return this builder.
@@ -471,7 +492,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        }

        /**
         * Set the default network status.
         * Set whether this network is the default network.
         *
         * @param defaultNetwork the default network status of the network.
         * @return this builder.
@@ -491,10 +512,27 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
         */
        @NonNull
        public Builder setOemManaged(@OemManaged int oemManaged) {
            // Assert input does not contain illegal oemManage bits.
            if ((~SUPPORTED_OEM_MANAGED_TYPES & oemManaged) != 0) {
                throw new IllegalArgumentException("Invalid value for OemManaged : " + oemManaged);
            }
            mOemManaged = oemManaged;
            return this;
        }

        private void ensureValidParameters() {
            // Assert non-mobile network cannot have a ratType.
            if (mType != TYPE_MOBILE && mRatType != NetworkTemplate.NETWORK_TYPE_ALL) {
                throw new IllegalArgumentException(
                        "Invalid ratType " + mRatType + " for type " + mType);
            }

            // Assert non-wifi network cannot have a wifi network key.
            if (mType != TYPE_WIFI && mWifiNetworkKey != null) {
                throw new IllegalArgumentException("Invalid wifi network key for type " + mType);
            }
        }

        /**
         * Builds the instance of the {@link NetworkIdentity}.
         *
@@ -502,6 +540,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
         */
        @NonNull
        public NetworkIdentity build() {
            ensureValidParameters();
            return new NetworkIdentity(mType, mRatType, mSubscriberId, mWifiNetworkKey,
                    mRoaming, mMetered, mDefaultNetwork, mOemManaged);
        }
+229 −1
Original line number Diff line number Diff line
@@ -51,7 +51,9 @@ import java.io.DataOutput;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
@@ -97,23 +99,157 @@ public final class NetworkStatsHistory implements Parcelable {
    private long[] operations;
    private long totalBytes;

    public static class Entry {
    /** @hide */
    public NetworkStatsHistory(long bucketDuration, long[] bucketStart, long[] activeTime,
            long[] rxBytes, long[] rxPackets, long[] txBytes, long[] txPackets,
            long[] operations, int bucketCount, long totalBytes) {
        this.bucketDuration = bucketDuration;
        this.bucketStart = bucketStart;
        this.activeTime = activeTime;
        this.rxBytes = rxBytes;
        this.rxPackets = rxPackets;
        this.txBytes = txBytes;
        this.txPackets = txPackets;
        this.operations = operations;
        this.bucketCount = bucketCount;
        this.totalBytes = totalBytes;
    }

    /**
     * An instance to represent a single record in a {@link NetworkStatsHistory} object.
     */
    public static final class Entry {
        /** @hide */
        public static final long UNKNOWN = -1;

        /** @hide */
        // TODO: Migrate all callers to get duration from the history object and remove this field.
        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        public long bucketDuration;
        /** @hide */
        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        public long bucketStart;
        /** @hide */
        public long activeTime;
        /** @hide */
        @UnsupportedAppUsage
        public long rxBytes;
        /** @hide */
        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        public long rxPackets;
        /** @hide */
        @UnsupportedAppUsage
        public long txBytes;
        /** @hide */
        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        public long txPackets;
        /** @hide */
        public long operations;
        /** @hide */
        Entry() {}

        /**
         * Construct a {@link Entry} instance to represent a single record in a
         * {@link NetworkStatsHistory} object.
         *
         * @param bucketStart Start of period for this {@link Entry}, in milliseconds since the
         *                    Unix epoch, see {@link java.lang.System#currentTimeMillis}.
         * @param activeTime Active time for this {@link Entry}, in milliseconds.
         * @param rxBytes Number of bytes received for this {@link Entry}. Statistics should
         *                represent the contents of IP packets, including IP headers.
         * @param rxPackets Number of packets received for this {@link Entry}. Statistics should
         *                  represent the contents of IP packets, including IP headers.
         * @param txBytes Number of bytes transmitted for this {@link Entry}. Statistics should
         *                represent the contents of IP packets, including IP headers.
         * @param txPackets Number of bytes transmitted for this {@link Entry}. Statistics should
         *                  represent the contents of IP packets, including IP headers.
         * @param operations count of network operations performed for this {@link Entry}. This can
         *                   be used to derive bytes-per-operation.
         */
        public Entry(long bucketStart, long activeTime, long rxBytes,
                long rxPackets, long txBytes, long txPackets, long operations) {
            this.bucketStart = bucketStart;
            this.activeTime = activeTime;
            this.rxBytes = rxBytes;
            this.rxPackets = rxPackets;
            this.txBytes = txBytes;
            this.txPackets = txPackets;
            this.operations = operations;
        }

        /**
         * Get start timestamp of the bucket's time interval, in milliseconds since the Unix epoch.
         */
        public long getBucketStart() {
            return bucketStart;
        }

        /**
         * Get active time of the bucket's time interval, in milliseconds.
         */
        public long getActiveTime() {
            return activeTime;
        }

        /** Get number of bytes received for this {@link Entry}. */
        public long getRxBytes() {
            return rxBytes;
        }

        /** Get number of packets received for this {@link Entry}. */
        public long getRxPackets() {
            return rxPackets;
        }

        /** Get number of bytes transmitted for this {@link Entry}. */
        public long getTxBytes() {
            return txBytes;
        }

        /** Get number of packets transmitted for this {@link Entry}. */
        public long getTxPackets() {
            return txPackets;
        }

        /** Get count of network operations performed for this {@link Entry}. */
        public long getOperations() {
            return operations;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o.getClass() != getClass()) return false;
            Entry entry = (Entry) o;
            return bucketStart == entry.bucketStart
                    && activeTime == entry.activeTime && rxBytes == entry.rxBytes
                    && rxPackets == entry.rxPackets && txBytes == entry.txBytes
                    && txPackets == entry.txPackets && operations == entry.operations;
        }

        @Override
        public int hashCode() {
            return (int) (bucketStart * 2
                    + activeTime * 3
                    + rxBytes * 5
                    + rxPackets * 7
                    + txBytes * 11
                    + txPackets * 13
                    + operations * 17);
        }

        @Override
        public String toString() {
            return "Entry{"
                    + "bucketStart=" + bucketStart
                    + ", activeTime=" + activeTime
                    + ", rxBytes=" + rxBytes
                    + ", rxPackets=" + rxPackets
                    + ", txBytes=" + txBytes
                    + ", txPackets=" + txPackets
                    + ", operations=" + operations
                    + "}";
        }
    }

    /** @hide */
@@ -324,6 +460,22 @@ public final class NetworkStatsHistory implements Parcelable {
        return entry;
    }

    /**
     * Get List of {@link Entry} of the {@link NetworkStatsHistory} instance.
     *
     * @return
     */
    @NonNull
    public List<Entry> getEntries() {
        // TODO: Return a wrapper that uses this list instead, to prevent the returned result
        //  from being changed.
        final ArrayList<Entry> ret = new ArrayList<>(size());
        for (int i = 0; i < size(); i++) {
            ret.add(getValues(i, null /* recycle */));
        }
        return ret;
    }

    /** @hide */
    public void setValues(int i, Entry entry) {
        // Unwind old values
@@ -928,4 +1080,80 @@ public final class NetworkStatsHistory implements Parcelable {
        }
    }

    /**
     * Builder class for {@link NetworkStatsHistory}.
     */
    public static final class Builder {
        private final long mBucketDuration;
        private final List<Long> mBucketStart;
        private final List<Long> mActiveTime;
        private final List<Long> mRxBytes;
        private final List<Long> mRxPackets;
        private final List<Long> mTxBytes;
        private final List<Long> mTxPackets;
        private final List<Long> mOperations;

        /**
         * Creates a new Builder with given bucket duration and initial capacity to construct
         * {@link NetworkStatsHistory} objects.
         *
         * @param bucketDuration Duration of the buckets of the object, in milliseconds.
         * @param initialCapacity Estimated number of records.
         */
        public Builder(long bucketDuration, int initialCapacity) {
            mBucketDuration = bucketDuration;
            mBucketStart = new ArrayList<>(initialCapacity);
            mActiveTime = new ArrayList<>(initialCapacity);
            mRxBytes = new ArrayList<>(initialCapacity);
            mRxPackets = new ArrayList<>(initialCapacity);
            mTxBytes = new ArrayList<>(initialCapacity);
            mTxPackets = new ArrayList<>(initialCapacity);
            mOperations = new ArrayList<>(initialCapacity);
        }

        /**
         * Add an {@link Entry} into the {@link NetworkStatsHistory} instance.
         *
         * @param entry The target {@link Entry} object.
         * @return The builder object.
         */
        @NonNull
        public Builder addEntry(@NonNull Entry entry) {
            mBucketStart.add(entry.bucketStart);
            mActiveTime.add(entry.activeTime);
            mRxBytes.add(entry.rxBytes);
            mRxPackets.add(entry.rxPackets);
            mTxBytes.add(entry.txBytes);
            mTxPackets.add(entry.txPackets);
            mOperations.add(entry.operations);
            return this;
        }

        private static long sum(@NonNull List<Long> list) {
            long sum = 0;
            for (long entry : list) {
                sum += entry;
            }
            return sum;
        }

        /**
         * Builds the instance of the {@link NetworkStatsHistory}.
         *
         * @return the built instance of {@link NetworkStatsHistory}.
         */
        @NonNull
        public NetworkStatsHistory build() {
            return new NetworkStatsHistory(mBucketDuration,
                    CollectionUtils.toLongArray(mBucketStart),
                    CollectionUtils.toLongArray(mActiveTime),
                    CollectionUtils.toLongArray(mRxBytes),
                    CollectionUtils.toLongArray(mRxPackets),
                    CollectionUtils.toLongArray(mTxBytes),
                    CollectionUtils.toLongArray(mTxPackets),
                    CollectionUtils.toLongArray(mOperations),
                    mBucketStart.size(),
                    sum(mRxBytes) + sum(mTxBytes));
        }
    }
}
+23 −23
Original line number Diff line number Diff line
@@ -63,7 +63,6 @@ import static android.net.INetd.FIREWALL_RULE_DENY;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.net.NetworkIdentity.OEM_NONE;
import static android.net.NetworkPolicy.LIMIT_DISABLED;
import static android.net.NetworkPolicy.SNOOZE_NEVER;
import static android.net.NetworkPolicy.WARNING_DISABLED;
@@ -1518,13 +1517,11 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        for (int i = 0; i < mSubIdToSubscriberId.size(); i++) {
            final int subId = mSubIdToSubscriberId.keyAt(i);
            final String subscriberId = mSubIdToSubscriberId.valueAt(i);
            final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
                    TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true,
                    true, OEM_NONE);
            /* While OEM_NONE indicates "any non OEM managed network", OEM_NONE is meant to be a
             * placeholder value here. The probeIdent is matched against a NetworkTemplate which
             * should have its OEM managed value set to OEM_MANAGED_ALL, which will cause the
             * template to match probeIdent without regard to OEM managed status. */
            final NetworkIdentity probeIdent = new NetworkIdentity.Builder()
                    .setType(TYPE_MOBILE)
                    .setSubscriberId(subscriberId)
                    .setMetered(true)
                    .setDefaultNetwork(true).build();
            if (template.matches(probeIdent)) {
                return subId;
            }
@@ -1757,9 +1754,11 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {

        // find and update the carrier NetworkPolicy for this subscriber id
        boolean policyUpdated = false;
        final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
                TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true,
                OEM_NONE);
        final NetworkIdentity probeIdent = new NetworkIdentity.Builder()
                .setType(TYPE_MOBILE)
                .setSubscriberId(subscriberId)
                .setMetered(true)
                .setDefaultNetwork(true).build();
        for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) {
            final NetworkTemplate template = mNetworkPolicy.keyAt(i);
            if (template.matches(probeIdent)) {
@@ -1987,10 +1986,11 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                for (int i = 0; i < mSubIdToSubscriberId.size(); i++) {
                    final int subId = mSubIdToSubscriberId.keyAt(i);
                    final String subscriberId = mSubIdToSubscriberId.valueAt(i);

                    final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
                            TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true,
                            true, OEM_NONE);
                    final NetworkIdentity probeIdent = new NetworkIdentity.Builder()
                            .setType(TYPE_MOBILE)
                            .setSubscriberId(subscriberId)
                            .setMetered(true)
                            .setDefaultNetwork(true).build();
                    // Template is matched when subscriber id matches.
                    if (template.matches(probeIdent)) {
                        matchingSubIds.add(subId);
@@ -2094,11 +2094,9 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        for (final NetworkStateSnapshot snapshot : snapshots) {
            mNetIdToSubId.put(snapshot.getNetwork().getNetId(), parseSubId(snapshot));

            // Policies matched by NPMS only match by subscriber ID or by network ID. Thus subtype
            // in the object created here is never used and its value doesn't matter, so use
            // NETWORK_TYPE_UNKNOWN.
            final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, snapshot,
                    true, TelephonyManager.NETWORK_TYPE_UNKNOWN /* subType */);
            // Policies matched by NPMS only match by subscriber ID or by network ID.
            final NetworkIdentity ident = new NetworkIdentity.Builder()
                    .setNetworkStateSnapshot(snapshot).setDefaultNetwork(true).build();
            identified.put(snapshot, ident);
        }

@@ -2295,9 +2293,11 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    @GuardedBy("mNetworkPoliciesSecondLock")
    private boolean ensureActiveCarrierPolicyAL(int subId, String subscriberId) {
        // Poke around to see if we already have a policy
        final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
                TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true,
                OEM_NONE);
        final NetworkIdentity probeIdent = new NetworkIdentity.Builder()
                .setType(TYPE_MOBILE)
                .setSubscriberId(subscriberId)
                .setMetered(true)
                .setDefaultNetwork(true).build();
        for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) {
            final NetworkTemplate template = mNetworkPolicy.keyAt(i);
            if (template.matches(probeIdent)) {