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

Commit 279477cd authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Refactors common code in discovery callback logic

Bug: 380293951
Flag: android.chre.flags.offload_api
Test: Compile
Change-Id: Iaeecb46db95767d02d763fe89cf4cdba92d249a8
parent 1c4ccb8e
Loading
Loading
Loading
Loading
+33 −27
Original line number Diff line number Diff line
@@ -754,6 +754,7 @@ public final class ContextHubManager {
     * @param executor the executor to invoke callbacks for this client
     * @return the callback interface
     */
    @FlaggedApi(Flags.FLAG_OFFLOAD_API)
    private IContextHubEndpointDiscoveryCallback createDiscoveryCallback(
            IHubEndpointDiscoveryCallback callback,
            Executor executor,
@@ -767,21 +768,9 @@ public final class ContextHubManager {
                }
                executor.execute(
                        () -> {
                            // TODO(b/380293951): Refactor
                            List<HubDiscoveryInfo> discoveryList =
                                    new ArrayList<>(hubEndpointInfoList.length);
                            for (HubEndpointInfo info : hubEndpointInfoList) {
                                if (serviceDescriptor != null) {
                                    for (HubServiceInfo sInfo : info.getServiceInfoCollection()) {
                                        if (sInfo.getServiceDescriptor()
                                                .equals(serviceDescriptor)) {
                                            discoveryList.add(new HubDiscoveryInfo(info, sInfo));
                                        }
                                    }
                                } else {
                                    discoveryList.add(new HubDiscoveryInfo(info));
                                }
                            }
                                    getMatchingEndpointDiscoveryList(
                                            hubEndpointInfoList, serviceDescriptor);
                            if (discoveryList.isEmpty()) {
                                Log.w(TAG, "onEndpointsStarted: no matching service descriptor");
                            } else {
@@ -799,12 +788,36 @@ public final class ContextHubManager {
                executor.execute(
                        () -> {
                            List<HubDiscoveryInfo> discoveryList =
                                    new ArrayList<>(hubEndpointInfoList.length);
                                    getMatchingEndpointDiscoveryList(
                                            hubEndpointInfoList, serviceDescriptor);
                            if (discoveryList.isEmpty()) {
                                Log.w(TAG, "onEndpointsStopped: no matching service descriptor");
                            } else {
                                callback.onEndpointsStopped(discoveryList, reason);
                            }
                        });
            }
        };
    }

    /**
     * Generates a list of matching endpoint discovery info, given the list and an (optional)
     * service descriptor. If service descriptor is null, all endpoints are added to the filtered
     * output list.
     *
     * @param hubEndpointInfoList The hub endpoints to filter.
     * @param serviceDescriptor The optional service descriptor to match, null if adding all
     *     endpoints.
     * @return The list of filtered HubDiscoveryInfo which matches the serviceDescriptor.
     */
    @FlaggedApi(Flags.FLAG_OFFLOAD_API)
    private List<HubDiscoveryInfo> getMatchingEndpointDiscoveryList(
            HubEndpointInfo[] hubEndpointInfoList, @Nullable String serviceDescriptor) {
        List<HubDiscoveryInfo> discoveryList = new ArrayList<>(hubEndpointInfoList.length);
        for (HubEndpointInfo info : hubEndpointInfoList) {
            if (serviceDescriptor != null) {
                for (HubServiceInfo sInfo : info.getServiceInfoCollection()) {
                                        if (sInfo.getServiceDescriptor()
                                                .equals(serviceDescriptor)) {
                    if (sInfo.getServiceDescriptor().equals(serviceDescriptor)) {
                        discoveryList.add(new HubDiscoveryInfo(info, sInfo));
                    }
                }
@@ -812,14 +825,7 @@ public final class ContextHubManager {
                discoveryList.add(new HubDiscoveryInfo(info));
            }
        }
                            if (discoveryList.isEmpty()) {
                                Log.w(TAG, "onEndpointsStopped: no matching service descriptor");
                            } else {
                                callback.onEndpointsStopped(discoveryList, reason);
                            }
                        });
            }
        };
        return discoveryList;
    }

    /**