Loading services/core/java/com/android/server/location/contexthub/ContextHubService.java +45 −52 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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. */ Loading Loading @@ -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) { Loading @@ -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 */); } @Override public void handleTxnResult(int transactionId, int result) { handleTransactionResultCallback(mContextHubId, transactionId, result == TransactionResult.SUCCESS); public void handleTransactionResult(int transactionId, boolean success) { handleTransactionResultCallback(mContextHubId, transactionId, 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) { Loading Loading @@ -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); } Loading Loading @@ -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 = Loading @@ -579,7 +575,7 @@ public class ContextHubService extends IContextHubService.Stub { contextHubId, onCompleteCallback, getCallingPackageName()); mTransactionManager.addTransaction(transaction); return Result.OK; return true; } @Override Loading @@ -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()); } Loading @@ -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); } /** Loading Loading @@ -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(); Loading Loading @@ -724,14 +720,11 @@ 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 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); } /** Loading services/core/java/com/android/server/location/contexthub/ContextHubServiceUtil.java +18 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; } } } services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java +117 −21 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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. * Loading Loading @@ -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. */ Loading Loading @@ -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; } Loading Loading @@ -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 { Loading @@ -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; } Loading Loading @@ -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; } Loading Loading @@ -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; } Loading Loading @@ -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); Loading services/core/java/com/android/server/location/contexthub/NanoAppStateManager.java +18 −16 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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. * Loading Loading @@ -99,7 +99,7 @@ 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. * Loading Loading @@ -148,14 +148,16 @@ 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 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(); Loading Loading
services/core/java/com/android/server/location/contexthub/ContextHubService.java +45 −52 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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. */ Loading Loading @@ -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) { Loading @@ -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 */); } @Override public void handleTxnResult(int transactionId, int result) { handleTransactionResultCallback(mContextHubId, transactionId, result == TransactionResult.SUCCESS); public void handleTransactionResult(int transactionId, boolean success) { handleTransactionResultCallback(mContextHubId, transactionId, 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) { Loading Loading @@ -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); } Loading Loading @@ -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 = Loading @@ -579,7 +575,7 @@ public class ContextHubService extends IContextHubService.Stub { contextHubId, onCompleteCallback, getCallingPackageName()); mTransactionManager.addTransaction(transaction); return Result.OK; return true; } @Override Loading @@ -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()); } Loading @@ -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); } /** Loading Loading @@ -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(); Loading Loading @@ -724,14 +720,11 @@ 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 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); } /** Loading
services/core/java/com/android/server/location/contexthub/ContextHubServiceUtil.java +18 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; } } }
services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java +117 −21 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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. * Loading Loading @@ -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. */ Loading Loading @@ -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; } Loading Loading @@ -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 { Loading @@ -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; } Loading Loading @@ -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; } Loading Loading @@ -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; } Loading Loading @@ -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); Loading
services/core/java/com/android/server/location/contexthub/NanoAppStateManager.java +18 −16 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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. * Loading Loading @@ -99,7 +99,7 @@ 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. * Loading Loading @@ -148,14 +148,16 @@ 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 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(); Loading