Merge changes I0913fafa,I91394259

* changes:
  Remove HIDL references in ContextHubTransactionManager
  Remove HIDL references in ContextHubClientBroker/Manager
This commit is contained in:
TreeHugger Robot
2021-08-31 15:02:08 +00:00
committed by Android (Google) Code Review
5 changed files with 189 additions and 97 deletions

View File

@@ -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;
}
/**

View File

@@ -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<String> nanoappPermissions,
List<String> messagePermissions) {
NanoAppMessage clientMessage = ContextHubServiceUtil.createNanoAppMessage(message);
int contextHubId, short hostEndpointId, NanoAppMessage message,
List<String> nanoappPermissions, List<String> 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 + ")");
}
}
}

View File

@@ -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.
*
* <p>
* 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.
*
@@ -636,8 +637,10 @@ public class ContextHubService extends IContextHubService.Stub {
private void handleClientMessageCallback(
int contextHubId, ContextHubMsg message, List<String> nanoappPermissions,
List<String> messagePermissions) {
NanoAppMessage clientMessage = ContextHubServiceUtil.createNanoAppMessage(message);
mClientManager.onMessageFromNanoApp(
contextHubId, message, nanoappPermissions, messagePermissions);
contextHubId, message.hostEndPoint, clientMessage, nanoappPermissions,
messagePermissions);
}
/**
@@ -662,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.
*
* <p>
* TODO(b/69270990): Remove this once the old APIs are obsolete.
*/
private void handleUnloadResponseOldApi(int contextHubId, int result) {
@@ -676,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);
}
/**
@@ -770,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
@@ -1092,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(
@@ -1120,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());

View File

@@ -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.
*
* <p>
* 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.
*
* <p>
* 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.
*
* <p>
* 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.
*
* <p>
* 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.
*
* <p>
* 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.
*
* <p>
* 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()) {

View File

@@ -17,10 +17,14 @@ 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.NanoAppBinary;
import android.hardware.location.NanoAppMessage;
import android.os.RemoteException;
import android.util.Log;
import android.util.Pair;
@@ -104,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.
*/
@@ -148,21 +147,133 @@ 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;
/**
* 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.
*/
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));
}
@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 {
private android.hardware.contexthub.V1_0.IContexthub mHub;
ContextHubWrapperV1_0(android.hardware.contexthub.V1_0.IContexthub hub) {
super(hub);
mHub = hub;
}
@@ -179,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;
}
@@ -212,10 +319,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;
}
@@ -232,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;
}
@@ -271,7 +375,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 +383,7 @@ public abstract class IContextHubWrapper {
new Pair<>(Collections.emptyList(), Collections.emptyList());
ContextHubWrapperV1_2(android.hardware.contexthub.V1_2.IContexthub hub) {
super(hub);
mHub = hub;
}
@@ -301,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;
}