diff --git a/core/java/android/hardware/location/IContextHubService.aidl b/core/java/android/hardware/location/IContextHubService.aidl index 490267f36b7d4..11f3046150d3b 100644 --- a/core/java/android/hardware/location/IContextHubService.aidl +++ b/core/java/android/hardware/location/IContextHubService.aidl @@ -113,4 +113,8 @@ interface IContextHubService { // Queries for a list of preloaded nanoapps @EnforcePermission("ACCESS_CONTEXT_HUB") long[] getPreloadedNanoAppIds(in ContextHubInfo hubInfo); + + // Enables or disables test mode + @EnforcePermission("ACCESS_CONTEXT_HUB") + boolean setTestMode(in boolean enable); } 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 7f6c2d6ececcb..8f657756eed37 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubService.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubService.java @@ -1155,6 +1155,34 @@ public class ContextHubService extends IContextHubService.Stub { return nanoappIds; } + @android.annotation.EnforcePermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) + /** + * Puts the context hub in and out of test mode. Test mode is a clean state + * where tests can be executed in the same environment. If enable is true, + * this will enable test mode by unloading all nanoapps. If enable is false, + * this will disable test mode and reverse the actions of enabling test mode + * by loading all preloaded nanoapps. This puts CHRE in a normal state. + * + * This should only be used for a test environment, either through a + * @TestApi or development tools. This should not be used in a production + * environment. + * + * @param enable If true, put the context hub in test mode. If false, disable + * test mode. + * @return If true, the operation was successful; false otherwise. + */ + @Override + public boolean setTestMode(boolean enable) { + super.setTestMode_enforcePermission(); + boolean status = mContextHubWrapper.setTestMode(enable); + + // Query nanoapps to update service state after test mode state change. + for (int contextHubId: mDefaultClientMap.keySet()) { + queryNanoAppsInternal(contextHubId); + } + return status; + } + @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; 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 3581b02b7fe31..ca184ee099079 100644 --- a/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java +++ b/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java @@ -382,6 +382,23 @@ public abstract class IContextHubWrapper { */ public abstract void registerExistingCallback(int contextHubId) throws RemoteException; + /** + * Puts the context hub in and out of test mode. Test mode is a clean state + * where tests can be executed in the same environment. If enable is true, + * this will enable test mode by unloading all nanoapps. If enable is false, + * this will disable test mode and reverse the actions of enabling test mode + * by loading all preloaded nanoapps. This puts CHRE in a normal state. + * + * This should only be used for a test environment, either through a + * @TestApi or development tools. This should not be used in a production + * environment. + * + * @param enable If true, put the context hub in test mode. If false, disable + * test mode. + * @return If true, the operation was successful; false otherwise. + */ + public abstract boolean setTestMode(boolean enable); + private static class ContextHubWrapperAidl extends IContextHubWrapper implements IBinder.DeathRecipient { private android.hardware.contexthub.IContextHub mHub; @@ -741,6 +758,22 @@ public abstract class IContextHubWrapper { registerExistingCallback(contextHubId); } + public boolean setTestMode(boolean enable) { + android.hardware.contexthub.IContextHub hub = getHub(); + if (hub == null) { + return false; + } + + try { + hub.setTestMode(enable); + return true; + } catch (RemoteException | ServiceSpecificException e) { + Log.e(TAG, "Exception while setting test mode (enable: " + + (enable ? "true" : "false") + "): " + e.getMessage()); + return false; + } + } + private void onSettingChanged(byte setting, boolean enabled) { android.hardware.contexthub.IContextHub hub = getHub(); if (hub == null) { @@ -911,6 +944,10 @@ public abstract class IContextHubWrapper { mHub.registerCallback(contextHubId, callback); } + public boolean setTestMode(boolean enable) { + return false; + } + public boolean supportsBtSettingNotifications() { return false; }