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

Commit 1f53e1e9 authored by Yifei Zhang's avatar Yifei Zhang
Browse files

contexthub: add missing pieces of HubEndpointInfo

- Add getter and setter for version
- Add getter for type and requiredPermissions

Test: build
API-Coverage-Bug: 377554469
Bug: 375487784
Flag: android.chre.flags.offload_api
Change-Id: I353bcc3a9d6f3cb8cfe89273ba2c9d0307a74fcb
parent 6059c583
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -5228,6 +5228,7 @@ package android.hardware.contexthub {
    method @Nullable public android.hardware.contexthub.IHubEndpointMessageCallback getMessageCallback();
    method @NonNull public java.util.Collection<android.hardware.contexthub.HubServiceInfo> getServiceInfoCollection();
    method @Nullable public String getTag();
    method public int getVersion();
  }
  public static final class HubEndpoint.Builder {
@@ -5245,10 +5246,18 @@ package android.hardware.contexthub {
    method public int describeContents();
    method @NonNull public android.hardware.contexthub.HubEndpointInfo.HubEndpointIdentifier getIdentifier();
    method @NonNull public String getName();
    method @NonNull public java.util.Collection<java.lang.String> getRequiredPermissions();
    method @NonNull public java.util.Collection<android.hardware.contexthub.HubServiceInfo> getServiceInfoCollection();
    method @Nullable public String getTag();
    method public int getType();
    method public int getVersion();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.hardware.contexthub.HubEndpointInfo> CREATOR;
    field public static final int TYPE_APP = 2; // 0x2
    field public static final int TYPE_FRAMEWORK = 1; // 0x1
    field public static final int TYPE_HUB_ENDPOINT = 5; // 0x5
    field public static final int TYPE_NANOAPP = 4; // 0x4
    field public static final int TYPE_NATIVE = 3; // 0x3
  }
  public static class HubEndpointInfo.HubEndpointIdentifier {
+18 −1
Original line number Diff line number Diff line
@@ -423,6 +423,10 @@ public class HubEndpoint {
        }
    }

    public int getVersion() {
        return mPendingHubEndpointInfo.getVersion();
    }

    @Nullable
    public String getTag() {
        return mPendingHubEndpointInfo.getTag();
@@ -454,6 +458,7 @@ public class HubEndpoint {
        @Nullable private IHubEndpointMessageCallback mMessageCallback;
        @NonNull private Executor mMessageCallbackExecutor;

        private int mVersion;
        @Nullable private String mTag;

        private List<HubServiceInfo> mServiceInfos = Collections.emptyList();
@@ -461,10 +466,22 @@ public class HubEndpoint {
        /** Create a builder for {@link HubEndpoint} */
        public Builder(@NonNull Context context) {
            mPackageName = context.getPackageName();
            mVersion = (int) context.getApplicationInfo().longVersionCode;
            mLifecycleCallbackExecutor = context.getMainExecutor();
            mMessageCallbackExecutor = context.getMainExecutor();
        }

        /**
         * Set the version for the endpoint. Default is 0.
         *
         * @hide
         */
        @NonNull
        public Builder setVersion(int version) {
            mVersion = version;
            return this;
        }

        /**
         * Set a tag string. The tag can be used to further identify the creator of the endpoint.
         * Endpoints created by the same package share the same name but should have different tags.
@@ -532,7 +549,7 @@ public class HubEndpoint {
        @NonNull
        public HubEndpoint build() {
            return new HubEndpoint(
                    new HubEndpointInfo(mPackageName, mTag, mServiceInfos),
                    new HubEndpointInfo(mPackageName, mVersion, mTag, mServiceInfos),
                    mLifecycleCallback,
                    mLifecycleCallbackExecutor,
                    mMessageCallback,
+109 −3
Original line number Diff line number Diff line
@@ -17,14 +17,19 @@
package android.hardware.contexthub;

import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.chre.flags.Flags;
import android.os.Parcel;
import android.os.Parcelable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -106,19 +111,49 @@ public final class HubEndpointInfo implements Parcelable {
        }
    }

    /** This endpoint is from the Android framework */
    public static final int TYPE_FRAMEWORK = 1;

    /** This endpoint is from an Android app */
    public static final int TYPE_APP = 2;

    /** This endpoint is from an Android native program. */
    public static final int TYPE_NATIVE = 3;

    /** This endpoint is from a nanoapp. */
    public static final int TYPE_NANOAPP = 4;

    /** This endpoint is a generic endpoint served by a hub (not from a nanoapp). */
    public static final int TYPE_HUB_ENDPOINT = 5;

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
        TYPE_FRAMEWORK,
        TYPE_APP,
        TYPE_NATIVE,
        TYPE_NANOAPP,
        TYPE_HUB_ENDPOINT,
    })
    public @interface EndpointType {}

    private final HubEndpointIdentifier mId;
    @EndpointType private final int mType;
    private final String mName;
    private final int mVersion;
    @Nullable private final String mTag;

    @NonNull private final List<String> mRequiredPermissions;
    @NonNull private final List<HubServiceInfo> mHubServiceInfos;

    // 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);
        mType = endpointInfo.type;
        mName = endpointInfo.name;
        mVersion = endpointInfo.version;
        mTag = endpointInfo.tag;
        mRequiredPermissions = Arrays.asList(endpointInfo.requiredPermissions);
        mHubServiceInfos = new ArrayList<>(endpointInfo.services.length);
        for (int i = 0; i < endpointInfo.services.length; i++) {
            mHubServiceInfos.set(i, new HubServiceInfo(endpointInfo.services[i]));
@@ -127,10 +162,16 @@ public final class HubEndpointInfo implements Parcelable {

    /** @hide */
    public HubEndpointInfo(
            String name, @Nullable String tag, @NonNull List<HubServiceInfo> hubServiceInfos) {
            String name,
            int version,
            @Nullable String tag,
            @NonNull List<HubServiceInfo> hubServiceInfos) {
        mId = HubEndpointIdentifier.invalid();
        mType = TYPE_APP;
        mName = name;
        mVersion = version;
        mTag = tag;
        mRequiredPermissions = Collections.emptyList();
        mHubServiceInfos = hubServiceInfos;
    }

@@ -138,8 +179,12 @@ public final class HubEndpointInfo implements Parcelable {
        long hubId = in.readLong();
        long endpointId = in.readLong();
        mId = new HubEndpointIdentifier(hubId, endpointId);
        mType = in.readInt();
        mName = in.readString();
        mVersion = in.readInt();
        mTag = in.readString();
        mRequiredPermissions = new ArrayList<>();
        in.readStringList(mRequiredPermissions);
        mHubServiceInfos = new ArrayList<>();
        in.readTypedList(mHubServiceInfos, HubServiceInfo.CREATOR);
    }
@@ -159,8 +204,11 @@ public final class HubEndpointInfo implements Parcelable {
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeLong(mId.getHub());
        dest.writeLong(mId.getEndpoint());
        dest.writeInt(mType);
        dest.writeString(mName);
        dest.writeInt(mVersion);
        dest.writeString(mTag);
        dest.writeStringList(mRequiredPermissions);
        dest.writeTypedList(mHubServiceInfos, flags);
    }

@@ -170,12 +218,50 @@ public final class HubEndpointInfo implements Parcelable {
        return mId;
    }

    /**
     * Get the type of this endpoint. Application may use this field to get more information about
     * who registered this endpoint for diagnostic purposes.
     *
     * <p>Type can be one of {@link HubEndpointInfo#TYPE_APP}, {@link
     * HubEndpointInfo#TYPE_FRAMEWORK}, {@link HubEndpointInfo#TYPE_NANOAPP}, {@link
     * HubEndpointInfo#TYPE_NATIVE} or {@link HubEndpointInfo#TYPE_HUB_ENDPOINT}.
     */
    public int getType() {
        return mType;
    }

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

    /**
     * Get the version of this endpoint.
     *
     * <p>Monotonically increasing version number. The two sides of an endpoint session can use this
     * version number to identify the other side and determine compatibility with each other. The
     * interpretation of the version number is specific to the implementation of an endpoint.
     *
     * <p>The version number should not be used to compare endpoints implementation freshness for
     * different endpoint types.
     *
     * <p>Depending on type of the endpoint, the following values (and formats) are used:
     *
     * <ol>
     *   <li>{@link #TYPE_FRAMEWORK}: android.os.Build.VERSION.SDK_INT_FULL
     *   <li>{@link #TYPE_APP}: versionCode
     *   <li>{@link #TYPE_NATIVE}: unspecified format (supplied by endpoint code)
     *   <li>{@link #TYPE_NANOAPP}: nanoapp version, typically following 0xMMmmpppp scheme where MM
     *       = major version, mm = minor version, pppp = patch version
     *   <li>{@link #TYPE_HUB_ENDPOINT}: unspecified format (supplied by endpoint code), following
     *       nanoapp versioning scheme is recommended
     * </ol>
     */
    public int getVersion() {
        return mVersion;
    }

    /**
     * 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
@@ -187,6 +273,26 @@ public final class HubEndpointInfo implements Parcelable {
        return mTag;
    }

    /**
     * Get the list of required permissions in order to talk to this endpoint.
     *
     * <p>This list is enforced by the Context Hub Service. The app would need to have the required
     * permissions list to open a session with this particular endpoint. Otherwise this will be
     * rejected by as permission failures.
     *
     * <p>This is mostly for allowing app to check what permission it needs first internally. App
     * will need to request permissions grant at runtime if not already granted. See {@link
     * android.content.Context#checkPermission} for more details.
     *
     * <p>See {@link android.Manifest.permission} for a list of standard Android permissions as
     * possible values.
     */
    @SuppressLint("RequiresPermission")
    @NonNull
    public Collection<String> getRequiredPermissions() {
        return Collections.unmodifiableList(mRequiredPermissions);
    }

    /**
     * Get the list of services provided by this endpoint.
     *