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

Commit d98f26e4 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topics 'network_specifier', 'networkspecifier' into oc-dev

* changes:
  [CM] Unhide the NetworkSpecifier as object API
  Make the NetworkSpecifier a class instead of a string.
  Add test coverage for NetworkSpecifiers.
parents 490ef3cd c1b654a0
Loading
Loading
Loading
Loading
+4 −0
Original line number Original line Diff line number Diff line
@@ -25795,6 +25795,10 @@ package android.net {
    method public android.net.NetworkRequest.Builder removeCapability(int);
    method public android.net.NetworkRequest.Builder removeCapability(int);
    method public android.net.NetworkRequest.Builder removeTransportType(int);
    method public android.net.NetworkRequest.Builder removeTransportType(int);
    method public android.net.NetworkRequest.Builder setNetworkSpecifier(java.lang.String);
    method public android.net.NetworkRequest.Builder setNetworkSpecifier(java.lang.String);
    method public android.net.NetworkRequest.Builder setNetworkSpecifier(android.net.NetworkSpecifier);
  }
  public abstract class NetworkSpecifier {
  }
  }
  public class ParseException extends java.lang.RuntimeException {
  public class ParseException extends java.lang.RuntimeException {
+4 −0
Original line number Original line Diff line number Diff line
@@ -28018,6 +28018,7 @@ package android.net {
    method public android.net.NetworkRequest.Builder removeCapability(int);
    method public android.net.NetworkRequest.Builder removeCapability(int);
    method public android.net.NetworkRequest.Builder removeTransportType(int);
    method public android.net.NetworkRequest.Builder removeTransportType(int);
    method public android.net.NetworkRequest.Builder setNetworkSpecifier(java.lang.String);
    method public android.net.NetworkRequest.Builder setNetworkSpecifier(java.lang.String);
    method public android.net.NetworkRequest.Builder setNetworkSpecifier(android.net.NetworkSpecifier);
  }
  }
  public class NetworkScoreManager {
  public class NetworkScoreManager {
@@ -28036,6 +28037,9 @@ package android.net {
    field public static final java.lang.String EXTRA_PACKAGE_NAME = "packageName";
    field public static final java.lang.String EXTRA_PACKAGE_NAME = "packageName";
  }
  }
  public abstract class NetworkSpecifier {
  }
  public class ParseException extends java.lang.RuntimeException {
  public class ParseException extends java.lang.RuntimeException {
    field public java.lang.String response;
    field public java.lang.String response;
  }
  }
+4 −0
Original line number Original line Diff line number Diff line
@@ -25902,6 +25902,10 @@ package android.net {
    method public android.net.NetworkRequest.Builder removeCapability(int);
    method public android.net.NetworkRequest.Builder removeCapability(int);
    method public android.net.NetworkRequest.Builder removeTransportType(int);
    method public android.net.NetworkRequest.Builder removeTransportType(int);
    method public android.net.NetworkRequest.Builder setNetworkSpecifier(java.lang.String);
    method public android.net.NetworkRequest.Builder setNetworkSpecifier(java.lang.String);
    method public android.net.NetworkRequest.Builder setNetworkSpecifier(android.net.NetworkSpecifier);
  }
  public abstract class NetworkSpecifier {
  }
  }
  public class ParseException extends java.lang.RuntimeException {
  public class ParseException extends java.lang.RuntimeException {
+80 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * MatchAllNetworkSpecifier is a marker class used by NetworkFactory classes to indicate
 * that they accept (match) any network specifier in requests.
 *
 * The class must never be used as part of a network request (those semantics aren't specified).
 *
 * @hide
 */
public final class MatchAllNetworkSpecifier extends NetworkSpecifier implements Parcelable {
    /**
     * Utility method which verifies that the ns argument is not a MatchAllNetworkSpecifier and
     * throws an IllegalArgumentException if it is.
     */
    public static void checkNotMatchAllNetworkSpecifier(NetworkSpecifier ns) {
        if (ns instanceof MatchAllNetworkSpecifier) {
            throw new IllegalArgumentException("A MatchAllNetworkSpecifier is not permitted");
        }
    }

    public boolean satisfiedBy(NetworkSpecifier other) {
        /*
         * The method is called by a NetworkRequest to see if it is satisfied by a proposed
         * network (e.g. as offered by a network factory). Since MatchAllNetweorkSpecifier must
         * not be used in network requests this method should never be called.
         */
        throw new IllegalStateException(
                "MatchAllNetworkSpecifier must not be used in NetworkRequests");
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof MatchAllNetworkSpecifier;
    }

    @Override
    public int hashCode() {
        return 0;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // Nothing to write.
    }

    public static final Parcelable.Creator<MatchAllNetworkSpecifier> CREATOR =
            new Parcelable.Creator<MatchAllNetworkSpecifier>() {
        public MatchAllNetworkSpecifier createFromParcel(Parcel in) {
            return new MatchAllNetworkSpecifier();
        }
        public MatchAllNetworkSpecifier[] newArray(int size) {
            return new MatchAllNetworkSpecifier[size];
        }
    };
}
+26 −42
Original line number Original line Diff line number Diff line
@@ -18,9 +18,11 @@ package android.net;


import android.os.Parcel;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable;
import android.text.TextUtils;

import com.android.internal.util.BitUtils;
import com.android.internal.util.BitUtils;


import java.util.Objects;

/**
/**
 * This class represents the capabilities of a network.  This is used both to specify
 * This class represents the capabilities of a network.  This is used both to specify
 * needs to {@link ConnectivityManager} and when inspecting a network.
 * needs to {@link ConnectivityManager} and when inspecting a network.
@@ -33,6 +35,8 @@ import com.android.internal.util.BitUtils;
 * all cellular based connections are metered and all Wi-Fi based connections are not.
 * all cellular based connections are metered and all Wi-Fi based connections are not.
 */
 */
public final class NetworkCapabilities implements Parcelable {
public final class NetworkCapabilities implements Parcelable {
    private static final String TAG = "NetworkCapabilities";

    /**
    /**
     * @hide
     * @hide
     */
     */
@@ -204,19 +208,6 @@ public final class NetworkCapabilities implements Parcelable {
            (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
            (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
            (1 << NET_CAPABILITY_FOREGROUND);
            (1 << NET_CAPABILITY_FOREGROUND);


    /**
     * Network specifier for factories which want to match any network specifier
     * (NS) in a request. Behavior:
     * <li>Empty NS in request matches any network factory NS</li>
     * <li>Empty NS in the network factory NS only matches a request with an
     * empty NS</li>
     * <li>"*" (this constant) NS in the network factory matches requests with
     * any NS</li>
     *
     * @hide
     */
    public static final String MATCH_ALL_REQUESTS_NETWORK_SPECIFIER = "*";

    /**
    /**
     * Network capabilities that are not allowed in NetworkRequests. This exists because the
     * Network capabilities that are not allowed in NetworkRequests. This exists because the
     * NetworkFactory / NetworkAgent model does not deal well with the situation where a
     * NetworkFactory / NetworkAgent model does not deal well with the situation where a
@@ -579,63 +570,56 @@ public final class NetworkCapabilities implements Parcelable {
                this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
                this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
    }
    }


    private String mNetworkSpecifier;
    private NetworkSpecifier mNetworkSpecifier = null;

    /**
    /**
     * Sets the optional bearer specific network specifier.
     * Sets the optional bearer specific network specifier.
     * This has no meaning if a single transport is also not specified, so calling
     * This has no meaning if a single transport is also not specified, so calling
     * this without a single transport set will generate an exception, as will
     * this without a single transport set will generate an exception, as will
     * subsequently adding or removing transports after this is set.
     * subsequently adding or removing transports after this is set.
     * </p>
     * </p>
     * The interpretation of this {@code String} is bearer specific and bearers that use
     *
     * it should document their particulars.  For example, Bluetooth may use some sort of
     * @param networkSpecifier A concrete, parcelable framework class that extends
     * device id while WiFi could used SSID and/or BSSID.  Cellular may use carrier SPN (name)
     *                         NetworkSpecifier.
     * or Subscription ID.
     *
     * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
     *                         specific network specifier where the bearer has a choice of
     *                         networks.
     * @return This NetworkCapabilities instance, to facilitate chaining.
     * @return This NetworkCapabilities instance, to facilitate chaining.
     * @hide
     * @hide
     */
     */
    public NetworkCapabilities setNetworkSpecifier(String networkSpecifier) {
    public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
        if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
        if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
            throw new IllegalStateException("Must have a single transport specified to use " +
            throw new IllegalStateException("Must have a single transport specified to use " +
                    "setNetworkSpecifier");
                    "setNetworkSpecifier");
        }
        }

        mNetworkSpecifier = networkSpecifier;
        mNetworkSpecifier = networkSpecifier;

        return this;
        return this;
    }
    }


    /**
    /**
     * Gets the optional bearer specific network specifier.
     * Gets the optional bearer specific network specifier.
     *
     *
     * @return The optional {@code String} specifying the bearer specific network specifier.
     * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
     *         See {@link #setNetworkSpecifier}.
     *         specifier. See {@link #setNetworkSpecifier}.
     * @hide
     * @hide
     */
     */
    public String getNetworkSpecifier() {
    public NetworkSpecifier getNetworkSpecifier() {
        return mNetworkSpecifier;
        return mNetworkSpecifier;
    }
    }


    private void combineSpecifiers(NetworkCapabilities nc) {
    private void combineSpecifiers(NetworkCapabilities nc) {
        String otherSpecifier = nc.getNetworkSpecifier();
        if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
        if (TextUtils.isEmpty(otherSpecifier)) return;
        if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
            throw new IllegalStateException("Can't combine two networkSpecifiers");
            throw new IllegalStateException("Can't combine two networkSpecifiers");
        }
        }
        setNetworkSpecifier(otherSpecifier);
        setNetworkSpecifier(nc.mNetworkSpecifier);
    }
    }

    private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
    private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
        return (TextUtils.isEmpty(mNetworkSpecifier) ||
        return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
                mNetworkSpecifier.equals(nc.mNetworkSpecifier) ||
                || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
                MATCH_ALL_REQUESTS_NETWORK_SPECIFIER.equals(nc.mNetworkSpecifier));
    }
    }

    private boolean equalsSpecifier(NetworkCapabilities nc) {
    private boolean equalsSpecifier(NetworkCapabilities nc) {
        if (TextUtils.isEmpty(mNetworkSpecifier)) {
        return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
            return TextUtils.isEmpty(nc.mNetworkSpecifier);
        } else {
            return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
        }
    }
    }


    /**
    /**
@@ -797,7 +781,7 @@ public final class NetworkCapabilities implements Parcelable {
                ((int)(mTransportTypes >> 32) * 7) +
                ((int)(mTransportTypes >> 32) * 7) +
                (mLinkUpBandwidthKbps * 11) +
                (mLinkUpBandwidthKbps * 11) +
                (mLinkDownBandwidthKbps * 13) +
                (mLinkDownBandwidthKbps * 13) +
                (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17) +
                Objects.hashCode(mNetworkSpecifier) * 17 +
                (mSignalStrength * 19));
                (mSignalStrength * 19));
    }
    }


@@ -811,7 +795,7 @@ public final class NetworkCapabilities implements Parcelable {
        dest.writeLong(mTransportTypes);
        dest.writeLong(mTransportTypes);
        dest.writeInt(mLinkUpBandwidthKbps);
        dest.writeInt(mLinkUpBandwidthKbps);
        dest.writeInt(mLinkDownBandwidthKbps);
        dest.writeInt(mLinkDownBandwidthKbps);
        dest.writeString(mNetworkSpecifier);
        dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
        dest.writeInt(mSignalStrength);
        dest.writeInt(mSignalStrength);
    }
    }


@@ -825,7 +809,7 @@ public final class NetworkCapabilities implements Parcelable {
                netCap.mTransportTypes = in.readLong();
                netCap.mTransportTypes = in.readLong();
                netCap.mLinkUpBandwidthKbps = in.readInt();
                netCap.mLinkUpBandwidthKbps = in.readInt();
                netCap.mLinkDownBandwidthKbps = in.readInt();
                netCap.mLinkDownBandwidthKbps = in.readInt();
                netCap.mNetworkSpecifier = in.readString();
                netCap.mNetworkSpecifier = in.readParcelable(null);
                netCap.mSignalStrength = in.readInt();
                netCap.mSignalStrength = in.readInt();
                return netCap;
                return netCap;
            }
            }
Loading