From ed470dafb8013b93ddecf4096e2723d819f01c19 Mon Sep 17 00:00:00 2001 From: Arthur Ishiguro Date: Mon, 30 Aug 2021 12:07:55 -0700 Subject: [PATCH] Remove HIDL references in ContextHubClientBroker/Manager Bug: 194285834 Test: Presubmits Change-Id: I91394259f819005cecf297d3268e85b4b4fb95bc --- .../contexthub/ContextHubClientBroker.java | 17 +++--- .../contexthub/ContextHubClientManager.java | 23 ++++---- .../contexthub/ContextHubService.java | 4 +- .../contexthub/IContextHubWrapper.java | 53 ++++++++++++++++--- 4 files changed, 66 insertions(+), 31 deletions(-) diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubClientBroker.java b/services/core/java/com/android/server/location/contexthub/ContextHubClientBroker.java index 3da5a43ee95b4..489b9b3136caf 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubClientBroker.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubClientBroker.java @@ -30,8 +30,6 @@ import android.compat.annotation.ChangeId; import android.compat.annotation.EnabledAfter; import android.content.Context; import android.content.Intent; -import android.hardware.contexthub.V1_0.ContextHubMsg; -import android.hardware.contexthub.V1_0.Result; import android.hardware.location.ContextHubInfo; import android.hardware.location.ContextHubManager; import android.hardware.location.ContextHubTransaction; @@ -383,23 +381,20 @@ public class ContextHubClientBroker extends IContextHubClient.Stub checkNanoappPermsAsync(); } - ContextHubMsg messageToNanoApp = - ContextHubServiceUtil.createHidlContextHubMessage(mHostEndPointId, message); - - int contextHubId = mAttachedContextHubInfo.getId(); try { - result = mContextHubProxy.getHub().sendMessageToHub(contextHubId, messageToNanoApp); + result = mContextHubProxy.sendMessageToContextHub( + mHostEndPointId, mAttachedContextHubInfo.getId(), message); } catch (RemoteException e) { Log.e(TAG, "RemoteException in sendMessageToNanoApp (target hub ID = " - + contextHubId + ")", e); - result = Result.UNKNOWN_FAILURE; + + mAttachedContextHubInfo.getId() + ")", e); + result = ContextHubTransaction.RESULT_FAILED_UNKNOWN; } } else { Log.e(TAG, "Failed to send message to nanoapp: client connection is closed"); - result = Result.UNKNOWN_FAILURE; + result = ContextHubTransaction.RESULT_FAILED_UNKNOWN; } - return ContextHubServiceUtil.toTransactionResult(result); + return result; } /** diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubClientManager.java b/services/core/java/com/android/server/location/contexthub/ContextHubClientManager.java index e3522f6d487c1..41a406ce7fcdb 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubClientManager.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubClientManager.java @@ -19,7 +19,6 @@ package com.android.server.location.contexthub; import android.annotation.IntDef; import android.app.PendingIntent; import android.content.Context; -import android.hardware.contexthub.V1_0.ContextHubMsg; import android.hardware.location.ContextHubInfo; import android.hardware.location.IContextHubClient; import android.hardware.location.IContextHubClientCallback; @@ -227,37 +226,37 @@ import java.util.function.Consumer; * Handles a message sent from a nanoapp. * * @param contextHubId the ID of the hub where the nanoapp sent the message from + * @param hostEndpointId The host endpoint ID of the client that this message is for. * @param message the message send by a nanoapp * @param nanoappPermissions the set of permissions the nanoapp holds * @param messagePermissions the set of permissions that should be used for attributing * permissions when this message is consumed by a client */ /* package */ void onMessageFromNanoApp( - int contextHubId, ContextHubMsg message, List nanoappPermissions, - List messagePermissions) { - NanoAppMessage clientMessage = ContextHubServiceUtil.createNanoAppMessage(message); - + int contextHubId, short hostEndpointId, NanoAppMessage message, + List nanoappPermissions, List messagePermissions) { if (DEBUG_LOG_ENABLED) { - Log.v(TAG, "Received " + clientMessage); + Log.v(TAG, "Received " + message); } - if (clientMessage.isBroadcastMessage()) { + if (message.isBroadcastMessage()) { // Broadcast messages shouldn't be sent with any permissions tagged per CHRE API // requirements. if (!messagePermissions.isEmpty()) { - Log.wtf(TAG, "Received broadcast message with permissions from " + message.appName); + Log.wtf(TAG, "Received broadcast message with permissions from " + + message.getNanoAppId()); } broadcastMessage( - contextHubId, clientMessage, nanoappPermissions, messagePermissions); + contextHubId, message, nanoappPermissions, messagePermissions); } else { - ContextHubClientBroker proxy = mHostEndPointIdToClientMap.get(message.hostEndPoint); + ContextHubClientBroker proxy = mHostEndPointIdToClientMap.get(hostEndpointId); if (proxy != null) { proxy.sendMessageToClient( - clientMessage, nanoappPermissions, messagePermissions); + message, nanoappPermissions, messagePermissions); } else { Log.e(TAG, "Cannot send message to unregistered client (host endpoint ID = " - + message.hostEndPoint + ")"); + + hostEndpointId + ")"); } } } 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 350a30195700f..0313b0cde2b9e 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubService.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubService.java @@ -636,8 +636,10 @@ public class ContextHubService extends IContextHubService.Stub { private void handleClientMessageCallback( int contextHubId, ContextHubMsg message, List nanoappPermissions, List messagePermissions) { + NanoAppMessage clientMessage = ContextHubServiceUtil.createNanoAppMessage(message); mClientManager.onMessageFromNanoApp( - contextHubId, message, nanoappPermissions, messagePermissions); + contextHubId, message.hostEndPoint, clientMessage, nanoappPermissions, + messagePermissions); } /** 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 4b6d093e4dc49..0d6dbed3907ad 100644 --- a/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java +++ b/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java @@ -17,10 +17,13 @@ package com.android.server.location.contexthub; import android.annotation.Nullable; import android.hardware.contexthub.V1_0.ContextHub; +import android.hardware.contexthub.V1_0.ContextHubMsg; import android.hardware.contexthub.V1_1.Setting; 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.NanoAppMessage; import android.os.RemoteException; import android.util.Log; import android.util.Pair; @@ -148,21 +151,55 @@ public abstract class IContextHubWrapper { public abstract void onAirplaneModeSettingChanged(boolean enabled); /** - * @return True if this version of the Contexthub HAL supports microphone - * disable setting notifications. + * @return True if this version of the Contexthub HAL supports microphone disable setting + * notifications. */ public abstract boolean supportsMicrophoneDisableSettingNotifications(); /** - * Notifies the Contexthub implementation of a microphone disable setting - * change. + * Notifies the Contexthub implementation of a microphone disable setting change. */ public abstract void onMicrophoneDisableSettingChanged(boolean enabled); - private static class ContextHubWrapperV1_0 extends IContextHubWrapper { + /** + * Sends a message to the Context Hub. + * + * @param hostEndpointId The host endpoint ID of the sender. + * @param contextHubId The ID of the Context Hub to send the message to. + * @param message The message to send. + * @return the result of the message sending. + */ + @ContextHubTransaction.Result + public abstract int sendMessageToContextHub( + short hostEndpointId, int contextHubId, NanoAppMessage message) + throws RemoteException; + + /** + * An abstract call that defines methods common to all HIDL IContextHubWrappers. + */ + private abstract static class ContextHubWrapperHidl extends IContextHubWrapper { + private android.hardware.contexthub.V1_0.IContexthub mHub; + + ContextHubWrapperHidl(android.hardware.contexthub.V1_0.IContexthub hub) { + mHub = hub; + } + + @ContextHubTransaction.Result + public int sendMessageToContextHub( + short hostEndpointId, int contextHubId, NanoAppMessage message) + throws RemoteException { + ContextHubMsg messageToNanoApp = + ContextHubServiceUtil.createHidlContextHubMessage(hostEndpointId, message); + return ContextHubServiceUtil.toTransactionResult( + mHub.sendMessageToHub(contextHubId, messageToNanoApp)); + } + } + + private static class ContextHubWrapperV1_0 extends ContextHubWrapperHidl { private android.hardware.contexthub.V1_0.IContexthub mHub; ContextHubWrapperV1_0(android.hardware.contexthub.V1_0.IContexthub hub) { + super(hub); mHub = hub; } @@ -212,10 +249,11 @@ public abstract class IContextHubWrapper { } } - private static class ContextHubWrapperV1_1 extends IContextHubWrapper { + private static class ContextHubWrapperV1_1 extends ContextHubWrapperHidl { private android.hardware.contexthub.V1_1.IContexthub mHub; ContextHubWrapperV1_1(android.hardware.contexthub.V1_1.IContexthub hub) { + super(hub); mHub = hub; } @@ -271,7 +309,7 @@ public abstract class IContextHubWrapper { } } - private static class ContextHubWrapperV1_2 extends IContextHubWrapper + private static class ContextHubWrapperV1_2 extends ContextHubWrapperHidl implements android.hardware.contexthub.V1_2.IContexthub.getHubs_1_2Callback { private final android.hardware.contexthub.V1_2.IContexthub mHub; @@ -279,6 +317,7 @@ public abstract class IContextHubWrapper { new Pair<>(Collections.emptyList(), Collections.emptyList()); ContextHubWrapperV1_2(android.hardware.contexthub.V1_2.IContexthub hub) { + super(hub); mHub = hub; }