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

Commit 159be51d authored by Junyu Lai's avatar Junyu Lai Committed by Gerrit Code Review
Browse files

Merge changes from topic "ms57-migrationapi"

* changes:
  [MS57.1] Prepare APIs for data migration utility
  [MS45] Expose SystemApis to query summary and history with template
parents 614c30b6 caaa2364
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -56,6 +56,11 @@ package android.app.usage {
  public class NetworkStatsManager {
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void forceUpdate();
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void notifyNetworkStatus(@NonNull java.util.List<android.net.Network>, @NonNull java.util.List<android.net.NetworkStateSnapshot>, @Nullable String, @NonNull java.util.List<android.net.UnderlyingNetworkInfo>);
    method @NonNull @WorkerThread public android.app.usage.NetworkStats queryDetailsForDevice(@NonNull android.net.NetworkTemplate, long, long);
    method @NonNull @WorkerThread public android.app.usage.NetworkStats queryDetailsForUidTagState(@NonNull android.net.NetworkTemplate, long, long, int, int, int) throws java.lang.SecurityException;
    method @NonNull @WorkerThread public android.app.usage.NetworkStats querySummary(@NonNull android.net.NetworkTemplate, long, long) throws java.lang.SecurityException;
    method @NonNull @WorkerThread public android.app.usage.NetworkStats.Bucket querySummaryForDevice(@NonNull android.net.NetworkTemplate, long, long);
    method @NonNull @WorkerThread public android.app.usage.NetworkStats queryTaggedSummary(@NonNull android.net.NetworkTemplate, long, long) throws java.lang.SecurityException;
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void setDefaultGlobalAlert(long);
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void setPollOnOpen(boolean);
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void setStatsProviderWarningAndLimitAsync(@NonNull String, long, long);
+23 −5
Original line number Diff line number Diff line
@@ -125,6 +125,19 @@ public class NetworkStatsManager {
    private final Context mContext;
    private final INetworkStatsService mService;

    /**
     * Type constants for reading different types of Data Usage.
     * @hide
     */
    // @SystemApi(client = MODULE_LIBRARIES)
    public static final String PREFIX_DEV = "dev";
    /** @hide */
    public static final String PREFIX_XT = "xt";
    /** @hide */
    public static final String PREFIX_UID = "uid";
    /** @hide */
    public static final String PREFIX_UID_TAG = "uid_tag";

    /** @hide */
    public static final int FLAG_POLL_ON_OPEN = 1 << 0;
    /** @hide */
@@ -211,9 +224,10 @@ public class NetworkStatsManager {
     */
    @NonNull
    @WorkerThread
    // @SystemApi(client = MODULE_LIBRARIES)
    @SystemApi(client = MODULE_LIBRARIES)
    public Bucket querySummaryForDevice(@NonNull NetworkTemplate template,
            long startTime, long endTime) {
        Objects.requireNonNull(template);
        try {
            NetworkStats stats =
                    new NetworkStats(mContext, template, mFlags, startTime, endTime, mService);
@@ -385,10 +399,11 @@ public class NetworkStatsManager {
     * @hide
     */
    @NonNull
    // @SystemApi(client = MODULE_LIBRARIES)
    @SystemApi(client = MODULE_LIBRARIES)
    @WorkerThread
    public NetworkStats querySummary(@NonNull NetworkTemplate template, long startTime,
            long endTime) throws SecurityException {
        Objects.requireNonNull(template);
        try {
            NetworkStats result =
                    new NetworkStats(mContext, template, mFlags, startTime, endTime, mService);
@@ -418,10 +433,11 @@ public class NetworkStatsManager {
     * @hide
     */
    @NonNull
    // @SystemApi(client = MODULE_LIBRARIES)
    @SystemApi(client = MODULE_LIBRARIES)
    @WorkerThread
    public NetworkStats queryTaggedSummary(@NonNull NetworkTemplate template, long startTime,
            long endTime) throws SecurityException {
        Objects.requireNonNull(template);
        try {
            NetworkStats result =
                    new NetworkStats(mContext, template, mFlags, startTime, endTime, mService);
@@ -453,10 +469,11 @@ public class NetworkStatsManager {
     * @hide
     */
    @NonNull
    // @SystemApi(client = MODULE_LIBRARIES)
    @SystemApi(client = MODULE_LIBRARIES)
    @WorkerThread
    public NetworkStats queryDetailsForDevice(@NonNull NetworkTemplate template,
            long startTime, long endTime) {
        Objects.requireNonNull(template);
        try {
            final NetworkStats result =
                    new NetworkStats(mContext, template, mFlags, startTime, endTime, mService);
@@ -580,10 +597,11 @@ public class NetworkStatsManager {
     * @hide
     */
    @NonNull
    // @SystemApi(client = MODULE_LIBRARIES)
    @SystemApi(client = MODULE_LIBRARIES)
    @WorkerThread
    public NetworkStats queryDetailsForUidTagState(@NonNull NetworkTemplate template,
            long startTime, long endTime, int uid, int tag, int state) throws SecurityException {
        Objects.requireNonNull(template);
        try {
            final NetworkStats result = new NetworkStats(
                    mContext, template, mFlags, startTime, endTime, mService);
+62 −34
Original line number Diff line number Diff line
@@ -17,12 +17,15 @@
package android.net;

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

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.service.NetworkIdentityProto;
import android.telephony.Annotation.NetworkType;
import android.telephony.Annotation;
import android.telephony.TelephonyManager;
import android.util.proto.ProtoOutputStream;

import com.android.net.module.util.NetworkCapabilitiesUtils;
@@ -37,9 +40,13 @@ import java.util.Objects;
 *
 * @hide
 */
// @SystemApi(client = MODULE_LIBRARIES)
public class NetworkIdentity implements Comparable<NetworkIdentity> {
    private static final String TAG = "NetworkIdentity";

    /** @hide */
    // TODO: Remove this after migrating all callers to use
    //  {@link NetworkTemplate#NETWORK_TYPE_ALL} instead.
    public static final int SUBTYPE_COMBINED = -1;

    /**
@@ -59,21 +66,22 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
    public static final int OEM_PRIVATE = 0x2;

    final int mType;
    final int mSubType;
    final int mRatType;
    final String mSubscriberId;
    final String mNetworkId;
    final String mWifiNetworkKey;
    final boolean mRoaming;
    final boolean mMetered;
    final boolean mDefaultNetwork;
    final int mOemManaged;

    /** @hide */
    public NetworkIdentity(
            int type, int subType, String subscriberId, String networkId, boolean roaming,
            boolean metered, boolean defaultNetwork, int oemManaged) {
            int type, int ratType, @Nullable String subscriberId, @Nullable String wifiNetworkKey,
            boolean roaming, boolean metered, boolean defaultNetwork, int oemManaged) {
        mType = type;
        mSubType = subType;
        mRatType = ratType;
        mSubscriberId = subscriberId;
        mNetworkId = networkId;
        mWifiNetworkKey = wifiNetworkKey;
        mRoaming = roaming;
        mMetered = metered;
        mDefaultNetwork = defaultNetwork;
@@ -82,7 +90,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {

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

@@ -90,9 +98,9 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
    public boolean equals(@Nullable Object obj) {
        if (obj instanceof NetworkIdentity) {
            final NetworkIdentity ident = (NetworkIdentity) obj;
            return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming
            return mType == ident.mType && mRatType == ident.mRatType && mRoaming == ident.mRoaming
                    && Objects.equals(mSubscriberId, ident.mSubscriberId)
                    && Objects.equals(mNetworkId, ident.mNetworkId)
                    && Objects.equals(mWifiNetworkKey, ident.mWifiNetworkKey)
                    && mMetered == ident.mMetered
                    && mDefaultNetwork == ident.mDefaultNetwork
                    && mOemManaged == ident.mOemManaged;
@@ -104,18 +112,18 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
    public String toString() {
        final StringBuilder builder = new StringBuilder("{");
        builder.append("type=").append(mType);
        builder.append(", subType=");
        if (mSubType == SUBTYPE_COMBINED) {
        builder.append(", ratType=");
        if (mRatType == NETWORK_TYPE_ALL) {
            builder.append("COMBINED");
        } else {
            builder.append(mSubType);
            builder.append(mRatType);
        }
        if (mSubscriberId != null) {
            builder.append(", subscriberId=")
                    .append(NetworkIdentityUtils.scrubSubscriberId(mSubscriberId));
        }
        if (mNetworkId != null) {
            builder.append(", networkId=").append(mNetworkId);
        if (mWifiNetworkKey != null) {
            builder.append(", wifiNetworkKey=").append(mWifiNetworkKey);
        }
        if (mRoaming) {
            builder.append(", ROAMING");
@@ -153,12 +161,13 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        }
    }

    /** @hide */
    public void dumpDebug(ProtoOutputStream proto, long tag) {
        final long start = proto.start(tag);

        proto.write(NetworkIdentityProto.TYPE, mType);

        // Not dumping mSubType, subtypes are no longer supported.
        // TODO: dump mRatType as well.

        proto.write(NetworkIdentityProto.ROAMING, mRoaming);
        proto.write(NetworkIdentityProto.METERED, mMetered);
@@ -168,50 +177,68 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
        proto.end(start);
    }

    /** @hide */
    public int getType() {
        return mType;
    }

    public int getSubType() {
        return mSubType;
    /** @hide */
    public int getRatType() {
        return mRatType;
    }

    /** @hide */
    public String getSubscriberId() {
        return mSubscriberId;
    }

    public String getNetworkId() {
        return mNetworkId;
    /** @hide */
    public String getWifiNetworkKey() {
        return mWifiNetworkKey;
    }

    /** @hide */
    public boolean getRoaming() {
        return mRoaming;
    }

    /** @hide */
    public boolean getMetered() {
        return mMetered;
    }

    /** @hide */
    public boolean getDefaultNetwork() {
        return mDefaultNetwork;
    }

    /** @hide */
    public int getOemManaged() {
        return mOemManaged;
    }

    /**
     * Build a {@link NetworkIdentity} from the given {@link NetworkStateSnapshot} and
     * {@code subType}, assuming that any mobile networks are using the current IMSI.
     * The subType if applicable, should be set as one of the TelephonyManager.NETWORK_TYPE_*
     * constants, or {@link android.telephony.TelephonyManager#NETWORK_TYPE_UNKNOWN} if not.
     * Assemble a {@link NetworkIdentity} from the passed arguments.
     *
     * This methods builds an identity based on the capabilities of the network in the
     * snapshot and other passed arguments. The identity is used as a key to record data usage.
     *
     * @param snapshot the snapshot of network state. See {@link NetworkStateSnapshot}.
     * @param defaultNetwork whether the network is a default network.
     * @param ratType the Radio Access Technology(RAT) type of the network. Or
     *                {@link TelephonyManager#NETWORK_TYPE_UNKNOWN} if not applicable.
     *                See {@code TelephonyManager.NETWORK_TYPE_*}.
     * @hide
     */
    // TODO: Remove this after all callers are migrated to use new Api.
    @NonNull
    public static NetworkIdentity buildNetworkIdentity(Context context,
            NetworkStateSnapshot snapshot, boolean defaultNetwork, @NetworkType int subType) {
            @NonNull NetworkStateSnapshot snapshot,
            boolean defaultNetwork, @Annotation.NetworkType int ratType) {
        final int legacyType = snapshot.getLegacyType();

        final String subscriberId = snapshot.getSubscriberId();
        String networkId = null;
        String wifiNetworkKey = null;
        boolean roaming = !snapshot.getNetworkCapabilities().hasCapability(
                NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
        boolean metered = !(snapshot.getNetworkCapabilities().hasCapability(
@@ -226,19 +253,19 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
                    .getTransportInfo();
            if (transportInfo instanceof WifiInfo) {
                final WifiInfo info = (WifiInfo) transportInfo;
                networkId = info != null ? info.getCurrentNetworkKey() : null;
                wifiNetworkKey = info != null ? info.getCurrentNetworkKey() : null;
            }
        }

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

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

        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID)) {
@@ -252,16 +279,17 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
    }

    @Override
    public int compareTo(NetworkIdentity another) {
    public int compareTo(@NonNull NetworkIdentity another) {
        Objects.requireNonNull(another);
        int res = Integer.compare(mType, another.mType);
        if (res == 0) {
            res = Integer.compare(mSubType, another.mSubType);
            res = Integer.compare(mRatType, another.mRatType);
        }
        if (res == 0 && mSubscriberId != null && another.mSubscriberId != null) {
            res = mSubscriberId.compareTo(another.mSubscriberId);
        }
        if (res == 0 && mNetworkId != null && another.mNetworkId != null) {
            res = mNetworkId.compareTo(another.mNetworkId);
        if (res == 0 && mWifiNetworkKey != null && another.mWifiNetworkKey != null) {
            res = mWifiNetworkKey.compareTo(another.mWifiNetworkKey);
        }
        if (res == 0) {
            res = Boolean.compare(mRoaming, another.mRoaming);
+29 −9
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.net;

import static android.net.ConnectivityManager.TYPE_MOBILE;

import android.annotation.NonNull;
import android.service.NetworkIdentitySetProto;
import android.util.proto.ProtoOutputStream;

@@ -25,6 +26,7 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashSet;
import java.util.Objects;

/**
 * Identity of a {@code iface}, defined by the set of {@link NetworkIdentity}
@@ -32,6 +34,7 @@ import java.util.HashSet;
 *
 * @hide
 */
// @SystemApi(client = MODULE_LIBRARIES)
public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
        Comparable<NetworkIdentitySet> {
    private static final int VERSION_INIT = 1;
@@ -41,9 +44,14 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
    private static final int VERSION_ADD_DEFAULT_NETWORK = 5;
    private static final int VERSION_ADD_OEM_MANAGED_NETWORK = 6;

    /**
     * Construct a {@link NetworkIdentitySet} object.
     */
    public NetworkIdentitySet() {
        super();
    }

    /** @hide */
    public NetworkIdentitySet(DataInput in) throws IOException {
        final int version = in.readInt();
        final int size = in.readInt();
@@ -52,7 +60,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
                final int ignored = in.readInt();
            }
            final int type = in.readInt();
            final int subType = in.readInt();
            final int ratType = in.readInt();
            final String subscriberId = readOptionalString(in);
            final String networkId;
            if (version >= VERSION_ADD_NETWORK_ID) {
@@ -91,22 +99,23 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
                oemNetCapabilities = NetworkIdentity.OEM_NONE;
            }

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

    /**
     * Method to serialize this object into a {@code DataOutput}.
     * @hide
     */
    public void writeToStream(DataOutput out) throws IOException {
        out.writeInt(VERSION_ADD_OEM_MANAGED_NETWORK);
        out.writeInt(size());
        for (NetworkIdentity ident : this) {
            out.writeInt(ident.getType());
            out.writeInt(ident.getSubType());
            out.writeInt(ident.getRatType());
            writeOptionalString(out, ident.getSubscriberId());
            writeOptionalString(out, ident.getNetworkId());
            writeOptionalString(out, ident.getWifiNetworkKey());
            out.writeBoolean(ident.getRoaming());
            out.writeBoolean(ident.getMetered());
            out.writeBoolean(ident.getDefaultNetwork());
@@ -114,7 +123,10 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
        }
    }

    /** @return whether any {@link NetworkIdentity} in this set is considered metered. */
    /**
     * @return whether any {@link NetworkIdentity} in this set is considered metered.
     * @hide
     */
    public boolean isAnyMemberMetered() {
        if (isEmpty()) {
            return false;
@@ -127,7 +139,10 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
        return false;
    }

    /** @return whether any {@link NetworkIdentity} in this set is considered roaming. */
    /**
     * @return whether any {@link NetworkIdentity} in this set is considered roaming.
     * @hide
     */
    public boolean isAnyMemberRoaming() {
        if (isEmpty()) {
            return false;
@@ -140,8 +155,11 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
        return false;
    }

    /** @return whether any {@link NetworkIdentity} in this set is considered on the default
            network. */
    /**
     * @return whether any {@link NetworkIdentity} in this set is considered on the default
     *         network.
     * @hide
     */
    public boolean areAllMembersOnDefaultNetwork() {
        if (isEmpty()) {
            return true;
@@ -172,7 +190,8 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
    }

    @Override
    public int compareTo(NetworkIdentitySet another) {
    public int compareTo(@NonNull NetworkIdentitySet another) {
        Objects.requireNonNull(another);
        if (isEmpty()) return -1;
        if (another.isEmpty()) return 1;

@@ -183,6 +202,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements

    /**
     * Method to dump this object into proto debug file.
     * @hide
     */
    public void dumpDebug(ProtoOutputStream proto, long tag) {
        final long start = proto.start(tag);
+64 −7

File changed.

Preview size limit exceeded, changes collapsed.

Loading