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

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

Refactors ContextHubClientManager/Broker classes

Makes changes so that ContextHubClientBroker objects of different types
(callback or PendingIntent) can be generated.

Bug: 117612105
Test: Compile only
Change-Id: I2c2e15be8b62c2aedec67505f8f1ffedcbfb5d32
parent a96fbfe2
Loading
Loading
Loading
Loading
+14 −32
Original line number Diff line number Diff line
@@ -134,12 +134,14 @@ public class ContextHubClientBroker extends IContextHubClient.Stub

    /* package */ ContextHubClientBroker(
            Context context, IContexthub contextHubProxy, ContextHubClientManager clientManager,
            ContextHubInfo contextHubInfo, short hostEndPointId) {
            ContextHubInfo contextHubInfo, short hostEndPointId,
            IContextHubClientCallback callback) {
        mContext = context;
        mContextHubProxy = contextHubProxy;
        mClientManager = clientManager;
        mAttachedContextHubInfo = contextHubInfo;
        mHostEndPointId = hostEndPointId;
        mCallbackInterface = callback;
    }

    /**
@@ -199,37 +201,6 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
        close();
    }

    /**
     * Sets the callback interface for this client, only if the callback is currently unregistered.
     *
     * Also attaches a death recipient to a ContextHubClientBroker object. If unsuccessful, the
     * connection is closed.
     *
     * @param callback the callback interface
     * @return true if the callback was successfully set, false otherwise
     *
     * @throws IllegalStateException if the client has already been registered to a callback
     */
    /* package */
    synchronized boolean setCallback(IContextHubClientCallback callback) {
        boolean success = false;
        if (mCallbackInterface != null) {
            throw new IllegalStateException("Client is already registered with a callback");
        } else {
            mCallbackInterface = callback;
            try {
                mCallbackInterface.asBinder().linkToDeath(this, 0 /* flags */);
                success = true;
            } catch (RemoteException e) {
                // The client process has died, so we close the connection.
                Log.e(TAG, "Failed to attach death recipient to client");
                close();
            }
        }

        return success;
    }

    /**
     * @return the ID of the context hub this client is attached to
     */
@@ -313,6 +284,17 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
        return (pendingIntent != null) && pendingIntent.equals(intent);
    }

    /**
     * Attaches the death recipient to the callback interface object, if any.
     *
     * @throws RemoteException if the client process already died
     */
    /* package */ void attachDeathRecipient() throws RemoteException {
        if (mCallbackInterface != null) {
            mCallbackInterface.asBinder().linkToDeath(this, 0 /* flags */);
        }
    }

    /**
     * Helper function to invoke a specified client callback, if the connection is open.
     *
+24 −18
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.hardware.location.ContextHubInfo;
import android.hardware.location.IContextHubClient;
import android.hardware.location.IContextHubClientCallback;
import android.hardware.location.NanoAppMessage;
import android.os.RemoteException;
import android.util.Log;

import java.util.concurrent.ConcurrentHashMap;
@@ -68,7 +69,7 @@ import java.util.function.Consumer;
    /*
     * The next host endpoint ID to start iterating for the next available host endpoint ID.
     */
    private int mNextHostEndpointId = 0;
    private int mNextHostEndPointId = 0;

    /* package */ ContextHubClientManager(
            Context context, IContexthub contextHubProxy) {
@@ -88,9 +89,22 @@ import java.util.function.Consumer;
     */
    /* package */ IContextHubClient registerClient(
            IContextHubClientCallback clientCallback, ContextHubInfo contextHubInfo) {
        ContextHubClientBroker broker = createNewClientBroker(contextHubInfo);
        if (!broker.setCallback(clientCallback)) {
            return null; // Client process has died, so we return null
        ContextHubClientBroker broker;
        synchronized (this) {
            short hostEndPointId = getHostEndPointId();
            broker = new ContextHubClientBroker(
                    mContext, mContextHubProxy, this /* clientManager */, contextHubInfo,
                    hostEndPointId, clientCallback);
            mHostEndPointIdToClientMap.put(hostEndPointId, broker);
        }

        try {
            broker.attachDeathRecipient();
        } catch (RemoteException e) {
            // The client process has died, so we close the connection and return null
            Log.e(TAG, "Failed to attach death recipient to client");
            broker.close();
            return null;
        }

        Log.d(TAG, "Registered client with host endpoint ID " + broker.getHostEndPointId());
@@ -187,36 +201,28 @@ import java.util.function.Consumer;
    }

    /**
     * Creates a new ContextHubClientBroker object for a client and registers it with the client
     * manager.
     * Returns an available host endpoint ID.
     *
     * @param contextHubInfo the object describing the hub this client is attached to
     *
     * @return the ContextHubClientBroker object
     * @returns an available host endpoint ID
     *
     * @throws IllegalStateException if max number of clients have already registered
     */
    private synchronized ContextHubClientBroker createNewClientBroker(
            ContextHubInfo contextHubInfo) {
    private short getHostEndPointId() {
        if (mHostEndPointIdToClientMap.size() == MAX_CLIENT_ID + 1) {
            throw new IllegalStateException("Could not register client - max limit exceeded");
        }

        ContextHubClientBroker broker = null;
        int id = mNextHostEndpointId;
        int id = mNextHostEndPointId;
        for (int i = 0; i <= MAX_CLIENT_ID; i++) {
            if (!mHostEndPointIdToClientMap.containsKey((short) id)) {
                broker = new ContextHubClientBroker(
                        mContext, mContextHubProxy, this, contextHubInfo, (short) id);
                mHostEndPointIdToClientMap.put((short) id, broker);
                mNextHostEndpointId = (id == MAX_CLIENT_ID) ? 0 : id + 1;
                mNextHostEndPointId = (id == MAX_CLIENT_ID) ? 0 : id + 1;
                break;
            }

            id = (id == MAX_CLIENT_ID) ? 0 : id + 1;
        }

        return broker;
        return (short) id;
    }

    /**