From c296119f52dc70d6a78c81a1146ae320789b65fb Mon Sep 17 00:00:00 2001 From: Arthur Ishiguro Date: Mon, 30 Aug 2021 15:19:44 -0700 Subject: [PATCH] Remove HIDL references in ContextHubTransactionManager Bug: 194285834 Test: Presubmits, run CHQTS on device Change-Id: I0913fafadd72cdcc4463e7576e4ee3f007892edd --- .../contexthub/ContextHubService.java | 36 +++---- .../ContextHubTransactionManager.java | 57 +++++------ .../contexthub/IContextHubWrapper.java | 96 +++++++++++++++---- 3 files changed, 123 insertions(+), 66 deletions(-) diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubService.java b/services/core/java/com/android/server/location/contexthub/ContextHubService.java index 0313b0cde2b9e..fa84d127697fe 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubService.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubService.java @@ -151,7 +151,8 @@ public class ContextHubService extends IContextHubService.Stub { @Override public void handleTxnResult(int transactionId, int result) { - handleTransactionResultCallback(mContextHubId, transactionId, result); + handleTransactionResultCallback(mContextHubId, transactionId, + result == TransactionResult.SUCCESS); } @Override @@ -213,7 +214,7 @@ public class ContextHubService extends IContextHubService.Stub { mContextHubInfoList = new ArrayList<>(mContextHubIdToInfoMap.values()); mClientManager = new ContextHubClientManager(mContext, mContextHubWrapper); mTransactionManager = new ContextHubTransactionManager( - mContextHubWrapper.getHub(), mClientManager, mNanoAppStateManager); + mContextHubWrapper, mClientManager, mNanoAppStateManager); mSensorPrivacyManagerInternal = LocalServices.getService(SensorPrivacyManagerInternal.class); @@ -260,7 +261,7 @@ public class ContextHubService extends IContextHubService.Stub { public void onReceive(Context context, Intent intent) { if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction()) || WifiManager.ACTION_WIFI_SCAN_AVAILABILITY_CHANGED.equals( - intent.getAction())) { + intent.getAction())) { sendWifiSettingUpdate(false /* forceUpdate */); } } @@ -303,7 +304,7 @@ public class ContextHubService extends IContextHubService.Stub { Log.d(TAG, "User: " + userId + "mic privacy: " + enabled); sendMicrophoneDisableSettingUpdate(enabled); } - }); + }); } } @@ -559,7 +560,7 @@ public class ContextHubService extends IContextHubService.Stub { /** * Performs a query at the specified hub. - * + *

* This method should only be invoked internally by the service, either to update the service * cache or as a result of an explicit query requested by a client through the sendMessage API. * @@ -664,7 +665,7 @@ public class ContextHubService extends IContextHubService.Stub { /** * A helper function to handle an unload response from the Context Hub for the old API. - * + *

* TODO(b/69270990): Remove this once the old APIs are obsolete. */ private void handleUnloadResponseOldApi(int contextHubId, int result) { @@ -678,10 +679,11 @@ public class ContextHubService extends IContextHubService.Stub { * * @param contextHubId the ID of the hub the response came from * @param transactionId the ID of the transaction - * @param result the result of the transaction reported by the hub + * @param success true if the transaction succeeded */ - private void handleTransactionResultCallback(int contextHubId, int transactionId, int result) { - mTransactionManager.onTransactionResponse(transactionId, result); + private void handleTransactionResultCallback(int contextHubId, int transactionId, + boolean success) { + mTransactionManager.onTransactionResponse(transactionId, success); } /** @@ -772,9 +774,9 @@ public class ContextHubService extends IContextHubService.Stub { /** * Creates and registers a PendingIntent client at the service for the specified Context Hub. * - * @param contextHubId the ID of the hub this client is attached to - * @param pendingIntent the PendingIntent associated with this client - * @param nanoAppId the ID of the nanoapp PendingIntent events will be sent for + * @param contextHubId the ID of the hub this client is attached to + * @param pendingIntent the PendingIntent associated with this client + * @param nanoAppId the ID of the nanoapp PendingIntent events will be sent for * @param attributionTag an optional attribution tag within the given package * @return the generated client interface * @throws IllegalArgumentException if hubInfo does not represent a valid hub @@ -1094,8 +1096,8 @@ public class ContextHubService extends IContextHubService.Stub { } /** - * Obtains the latest microphone disabled setting for the current user - * and notifies the Context Hub. + * Obtains the latest microphone disabled setting for the current user and notifies the Context + * Hub. */ private void sendMicrophoneDisableSettingUpdateForCurrentUser() { boolean isEnabled = mSensorPrivacyManagerInternal.isSensorPrivacyEnabled( @@ -1122,9 +1124,9 @@ public class ContextHubService extends IContextHubService.Stub { } /** - * Send a microphone disable settings update whenever the foreground user changes. - * We always send a settings update regardless of the previous state for the same user - * since the CHRE framework is expected to handle repeated identical setting update. + * Send a microphone disable settings update whenever the foreground user changes. We always + * send a settings update regardless of the previous state for the same user since the CHRE + * framework is expected to handle repeated identical setting update. */ public void onUserChanged() { Log.d(TAG, "User changed to id: " + getCurrentUserId()); diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java b/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java index f81208fbf2414..abf5a24942240 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java @@ -16,9 +16,6 @@ package com.android.server.location.contexthub; -import android.hardware.contexthub.V1_0.IContexthub; -import android.hardware.contexthub.V1_0.Result; -import android.hardware.contexthub.V1_0.TransactionResult; import android.hardware.location.ContextHubTransaction; import android.hardware.location.IContextHubTransactionCallback; import android.hardware.location.NanoAppBinary; @@ -40,7 +37,7 @@ import java.util.concurrent.atomic.AtomicInteger; /** * Manages transactions at the Context Hub Service. - * + *

* This class maintains a queue of transaction requests made to the ContextHubService by clients, * and executes them through the Context Hub. At any point in time, either the transaction queue is * empty, or there is a pending transaction that is waiting for an asynchronous response from the @@ -64,7 +61,7 @@ import java.util.concurrent.atomic.AtomicInteger; /* * The proxy to talk to the Context Hub */ - private final IContexthub mContextHubProxy; + private final IContextHubWrapper mContextHubProxy; /* * The manager for all clients for the service. @@ -120,7 +117,7 @@ import java.util.concurrent.atomic.AtomicInteger; } /* package */ ContextHubTransactionManager( - IContexthub contextHubProxy, ContextHubClientManager clientManager, + IContextHubWrapper contextHubProxy, ContextHubClientManager clientManager, NanoAppStateManager nanoAppStateManager) { mContextHubProxy = contextHubProxy; mClientManager = clientManager; @@ -143,15 +140,13 @@ import java.util.concurrent.atomic.AtomicInteger; nanoAppBinary.getNanoAppId(), packageName) { @Override /* package */ int onTransact() { - android.hardware.contexthub.V1_0.NanoAppBinary hidlNanoAppBinary = - ContextHubServiceUtil.createHidlNanoAppBinary(nanoAppBinary); try { - return mContextHubProxy.loadNanoApp( - contextHubId, hidlNanoAppBinary, this.getTransactionId()); + return mContextHubProxy.loadNanoapp( + contextHubId, nanoAppBinary, this.getTransactionId()); } catch (RemoteException e) { Log.e(TAG, "RemoteException while trying to load nanoapp with ID 0x" + Long.toHexString(nanoAppBinary.getNanoAppId()), e); - return Result.UNKNOWN_FAILURE; + return ContextHubTransaction.RESULT_FAILED_UNKNOWN; } } @@ -194,12 +189,12 @@ import java.util.concurrent.atomic.AtomicInteger; @Override /* package */ int onTransact() { try { - return mContextHubProxy.unloadNanoApp( + return mContextHubProxy.unloadNanoapp( contextHubId, nanoAppId, this.getTransactionId()); } catch (RemoteException e) { Log.e(TAG, "RemoteException while trying to unload nanoapp with ID 0x" + Long.toHexString(nanoAppId), e); - return Result.UNKNOWN_FAILURE; + return ContextHubTransaction.RESULT_FAILED_UNKNOWN; } } @@ -237,12 +232,12 @@ import java.util.concurrent.atomic.AtomicInteger; @Override /* package */ int onTransact() { try { - return mContextHubProxy.enableNanoApp( + 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; + return ContextHubTransaction.RESULT_FAILED_UNKNOWN; } } @@ -274,12 +269,12 @@ import java.util.concurrent.atomic.AtomicInteger; @Override /* package */ int onTransact() { try { - return mContextHubProxy.disableNanoApp( + return mContextHubProxy.disableNanoapp( contextHubId, nanoAppId, this.getTransactionId()); } catch (RemoteException e) { Log.e(TAG, "RemoteException while trying to disable nanoapp with ID 0x" + Long.toHexString(nanoAppId), e); - return Result.UNKNOWN_FAILURE; + return ContextHubTransaction.RESULT_FAILED_UNKNOWN; } } @@ -310,10 +305,10 @@ import java.util.concurrent.atomic.AtomicInteger; @Override /* package */ int onTransact() { try { - return mContextHubProxy.queryApps(contextHubId); + return mContextHubProxy.queryNanoapps(contextHubId); } catch (RemoteException e) { Log.e(TAG, "RemoteException while trying to query for nanoapps", e); - return Result.UNKNOWN_FAILURE; + return ContextHubTransaction.RESULT_FAILED_UNKNOWN; } } @@ -336,7 +331,7 @@ import java.util.concurrent.atomic.AtomicInteger; /** * Adds a new transaction to the queue. - * + *

* If there was no pending transaction at the time, the transaction that was added will be * started in this method. If there were too many transactions in the queue, an exception will * be thrown. @@ -363,10 +358,10 @@ import java.util.concurrent.atomic.AtomicInteger; * Handles a transaction response from a Context Hub. * * @param transactionId the transaction ID of the response - * @param result the result of the transaction as defined by the HAL TransactionResult + * @param success true if the transaction succeeded */ /* package */ - synchronized void onTransactionResponse(int transactionId, int result) { + synchronized void onTransactionResponse(int transactionId, boolean success) { ContextHubServiceTransaction transaction = mTransactionQueue.peek(); if (transaction == null) { Log.w(TAG, "Received unexpected transaction response (no transaction pending)"); @@ -378,9 +373,7 @@ import java.util.concurrent.atomic.AtomicInteger; return; } - transaction.onTransactionComplete( - (result == TransactionResult.SUCCESS) ? - ContextHubTransaction.RESULT_SUCCESS : + transaction.onTransactionComplete(success ? ContextHubTransaction.RESULT_SUCCESS : ContextHubTransaction.RESULT_FAILED_AT_HUB); removeTransactionAndStartNext(); } @@ -421,11 +414,11 @@ import java.util.concurrent.atomic.AtomicInteger; /** * Pops the front transaction from the queue and starts the next pending transaction request. - * + *

* Removing elements from the transaction queue must only be done through this method. When a * pending transaction is removed, the timeout timer is cancelled and the transaction is marked * complete. - * + *

* It is assumed that the transaction queue is non-empty when this method is invoked, and that * the caller has obtained a lock on this ContextHubTransactionManager object. */ @@ -442,21 +435,21 @@ import java.util.concurrent.atomic.AtomicInteger; /** * Starts the next pending transaction request. - * + *

* Starting new transactions must only be done through this method. This method continues to * process the transaction queue as long as there are pending requests, and no transaction is * pending. - * + *

* It is assumed that the caller has obtained a lock on this ContextHubTransactionManager * object. */ private void startNextTransaction() { - int result = Result.UNKNOWN_FAILURE; - while (result != Result.OK && !mTransactionQueue.isEmpty()) { + int result = ContextHubTransaction.RESULT_FAILED_UNKNOWN; + while (result != ContextHubTransaction.RESULT_SUCCESS && !mTransactionQueue.isEmpty()) { ContextHubServiceTransaction transaction = mTransactionQueue.peek(); result = transaction.onTransact(); - if (result == Result.OK) { + if (result == ContextHubTransaction.RESULT_SUCCESS) { Runnable onTimeoutFunc = () -> { synchronized (this) { if (!transaction.isComplete()) { diff --git a/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java b/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java index 0d6dbed3907ad..092e1a1be7ee4 100644 --- a/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java +++ b/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java @@ -23,6 +23,7 @@ import android.hardware.contexthub.V1_1.SettingValue; import android.hardware.contexthub.V1_2.IContexthubCallback; import android.hardware.location.ContextHubInfo; import android.hardware.location.ContextHubTransaction; +import android.hardware.location.NanoAppBinary; import android.hardware.location.NanoAppMessage; import android.os.RemoteException; import android.util.Log; @@ -107,11 +108,6 @@ public abstract class IContextHubWrapper { public abstract void registerCallback( int hubId, IContexthubCallback callback) throws RemoteException; - /** - * @return A valid instance of Contexthub HAL 1.0. - */ - public abstract android.hardware.contexthub.V1_0.IContexthub getHub(); - /** * @return True if this version of the Contexthub HAL supports Location setting notifications. */ @@ -174,6 +170,48 @@ public abstract class IContextHubWrapper { short hostEndpointId, int contextHubId, NanoAppMessage message) throws RemoteException; + /** + * Loads a nanoapp on the Context Hub. + * + * @param contextHubId The ID of the Context Hub to load the nanoapp to. + * @param binary The nanoapp binary to load. + * @param transactionId The transaction ID of this load. + * @return the result of this load transaction. + */ + @ContextHubTransaction.Result + public abstract int loadNanoapp(int contextHubId, NanoAppBinary binary, + int transactionId) throws RemoteException; + + /** + * Unloads a nanoapp on the Context Hub. Semantics are similar to loadNanoapp(). + */ + @ContextHubTransaction.Result + public abstract int unloadNanoapp(int contextHubId, long nanoappId, + int transactionId) throws RemoteException; + + /** + * Enables a nanoapp on the Context Hub. Semantics are similar to loadNanoapp(). + */ + @ContextHubTransaction.Result + public abstract int enableNanoapp(int contextHubId, long nanoappId, + int transactionId) throws RemoteException; + + /** + * Disables a nanoapp on the Context Hub. Semantics are similar to loadNanoapp(). + */ + @ContextHubTransaction.Result + public abstract int disableNanoapp(int contextHubId, long nanoappId, + int transactionId) throws RemoteException; + + /** + * Queries a list of nanoapp from the Context hub. + * + * @param contextHubId The ID of the Context Hub to query. + * @return the result of this query transaction. + */ + @ContextHubTransaction.Result + public abstract int queryNanoapps(int contextHubId) throws RemoteException; + /** * An abstract call that defines methods common to all HIDL IContextHubWrappers. */ @@ -193,6 +231,42 @@ public abstract class IContextHubWrapper { return ContextHubServiceUtil.toTransactionResult( mHub.sendMessageToHub(contextHubId, messageToNanoApp)); } + + @ContextHubTransaction.Result + public int loadNanoapp(int contextHubId, NanoAppBinary binary, + int transactionId) throws RemoteException { + android.hardware.contexthub.V1_0.NanoAppBinary hidlNanoAppBinary = + ContextHubServiceUtil.createHidlNanoAppBinary(binary); + return ContextHubServiceUtil.toTransactionResult(mHub.loadNanoApp( + contextHubId, hidlNanoAppBinary, transactionId)); + } + + @ContextHubTransaction.Result + public int unloadNanoapp(int contextHubId, long nanoappId, int transactionId) + throws RemoteException { + return ContextHubServiceUtil.toTransactionResult(mHub.unloadNanoApp( + contextHubId, nanoappId, transactionId)); + } + + @ContextHubTransaction.Result + public int enableNanoapp(int contextHubId, long nanoappId, int transactionId) + throws RemoteException { + return ContextHubServiceUtil.toTransactionResult(mHub.enableNanoApp( + contextHubId, nanoappId, transactionId)); + } + + @ContextHubTransaction.Result + public int disableNanoapp(int contextHubId, long nanoappId, int transactionId) + throws RemoteException { + return ContextHubServiceUtil.toTransactionResult(mHub.disableNanoApp( + contextHubId, nanoappId, transactionId)); + } + + @ContextHubTransaction.Result + public int queryNanoapps(int contextHubId) throws RemoteException { + return ContextHubServiceUtil.toTransactionResult( + mHub.queryApps(contextHubId)); + } } private static class ContextHubWrapperV1_0 extends ContextHubWrapperHidl { @@ -216,10 +290,6 @@ public abstract class IContextHubWrapper { mHub.registerCallback(hubId, callback); } - public android.hardware.contexthub.V1_0.IContexthub getHub() { - return mHub; - } - public boolean supportsLocationSettingNotifications() { return false; } @@ -270,10 +340,6 @@ public abstract class IContextHubWrapper { mHub.registerCallback(hubId, callback); } - public android.hardware.contexthub.V1_0.IContexthub getHub() { - return mHub; - } - public boolean supportsLocationSettingNotifications() { return true; } @@ -340,10 +406,6 @@ public abstract class IContextHubWrapper { mHub.registerCallback_1_2(hubId, callback); } - public android.hardware.contexthub.V1_0.IContexthub getHub() { - return mHub; - } - public boolean supportsLocationSettingNotifications() { return true; }