Implements enableNanoApp

Bug: 67734082
Test: Run a test app to issue an enable request, verify HAL code is
executed via logs and client receives an error response.
Change-Id: Ie4ec660a094082887eaefbdfc2e1fd8d1ee7c0e3
diff --git a/services/core/java/com/android/server/location/ContextHubService.java b/services/core/java/com/android/server/location/ContextHubService.java
index 90c912a..694dd26 100644
--- a/services/core/java/com/android/server/location/ContextHubService.java
+++ b/services/core/java/com/android/server/location/ContextHubService.java
@@ -642,11 +642,10 @@
     /**
      * Loads a nanoapp binary at the specified Context hub.
      *
-     * @param contextHubId the ID of the hub to load the binary
+     * @param contextHubId        the ID of the hub to load the binary
      * @param transactionCallback the client-facing transaction callback interface
-     * @param nanoAppBinary the binary to load
+     * @param nanoAppBinary       the binary to load
      *
-     * @throws RemoteException
      * @throws IllegalStateException if the transaction queue is full
      */
     @Override
@@ -673,11 +672,10 @@
     /**
      * Unloads a nanoapp from the specified Context Hub.
      *
-     * @param contextHubId the ID of the hub to unload the nanoapp
+     * @param contextHubId        the ID of the hub to unload the nanoapp
      * @param transactionCallback the client-facing transaction callback interface
-     * @param nanoAppId the ID of the nanoapp to unload
+     * @param nanoAppId           the ID of the nanoapp to unload
      *
-     * @throws RemoteException
      * @throws IllegalStateException if the transaction queue is full
      */
     @Override
@@ -696,12 +694,35 @@
     }
 
     /**
+     * Enables a nanoapp at the specified Context Hub.
+     *
+     * @param contextHubId        the ID of the hub to enable the nanoapp
+     * @param transactionCallback the client-facing transaction callback interface
+     * @param nanoAppId           the ID of the nanoapp to enable
+     *
+     * @throws IllegalStateException if the transaction queue is full
+     */
+    @Override
+    public void enableNanoApp(
+            int contextHubId, IContextHubTransactionCallback transactionCallback, long nanoAppId)
+            throws RemoteException {
+        checkPermissions();
+        if (!checkHalProxyAndContextHubId(
+                contextHubId, transactionCallback, ContextHubTransaction.TYPE_ENABLE_NANOAPP)) {
+            return;
+        }
+
+        ContextHubServiceTransaction transaction = mTransactionManager.createEnableTransaction(
+                contextHubId, nanoAppId, transactionCallback);
+        mTransactionManager.addTransaction(transaction);
+    }
+
+    /**
      * Queries for a list of nanoapps from the specified Context hub.
      *
-     * @param contextHubId the ID of the hub to query
+     * @param contextHubId        the ID of the hub to query
      * @param transactionCallback the client-facing transaction callback interface
      *
-     * @throws RemoteException
      * @throws IllegalStateException if the transaction queue is full
      */
     @Override
@@ -713,8 +734,8 @@
             return;
         }
 
-        ContextHubServiceTransaction transaction =
-                mTransactionManager.createQueryTransaction(contextHubId, transactionCallback);
+        ContextHubServiceTransaction transaction = mTransactionManager.createQueryTransaction(
+                contextHubId, transactionCallback);
         mTransactionManager.addTransaction(transaction);
     }
 
diff --git a/services/core/java/com/android/server/location/ContextHubTransactionManager.java b/services/core/java/com/android/server/location/ContextHubTransactionManager.java
index 00252bc..30468bf 100644
--- a/services/core/java/com/android/server/location/ContextHubTransactionManager.java
+++ b/services/core/java/com/android/server/location/ContextHubTransactionManager.java
@@ -143,7 +143,7 @@
     /**
      * Creates a transaction for unloading a nanoapp.
      *
-     * @param contextHubId       the ID of the hub to load the nanoapp to
+     * @param contextHubId       the ID of the hub to unload the nanoapp from
      * @param nanoAppId          the ID of the nanoapp to unload
      * @param onCompleteCallback the client on complete callback
      * @return the generated transaction
@@ -182,6 +182,41 @@
     }
 
     /**
+     * Creates a transaction for enabling a nanoapp.
+     *
+     * @param contextHubId       the ID of the hub to enable the nanoapp on
+     * @param nanoAppId          the ID of the nanoapp to enable
+     * @param onCompleteCallback the client on complete callback
+     * @return the generated transaction
+     */
+    /* package */ ContextHubServiceTransaction createEnableTransaction(
+            int contextHubId, long nanoAppId, IContextHubTransactionCallback onCompleteCallback) {
+        return new ContextHubServiceTransaction(
+                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_ENABLE_NANOAPP) {
+            @Override
+            /* package */ int onTransact() {
+                try {
+                    return mContextHubProxy.enableNanoApp(
+                            contextHubId, nanoAppId, this.getTransactionId());
+                } catch (RemoteException e) {
+                    Log.e(TAG, "RemoteException while trying to enable nanoapp with ID 0x" +
+                            Long.toHexString(nanoAppId), e);
+                    return Result.UNKNOWN_FAILURE;
+                }
+            }
+
+            @Override
+            /* package */ void onTransactionComplete(@ContextHubTransaction.Result int result) {
+                try {
+                    onCompleteCallback.onTransactionComplete(result);
+                } catch (RemoteException e) {
+                    Log.e(TAG, "RemoteException while calling client onTransactionComplete", e);
+                }
+            }
+        };
+    }
+
+    /**
      * Creates a transaction for querying for a list of nanoapps.
      *
      * @param contextHubId       the ID of the hub to query