diff --git a/api/current.txt b/api/current.txt index 6e0ca5de25755..f773094fe72f4 100644 --- a/api/current.txt +++ b/api/current.txt @@ -29677,8 +29677,8 @@ package android.net.wifi.aware { public class DiscoverySession implements java.lang.AutoCloseable { method public void close(); - method public 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 createNetworkSpecifierOpen(android.net.wifi.aware.PeerHandle); + 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[]); } @@ -29763,6 +29763,14 @@ package android.net.wifi.aware { 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 { method public int describeContents(); method public java.net.Inet6Address getPeerIpv6Addr(); diff --git a/api/system-current.txt b/api/system-current.txt index 6bb766efb3731..017df40b700a2 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -3888,7 +3888,11 @@ package android.net.wifi { package android.net.wifi.aware { 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 { diff --git a/wifi/java/android/net/wifi/aware/DiscoverySession.java b/wifi/java/android/net/wifi/aware/DiscoverySession.java index 699f54cf13fb3..a47e70b6f30fa 100644 --- a/wifi/java/android/net/wifi/aware/DiscoverySession.java +++ b/wifi/java/android/net/wifi/aware/DiscoverySession.java @@ -34,11 +34,11 @@ import java.lang.ref.WeakReference; * {@link PublishDiscoverySession} and {@link SubscribeDiscoverySession}. This * class provides functionality common to both publish and subscribe discovery sessions: *
* The {@link #close()} method must be called to destroy discovery sessions once they are * no longer needed. */ @@ -270,6 +270,7 @@ public class DiscoverySession implements AutoCloseable { *
* To set up an encrypted link use the * {@link #createNetworkSpecifierPassphrase(PeerHandle, String)} API. + * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}. * * @param peerHandle The peer's handle obtained through * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, byte[], java.util.List)} @@ -284,6 +285,7 @@ public class DiscoverySession implements AutoCloseable { * android.net.ConnectivityManager.NetworkCallback)} * [or other varieties of that API]. */ + @Deprecated public NetworkSpecifier createNetworkSpecifierOpen(@NonNull PeerHandle peerHandle) { if (mTerminated) { Log.w(TAG, "createNetworkSpecifierOpen: called on terminated session"); @@ -318,6 +320,7 @@ public class DiscoverySession implements AutoCloseable { *
* 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}. * * @param peerHandle The peer's handle obtained through * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, @@ -336,6 +339,7 @@ public class DiscoverySession implements AutoCloseable { * android.net.ConnectivityManager.NetworkCallback)} * [or other varieties of that API]. */ + @Deprecated public NetworkSpecifier createNetworkSpecifierPassphrase( @NonNull PeerHandle peerHandle, @NonNull String passphrase) { if (!WifiAwareUtils.validatePassphrase(passphrase)) { @@ -376,6 +380,7 @@ public class DiscoverySession implements AutoCloseable { *
* 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}. * * @param peerHandle The peer's handle obtained through * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, @@ -397,6 +402,7 @@ public class DiscoverySession implements AutoCloseable { * * @hide */ + @Deprecated @SystemApi public NetworkSpecifier createNetworkSpecifierPmk(@NonNull PeerHandle peerHandle, @NonNull byte[] pmk) { diff --git a/wifi/java/android/net/wifi/aware/WifiAwareManager.java b/wifi/java/android/net/wifi/aware/WifiAwareManager.java index 8529a89a99149..26a6c08bee290 100644 --- a/wifi/java/android/net/wifi/aware/WifiAwareManager.java +++ b/wifi/java/android/net/wifi/aware/WifiAwareManager.java @@ -21,6 +21,7 @@ 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; @@ -57,11 +58,7 @@ import java.util.List; * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback, Handler)}. *
* Aware may not be usable when Wi-Fi is disabled (and other conditions). To validate that @@ -110,10 +107,7 @@ import java.util.List; *
+ * 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. + *
+ * 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, + *
+ * 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}. + *
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()); + } + } } diff --git a/wifi/java/android/net/wifi/aware/WifiAwareSession.java b/wifi/java/android/net/wifi/aware/WifiAwareSession.java index 321965330e0b2..5f8841cb01487 100644 --- a/wifi/java/android/net/wifi/aware/WifiAwareSession.java +++ b/wifi/java/android/net/wifi/aware/WifiAwareSession.java @@ -213,7 +213,7 @@ public class WifiAwareSession implements AutoCloseable { * 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 - * when using Aware discovery use the alternative network specifier method - - * {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)}. + * {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}. *
* To set up an encrypted link use the
* {@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
* (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 -
- * {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}.
+ * {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}.
*
* @param role The role of this device:
* {@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
* (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 -
- * {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}.
+ * {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}.
*
* @param role The role of this device:
* {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR} or
diff --git a/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java b/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java
index fd383ef6fd388..45e17201bd1f7 100644
--- a/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java
+++ b/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java
@@ -108,7 +108,7 @@ public class WifiAwareManagerTest {
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
- mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.P;
+ mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.Q;
when(mockPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(
mockApplicationInfo);
when(mockContext.getOpPackageName()).thenReturn("XXX");
@@ -918,6 +918,8 @@ public class WifiAwareManagerTest {
final ConfigRequest configRequest = new ConfigRequest.Builder().build();
final PublishConfig publishConfig = new PublishConfig.Builder().build();
+ mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.P;
+
ArgumentCaptor