diff options
5 files changed, 102 insertions, 42 deletions
diff --git a/core/java/android/hardware/location/ContextHubManager.java b/core/java/android/hardware/location/ContextHubManager.java index 7b9f8f09667a..1d66dc6d939f 100644 --- a/core/java/android/hardware/location/ContextHubManager.java +++ b/core/java/android/hardware/location/ContextHubManager.java @@ -421,7 +421,17 @@ public final class ContextHubManager { */ @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) public ContextHubTransaction<List<NanoAppState>> queryNanoApps(ContextHubInfo hubInfo) { - throw new UnsupportedOperationException("TODO: Implement this"); + ContextHubTransaction<List<NanoAppState>> transaction = + new ContextHubTransaction<>(ContextHubTransaction.TYPE_QUERY_NANOAPPS); + IContextHubTransactionCallback callback = createQueryCallback(transaction); + + try { + mService.queryNanoApps(hubInfo.getId(), callback); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + + return transaction; } /** diff --git a/core/java/android/hardware/location/ContextHubTransaction.java b/core/java/android/hardware/location/ContextHubTransaction.java index d35aa3cc7aef..b808de3a11d6 100644 --- a/core/java/android/hardware/location/ContextHubTransaction.java +++ b/core/java/android/hardware/location/ContextHubTransaction.java @@ -194,6 +194,30 @@ public class ContextHubTransaction<T> { } /** + * Converts a transaction type to a human-readable string + * + * @param type the type of a transaction + * @param upperCase {@code true} if upper case the first letter, {@code false} otherwise + * @return a string describing the transaction + */ + public static String typeToString(@Type int type, boolean upperCase) { + switch (type) { + case ContextHubTransaction.TYPE_LOAD_NANOAPP: + return upperCase ? "Load" : "load"; + case ContextHubTransaction.TYPE_UNLOAD_NANOAPP: + return upperCase ? "Unload" : "unload"; + case ContextHubTransaction.TYPE_ENABLE_NANOAPP: + return upperCase ? "Enable" : "enable"; + case ContextHubTransaction.TYPE_DISABLE_NANOAPP: + return upperCase ? "Disable" : "disable"; + case ContextHubTransaction.TYPE_QUERY_NANOAPPS: + return upperCase ? "Query" : "query"; + default: + return upperCase ? "Unknown" : "unknown"; + } + } + + /** * @return the type of the transaction */ @Type @@ -244,7 +268,7 @@ public class ContextHubTransaction<T> { * A transaction can be invalidated if the process owning the transaction is no longer active * and the reference to this object is lost. * - * This method or {@link #setCallbackOnCompletecan(ContextHubTransaction.Callback)} can only be + * This method or {@link #setCallbackOnComplete(ContextHubTransaction.Callback)} can only be * invoked once, or an IllegalStateException will be thrown. * * @param callback the callback to be invoked upon completion diff --git a/core/java/android/hardware/location/IContextHubService.aidl b/core/java/android/hardware/location/IContextHubService.aidl index 3b39c8d11c30..628ebc7d4579 100644 --- a/core/java/android/hardware/location/IContextHubService.aidl +++ b/core/java/android/hardware/location/IContextHubService.aidl @@ -69,4 +69,7 @@ interface IContextHubService { void unloadNanoAppFromHub( int contextHubId, in IContextHubTransactionCallback transactionCallback, long nanoAppId); + + // Queries for a list of nanoapps + void queryNanoApps(int contextHubId, in IContextHubTransactionCallback transactionCallback); } diff --git a/services/core/java/com/android/server/location/ContextHubService.java b/services/core/java/com/android/server/location/ContextHubService.java index fcf1f14df162..08c18a68f09d 100644 --- a/services/core/java/com/android/server/location/ContextHubService.java +++ b/services/core/java/com/android/server/location/ContextHubService.java @@ -752,15 +752,8 @@ public class ContextHubService extends IContextHubService.Stub { int contextHubId, IContextHubTransactionCallback transactionCallback, NanoAppBinary nanoAppBinary) throws RemoteException { checkPermissions(); - if (mContextHubProxy == null) { - transactionCallback.onTransactionComplete( - ContextHubTransaction.TRANSACTION_FAILED_HAL_UNAVAILABLE); - return; - } - if (!isValidContextHubId(contextHubId)) { - Log.e(TAG, "Cannot load nanoapp for invalid hub ID " + contextHubId); - transactionCallback.onTransactionComplete( - ContextHubTransaction.TRANSACTION_FAILED_BAD_PARAMS); + if (!checkHalProxyAndContextHubId( + contextHubId, transactionCallback, ContextHubTransaction.TYPE_LOAD_NANOAPP)) { return; } if (nanoAppBinary == null) { @@ -772,7 +765,6 @@ public class ContextHubService extends IContextHubService.Stub { ContextHubServiceTransaction transaction = mTransactionManager.createLoadTransaction( contextHubId, nanoAppBinary, transactionCallback); - addTransaction(transaction); } @@ -790,21 +782,35 @@ public class ContextHubService extends IContextHubService.Stub { int contextHubId, IContextHubTransactionCallback transactionCallback, long nanoAppId) throws RemoteException { checkPermissions(); - if (mContextHubProxy == null) { - transactionCallback.onTransactionComplete( - ContextHubTransaction.TRANSACTION_FAILED_HAL_UNAVAILABLE); - return; - } - if (!isValidContextHubId(contextHubId)) { - Log.e(TAG, "Cannot unload nanoapp for invalid hub ID " + contextHubId); - transactionCallback.onTransactionComplete( - ContextHubTransaction.TRANSACTION_FAILED_BAD_PARAMS); + if (!checkHalProxyAndContextHubId( + contextHubId, transactionCallback, ContextHubTransaction.TYPE_UNLOAD_NANOAPP)) { return; } ContextHubServiceTransaction transaction = mTransactionManager.createUnloadTransaction( contextHubId, nanoAppId, transactionCallback); + addTransaction(transaction); + } + /** + * Queries for a list of nanoapps from the specified Context hub. + * + * @param contextHubId the ID of the hub to query + * @param transactionCallback the client-facing transaction callback interface + * + * @throws RemoteException + */ + @Override + public void queryNanoApps(int contextHubId, IContextHubTransactionCallback transactionCallback) + throws RemoteException { + checkPermissions(); + if (!checkHalProxyAndContextHubId( + contextHubId, transactionCallback, ContextHubTransaction.TYPE_QUERY_NANOAPPS)) { + return; + } + + ContextHubServiceTransaction transaction = + mTransactionManager.createQueryTransaction(contextHubId, transactionCallback); addTransaction(transaction); } @@ -863,6 +869,42 @@ public class ContextHubService extends IContextHubService.Stub { return 0; } + /** + * Validates the HAL proxy state and context hub ID to see if we can start the transaction. + * + * @param contextHubId the ID of the hub to start the transaction + * @param callback the client transaction callback interface + * @param transactionType the type of the transaction + * + * @return {@code true} if mContextHubProxy and contextHubId is valid, {@code false} otherwise + */ + private boolean checkHalProxyAndContextHubId( + int contextHubId, IContextHubTransactionCallback callback, + @ContextHubTransaction.Type int transactionType) { + if (mContextHubProxy == null) { + try { + callback.onTransactionComplete( + ContextHubTransaction.TRANSACTION_FAILED_HAL_UNAVAILABLE); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException while calling onTransactionComplete", e); + } + return false; + } + if (!isValidContextHubId(contextHubId)) { + Log.e(TAG, "Cannot start " + + ContextHubTransaction.typeToString(transactionType, false /* upperCase */) + + " transaction for invalid hub ID " + contextHubId); + try { + callback.onTransactionComplete(ContextHubTransaction.TRANSACTION_FAILED_BAD_PARAMS); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException while calling onTransactionComplete", e); + } + return false; + } + + return true; + } + private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) { // App Id encodes vendor & version NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo(); diff --git a/services/core/java/com/android/server/location/ContextHubServiceTransaction.java b/services/core/java/com/android/server/location/ContextHubServiceTransaction.java index a543dafc1ed1..ce92f722e2d2 100644 --- a/services/core/java/com/android/server/location/ContextHubServiceTransaction.java +++ b/services/core/java/com/android/server/location/ContextHubServiceTransaction.java @@ -126,28 +126,9 @@ import java.util.concurrent.TimeUnit; return mIsComplete; } - /** - * @return the human-readable string of this transaction's type - */ - private String getTransactionTypeString() { - switch (mTransactionType) { - case ContextHubTransaction.TYPE_LOAD_NANOAPP: - return "Load"; - case ContextHubTransaction.TYPE_UNLOAD_NANOAPP: - return "Unload"; - case ContextHubTransaction.TYPE_ENABLE_NANOAPP: - return "Enable"; - case ContextHubTransaction.TYPE_DISABLE_NANOAPP: - return "Disable"; - case ContextHubTransaction.TYPE_QUERY_NANOAPPS: - return "Query"; - default: - return "Unknown"; - } - } - @Override public String toString() { - return getTransactionTypeString() + " transaction (ID = " + mTransactionId + ")"; + return ContextHubTransaction.typeToString(mTransactionType, true /* upperCase */) + + " transaction (ID = " + mTransactionId + ")"; } } |