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

Commit ef02883b authored by Etan Cohen's avatar Etan Cohen Committed by Android (Google) Code Review
Browse files

Merge "[AWARE] Add builder pattern for Wi-Fi Aware NetworkSpecifier"

parents e48e378b 8690e5d9
Loading
Loading
Loading
Loading
+10 −2
Original line number Original line Diff line number Diff line
@@ -29677,8 +29677,8 @@ package android.net.wifi.aware {
  public class DiscoverySession implements java.lang.AutoCloseable {
  public class DiscoverySession implements java.lang.AutoCloseable {
    method public void close();
    method public void close();
    method public android.net.NetworkSpecifier createNetworkSpecifierOpen(android.net.wifi.aware.PeerHandle);
    method public deprecated android.net.NetworkSpecifier createNetworkSpecifierOpen(android.net.wifi.aware.PeerHandle);
    method public android.net.NetworkSpecifier createNetworkSpecifierPassphrase(android.net.wifi.aware.PeerHandle, java.lang.String);
    method public deprecated android.net.NetworkSpecifier createNetworkSpecifierPassphrase(android.net.wifi.aware.PeerHandle, java.lang.String);
    method public void sendMessage(android.net.wifi.aware.PeerHandle, int, byte[]);
    method public void sendMessage(android.net.wifi.aware.PeerHandle, int, byte[]);
  }
  }
@@ -29763,6 +29763,14 @@ package android.net.wifi.aware {
    field public static final int WIFI_AWARE_DATA_PATH_ROLE_RESPONDER = 1; // 0x1
    field public static final int WIFI_AWARE_DATA_PATH_ROLE_RESPONDER = 1; // 0x1
  }
  }
  public static class WifiAwareManager.NetworkSpecifierBuilder {
    ctor public WifiAwareManager.NetworkSpecifierBuilder();
    method public android.net.NetworkSpecifier build();
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setDiscoverySession(android.net.wifi.aware.DiscoverySession);
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPeerHandle(android.net.wifi.aware.PeerHandle);
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPskPassphrase(java.lang.String);
  }
  public final class WifiAwareNetworkInfo implements android.os.Parcelable android.net.TransportInfo {
  public final class WifiAwareNetworkInfo implements android.os.Parcelable android.net.TransportInfo {
    method public int describeContents();
    method public int describeContents();
    method public java.net.Inet6Address getPeerIpv6Addr();
    method public java.net.Inet6Address getPeerIpv6Addr();
+5 −1
Original line number Original line Diff line number Diff line
@@ -3888,7 +3888,11 @@ package android.net.wifi {
package android.net.wifi.aware {
package android.net.wifi.aware {


  public class DiscoverySession implements java.lang.AutoCloseable {
  public class DiscoverySession implements java.lang.AutoCloseable {
    method public android.net.NetworkSpecifier createNetworkSpecifierPmk(android.net.wifi.aware.PeerHandle, byte[]);
    method public deprecated android.net.NetworkSpecifier createNetworkSpecifierPmk(android.net.wifi.aware.PeerHandle, byte[]);
  }

  public static class WifiAwareManager.NetworkSpecifierBuilder {
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPmk(byte[]);
  }
  }


  public class WifiAwareSession implements java.lang.AutoCloseable {
  public class WifiAwareSession implements java.lang.AutoCloseable {
+10 −4
Original line number Original line Diff line number Diff line
@@ -35,10 +35,10 @@ import java.lang.ref.WeakReference;
 * class provides functionality common to both publish and subscribe discovery sessions:
 * class provides functionality common to both publish and subscribe discovery sessions:
 * <ul>
 * <ul>
 *      <li>Sending messages: {@link #sendMessage(PeerHandle, int, byte[])} method.
 *      <li>Sending messages: {@link #sendMessage(PeerHandle, int, byte[])} method.
 *     <li>Creating a network-specifier when requesting a Aware connection:
 *      <li>Creating a network-specifier when requesting a Aware connection using
 *     {@link #createNetworkSpecifierOpen(PeerHandle)} or
 *      {@link WifiAwareManager.NetworkSpecifierBuilder}.
 *     {@link #createNetworkSpecifierPassphrase(PeerHandle, String)}.
 * </ul>
 * </ul>
 * <p>
 * The {@link #close()} method must be called to destroy discovery sessions once they are
 * The {@link #close()} method must be called to destroy discovery sessions once they are
 * no longer needed.
 * no longer needed.
 */
 */
@@ -270,6 +270,7 @@ public class DiscoverySession implements AutoCloseable {
     * <p>
     * <p>
     * To set up an encrypted link use the
     * To set up an encrypted link use the
     * {@link #createNetworkSpecifierPassphrase(PeerHandle, String)} API.
     * {@link #createNetworkSpecifierPassphrase(PeerHandle, String)} API.
     * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}.
     *
     *
     * @param peerHandle The peer's handle obtained through
     * @param peerHandle The peer's handle obtained through
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, byte[], java.util.List)}
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, byte[], java.util.List)}
@@ -284,6 +285,7 @@ public class DiscoverySession implements AutoCloseable {
     * android.net.ConnectivityManager.NetworkCallback)}
     * android.net.ConnectivityManager.NetworkCallback)}
     * [or other varieties of that API].
     * [or other varieties of that API].
     */
     */
    @Deprecated
    public NetworkSpecifier createNetworkSpecifierOpen(@NonNull PeerHandle peerHandle) {
    public NetworkSpecifier createNetworkSpecifierOpen(@NonNull PeerHandle peerHandle) {
        if (mTerminated) {
        if (mTerminated) {
            Log.w(TAG, "createNetworkSpecifierOpen: called on terminated session");
            Log.w(TAG, "createNetworkSpecifierOpen: called on terminated session");
@@ -318,6 +320,7 @@ public class DiscoverySession implements AutoCloseable {
     * <p>
     * <p>
     * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR
     * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR
     * and a Publisher is a RESPONDER.
     * and a Publisher is a RESPONDER.
     * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}.
     *
     *
     * @param peerHandle The peer's handle obtained through
     * @param peerHandle The peer's handle obtained through
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle,
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle,
@@ -336,6 +339,7 @@ public class DiscoverySession implements AutoCloseable {
     * android.net.ConnectivityManager.NetworkCallback)}
     * android.net.ConnectivityManager.NetworkCallback)}
     * [or other varieties of that API].
     * [or other varieties of that API].
     */
     */
    @Deprecated
    public NetworkSpecifier createNetworkSpecifierPassphrase(
    public NetworkSpecifier createNetworkSpecifierPassphrase(
            @NonNull PeerHandle peerHandle, @NonNull String passphrase) {
            @NonNull PeerHandle peerHandle, @NonNull String passphrase) {
        if (!WifiAwareUtils.validatePassphrase(passphrase)) {
        if (!WifiAwareUtils.validatePassphrase(passphrase)) {
@@ -376,6 +380,7 @@ public class DiscoverySession implements AutoCloseable {
     * <p>
     * <p>
     * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR
     * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR
     * and a Publisher is a RESPONDER.
     * and a Publisher is a RESPONDER.
     * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}.
     *
     *
     * @param peerHandle The peer's handle obtained through
     * @param peerHandle The peer's handle obtained through
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle,
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle,
@@ -397,6 +402,7 @@ public class DiscoverySession implements AutoCloseable {
     *
     *
     * @hide
     * @hide
     */
     */
    @Deprecated
    @SystemApi
    @SystemApi
    public NetworkSpecifier createNetworkSpecifierPmk(@NonNull PeerHandle peerHandle,
    public NetworkSpecifier createNetworkSpecifierPmk(@NonNull PeerHandle peerHandle,
            @NonNull byte[] pmk) {
            @NonNull byte[] pmk) {
+139 −13
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.SystemService;
import android.content.Context;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager;
@@ -57,11 +58,7 @@ import java.util.List;
 * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback, Handler)}.
 * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback, Handler)}.
 * <li>Create a Aware network specifier to be used with
 * <li>Create a Aware network specifier to be used with
 * {@link ConnectivityManager#requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)}
 * {@link ConnectivityManager#requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)}
 * to set-up a Aware connection with a peer. Refer to
 * to set-up a Aware connection with a peer. Refer to {@link NetworkSpecifierBuilder}.
 * {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)},
 * {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)},
 * {@link WifiAwareSession#createNetworkSpecifierOpen(int, byte[])}, and
 * {@link WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)}.
 * </ul>
 * </ul>
 * <p>
 * <p>
 *     Aware may not be usable when Wi-Fi is disabled (and other conditions). To validate that
 *     Aware may not be usable when Wi-Fi is disabled (and other conditions). To validate that
@@ -110,10 +107,7 @@ import java.util.List;
 *        <li>{@link NetworkRequest.Builder#addTransportType(int)} of
 *        <li>{@link NetworkRequest.Builder#addTransportType(int)} of
 *        {@link android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE}.
 *        {@link android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE}.
 *        <li>{@link NetworkRequest.Builder#setNetworkSpecifier(String)} using
 *        <li>{@link NetworkRequest.Builder#setNetworkSpecifier(String)} using
 *        {@link WifiAwareSession#createNetworkSpecifierOpen(int, byte[])},
 *        {@link NetworkSpecifierBuilder}.
 *        {@link WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)},
 *        {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)}, or
 *        {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}.
 *    </ul>
 *    </ul>
 */
 */
@SystemService(Context.WIFI_AWARE_SERVICE)
@SystemService(Context.WIFI_AWARE_SERVICE)
@@ -145,8 +139,6 @@ public class WifiAwareManager {
     * Connection creation role is that of INITIATOR. Used to create a network specifier string
     * Connection creation role is that of INITIATOR. Used to create a network specifier string
     * when requesting a Aware network.
     * when requesting a Aware network.
     *
     *
     * @see DiscoverySession#createNetworkSpecifierOpen(PeerHandle)
     * @see DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)
     * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[])
     * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[])
     * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)
     * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)
     */
     */
@@ -156,8 +148,6 @@ public class WifiAwareManager {
     * Connection creation role is that of RESPONDER. Used to create a network specifier string
     * Connection creation role is that of RESPONDER. Used to create a network specifier string
     * when requesting a Aware network.
     * when requesting a Aware network.
     *
     *
     * @see DiscoverySession#createNetworkSpecifierOpen(PeerHandle)
     * @see DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)
     * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[])
     * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[])
     * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)
     * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)
     */
     */
@@ -415,6 +405,11 @@ public class WifiAwareManager {
                    + ", passphrase=" + ((passphrase == null) ? "null" : "non-null"));
                    + ", passphrase=" + ((passphrase == null) ? "null" : "non-null"));
        }
        }


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

        if (role != WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
        if (role != WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
                && role != WIFI_AWARE_DATA_PATH_ROLE_RESPONDER) {
                && role != WIFI_AWARE_DATA_PATH_ROLE_RESPONDER) {
            throw new IllegalArgumentException(
            throw new IllegalArgumentException(
@@ -813,4 +808,135 @@ public class WifiAwareManager {
            mOriginalCallback.onSessionTerminated();
            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 class NetworkSpecifierBuilder {
        private DiscoverySession mDiscoverySession;
        private PeerHandle mPeerHandle;
        private String mPskPassphrase;
        private byte[] mPmk;

        /**
         * 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;
        }

        /**
         * 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 (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, Process.myUid());
        }
    }
}
}
+3 −3
Original line number Original line Diff line number Diff line
@@ -213,7 +213,7 @@ public class WifiAwareSession implements AutoCloseable {
     *     This API is targeted for applications which can obtain the peer MAC address using OOB
     *     This API is targeted for applications which can obtain the peer MAC address using OOB
     *     (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
     *     (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
     *     when using Aware discovery use the alternative network specifier method -
     *     when using Aware discovery use the alternative network specifier method -
     *     {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)}.
     *     {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}.
     * <p>
     * <p>
     * To set up an encrypted link use the
     * To set up an encrypted link use the
     * {@link #createNetworkSpecifierPassphrase(int, byte[], String)} API.
     * {@link #createNetworkSpecifierPassphrase(int, byte[], String)} API.
@@ -254,7 +254,7 @@ public class WifiAwareSession implements AutoCloseable {
     *     This API is targeted for applications which can obtain the peer MAC address using OOB
     *     This API is targeted for applications which can obtain the peer MAC address using OOB
     *     (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
     *     (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
     *     when using Aware discovery use the alternative network specifier method -
     *     when using Aware discovery use the alternative network specifier method -
     *     {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}.
     *     {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}.
     *
     *
     * @param role  The role of this device:
     * @param role  The role of this device:
     *              {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR} or
     *              {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR} or
@@ -300,7 +300,7 @@ public class WifiAwareSession implements AutoCloseable {
     *     This API is targeted for applications which can obtain the peer MAC address using OOB
     *     This API is targeted for applications which can obtain the peer MAC address using OOB
     *     (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
     *     (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
     *     when using Aware discovery use the alternative network specifier method -
     *     when using Aware discovery use the alternative network specifier method -
     *     {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}.
     *     {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}.
     *
     *
     * @param role  The role of this device:
     * @param role  The role of this device:
     *              {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR} or
     *              {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR} or
Loading