New API to stop service resolution
Resolve service may take long time due to network issue or using wrong service information, but users are not able to stop it. They can only wait for the callback of resolveService to end, which sometimes takes a long time. Thus, add the new API that users can stop the service resolution. Bug: 245369943 Test: atest FrameworksNetTests CtsNetTestCases Change-Id: I6b6183c8c73f8db981b9afa51fbc73bf886d9ed3
This commit is contained in:
@@ -36,4 +36,6 @@ oneway interface INsdManagerCallback {
|
||||
void onUnregisterServiceSucceeded(int listenerKey);
|
||||
void onResolveServiceFailed(int listenerKey, int error);
|
||||
void onResolveServiceSucceeded(int listenerKey, in NsdServiceInfo info);
|
||||
void onStopResolutionFailed(int listenerKey, int error);
|
||||
void onStopResolutionSucceeded(int listenerKey);
|
||||
}
|
||||
|
||||
@@ -32,4 +32,5 @@ interface INsdServiceConnector {
|
||||
void stopDiscovery(int listenerKey);
|
||||
void resolveService(int listenerKey, in NsdServiceInfo serviceInfo);
|
||||
void startDaemon();
|
||||
void stopResolution(int listenerKey);
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package android.net.nsd;
|
||||
|
||||
import android.annotation.IntDef;
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.RequiresPermission;
|
||||
@@ -44,6 +45,8 @@ import android.util.SparseArray;
|
||||
import com.android.internal.annotations.GuardedBy;
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@@ -230,7 +233,6 @@ public final class NsdManager {
|
||||
|
||||
/** @hide */
|
||||
public static final int DAEMON_CLEANUP = 18;
|
||||
|
||||
/** @hide */
|
||||
public static final int DAEMON_STARTUP = 19;
|
||||
|
||||
@@ -245,6 +247,13 @@ public final class NsdManager {
|
||||
/** @hide */
|
||||
public static final int MDNS_DISCOVERY_MANAGER_EVENT = 23;
|
||||
|
||||
/** @hide */
|
||||
public static final int STOP_RESOLUTION = 24;
|
||||
/** @hide */
|
||||
public static final int STOP_RESOLUTION_FAILED = 25;
|
||||
/** @hide */
|
||||
public static final int STOP_RESOLUTION_SUCCEEDED = 26;
|
||||
|
||||
/** Dns based service discovery protocol */
|
||||
public static final int PROTOCOL_DNS_SD = 0x0001;
|
||||
|
||||
@@ -270,6 +279,9 @@ public final class NsdManager {
|
||||
EVENT_NAMES.put(DAEMON_CLEANUP, "DAEMON_CLEANUP");
|
||||
EVENT_NAMES.put(DAEMON_STARTUP, "DAEMON_STARTUP");
|
||||
EVENT_NAMES.put(MDNS_SERVICE_EVENT, "MDNS_SERVICE_EVENT");
|
||||
EVENT_NAMES.put(STOP_RESOLUTION, "STOP_RESOLUTION");
|
||||
EVENT_NAMES.put(STOP_RESOLUTION_FAILED, "STOP_RESOLUTION_FAILED");
|
||||
EVENT_NAMES.put(STOP_RESOLUTION_SUCCEEDED, "STOP_RESOLUTION_SUCCEEDED");
|
||||
}
|
||||
|
||||
/** @hide */
|
||||
@@ -595,6 +607,16 @@ public final class NsdManager {
|
||||
public void onResolveServiceSucceeded(int listenerKey, NsdServiceInfo info) {
|
||||
sendInfo(RESOLVE_SERVICE_SUCCEEDED, listenerKey, info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopResolutionFailed(int listenerKey, int error) {
|
||||
sendError(STOP_RESOLUTION_FAILED, listenerKey, error);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopResolutionSucceeded(int listenerKey) {
|
||||
sendNoArg(STOP_RESOLUTION_SUCCEEDED, listenerKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -618,6 +640,20 @@ public final class NsdManager {
|
||||
*/
|
||||
public static final int FAILURE_MAX_LIMIT = 4;
|
||||
|
||||
/**
|
||||
* Indicates that the stop operation failed because it is not running.
|
||||
* This failure is passed with {@link ResolveListener#onStopResolutionFailed}.
|
||||
*/
|
||||
public static final int FAILURE_OPERATION_NOT_RUNNING = 5;
|
||||
|
||||
/** @hide */
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef(value = {
|
||||
FAILURE_OPERATION_NOT_RUNNING,
|
||||
})
|
||||
public @interface StopOperationFailureCode {
|
||||
}
|
||||
|
||||
/** Interface for callback invocation for service discovery */
|
||||
public interface DiscoveryListener {
|
||||
|
||||
@@ -646,12 +682,49 @@ public final class NsdManager {
|
||||
public void onServiceUnregistered(NsdServiceInfo serviceInfo);
|
||||
}
|
||||
|
||||
/** Interface for callback invocation for service resolution */
|
||||
/**
|
||||
* Callback for use with {@link NsdManager#resolveService} to resolve the service info and use
|
||||
* with {@link NsdManager#stopServiceResolution} to stop resolution.
|
||||
*/
|
||||
public interface ResolveListener {
|
||||
|
||||
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode);
|
||||
/**
|
||||
* Called on the internal thread or with an executor passed to
|
||||
* {@link NsdManager#resolveService} to report the resolution was failed with an error.
|
||||
*
|
||||
* A resolution operation would call either onServiceResolved or onResolveFailed once based
|
||||
* on the result.
|
||||
*/
|
||||
void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode);
|
||||
|
||||
public void onServiceResolved(NsdServiceInfo serviceInfo);
|
||||
/**
|
||||
* Called on the internal thread or with an executor passed to
|
||||
* {@link NsdManager#resolveService} to report the resolved service info.
|
||||
*
|
||||
* A resolution operation would call either onServiceResolved or onResolveFailed once based
|
||||
* on the result.
|
||||
*/
|
||||
void onServiceResolved(NsdServiceInfo serviceInfo);
|
||||
|
||||
/**
|
||||
* Called on the internal thread or with an executor passed to
|
||||
* {@link NsdManager#resolveService} to report the resolution was stopped.
|
||||
*
|
||||
* A stop resolution operation would call either onResolveStopped or onStopResolutionFailed
|
||||
* once based on the result.
|
||||
*/
|
||||
default void onResolveStopped(@NonNull NsdServiceInfo serviceInfo) { }
|
||||
|
||||
/**
|
||||
* Called once on the internal thread or with an executor passed to
|
||||
* {@link NsdManager#resolveService} to report that stopping resolution failed with an
|
||||
* error.
|
||||
*
|
||||
* A stop resolution operation would call either onResolveStopped or onStopResolutionFailed
|
||||
* once based on the result.
|
||||
*/
|
||||
default void onStopResolutionFailed(@NonNull NsdServiceInfo serviceInfo,
|
||||
@StopOperationFailureCode int errorCode) { }
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@@ -744,6 +817,16 @@ public final class NsdManager {
|
||||
executor.execute(() -> ((ResolveListener) listener).onServiceResolved(
|
||||
(NsdServiceInfo) obj));
|
||||
break;
|
||||
case STOP_RESOLUTION_FAILED:
|
||||
removeListener(key);
|
||||
executor.execute(() -> ((ResolveListener) listener).onStopResolutionFailed(
|
||||
ns, errorCode));
|
||||
break;
|
||||
case STOP_RESOLUTION_SUCCEEDED:
|
||||
removeListener(key);
|
||||
executor.execute(() -> ((ResolveListener) listener).onResolveStopped(
|
||||
ns));
|
||||
break;
|
||||
default:
|
||||
Log.d(TAG, "Ignored " + message);
|
||||
break;
|
||||
@@ -1079,6 +1162,29 @@ public final class NsdManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop service resolution initiated with {@link #resolveService}.
|
||||
*
|
||||
* A successful stop is notified with a call to {@link ResolveListener#onResolveStopped}.
|
||||
*
|
||||
* <p> Upon failure to stop service resolution for example if resolution is done or the
|
||||
* requester stops resolution repeatedly, the application is notified
|
||||
* {@link ResolveListener#onStopResolutionFailed} with {@link #FAILURE_OPERATION_NOT_RUNNING}
|
||||
*
|
||||
* @param listener This should be a listener object that was passed to {@link #resolveService}.
|
||||
* It identifies the resolution that should be stopped and notifies of a
|
||||
* successful or unsuccessful stop. Throws {@code IllegalArgumentException} if
|
||||
* the listener was not passed to resolveService before.
|
||||
*/
|
||||
public void stopServiceResolution(@NonNull ResolveListener listener) {
|
||||
int id = getListenerKey(listener);
|
||||
try {
|
||||
mService.stopResolution(id);
|
||||
} catch (RemoteException e) {
|
||||
e.rethrowFromSystemServer();
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkListener(Object listener) {
|
||||
Objects.requireNonNull(listener, "listener cannot be null");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user