Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 17 | package android.net.wifi.aware; |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 18 | |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Etan Cohen | 5e680da | 2016-04-06 15:01:48 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
Roshan Pius | d0f5475 | 2019-12-05 10:19:39 -0800 | [diff] [blame] | 22 | import android.annotation.RequiresPermission; |
Etan Cohen | 5f47bf2 | 2016-03-23 11:40:26 -0700 | [diff] [blame] | 23 | import android.annotation.SdkConstant; |
| 24 | import android.annotation.SdkConstant.SdkConstantType; |
Etan Cohen | 88aa12b | 2017-11-08 08:41:39 -0800 | [diff] [blame] | 25 | import android.annotation.SystemService; |
Etan Cohen | 4503dc6 | 2016-08-11 09:31:21 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Etan Cohen | ee58a68 | 2016-07-15 15:28:39 -0700 | [diff] [blame] | 27 | import android.net.ConnectivityManager; |
| 28 | import android.net.NetworkRequest; |
Etan Cohen | 6850728 | 2017-03-30 08:54:39 -0700 | [diff] [blame] | 29 | import android.net.NetworkSpecifier; |
David Su | 5b62f3d | 2019-12-06 12:02:51 -0800 | [diff] [blame] | 30 | import android.net.wifi.util.HexEncoding; |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 31 | import android.os.Binder; |
Etan Cohen | e43df4d | 2018-02-22 15:54:20 -0800 | [diff] [blame] | 32 | import android.os.Build; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
| 34 | import android.os.Handler; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 35 | import android.os.Looper; |
| 36 | import android.os.Message; |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 37 | import android.os.RemoteException; |
| 38 | import android.util.Log; |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 39 | |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 40 | import java.lang.annotation.Retention; |
| 41 | import java.lang.annotation.RetentionPolicy; |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 42 | import java.lang.ref.WeakReference; |
Etan Cohen | 1857b98 | 2016-11-23 11:11:23 -0800 | [diff] [blame] | 43 | import java.nio.BufferOverflowException; |
Etan Cohen | 1857b98 | 2016-11-23 11:11:23 -0800 | [diff] [blame] | 44 | import java.util.List; |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 45 | |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 46 | /** |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 47 | * This class provides the primary API for managing Wi-Fi Aware operations: |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 48 | * discovery and peer-to-peer data connections. |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 49 | * <p> |
| 50 | * The class provides access to: |
| 51 | * <ul> |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 52 | * <li>Initialize a Aware cluster (peer-to-peer synchronization). Refer to |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 53 | * {@link #attach(AttachCallback, Handler)}. |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 54 | * <li>Create discovery sessions (publish or subscribe sessions). Refer to |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 55 | * {@link WifiAwareSession#publish(PublishConfig, DiscoverySessionCallback, Handler)} and |
| 56 | * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback, Handler)}. |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 57 | * <li>Create a Aware network specifier to be used with |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 58 | * {@link ConnectivityManager#requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)} |
Etan Cohen | 0ccb0c8 | 2019-03-10 18:09:17 -0700 | [diff] [blame] | 59 | * to set-up a Aware connection with a peer. Refer to {@link WifiAwareNetworkSpecifier.Builder}. |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 60 | * </ul> |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 61 | * <p> |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 62 | * Aware may not be usable when Wi-Fi is disabled (and other conditions). To validate that |
Etan Cohen | b68ed28 | 2016-09-20 09:23:33 -0700 | [diff] [blame] | 63 | * the functionality is available use the {@link #isAvailable()} function. To track |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 64 | * changes in Aware usability register for the {@link #ACTION_WIFI_AWARE_STATE_CHANGED} |
| 65 | * broadcast. Note that this broadcast is not sticky - you should register for it and then |
| 66 | * check the above API to avoid a race condition. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 67 | * <p> |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 68 | * An application must use {@link #attach(AttachCallback, Handler)} to initialize a |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 69 | * Aware cluster - before making any other Aware operation. Aware cluster membership is a |
| 70 | * device-wide operation - the API guarantees that the device is in a cluster or joins a |
| 71 | * Aware cluster (or starts one if none can be found). Information about attach success (or |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 72 | * failure) are returned in callbacks of {@link AttachCallback}. Proceed with Aware |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 73 | * discovery or connection setup only after receiving confirmation that Aware attach |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 74 | * succeeded - {@link AttachCallback#onAttached(WifiAwareSession)}. When an |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 75 | * application is finished using Aware it <b>must</b> use the |
Etan Cohen | a1d90d9 | 2017-04-20 15:13:24 -0700 | [diff] [blame] | 76 | * {@link WifiAwareSession#close()} API to indicate to the Aware service that the device |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 77 | * may detach from the Aware cluster. The device will actually disable Aware once the last |
| 78 | * application detaches. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 79 | * <p> |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 80 | * Once a Aware attach is confirmed use the |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 81 | * {@link WifiAwareSession#publish(PublishConfig, DiscoverySessionCallback, Handler)} |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 82 | * or |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 83 | * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback, |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 84 | * Handler)} to create publish or subscribe Aware discovery sessions. Events are called on the |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 85 | * provided callback object {@link DiscoverySessionCallback}. Specifically, the |
| 86 | * {@link DiscoverySessionCallback#onPublishStarted(PublishDiscoverySession)} |
Etan Cohen | 3855370 | 2016-09-14 14:41:56 -0700 | [diff] [blame] | 87 | * and |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 88 | * {@link DiscoverySessionCallback#onSubscribeStarted( |
| 89 | *SubscribeDiscoverySession)} |
| 90 | * return {@link PublishDiscoverySession} and |
| 91 | * {@link SubscribeDiscoverySession} |
Etan Cohen | 3855370 | 2016-09-14 14:41:56 -0700 | [diff] [blame] | 92 | * objects respectively on which additional session operations can be performed, e.g. updating |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 93 | * the session {@link PublishDiscoverySession#updatePublish(PublishConfig)} and |
| 94 | * {@link SubscribeDiscoverySession#updateSubscribe(SubscribeConfig)}. Sessions can |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 95 | * also be used to send messages using the |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 96 | * {@link DiscoverySession#sendMessage(PeerHandle, int, byte[])} APIs. When an |
Etan Cohen | 27c18cb | 2016-09-22 08:18:36 -0700 | [diff] [blame] | 97 | * application is finished with a discovery session it <b>must</b> terminate it using the |
Etan Cohen | a1d90d9 | 2017-04-20 15:13:24 -0700 | [diff] [blame] | 98 | * {@link DiscoverySession#close()} API. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 99 | * <p> |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 100 | * Creating connections between Aware devices is managed by the standard |
| 101 | * {@link ConnectivityManager#requestNetwork(NetworkRequest, |
| 102 | * ConnectivityManager.NetworkCallback)}. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 103 | * The {@link NetworkRequest} object should be constructed with: |
| 104 | * <ul> |
| 105 | * <li>{@link NetworkRequest.Builder#addTransportType(int)} of |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 106 | * {@link android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE}. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 107 | * <li>{@link NetworkRequest.Builder#setNetworkSpecifier(String)} using |
Etan Cohen | 0ccb0c8 | 2019-03-10 18:09:17 -0700 | [diff] [blame] | 108 | * {@link WifiAwareNetworkSpecifier.Builder}. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 109 | * </ul> |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 110 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 111 | @SystemService(Context.WIFI_AWARE_SERVICE) |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 112 | public class WifiAwareManager { |
| 113 | private static final String TAG = "WifiAwareManager"; |
Etan Cohen | 7d6c389 | 2016-02-11 14:57:00 -0800 | [diff] [blame] | 114 | private static final boolean DBG = false; |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 115 | private static final boolean VDBG = false; // STOPSHIP if true |
| 116 | |
Etan Cohen | 5f47bf2 | 2016-03-23 11:40:26 -0700 | [diff] [blame] | 117 | /** |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 118 | * Broadcast intent action to indicate that the state of Wi-Fi Aware availability has changed. |
Etan Cohen | 9361799 | 2016-09-21 17:54:35 -0700 | [diff] [blame] | 119 | * Use the {@link #isAvailable()} to query the current status. |
| 120 | * This broadcast is <b>not</b> sticky, use the {@link #isAvailable()} API after registering |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 121 | * the broadcast to check the current state of Wi-Fi Aware. |
Etan Cohen | 4c22e8b | 2016-09-29 16:02:30 -0700 | [diff] [blame] | 122 | * <p>Note: The broadcast is only delivered to registered receivers - no manifest registered |
| 123 | * components will be launched. |
Etan Cohen | 5f47bf2 | 2016-03-23 11:40:26 -0700 | [diff] [blame] | 124 | */ |
| 125 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 126 | public static final String ACTION_WIFI_AWARE_STATE_CHANGED = |
| 127 | "android.net.wifi.aware.action.WIFI_AWARE_STATE_CHANGED"; |
Etan Cohen | 5f47bf2 | 2016-03-23 11:40:26 -0700 | [diff] [blame] | 128 | |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 129 | /** @hide */ |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 130 | @IntDef({ |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 131 | WIFI_AWARE_DATA_PATH_ROLE_INITIATOR, WIFI_AWARE_DATA_PATH_ROLE_RESPONDER}) |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 132 | @Retention(RetentionPolicy.SOURCE) |
| 133 | public @interface DataPathRole { |
| 134 | } |
| 135 | |
| 136 | /** |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 137 | * Connection creation role is that of INITIATOR. Used to create a network specifier string |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 138 | * when requesting a Aware network. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 139 | * |
Etan Cohen | 3a764c1 | 2017-03-08 09:05:02 -0800 | [diff] [blame] | 140 | * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[]) |
| 141 | * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String) |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 142 | */ |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 143 | public static final int WIFI_AWARE_DATA_PATH_ROLE_INITIATOR = 0; |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 146 | * Connection creation role is that of RESPONDER. Used to create a network specifier string |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 147 | * when requesting a Aware network. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 148 | * |
Etan Cohen | 3a764c1 | 2017-03-08 09:05:02 -0800 | [diff] [blame] | 149 | * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[]) |
| 150 | * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String) |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 151 | */ |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 152 | public static final int WIFI_AWARE_DATA_PATH_ROLE_RESPONDER = 1; |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 153 | |
Etan Cohen | 4503dc6 | 2016-08-11 09:31:21 -0700 | [diff] [blame] | 154 | private final Context mContext; |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 155 | private final IWifiAwareManager mService; |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 156 | |
Etan Cohen | 447b032 | 2016-05-09 15:30:06 -0700 | [diff] [blame] | 157 | private final Object mLock = new Object(); // lock access to the following vars |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 158 | |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 159 | /** @hide */ |
David Su | 409f371 | 2019-10-24 11:18:41 -0700 | [diff] [blame] | 160 | public WifiAwareManager(@NonNull Context context, @NonNull IWifiAwareManager service) { |
Etan Cohen | 4503dc6 | 2016-08-11 09:31:21 -0700 | [diff] [blame] | 161 | mContext = context; |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 162 | mService = service; |
| 163 | } |
| 164 | |
| 165 | /** |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 166 | * Returns the current status of Aware API: whether or not Aware is available. To track |
| 167 | * changes in the state of Aware API register for the |
| 168 | * {@link #ACTION_WIFI_AWARE_STATE_CHANGED} broadcast. |
Etan Cohen | 5f47bf2 | 2016-03-23 11:40:26 -0700 | [diff] [blame] | 169 | * |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 170 | * @return A boolean indicating whether the app can use the Aware API at this time (true) or |
Etan Cohen | b68ed28 | 2016-09-20 09:23:33 -0700 | [diff] [blame] | 171 | * not (false). |
Etan Cohen | 5f47bf2 | 2016-03-23 11:40:26 -0700 | [diff] [blame] | 172 | */ |
Etan Cohen | b68ed28 | 2016-09-20 09:23:33 -0700 | [diff] [blame] | 173 | public boolean isAvailable() { |
Etan Cohen | 5f47bf2 | 2016-03-23 11:40:26 -0700 | [diff] [blame] | 174 | try { |
| 175 | return mService.isUsageEnabled(); |
| 176 | } catch (RemoteException e) { |
Etan Cohen | fc3c0a0 | 2016-08-16 11:54:49 -0700 | [diff] [blame] | 177 | throw e.rethrowFromSystemServer(); |
Etan Cohen | 5f47bf2 | 2016-03-23 11:40:26 -0700 | [diff] [blame] | 178 | } |
Etan Cohen | 5f47bf2 | 2016-03-23 11:40:26 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 182 | * Returns the characteristics of the Wi-Fi Aware interface: a set of parameters which specify |
Etan Cohen | e64c62f | 2016-10-03 15:14:51 -0700 | [diff] [blame] | 183 | * limitations on configurations, e.g. the maximum service name length. |
| 184 | * |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 185 | * @return An object specifying configuration limitations of Aware. |
Etan Cohen | e64c62f | 2016-10-03 15:14:51 -0700 | [diff] [blame] | 186 | */ |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 187 | public Characteristics getCharacteristics() { |
Etan Cohen | e64c62f | 2016-10-03 15:14:51 -0700 | [diff] [blame] | 188 | try { |
| 189 | return mService.getCharacteristics(); |
| 190 | } catch (RemoteException e) { |
| 191 | throw e.rethrowFromSystemServer(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 196 | * Attach to the Wi-Fi Aware service - enabling the application to create discovery sessions or |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 197 | * create connections to peers. The device will attach to an existing cluster if it can find |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 198 | * one or create a new cluster (if it is the first to enable Aware in its vicinity). Results |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 199 | * (e.g. successful attach to a cluster) are provided to the {@code attachCallback} object. |
Etan Cohen | a1d90d9 | 2017-04-20 15:13:24 -0700 | [diff] [blame] | 200 | * An application <b>must</b> call {@link WifiAwareSession#close()} when done with the |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 201 | * Wi-Fi Aware object. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 202 | * <p> |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 203 | * Note: a Aware cluster is a shared resource - if the device is already attached to a cluster |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 204 | * then this function will simply indicate success immediately using the same {@code |
| 205 | * attachCallback}. |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 206 | * |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 207 | * @param attachCallback A callback for attach events, extended from |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 208 | * {@link AttachCallback}. |
Etan Cohen | ede1d0f | 2016-09-21 11:18:35 -0700 | [diff] [blame] | 209 | * @param handler The Handler on whose thread to execute the callbacks of the {@code |
| 210 | * attachCallback} object. If a null is provided then the application's main thread will be |
| 211 | * used. |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 212 | */ |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 213 | public void attach(@NonNull AttachCallback attachCallback, @Nullable Handler handler) { |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 214 | attach(handler, null, attachCallback, null); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 215 | } |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 216 | |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 217 | /** |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 218 | * Attach to the Wi-Fi Aware service - enabling the application to create discovery sessions or |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 219 | * create connections to peers. The device will attach to an existing cluster if it can find |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 220 | * one or create a new cluster (if it is the first to enable Aware in its vicinity). Results |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 221 | * (e.g. successful attach to a cluster) are provided to the {@code attachCallback} object. |
Etan Cohen | a1d90d9 | 2017-04-20 15:13:24 -0700 | [diff] [blame] | 222 | * An application <b>must</b> call {@link WifiAwareSession#close()} when done with the |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 223 | * Wi-Fi Aware object. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 224 | * <p> |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 225 | * Note: a Aware cluster is a shared resource - if the device is already attached to a cluster |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 226 | * then this function will simply indicate success immediately using the same {@code |
| 227 | * attachCallback}. |
| 228 | * <p> |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 229 | * This version of the API attaches a listener to receive the MAC address of the Aware interface |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 230 | * on startup and whenever it is updated (it is randomized at regular intervals for privacy). |
Etan Cohen | 12f7053 | 2019-01-23 09:01:46 -0800 | [diff] [blame] | 231 | * The application must have the {@link android.Manifest.permission#ACCESS_FINE_LOCATION} |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 232 | * permission to execute this attach request. Otherwise, use the |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 233 | * {@link #attach(AttachCallback, Handler)} version. Note that aside from permission |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 234 | * requirements this listener will wake up the host at regular intervals causing higher power |
| 235 | * consumption, do not use it unless the information is necessary (e.g. for OOB discovery). |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 236 | * |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 237 | * @param attachCallback A callback for attach events, extended from |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 238 | * {@link AttachCallback}. |
Etan Cohen | ede1d0f | 2016-09-21 11:18:35 -0700 | [diff] [blame] | 239 | * @param identityChangedListener A listener for changed identity, extended from |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 240 | * {@link IdentityChangedListener}. |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 241 | * @param handler The Handler on whose thread to execute the callbacks of the {@code |
| 242 | * attachCallback} and {@code identityChangedListener} objects. If a null is provided then the |
| 243 | * application's main thread will be used. |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 244 | */ |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 245 | public void attach(@NonNull AttachCallback attachCallback, |
| 246 | @NonNull IdentityChangedListener identityChangedListener, |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 247 | @Nullable Handler handler) { |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 248 | attach(handler, null, attachCallback, identityChangedListener); |
| 249 | } |
| 250 | |
| 251 | /** @hide */ |
| 252 | public void attach(Handler handler, ConfigRequest configRequest, |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 253 | AttachCallback attachCallback, |
| 254 | IdentityChangedListener identityChangedListener) { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 255 | if (VDBG) { |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 256 | Log.v(TAG, "attach(): handler=" + handler + ", callback=" + attachCallback |
| 257 | + ", configRequest=" + configRequest + ", identityChangedListener=" |
| 258 | + identityChangedListener); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Etan Cohen | b7abd81 | 2018-01-04 07:34:50 -0800 | [diff] [blame] | 261 | if (attachCallback == null) { |
| 262 | throw new IllegalArgumentException("Null callback provided"); |
| 263 | } |
| 264 | |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 265 | synchronized (mLock) { |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 266 | Looper looper = (handler == null) ? Looper.getMainLooper() : handler.getLooper(); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 267 | |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 268 | try { |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 269 | Binder binder = new Binder(); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 270 | mService.connect(binder, mContext.getOpPackageName(), mContext.getAttributionTag(), |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 271 | new WifiAwareEventCallbackProxy(this, looper, binder, attachCallback, |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 272 | identityChangedListener), configRequest, |
| 273 | identityChangedListener != null); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 274 | } catch (RemoteException e) { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 275 | throw e.rethrowFromSystemServer(); |
| 276 | } |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 280 | /** @hide */ |
| 281 | public void disconnect(int clientId, Binder binder) { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 282 | if (VDBG) Log.v(TAG, "disconnect()"); |
| 283 | |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 284 | try { |
| 285 | mService.disconnect(clientId, binder); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 286 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 287 | throw e.rethrowFromSystemServer(); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 291 | /** @hide */ |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 292 | public void publish(int clientId, Looper looper, PublishConfig publishConfig, |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 293 | DiscoverySessionCallback callback) { |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 294 | if (VDBG) Log.v(TAG, "publish(): clientId=" + clientId + ", config=" + publishConfig); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 295 | |
Etan Cohen | b7abd81 | 2018-01-04 07:34:50 -0800 | [diff] [blame] | 296 | if (callback == null) { |
| 297 | throw new IllegalArgumentException("Null callback provided"); |
| 298 | } |
| 299 | |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 300 | try { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 301 | mService.publish(mContext.getOpPackageName(), mContext.getAttributionTag(), clientId, |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 302 | publishConfig, |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 303 | new WifiAwareDiscoverySessionCallbackProxy(this, looper, true, callback, |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 304 | clientId)); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 305 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 306 | throw e.rethrowFromSystemServer(); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 307 | } |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 310 | /** @hide */ |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 311 | public void updatePublish(int clientId, int sessionId, PublishConfig publishConfig) { |
| 312 | if (VDBG) { |
| 313 | Log.v(TAG, "updatePublish(): clientId=" + clientId + ",sessionId=" + sessionId |
| 314 | + ", config=" + publishConfig); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 315 | } |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 316 | |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 317 | try { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 318 | mService.updatePublish(clientId, sessionId, publishConfig); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 319 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 320 | throw e.rethrowFromSystemServer(); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Etan Cohen | b29bbcc | 2016-02-17 14:25:41 -0800 | [diff] [blame] | 323 | |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 324 | /** @hide */ |
| 325 | public void subscribe(int clientId, Looper looper, SubscribeConfig subscribeConfig, |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 326 | DiscoverySessionCallback callback) { |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 327 | if (VDBG) { |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 328 | if (VDBG) { |
| 329 | Log.v(TAG, |
| 330 | "subscribe(): clientId=" + clientId + ", config=" + subscribeConfig); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 331 | } |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 332 | } |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 333 | |
Etan Cohen | b7abd81 | 2018-01-04 07:34:50 -0800 | [diff] [blame] | 334 | if (callback == null) { |
| 335 | throw new IllegalArgumentException("Null callback provided"); |
| 336 | } |
| 337 | |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 338 | try { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 339 | mService.subscribe(mContext.getOpPackageName(), mContext.getAttributionTag(), clientId, |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 340 | subscribeConfig, |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 341 | new WifiAwareDiscoverySessionCallbackProxy(this, looper, false, callback, |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 342 | clientId)); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 343 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 344 | throw e.rethrowFromSystemServer(); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 345 | } |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 348 | /** @hide */ |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 349 | public void updateSubscribe(int clientId, int sessionId, SubscribeConfig subscribeConfig) { |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 350 | if (VDBG) { |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 351 | Log.v(TAG, "updateSubscribe(): clientId=" + clientId + ",sessionId=" + sessionId |
| 352 | + ", config=" + subscribeConfig); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | try { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 356 | mService.updateSubscribe(clientId, sessionId, subscribeConfig); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 357 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 358 | throw e.rethrowFromSystemServer(); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 362 | /** @hide */ |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 363 | public void terminateSession(int clientId, int sessionId) { |
| 364 | if (VDBG) { |
| 365 | Log.d(TAG, |
| 366 | "terminateSession(): clientId=" + clientId + ", sessionId=" + sessionId); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 369 | try { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 370 | mService.terminateSession(clientId, sessionId); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 371 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 372 | throw e.rethrowFromSystemServer(); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 376 | /** @hide */ |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 377 | public void sendMessage(int clientId, int sessionId, PeerHandle peerHandle, byte[] message, |
Etan Cohen | 27c18cb | 2016-09-22 08:18:36 -0700 | [diff] [blame] | 378 | int messageId, int retryCount) { |
Etan Cohen | f085c19 | 2016-09-27 09:15:46 -0700 | [diff] [blame] | 379 | if (peerHandle == null) { |
| 380 | throw new IllegalArgumentException( |
| 381 | "sendMessage: invalid peerHandle - must be non-null"); |
| 382 | } |
| 383 | |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 384 | if (VDBG) { |
Etan Cohen | 27c18cb | 2016-09-22 08:18:36 -0700 | [diff] [blame] | 385 | Log.v(TAG, "sendMessage(): clientId=" + clientId + ", sessionId=" + sessionId |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 386 | + ", peerHandle=" + peerHandle.peerId + ", messageId=" |
Etan Cohen | f085c19 | 2016-09-27 09:15:46 -0700 | [diff] [blame] | 387 | + messageId + ", retryCount=" + retryCount); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | try { |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 391 | mService.sendMessage(clientId, sessionId, peerHandle.peerId, message, messageId, |
| 392 | retryCount); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 393 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 394 | throw e.rethrowFromSystemServer(); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 397 | |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 398 | /** @hide */ |
Roshan Pius | d0f5475 | 2019-12-05 10:19:39 -0800 | [diff] [blame] | 399 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
| 400 | public void requestMacAddresses(int uid, List<Integer> peerIds, |
| 401 | IWifiAwareMacAddressProvider callback) { |
| 402 | try { |
| 403 | mService.requestMacAddresses(uid, peerIds, callback); |
| 404 | } catch (RemoteException e) { |
| 405 | throw e.rethrowFromSystemServer(); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /** @hide */ |
Etan Cohen | 6850728 | 2017-03-30 08:54:39 -0700 | [diff] [blame] | 410 | public NetworkSpecifier createNetworkSpecifier(int clientId, int role, int sessionId, |
Etan Cohen | 9b78f82 | 2018-01-19 17:21:06 -0800 | [diff] [blame] | 411 | @NonNull PeerHandle peerHandle, @Nullable byte[] pmk, @Nullable String passphrase) { |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 412 | if (VDBG) { |
| 413 | Log.v(TAG, "createNetworkSpecifier: role=" + role + ", sessionId=" + sessionId |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 414 | + ", peerHandle=" + ((peerHandle == null) ? peerHandle : peerHandle.peerId) |
Etan Cohen | 62a2f9f | 2017-02-21 10:08:42 -0800 | [diff] [blame] | 415 | + ", pmk=" + ((pmk == null) ? "null" : "non-null") |
| 416 | + ", passphrase=" + ((passphrase == null) ? "null" : "non-null")); |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Etan Cohen | 8690e5d | 2018-11-27 07:33:43 -0800 | [diff] [blame] | 419 | if (!WifiAwareUtils.isLegacyVersion(mContext, Build.VERSION_CODES.Q)) { |
| 420 | throw new UnsupportedOperationException( |
Etan Cohen | 09231fb | 2019-04-06 10:21:37 -0700 | [diff] [blame] | 421 | "API deprecated - use WifiAwareNetworkSpecifier.Builder"); |
Etan Cohen | 8690e5d | 2018-11-27 07:33:43 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 424 | if (role != WIFI_AWARE_DATA_PATH_ROLE_INITIATOR |
| 425 | && role != WIFI_AWARE_DATA_PATH_ROLE_RESPONDER) { |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 426 | throw new IllegalArgumentException( |
| 427 | "createNetworkSpecifier: Invalid 'role' argument when creating a network " |
| 428 | + "specifier"); |
| 429 | } |
Etan Cohen | e43df4d | 2018-02-22 15:54:20 -0800 | [diff] [blame] | 430 | if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR || !WifiAwareUtils.isLegacyVersion(mContext, |
| 431 | Build.VERSION_CODES.P)) { |
| 432 | if (peerHandle == null) { |
| 433 | throw new IllegalArgumentException( |
| 434 | "createNetworkSpecifier: Invalid peer handle - cannot be null"); |
| 435 | } |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 436 | } |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 437 | |
Etan Cohen | 6850728 | 2017-03-30 08:54:39 -0700 | [diff] [blame] | 438 | return new WifiAwareNetworkSpecifier( |
| 439 | (peerHandle == null) ? WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_IB_ANY_PEER |
| 440 | : WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_IB, |
| 441 | role, |
| 442 | clientId, |
| 443 | sessionId, |
| 444 | peerHandle != null ? peerHandle.peerId : 0, // 0 is an invalid peer ID |
| 445 | null, // peerMac (not used in this method) |
| 446 | pmk, |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 447 | passphrase, |
Etan Cohen | 7faa4fa | 2018-10-17 15:48:39 -0700 | [diff] [blame] | 448 | 0, // no port info for deprecated IB APIs |
Roshan Pius | cb3d652 | 2020-01-16 12:53:01 -0800 | [diff] [blame] | 449 | -1); // no transport info for deprecated IB APIs |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 452 | /** @hide */ |
Etan Cohen | 6850728 | 2017-03-30 08:54:39 -0700 | [diff] [blame] | 453 | public NetworkSpecifier createNetworkSpecifier(int clientId, @DataPathRole int role, |
Etan Cohen | 9b78f82 | 2018-01-19 17:21:06 -0800 | [diff] [blame] | 454 | @NonNull byte[] peer, @Nullable byte[] pmk, @Nullable String passphrase) { |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 455 | if (VDBG) { |
Etan Cohen | 954111a | 2017-02-21 10:08:42 -0800 | [diff] [blame] | 456 | Log.v(TAG, "createNetworkSpecifier: role=" + role |
Etan Cohen | 62a2f9f | 2017-02-21 10:08:42 -0800 | [diff] [blame] | 457 | + ", pmk=" + ((pmk == null) ? "null" : "non-null") |
| 458 | + ", passphrase=" + ((passphrase == null) ? "null" : "non-null")); |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 461 | if (role != WIFI_AWARE_DATA_PATH_ROLE_INITIATOR |
| 462 | && role != WIFI_AWARE_DATA_PATH_ROLE_RESPONDER) { |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 463 | throw new IllegalArgumentException( |
| 464 | "createNetworkSpecifier: Invalid 'role' argument when creating a network " |
| 465 | + "specifier"); |
| 466 | } |
Etan Cohen | e43df4d | 2018-02-22 15:54:20 -0800 | [diff] [blame] | 467 | if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR || !WifiAwareUtils.isLegacyVersion(mContext, |
| 468 | Build.VERSION_CODES.P)) { |
| 469 | if (peer == null) { |
| 470 | throw new IllegalArgumentException( |
| 471 | "createNetworkSpecifier: Invalid peer MAC - cannot be null"); |
| 472 | } |
Etan Cohen | 954111a | 2017-02-21 10:08:42 -0800 | [diff] [blame] | 473 | } |
| 474 | if (peer != null && peer.length != 6) { |
| 475 | throw new IllegalArgumentException("createNetworkSpecifier: Invalid peer MAC address"); |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 476 | } |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 477 | |
Etan Cohen | 6850728 | 2017-03-30 08:54:39 -0700 | [diff] [blame] | 478 | return new WifiAwareNetworkSpecifier( |
| 479 | (peer == null) ? WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_OOB_ANY_PEER |
| 480 | : WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_OOB, |
| 481 | role, |
| 482 | clientId, |
| 483 | 0, // 0 is an invalid session ID |
| 484 | 0, // 0 is an invalid peer ID |
| 485 | peer, |
| 486 | pmk, |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 487 | passphrase, |
Etan Cohen | 7faa4fa | 2018-10-17 15:48:39 -0700 | [diff] [blame] | 488 | 0, // no port info for OOB APIs |
Roshan Pius | cb3d652 | 2020-01-16 12:53:01 -0800 | [diff] [blame] | 489 | -1); // no transport protocol info for OOB APIs |
Etan Cohen | 85b1a5b | 2016-06-20 09:28:55 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 492 | private static class WifiAwareEventCallbackProxy extends IWifiAwareEventCallback.Stub { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 493 | private static final int CALLBACK_CONNECT_SUCCESS = 0; |
| 494 | private static final int CALLBACK_CONNECT_FAIL = 1; |
Etan Cohen | 271c037 | 2016-05-06 08:57:56 -0700 | [diff] [blame] | 495 | private static final int CALLBACK_IDENTITY_CHANGED = 2; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 496 | |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 497 | private final Handler mHandler; |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 498 | private final WeakReference<WifiAwareManager> mAwareManager; |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 499 | private final Binder mBinder; |
| 500 | private final Looper mLooper; |
Etan Cohen | 447b032 | 2016-05-09 15:30:06 -0700 | [diff] [blame] | 501 | |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 502 | /** |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 503 | * Constructs a {@link AttachCallback} using the specified looper. |
Etan Cohen | b0214c4 | 2016-08-18 09:55:31 -0700 | [diff] [blame] | 504 | * All callbacks will delivered on the thread of the specified looper. |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 505 | * |
| 506 | * @param looper The looper on which to execute the callbacks. |
| 507 | */ |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 508 | WifiAwareEventCallbackProxy(WifiAwareManager mgr, Looper looper, Binder binder, |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 509 | final AttachCallback attachCallback, |
| 510 | final IdentityChangedListener identityChangedListener) { |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 511 | mAwareManager = new WeakReference<>(mgr); |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 512 | mLooper = looper; |
| 513 | mBinder = binder; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 514 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 515 | if (VDBG) Log.v(TAG, "WifiAwareEventCallbackProxy ctor: looper=" + looper); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 516 | mHandler = new Handler(looper) { |
| 517 | @Override |
| 518 | public void handleMessage(Message msg) { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 519 | if (DBG) { |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 520 | Log.d(TAG, "WifiAwareEventCallbackProxy: What=" + msg.what + ", msg=" |
| 521 | + msg); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 524 | WifiAwareManager mgr = mAwareManager.get(); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 525 | if (mgr == null) { |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 526 | Log.w(TAG, "WifiAwareEventCallbackProxy: handleMessage post GC"); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 527 | return; |
| 528 | } |
| 529 | |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 530 | switch (msg.what) { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 531 | case CALLBACK_CONNECT_SUCCESS: |
Etan Cohen | 78ef40a | 2016-09-20 17:22:35 -0700 | [diff] [blame] | 532 | attachCallback.onAttached( |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 533 | new WifiAwareSession(mgr, mBinder, msg.arg1)); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 534 | break; |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 535 | case CALLBACK_CONNECT_FAIL: |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 536 | mAwareManager.clear(); |
Etan Cohen | 5542ccc | 2016-09-21 14:37:02 -0700 | [diff] [blame] | 537 | attachCallback.onAttachFailed(); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 538 | break; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 539 | case CALLBACK_IDENTITY_CHANGED: |
Etan Cohen | 275f323 | 2017-01-30 13:01:50 -0800 | [diff] [blame] | 540 | if (identityChangedListener == null) { |
| 541 | Log.e(TAG, "CALLBACK_IDENTITY_CHANGED: null listener."); |
| 542 | } else { |
| 543 | identityChangedListener.onIdentityChanged((byte[]) msg.obj); |
| 544 | } |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 545 | break; |
| 546 | } |
| 547 | } |
| 548 | }; |
| 549 | } |
| 550 | |
| 551 | @Override |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 552 | public void onConnectSuccess(int clientId) { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 553 | if (VDBG) Log.v(TAG, "onConnectSuccess"); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 554 | |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 555 | Message msg = mHandler.obtainMessage(CALLBACK_CONNECT_SUCCESS); |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 556 | msg.arg1 = clientId; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 557 | mHandler.sendMessage(msg); |
| 558 | } |
| 559 | |
| 560 | @Override |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 561 | public void onConnectFail(int reason) { |
Etan Cohen | 41d6f37 | 2016-09-22 11:00:57 -0700 | [diff] [blame] | 562 | if (VDBG) Log.v(TAG, "onConnectFail: reason=" + reason); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 563 | |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 564 | Message msg = mHandler.obtainMessage(CALLBACK_CONNECT_FAIL); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 565 | msg.arg1 = reason; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 566 | mHandler.sendMessage(msg); |
| 567 | } |
| 568 | |
| 569 | @Override |
Etan Cohen | 2d3face | 2016-07-07 15:07:38 -0700 | [diff] [blame] | 570 | public void onIdentityChanged(byte[] mac) { |
| 571 | if (VDBG) Log.v(TAG, "onIdentityChanged: mac=" + new String(HexEncoding.encode(mac))); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 572 | |
| 573 | Message msg = mHandler.obtainMessage(CALLBACK_IDENTITY_CHANGED); |
Etan Cohen | 2d3face | 2016-07-07 15:07:38 -0700 | [diff] [blame] | 574 | msg.obj = mac; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 575 | mHandler.sendMessage(msg); |
| 576 | } |
| 577 | } |
| 578 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 579 | private static class WifiAwareDiscoverySessionCallbackProxy extends |
| 580 | IWifiAwareDiscoverySessionCallback.Stub { |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 581 | private static final int CALLBACK_SESSION_STARTED = 0; |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 582 | private static final int CALLBACK_SESSION_CONFIG_SUCCESS = 1; |
| 583 | private static final int CALLBACK_SESSION_CONFIG_FAIL = 2; |
| 584 | private static final int CALLBACK_SESSION_TERMINATED = 3; |
| 585 | private static final int CALLBACK_MATCH = 4; |
| 586 | private static final int CALLBACK_MESSAGE_SEND_SUCCESS = 5; |
| 587 | private static final int CALLBACK_MESSAGE_SEND_FAIL = 6; |
| 588 | private static final int CALLBACK_MESSAGE_RECEIVED = 7; |
Etan Cohen | 88aa12b | 2017-11-08 08:41:39 -0800 | [diff] [blame] | 589 | private static final int CALLBACK_MATCH_WITH_DISTANCE = 8; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 590 | |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 591 | private static final String MESSAGE_BUNDLE_KEY_MESSAGE = "message"; |
| 592 | private static final String MESSAGE_BUNDLE_KEY_MESSAGE2 = "message2"; |
| 593 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 594 | private final WeakReference<WifiAwareManager> mAwareManager; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 595 | private final boolean mIsPublish; |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 596 | private final DiscoverySessionCallback mOriginalCallback; |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 597 | private final int mClientId; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 598 | |
| 599 | private final Handler mHandler; |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 600 | private DiscoverySession mSession; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 601 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 602 | WifiAwareDiscoverySessionCallbackProxy(WifiAwareManager mgr, Looper looper, |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 603 | boolean isPublish, DiscoverySessionCallback originalCallback, |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 604 | int clientId) { |
| 605 | mAwareManager = new WeakReference<>(mgr); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 606 | mIsPublish = isPublish; |
| 607 | mOriginalCallback = originalCallback; |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 608 | mClientId = clientId; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 609 | |
Etan Cohen | 3855370 | 2016-09-14 14:41:56 -0700 | [diff] [blame] | 610 | if (VDBG) { |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 611 | Log.v(TAG, "WifiAwareDiscoverySessionCallbackProxy ctor: isPublish=" + isPublish); |
Etan Cohen | 3855370 | 2016-09-14 14:41:56 -0700 | [diff] [blame] | 612 | } |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 613 | |
| 614 | mHandler = new Handler(looper) { |
| 615 | @Override |
| 616 | public void handleMessage(Message msg) { |
| 617 | if (DBG) Log.d(TAG, "What=" + msg.what + ", msg=" + msg); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 618 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 619 | if (mAwareManager.get() == null) { |
| 620 | Log.w(TAG, "WifiAwareDiscoverySessionCallbackProxy: handleMessage post GC"); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 624 | switch (msg.what) { |
| 625 | case CALLBACK_SESSION_STARTED: |
| 626 | onProxySessionStarted(msg.arg1); |
| 627 | break; |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 628 | case CALLBACK_SESSION_CONFIG_SUCCESS: |
Etan Cohen | b68ed28 | 2016-09-20 09:23:33 -0700 | [diff] [blame] | 629 | mOriginalCallback.onSessionConfigUpdated(); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 630 | break; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 631 | case CALLBACK_SESSION_CONFIG_FAIL: |
Etan Cohen | 5542ccc | 2016-09-21 14:37:02 -0700 | [diff] [blame] | 632 | mOriginalCallback.onSessionConfigFailed(); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 633 | if (mSession == null) { |
| 634 | /* |
| 635 | * creation failed (as opposed to update |
| 636 | * failing) |
| 637 | */ |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 638 | mAwareManager.clear(); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 639 | } |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 640 | break; |
| 641 | case CALLBACK_SESSION_TERMINATED: |
| 642 | onProxySessionTerminated(msg.arg1); |
| 643 | break; |
Etan Cohen | 88aa12b | 2017-11-08 08:41:39 -0800 | [diff] [blame] | 644 | case CALLBACK_MATCH: |
| 645 | case CALLBACK_MATCH_WITH_DISTANCE: |
| 646 | { |
Etan Cohen | 1857b98 | 2016-11-23 11:11:23 -0800 | [diff] [blame] | 647 | List<byte[]> matchFilter = null; |
| 648 | byte[] arg = msg.getData().getByteArray(MESSAGE_BUNDLE_KEY_MESSAGE2); |
| 649 | try { |
| 650 | matchFilter = new TlvBufferUtils.TlvIterable(0, 1, arg).toList(); |
| 651 | } catch (BufferOverflowException e) { |
| 652 | matchFilter = null; |
| 653 | Log.e(TAG, "onServiceDiscovered: invalid match filter byte array '" |
| 654 | + new String(HexEncoding.encode(arg)) |
| 655 | + "' - cannot be parsed: e=" + e); |
| 656 | } |
Etan Cohen | 88aa12b | 2017-11-08 08:41:39 -0800 | [diff] [blame] | 657 | if (msg.what == CALLBACK_MATCH) { |
| 658 | mOriginalCallback.onServiceDiscovered(new PeerHandle(msg.arg1), |
| 659 | msg.getData().getByteArray(MESSAGE_BUNDLE_KEY_MESSAGE), |
| 660 | matchFilter); |
| 661 | } else { |
| 662 | mOriginalCallback.onServiceDiscoveredWithinRange( |
| 663 | new PeerHandle(msg.arg1), |
| 664 | msg.getData().getByteArray(MESSAGE_BUNDLE_KEY_MESSAGE), |
| 665 | matchFilter, msg.arg2); |
| 666 | } |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 667 | break; |
Etan Cohen | 1857b98 | 2016-11-23 11:11:23 -0800 | [diff] [blame] | 668 | } |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 669 | case CALLBACK_MESSAGE_SEND_SUCCESS: |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 670 | mOriginalCallback.onMessageSendSucceeded(msg.arg1); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 671 | break; |
| 672 | case CALLBACK_MESSAGE_SEND_FAIL: |
Etan Cohen | 5542ccc | 2016-09-21 14:37:02 -0700 | [diff] [blame] | 673 | mOriginalCallback.onMessageSendFailed(msg.arg1); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 674 | break; |
| 675 | case CALLBACK_MESSAGE_RECEIVED: |
Etan Cohen | 6831f93 | 2016-11-16 13:40:55 -0800 | [diff] [blame] | 676 | mOriginalCallback.onMessageReceived(new PeerHandle(msg.arg1), |
Etan Cohen | 27c18cb | 2016-09-22 08:18:36 -0700 | [diff] [blame] | 677 | (byte[]) msg.obj); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 678 | break; |
| 679 | } |
| 680 | } |
| 681 | }; |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public void onSessionStarted(int sessionId) { |
| 686 | if (VDBG) Log.v(TAG, "onSessionStarted: sessionId=" + sessionId); |
| 687 | |
| 688 | Message msg = mHandler.obtainMessage(CALLBACK_SESSION_STARTED); |
| 689 | msg.arg1 = sessionId; |
| 690 | mHandler.sendMessage(msg); |
| 691 | } |
| 692 | |
| 693 | @Override |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 694 | public void onSessionConfigSuccess() { |
| 695 | if (VDBG) Log.v(TAG, "onSessionConfigSuccess"); |
| 696 | |
| 697 | Message msg = mHandler.obtainMessage(CALLBACK_SESSION_CONFIG_SUCCESS); |
| 698 | mHandler.sendMessage(msg); |
| 699 | } |
| 700 | |
| 701 | @Override |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 702 | public void onSessionConfigFail(int reason) { |
| 703 | if (VDBG) Log.v(TAG, "onSessionConfigFail: reason=" + reason); |
| 704 | |
| 705 | Message msg = mHandler.obtainMessage(CALLBACK_SESSION_CONFIG_FAIL); |
| 706 | msg.arg1 = reason; |
| 707 | mHandler.sendMessage(msg); |
| 708 | } |
| 709 | |
| 710 | @Override |
| 711 | public void onSessionTerminated(int reason) { |
| 712 | if (VDBG) Log.v(TAG, "onSessionTerminated: reason=" + reason); |
| 713 | |
| 714 | Message msg = mHandler.obtainMessage(CALLBACK_SESSION_TERMINATED); |
| 715 | msg.arg1 = reason; |
| 716 | mHandler.sendMessage(msg); |
| 717 | } |
| 718 | |
Etan Cohen | 88aa12b | 2017-11-08 08:41:39 -0800 | [diff] [blame] | 719 | private void onMatchCommon(int messageType, int peerId, byte[] serviceSpecificInfo, |
| 720 | byte[] matchFilter, int distanceMm) { |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 721 | Bundle data = new Bundle(); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 722 | data.putByteArray(MESSAGE_BUNDLE_KEY_MESSAGE, serviceSpecificInfo); |
| 723 | data.putByteArray(MESSAGE_BUNDLE_KEY_MESSAGE2, matchFilter); |
| 724 | |
Etan Cohen | 88aa12b | 2017-11-08 08:41:39 -0800 | [diff] [blame] | 725 | Message msg = mHandler.obtainMessage(messageType); |
Etan Cohen | ee58a68 | 2016-07-15 15:28:39 -0700 | [diff] [blame] | 726 | msg.arg1 = peerId; |
Etan Cohen | 88aa12b | 2017-11-08 08:41:39 -0800 | [diff] [blame] | 727 | msg.arg2 = distanceMm; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 728 | msg.setData(data); |
| 729 | mHandler.sendMessage(msg); |
| 730 | } |
| 731 | |
| 732 | @Override |
Etan Cohen | 88aa12b | 2017-11-08 08:41:39 -0800 | [diff] [blame] | 733 | public void onMatch(int peerId, byte[] serviceSpecificInfo, byte[] matchFilter) { |
| 734 | if (VDBG) Log.v(TAG, "onMatch: peerId=" + peerId); |
| 735 | |
| 736 | onMatchCommon(CALLBACK_MATCH, peerId, serviceSpecificInfo, matchFilter, 0); |
| 737 | } |
| 738 | |
| 739 | @Override |
| 740 | public void onMatchWithDistance(int peerId, byte[] serviceSpecificInfo, byte[] matchFilter, |
| 741 | int distanceMm) { |
| 742 | if (VDBG) { |
| 743 | Log.v(TAG, "onMatchWithDistance: peerId=" + peerId + ", distanceMm=" + distanceMm); |
| 744 | } |
| 745 | |
| 746 | onMatchCommon(CALLBACK_MATCH_WITH_DISTANCE, peerId, serviceSpecificInfo, matchFilter, |
| 747 | distanceMm); |
| 748 | } |
| 749 | |
| 750 | @Override |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 751 | public void onMessageSendSuccess(int messageId) { |
| 752 | if (VDBG) Log.v(TAG, "onMessageSendSuccess"); |
| 753 | |
| 754 | Message msg = mHandler.obtainMessage(CALLBACK_MESSAGE_SEND_SUCCESS); |
| 755 | msg.arg1 = messageId; |
| 756 | mHandler.sendMessage(msg); |
| 757 | } |
| 758 | |
| 759 | @Override |
| 760 | public void onMessageSendFail(int messageId, int reason) { |
| 761 | if (VDBG) Log.v(TAG, "onMessageSendFail: reason=" + reason); |
| 762 | |
| 763 | Message msg = mHandler.obtainMessage(CALLBACK_MESSAGE_SEND_FAIL); |
| 764 | msg.arg1 = messageId; |
| 765 | msg.arg2 = reason; |
| 766 | mHandler.sendMessage(msg); |
| 767 | } |
| 768 | |
| 769 | @Override |
Etan Cohen | ee58a68 | 2016-07-15 15:28:39 -0700 | [diff] [blame] | 770 | public void onMessageReceived(int peerId, byte[] message) { |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 771 | if (VDBG) { |
Etan Cohen | 1857b98 | 2016-11-23 11:11:23 -0800 | [diff] [blame] | 772 | Log.v(TAG, "onMessageReceived: peerId=" + peerId); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 775 | Message msg = mHandler.obtainMessage(CALLBACK_MESSAGE_RECEIVED); |
Etan Cohen | ee58a68 | 2016-07-15 15:28:39 -0700 | [diff] [blame] | 776 | msg.arg1 = peerId; |
| 777 | msg.obj = message; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 778 | mHandler.sendMessage(msg); |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Proxied methods |
| 783 | */ |
| 784 | public void onProxySessionStarted(int sessionId) { |
| 785 | if (VDBG) Log.v(TAG, "Proxy: onSessionStarted: sessionId=" + sessionId); |
| 786 | if (mSession != null) { |
| 787 | Log.e(TAG, |
| 788 | "onSessionStarted: sessionId=" + sessionId + ": session already created!?"); |
| 789 | throw new IllegalStateException( |
| 790 | "onSessionStarted: sessionId=" + sessionId + ": session already created!?"); |
| 791 | } |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 792 | |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 793 | WifiAwareManager mgr = mAwareManager.get(); |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 794 | if (mgr == null) { |
| 795 | Log.w(TAG, "onProxySessionStarted: mgr GC'd"); |
| 796 | return; |
| 797 | } |
| 798 | |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 799 | if (mIsPublish) { |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 800 | PublishDiscoverySession session = new PublishDiscoverySession(mgr, |
Etan Cohen | bd4bf35 | 2016-09-15 12:15:10 -0700 | [diff] [blame] | 801 | mClientId, sessionId); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 802 | mSession = session; |
| 803 | mOriginalCallback.onPublishStarted(session); |
| 804 | } else { |
Etan Cohen | 3de35a5 | 2016-12-19 15:54:51 -0800 | [diff] [blame] | 805 | SubscribeDiscoverySession |
| 806 | session = new SubscribeDiscoverySession(mgr, mClientId, sessionId); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 807 | mSession = session; |
| 808 | mOriginalCallback.onSubscribeStarted(session); |
| 809 | } |
| 810 | } |
| 811 | |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 812 | public void onProxySessionTerminated(int reason) { |
| 813 | if (VDBG) Log.v(TAG, "Proxy: onSessionTerminated: reason=" + reason); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 814 | if (mSession != null) { |
Etan Cohen | 51181fb | 2016-03-18 08:43:38 -0700 | [diff] [blame] | 815 | mSession.setTerminated(); |
| 816 | mSession = null; |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 817 | } else { |
| 818 | Log.w(TAG, "Proxy: onSessionTerminated called but mSession is null!?"); |
| 819 | } |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 820 | mAwareManager.clear(); |
Etan Cohen | b84ad80 | 2016-12-24 12:44:59 -0800 | [diff] [blame] | 821 | mOriginalCallback.onSessionTerminated(); |
Etan Cohen | 231859c | 2016-03-14 18:06:46 -0700 | [diff] [blame] | 822 | } |
| 823 | } |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 824 | } |