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

Commit ca5f8382 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I7cd58841,I800bde6f

* changes:
  Implements getContextHubs
  Merges ContextHubService notion of hub ID/handle
parents 1fa5f21a fdbbd468
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -262,7 +262,7 @@ public class ContextHubInfo implements Parcelable {
    @Override
    public String toString() {
        String retVal = "";
        retVal += "Id : " + mId;
        retVal += "ID/handle : " + mId;
        retVal += ", Name : " + mName;
        retVal += "\n\tVendor : " + mVendor;
        retVal += ", Toolchain : " + mToolchain;
+7 −3
Original line number Diff line number Diff line
@@ -258,9 +258,9 @@ public final class ContextHubManager {
    }

    /**
     * Returns the list of context hubs in the system.
     * Returns the list of ContextHubInfo objects describing the available Context Hubs.
     *
     * @return the list of context hub informations
     * @return the list of ContextHubInfo objects
     *
     * @see ContextHubInfo
     *
@@ -268,7 +268,11 @@ public final class ContextHubManager {
     */
    @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
    public List<ContextHubInfo> getContextHubs() {
        throw new UnsupportedOperationException("TODO: Implement this");
        try {
            return mService.getContextHubs();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
+8 −5
Original line number Diff line number Diff line
@@ -43,23 +43,26 @@ interface IContextHubService {
    ContextHubInfo getContextHubInfo(int contextHubHandle);

    // Loads a nanoapp at the specified hub (old API)
    int loadNanoApp(int hubHandle, in NanoApp app);
    int loadNanoApp(int contextHubHandle, in NanoApp nanoApp);

    // Unloads a nanoapp given its instance ID (old API)
    int unloadNanoApp(int nanoAppInstanceHandle);
    int unloadNanoApp(int nanoAppHandle);

    // Gets the NanoAppInstanceInfo of a nanoapp give its instance ID
    NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle);
    NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppHandle);

    // Finds all nanoApp instances matching some filter
    int[] findNanoAppOnHub(int hubHandle, in NanoAppFilter filter);
    int[] findNanoAppOnHub(int contextHubHandle, in NanoAppFilter filter);

    // Sends a message to a nanoApp
    int sendMessage(int hubHandle, int nanoAppHandle, in ContextHubMessage msg);
    int sendMessage(int contextHubHandle, int nanoAppHandle, in ContextHubMessage msg);

    // Creates a client to send and receive messages
    IContextHubClient createClient(in IContextHubClientCallback client, int contextHubId);

    // Returns a list of ContextHub objects of available hubs
    List<ContextHubInfo> getContextHubs();

    // Loads a nanoapp at the specified hub (new API)
    void loadNanoAppOnHub(
            int contextHubId, in IContextHubTransactionCallback transactionCallback,
+57 −63
Original line number Diff line number Diff line
@@ -79,7 +79,8 @@ public class ContextHubService extends IContextHubService.Stub {

    private final Context mContext;

    private final ContextHubInfo[] mContextHubInfo;
    private final Map<Integer, ContextHubInfo> mContextHubIdToInfoMap;
    private final List<ContextHubInfo> mContextHubInfoList;
    private final RemoteCallbackList<IContextHubCallback> mCallbacksList =
            new RemoteCallbackList<>();

@@ -141,8 +142,9 @@ public class ContextHubService extends IContextHubService.Stub {
        if (mContextHubProxy == null) {
            mTransactionManager = null;
            mClientManager = null;
            mDefaultClientMap = Collections.EMPTY_MAP;
            mContextHubInfo = new ContextHubInfo[0];
            mDefaultClientMap = Collections.emptyMap();
            mContextHubIdToInfoMap = Collections.emptyMap();
            mContextHubInfoList = Collections.emptyList();
            return;
        }

@@ -157,20 +159,16 @@ public class ContextHubService extends IContextHubService.Stub {
            Log.e(TAG, "RemoteException while getting Context Hub info", e);
            hubList = Collections.emptyList();
        }
        mContextHubInfo = ContextHubServiceUtil.createContextHubInfoArray(hubList);
        mContextHubIdToInfoMap = Collections.unmodifiableMap(
                ContextHubServiceUtil.createContextHubInfoMap(hubList));
        mContextHubInfoList = new ArrayList<>(mContextHubIdToInfoMap.values());

        HashMap<Integer, IContextHubClient> defaultClientMap = new HashMap<>();
        for (ContextHubInfo contextHubInfo : mContextHubInfo) {
            int contextHubId = contextHubInfo.getId();

        for (int contextHubId : mContextHubIdToInfoMap.keySet()) {
            IContextHubClient client = mClientManager.registerClient(
                    createDefaultClientCallback(contextHubId), contextHubId);
            defaultClientMap.put(contextHubId, client);
        }
        mDefaultClientMap = Collections.unmodifiableMap(defaultClientMap);

        for (ContextHubInfo contextHubInfo : mContextHubInfo) {
            int contextHubId = contextHubInfo.getId();
            try {
                mContextHubProxy.registerCallback(
                        contextHubId, new ContextHubServiceCallback(contextHubId));
@@ -178,18 +176,12 @@ public class ContextHubService extends IContextHubService.Stub {
                Log.e(TAG, "RemoteException while registering service callback for hub (ID = "
                        + contextHubId + ")", e);
            }
        }

            // Do a query to initialize the service cache list of nanoapps
            // TODO(b/69270990): Remove this when old API is deprecated
        for (ContextHubInfo contextHubInfo : mContextHubInfo) {
            queryNanoAppsInternal(contextHubInfo.getId());
        }

        for (int i = 0; i < mContextHubInfo.length; i++) {
            Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId()
                    + ", name:  " + mContextHubInfo[i].getName());
            queryNanoAppsInternal(contextHubId);
        }
        mDefaultClientMap = Collections.unmodifiableMap(defaultClientMap);
    }

    /**
@@ -267,23 +259,29 @@ public class ContextHubService extends IContextHubService.Stub {
    @Override
    public int[] getContextHubHandles() throws RemoteException {
        checkPermissions();
        int[] returnArray = new int[mContextHubInfo.length];

        for (int i = 0; i < returnArray.length; ++i) {
            returnArray[i] = i;
        }
        return returnArray;
        return ContextHubServiceUtil.createPrimitiveIntArray(mContextHubIdToInfoMap.keySet());
    }

    @Override
    public ContextHubInfo getContextHubInfo(int contextHubId) throws RemoteException {
    public ContextHubInfo getContextHubInfo(int contextHubHandle) throws RemoteException {
        checkPermissions();
        if (!(contextHubId >= 0 && contextHubId < mContextHubInfo.length)) {
            Log.e(TAG, "Invalid context hub handle " + contextHubId);
            return null; // null means fail
        if (!mContextHubIdToInfoMap.containsKey(contextHubHandle)) {
            Log.e(TAG, "Invalid Context Hub handle " + contextHubHandle + " in getContextHubInfo");
            return null;
        }

        return mContextHubIdToInfoMap.get(contextHubHandle);
    }

        return mContextHubInfo[contextHubId];
    /**
     * Returns a List of ContextHubInfo object describing the available hubs.
     *
     * @return the List of ContextHubInfo objects
     */
    @Override
    public List<ContextHubInfo> getContextHubs() throws RemoteException {
        checkPermissions();
        return mContextHubInfoList;
    }

    /**
@@ -347,28 +345,27 @@ public class ContextHubService extends IContextHubService.Stub {
    }

    @Override
    public int loadNanoApp(int contextHubId, NanoApp app) throws RemoteException {
    public int loadNanoApp(int contextHubHandle, NanoApp nanoApp) throws RemoteException {
        checkPermissions();
        if (mContextHubProxy == null) {
            return -1;
        }

        if (!(contextHubId >= 0 && contextHubId < mContextHubInfo.length)) {
            Log.e(TAG, "Invalid contextHubhandle " + contextHubId);
        if (!isValidContextHubId(contextHubHandle)) {
            Log.e(TAG, "Invalid Context Hub handle " + contextHubHandle + " in loadNanoApp");
            return -1;
        }
        if (app == null) {
            Log.e(TAG, "Invalid null app");
        if (nanoApp == null) {
            Log.e(TAG, "NanoApp cannot be null in loadNanoApp");
            return -1;
        }

        // Create an internal IContextHubTransactionCallback for the old API clients
        NanoAppBinary nanoAppBinary = new NanoAppBinary(app.getAppBinary());
        NanoAppBinary nanoAppBinary = new NanoAppBinary(nanoApp.getAppBinary());
        IContextHubTransactionCallback onCompleteCallback =
                createLoadTransactionCallback(contextHubId, nanoAppBinary);
                createLoadTransactionCallback(contextHubHandle, nanoAppBinary);

        ContextHubServiceTransaction transaction = mTransactionManager.createLoadTransaction(
                contextHubId, nanoAppBinary, onCompleteCallback);
                contextHubHandle, nanoAppBinary, onCompleteCallback);

        mTransactionManager.addTransaction(transaction);
        return 0;
@@ -384,7 +381,7 @@ public class ContextHubService extends IContextHubService.Stub {
        NanoAppInstanceInfo info =
                mNanoAppStateManager.getNanoAppInstanceInfo(nanoAppHandle);
        if (info == null) {
            Log.e(TAG, "Cannot find nanoapp with handle " + nanoAppHandle);
            Log.e(TAG, "Invalid nanoapp handle " + nanoAppHandle + " in unloadNanoApp");
            return -1;
        }

@@ -407,7 +404,8 @@ public class ContextHubService extends IContextHubService.Stub {
    }

    @Override
    public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException {
    public int[] findNanoAppOnHub(
            int contextHubHandle, NanoAppFilter filter) throws RemoteException {
        checkPermissions();

        ArrayList<Integer> foundInstances = new ArrayList<>();
@@ -450,29 +448,29 @@ public class ContextHubService extends IContextHubService.Stub {
    }

    @Override
    public int sendMessage(
            int hubHandle, int nanoAppHandle, ContextHubMessage msg) throws RemoteException {
    public int sendMessage(int contextHubHandle, int nanoAppHandle, ContextHubMessage msg)
            throws RemoteException {
        checkPermissions();
        if (mContextHubProxy == null) {
            return -1;
        }
        if (msg == null) {
            Log.e(TAG, "ContextHubMessage cannot be null");
            Log.e(TAG, "ContextHubMessage cannot be null in sendMessage");
            return -1;
        }
        if (msg.getData() == null) {
            Log.e(TAG, "ContextHubMessage message body cannot be null");
            Log.e(TAG, "ContextHubMessage message body cannot be null in sendMessage");
            return -1;
        }
        if (!mDefaultClientMap.containsKey(hubHandle)) {
            Log.e(TAG, "Hub with ID " + hubHandle + " does not exist");
        if (!isValidContextHubId(contextHubHandle)) {
            Log.e(TAG, "Invalid Context Hub handle " + contextHubHandle + " in sendMessage");
            return -1;
        }

        boolean success = false;
        if (nanoAppHandle == OS_APP_INSTANCE) {
            if (msg.getMsgType() == MSG_QUERY_NANO_APPS) {
                success = (queryNanoAppsInternal(hubHandle) == Result.OK);
                success = (queryNanoAppsInternal(contextHubHandle) == Result.OK);
            } else {
                Log.e(TAG, "Invalid OS message params of type " + msg.getMsgType());
            }
@@ -482,7 +480,7 @@ public class ContextHubService extends IContextHubService.Stub {
                NanoAppMessage message = NanoAppMessage.createMessageToNanoApp(
                        info.getAppId(), msg.getMsgType(), msg.getData());

                IContextHubClient client = mDefaultClientMap.get(hubHandle);
                IContextHubClient client = mDefaultClientMap.get(contextHubHandle);
                success = (client.sendMessageToNanoApp(message) ==
                        ContextHubTransaction.TRANSACTION_SUCCESS);
            } else {
@@ -595,13 +593,7 @@ public class ContextHubService extends IContextHubService.Stub {
     * @return {@code true} if the ID represents that of an available hub, {@code false} otherwise
     */
    private boolean isValidContextHubId(int contextHubId) {
        for (ContextHubInfo hubInfo : mContextHubInfo) {
            if (hubInfo.getId() == contextHubId) {
                return true;
            }
        }

        return false;
        return mContextHubIdToInfoMap.containsKey(contextHubId);
    }

    /**
@@ -762,8 +754,8 @@ public class ContextHubService extends IContextHubService.Stub {
        pw.println("");
        // dump ContextHubInfo
        pw.println("=================== CONTEXT HUBS ====================");
        for (int i = 0; i < mContextHubInfo.length; i++) {
            pw.println("Handle " + i + " : " + mContextHubInfo[i].toString());
        for (ContextHubInfo hubInfo : mContextHubIdToInfoMap.values()) {
            pw.println(hubInfo);
        }
        pw.println("");
        pw.println("=================== NANOAPPS ====================");
@@ -779,7 +771,8 @@ public class ContextHubService extends IContextHubService.Stub {
        ContextHubServiceUtil.checkPermissions(mContext);
    }

    private int onMessageReceiptOldApi(int msgType, int hubHandle, int appInstance, byte[] data) {
    private int onMessageReceiptOldApi(
            int msgType, int contextHubHandle, int appInstance, byte[] data) {
        if (data == null) {
            return -1;
        }
@@ -787,7 +780,8 @@ public class ContextHubService extends IContextHubService.Stub {
        int msgVersion = 0;
        int callbacksCount = mCallbacksList.beginBroadcast();
        Log.d(TAG, "Sending message " + msgType + " version " + msgVersion + " from hubHandle " +
                hubHandle + ", appInstance " + appInstance + ", callBackCount " + callbacksCount);
                contextHubHandle + ", appInstance " + appInstance + ", callBackCount "
                + callbacksCount);

        if (callbacksCount < 1) {
            Log.v(TAG, "No message callbacks registered.");
@@ -798,7 +792,7 @@ public class ContextHubService extends IContextHubService.Stub {
        for (int i = 0; i < callbacksCount; ++i) {
            IContextHubCallback callback = mCallbacksList.getBroadcastItem(i);
            try {
                callback.onMessageReceipt(hubHandle, appInstance, msg);
                callback.onMessageReceipt(contextHubHandle, appInstance, msg);
            } catch (RemoteException e) {
                Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ").");
                continue;
+27 −7
Original line number Diff line number Diff line
@@ -30,6 +30,9 @@ import android.hardware.location.NanoAppMessage;
import android.hardware.location.NanoAppState;
import android.util.Log;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

@@ -43,19 +46,20 @@ import java.util.ArrayList;
            + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware";

    /**
     * Creates a ContextHubInfo array from an ArrayList of HIDL ContextHub objects.
     * Creates a ConcurrentHashMap of the Context Hub ID to the ContextHubInfo object given an
     * ArrayList of HIDL ContextHub objects.
     *
     * @param hubList the ContextHub ArrayList
     * @return the ContextHubInfo array
     * @return the HashMap object
     */
    /* package */
    static ContextHubInfo[] createContextHubInfoArray(List<ContextHub> hubList) {
        ContextHubInfo[] contextHubInfoList = new ContextHubInfo[hubList.size()];
        for (int i = 0; i < hubList.size(); i++) {
            contextHubInfoList[i] = new ContextHubInfo(hubList.get(i));
    static HashMap<Integer, ContextHubInfo> createContextHubInfoMap(List<ContextHub> hubList) {
        HashMap<Integer, ContextHubInfo> contextHubIdToInfoMap = new HashMap<>();
        for (ContextHub contextHub : hubList) {
            contextHubIdToInfoMap.put(contextHub.hubId, new ContextHubInfo(contextHub));
        }

        return contextHubInfoList;
        return contextHubIdToInfoMap;
    }

    /**
@@ -89,6 +93,22 @@ import java.util.ArrayList;
        return primitiveArray;
    }

    /**
     * Creates a primitive integer array given a Collection<Integer>.
     * @param collection the collection to iterate
     * @return the primitive integer array
     */
    static int[] createPrimitiveIntArray(Collection<Integer> collection) {
        int[] primitiveArray = new int[collection.size()];

        int i = 0;
        for (int contextHubId : collection) {
            primitiveArray[i++] = contextHubId;
        }

        return primitiveArray;
    }

    /**
     * Generates the Context Hub HAL's NanoAppBinary object from the client-facing
     * android.hardware.location.NanoAppBinary object.