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

Commit 880cf047 authored by Yifei Zhang's avatar Yifei Zhang Committed by Android (Google) Code Review
Browse files

Merge "contexthub: create basic findEndpoints EndpointInfo API" into main

parents c42073f4 17a02e66
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -5171,6 +5171,28 @@ package android.hardware.camera2.params {
}
package android.hardware.contexthub {
  @FlaggedApi("android.chre.flags.offload_api") public class HubDiscoveryInfo {
    method @NonNull public android.hardware.contexthub.HubEndpointInfo getHubEndpointInfo();
  }
  @FlaggedApi("android.chre.flags.offload_api") public final class HubEndpointInfo implements android.os.Parcelable {
    method public int describeContents();
    method @NonNull public android.hardware.contexthub.HubEndpointInfo.HubEndpointIdentifier getIdentifier();
    method @NonNull public String getName();
    method @Nullable public String getTag();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.hardware.contexthub.HubEndpointInfo> CREATOR;
  }
  public static class HubEndpointInfo.HubEndpointIdentifier {
    method public long getEndpoint();
    method public long getHub();
  }
}
package android.hardware.devicestate {
  @FlaggedApi("android.hardware.devicestate.feature.flags.device_state_property_api") public final class DeviceState {
@@ -6162,6 +6184,7 @@ package android.hardware.location {
    method @NonNull @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) public android.hardware.location.ContextHubClient createClient(@NonNull android.hardware.location.ContextHubInfo, @NonNull android.app.PendingIntent, long);
    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 @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);
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.contexthub;

import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.chre.flags.Flags;
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.
 *
 * @hide
 */
@SystemApi
@FlaggedApi(Flags.FLAG_OFFLOAD_API)
public class HubDiscoveryInfo {
    // TODO(b/375487784): Add ServiceInfo to the result.
    android.hardware.contexthub.HubEndpointInfo mEndpointInfo;

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

    /** Get the {@link android.hardware.contexthub.HubEndpointInfo} for the endpoint found. */
    @NonNull
    public HubEndpointInfo getHubEndpointInfo() {
        return mEndpointInfo;
    }
}
+20 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.contexthub;

/** @hide */
parcelable HubEndpointInfo;
+189 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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.os.Parcel;
import android.os.Parcelable;

import java.util.Objects;

/**
 * Parcelable representing an endpoint from ContextHub or VendorHub.
 *
 * <p>HubEndpointInfo contains information about an endpoint, including its name, tag and other
 * information. A HubEndpointInfo object can be used to accurately identify a specific endpoint.
 * Application can use this object to identify and describe an endpoint.
 *
 * <p>See: {@link android.hardware.location.ContextHubManager#findEndpoints} for how to retrieve
 * {@link HubEndpointInfo} for endpoints on a hub.
 *
 * @hide
 */
@SystemApi
@FlaggedApi(Flags.FLAG_OFFLOAD_API)
public final class HubEndpointInfo implements Parcelable {
    /**
     * A unique identifier for one endpoint. A unique identifier for one endpoint consists of two
     * parts: (1) a unique long number for a hub and (2) a long number for the endpoint, unique
     * within a hub. This class overrides equality methods and can be used to compare if two
     * endpoints are the same.
     */
    public static class HubEndpointIdentifier {
        private final long mEndpointId;
        private final long mHubId;

        /** @hide */
        public HubEndpointIdentifier(long hubId, long endpointId) {
            mEndpointId = endpointId;
            mHubId = hubId;
        }

        /** @hide */
        public HubEndpointIdentifier(android.hardware.contexthub.EndpointId halEndpointId) {
            mEndpointId = halEndpointId.id;
            mHubId = halEndpointId.hubId;
        }

        /** Get the endpoint portion of the identifier. */
        public long getEndpoint() {
            return mEndpointId;
        }

        /** Get the hub portion of the identifier. */
        public long getHub() {
            return mHubId;
        }

        /**
         * Create an invalid endpoint id, to represent endpoint that are not yet registered with the
         * HAL.
         *
         * @hide
         */
        public static HubEndpointIdentifier invalid() {
            return new HubEndpointIdentifier(
                    android.hardware.contexthub.HubInfo.HUB_ID_INVALID,
                    android.hardware.contexthub.EndpointId.ENDPOINT_ID_INVALID);
        }

        @Override
        public int hashCode() {
            return Objects.hash(mEndpointId, mHubId);
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof HubEndpointIdentifier other)) {
                return false;
            }
            if (other.mHubId != mHubId) {
                return false;
            }
            return other.mEndpointId == mEndpointId;
        }
    }

    private final HubEndpointIdentifier mId;
    private final String mName;
    @Nullable private final String mTag;

    // TODO(b/375487784): Add Service/version and other information to this object

    /** @hide */
    public HubEndpointInfo(android.hardware.contexthub.EndpointInfo endpointInfo) {
        mId = new HubEndpointIdentifier(endpointInfo.id.hubId, endpointInfo.id.id);
        mName = endpointInfo.name;
        mTag = endpointInfo.tag;
    }

    private HubEndpointInfo(Parcel in) {
        long hubId = in.readLong();
        long endpointId = in.readLong();
        mName = in.readString();
        mTag = in.readString();

        mId = new HubEndpointIdentifier(hubId, endpointId);
    }

    /** Parcel implementation details */
    @Override
    public int describeContents() {
        return 0;
    }

    /** Parcel implementation details */
    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeLong(mId.getHub());
        dest.writeLong(mId.getEndpoint());
        dest.writeString(mName);
        dest.writeString(mTag);
    }

    /** Get a unique identifier for this endpoint. */
    @NonNull
    public HubEndpointIdentifier getIdentifier() {
        return mId;
    }

    /** Get the human-readable name of this endpoint (for debugging purposes). */
    @NonNull
    public String getName() {
        return mName;
    }

    /**
     * Get the tag that further identifies the submodule that created this endpoint. For example, a
     * single application could provide multiple endpoints. These endpoints will share the same
     * name, but will have different tags. This tag can be used to identify the submodule within the
     * application that provided the endpoint.
     */
    @Nullable
    public String getTag() {
        return mTag;
    }

    @Override
    public String toString() {
        StringBuilder out = new StringBuilder();
        out.append("Endpoint [0x");
        out.append(Long.toHexString(mId.getEndpoint()));
        out.append("@ Hub 0x");
        out.append(Long.toHexString(mId.getHub()));
        out.append("] Name=");
        out.append(mName);
        out.append(", Tag=");
        out.append(mTag);
        return out.toString();
    }

    public static final @android.annotation.NonNull Creator<HubEndpointInfo> CREATOR =
            new Creator<>() {
                public HubEndpointInfo createFromParcel(Parcel in) {
                    return new HubEndpointInfo(in);
                }

                public HubEndpointInfo[] newArray(int size) {
                    return new HubEndpointInfo[size];
                }
            };
}
+26 −0
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ import android.chre.flags.Flags;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.contexthub.ErrorCode;
import android.hardware.contexthub.HubDiscoveryInfo;
import android.hardware.contexthub.HubEndpointInfo;
import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.Looper;
@@ -42,6 +44,7 @@ import android.util.Log;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
@@ -678,6 +681,29 @@ public final class ContextHubManager {
        return transaction;
    }

    /**
     * Find a list of endpoints that matches a specific ID.
     *
     * @param endpointId Statically generated ID for an endpoint.
     * @return A list of {@link HubDiscoveryInfo} objects that represents the result of discovery.
     */
    @FlaggedApi(Flags.FLAG_OFFLOAD_API)
    @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB)
    @NonNull
    public List<HubDiscoveryInfo> findEndpoints(long endpointId) {
        try {
            List<HubEndpointInfo> endpointInfos = mService.findEndpoints(endpointId);
            List<HubDiscoveryInfo> results = new ArrayList<>(endpointInfos.size());
            // Wrap with result type
            for (HubEndpointInfo endpointInfo : endpointInfos) {
                results.add(new HubDiscoveryInfo(endpointInfo));
            }
            return results;
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Set a callback to receive messages from the context hub
     *
Loading