From 0069f128d4105100eba85daaa4af849023d1d96a Mon Sep 17 00:00:00 2001 From: Arthur Ishiguro Date: Mon, 20 Nov 2017 15:07:14 -0800 Subject: [PATCH] Handles load and unload lifecycle callbacks Bug: 67734082 Test: Create test app to load/unload a nanoapp after client registration, verify listener callbacks are invoked Change-Id: Ibf8cd200307ace0b3dcc9431166f61fc2929cff4 --- .../location/ContextHubClientBroker.java | 32 +++++++++++++++++++ .../location/ContextHubClientManager.java | 20 ++++++++++++ .../server/location/ContextHubService.java | 2 +- .../ContextHubTransactionManager.java | 15 ++++++++- 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/location/ContextHubClientBroker.java b/services/core/java/com/android/server/location/ContextHubClientBroker.java index 1ba60b7fa7cc5..41d9feb97c4e2 100644 --- a/services/core/java/com/android/server/location/ContextHubClientBroker.java +++ b/services/core/java/com/android/server/location/ContextHubClientBroker.java @@ -178,6 +178,38 @@ public class ContextHubClientBroker extends IContextHubClient.Stub } } + /** + * Handles a nanoapp load event. + * + * @param nanoAppId the ID of the nanoapp that was loaded. + */ + /* package */ void onNanoAppLoaded(long nanoAppId) { + if (mConnectionOpen.get()) { + try { + mCallbackInterface.onNanoAppLoaded(nanoAppId); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException while calling onNanoAppLoaded on client" + + " (host endpoint ID = " + mHostEndPointId + ")", e); + } + } + } + + /** + * Handles a nanoapp unload event. + * + * @param nanoAppId the ID of the nanoapp that was unloaded. + */ + /* package */ void onNanoAppUnloaded(long nanoAppId) { + if (mConnectionOpen.get()) { + try { + mCallbackInterface.onNanoAppUnloaded(nanoAppId); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException while calling onNanoAppUnloaded on client" + + " (host endpoint ID = " + mHostEndPointId + ")", e); + } + } + } + /** * Handles a hub reset for this client. */ diff --git a/services/core/java/com/android/server/location/ContextHubClientManager.java b/services/core/java/com/android/server/location/ContextHubClientManager.java index 5d0d41f27e178..d58a7460b23fa 100644 --- a/services/core/java/com/android/server/location/ContextHubClientManager.java +++ b/services/core/java/com/android/server/location/ContextHubClientManager.java @@ -148,6 +148,26 @@ import java.util.function.Consumer; } } + /** + * Handles a nanoapp load event. + * + * @param contextHubId the ID of the hub where the nanoapp was loaded. + * @param nanoAppId the ID of the nanoapp that was loaded. + */ + /* package */ void onNanoAppLoaded(int contextHubId, long nanoAppId) { + forEachClientOfHub(contextHubId, client -> client.onNanoAppLoaded(nanoAppId)); + } + + /** + * Handles a nanoapp unload event. + * + * @param contextHubId the ID of the hub where the nanoapp was unloaded. + * @param nanoAppId the ID of the nanoapp that was unloaded. + */ + /* package */ void onNanoAppUnloaded(int contextHubId, long nanoAppId) { + forEachClientOfHub(contextHubId, client -> client.onNanoAppUnloaded(nanoAppId)); + } + /** * Handles a hub reset. * diff --git a/services/core/java/com/android/server/location/ContextHubService.java b/services/core/java/com/android/server/location/ContextHubService.java index f285eb24d19b2..e08c65967b32a 100644 --- a/services/core/java/com/android/server/location/ContextHubService.java +++ b/services/core/java/com/android/server/location/ContextHubService.java @@ -160,7 +160,7 @@ public class ContextHubService extends IContextHubService.Stub { } mClientManager = new ContextHubClientManager(mContext, mContextHubProxy); - mTransactionManager = new ContextHubTransactionManager(mContextHubProxy); + mTransactionManager = new ContextHubTransactionManager(mContextHubProxy, mClientManager); List hubList; try { diff --git a/services/core/java/com/android/server/location/ContextHubTransactionManager.java b/services/core/java/com/android/server/location/ContextHubTransactionManager.java index 898b76c9d8208..47d9d5686140a 100644 --- a/services/core/java/com/android/server/location/ContextHubTransactionManager.java +++ b/services/core/java/com/android/server/location/ContextHubTransactionManager.java @@ -57,6 +57,11 @@ import java.util.concurrent.atomic.AtomicInteger; */ private final IContexthub mContextHubProxy; + /* + * The manager for all clients for the service. + */ + private final ContextHubClientManager mClientManager; + /* * A queue containing the current transactions */ @@ -73,8 +78,10 @@ import java.util.concurrent.atomic.AtomicInteger; private final ScheduledThreadPoolExecutor mTimeoutExecutor = new ScheduledThreadPoolExecutor(1); private ScheduledFuture mTimeoutFuture = null; - /* package */ ContextHubTransactionManager(IContexthub contextHubProxy) { + /* package */ ContextHubTransactionManager( + IContexthub contextHubProxy, ContextHubClientManager clientManager) { mContextHubProxy = contextHubProxy; + mClientManager = clientManager; } /** @@ -113,6 +120,9 @@ import java.util.concurrent.atomic.AtomicInteger; /* package */ void onTransactionComplete(int result) { try { onCompleteCallback.onTransactionComplete(result); + if (result == Result.OK) { + mClientManager.onNanoAppLoaded(contextHubId, nanoAppBinary.getNanoAppId()); + } } catch (RemoteException e) { Log.e(TAG, "RemoteException while calling client onTransactionComplete"); } @@ -153,6 +163,9 @@ import java.util.concurrent.atomic.AtomicInteger; /* package */ void onTransactionComplete(int result) { try { onCompleteCallback.onTransactionComplete(result); + if (result == Result.OK) { + mClientManager.onNanoAppUnloaded(contextHubId, nanoAppId); + } } catch (RemoteException e) { Log.e(TAG, "RemoteException while calling client onTransactionComplete"); }