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

Commit 4493e149 authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Implements queryNanoApps API

Bug: 67734082
Test: Run a test app to use the new query API, verify returns the
expected set of nanoapps
Change-Id: I2a808b9f80dd0d9168466f80b79ca1fa35da242c
parent e1ade436
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -421,7 +421,17 @@ public final class ContextHubManager {
     */
    @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
    public ContextHubTransaction<List<NanoAppState>> queryNanoApps(ContextHubInfo hubInfo) {
        throw new UnsupportedOperationException("TODO: Implement this");
        ContextHubTransaction<List<NanoAppState>> transaction =
                new ContextHubTransaction<>(ContextHubTransaction.TYPE_QUERY_NANOAPPS);
        IContextHubTransactionCallback callback = createQueryCallback(transaction);

        try {
            mService.queryNanoApps(hubInfo.getId(), callback);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }

        return transaction;
    }

    /**
+25 −1
Original line number Diff line number Diff line
@@ -193,6 +193,30 @@ public class ContextHubTransaction<T> {
        mTransactionType = type;
    }

    /**
     * Converts a transaction type to a human-readable string
     *
     * @param type the type of a transaction
     * @param upperCase {@code true} if upper case the first letter, {@code false} otherwise
     * @return a string describing the transaction
     */
    public static String typeToString(@Type int type, boolean upperCase) {
        switch (type) {
            case ContextHubTransaction.TYPE_LOAD_NANOAPP:
                return upperCase ? "Load" : "load";
            case ContextHubTransaction.TYPE_UNLOAD_NANOAPP:
                return upperCase ? "Unload" : "unload";
            case ContextHubTransaction.TYPE_ENABLE_NANOAPP:
                return upperCase ? "Enable" : "enable";
            case ContextHubTransaction.TYPE_DISABLE_NANOAPP:
                return upperCase ? "Disable" : "disable";
            case ContextHubTransaction.TYPE_QUERY_NANOAPPS:
                return upperCase ? "Query" : "query";
            default:
                return upperCase ? "Unknown" : "unknown";
        }
    }

    /**
     * @return the type of the transaction
     */
@@ -244,7 +268,7 @@ public class ContextHubTransaction<T> {
     * A transaction can be invalidated if the process owning the transaction is no longer active
     * and the reference to this object is lost.
     *
     * This method or {@link #setCallbackOnCompletecan(ContextHubTransaction.Callback)} can only be
     * This method or {@link #setCallbackOnComplete(ContextHubTransaction.Callback)} can only be
     * invoked once, or an IllegalStateException will be thrown.
     *
     * @param callback the callback to be invoked upon completion
+3 −0
Original line number Diff line number Diff line
@@ -69,4 +69,7 @@ interface IContextHubService {
    void unloadNanoAppFromHub(
            int contextHubId, in IContextHubTransactionCallback transactionCallback,
            long nanoAppId);

    // Queries for a list of nanoapps
    void queryNanoApps(int contextHubId, in IContextHubTransactionCallback transactionCallback);
}
+61 −19
Original line number Diff line number Diff line
@@ -752,15 +752,8 @@ public class ContextHubService extends IContextHubService.Stub {
            int contextHubId, IContextHubTransactionCallback transactionCallback,
            NanoAppBinary nanoAppBinary) throws RemoteException {
        checkPermissions();
        if (mContextHubProxy == null) {
            transactionCallback.onTransactionComplete(
                    ContextHubTransaction.TRANSACTION_FAILED_HAL_UNAVAILABLE);
            return;
        }
        if (!isValidContextHubId(contextHubId)) {
            Log.e(TAG, "Cannot load nanoapp for invalid hub ID " + contextHubId);
            transactionCallback.onTransactionComplete(
                    ContextHubTransaction.TRANSACTION_FAILED_BAD_PARAMS);
        if (!checkHalProxyAndContextHubId(
                contextHubId, transactionCallback, ContextHubTransaction.TYPE_LOAD_NANOAPP)) {
            return;
        }
        if (nanoAppBinary == null) {
@@ -772,7 +765,6 @@ public class ContextHubService extends IContextHubService.Stub {

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

        addTransaction(transaction);
    }

@@ -790,21 +782,35 @@ public class ContextHubService extends IContextHubService.Stub {
            int contextHubId, IContextHubTransactionCallback transactionCallback, long nanoAppId)
            throws RemoteException {
        checkPermissions();
        if (mContextHubProxy == null) {
            transactionCallback.onTransactionComplete(
                    ContextHubTransaction.TRANSACTION_FAILED_HAL_UNAVAILABLE);
            return;
        }
        if (!isValidContextHubId(contextHubId)) {
            Log.e(TAG, "Cannot unload nanoapp for invalid hub ID " + contextHubId);
            transactionCallback.onTransactionComplete(
                    ContextHubTransaction.TRANSACTION_FAILED_BAD_PARAMS);
        if (!checkHalProxyAndContextHubId(
                contextHubId, transactionCallback, ContextHubTransaction.TYPE_UNLOAD_NANOAPP)) {
            return;
        }

        ContextHubServiceTransaction transaction = mTransactionManager.createUnloadTransaction(
                contextHubId, nanoAppId, transactionCallback);
        addTransaction(transaction);
    }

    /**
     * Queries for a list of nanoapps from the specified Context hub.
     *
     * @param contextHubId the ID of the hub to query
     * @param transactionCallback the client-facing transaction callback interface
     *
     * @throws RemoteException
     */
    @Override
    public void queryNanoApps(int contextHubId, IContextHubTransactionCallback transactionCallback)
            throws RemoteException {
        checkPermissions();
        if (!checkHalProxyAndContextHubId(
                contextHubId, transactionCallback, ContextHubTransaction.TYPE_QUERY_NANOAPPS)) {
            return;
        }

        ContextHubServiceTransaction transaction =
                mTransactionManager.createQueryTransaction(contextHubId, transactionCallback);
        addTransaction(transaction);
    }

@@ -863,6 +869,42 @@ public class ContextHubService extends IContextHubService.Stub {
        return 0;
    }

    /**
     * Validates the HAL proxy state and context hub ID to see if we can start the transaction.
     *
     * @param contextHubId    the ID of the hub to start the transaction
     * @param callback        the client transaction callback interface
     * @param transactionType the type of the transaction
     *
     * @return {@code true} if mContextHubProxy and contextHubId is valid, {@code false} otherwise
     */
    private boolean checkHalProxyAndContextHubId(
            int contextHubId, IContextHubTransactionCallback callback,
            @ContextHubTransaction.Type int transactionType) {
        if (mContextHubProxy == null) {
            try {
                callback.onTransactionComplete(
                        ContextHubTransaction.TRANSACTION_FAILED_HAL_UNAVAILABLE);
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException while calling onTransactionComplete", e);
            }
            return false;
        }
        if (!isValidContextHubId(contextHubId)) {
            Log.e(TAG, "Cannot start "
                    + ContextHubTransaction.typeToString(transactionType, false /* upperCase */)
                    + " transaction for invalid hub ID " + contextHubId);
            try {
                callback.onTransactionComplete(ContextHubTransaction.TRANSACTION_FAILED_BAD_PARAMS);
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException while calling onTransactionComplete", e);
            }
            return false;
        }

        return true;
    }

    private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) {
        // App Id encodes vendor & version
        NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo();
+2 −21
Original line number Diff line number Diff line
@@ -126,28 +126,9 @@ import java.util.concurrent.TimeUnit;
        return mIsComplete;
    }

    /**
     * @return the human-readable string of this transaction's type
     */
    private String getTransactionTypeString() {
        switch (mTransactionType) {
            case ContextHubTransaction.TYPE_LOAD_NANOAPP:
                return "Load";
            case ContextHubTransaction.TYPE_UNLOAD_NANOAPP:
                return "Unload";
            case ContextHubTransaction.TYPE_ENABLE_NANOAPP:
                return "Enable";
            case ContextHubTransaction.TYPE_DISABLE_NANOAPP:
                return "Disable";
            case ContextHubTransaction.TYPE_QUERY_NANOAPPS:
                return "Query";
            default:
                return "Unknown";
        }
    }

    @Override
    public String toString() {
        return getTransactionTypeString() + " transaction (ID = " + mTransactionId + ")";
        return ContextHubTransaction.typeToString(mTransactionType, true /* upperCase */)
                + " transaction (ID = " + mTransactionId + ")";
    }
}