Merge "Removes HIDL references from ContextHubService"

This commit is contained in:
Arthur Ishiguro
2021-09-01 18:48:43 +00:00
committed by Android (Google) Code Review
4 changed files with 198 additions and 89 deletions

View File

@@ -16,6 +16,7 @@
package com.android.server.location.contexthub;
import android.annotation.IntDef;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.PendingIntent;
@@ -27,12 +28,6 @@ import android.content.pm.UserInfo;
import android.database.ContentObserver;
import android.hardware.SensorPrivacyManager;
import android.hardware.SensorPrivacyManagerInternal;
import android.hardware.contexthub.V1_0.AsyncEventType;
import android.hardware.contexthub.V1_0.ContextHubMsg;
import android.hardware.contexthub.V1_0.Result;
import android.hardware.contexthub.V1_0.TransactionResult;
import android.hardware.contexthub.V1_2.HubAppInfo;
import android.hardware.contexthub.V1_2.IContexthubCallback;
import android.hardware.location.ContextHubInfo;
import android.hardware.location.ContextHubMessage;
import android.hardware.location.ContextHubTransaction;
@@ -66,6 +61,8 @@ import com.android.server.location.ContextHubServiceProto;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
@@ -96,6 +93,20 @@ public class ContextHubService extends IContextHubService.Stub {
private static final int OS_APP_INSTANCE = -1;
/**
* Constants describing an async event from the Context Hub.
* {@hide}
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef(prefix = { "CONTEXT_HUB_EVENT_" }, value = {
CONTEXT_HUB_EVENT_UNKNOWN,
CONTEXT_HUB_EVENT_RESTARTED,
})
public @interface Type { }
public static final int CONTEXT_HUB_EVENT_UNKNOWN = 0;
public static final int CONTEXT_HUB_EVENT_RESTARTED = 1;
/*
* Local flag to enable debug logging.
*/
@@ -135,7 +146,7 @@ public class ContextHubService extends IContextHubService.Stub {
/**
* Class extending the callback to register with a Context Hub.
*/
private class ContextHubServiceCallback extends IContexthubCallback.Stub {
private class ContextHubServiceCallback implements IContextHubWrapper.ICallback {
private final int mContextHubId;
ContextHubServiceCallback(int contextHubId) {
@@ -143,46 +154,31 @@ public class ContextHubService extends IContextHubService.Stub {
}
@Override
public void handleClientMsg(ContextHubMsg message) {
handleClientMessageCallback(mContextHubId, message,
Collections.emptyList() /* nanoappPermissions */,
Collections.emptyList() /* messagePermissions */);
public void handleTransactionResult(int transactionId, boolean success) {
handleTransactionResultCallback(mContextHubId, transactionId, success);
}
@Override
public void handleTxnResult(int transactionId, int result) {
handleTransactionResultCallback(mContextHubId, transactionId,
result == TransactionResult.SUCCESS);
}
@Override
public void handleHubEvent(int eventType) {
public void handleContextHubEvent(int eventType) {
handleHubEventCallback(mContextHubId, eventType);
}
@Override
public void handleAppAbort(long nanoAppId, int abortCode) {
handleAppAbortCallback(mContextHubId, nanoAppId, abortCode);
public void handleNanoappAbort(long nanoappId, int abortCode) {
handleAppAbortCallback(mContextHubId, nanoappId, abortCode);
}
@Override
public void handleAppsInfo(
ArrayList<android.hardware.contexthub.V1_0.HubAppInfo> nanoAppInfoList) {
handleQueryAppsCallback(mContextHubId,
ContextHubServiceUtil.toHubAppInfo_1_2(nanoAppInfoList));
public void handleNanoappInfo(List<NanoAppState> nanoappStateList) {
handleQueryAppsCallback(mContextHubId, nanoappStateList);
}
@Override
public void handleClientMsg_1_2(android.hardware.contexthub.V1_2.ContextHubMsg message,
ArrayList<String> messagePermissions) {
handleClientMessageCallback(mContextHubId, message.msg_1_0, message.permissions,
public void handleNanoappMessage(short hostEndpointId, NanoAppMessage message,
List<String> nanoappPermissions, List<String> messagePermissions) {
handleClientMessageCallback(mContextHubId, hostEndpointId, message, nanoappPermissions,
messagePermissions);
}
@Override
public void handleAppsInfo_1_2(ArrayList<HubAppInfo> nanoAppInfoList) {
handleQueryAppsCallback(mContextHubId, nanoAppInfoList);
}
}
public ContextHubService(Context context) {
@@ -329,7 +325,7 @@ public class ContextHubService extends IContextHubService.Stub {
@Override
public void onHubReset() {
byte[] data = {TransactionResult.SUCCESS};
byte[] data = {android.hardware.contexthub.V1_0.TransactionResult.SUCCESS};
onMessageReceiptOldApi(MSG_HUB_RESET, contextHubId, OS_APP_INSTANCE, data);
}
@@ -565,12 +561,12 @@ public class ContextHubService extends IContextHubService.Stub {
* cache or as a result of an explicit query requested by a client through the sendMessage API.
*
* @param contextHubId the ID of the hub to do the query
* @return the result of the query
* @return true if the query succeeded
* @throws IllegalStateException if the transaction queue is full
*/
private int queryNanoAppsInternal(int contextHubId) {
private boolean queryNanoAppsInternal(int contextHubId) {
if (mContextHubWrapper == null) {
return Result.UNKNOWN_FAILURE;
return false;
}
IContextHubTransactionCallback onCompleteCallback =
@@ -579,7 +575,7 @@ public class ContextHubService extends IContextHubService.Stub {
contextHubId, onCompleteCallback, getCallingPackageName());
mTransactionManager.addTransaction(transaction);
return Result.OK;
return true;
}
@Override
@@ -605,7 +601,7 @@ public class ContextHubService extends IContextHubService.Stub {
boolean success = false;
if (nanoAppHandle == OS_APP_INSTANCE) {
if (msg.getMsgType() == MSG_QUERY_NANO_APPS) {
success = (queryNanoAppsInternal(contextHubHandle) == Result.OK);
success = queryNanoAppsInternal(contextHubHandle);
} else {
Log.e(TAG, "Invalid OS message params of type " + msg.getMsgType());
}
@@ -631,16 +627,16 @@ public class ContextHubService extends IContextHubService.Stub {
* Handles a unicast or broadcast message from a nanoapp.
*
* @param contextHubId the ID of the hub the message came from
* @param hostEndpointId the host endpoint ID of the client receiving this message
* @param message the message contents
* @param reqPermissions the permissions required to consume this message
*/
private void handleClientMessageCallback(
int contextHubId, ContextHubMsg message, List<String> nanoappPermissions,
int contextHubId, short hostEndpointId, NanoAppMessage message,
List<String> nanoappPermissions,
List<String> messagePermissions) {
NanoAppMessage clientMessage = ContextHubServiceUtil.createNanoAppMessage(message);
mClientManager.onMessageFromNanoApp(
contextHubId, message.hostEndPoint, clientMessage, nanoappPermissions,
messagePermissions);
contextHubId, hostEndpointId, message, nanoappPermissions, messagePermissions);
}
/**
@@ -690,10 +686,10 @@ public class ContextHubService extends IContextHubService.Stub {
* Handles an asynchronous event from a Context Hub.
*
* @param contextHubId the ID of the hub the response came from
* @param eventType the type of the event as defined in Context Hub HAL AsyncEventType
* @param eventType the type of the event as in CONTEXT_HUB_EVENT_*
*/
private void handleHubEventCallback(int contextHubId, int eventType) {
if (eventType == AsyncEventType.RESTARTED) {
if (eventType == CONTEXT_HUB_EVENT_RESTARTED) {
sendLocationSettingUpdate();
sendWifiSettingUpdate(true /* forceUpdate */);
sendAirplaneModeSettingUpdate();
@@ -723,15 +719,12 @@ public class ContextHubService extends IContextHubService.Stub {
/**
* Handles a query response from a Context Hub.
*
* @param contextHubId the ID of the hub of the response
* @param nanoAppInfoList the list of loaded nanoapps
* @param contextHubId the ID of the hub of the response
* @param nanoappStateList the list of loaded nanoapps
*/
private void handleQueryAppsCallback(int contextHubId, List<HubAppInfo> nanoAppInfoList) {
List<NanoAppState> nanoAppStateList =
ContextHubServiceUtil.createNanoAppStateList(nanoAppInfoList);
mNanoAppStateManager.updateCache(contextHubId, nanoAppInfoList);
mTransactionManager.onQueryResponse(nanoAppStateList);
private void handleQueryAppsCallback(int contextHubId, List<NanoAppState> nanoappStateList) {
mNanoAppStateManager.updateCache(contextHubId, nanoappStateList);
mTransactionManager.onQueryResponse(nanoappStateList);
}
/**

View File

@@ -20,6 +20,7 @@ import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import android.Manifest;
import android.content.Context;
import android.hardware.contexthub.V1_0.AsyncEventType;
import android.hardware.contexthub.V1_0.ContextHubMsg;
import android.hardware.contexthub.V1_0.HostEndPoint;
import android.hardware.contexthub.V1_0.Result;
@@ -256,4 +257,21 @@ import java.util.List;
}
return newAppInfo;
}
/**
* Converts a HIDL AsyncEventType to the corresponding ContextHubService.CONTEXT_HUB_EVENT_*.
*
* @param hidlEventType The AsyncEventType value.
* @return The converted event type.
*/
/* package */
static int toContextHubEvent(int hidlEventType) {
switch (hidlEventType) {
case AsyncEventType.RESTARTED:
return ContextHubService.CONTEXT_HUB_EVENT_RESTARTED;
default:
Log.e(TAG, "toContextHubEvent: Unknown event type: " + hidlEventType);
return ContextHubService.CONTEXT_HUB_EVENT_UNKNOWN;
}
}
}

View File

@@ -15,16 +15,20 @@
*/
package com.android.server.location.contexthub;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.hardware.contexthub.V1_0.ContextHub;
import android.hardware.contexthub.V1_0.ContextHubMsg;
import android.hardware.contexthub.V1_0.TransactionResult;
import android.hardware.contexthub.V1_1.Setting;
import android.hardware.contexthub.V1_1.SettingValue;
import android.hardware.contexthub.V1_2.HubAppInfo;
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.hardware.location.NanoAppState;
import android.os.RemoteException;
import android.util.Log;
import android.util.Pair;
@@ -40,6 +44,45 @@ import java.util.NoSuchElementException;
public abstract class IContextHubWrapper {
private static final String TAG = "IContextHubWrapper";
/**
* The callback interface to use in registerCallback.
*/
public interface ICallback {
/**
* @param transactionId The ID of the transaction that completed.
* @param success true if the transaction succeeded.
*/
void handleTransactionResult(int transactionId, boolean success);
/**
* @param eventType The Context Hub event type defined by ContextHubService
* .CONTEXT_HUB_EVENT_*.
*/
void handleContextHubEvent(int eventType);
/**
* @param nanoappId The ID of the nanoapp that aborted.
* @param abortCode The nanoapp-defined abort code.
*/
void handleNanoappAbort(long nanoappId, int abortCode);
/**
* @param nanoappStateList The list of loaded nanoapps on the Context Hub.
*/
void handleNanoappInfo(List<NanoAppState> nanoappStateList);
/**
* Handles a message from a nanoapp to a ContextHubClient.
*
* @param hostEndpointId The host endpoint ID of the recipient.
* @param message The message from the nanoapp.
* @param nanoappPermissions The list of permissions held by the nanoapp.
* @param messagePermissions The list of permissions required to receive the message.
*/
void handleNanoappMessage(short hostEndpointId, NanoAppMessage message,
List<String> nanoappPermissions, List<String> messagePermissions);
}
/**
* Attempts to connect to the Contexthub HAL 1.0 service, if it exists.
*
@@ -102,12 +145,6 @@ public abstract class IContextHubWrapper {
*/
public abstract Pair<List<ContextHubInfo>, List<String>> getHubs() throws RemoteException;
/**
* Calls the appropriate registerCallback function depending on the HAL version.
*/
public abstract void registerCallback(
int hubId, IContexthubCallback callback) throws RemoteException;
/**
* @return True if this version of the Contexthub HAL supports Location setting notifications.
*/
@@ -212,12 +249,76 @@ public abstract class IContextHubWrapper {
@ContextHubTransaction.Result
public abstract int queryNanoapps(int contextHubId) throws RemoteException;
/**
* Registers a callback with the Context Hub.
*
* @param contextHubId The ID of the Context Hub to register the callback with.
* @param callback The callback to register.
*/
public abstract void registerCallback(int contextHubId, @NonNull ICallback callback)
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;
protected ICallback mCallback = null;
protected final ContextHubWrapperHidlCallback mHidlCallback =
new ContextHubWrapperHidlCallback();
protected class ContextHubWrapperHidlCallback extends IContexthubCallback.Stub {
@Override
public void handleClientMsg(ContextHubMsg message) {
mCallback.handleNanoappMessage(
message.hostEndPoint,
ContextHubServiceUtil.createNanoAppMessage(message),
Collections.emptyList() /* nanoappPermissions */,
Collections.emptyList() /* messagePermissions */);
}
@Override
public void handleTxnResult(int transactionId, int result) {
mCallback.handleTransactionResult(transactionId,
result == TransactionResult.SUCCESS);
}
@Override
public void handleHubEvent(int eventType) {
mCallback.handleContextHubEvent(
ContextHubServiceUtil.toContextHubEvent(eventType));
}
@Override
public void handleAppAbort(long nanoAppId, int abortCode) {
mCallback.handleNanoappAbort(nanoAppId, abortCode);
}
@Override
public void handleAppsInfo(
ArrayList<android.hardware.contexthub.V1_0.HubAppInfo> nanoAppInfoList) {
handleAppsInfo_1_2(ContextHubServiceUtil.toHubAppInfo_1_2(nanoAppInfoList));
}
@Override
public void handleClientMsg_1_2(android.hardware.contexthub.V1_2.ContextHubMsg message,
ArrayList<String> messagePermissions) {
mCallback.handleNanoappMessage(
message.msg_1_0.hostEndPoint,
ContextHubServiceUtil.createNanoAppMessage(message.msg_1_0),
message.permissions, messagePermissions);
}
@Override
public void handleAppsInfo_1_2(ArrayList<HubAppInfo> nanoAppInfoList) {
List<NanoAppState> nanoAppStateList =
ContextHubServiceUtil.createNanoAppStateList(nanoAppInfoList);
mCallback.handleNanoappInfo(nanoAppStateList);
}
}
ContextHubWrapperHidl(android.hardware.contexthub.V1_0.IContexthub hub) {
mHub = hub;
}
@@ -267,6 +368,11 @@ public abstract class IContextHubWrapper {
return ContextHubServiceUtil.toTransactionResult(
mHub.queryApps(contextHubId));
}
public void registerCallback(int contextHubId, ICallback callback) throws RemoteException {
mCallback = callback;
mHub.registerCallback(contextHubId, mHidlCallback);
}
}
private static class ContextHubWrapperV1_0 extends ContextHubWrapperHidl {
@@ -285,11 +391,6 @@ public abstract class IContextHubWrapper {
return new Pair(hubInfoList, new ArrayList<String>());
}
public void registerCallback(
int hubId, IContexthubCallback callback) throws RemoteException {
mHub.registerCallback(hubId, callback);
}
public boolean supportsLocationSettingNotifications() {
return false;
}
@@ -335,11 +436,6 @@ public abstract class IContextHubWrapper {
return new Pair(hubInfoList, new ArrayList<String>());
}
public void registerCallback(
int hubId, IContexthubCallback callback) throws RemoteException {
mHub.registerCallback(hubId, callback);
}
public boolean supportsLocationSettingNotifications() {
return true;
}
@@ -401,11 +497,6 @@ public abstract class IContextHubWrapper {
return mHubInfo;
}
public void registerCallback(
int hubId, IContexthubCallback callback) throws RemoteException {
mHub.registerCallback_1_2(hubId, callback);
}
public boolean supportsLocationSettingNotifications() {
return true;
}
@@ -445,6 +536,11 @@ public abstract class IContextHubWrapper {
enabled ? SettingValue.DISABLED : SettingValue.ENABLED);
}
public void registerCallback(int contextHubId, ICallback callback) throws RemoteException {
mCallback = callback;
mHub.registerCallback_1_2(contextHubId, mHidlCallback);
}
private void sendSettingChanged(byte setting, byte newValue) {
try {
mHub.onSettingChanged_1_2(setting, newValue);

View File

@@ -17,8 +17,8 @@
package com.android.server.location.contexthub;
import android.annotation.Nullable;
import android.hardware.contexthub.V1_2.HubAppInfo;
import android.hardware.location.NanoAppInstanceInfo;
import android.hardware.location.NanoAppState;
import android.util.Log;
import java.util.HashMap;
@@ -31,8 +31,8 @@ import java.util.function.Consumer;
* Manages the state of loaded nanoapps at the Context Hubs.
*
* This class maintains a list of nanoapps that have been informed as loaded at the hubs. The state
* should be updated based on the hub callbacks (defined in IContexthubCallback.hal), as a result
* of either loadNanoApp, unloadNanoApp, or queryApps.
* should be updated based on the hub callbacks (defined in IContexthubCallback.hal), as a result of
* either loadNanoApp, unloadNanoApp, or queryApps.
*
* The state tracked by this manager is used by clients of ContextHubService that use the old APIs.
*
@@ -61,7 +61,7 @@ import java.util.function.Consumer;
/**
* @param nanoAppHandle the nanoapp handle
* @return the NanoAppInstanceInfo for the given nanoapp, or null if the nanoapp does not exist
* in the cache
* in the cache
*/
@Nullable
/* package */
@@ -83,7 +83,7 @@ import java.util.function.Consumer;
/**
* @param contextHubId the ID of the hub to search for the instance
* @param nanoAppId the unique 64-bit ID of the nanoapp
* @param nanoAppId the unique 64-bit ID of the nanoapp
* @return the nanoapp handle, -1 if the nanoapp is not in the cache
*/
/* package */
@@ -99,12 +99,12 @@ import java.util.function.Consumer;
/**
* Adds a nanoapp instance to the cache.
*
* <p>
* If the cache already contained the nanoapp, the entry is removed and a new nanoapp handle is
* generated.
*
* @param contextHubId the ID of the hub the nanoapp is loaded in
* @param nanoAppId the unique 64-bit ID of the nanoapp
* @param contextHubId the ID of the hub the nanoapp is loaded in
* @param nanoAppId the unique 64-bit ID of the nanoapp
* @param nanoAppVersion the version of the nanoapp
*/
/* package */
@@ -147,15 +147,17 @@ import java.util.function.Consumer;
/**
* Performs a batch update of the nanoapp cache given a nanoapp query response.
*
* @param contextHubId the ID of the hub the response came from
* @param nanoAppInfoList the list of loaded nanoapps
* @param contextHubId the ID of the hub the response came from
* @param nanoappStateList the list of loaded nanoapps
*/
/* package */
synchronized void updateCache(int contextHubId, List<HubAppInfo> nanoAppInfoList) {
synchronized void updateCache(int contextHubId, List<NanoAppState> nanoappStateList) {
HashSet<Long> nanoAppIdSet = new HashSet<>();
for (HubAppInfo appInfo : nanoAppInfoList) {
handleQueryAppEntry(contextHubId, appInfo.info_1_0.appId, appInfo.info_1_0.version);
nanoAppIdSet.add(appInfo.info_1_0.appId);
for (NanoAppState nanoappState : nanoappStateList) {
handleQueryAppEntry(
contextHubId, nanoappState.getNanoAppId(),
(int) nanoappState.getNanoAppVersion());
nanoAppIdSet.add(nanoappState.getNanoAppId());
}
Iterator<NanoAppInstanceInfo> iterator = mNanoAppHash.values().iterator();
@@ -172,8 +174,8 @@ import java.util.function.Consumer;
* If the nanoapp exists in the cache, then the entry is updated. Otherwise, inserts a new
* instance of the nanoapp in the cache. This method should only be invoked from updateCache.
*
* @param contextHubId the ID of the hub the nanoapp is loaded in
* @param nanoAppId the unique 64-bit ID of the nanoapp
* @param contextHubId the ID of the hub the nanoapp is loaded in
* @param nanoAppId the unique 64-bit ID of the nanoapp
* @param nanoAppVersion the version of the nanoapp
*/
private void handleQueryAppEntry(int contextHubId, long nanoAppId, int nanoAppVersion) {