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

Commit cad5bb11 authored by Yifei Zhang's avatar Yifei Zhang
Browse files

contexthub: add new getHubs hidden API for V4

- Add hidden API ContextHubManager.getHubs() -> HubInfo
- HubInfo is a union type for VendorHubInfo (newly introduced) and
  ContextHubInfo
- Create HubInfoRegistry for tracking hub and endpoint info

Refactor
- Renamed getHubs to getContextHubs for some internal methods

Sample dumpsys for HubInfo: https://paste.googleplex.com/4989897428697088?raw

Test: manual
Bug: 375487784
Flag: android.chre.flags.offload_api
Change-Id: I841ff14b5378fc1d4df3a0c6b0e6fc082a189059
parent 7045c543
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -220,7 +220,7 @@ java_library {
        "android.hardware.contexthub-V1.0-java",
        "android.hardware.contexthub-V1.1-java",
        "android.hardware.contexthub-V1.2-java",
        "android.hardware.contexthub-V3-java",
        "android.hardware.contexthub-V4-java",
        "android.hardware.gnss-V1.0-java",
        "android.hardware.gnss-V2.1-java",
        "android.hardware.health-V1.0-java-constants",
+0 −1
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
 */
package android.hardware.location;

import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
+28 −10
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.hardware.location;
import static java.util.Objects.requireNonNull;

import android.annotation.CallbackExecutor;
import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -31,7 +32,6 @@ import android.app.ActivityThread;
import android.app.PendingIntent;
import android.chre.flags.Flags;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.contexthub.ErrorCode;
import android.os.Handler;
@@ -484,13 +484,31 @@ public final class ContextHubManager {
        }
    }

    /**
     * Returns the list of HubInfo objects describing the available hubs (including ContextHub and
     * VendorHub). This method is primarily used for debugging purposes as most clients care about
     * endpoints and services more than hubs.
     *
     * @return the list of HubInfo objects
     * @see HubInfo
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB)
    @NonNull
    @FlaggedApi(Flags.FLAG_OFFLOAD_API)
    public List<HubInfo> getHubs() {
        try {
            return mService.getHubs();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Helper function to generate a stub for a query transaction callback.
     *
     * @param transaction the transaction to unblock when complete
    *
     * @return the callback
    *
     * @hide
     */
    private IContextHubTransactionCallback createQueryCallback(
+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.location;

/** @hide */
parcelable HubInfo;
+153 −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.location;

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

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Union type for {@link ContextHubInfo} and {@link VendorHubInfo}
 *
 * @hide
 */
@FlaggedApi(Flags.FLAG_OFFLOAD_API)
public final class HubInfo implements Parcelable {

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(value = {TYPE_CONTEXT_HUB, TYPE_VENDOR_HUB})
    private @interface HubType {}

    public static final int TYPE_CONTEXT_HUB = 0;
    public static final int TYPE_VENDOR_HUB = 1;

    private final long mId;
    @HubType private final int mType;
    @Nullable private final ContextHubInfo mContextHubInfo;
    @Nullable private final VendorHubInfo mVendorHubInfo;

    /** @hide */
    public HubInfo(long id, @NonNull ContextHubInfo contextHubInfo) {
        mId = id;
        mType = TYPE_CONTEXT_HUB;
        mContextHubInfo = contextHubInfo;
        mVendorHubInfo = null;
    }

    /** @hide */
    public HubInfo(long id, @NonNull VendorHubInfo vendorHubInfo) {
        mId = id;
        mType = TYPE_VENDOR_HUB;
        mContextHubInfo = null;
        mVendorHubInfo = vendorHubInfo;
    }

    private HubInfo(Parcel in) {
        mId = in.readLong();
        mType = in.readInt();

        switch (mType) {
            case TYPE_CONTEXT_HUB:
                mContextHubInfo = ContextHubInfo.CREATOR.createFromParcel(in);
                mVendorHubInfo = null;
                break;
            case TYPE_VENDOR_HUB:
                mVendorHubInfo = VendorHubInfo.CREATOR.createFromParcel(in);
                mContextHubInfo = null;
                break;
            default:
                throw new BadParcelableException("Parcelable has invalid type");
        }
    }

    /** Get the hub unique identifier */
    public long getId() {
        return mId;
    }

    /** Get the hub type. The type can be {@link TYPE_CONTEXT_HUB} or {@link TYPE_VENDOR_HUB} */
    public int getType() {
        return mType;
    }

    /** Get the {@link ContextHubInfo} object, null if type is not {@link TYPE_CONTEXT_HUB} */
    @Nullable
    public ContextHubInfo getContextHubInfo() {
        return mContextHubInfo;
    }

    /** Parcel implementation details */
    public int describeContents() {
        if (mType == TYPE_CONTEXT_HUB && mContextHubInfo != null) {
            return mContextHubInfo.describeContents();
        }
        if (mType == TYPE_VENDOR_HUB && mVendorHubInfo != null) {
            return mVendorHubInfo.describeContents();
        }
        return 0;
    }

    /** Parcel implementation details */
    public void writeToParcel(@NonNull Parcel out, int flags) {
        out.writeLong(mId);
        out.writeInt(mType);

        if (mType == TYPE_CONTEXT_HUB && mContextHubInfo != null) {
            mContextHubInfo.writeToParcel(out, flags);
        }

        if (mType == TYPE_VENDOR_HUB && mVendorHubInfo != null) {
            mVendorHubInfo.writeToParcel(out, flags);
        }
    }

    @NonNull
    @Override
    public String toString() {
        StringBuilder out = new StringBuilder();
        out.append("HubInfo ID: 0x");
        out.append(Long.toHexString(mId));
        out.append("\n");
        if (mType == TYPE_CONTEXT_HUB && mContextHubInfo != null) {
            out.append(" ContextHubDetails: ");
            out.append(mContextHubInfo);
        }
        if (mType == TYPE_VENDOR_HUB && mVendorHubInfo != null) {
            out.append(" VendorHubDetails: ");
            out.append(mVendorHubInfo);
        }
        return out.toString();
    }

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

                public HubInfo[] newArray(int size) {
                    return new HubInfo[size];
                }
            };
}
Loading