Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6059c583 authored by Yifei Zhang's avatar Yifei Zhang
Browse files

contexthub: add findEndpoints(String) for service discovery

- Add findEndpoints(String) to allow client to query for the endpoints
  with service descriptor.

Test: build
API-Coverage-Bug: 377554469
Bug: 375487784
Bug: 379323274
Flag: android.chre.flags.offload_api
Change-Id: I7016caa851b96a521bb86656d42795c3b1e9d405
parent d8c6f1d8
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -5220,6 +5220,7 @@ package android.hardware.contexthub {
  @FlaggedApi("android.chre.flags.offload_api") public class HubDiscoveryInfo {
    method @NonNull public android.hardware.contexthub.HubEndpointInfo getHubEndpointInfo();
    method @Nullable public android.hardware.contexthub.HubServiceInfo getHubServiceInfo();
  }
  @FlaggedApi("android.chre.flags.offload_api") public class HubEndpoint {
@@ -6312,6 +6313,7 @@ package android.hardware.location {
    method @NonNull @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) public android.hardware.location.ContextHubTransaction<java.lang.Void> disableNanoApp(@NonNull android.hardware.location.ContextHubInfo, long);
    method @NonNull @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) public android.hardware.location.ContextHubTransaction<java.lang.Void> enableNanoApp(@NonNull android.hardware.location.ContextHubInfo, long);
    method @FlaggedApi("android.chre.flags.offload_api") @NonNull @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) public java.util.List<android.hardware.contexthub.HubDiscoveryInfo> findEndpoints(long);
    method @FlaggedApi("android.chre.flags.offload_api") @NonNull @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) public java.util.List<android.hardware.contexthub.HubDiscoveryInfo> findEndpoints(@NonNull String);
    method @Deprecated @NonNull @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) public int[] findNanoAppOnHub(int, @NonNull android.hardware.location.NanoAppFilter);
    method @Deprecated @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) public int[] getContextHubHandles();
    method @Deprecated @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) public android.hardware.location.ContextHubInfo getContextHubInfo(int);
+30 −13
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.hardware.contexthub;

