Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 17 | package android.net.nsd; |
| 18 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 23 | import android.os.HandlerThread; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 24 | import android.os.Looper; |
| 25 | import android.os.Message; |
| 26 | import android.os.RemoteException; |
| 27 | import android.os.Messenger; |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 30 | import android.util.SparseArray; |
| 31 | |
| 32 | import java.util.concurrent.CountDownLatch; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 33 | |
| 34 | import com.android.internal.util.AsyncChannel; |
| 35 | import com.android.internal.util.Protocol; |
| 36 | |
| 37 | /** |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 38 | * The Network Service Discovery Manager class provides the API to discover services |
| 39 | * on a network. As an example, if device A and device B are connected over a Wi-Fi |
| 40 | * network, a game registered on device A can be discovered by a game on device |
| 41 | * B. Another example use case is an application discovering printers on the network. |
| 42 | * |
| 43 | * <p> The API currently supports DNS based service discovery and discovery is currently |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 44 | * limited to a local network over Multicast DNS. DNS service discovery is described at |
| 45 | * http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 46 | * |
| 47 | * <p> The API is asynchronous and responses to requests from an application are on listener |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 48 | * callbacks on a seperate thread. |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 49 | * |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 50 | * <p> There are three main operations the API supports - registration, discovery and resolution. |
| 51 | * <pre> |
| 52 | * Application start |
| 53 | * | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 54 | * | |
| 55 | * | onServiceRegistered() |
| 56 | * Register any local services / |
| 57 | * to be advertised with \ |
| 58 | * registerService() onRegistrationFailed() |
| 59 | * | |
| 60 | * | |
| 61 | * discoverServices() |
| 62 | * | |
| 63 | * Maintain a list to track |
| 64 | * discovered services |
| 65 | * | |
| 66 | * |---------> |
| 67 | * | | |
| 68 | * | onServiceFound() |
| 69 | * | | |
| 70 | * | add service to list |
| 71 | * | | |
| 72 | * |<---------- |
| 73 | * | |
| 74 | * |---------> |
| 75 | * | | |
| 76 | * | onServiceLost() |
| 77 | * | | |
| 78 | * | remove service from list |
| 79 | * | | |
| 80 | * |<---------- |
| 81 | * | |
| 82 | * | |
| 83 | * | Connect to a service |
| 84 | * | from list ? |
| 85 | * | |
| 86 | * resolveService() |
| 87 | * | |
| 88 | * onServiceResolved() |
| 89 | * | |
| 90 | * Establish connection to service |
| 91 | * with the host and port information |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 92 | * |
| 93 | * </pre> |
| 94 | * An application that needs to advertise itself over a network for other applications to |
| 95 | * discover it can do so with a call to {@link #registerService}. If Example is a http based |
| 96 | * application that can provide HTML data to peer services, it can register a name "Example" |
| 97 | * with service type "_http._tcp". A successful registration is notified with a callback to |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 98 | * {@link RegistrationListener#onServiceRegistered} and a failure to register is notified |
| 99 | * over {@link RegistrationListener#onRegistrationFailed} |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 100 | * |
| 101 | * <p> A peer application looking for http services can initiate a discovery for "_http._tcp" |
| 102 | * with a call to {@link #discoverServices}. A service found is notified with a callback |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 103 | * to {@link DiscoveryListener#onServiceFound} and a service lost is notified on |
| 104 | * {@link DiscoveryListener#onServiceLost}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 105 | * |
Philip P. Moltmann | 7d5da4b | 2016-04-18 16:23:06 -0700 | [diff] [blame] | 106 | * <p> Once the peer application discovers the "Example" http service, and either needs to read the |
| 107 | * attributes of the service or wants to receive data from the "Example" application, it can |
| 108 | * initiate a resolve with {@link #resolveService} to resolve the attributes, host, and port |
| 109 | * details. A successful resolve is notified on {@link ResolveListener#onServiceResolved} and a |
| 110 | * failure is notified on {@link ResolveListener#onResolveFailed}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 111 | * |
| 112 | * Applications can reserve for a service type at |
| 113 | * http://www.iana.org/form/ports-service. Existing services can be found at |
| 114 | * http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 115 | * |
| 116 | * Get an instance of this class by calling {@link android.content.Context#getSystemService(String) |
| 117 | * Context.getSystemService(Context.NSD_SERVICE)}. |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 118 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 119 | * {@see NsdServiceInfo} |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 120 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 121 | public final class NsdManager { |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 122 | private static final String TAG = "NsdManager"; |
| 123 | INsdManager mService; |
| 124 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Broadcast intent action to indicate whether network service discovery is |
| 127 | * enabled or disabled. An extra {@link #EXTRA_NSD_STATE} provides the state |
| 128 | * information as int. |
| 129 | * |
| 130 | * @see #EXTRA_NSD_STATE |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 131 | */ |
| 132 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Irfan Sheriff | 6c07ba8 | 2012-04-17 23:23:42 -0700 | [diff] [blame] | 133 | public static final String ACTION_NSD_STATE_CHANGED = |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 134 | "android.net.nsd.STATE_CHANGED"; |
| 135 | |
| 136 | /** |
| 137 | * The lookup key for an int that indicates whether network service discovery is enabled |
| 138 | * or disabled. Retrieve it with {@link android.content.Intent#getIntExtra(String,int)}. |
| 139 | * |
| 140 | * @see #NSD_STATE_DISABLED |
| 141 | * @see #NSD_STATE_ENABLED |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 142 | */ |
| 143 | public static final String EXTRA_NSD_STATE = "nsd_state"; |
| 144 | |
| 145 | /** |
| 146 | * Network service discovery is disabled |
| 147 | * |
Irfan Sheriff | 54ac7a5 | 2012-04-19 10:26:34 -0700 | [diff] [blame] | 148 | * @see #ACTION_NSD_STATE_CHANGED |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 149 | */ |
| 150 | public static final int NSD_STATE_DISABLED = 1; |
| 151 | |
| 152 | /** |
| 153 | * Network service discovery is enabled |
| 154 | * |
Irfan Sheriff | 54ac7a5 | 2012-04-19 10:26:34 -0700 | [diff] [blame] | 155 | * @see #ACTION_NSD_STATE_CHANGED |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 156 | */ |
| 157 | public static final int NSD_STATE_ENABLED = 2; |
| 158 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 159 | private static final int BASE = Protocol.BASE_NSD_MANAGER; |
| 160 | |
| 161 | /** @hide */ |
| 162 | public static final int DISCOVER_SERVICES = BASE + 1; |
| 163 | /** @hide */ |
| 164 | public static final int DISCOVER_SERVICES_STARTED = BASE + 2; |
| 165 | /** @hide */ |
| 166 | public static final int DISCOVER_SERVICES_FAILED = BASE + 3; |
| 167 | /** @hide */ |
| 168 | public static final int SERVICE_FOUND = BASE + 4; |
| 169 | /** @hide */ |
| 170 | public static final int SERVICE_LOST = BASE + 5; |
| 171 | |
| 172 | /** @hide */ |
| 173 | public static final int STOP_DISCOVERY = BASE + 6; |
| 174 | /** @hide */ |
| 175 | public static final int STOP_DISCOVERY_FAILED = BASE + 7; |
| 176 | /** @hide */ |
| 177 | public static final int STOP_DISCOVERY_SUCCEEDED = BASE + 8; |
| 178 | |
| 179 | /** @hide */ |
| 180 | public static final int REGISTER_SERVICE = BASE + 9; |
| 181 | /** @hide */ |
| 182 | public static final int REGISTER_SERVICE_FAILED = BASE + 10; |
| 183 | /** @hide */ |
| 184 | public static final int REGISTER_SERVICE_SUCCEEDED = BASE + 11; |
| 185 | |
| 186 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 187 | public static final int UNREGISTER_SERVICE = BASE + 12; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 188 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 189 | public static final int UNREGISTER_SERVICE_FAILED = BASE + 13; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 190 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 191 | public static final int UNREGISTER_SERVICE_SUCCEEDED = BASE + 14; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 192 | |
| 193 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 194 | public static final int RESOLVE_SERVICE = BASE + 18; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 195 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 196 | public static final int RESOLVE_SERVICE_FAILED = BASE + 19; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 197 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 198 | public static final int RESOLVE_SERVICE_SUCCEEDED = BASE + 20; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 199 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 200 | /** @hide */ |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 201 | public static final int ENABLE = BASE + 24; |
| 202 | /** @hide */ |
| 203 | public static final int DISABLE = BASE + 25; |
| 204 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 205 | /** @hide */ |
| 206 | public static final int NATIVE_DAEMON_EVENT = BASE + 26; |
| 207 | |
| 208 | /** Dns based service discovery protocol */ |
| 209 | public static final int PROTOCOL_DNS_SD = 0x0001; |
| 210 | |
| 211 | private Context mContext; |
| 212 | |
| 213 | private static final int INVALID_LISTENER_KEY = 0; |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 214 | private static final int BUSY_LISTENER_KEY = -1; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 215 | private int mListenerKey = 1; |
| 216 | private final SparseArray mListenerMap = new SparseArray(); |
| 217 | private final SparseArray<NsdServiceInfo> mServiceMap = new SparseArray<NsdServiceInfo>(); |
| 218 | private final Object mMapLock = new Object(); |
| 219 | |
| 220 | private final AsyncChannel mAsyncChannel = new AsyncChannel(); |
| 221 | private ServiceHandler mHandler; |
| 222 | private final CountDownLatch mConnected = new CountDownLatch(1); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 223 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 224 | /** |
| 225 | * Create a new Nsd instance. Applications use |
| 226 | * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve |
| 227 | * {@link android.content.Context#NSD_SERVICE Context.NSD_SERVICE}. |
| 228 | * @param service the Binder interface |
| 229 | * @hide - hide this because it takes in a parameter of type INsdManager, which |
| 230 | * is a system private class. |
| 231 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 232 | public NsdManager(Context context, INsdManager service) { |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 233 | mService = service; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 234 | mContext = context; |
| 235 | init(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 239 | * Failures are passed with {@link RegistrationListener#onRegistrationFailed}, |
| 240 | * {@link RegistrationListener#onUnregistrationFailed}, |
| 241 | * {@link DiscoveryListener#onStartDiscoveryFailed}, |
| 242 | * {@link DiscoveryListener#onStopDiscoveryFailed} or {@link ResolveListener#onResolveFailed}. |
| 243 | * |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 244 | * Indicates that the operation failed due to an internal error. |
| 245 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 246 | public static final int FAILURE_INTERNAL_ERROR = 0; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 247 | |
| 248 | /** |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 249 | * Indicates that the operation failed because it is already active. |
| 250 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 251 | public static final int FAILURE_ALREADY_ACTIVE = 3; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 252 | |
| 253 | /** |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 254 | * Indicates that the operation failed because the maximum outstanding |
| 255 | * requests from the applications have reached. |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 256 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 257 | public static final int FAILURE_MAX_LIMIT = 4; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 258 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 259 | /** Interface for callback invocation for service discovery */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 260 | public interface DiscoveryListener { |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 261 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 262 | public void onStartDiscoveryFailed(String serviceType, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 263 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 264 | public void onStopDiscoveryFailed(String serviceType, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 265 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 266 | public void onDiscoveryStarted(String serviceType); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 267 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 268 | public void onDiscoveryStopped(String serviceType); |
| 269 | |
| 270 | public void onServiceFound(NsdServiceInfo serviceInfo); |
| 271 | |
| 272 | public void onServiceLost(NsdServiceInfo serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 273 | |
| 274 | } |
| 275 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 276 | /** Interface for callback invocation for service registration */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 277 | public interface RegistrationListener { |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 278 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 279 | public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 280 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 281 | public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 282 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 283 | public void onServiceRegistered(NsdServiceInfo serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 284 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 285 | public void onServiceUnregistered(NsdServiceInfo serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 288 | /** Interface for callback invocation for service resolution */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 289 | public interface ResolveListener { |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 290 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 291 | public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 292 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 293 | public void onServiceResolved(NsdServiceInfo serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 296 | private class ServiceHandler extends Handler { |
| 297 | ServiceHandler(Looper looper) { |
| 298 | super(looper); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 299 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 300 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 301 | @Override |
| 302 | public void handleMessage(Message message) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 303 | switch (message.what) { |
| 304 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: |
| 305 | mAsyncChannel.sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 306 | return; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 307 | case AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED: |
Robert Greenwalt | af2eefb | 2013-05-02 15:45:32 -0700 | [diff] [blame] | 308 | mConnected.countDown(); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 309 | return; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 310 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: |
| 311 | Log.e(TAG, "Channel lost"); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 312 | return; |
| 313 | default: |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 314 | break; |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 315 | } |
| 316 | Object listener = getListener(message.arg2); |
| 317 | if (listener == null) { |
| 318 | Log.d(TAG, "Stale key " + message.arg2); |
| 319 | return; |
| 320 | } |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 321 | NsdServiceInfo ns = getNsdService(message.arg2); |
| 322 | switch (message.what) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 323 | case DISCOVER_SERVICES_STARTED: |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 324 | String s = getNsdServiceInfoType((NsdServiceInfo) message.obj); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 325 | ((DiscoveryListener) listener).onDiscoveryStarted(s); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 326 | break; |
| 327 | case DISCOVER_SERVICES_FAILED: |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 328 | removeListener(message.arg2); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 329 | ((DiscoveryListener) listener).onStartDiscoveryFailed(getNsdServiceInfoType(ns), |
| 330 | message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 331 | break; |
| 332 | case SERVICE_FOUND: |
| 333 | ((DiscoveryListener) listener).onServiceFound((NsdServiceInfo) message.obj); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 334 | break; |
| 335 | case SERVICE_LOST: |
| 336 | ((DiscoveryListener) listener).onServiceLost((NsdServiceInfo) message.obj); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 337 | break; |
| 338 | case STOP_DISCOVERY_FAILED: |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 339 | removeListener(message.arg2); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 340 | ((DiscoveryListener) listener).onStopDiscoveryFailed(getNsdServiceInfoType(ns), |
| 341 | message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 342 | break; |
| 343 | case STOP_DISCOVERY_SUCCEEDED: |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 344 | removeListener(message.arg2); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 345 | ((DiscoveryListener) listener).onDiscoveryStopped(getNsdServiceInfoType(ns)); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 346 | break; |
| 347 | case REGISTER_SERVICE_FAILED: |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 348 | removeListener(message.arg2); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 349 | ((RegistrationListener) listener).onRegistrationFailed(ns, message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 350 | break; |
| 351 | case REGISTER_SERVICE_SUCCEEDED: |
| 352 | ((RegistrationListener) listener).onServiceRegistered( |
| 353 | (NsdServiceInfo) message.obj); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 354 | break; |
| 355 | case UNREGISTER_SERVICE_FAILED: |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 356 | removeListener(message.arg2); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 357 | ((RegistrationListener) listener).onUnregistrationFailed(ns, message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 358 | break; |
| 359 | case UNREGISTER_SERVICE_SUCCEEDED: |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 360 | removeListener(message.arg2); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 361 | ((RegistrationListener) listener).onServiceUnregistered(ns); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 362 | break; |
| 363 | case RESOLVE_SERVICE_FAILED: |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 364 | removeListener(message.arg2); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 365 | ((ResolveListener) listener).onResolveFailed(ns, message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 366 | break; |
| 367 | case RESOLVE_SERVICE_SUCCEEDED: |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 368 | removeListener(message.arg2); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 369 | ((ResolveListener) listener).onServiceResolved((NsdServiceInfo) message.obj); |
| 370 | break; |
| 371 | default: |
| 372 | Log.d(TAG, "Ignored " + message); |
| 373 | break; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 374 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 375 | } |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 378 | // if the listener is already in the map, reject it. Otherwise, add it and |
| 379 | // return its key. |
| 380 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 381 | private int putListener(Object listener, NsdServiceInfo s) { |
| 382 | if (listener == null) return INVALID_LISTENER_KEY; |
| 383 | int key; |
| 384 | synchronized (mMapLock) { |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 385 | int valueIndex = mListenerMap.indexOfValue(listener); |
| 386 | if (valueIndex != -1) { |
| 387 | return BUSY_LISTENER_KEY; |
| 388 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 389 | do { |
| 390 | key = mListenerKey++; |
| 391 | } while (key == INVALID_LISTENER_KEY); |
| 392 | mListenerMap.put(key, listener); |
| 393 | mServiceMap.put(key, s); |
| 394 | } |
| 395 | return key; |
| 396 | } |
| 397 | |
| 398 | private Object getListener(int key) { |
| 399 | if (key == INVALID_LISTENER_KEY) return null; |
| 400 | synchronized (mMapLock) { |
| 401 | return mListenerMap.get(key); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | private NsdServiceInfo getNsdService(int key) { |
| 406 | synchronized (mMapLock) { |
| 407 | return mServiceMap.get(key); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | private void removeListener(int key) { |
| 412 | if (key == INVALID_LISTENER_KEY) return; |
| 413 | synchronized (mMapLock) { |
| 414 | mListenerMap.remove(key); |
| 415 | mServiceMap.remove(key); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | private int getListenerKey(Object listener) { |
| 420 | synchronized (mMapLock) { |
| 421 | int valueIndex = mListenerMap.indexOfValue(listener); |
| 422 | if (valueIndex != -1) { |
| 423 | return mListenerMap.keyAt(valueIndex); |
| 424 | } |
| 425 | } |
| 426 | return INVALID_LISTENER_KEY; |
| 427 | } |
| 428 | |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 429 | private String getNsdServiceInfoType(NsdServiceInfo s) { |
| 430 | if (s == null) return "?"; |
| 431 | return s.getServiceType(); |
| 432 | } |
| 433 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 434 | /** |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 435 | * Initialize AsyncChannel |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 436 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 437 | private void init() { |
| 438 | final Messenger messenger = getMessenger(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 439 | if (messenger == null) throw new RuntimeException("Failed to initialize"); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 440 | HandlerThread t = new HandlerThread("NsdManager"); |
| 441 | t.start(); |
| 442 | mHandler = new ServiceHandler(t.getLooper()); |
| 443 | mAsyncChannel.connect(mContext, mHandler, messenger); |
| 444 | try { |
| 445 | mConnected.await(); |
| 446 | } catch (InterruptedException e) { |
| 447 | Log.e(TAG, "interrupted wait at init"); |
| 448 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /** |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 452 | * Register a service to be discovered by other services. |
| 453 | * |
| 454 | * <p> The function call immediately returns after sending a request to register service |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 455 | * to the framework. The application is notified of a successful registration |
| 456 | * through the callback {@link RegistrationListener#onServiceRegistered} or a failure |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 457 | * through {@link RegistrationListener#onRegistrationFailed}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 458 | * |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 459 | * <p> The application should call {@link #unregisterService} when the service |
| 460 | * registration is no longer required, and/or whenever the application is stopped. |
| 461 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 462 | * @param serviceInfo The service being registered |
| 463 | * @param protocolType The service discovery protocol |
| 464 | * @param listener The listener notifies of a successful registration and is used to |
| 465 | * unregister this service through a call on {@link #unregisterService}. Cannot be null. |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 466 | * Cannot be in use for an active service registration. |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 467 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 468 | public void registerService(NsdServiceInfo serviceInfo, int protocolType, |
| 469 | RegistrationListener listener) { |
| 470 | if (TextUtils.isEmpty(serviceInfo.getServiceName()) || |
| 471 | TextUtils.isEmpty(serviceInfo.getServiceType())) { |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 472 | throw new IllegalArgumentException("Service name or type cannot be empty"); |
| 473 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 474 | if (serviceInfo.getPort() <= 0) { |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 475 | throw new IllegalArgumentException("Invalid port number"); |
| 476 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 477 | if (listener == null) { |
| 478 | throw new IllegalArgumentException("listener cannot be null"); |
| 479 | } |
| 480 | if (protocolType != PROTOCOL_DNS_SD) { |
| 481 | throw new IllegalArgumentException("Unsupported protocol"); |
| 482 | } |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 483 | int key = putListener(listener, serviceInfo); |
| 484 | if (key == BUSY_LISTENER_KEY) { |
| 485 | throw new IllegalArgumentException("listener already in use"); |
| 486 | } |
| 487 | mAsyncChannel.sendMessage(REGISTER_SERVICE, 0, key, serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 490 | /** |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 491 | * Unregister a service registered through {@link #registerService}. A successful |
| 492 | * unregister is notified to the application with a call to |
| 493 | * {@link RegistrationListener#onServiceUnregistered}. |
| 494 | * |
| 495 | * @param listener This should be the listener object that was passed to |
| 496 | * {@link #registerService}. It identifies the service that should be unregistered |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 497 | * and notifies of a successful or unsuccessful unregistration via the listener |
| 498 | * callbacks. In API versions 20 and above, the listener object may be used for |
| 499 | * another service registration once the callback has been called. In API versions <= 19, |
| 500 | * there is no entirely reliable way to know when a listener may be re-used, and a new |
| 501 | * listener should be created for each service registration request. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 502 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 503 | public void unregisterService(RegistrationListener listener) { |
| 504 | int id = getListenerKey(listener); |
| 505 | if (id == INVALID_LISTENER_KEY) { |
| 506 | throw new IllegalArgumentException("listener not registered"); |
| 507 | } |
| 508 | if (listener == null) { |
| 509 | throw new IllegalArgumentException("listener cannot be null"); |
| 510 | } |
| 511 | mAsyncChannel.sendMessage(UNREGISTER_SERVICE, 0, id); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 514 | /** |
| 515 | * Initiate service discovery to browse for instances of a service type. Service discovery |
| 516 | * consumes network bandwidth and will continue until the application calls |
| 517 | * {@link #stopServiceDiscovery}. |
| 518 | * |
| 519 | * <p> The function call immediately returns after sending a request to start service |
| 520 | * discovery to the framework. The application is notified of a success to initiate |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 521 | * discovery through the callback {@link DiscoveryListener#onDiscoveryStarted} or a failure |
| 522 | * through {@link DiscoveryListener#onStartDiscoveryFailed}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 523 | * |
| 524 | * <p> Upon successful start, application is notified when a service is found with |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 525 | * {@link DiscoveryListener#onServiceFound} or when a service is lost with |
| 526 | * {@link DiscoveryListener#onServiceLost}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 527 | * |
| 528 | * <p> Upon failure to start, service discovery is not active and application does |
| 529 | * not need to invoke {@link #stopServiceDiscovery} |
| 530 | * |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 531 | * <p> The application should call {@link #stopServiceDiscovery} when discovery of this |
| 532 | * service type is no longer required, and/or whenever the application is paused or |
| 533 | * stopped. |
| 534 | * |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 535 | * @param serviceType The service type being discovered. Examples include "_http._tcp" for |
| 536 | * http services or "_ipp._tcp" for printers |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 537 | * @param protocolType The service discovery protocol |
| 538 | * @param listener The listener notifies of a successful discovery and is used |
| 539 | * to stop discovery on this serviceType through a call on {@link #stopServiceDiscovery}. |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 540 | * Cannot be null. Cannot be in use for an active service discovery. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 541 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 542 | public void discoverServices(String serviceType, int protocolType, DiscoveryListener listener) { |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 543 | if (listener == null) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 544 | throw new IllegalArgumentException("listener cannot be null"); |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 545 | } |
| 546 | if (TextUtils.isEmpty(serviceType)) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 547 | throw new IllegalArgumentException("Service type cannot be empty"); |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 548 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 549 | |
| 550 | if (protocolType != PROTOCOL_DNS_SD) { |
| 551 | throw new IllegalArgumentException("Unsupported protocol"); |
| 552 | } |
| 553 | |
| 554 | NsdServiceInfo s = new NsdServiceInfo(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 555 | s.setServiceType(serviceType); |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 556 | |
| 557 | int key = putListener(listener, s); |
| 558 | if (key == BUSY_LISTENER_KEY) { |
| 559 | throw new IllegalArgumentException("listener already in use"); |
| 560 | } |
| 561 | |
| 562 | mAsyncChannel.sendMessage(DISCOVER_SERVICES, 0, key, s); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 565 | /** |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 566 | * Stop service discovery initiated with {@link #discoverServices}. An active service |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 567 | * discovery is notified to the application with {@link DiscoveryListener#onDiscoveryStarted} |
| 568 | * and it stays active until the application invokes a stop service discovery. A successful |
| 569 | * stop is notified to with a call to {@link DiscoveryListener#onDiscoveryStopped}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 570 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 571 | * <p> Upon failure to stop service discovery, application is notified through |
| 572 | * {@link DiscoveryListener#onStopDiscoveryFailed}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 573 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 574 | * @param listener This should be the listener object that was passed to {@link #discoverServices}. |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 575 | * It identifies the discovery that should be stopped and notifies of a successful or |
| 576 | * unsuccessful stop. In API versions 20 and above, the listener object may be used for |
| 577 | * another service discovery once the callback has been called. In API versions <= 19, |
| 578 | * there is no entirely reliable way to know when a listener may be re-used, and a new |
| 579 | * listener should be created for each service discovery request. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 580 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 581 | public void stopServiceDiscovery(DiscoveryListener listener) { |
| 582 | int id = getListenerKey(listener); |
| 583 | if (id == INVALID_LISTENER_KEY) { |
| 584 | throw new IllegalArgumentException("service discovery not active on listener"); |
| 585 | } |
| 586 | if (listener == null) { |
| 587 | throw new IllegalArgumentException("listener cannot be null"); |
| 588 | } |
| 589 | mAsyncChannel.sendMessage(STOP_DISCOVERY, 0, id); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 592 | /** |
| 593 | * Resolve a discovered service. An application can resolve a service right before |
| 594 | * establishing a connection to fetch the IP and port details on which to setup |
| 595 | * the connection. |
| 596 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 597 | * @param serviceInfo service to be resolved |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 598 | * @param listener to receive callback upon success or failure. Cannot be null. |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 599 | * Cannot be in use for an active service resolution. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 600 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 601 | public void resolveService(NsdServiceInfo serviceInfo, ResolveListener listener) { |
| 602 | if (TextUtils.isEmpty(serviceInfo.getServiceName()) || |
| 603 | TextUtils.isEmpty(serviceInfo.getServiceType())) { |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 604 | throw new IllegalArgumentException("Service name or type cannot be empty"); |
| 605 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 606 | if (listener == null) { |
| 607 | throw new IllegalArgumentException("listener cannot be null"); |
| 608 | } |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 609 | |
| 610 | int key = putListener(listener, serviceInfo); |
| 611 | |
| 612 | if (key == BUSY_LISTENER_KEY) { |
| 613 | throw new IllegalArgumentException("listener already in use"); |
| 614 | } |
| 615 | mAsyncChannel.sendMessage(RESOLVE_SERVICE, 0, key, serviceInfo); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 618 | /** Internal use only @hide */ |
| 619 | public void setEnabled(boolean enabled) { |
| 620 | try { |
| 621 | mService.setEnabled(enabled); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 622 | } catch (RemoteException e) { |
| 623 | throw e.rethrowFromSystemServer(); |
| 624 | } |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 627 | /** |
| 628 | * Get a reference to NetworkService handler. This is used to establish |
| 629 | * an AsyncChannel communication with the service |
| 630 | * |
| 631 | * @return Messenger pointing to the NetworkService handler |
| 632 | */ |
| 633 | private Messenger getMessenger() { |
| 634 | try { |
| 635 | return mService.getMessenger(); |
| 636 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 637 | throw e.rethrowFromSystemServer(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |