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

Commit e9979035 authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Implements ContextHubEndpointBroker unregisterEndpoint

Bug: 378487799
Flag: android.chre.flags.offload_implementation
Test: make services
Change-Id: I200c64e267d56d36dc86ac0e45cc1bc5323bb48b
parent 9526ea6c
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ import android.hardware.location.IContextHubTransactionCallback;
import android.os.RemoteException;
import android.util.Log;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * A class that represents a broker for the endpoint registered by the client app. This class
 * manages direct IContextHubEndpoint/IContextHubEndpointCallback API/callback calls.
@@ -54,6 +56,9 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub {
    /** The remote callback interface for this endpoint. */
    private final IContextHubEndpointCallback mContextHubEndpointCallback;

    /** True if this endpoint is registered with the service. */
    private AtomicBoolean mIsRegistered = new AtomicBoolean(true);

    /* package */ ContextHubEndpointBroker(
            Context context,
            IContextHubWrapper contextHubProxy,
@@ -77,6 +82,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub {
    public int openSession(HubEndpointInfo destination, HubServiceInfo serviceInfo)
            throws RemoteException {
        ContextHubServiceUtil.checkPermissions(mContext);
        if (!mIsRegistered.get()) throw new IllegalStateException("Endpoint is not registered");
        int sessionId = mEndpointManager.reserveSessionId();
        EndpointInfo halEndpointInfo = ContextHubServiceUtil.convertHalEndpointInfo(destination);
        try {
@@ -97,6 +103,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub {
    @Override
    public void closeSession(int sessionId, int reason) throws RemoteException {
        ContextHubServiceUtil.checkPermissions(mContext);
        if (!mIsRegistered.get()) throw new IllegalStateException("Endpoint is not registered");
        try {
            mContextHubProxy.closeEndpointSession(sessionId, (byte) reason);
        } catch (RemoteException | IllegalArgumentException | UnsupportedOperationException e) {
@@ -112,8 +119,15 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub {

    @Override
    public void unregister() {
        // TODO(b/378487799): Implement this

        ContextHubServiceUtil.checkPermissions(mContext);
        mIsRegistered.set(false);
        try {
            mContextHubProxy.unregisterEndpoint(mHalEndpointInfo);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException while calling HAL unregisterEndpoint", e);
        }
        // TODO(b/378487799): Release reserved session IDs
        mEndpointManager.unregisterEndpoint(mEndpointInfo.getIdentifier().getEndpoint());
    }

    @Override
+9 −0
Original line number Diff line number Diff line
@@ -194,6 +194,15 @@ import java.util.concurrent.ConcurrentHashMap;
        }
    }

    /**
     * Unregisters an endpoint given its ID.
     *
     * @param endpointId The ID of the endpoint to unregister.
     */
    /* package */ void unregisterEndpoint(long endpointId) {
        mEndpointMap.remove(endpointId);
    }

    /** @return an available endpoint ID */
    private long getNewEndpointId() {
        synchronized (mEndpointLock) {
+14 −0
Original line number Diff line number Diff line
@@ -257,6 +257,10 @@ public abstract class IContextHubWrapper {
    /** Closes a previously opened endpoint */
    public void closeEndpointSession(int sessionId, byte reason) throws RemoteException {}

    /** Unregisters a previously registered endpoint */
    public void unregisterEndpoint(android.hardware.contexthub.EndpointInfo info)
            throws RemoteException {}

    /**
     * @return True if this version of the Contexthub HAL supports Location setting notifications.
     */
@@ -731,6 +735,16 @@ public abstract class IContextHubWrapper {
            hub.closeEndpointSession(sessionId, reason);
        }

        @Override
        public void unregisterEndpoint(android.hardware.contexthub.EndpointInfo info)
                throws RemoteException {
            android.hardware.contexthub.IContextHub hub = getHub();
            if (hub == null) {
                return;
            }
            hub.unregisterEndpoint(info);
        }

        public boolean supportsLocationSettingNotifications() {
            return true;
        }