import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.chre.flags.Flags;
import android.hardware.location.ContextHubManager;
@@ -26,31 +27,47 @@ import android.hardware.location.ContextHubManager;
 * Class that represents the result of from an hub endpoint discovery.
 *
 * <p>The type is returned from an endpoint discovery query via {@link
 * ContextHubManager#findEndpoints}. Application may use the values {@link #getHubEndpointInfo} to
 * retrieve the {@link HubEndpointInfo} that describes the endpoint that matches the query. The
 * class provides flexibility in returning more information (e.g. service provided by the endpoint)
 * in addition to the information about the endpoint.
 * ContextHubManager#findEndpoints}.
 *
 * <p>Application may use the values {@link #getHubEndpointInfo} to retrieve the {@link
 * HubEndpointInfo} that describes the endpoint that matches the query.
 *
 * <p>Application may use the values {@link #getHubServiceInfo()} to retrieve the {@link
 * HubServiceInfo} that describes the service that matches the query.
 *
 * @hide
 */
@SystemApi
@FlaggedApi(Flags.FLAG_OFFLOAD_API)
public class HubDiscoveryInfo {
    // TODO(b/375487784): Add ServiceInfo to the result.
    android.hardware.contexthub.HubEndpointInfo mEndpointInfo;
    @NonNull private final HubEndpointInfo mEndpointInfo;
    @Nullable private final HubServiceInfo mServiceInfo;

    /**
     * Constructor for internal use.
     *
     * @hide
     */
    public HubDiscoveryInfo(android.hardware.contexthub.HubEndpointInfo endpointInfo) {
    /** @hide */
    public HubDiscoveryInfo(@NonNull HubEndpointInfo endpointInfo) {
        mEndpointInfo = endpointInfo;
        mServiceInfo = null;
    }

    /** Get the {@link android.hardware.contexthub.HubEndpointInfo} for the endpoint found. */
    /** @hide */
    public HubDiscoveryInfo(
            @NonNull HubEndpointInfo endpointInfo, @NonNull HubServiceInfo serviceInfo) {
        mEndpointInfo = endpointInfo;
        mServiceInfo = serviceInfo;
    }

    /** Get the {@link HubEndpointInfo} for the endpoint found. */
    @NonNull
    public HubEndpointInfo getHubEndpointInfo() {
        return mEndpointInfo;
    }

    /**
     * Get the {@link HubServiceInfo} for the endpoint found. The value will be null if there is no
     * service info specified in the query.
     */
    @Nullable
    public HubServiceInfo getHubServiceInfo() {
        return mServiceInfo;
    }
}
+36 −0
Original line number Diff line number Diff line
@@ -694,6 +694,8 @@ public final class ContextHubManager {
    @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB)
    @NonNull
    public List<HubDiscoveryInfo> findEndpoints(long endpointId) {
        // TODO(b/379323274): Consider improving these getters to avoid racing with nano app load
        //  timing.
        try {
            List<HubEndpointInfo> endpointInfos = mService.findEndpoints(endpointId);
            List<HubDiscoveryInfo> results = new ArrayList<>(endpointInfos.size());
@@ -707,6 +709,40 @@ public final class ContextHubManager {
        }
    }

    /**
     * Find a list of endpoints that provides a specific service.
     *
     * @param serviceDescriptor Statically generated ID for an endpoint.
     * @return A list of {@link HubDiscoveryInfo} objects that represents the result of discovery.
     * @throws IllegalArgumentException if the serviceDescriptor is empty/null.
     */
    @FlaggedApi(Flags.FLAG_OFFLOAD_API)
    @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB)
    @NonNull
    public List<HubDiscoveryInfo> findEndpoints(@NonNull String serviceDescriptor) {
        // TODO(b/379323274): Consider improving these getters to avoid racing with nano app load
        //  timing.
        if (serviceDescriptor.isBlank()) {
            throw new IllegalArgumentException("Invalid service descriptor: " + serviceDescriptor);
        }
        try {
            List<HubEndpointInfo> endpointInfos =
                    mService.findEndpointsWithService(serviceDescriptor);
            List<HubDiscoveryInfo> results = new ArrayList<>(endpointInfos.size());
            // Wrap with result type
            for (HubEndpointInfo endpointInfo : endpointInfos) {
                for (HubServiceInfo serviceInfo : endpointInfo.getServiceInfoCollection()) {
                    if (serviceInfo.getServiceDescriptor().equals(serviceDescriptor)) {
                        results.add(new HubDiscoveryInfo(endpointInfo, serviceInfo));
                    }
                }
            }
            return results;
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Set a callback to receive messages from the context hub
     *
+5 −1
Original line number Diff line number Diff line
@@ -126,10 +126,14 @@ interface IContextHubService {
    @EnforcePermission("ACCESS_CONTEXT_HUB")
    boolean setTestMode(in boolean enable);

    // Finds all endpoints that havea specific ID
    // Finds all endpoints that has a specific ID
    @EnforcePermission("ACCESS_CONTEXT_HUB")
    List<HubEndpointInfo> findEndpoints(long endpointId);

    // Finds all endpoints that has a specific service
    @EnforcePermission("ACCESS_CONTEXT_HUB")
    List<HubEndpointInfo> findEndpointsWithService(String service);

    // Register an endpoint with the context hub
    @EnforcePermission("ACCESS_CONTEXT_HUB")
    IContextHubEndpoint registerEndpoint(in HubEndpointInfo pendingEndpointInfo, in IContextHubEndpointCallback callback);
+10 −0
Original line number Diff line number Diff line
@@ -766,6 +766,16 @@ public class ContextHubService extends IContextHubService.Stub {
        return mHubInfoRegistry.findEndpoints(endpointId);
    }

    @android.annotation.EnforcePermission(android.Manifest.permission.ACCESS_CONTEXT_HUB)
    @Override
    public List<HubEndpointInfo> findEndpointsWithService(String serviceDescriptor) {
        super.findEndpointsWithService_enforcePermission();
        if (mHubInfoRegistry == null) {
            return Collections.emptyList();
        }
        return mHubInfoRegistry.findEndpointsWithService(serviceDescriptor);
    }

    @android.annotation.EnforcePermission(android.Manifest.permission.ACCESS_CONTEXT_HUB)
    @Override
    public IContextHubEndpoint registerEndpoint(
Loading