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

Commit 0ccb0c81 authored by Etan Cohen's avatar Etan Cohen
Browse files

[AWARE] Move WifiAwareNetworkSpecifier Builder to its parent class

Note: API only change - no functional changes

Move the Builder class and method which generates WifiAwareNetworkSpecifier
to the class it actually builds and have it return that type.

Per API council feedback to match standard builder patterns.

Bug: 126700127
Test: builds + make docs
Test: atest com.android.server.wifi.aware
Test: atest android.net.wifi.aware
Test: atest SingleDeviceTest (CTS)
Change-Id: If4d58e96b4a268df7afe58bc3d883b6c54ec889d
parent 3057634f
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
@@ -30255,16 +30255,6 @@ package android.net.wifi.aware {
    field public static final int WIFI_AWARE_DATA_PATH_ROLE_RESPONDER = 1; // 0x1
  }
  public static final class WifiAwareManager.NetworkSpecifierBuilder {
    ctor public WifiAwareManager.NetworkSpecifierBuilder();
    method @NonNull public android.net.NetworkSpecifier build();
    method @NonNull public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setDiscoverySession(@NonNull android.net.wifi.aware.DiscoverySession);
    method @NonNull public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPeerHandle(@NonNull android.net.wifi.aware.PeerHandle);
    method @NonNull public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPort(int);
    method @NonNull public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPskPassphrase(@NonNull String);
    method @NonNull public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setTransportProtocol(int);
  }
  public final class WifiAwareNetworkInfo implements android.os.Parcelable android.net.TransportInfo {
    method public int describeContents();
    method @Nullable public java.net.Inet6Address getPeerIpv6Addr();
@@ -30274,6 +30264,22 @@ package android.net.wifi.aware {
    field @NonNull public static final android.os.Parcelable.Creator<android.net.wifi.aware.WifiAwareNetworkInfo> CREATOR;
  }
  public final class WifiAwareNetworkSpecifier extends android.net.NetworkSpecifier implements android.os.Parcelable {
    method public int describeContents();
    method public void writeToParcel(android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.net.wifi.aware.WifiAwareNetworkSpecifier> CREATOR;
  }
  public static final class WifiAwareNetworkSpecifier.Builder {
    ctor public WifiAwareNetworkSpecifier.Builder();
    method @NonNull public android.net.wifi.aware.WifiAwareNetworkSpecifier build();
    method @NonNull public android.net.wifi.aware.WifiAwareNetworkSpecifier.Builder setDiscoverySession(@NonNull android.net.wifi.aware.DiscoverySession);
    method @NonNull public android.net.wifi.aware.WifiAwareNetworkSpecifier.Builder setPeerHandle(@NonNull android.net.wifi.aware.PeerHandle);
    method @NonNull public android.net.wifi.aware.WifiAwareNetworkSpecifier.Builder setPort(int);
    method @NonNull public android.net.wifi.aware.WifiAwareNetworkSpecifier.Builder setPskPassphrase(@NonNull String);
    method @NonNull public android.net.wifi.aware.WifiAwareNetworkSpecifier.Builder setTransportProtocol(int);
  }
  public class WifiAwareSession implements java.lang.AutoCloseable {
    method public void close();
    method public android.net.NetworkSpecifier createNetworkSpecifierOpen(int, @NonNull byte[]);
+2 −2
Original line number Diff line number Diff line
@@ -4981,8 +4981,8 @@ package android.net.wifi.aware {
    method @Deprecated public android.net.NetworkSpecifier createNetworkSpecifierPmk(@NonNull android.net.wifi.aware.PeerHandle, @NonNull byte[]);
  }
  public static final class WifiAwareManager.NetworkSpecifierBuilder {
    method @NonNull public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPmk(@NonNull byte[]);
  public static final class WifiAwareNetworkSpecifier.Builder {
    method @NonNull public android.net.wifi.aware.WifiAwareNetworkSpecifier.Builder setPmk(@NonNull byte[]);
  }
  public class WifiAwareSession implements java.lang.AutoCloseable {
+4 −4
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ import java.lang.ref.WeakReference;
 * <ul>
 *      <li>Sending messages: {@link #sendMessage(PeerHandle, int, byte[])} method.
 *      <li>Creating a network-specifier when requesting a Aware connection using
 *      {@link WifiAwareManager.NetworkSpecifierBuilder}.
 *      {@link WifiAwareNetworkSpecifier.Builder}.
 * </ul>
 * <p>
 * The {@link #close()} method must be called to destroy discovery sessions once they are
@@ -270,7 +270,7 @@ public class DiscoverySession implements AutoCloseable {
     * <p>
     * To set up an encrypted link use the
     * {@link #createNetworkSpecifierPassphrase(PeerHandle, String)} API.
     * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}.
     * @deprecated Use the replacement {@link WifiAwareNetworkSpecifier.Builder}.
     *
     * @param peerHandle The peer's handle obtained through
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, byte[], java.util.List)}
@@ -320,7 +320,7 @@ public class DiscoverySession implements AutoCloseable {
     * <p>
     * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR
     * and a Publisher is a RESPONDER.
     * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}.
     * @deprecated Use the replacement {@link WifiAwareNetworkSpecifier.Builder}.
     *
     * @param peerHandle The peer's handle obtained through
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle,
@@ -380,7 +380,7 @@ public class DiscoverySession implements AutoCloseable {
     * <p>
     * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR
     * and a Publisher is a RESPONDER.
     * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}.
     * @deprecated Use the replacement {@link WifiAwareNetworkSpecifier.Builder}.
     *
     * @param peerHandle The peer's handle obtained through
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle,
+3 −199
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.content.Context;
import android.net.ConnectivityManager;
@@ -35,7 +34,6 @@ import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import libcore.util.HexEncoding;
@@ -59,7 +57,7 @@ import java.util.List;
 * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback, Handler)}.
 * <li>Create a Aware network specifier to be used with
 * {@link ConnectivityManager#requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)}
 * to set-up a Aware connection with a peer. Refer to {@link NetworkSpecifierBuilder}.
 * to set-up a Aware connection with a peer. Refer to {@link WifiAwareNetworkSpecifier.Builder}.
 * </ul>
 * <p>
 *     Aware may not be usable when Wi-Fi is disabled (and other conditions). To validate that
@@ -108,7 +106,7 @@ import java.util.List;
 *        <li>{@link NetworkRequest.Builder#addTransportType(int)} of
 *        {@link android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE}.
 *        <li>{@link NetworkRequest.Builder#setNetworkSpecifier(String)} using
 *        {@link NetworkSpecifierBuilder}.
 *        {@link WifiAwareNetworkSpecifier.Builder}.
 *    </ul>
 */
@SystemService(Context.WIFI_AWARE_SERVICE)
@@ -408,7 +406,7 @@ public class WifiAwareManager {

        if (!WifiAwareUtils.isLegacyVersion(mContext, Build.VERSION_CODES.Q)) {
            throw new UnsupportedOperationException(
                    "API not deprecated - use WifiAwareManager.NetworkSpecifierBuilder");
                    "API not deprecated - use WifiAwareNetworkSpecifier.Builder");
        }

        if (role != WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
@@ -813,198 +811,4 @@ public class WifiAwareManager {
            mOriginalCallback.onSessionTerminated();
        }
    }

    /**
     * A builder class for a Wi-Fi Aware network specifier to set up an Aware connection with a
     * peer.
     * <p>
     * Note that all Wi-Fi Aware connection specifier objects must call the
     * {@link NetworkSpecifierBuilder#setDiscoverySession(DiscoverySession)} to specify the context
     * within which the connection is created, and
     * {@link NetworkSpecifierBuilder#setPeerHandle(PeerHandle)} to specify the peer to which the
     * connection is created.
     */
    public static final class NetworkSpecifierBuilder {
        private DiscoverySession mDiscoverySession;
        private PeerHandle mPeerHandle;
        private String mPskPassphrase;
        private byte[] mPmk;
        private int mPort = 0; // invalid value
        private int mTransportProtocol = -1; // invalid value

        /**
         * Configure the {@link PublishDiscoverySession} or {@link SubscribeDiscoverySession}
         * discovery session in whose context the connection is created.
         * <p>
         * Note: this method must be called for any connection request!
         *
         * @param discoverySession A Wi-Fi Aware discovery session.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setDiscoverySession(
                @NonNull DiscoverySession discoverySession) {
            if (discoverySession == null) {
                throw new IllegalArgumentException("Non-null discoverySession required");
            }
            mDiscoverySession = discoverySession;
            return this;
        }

        /**
         * Configure the {@link PeerHandle} of the peer to which the Wi-Fi Aware connection is
         * requested. The peer is discovered through Wi-Fi Aware discovery,
         * <p>
         * Note: this method must be called for any connection request!
         *
         * @param peerHandle The peer's handle obtained through
         * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, byte[], java.util.List)}
         *                   or
         *                   {@link DiscoverySessionCallback#onMessageReceived(PeerHandle, byte[])}.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setPeerHandle(@NonNull PeerHandle peerHandle) {
            if (peerHandle == null) {
                throw new IllegalArgumentException("Non-null peerHandle required");
            }
            mPeerHandle = peerHandle;
            return this;
        }

        /**
         * Configure the PSK Passphrase for the Wi-Fi Aware connection being requested. This method
         * is optional - if not called, then an Open (unencrypted) connection will be created.
         *
         * @param pskPassphrase The (optional) passphrase to be used to encrypt the link.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setPskPassphrase(@NonNull String pskPassphrase) {
            if (!WifiAwareUtils.validatePassphrase(pskPassphrase)) {
                throw new IllegalArgumentException("Passphrase must meet length requirements");
            }
            mPskPassphrase = pskPassphrase;
            return this;
        }

        /**
         * Configure the PMK for the Wi-Fi Aware connection being requested. This method
         * is optional - if not called, then an Open (unencrypted) connection will be created.
         *
         * @param pmk A PMK (pairwise master key, see IEEE 802.11i) specifying the key to use for
         *            encrypting the data-path. Use the {@link #setPskPassphrase(String)} to
         *            specify a Passphrase.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         * @hide
         */
        @SystemApi
        public @NonNull NetworkSpecifierBuilder setPmk(@NonNull byte[] pmk) {
            if (!WifiAwareUtils.validatePmk(pmk)) {
                throw new IllegalArgumentException("PMK must 32 bytes");
            }
            mPmk = pmk;
            return this;
        }

        /**
         * Configure the port number which will be used to create a connection over this link. This
         * configuration should only be done on the server device, e.g. the device creating the
         * {@link java.net.ServerSocket}.
         * <p>Notes:
         * <ul>
         *     <li>The server device must be the Publisher device!
         *     <li>The port information can only be specified on secure links, specified using
         *     {@link #setPskPassphrase(String)}.
         * </ul>
         *
         * @param port A positive integer indicating the port to be used for communication.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setPort(int port) {
            if (port <= 0 || port > 65535) {
                throw new IllegalArgumentException("The port must be a positive value (0, 65535]");
            }
            mPort = port;
            return this;
        }

        /**
         * Configure the transport protocol which will be used to create a connection over this
         * link. This configuration should only be done on the server device, e.g. the device
         * creating the {@link java.net.ServerSocket} for TCP.
         * <p>Notes:
         * <ul>
         *     <li>The server device must be the Publisher device!
         *     <li>The transport protocol information can only be specified on secure links,
         *     specified using {@link #setPskPassphrase(String)}.
         * </ul>
         * The transport protocol number is assigned by the Internet Assigned Numbers Authority
         * (IANA) https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml.
         *
         * @param transportProtocol The transport protocol to be used for communication.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setTransportProtocol(int transportProtocol) {
            if (transportProtocol < 0 || transportProtocol > 255) {
                throw new IllegalArgumentException(
                        "The transport protocol must be in range [0, 255]");
            }
            mTransportProtocol = transportProtocol;
            return this;
        }

        /**
         * Create a {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier)}
         * for a WiFi Aware connection (link) to the specified peer. The
         * {@link android.net.NetworkRequest.Builder#addTransportType(int)} should be set to
         * {@link android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE}.
         * <p> The default builder constructor will initialize a NetworkSpecifier which requests an
         * open (non-encrypted) link. To request an encrypted link use the
         * {@link #setPskPassphrase(String)} builder method.
         *
         * @return A {@link NetworkSpecifier} to be used to construct
         * {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier)} to pass
         * to {@link android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest,
         * android.net.ConnectivityManager.NetworkCallback)}
         * [or other varieties of that API].
         */
        public @NonNull NetworkSpecifier build() {
            if (mDiscoverySession == null) {
                throw new IllegalStateException("Null discovery session!?");
            }
            if (mPskPassphrase != null & mPmk != null) {
                throw new IllegalStateException(
                        "Can only specify a Passphrase or a PMK - not both!");
            }

            int role = mDiscoverySession instanceof SubscribeDiscoverySession
                    ? WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
                    : WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER;

            if (mPort != 0 || mTransportProtocol != -1) {
                if (role != WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER) {
                    throw new IllegalStateException(
                            "Port and transport protocol information can only "
                                    + "be specified on the Publisher device (which is the server");
                }
                if (TextUtils.isEmpty(mPskPassphrase) && mPmk == null) {
                    throw new IllegalStateException("Port and transport protocol information can "
                            + "only be specified on a secure link");
                }
            }

            if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR && mPeerHandle == null) {
                throw new IllegalStateException("Null peerHandle!?");
            }

            return new WifiAwareNetworkSpecifier(
                    WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_IB, role,
                    mDiscoverySession.mClientId, mDiscoverySession.mSessionId, mPeerHandle.peerId,
                    null, mPmk, mPskPassphrase, mPort, mTransportProtocol, Process.myUid());
        }
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ public final class WifiAwareNetworkInfo implements TransportInfo, Parcelable {
    /**
     * Get the port number to be used to create a network connection to the Wi-Fi Aware peer.
     * The port information is provided by the app running on the peer which requested the
     * connection, using the {@link WifiAwareManager.NetworkSpecifierBuilder#setPort(int)}.
     * connection, using the {@link WifiAwareNetworkSpecifier.Builder#setPort(int)}.
     *
     * @return A port number on the peer. A value of 0 indicates that no port was specified by the
     *         peer.
@@ -88,7 +88,7 @@ public final class WifiAwareNetworkInfo implements TransportInfo, Parcelable {
     * Get the transport protocol to be used to communicate over a network connection to the Wi-Fi
     * Aware peer. The transport protocol is provided by the app running on the peer which requested
     * the connection, using the
     * {@link WifiAwareManager.NetworkSpecifierBuilder#setTransportProtocol(int)}.
     * {@link WifiAwareNetworkSpecifier.Builder#setTransportProtocol(int)}.
     * <p>
     * The transport protocol number is assigned by the Internet Assigned Numbers Authority
     * (IANA) https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml.
Loading