Merge "Remove Preconditions, Protocol @hide usage"
This commit is contained in:
@@ -16,10 +16,6 @@
|
|||||||
|
|
||||||
package android.net.nsd;
|
package android.net.nsd;
|
||||||
|
|
||||||
import static com.android.internal.util.Preconditions.checkArgument;
|
|
||||||
import static com.android.internal.util.Preconditions.checkNotNull;
|
|
||||||
import static com.android.internal.util.Preconditions.checkStringNotEmpty;
|
|
||||||
|
|
||||||
import android.annotation.SdkConstant;
|
import android.annotation.SdkConstant;
|
||||||
import android.annotation.SdkConstant.SdkConstantType;
|
import android.annotation.SdkConstant.SdkConstantType;
|
||||||
import android.annotation.SystemService;
|
import android.annotation.SystemService;
|
||||||
@@ -32,11 +28,13 @@ import android.os.HandlerThread;
|
|||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
|
|
||||||
import com.android.internal.annotations.VisibleForTesting;
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
import com.android.internal.util.Protocol;
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Network Service Discovery Manager class provides the API to discover services
|
* The Network Service Discovery Manager class provides the API to discover services
|
||||||
@@ -175,65 +173,63 @@ public final class NsdManager {
|
|||||||
*/
|
*/
|
||||||
public static final int NSD_STATE_ENABLED = 2;
|
public static final int NSD_STATE_ENABLED = 2;
|
||||||
|
|
||||||
private static final int BASE = Protocol.BASE_NSD_MANAGER;
|
/** @hide */
|
||||||
|
public static final int DISCOVER_SERVICES = 1;
|
||||||
|
/** @hide */
|
||||||
|
public static final int DISCOVER_SERVICES_STARTED = 2;
|
||||||
|
/** @hide */
|
||||||
|
public static final int DISCOVER_SERVICES_FAILED = 3;
|
||||||
|
/** @hide */
|
||||||
|
public static final int SERVICE_FOUND = 4;
|
||||||
|
/** @hide */
|
||||||
|
public static final int SERVICE_LOST = 5;
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int DISCOVER_SERVICES = BASE + 1;
|
public static final int STOP_DISCOVERY = 6;
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int DISCOVER_SERVICES_STARTED = BASE + 2;
|
public static final int STOP_DISCOVERY_FAILED = 7;
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int DISCOVER_SERVICES_FAILED = BASE + 3;
|
public static final int STOP_DISCOVERY_SUCCEEDED = 8;
|
||||||
/** @hide */
|
|
||||||
public static final int SERVICE_FOUND = BASE + 4;
|
|
||||||
/** @hide */
|
|
||||||
public static final int SERVICE_LOST = BASE + 5;
|
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int STOP_DISCOVERY = BASE + 6;
|
public static final int REGISTER_SERVICE = 9;
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int STOP_DISCOVERY_FAILED = BASE + 7;
|
public static final int REGISTER_SERVICE_FAILED = 10;
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int STOP_DISCOVERY_SUCCEEDED = BASE + 8;
|
public static final int REGISTER_SERVICE_SUCCEEDED = 11;
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int REGISTER_SERVICE = BASE + 9;
|
public static final int UNREGISTER_SERVICE = 12;
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int REGISTER_SERVICE_FAILED = BASE + 10;
|
public static final int UNREGISTER_SERVICE_FAILED = 13;
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int REGISTER_SERVICE_SUCCEEDED = BASE + 11;
|
public static final int UNREGISTER_SERVICE_SUCCEEDED = 14;
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int UNREGISTER_SERVICE = BASE + 12;
|
public static final int RESOLVE_SERVICE = 15;
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int UNREGISTER_SERVICE_FAILED = BASE + 13;
|
public static final int RESOLVE_SERVICE_FAILED = 16;
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int UNREGISTER_SERVICE_SUCCEEDED = BASE + 14;
|
public static final int RESOLVE_SERVICE_SUCCEEDED = 17;
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int RESOLVE_SERVICE = BASE + 18;
|
public static final int DAEMON_CLEANUP = 18;
|
||||||
/** @hide */
|
|
||||||
public static final int RESOLVE_SERVICE_FAILED = BASE + 19;
|
|
||||||
/** @hide */
|
|
||||||
public static final int RESOLVE_SERVICE_SUCCEEDED = BASE + 20;
|
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int DAEMON_CLEANUP = BASE + 21;
|
public static final int DAEMON_STARTUP = 19;
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int DAEMON_STARTUP = BASE + 22;
|
public static final int ENABLE = 20;
|
||||||
|
/** @hide */
|
||||||
|
public static final int DISABLE = 21;
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int ENABLE = BASE + 24;
|
public static final int NATIVE_DAEMON_EVENT = 22;
|
||||||
/** @hide */
|
|
||||||
public static final int DISABLE = BASE + 25;
|
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int NATIVE_DAEMON_EVENT = BASE + 26;
|
public static final int REGISTER_CLIENT = 23;
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static final int REGISTER_CLIENT = BASE + 27;
|
public static final int UNREGISTER_CLIENT = 24;
|
||||||
/** @hide */
|
|
||||||
public static final int UNREGISTER_CLIENT = BASE + 28;
|
|
||||||
|
|
||||||
/** Dns based service discovery protocol */
|
/** Dns based service discovery protocol */
|
||||||
public static final int PROTOCOL_DNS_SD = 0x0001;
|
public static final int PROTOCOL_DNS_SD = 0x0001;
|
||||||
@@ -550,7 +546,9 @@ public final class NsdManager {
|
|||||||
final int key;
|
final int key;
|
||||||
synchronized (mMapLock) {
|
synchronized (mMapLock) {
|
||||||
int valueIndex = mListenerMap.indexOfValue(listener);
|
int valueIndex = mListenerMap.indexOfValue(listener);
|
||||||
checkArgument(valueIndex == -1, "listener already in use");
|
if (valueIndex != -1) {
|
||||||
|
throw new IllegalArgumentException("listener already in use");
|
||||||
|
}
|
||||||
key = nextListenerKey();
|
key = nextListenerKey();
|
||||||
mListenerMap.put(key, listener);
|
mListenerMap.put(key, listener);
|
||||||
mServiceMap.put(key, s);
|
mServiceMap.put(key, s);
|
||||||
@@ -569,7 +567,9 @@ public final class NsdManager {
|
|||||||
checkListener(listener);
|
checkListener(listener);
|
||||||
synchronized (mMapLock) {
|
synchronized (mMapLock) {
|
||||||
int valueIndex = mListenerMap.indexOfValue(listener);
|
int valueIndex = mListenerMap.indexOfValue(listener);
|
||||||
checkArgument(valueIndex != -1, "listener not registered");
|
if (valueIndex == -1) {
|
||||||
|
throw new IllegalArgumentException("listener not registered");
|
||||||
|
}
|
||||||
return mListenerMap.keyAt(valueIndex);
|
return mListenerMap.keyAt(valueIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -598,7 +598,9 @@ public final class NsdManager {
|
|||||||
*/
|
*/
|
||||||
public void registerService(NsdServiceInfo serviceInfo, int protocolType,
|
public void registerService(NsdServiceInfo serviceInfo, int protocolType,
|
||||||
RegistrationListener listener) {
|
RegistrationListener listener) {
|
||||||
checkArgument(serviceInfo.getPort() > 0, "Invalid port number");
|
if (serviceInfo.getPort() <= 0) {
|
||||||
|
throw new IllegalArgumentException("Invalid port number");
|
||||||
|
}
|
||||||
checkServiceInfo(serviceInfo);
|
checkServiceInfo(serviceInfo);
|
||||||
checkProtocol(protocolType);
|
checkProtocol(protocolType);
|
||||||
int key = putListener(listener, serviceInfo);
|
int key = putListener(listener, serviceInfo);
|
||||||
@@ -660,7 +662,9 @@ public final class NsdManager {
|
|||||||
* Cannot be null. Cannot be in use for an active service discovery.
|
* Cannot be null. Cannot be in use for an active service discovery.
|
||||||
*/
|
*/
|
||||||
public void discoverServices(String serviceType, int protocolType, DiscoveryListener listener) {
|
public void discoverServices(String serviceType, int protocolType, DiscoveryListener listener) {
|
||||||
checkStringNotEmpty(serviceType, "Service type cannot be empty");
|
if (TextUtils.isEmpty(serviceType)) {
|
||||||
|
throw new IllegalArgumentException("Service type cannot be empty");
|
||||||
|
}
|
||||||
checkProtocol(protocolType);
|
checkProtocol(protocolType);
|
||||||
|
|
||||||
NsdServiceInfo s = new NsdServiceInfo();
|
NsdServiceInfo s = new NsdServiceInfo();
|
||||||
@@ -719,16 +723,22 @@ public final class NsdManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void checkListener(Object listener) {
|
private static void checkListener(Object listener) {
|
||||||
checkNotNull(listener, "listener cannot be null");
|
Objects.requireNonNull(listener, "listener cannot be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkProtocol(int protocolType) {
|
private static void checkProtocol(int protocolType) {
|
||||||
checkArgument(protocolType == PROTOCOL_DNS_SD, "Unsupported protocol");
|
if (protocolType != PROTOCOL_DNS_SD) {
|
||||||
|
throw new IllegalArgumentException("Unsupported protocol");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkServiceInfo(NsdServiceInfo serviceInfo) {
|
private static void checkServiceInfo(NsdServiceInfo serviceInfo) {
|
||||||
checkNotNull(serviceInfo, "NsdServiceInfo cannot be null");
|
Objects.requireNonNull(serviceInfo, "NsdServiceInfo cannot be null");
|
||||||
checkStringNotEmpty(serviceInfo.getServiceName(), "Service name cannot be empty");
|
if (TextUtils.isEmpty(serviceInfo.getServiceName())) {
|
||||||
checkStringNotEmpty(serviceInfo.getServiceType(), "Service type cannot be empty");
|
throw new IllegalArgumentException("Service name cannot be empty");
|
||||||
|
}
|
||||||
|
if (TextUtils.isEmpty(serviceInfo.getServiceType())) {
|
||||||
|
throw new IllegalArgumentException("Service type cannot be empty");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user