diff options
| author | 2024-11-14 23:28:19 -0800 | |
|---|---|---|
| committer | 2024-11-15 15:10:57 -0800 | |
| commit | 6059c5830d3f0ee07ef6a9c36ae7ae6151c9db4f (patch) | |
| tree | f90aa5aac40309b19608ec35cc2b12c6c563f08d | |
| parent | d8c6f1d884b5720366f013492ae825adae29dfe6 (diff) | |
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
6 files changed, 101 insertions, 15 deletions
diff --git a/core/api/system-current.txt b/core/api/system-current.txt index 0aa5921be4cf..388c88dde226 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -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); diff --git a/core/java/android/hardware/contexthub/HubDiscoveryInfo.java b/core/java/android/hardware/contexthub/HubDiscoveryInfo.java index 875c4b4182be..581040dbfa56 100644 --- a/core/java/android/hardware/contexthub/HubDiscoveryInfo.java +++ b/core/java/android/hardware/contexthub/HubDiscoveryInfo.java @@ -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; + } } diff --git a/core/java/android/hardware/location/ContextHubManager.java b/core/java/android/hardware/location/ContextHubManager.java index 24485a8c295c..426cd69f76a0 100644 --- a/core/java/android/hardware/location/ContextHubManager.java +++ b/core/java/android/hardware/location/ContextHubManager.java @@ -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()); @@ -708,6 +710,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 * * @param callback Callback object diff --git a/core/java/android/hardware/location/IContextHubService.aidl b/core/java/android/hardware/location/IContextHubService.aidl index 512872303291..f9f412446038 100644 --- a/core/java/android/hardware/location/IContextHubService.aidl +++ b/core/java/android/hardware/location/IContextHubService.aidl @@ -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); diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubService.java b/services/core/java/com/android/server/location/contexthub/ContextHubService.java index 8cf0578523ad..946e89604553 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubService.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubService.java @@ -768,6 +768,16 @@ public class ContextHubService extends IContextHubService.Stub { @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( HubEndpointInfo pendingHubEndpointInfo, IContextHubEndpointCallback callback) throws RemoteException { diff --git a/services/core/java/com/android/server/location/contexthub/HubInfoRegistry.java b/services/core/java/com/android/server/location/contexthub/HubInfoRegistry.java index 4d1000f3e0e5..d2b2331d54f3 100644 --- a/services/core/java/com/android/server/location/contexthub/HubInfoRegistry.java +++ b/services/core/java/com/android/server/location/contexthub/HubInfoRegistry.java @@ -17,6 +17,7 @@ package com.android.server.location.contexthub; import android.hardware.contexthub.HubEndpointInfo; +import android.hardware.contexthub.HubServiceInfo; import android.hardware.location.HubInfo; import android.os.RemoteException; import android.util.ArrayMap; @@ -127,6 +128,23 @@ class HubInfoRegistry implements ContextHubHalEndpointCallback.IEndpointLifecycl return searchResult; } + /** + * Return a list of {@link HubEndpointInfo} that represents endpoints with the matching service. + */ + public List<HubEndpointInfo> findEndpointsWithService(String serviceDescriptor) { + List<HubEndpointInfo> searchResult = new ArrayList<>(); + synchronized (mLock) { + for (HubEndpointInfo endpointInfo : mHubEndpointInfos.values()) { + for (HubServiceInfo serviceInfo : endpointInfo.getServiceInfoCollection()) { + if (serviceDescriptor.equals(serviceInfo.getServiceDescriptor())) { + searchResult.add(endpointInfo); + } + } + } + } + return searchResult; + } + void dump(IndentingPrintWriter ipw) { synchronized (mLock) { dumpLocked(ipw); @@ -155,5 +173,4 @@ class HubInfoRegistry implements ContextHubHalEndpointCallback.IEndpointLifecycl ipw.println(); } - } |