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

Commit 4195f030 authored by Chiachang Wang's avatar Chiachang Wang Committed by Automerger Merge Worker
Browse files

Merge changes from topic "replaceUidRange" am: 1b6c0b7a am: 6319e35a

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

Change-Id: Ie767ae0818210b2f4946450bc6784b89514ff3ad
parents 9ecd1698 6319e35a
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -35,9 +35,18 @@ package android.net {
  }

  public final class NetworkCapabilities implements android.os.Parcelable {
    method @Nullable public java.util.Set<android.util.Range<java.lang.Integer>> getUids();
    field public static final int TRANSPORT_TEST = 7; // 0x7
  }

  public static final class NetworkCapabilities.Builder {
    method @NonNull public android.net.NetworkCapabilities.Builder setUids(@Nullable java.util.Set<android.util.Range<java.lang.Integer>>);
  }

  public static class NetworkRequest.Builder {
    method @NonNull public android.net.NetworkRequest.Builder setUids(@Nullable java.util.Set<android.util.Range<java.lang.Integer>>);
  }

  public class ParseException extends java.lang.RuntimeException {
    ctor public ParseException(@NonNull String);
    ctor public ParseException(@NonNull String, @NonNull Throwable);
+40 −12
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage;
import android.net.ConnectivityManager.NetworkCallback;
@@ -32,6 +33,7 @@ import android.os.Parcelable;
import android.os.Process;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Range;
import android.util.proto.ProtoOutputStream;

import com.android.internal.annotations.VisibleForTesting;
@@ -153,7 +155,7 @@ public final class NetworkCapabilities implements Parcelable {
            setTransportInfo(null);
        }
        mSignalStrength = nc.mSignalStrength;
        setUids(nc.mUids); // Will make the defensive copy
        mUids = (nc.mUids == null) ? null : new ArraySet<>(nc.mUids);
        setAdministratorUids(nc.getAdministratorUids());
        mOwnerUid = nc.mOwnerUid;
        mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
@@ -1458,9 +1460,8 @@ public final class NetworkCapabilities implements Parcelable {
     * @hide
     */
    public @NonNull NetworkCapabilities setSingleUid(int uid) {
        final ArraySet<UidRange> identity = new ArraySet<>(1);
        identity.add(new UidRange(uid, uid));
        setUids(identity);
        mUids = new ArraySet<>(1);
        mUids.add(new UidRange(uid, uid));
        return this;
    }

@@ -1469,22 +1470,34 @@ public final class NetworkCapabilities implements Parcelable {
     * This makes a copy of the set so that callers can't modify it after the call.
     * @hide
     */
    public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) {
        if (null == uids) {
            mUids = null;
        } else {
            mUids = new ArraySet<>(uids);
        }
    public @NonNull NetworkCapabilities setUids(@Nullable Set<Range<Integer>> uids) {
        mUids = UidRange.fromIntRanges(uids);
        return this;
    }

    /**
     * Get the list of UIDs this network applies to.
     * This returns a copy of the set so that callers can't modify the original object.
     *
     * @return the list of UIDs this network applies to. If {@code null}, then the network applies
     *         to all UIDs.
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    @SuppressLint("NullableCollection")
    public @Nullable Set<Range<Integer>> getUids() {
        return UidRange.toIntRanges(mUids);
    }

    /**
     * Get the list of UIDs this network applies to.
     * This returns a copy of the set so that callers can't modify the original object.
     * @hide
     */
    public @Nullable Set<UidRange> getUids() {
        return null == mUids ? null : new ArraySet<>(mUids);
    public @Nullable Set<UidRange> getUidRanges() {
        if (mUids == null) return null;

        return new ArraySet<>(mUids);
    }

    /**
@@ -2654,6 +2667,21 @@ public final class NetworkCapabilities implements Parcelable {
            return this;
        }

        /**
         * Set the list of UIDs this network applies to.
         *
         * @param uids the list of UIDs this network applies to, or {@code null} if this network
         *             applies to all UIDs.
         * @return this builder
         * @hide
         */
        @NonNull
        @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
        public Builder setUids(@Nullable Set<Range<Integer>> uids) {
            mCaps.setUids(uids);
            return this;
        }

        /**
         * Builds the instance of the capabilities.
         *
+7 −2
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import static android.net.NetworkCapabilities.TRANSPORT_TEST;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage;
import android.net.NetworkCapabilities.NetCapability;
@@ -45,6 +46,7 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.os.Process;
import android.text.TextUtils;
import android.util.Range;
import android.util.proto.ProtoOutputStream;

import java.util.Arrays;
@@ -277,11 +279,14 @@ public class NetworkRequest implements Parcelable {
         * Set the watched UIDs for this request. This will be reset and wiped out unless
         * the calling app holds the CHANGE_NETWORK_STATE permission.
         *
         * @param uids The watched UIDs as a set of UidRanges, or null for everything.
         * @param uids The watched UIDs as a set of {@code Range<Integer>}, or null for everything.
         * @return The builder to facilitate chaining.
         * @hide
         */
        public Builder setUids(Set<UidRange> uids) {
        @NonNull
        @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
        @SuppressLint("MissingGetterMatchingBuilder")
        public Builder setUids(@Nullable Set<Range<Integer>> uids) {
            mNetworkCapabilities.setUids(uids);
            return this;
        }
+31 −0
Original line number Diff line number Diff line
@@ -20,8 +20,11 @@ import android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.util.ArraySet;
import android.util.Range;

import java.util.Collection;
import java.util.Set;

/**
 * An inclusive range of UIDs.
@@ -149,4 +152,32 @@ public final class UidRange implements Parcelable {
        }
        return false;
    }

    /**
     *  Convert a set of {@code Range<Integer>} to a set of {@link UidRange}.
     */
    @Nullable
    public static ArraySet<UidRange> fromIntRanges(@Nullable Set<Range<Integer>> ranges) {
        if (null == ranges) return null;

        final ArraySet<UidRange> uids = new ArraySet<>();
        for (Range<Integer> range : ranges) {
            uids.add(new UidRange(range.getLower(), range.getUpper()));
        }
        return uids;
    }

    /**
     *  Convert a set of {@link UidRange} to a set of {@code Range<Integer>}.
     */
    @Nullable
    public static ArraySet<Range<Integer>> toIntRanges(@Nullable Set<UidRange> ranges) {
        if (null == ranges) return null;

        final ArraySet<Range<Integer>> uids = new ArraySet<>();
        for (UidRange range : ranges) {
            uids.add(new Range<Integer>(range.start, range.stop));
        }
        return uids;
    }
}
+10 −12
Original line number Diff line number Diff line
@@ -1344,7 +1344,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
        netCap.addCapability(NET_CAPABILITY_INTERNET);
        netCap.addCapability(NET_CAPABILITY_NOT_VCN_MANAGED);
        netCap.removeCapability(NET_CAPABILITY_NOT_VPN);
        netCap.setUids(Collections.singleton(uids));
        netCap.setUids(UidRange.toIntRanges(Collections.singleton(uids)));
        return netCap;
    }

@@ -2904,7 +2904,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
            if (0 == defaultRequest.mRequests.size()) {
                pw.println("none, this should never occur.");
            } else {
                pw.println(defaultRequest.mRequests.get(0).networkCapabilities.getUids());
                pw.println(defaultRequest.mRequests.get(0).networkCapabilities.getUidRanges());
            }
            pw.decreaseIndent();
            pw.decreaseIndent();
@@ -5322,9 +5322,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
        private Set<UidRange> getUids() {
            // networkCapabilities.getUids() returns a defensive copy.
            // multilayer requests will all have the same uids so return the first one.
            final Set<UidRange> uids = null == mRequests.get(0).networkCapabilities.getUids()
                    ? new ArraySet<>() : mRequests.get(0).networkCapabilities.getUids();
            return uids;
            final Set<UidRange> uids = mRequests.get(0).networkCapabilities.getUidRanges();
            return (null == uids) ? new ArraySet<>() : uids;
        }

        NetworkRequestInfo(@NonNull final NetworkRequest r, @Nullable final PendingIntent pi,
@@ -6130,7 +6129,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
        for (final NetworkRequestInfo nri : mDefaultNetworkRequests) {
            // Currently, all network requests will have the same uids therefore checking the first
            // one is sufficient. If/when uids are tracked at the nri level, this can change.
            final Set<UidRange> uids = nri.mRequests.get(0).networkCapabilities.getUids();
            final Set<UidRange> uids = nri.mRequests.get(0).networkCapabilities.getUidRanges();
            if (null == uids) {
                continue;
            }
@@ -6571,7 +6570,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
            return;
        }

        final Set<UidRange> ranges = nai.networkCapabilities.getUids();
        final Set<UidRange> ranges = nai.networkCapabilities.getUidRanges();
        final int vpnAppUid = nai.networkCapabilities.getOwnerUid();
        // TODO: this create a window of opportunity for apps to receive traffic between the time
        // when the old rules are removed and the time when new rules are added. To fix this,
@@ -6936,8 +6935,8 @@ public class ConnectivityService extends IConnectivityManager.Stub

    private void updateUids(NetworkAgentInfo nai, NetworkCapabilities prevNc,
            NetworkCapabilities newNc) {
        Set<UidRange> prevRanges = null == prevNc ? null : prevNc.getUids();
        Set<UidRange> newRanges = null == newNc ? null : newNc.getUids();
        Set<UidRange> prevRanges = null == prevNc ? null : prevNc.getUidRanges();
        Set<UidRange> newRanges = null == newNc ? null : newNc.getUidRanges();
        if (null == prevRanges) prevRanges = new ArraySet<>();
        if (null == newRanges) newRanges = new ArraySet<>();
        final Set<UidRange> prevRangesCopy = new ArraySet<>(prevRanges);
@@ -9268,7 +9267,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
            final ArrayList<NetworkRequest> nrs = new ArrayList<>();
            nrs.add(createNetworkRequest(NetworkRequest.Type.REQUEST, pref.capabilities));
            nrs.add(createDefaultRequest());
            setNetworkRequestUids(nrs, pref.capabilities.getUids());
            setNetworkRequestUids(nrs, UidRange.fromIntRanges(pref.capabilities.getUids()));
            final NetworkRequestInfo nri = new NetworkRequestInfo(nrs);
            result.add(nri);
        }
@@ -9484,9 +9483,8 @@ public class ConnectivityService extends IConnectivityManager.Stub

    private static void setNetworkRequestUids(@NonNull final List<NetworkRequest> requests,
            @NonNull final Set<UidRange> uids) {
        final Set<UidRange> ranges = new ArraySet<>(uids);
        for (final NetworkRequest req : requests) {
            req.networkCapabilities.setUids(ranges);
            req.networkCapabilities.setUids(UidRange.toIntRanges(uids));
        }
    }

Loading